From 10f0beb6a8be10944ffddca8d14443415f245526 Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Thu, 25 Jul 2013 15:47:02 -0400 Subject: [PATCH] introduced static File::get that can operate on both FileList and TemplateList --- openbr/core/bee.cpp | 4 ++-- openbr/core/cluster.cpp | 2 +- openbr/openbr_plugin.cpp | 8 ++++---- openbr/openbr_plugin.h | 85 +++++++++++++++++++++++++++++++++++++++---------------------------------------------- openbr/plugins/eigen3.cpp | 2 +- openbr/plugins/independent.cpp | 2 +- openbr/plugins/output.cpp | 4 ++-- openbr/plugins/svm.cpp | 2 +- 8 files changed, 51 insertions(+), 58 deletions(-) diff --git a/openbr/core/bee.cpp b/openbr/core/bee.cpp index 18c498b..d9f320b 100644 --- a/openbr/core/bee.cpp +++ b/openbr/core/bee.cpp @@ -268,8 +268,8 @@ cv::Mat BEE::makeMask(const br::FileList &targets, const br::FileList &queries, { // Would like to use indexProperty for this, but didn't make a version of that for Filelist yet // -cao - QList targetLabels = targets.get("Subject", "-1"); - QList queryLabels = queries.get("Subject", "-1"); + QList targetLabels = File::get(targets, "Subject", "-1"); + QList queryLabels = File::get(queries, "Subject", "-1"); QList targetPartitions = targets.crossValidationPartitions(); QList queryPartitions = queries.crossValidationPartitions(); diff --git a/openbr/core/cluster.cpp b/openbr/core/cluster.cpp index fdff3d8..6a6b954 100644 --- a/openbr/core/cluster.cpp +++ b/openbr/core/cluster.cpp @@ -280,7 +280,7 @@ void br::EvalClustering(const QString &csv, const QString &input) // We assume clustering algorithms store assigned cluster labels as integers (since the clusters are // not named). - QList labels = TemplateList::fromGallery(input).files().get("Subject"); + QList labels = File::get(TemplateList::fromGallery(input), "Subject"); QHash labelToIndex; int nClusters = 0; diff --git a/openbr/openbr_plugin.cpp b/openbr/openbr_plugin.cpp index 5a4e89a..5f5fbb3 100644 --- a/openbr/openbr_plugin.cpp +++ b/openbr/openbr_plugin.cpp @@ -436,7 +436,7 @@ TemplateList TemplateList::fromGallery(const br::File &gallery) // stores the index values in "Label" of the output template list TemplateList TemplateList::relabel(const TemplateList &tl, const QString & propName) { - const QList originalLabels = tl.get(propName); + const QList originalLabels = File::get(tl, propName); QHash labelTable; foreach (const QString & label, originalLabels) if (!labelTable.contains(label)) @@ -464,7 +464,7 @@ QList TemplateList::indexProperty(const QString & propName, QHash originalLabels = values(propName); + const QList originalLabels = File::values(*this, propName); foreach (const QVariant & label, originalLabels) { QString labelString = label.toString(); if (!valueMap.contains(labelString)) { @@ -481,9 +481,9 @@ QList TemplateList::indexProperty(const QString & propName, QHash TemplateList::applyIndex(const QString & propName, const QHash & valueMap) const +QList TemplateList::applyIndex(const QString &propName, const QHash &valueMap) const { - const QList originalLabels = get(propName); + const QList originalLabels = File::get(*this, propName); QList result; for (int i=0; i(); } - /*!< \brief Returns a list of type T for the key, throwing an error if the key does not exist or if the value cannot be converted to the specified type. */ + /*!< \brief Returns a value for the key, returning \em defaultValue if the key does not exist or can't be converted. */ + template + T get(const QString &key, const T &defaultValue) const + { + if (!contains(key)) return defaultValue; + QVariant variant = value(key); + if (!variant.canConvert()) return defaultValue; + return variant.value(); + } + + /*!< \brief Specialization for boolean type. */ + bool getBool(const QString &key, bool defaultValue = false) const; + + /*!< \brief Specialization for list type. Returns a list of type T for the key, throwing an error if the key does not exist or if the value cannot be converted to the specified type. */ template QList getList(const QString &key) const { @@ -241,17 +254,31 @@ struct BR_EXPORT File return list; } - /*!< \brief Specialization for boolean type. */ - bool getBool(const QString &key, bool defaultValue = false) const; + /*!< \brief Returns the value for the specified key for every file in the list. */ + template + static QList values(const QList &fileList, const QString &key) + { + QList values; values.reserve(fileList.size()); + foreach (const U &f, fileList) values.append(((const File&)f).value(key)); + return values; + } - /*!< \brief Returns a value for the key, returning \em defaultValue if the key does not exist or can't be converted. */ - template - T get(const QString &key, const T &defaultValue) const + /*!< \brief Returns a value for the key for every file in the list, throwing an error if the key does not exist. */ + template + static QList get(const QList &fileList, const QString &key) { - if (!contains(key)) return defaultValue; - QVariant variant = value(key); - if (!variant.canConvert()) return defaultValue; - return variant.value(); + QList result; result.reserve(fileList.size()); + foreach (const U &f, fileList) result.append(((const File&)f).get(key)); + return result; + } + + /*!< \brief Returns a value for the key for every file in the list, returning \em defaultValue if the key does not exist or can't be converted. */ + template + static QList get(const QList &fileList, const QString &key, const T &defaultValue) + { + QList result; result.reserve(fileList.size()); + foreach (const U &f, fileList) result.append(((const File&)f).get(key, defaultValue)); + return result; } inline bool failed() const { return getBool("FTE") || getBool("FTO"); } /*!< \brief Returns \c true if the file failed to open or enroll, \c false otherwise. */ @@ -297,23 +324,6 @@ struct BR_EXPORT FileList : public QList QStringList flat() const; /*!< \brief Returns br::File::flat() for each file in the list. */ QStringList names() const; /*!< \brief Returns #br::File::name for each file in the list. */ void sort(const QString& key); /*!< \brief Sort the list based on metadata. */ - /*!< \brief Returns values associated with the input propName for each file in the list. */ - template - QList get(const QString & propName) const - { - QList values; values.reserve(size()); - foreach (const File &f, *this) - values.append(f.get(propName)); - return values; - } - template - QList get(const QString & propName, T defaultValue) const - { - QList values; values.reserve(size()); - foreach (const File &f, *this) - values.append(f.contains(propName) ? f.get(propName) : defaultValue); - return values; - } QList crossValidationPartitions() const; /*!< \brief Returns the cross-validation partition (default=0) for each file in the list. */ int failures() const; /*!< \brief Returns the number of files with br::File::failed(). */ @@ -344,6 +354,7 @@ struct Template : public QList inline const cv::Mat &m() const { static const cv::Mat NullMatrix; return isEmpty() ? qFatal("Empty template."), NullMatrix : last(); } /*!< \brief Idiom to treat the template as a matrix. */ inline cv::Mat &m() { return isEmpty() ? append(cv::Mat()), last() : last(); } /*!< \brief Idiom to treat the template as a matrix. */ + inline const File &operator()() const { return file; } inline cv::Mat &operator=(const cv::Mat &other) { return m() = other; } /*!< \brief Idiom to treat the template as a matrix. */ inline operator const cv::Mat&() const { return m(); } /*!< \brief Idiom to treat the template as a matrix. */ inline operator cv::Mat&() { return m(); } /*!< \brief Idiom to treat the template as a matrix. */ @@ -406,7 +417,6 @@ struct TemplateList : public QList