00001 #ifndef ActiveLearnEngineQuids_h
00002 #define ActiveLearnEngineQuids_h
00003
00004 #include "Basis/ILog.h"
00005 #include "Core/Table/SimilarityTableSet.h"
00006 #include "Util/Database.h"
00007 #include "Core/Trec/QuidResult.h"
00008
00009 #include "Basis/Timer.h"
00010
00011 namespace Impala {
00012
00013 namespace Samples {
00014
00015
00016
00017
00018 typedef Core::Table::TableTem<Core::Column::ColumnTem<Int32>, Core::Column::ColumnTem<double> > ShotWithWeight;
00019
00020 class ActiveLearnEngineQuids
00021 {
00022 public:
00023 typedef std::list<Core::Trec::QuidResult> QuidResultList;
00024
00025 ActiveLearnEngineQuids()
00026 {
00027 mAnnotations = new Core::Table::AnnotationTable();
00028 mTimer = new Timer();
00029 mTimer->Start();
00030 srand( time(NULL) );
00031 mInfoSent = false;
00032 mActiveID = "start";
00033 mErrCount = 0;
00034
00035 mALDatabase = new Util::Database("activelearning");
00036 mALDatabase->SetOverride(true);
00037 }
00038
00039 void
00040 PrepareNewID()
00041 {
00042 mActiveID = "al" + mTimer->SplitTimeStr();
00043 mInfoSent = false;
00044 }
00045
00046 std::string
00047 GetID()
00048 {
00049 return mActiveID;
00050 }
00051
00052 bool
00053 RequestSubmitted()
00054 {
00055 return mInfoSent;
00056 }
00057
00058 int
00059 GetErrCount()
00060 {
00061 return mErrCount;
00062 }
00063
00064 void
00065 PrepRequest()
00066 {
00067 if (mInfoSent)
00068 return;
00069
00070 PrepareNewID();
00071
00072 ILOG_DEBUG("SubmitRequest(), ID for submit = " << GetID());
00073
00074 mAnnotations->SetEmpty();
00075
00076 }
00077
00078
00079 void
00080 AddPositive(Quid q)
00081 {
00082 mAnnotations->AddPositive(q);
00083 }
00084
00085 void
00086 AddNegative(Quid q)
00087 {
00088 mAnnotations->AddNegative(q);
00089 }
00090
00091 void
00092 SubmitRequest()
00093 {
00094 if (mInfoSent)
00095 return;
00096
00097 if (mAnnotations->GetNrPositive() == 0)
00098 {
00099 ILOG_WARN("No positive examples, ALE request not submitted.");
00100 return;
00101 }
00102
00103 if (mAnnotations->GetNrNegative() == 0)
00104 {
00105 ILOG_WARN("No negative examples and random selection disabled, ALE request not submitted.");
00106 return;
00107 }
00108
00109 ILOG_DEBUG("Using table with " << mAnnotations->GetNrPositive() << " positive shots and " << mAnnotations->GetNrNegative() << " negative shots.");
00110
00111 String dir = "Annotations/" + QuidClassToString(QUID_CLASS_FRAME) + "/" + "conceptsActiveLearn.txt";
00112 String filename = mALDatabase->GetFilePath(dir, GetID() + ".tab", true, false);
00113
00114 ILOG_DEBUG("Writing to " << filename << "...");
00115
00116 Core::Table::Write(mAnnotations, filename, mALDatabase, true);
00117
00118 ILOG_DEBUG("Signalling active learning engine...");
00119
00120 String startlearnfile = mALDatabase->GetFilePath(dir, "startlearner.txt", true, false);
00121
00122 Util::IOBuffer *slf = mALDatabase->GetIOBuffer(
00123 startlearnfile, false, false, "tmp");
00124 if (!slf)
00125 {
00126 ILOG_WARN("No startlearnfile, ALE request not submitted.");
00127 return;
00128 }
00129 slf->IOBuffer::Puts(GetID());
00130 delete slf;
00131
00132 ILOG_DEBUG("Signal sent.");
00133 mInfoSent = true;
00134 }
00135
00136 bool
00137 HasResults()
00138 {
00139 ILOG_DEBUG("Locating results...");
00140
00141 String dir =
00142 "SimilarityIndex/conceptsActiveLearn.txt/activelearn/activelearn";
00143
00144 String path = mALDatabase->GetFilePath(dir, "names.txt", false, false);
00145 Core::Table::SimilarityTableSet *conceptSet =
00146 Core::Table::SimilarityTableSet::MakeFromFile(path, mALDatabase);
00147
00148 Core::Table::SimilarityTableSet::RankTableType* rank = conceptSet->GetRankTable(GetID());
00149
00150
00151
00152 if (!rank)
00153 {
00154 ILOG_DEBUG("ID " << GetID() << " not (yet) found.");
00155 delete conceptSet;
00156 return false;
00157 }
00158 delete conceptSet;
00159 ILOG_DEBUG("ID " << GetID() << " found!");
00160 return true;
00161 }
00162
00163 QuidResultList
00164 RetrieveResults()
00165 {
00166 ILOG_DEBUG("Retrieving results...");
00167
00168 String dir =
00169 "SimilarityIndex/conceptsActiveLearn.txt/activelearn/activelearn";
00170
00171 String path = mALDatabase->GetFilePath(dir, "names.txt", false, false);
00172
00173 Core::Table::SimilarityTableSet *conceptSet =
00174 Core::Table::SimilarityTableSet::MakeFromFile(path, mALDatabase);
00175
00176 Core::Table::SimilarityTableSet::RankTableType* rank = conceptSet->GetRankTable(GetID());
00177
00178 QuidResultList out;
00179
00180 if (!rank)
00181 {
00182 ILOG_DEBUG("Could not find ID, no data present. :'(");
00183 delete conceptSet;
00184 return out;
00185 }
00186
00187 mInfoSent = false;
00188
00189 int results = 5000;
00190
00191 ILOG_DEBUG("Concept loaded, converting to list...");
00192
00193
00194 std::map<int,bool> noduplicates;
00195 int rrank = 1;
00196 mErrCount = 0;
00197 int dropcount = 0;
00198 for (int i=0; i<results; i++)
00199 {
00200
00201 Quid q = rank->Get1(i);
00202
00203
00204
00205
00206
00207
00208 if (QuidSet(q) != 11 && QuidSet(q) != 12) {
00209 ILOG_ERROR("data not from trec2008devel or test set, dropping results.");
00210
00211
00212
00213
00214
00215 mErrCount++;
00216 mInfoSent = true;
00217 delete conceptSet;
00218 return out;
00219 }
00220
00221 if (q > -1)
00222 {
00223 Core::Trec::QuidResult r;
00224 r.quid = q;
00225 r.rank = rrank++;
00226 r.score = conceptSet->GetSimTable(GetID())->Get1(conceptSet->FindQuid(q));
00227 out.push_back(r);
00228 }
00229 }
00230
00231 if (dropcount > 0)
00232 ILOG_DEBUG("there were " << dropcount << " silent drops from results");
00233 if (mErrCount > 0)
00234 ILOG_DEBUG("there were " << mErrCount << " errors while receiving results");
00235
00236 delete conceptSet;
00237
00238 return out;
00239 }
00240
00241 bool
00242 Ready()
00243 {
00244
00245
00246 if (mAnnotations->GetNrPositive() == 0)
00247 {
00248 ILOG_WARN("No positive examples, ALE not ready.");
00249 return false;
00250 }
00251
00252 if (mAnnotations->GetNrNegative() == 0)
00253 {
00254 ILOG_WARN("No negative examples, ALE not ready.");
00255 return false;
00256 }
00257
00258
00259 return true;
00260 }
00261
00262 private:
00263
00264 std::string mActiveID;
00265 Core::Table::AnnotationTable *mAnnotations;
00266 Util::Database *mALDatabase;
00267 bool mInfoSent;
00268
00269 int mErrCount;
00270
00271 Timer* mTimer;
00272 ILOG_VAR_DEC;
00273
00274 };
00275 ILOG_VAR_INIT(ActiveLearnEngineQuids, Core.Trec);
00276
00277
00278 }
00279
00280 }
00281
00282
00283 #endif