00001 #ifndef Impala_Core_Feature_FeatureTableSet_h
00002 #define Impala_Core_Feature_FeatureTableSet_h
00003
00004 #include <vector>
00005 #include "Basis/StringList.h"
00006 #include "Core/Vector/SetPart.h"
00007 #include "Core/Column/Copy.h"
00008 #include "Core/Table/Append.h"
00009 #include "Core/Feature/FeatureTable.h"
00010 #include "Core/Database/RawDataSet.h"
00011
00012 namespace Impala
00013 {
00014 namespace Core
00015 {
00016 namespace Feature
00017 {
00018
00019
00020
00021
00022 class FeatureTableSet
00023 {
00024 public:
00025
00026 FeatureTableSet()
00027 {
00028 }
00029
00030 ~FeatureTableSet()
00031 {
00032
00033
00034
00035 }
00036
00037 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00038
00039 static FeatureTableSet*
00040 MakeFromFile(String dir, bool useName, String featureDefs,
00041 Database::RawDataSet* dataSet = 0)
00042 {
00043 FeatureTableSet* res = new FeatureTableSet();
00044 StringList defList(featureDefs, ';');
00045 for (StringListCI i=defList.begin() ; i!=defList.end() ; i++)
00046 {
00047 FeatureDefinition def(*i);
00048 String fName = dir;
00049 if (useName)
00050 fName += "/" + def.GetName();
00051 Util::Database* db = &Util::Database::GetInstance();
00052 String path;
00053 if (dataSet != 0)
00054 {
00055 db = dataSet->GetDatabase();
00056 path = dataSet->GetFilePath(fName, def.AsString() + ".tab",
00057 false, false);
00058 }
00059 if (!path.empty())
00060 fName = path;
00061 else
00062 fName += "/" + def.AsString() + ".tab";
00063 ILOG_INFO("reading " << fName);
00064 FeatureTable* table = FeatureTable::MakeFromFile(def, fName, db);
00065 res->Add(table);
00066 }
00067 return res;
00068 }
00069
00070
00071
00072 static FeatureTableSet*
00073 MakeFromFile(String walkType, bool keyframesIsObsolete, String featureDefs,
00074 Database::RawDataSet* dataSet, int fileId, bool partial,
00075 int numberFrames)
00076 {
00077 FeatureTableSet* res = new FeatureTableSet();
00078 StringList defList(featureDefs, ';');
00079 for (StringListCI i=defList.begin() ; i!=defList.end() ; i++)
00080 {
00081 FeatureDefinition def(*i);
00082 FeatureTable* table = 0;
00083 int startFr = 0;
00084 bool done = false;
00085 while (!done)
00086 {
00087 bool silent = partial && (startFr != 0);
00088 String fName = dataSet->GetFilePathFeatureData
00089 (walkType, def, fileId, partial, startFr, false, silent);
00090 if (!fName.empty())
00091 {
00092 ILOG_INFO("reading " << fName);
00093 Util::Database* db = dataSet->GetDatabase();
00094 if (table == 0)
00095 {
00096 table = FeatureTable::MakeFromFile(def, fName, db);
00097 }
00098 else
00099 {
00100 FeatureTable* t = FeatureTable::MakeFromFile(def, fName,
00101 db);
00102 Table::Append(table, t);
00103 delete t;
00104 }
00105 }
00106 else
00107 {
00108 done = true;
00109 }
00110 if (!partial)
00111 done = true;
00112 startFr += numberFrames;
00113 }
00114 if (table != 0)
00115 res->Add(table);
00116 }
00117 if (res->Size() == 0)
00118 {
00119 res->Delete();
00120 delete res;
00121 return 0;
00122 }
00123 return res;
00124 }
00125
00126
00127
00128 static FeatureTableSet*
00129 MakeFromFile(String featureDefs, Database::RawDataSet* dataSet,
00130 int dirId, bool partial, int numberFrames)
00131 {
00132 FeatureTableSet* res = new FeatureTableSet();
00133 StringList defList(featureDefs, ';');
00134 for (StringListCI i=defList.begin() ; i!=defList.end() ; i++)
00135 {
00136 FeatureDefinition def(*i);
00137 FeatureTable* table = 0;
00138 int startFr = (partial) ? 0 : -1;
00139 bool done = false;
00140 while (!done)
00141 {
00142 bool silent = partial && (startFr != 0);
00143 String fName = dataSet->GetFilePathFeatureData
00144 (def, dirId, partial, startFr, false, silent);
00145 if (!fName.empty())
00146 {
00147 ILOG_INFO("reading " << fName);
00148 Util::Database* db = dataSet->GetDatabase();
00149 if (table == 0)
00150 {
00151 table = FeatureTable::MakeFromFile(def, fName, db);
00152 }
00153 else
00154 {
00155 FeatureTable* t = FeatureTable::MakeFromFile(def, fName,
00156 db);
00157 Table::Append(table, t);
00158 delete t;
00159 }
00160 }
00161 else
00162 {
00163 done = true;
00164 }
00165 if (!partial)
00166 done = true;
00167 startFr += numberFrames;
00168 }
00169 res->Add(table);
00170 }
00171 return res;
00172 }
00173 #endif // REPOSITORY_USED
00174
00175 void
00176 Delete()
00177 {
00178 for (int i=0 ; i<mTables.size() ; i++)
00179 delete mTables[i];
00180 mTables.clear();
00181 }
00182
00183 int
00184 Size()
00185 {
00186 return mTables.size();
00187 }
00188
00189 int
00190 GetTotalFeatureVectorLength()
00191 {
00192 int res = 0;
00193 for (int i=0 ; i<Size() ; i++)
00194 {
00195 FeatureTable* table = GetTable(i);
00196 res += table->GetFeatureVectorLength();
00197 }
00198 return res;
00199 }
00200
00201
00202
00203 int
00204 CheckEqualSize()
00205 {
00206 int size = -1;
00207 for (int i=0 ; i<Size() ; i++)
00208 {
00209 FeatureTable* table = GetTable(i);
00210 if (size == -1)
00211 size = table->Size();
00212 else if (size != table->Size())
00213 return -1;
00214 }
00215 return size;
00216 }
00217
00218 void
00219 Add(FeatureTable* table)
00220 {
00221 mTables.push_back(table);
00222 }
00223
00224 FeatureDefinition
00225 GetFeatureDefinition(int i) const
00226 {
00227 return mTables[i]->GetFeatureDefinition();
00228 }
00229
00230 String
00231 GetDefinitionName(int i) const
00232 {
00233 return GetFeatureDefinition(i).GetName();
00234 }
00235
00236 String
00237 GetDefinitionAsString(int i) const
00238 {
00239 return GetFeatureDefinition(i).AsString();
00240 }
00241
00242 FeatureTable*
00243 GetTable(int i) const
00244 {
00245 return mTables[i];
00246 }
00247
00248 FeatureTable*
00249 GetTable(FeatureDefinition def) const
00250 {
00251 for (int i=0 ; i<mTables.size() ; i++)
00252 {
00253 if (GetFeatureDefinition(i) == def)
00254 return mTables[i];
00255 }
00256 return 0;
00257 }
00258
00259
00260 FeatureTable*
00261 GetTable(String name) const
00262 {
00263 for (int i=0 ; i<mTables.size() ; i++)
00264 if (GetDefinitionName(i) == name)
00265 return mTables[i];
00266 return 0;
00267 }
00268
00269
00270 template <class C>
00271 FeatureTable*
00272 GetTable(String name, C val)
00273 {
00274 String str = MakeString(val);
00275 for (int i=0 ; i<mTables.size() ; i++)
00276 {
00277 FeatureDefinition def = GetFeatureDefinition(i);
00278 if (def.GetParameterValue(name) == str)
00279 return mTables[i];
00280 }
00281 return 0;
00282 }
00283
00284
00285 template <class C>
00286 std::vector<FeatureTable*>
00287 GetTables(String name, C val)
00288 {
00289 std::vector<FeatureTable*> res;
00290 String str = MakeString(val);
00291 for (int i=0 ; i<mTables.size() ; i++)
00292 {
00293 FeatureDefinition def = GetFeatureDefinition(i);
00294 if (def.GetParameterValue(name) == str)
00295 {
00296 res.push_back(mTables[i]);
00297 }
00298 }
00299 return res;
00300 }
00301
00302
00303 template <class C1, class C2>
00304 FeatureTable*
00305 GetTable(String name1, C1 val1, String name2, C2 val2)
00306 {
00307 String str1 = MakeString(val1);
00308 String str2 = MakeString(val2);
00309 for (int i=0 ; i<mTables.size() ; i++)
00310 {
00311 FeatureDefinition def = GetFeatureDefinition(i);
00312 if (def.GetParameterValue(name1) == str1 &&
00313 def.GetParameterValue(name2) == str2)
00314 {
00315 return mTables[i];
00316 }
00317 }
00318 return 0;
00319 }
00320
00321
00322 template <class C1, class C2>
00323 std::vector<FeatureTable*>
00324 GetTables(String name1, C1 val1, String name2, C2 val2)
00325 {
00326 std::vector<FeatureTable*> res;
00327 String str1 = MakeString(val1);
00328 String str2 = MakeString(val2);
00329 for (int i=0 ; i<mTables.size() ; i++)
00330 {
00331 FeatureDefinition def = GetFeatureDefinition(i);
00332 if (def.GetParameterValue(name1) == str1 &&
00333 def.GetParameterValue(name2) == str2)
00334 {
00335 res.push_back(mTables[i]);
00336 }
00337 }
00338 return res;
00339 }
00340
00341
00342 template <class C1, class C2, class C3>
00343 FeatureTable*
00344 GetTable(String name1, C1 val1, String name2, C2 val2,
00345 String name3, C3 val3)
00346 {
00347 String str1 = MakeString(val1);
00348 String str2 = MakeString(val2);
00349 String str3 = MakeString(val3);
00350 for (int i=0 ; i<mTables.size() ; i++)
00351 {
00352 FeatureDefinition def = GetFeatureDefinition(i);
00353 if (def.GetParameterValue(name1) == str1 &&
00354 def.GetParameterValue(name2) == str2 &&
00355 def.GetParameterValue(name3) == str3)
00356 {
00357 return mTables[i];
00358 }
00359 }
00360 return 0;
00361 }
00362
00363 void
00364 SetEmpty()
00365 {
00366 for (int i=0 ; i<mTables.size() ; i++)
00367 mTables[i]->SetEmpty();
00368 }
00369
00370 FeatureTable*
00371 ConcatTables(FeatureDefinition resultDef)
00372 {
00373 int totalLength = GetTotalFeatureVectorLength();
00374 int size = CheckEqualSize();
00375 if (size == -1)
00376 {
00377 ILOG_ERROR("Tables do not have same size");
00378 return 0;
00379 }
00380 FeatureTable* res = new FeatureTable(resultDef, size, totalLength);
00381 int dstColumn = 0;
00382 for (int i=0 ; i<Size() ; i++)
00383 {
00384 FeatureTable* table = GetTable(i);
00385 if (i == 0)
00386 Column::Copy(res->GetColumn1(), table->GetColumn1(), size, 0, 0);
00387 Vector::SetPart(res->GetColumn2(), table->GetColumn2(), dstColumn);
00388 dstColumn += table->GetFeatureVectorLength();
00389 }
00390 res->SetSize(size);
00391 return res;
00392 }
00393
00394
00395 FeatureTable*
00396 ConcatTablesVissemHack(FeatureDefinition resultDef)
00397 {
00398 int totalLength = GetTotalFeatureVectorLength();
00399 int size = CheckEqualSize();
00400 if (size == -1)
00401 {
00402 ILOG_ERROR("Tables do not have same size");
00403 return 0;
00404 }
00405 FeatureTable* res = new FeatureTable(resultDef, size, totalLength);
00406 int dstColumn = 0;
00407 for (int col=0 ; col<totalLength ; col++)
00408 {
00409 int feat = col / 30;
00410 int j = col % 30;
00411 int agg = j / 15;
00412 int anno = j % 15;
00413 FeatureTable* table = GetTable(anno * 4 + feat);
00414 if (col == 0)
00415 Column::Copy(res->GetColumn1(), table->GetColumn1(), size, 0, 0);
00416 table->GetColumn2()->SetSize(size);
00417 Vector::SetPart(res->GetColumn2(), table->GetColumn2(), agg, 1, col);
00418 }
00419 res->SetSize(size);
00420 return res;
00421 }
00422
00423 void
00424 Dump(bool doTable)
00425 {
00426 for (int i=0 ; i<mTables.size() ; i++)
00427 {
00428 std::cout << "FeatureTable " << i
00429 << ", def = " << GetDefinitionAsString(i)
00430 << ", length = " << mTables[i]->GetFeatureVectorLength()
00431 << std::endl;
00432 if (doTable)
00433 GetTable(i)->Dump();
00434 }
00435 }
00436
00437 private:
00438
00439 std::vector<FeatureTable*> mTables;
00440
00441 ILOG_VAR_DEC;
00442 };
00443
00444 ILOG_VAR_INIT(FeatureTableSet, Impala.Core.Feature);
00445
00446 }
00447 }
00448 }
00449
00450 #endif