Commit 794f8fc1e3f2db102422274d5495a13c110c450f

Authored by Josh Klontz
1 parent 6e3e109f

removed db gallery

Showing 1 changed file with 0 additions and 190 deletions
openbr/plugins/gallery/db.cpp deleted
1 -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  
2 - * Copyright 2012 The MITRE Corporation *  
3 - * *  
4 - * Licensed under the Apache License, Version 2.0 (the "License"); *  
5 - * you may not use this file except in compliance with the License. *  
6 - * You may obtain a copy of the License at *  
7 - * *  
8 - * http://www.apache.org/licenses/LICENSE-2.0 *  
9 - * *  
10 - * Unless required by applicable law or agreed to in writing, software *  
11 - * distributed under the License is distributed on an "AS IS" BASIS, *  
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *  
13 - * See the License for the specific language governing permissions and *  
14 - * limitations under the License. *  
15 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */  
16 -  
17 -#include <QtSql>  
18 -  
19 -#include <openbr/plugins/openbr_internal.h>  
20 -#include <openbr/core/qtutils.h>  
21 -  
22 -namespace br  
23 -{  
24 -  
25 -/*!  
26 - * \ingroup galleries  
27 - * \brief Database input.  
28 - * \author Josh Klontz \cite jklontz  
29 - */  
30 -class dbGallery : public Gallery  
31 -{  
32 - Q_OBJECT  
33 -  
34 - TemplateList readBlock(bool *done)  
35 - {  
36 - TemplateList templates;  
37 - br::File import = file.get<QString>("import", "");  
38 - QString query = file.get<QString>("query");  
39 - QString subset = file.get<QString>("subset", "");  
40 -  
41 - QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  
42 - db.setDatabaseName(file);  
43 - if (!db.open()) qFatal("Failed to open SQLite database %s.", qPrintable(file.name));  
44 -  
45 - if (!import.isNull()) {  
46 - qDebug("Parsing %s", qPrintable(import.name));  
47 - QStringList lines = QtUtils::readLines(import);  
48 - QList<QStringList> cells; cells.reserve(lines.size());  
49 - const QRegExp re("\\s*,\\s*");  
50 - foreach (const QString &line, lines) {  
51 - cells.append(line.split(re));  
52 - if (cells.last().size() != cells.first().size()) qFatal("Column count mismatch.");  
53 - }  
54 -  
55 - QStringList columns, qMarks;  
56 - QList<QVariantList> variantLists;  
57 - for (int i=0; i<cells[0].size(); i++) {  
58 - bool isNumeric;  
59 - cells[1][i].toInt(&isNumeric);  
60 - columns.append(cells[0][i] + (isNumeric ? " INTEGER" : " STRING"));  
61 - qMarks.append("?");  
62 -  
63 - QVariantList variantList; variantList.reserve(lines.size()-1);  
64 - for (int j=1; j<lines.size(); j++) {  
65 - if (isNumeric) variantList << cells[j][i].toInt();  
66 - else variantList << cells[j][i];  
67 - }  
68 - variantLists.append(variantList);  
69 - }  
70 -  
71 - const QString &table = import.baseName();  
72 - qDebug("Creating table %s", qPrintable(table));  
73 - QSqlQuery q(db);  
74 - if (!q.exec("CREATE TABLE " + table + " (" + columns.join(", ") + ");"))  
75 - qFatal("%s.", qPrintable(q.lastError().text()));  
76 - if (!q.prepare("insert into " + table + " values (" + qMarks.join(", ") + ")"))  
77 - qFatal("%s.", qPrintable(q.lastError().text()));  
78 - foreach (const QVariantList &vl, variantLists)  
79 - q.addBindValue(vl);  
80 - if (!q.execBatch()) qFatal("%s.", qPrintable(q.lastError().text()));  
81 - }  
82 -  
83 - QSqlQuery q(db);  
84 - if (query.startsWith('\'') && query.endsWith('\''))  
85 - query = query.mid(1, query.size()-2);  
86 - if (!q.exec(query))  
87 - qFatal("%s.", qPrintable(q.lastError().text()));  
88 -  
89 - if ((q.record().count() == 0) || (q.record().count() > 3))  
90 - qFatal("Query record expected one to three fields, got %d.", q.record().count());  
91 - const bool hasMetadata = (q.record().count() >= 2);  
92 - const bool hasFilter = (q.record().count() >= 3);  
93 -  
94 - QString labelName = "Label";  
95 - if (q.record().count() >= 2)  
96 - labelName = q.record().fieldName(1);  
97 -  
98 - // subset = seed:subjectMaxSize:numSubjects:subjectMinSize or  
99 - // subset = seed:{Metadata,...,Metadata}:numSubjects  
100 - int seed = 0, subjectMaxSize = std::numeric_limits<int>::max(), numSubjects = std::numeric_limits<int>::max(), subjectMinSize = 0;  
101 - QList<QRegExp> metadataFields;  
102 - if (!subset.isEmpty()) {  
103 - const QStringList &words = subset.split(":");  
104 - QtUtils::checkArgsSize("Input", words, 2, 4);  
105 - if (words[0] == "train") seed = 0;  
106 - else if (words[0] == "test" ) seed = 1;  
107 - else seed = QtUtils::toInt(words[0]);  
108 - if (words[1].startsWith('{') && words[1].endsWith('}')) {  
109 - foreach (const QString &regexp, words[1].mid(1, words[1].size()-2).split(","))  
110 - metadataFields.append(QRegExp(regexp));  
111 - subjectMaxSize = metadataFields.size();  
112 - } else {  
113 - subjectMaxSize = QtUtils::toInt(words[1]);  
114 - }  
115 - numSubjects = words.size() >= 3 ? QtUtils::toInt(words[2]) : std::numeric_limits<int>::max();  
116 - subjectMinSize = words.size() >= 4 ? QtUtils::toInt(words[3]) : subjectMaxSize;  
117 - }  
118 -  
119 - srand(seed);  
120 -  
121 - typedef QPair<QString,QString> Entry; // QPair<File,Metadata>  
122 - QHash<QString, QList<Entry> > entries; // QHash<Label, QList<Entry> >  
123 - while (q.next()) {  
124 - if (hasFilter && (seed >= 0) && (qHash(q.value(2).toString()) % 2 != (uint)seed % 2)) continue; // Ensures training and testing filters don't overlap  
125 -  
126 - if (metadataFields.isEmpty())  
127 - entries[hasMetadata ? q.value(1).toString() : ""].append(QPair<QString,QString>(q.value(0).toString(), hasFilter ? q.value(2).toString() : ""));  
128 - else  
129 - entries[hasFilter ? q.value(2).toString() : ""].append(QPair<QString,QString>(q.value(0).toString(), hasMetadata ? q.value(1).toString() : ""));  
130 - }  
131 -  
132 - QStringList labels = entries.keys();  
133 - qSort(labels);  
134 -  
135 - if (hasFilter && ((labels.size() > numSubjects) || (numSubjects == std::numeric_limits<int>::max())))  
136 - std::random_shuffle(labels.begin(), labels.end());  
137 -  
138 - foreach (const QString &label, labels) {  
139 - QList<Entry> entryList = entries[label];  
140 - if ((entryList.size() >= subjectMinSize) && (numSubjects > 0)) {  
141 -  
142 - if (!metadataFields.isEmpty()) {  
143 - QList<Entry> subEntryList;  
144 - foreach (const QRegExp &metadata, metadataFields) {  
145 - for (int i=0; i<entryList.size(); i++) {  
146 - if (metadata.exactMatch(entryList[i].second)) {  
147 - subEntryList.append(entryList.takeAt(i));  
148 - break;  
149 - }  
150 - }  
151 - }  
152 - if (subEntryList.size() == metadataFields.size())  
153 - entryList = subEntryList;  
154 - else  
155 - continue;  
156 - }  
157 -  
158 - if (entryList.size() > subjectMaxSize)  
159 - std::random_shuffle(entryList.begin(), entryList.end());  
160 - foreach (const Entry &entry, entryList.mid(0, subjectMaxSize)) {  
161 - templates.append(File(entry.first));  
162 - templates.last().file.set(labelName, label);  
163 - }  
164 - numSubjects--;  
165 - }  
166 - }  
167 -  
168 - db.close();  
169 -  
170 - *done = true;  
171 - return templates;  
172 - }  
173 -  
174 - void write(const Template &t)  
175 - {  
176 - (void) t;  
177 - qFatal("Not supported.");  
178 - }  
179 -  
180 - void init()  
181 - {  
182 - //  
183 - }  
184 -};  
185 -  
186 -BR_REGISTER(Gallery, dbGallery)  
187 -  
188 -} // namespace br  
189 -  
190 -#include "gallery/db.moc"