annotate reporter.h @ 277:abfb26e08d9c audiodb-debian

Merge trunk changes -r326:386 into audiodb-debian branch. Plus new debian/changelog version. (Should have used an epoch really, but couldn't be bothered; TODO: work out a sane version numbering policy).
author mas01cr
date Tue, 01 Jul 2008 09:12:40 +0000
parents cbf51690c78c
children
rev   line source
mas01cr@243 1 #include <utility>
mas01cr@243 2 #include <queue>
mas01cr@277 3 #include <deque>
mas01cr@243 4 #include <set>
mas01cr@243 5 #include <functional>
mas01cr@243 6
mas01cr@243 7 typedef struct nnresult {
mas01cr@243 8 unsigned int trackID;
mas01cr@243 9 double dist;
mas01cr@243 10 unsigned int qpos;
mas01cr@243 11 unsigned int spos;
mas01cr@243 12 } NNresult;
mas01cr@243 13
mas01cr@243 14 typedef struct radresult {
mas01cr@243 15 unsigned int trackID;
mas01cr@243 16 unsigned int count;
mas01cr@243 17 } Radresult;
mas01cr@243 18
mas01cr@243 19 bool operator< (const NNresult &a, const NNresult &b) {
mas01cr@243 20 return a.dist < b.dist;
mas01cr@243 21 }
mas01cr@243 22
mas01cr@243 23 bool operator> (const NNresult &a, const NNresult &b) {
mas01cr@243 24 return a.dist > b.dist;
mas01cr@243 25 }
mas01cr@243 26
mas01cr@243 27 bool operator< (const Radresult &a, const Radresult &b) {
mas01cr@243 28 return a.count < b.count;
mas01cr@243 29 }
mas01cr@243 30
mas01cr@243 31 class Reporter {
mas01cr@243 32 public:
mas01cr@243 33 virtual ~Reporter() {};
mas01cr@243 34 virtual void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist) = 0;
mas01cr@243 35 // FIXME: this interface is a bit wacky: a relic of previous, more
mas01cr@243 36 // confused times. Really it might make sense to have separate
mas01cr@243 37 // reporter classes for WS and for stdout, rather than passing this
mas01cr@243 38 // adbQueryResponse thing everywhere; the fileTable argument is
mas01cr@243 39 // there solely for convertion trackIDs into names. -- CSR,
mas01cr@243 40 // 2007-12-10.
mas01cr@243 41 virtual void report(char *fileTable, adb__queryResponse *adbQueryResponse) = 0;
mas01cr@243 42 };
mas01cr@243 43
mas01cr@243 44 template <class T> class pointQueryReporter : public Reporter {
mas01cr@243 45 public:
mas01cr@243 46 pointQueryReporter(unsigned int pointNN);
mas01cr@243 47 ~pointQueryReporter();
mas01cr@243 48 void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
mas01cr@243 49 void report(char *fileTable, adb__queryResponse *adbQueryResponse);
mas01cr@243 50 private:
mas01cr@243 51 unsigned int pointNN;
mas01cr@243 52 std::priority_queue< NNresult, std::vector< NNresult >, T> *queue;
mas01cr@243 53 };
mas01cr@243 54
mas01cr@243 55 template <class T> pointQueryReporter<T>::pointQueryReporter(unsigned int pointNN)
mas01cr@243 56 : pointNN(pointNN) {
mas01cr@243 57 queue = new std::priority_queue< NNresult, std::vector< NNresult >, T>;
mas01cr@243 58 }
mas01cr@243 59
mas01cr@243 60 template <class T> pointQueryReporter<T>::~pointQueryReporter() {
mas01cr@243 61 delete queue;
mas01cr@243 62 }
mas01cr@243 63
mas01cr@243 64 template <class T> void pointQueryReporter<T>::add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist) {
mas01cr@246 65 if (!isnan(dist)) {
mas01cr@246 66 NNresult r;
mas01cr@246 67 r.trackID = trackID;
mas01cr@246 68 r.qpos = qpos;
mas01cr@246 69 r.spos = spos;
mas01cr@246 70 r.dist = dist;
mas01cr@246 71 queue->push(r);
mas01cr@246 72 if(queue->size() > pointNN) {
mas01cr@246 73 queue->pop();
mas01cr@246 74 }
mas01cr@243 75 }
mas01cr@243 76 }
mas01cr@243 77
mas01cr@243 78 template <class T> void pointQueryReporter<T>::report(char *fileTable, adb__queryResponse *adbQueryResponse) {
mas01cr@243 79 NNresult r;
mas01cr@243 80 std::vector<NNresult> v;
mas01cr@243 81 unsigned int size = queue->size();
mas01cr@243 82 for(unsigned int k = 0; k < size; k++) {
mas01cr@243 83 r = queue->top();
mas01cr@243 84 v.push_back(r);
mas01cr@243 85 queue->pop();
mas01cr@243 86 }
mas01cr@243 87 std::vector<NNresult>::reverse_iterator rit;
mas01cr@243 88
mas01cr@243 89 if(adbQueryResponse==0) {
mas01cr@243 90 for(rit = v.rbegin(); rit < v.rend(); rit++) {
mas01cr@243 91 r = *rit;
mas01cr@277 92 std::cout << fileTable + r.trackID*O2_FILETABLE_ENTRY_SIZE << " ";
mas01cr@243 93 std::cout << r.dist << " " << r.qpos << " " << r.spos << std::endl;
mas01cr@243 94 }
mas01cr@243 95 } else {
mas01cr@243 96 adbQueryResponse->result.__sizeRlist=size;
mas01cr@243 97 adbQueryResponse->result.__sizeDist=size;
mas01cr@243 98 adbQueryResponse->result.__sizeQpos=size;
mas01cr@243 99 adbQueryResponse->result.__sizeSpos=size;
mas01cr@243 100 adbQueryResponse->result.Rlist= new char*[size];
mas01cr@243 101 adbQueryResponse->result.Dist = new double[size];
mas01cr@243 102 adbQueryResponse->result.Qpos = new unsigned int[size];
mas01cr@243 103 adbQueryResponse->result.Spos = new unsigned int[size];
mas01cr@243 104 unsigned int k = 0;
mas01cr@243 105 for(rit = v.rbegin(); rit < v.rend(); rit++, k++) {
mas01cr@243 106 r = *rit;
mas01cr@243 107 adbQueryResponse->result.Rlist[k] = new char[O2_MAXFILESTR];
mas01cr@243 108 adbQueryResponse->result.Dist[k] = r.dist;
mas01cr@243 109 adbQueryResponse->result.Qpos[k] = r.qpos;
mas01cr@243 110 adbQueryResponse->result.Spos[k] = r.spos;
mas01cr@277 111 snprintf(adbQueryResponse->result.Rlist[k], O2_MAXFILESTR, "%s", fileTable+r.trackID*O2_FILETABLE_ENTRY_SIZE);
mas01cr@243 112 }
mas01cr@243 113 }
mas01cr@243 114 }
mas01cr@243 115
mas01cr@243 116 template <class T> class trackAveragingReporter : public Reporter {
mas01cr@243 117 public:
mas01cr@243 118 trackAveragingReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
mas01cr@243 119 ~trackAveragingReporter();
mas01cr@243 120 void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
mas01cr@243 121 void report(char *fileTable, adb__queryResponse *adbQueryResponse);
mas01cr@277 122 protected:
mas01cr@243 123 unsigned int pointNN;
mas01cr@243 124 unsigned int trackNN;
mas01cr@243 125 unsigned int numFiles;
mas01cr@243 126 std::priority_queue< NNresult, std::vector< NNresult>, T > *queues;
mas01cr@243 127 };
mas01cr@243 128
mas01cr@243 129 template <class T> trackAveragingReporter<T>::trackAveragingReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles)
mas01cr@243 130 : pointNN(pointNN), trackNN(trackNN), numFiles(numFiles) {
mas01cr@243 131 queues = new std::priority_queue< NNresult, std::vector< NNresult>, T >[numFiles];
mas01cr@243 132 }
mas01cr@243 133
mas01cr@243 134 template <class T> trackAveragingReporter<T>::~trackAveragingReporter() {
mas01cr@243 135 delete [] queues;
mas01cr@243 136 }
mas01cr@243 137
mas01cr@243 138 template <class T> void trackAveragingReporter<T>::add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist) {
mas01cr@246 139 if (!isnan(dist)) {
mas01cr@246 140 NNresult r;
mas01cr@246 141 r.trackID = trackID;
mas01cr@246 142 r.qpos = qpos;
mas01cr@246 143 r.spos = spos;
mas01cr@246 144 r.dist = dist;
mas01cr@246 145 queues[trackID].push(r);
mas01cr@246 146 if(queues[trackID].size() > pointNN) {
mas01cr@246 147 queues[trackID].pop();
mas01cr@246 148 }
mas01cr@243 149 }
mas01cr@243 150 }
mas01cr@243 151
mas01cr@243 152 template <class T> void trackAveragingReporter<T>::report(char *fileTable, adb__queryResponse *adbQueryResponse) {
mas01cr@243 153 std::priority_queue < NNresult, std::vector< NNresult>, T> result;
mas01cr@243 154 for (int i = numFiles-1; i >= 0; i--) {
mas01cr@243 155 unsigned int size = queues[i].size();
mas01cr@243 156 if (size > 0) {
mas01cr@243 157 NNresult r;
mas01cr@243 158 double dist = 0;
mas01cr@243 159 NNresult oldr = queues[i].top();
mas01cr@243 160 for (unsigned int j = 0; j < size; j++) {
mas01cr@243 161 r = queues[i].top();
mas01cr@243 162 dist += r.dist;
mas01cr@243 163 queues[i].pop();
mas01cr@243 164 if (r.dist == oldr.dist) {
mas01cr@243 165 r.qpos = oldr.qpos;
mas01cr@243 166 r.spos = oldr.spos;
mas01cr@243 167 } else {
mas01cr@243 168 oldr = r;
mas01cr@243 169 }
mas01cr@243 170 }
mas01cr@243 171 dist /= size;
mas01cr@243 172 r.dist = dist; // trackID, qpos and spos are magically right already.
mas01cr@243 173 result.push(r);
mas01cr@243 174 if (result.size() > trackNN) {
mas01cr@243 175 result.pop();
mas01cr@243 176 }
mas01cr@243 177 }
mas01cr@243 178 }
mas01cr@243 179
mas01cr@243 180 NNresult r;
mas01cr@243 181 std::vector<NNresult> v;
mas01cr@243 182 unsigned int size = result.size();
mas01cr@243 183 for(unsigned int k = 0; k < size; k++) {
mas01cr@243 184 r = result.top();
mas01cr@243 185 v.push_back(r);
mas01cr@243 186 result.pop();
mas01cr@243 187 }
mas01cr@243 188 std::vector<NNresult>::reverse_iterator rit;
mas01cr@243 189
mas01cr@243 190 if(adbQueryResponse==0) {
mas01cr@243 191 for(rit = v.rbegin(); rit < v.rend(); rit++) {
mas01cr@243 192 r = *rit;
mas01cr@277 193 std::cout << fileTable + r.trackID*O2_FILETABLE_ENTRY_SIZE << " ";
mas01cr@243 194 std::cout << r.dist << " " << r.qpos << " " << r.spos << std::endl;
mas01cr@243 195 }
mas01cr@243 196 } else {
mas01cr@243 197 adbQueryResponse->result.__sizeRlist=size;
mas01cr@243 198 adbQueryResponse->result.__sizeDist=size;
mas01cr@243 199 adbQueryResponse->result.__sizeQpos=size;
mas01cr@243 200 adbQueryResponse->result.__sizeSpos=size;
mas01cr@243 201 adbQueryResponse->result.Rlist= new char*[size];
mas01cr@243 202 adbQueryResponse->result.Dist = new double[size];
mas01cr@243 203 adbQueryResponse->result.Qpos = new unsigned int[size];
mas01cr@243 204 adbQueryResponse->result.Spos = new unsigned int[size];
mas01cr@243 205 unsigned int k = 0;
mas01cr@243 206 for(rit = v.rbegin(); rit < v.rend(); rit++, k++) {
mas01cr@243 207 r = *rit;
mas01cr@243 208 adbQueryResponse->result.Rlist[k] = new char[O2_MAXFILESTR];
mas01cr@243 209 adbQueryResponse->result.Dist[k] = r.dist;
mas01cr@243 210 adbQueryResponse->result.Qpos[k] = r.qpos;
mas01cr@243 211 adbQueryResponse->result.Spos[k] = r.spos;
mas01cr@277 212 snprintf(adbQueryResponse->result.Rlist[k], O2_MAXFILESTR, "%s", fileTable+r.trackID*O2_FILETABLE_ENTRY_SIZE);
mas01cr@243 213 }
mas01cr@243 214 }
mas01cr@243 215 }
mas01cr@243 216
mas01cr@277 217 // track Sequence Query Radius Reporter
mas01cr@277 218 // only return tracks and retrieved point counts
mas01cr@243 219 class trackSequenceQueryRadReporter : public Reporter {
mas01cr@243 220 public:
mas01cr@243 221 trackSequenceQueryRadReporter(unsigned int trackNN, unsigned int numFiles);
mas01cr@243 222 ~trackSequenceQueryRadReporter();
mas01cr@243 223 void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
mas01cr@243 224 void report(char *fileTable, adb__queryResponse *adbQueryResponse);
mas01cr@277 225 protected:
mas01cr@243 226 unsigned int trackNN;
mas01cr@243 227 unsigned int numFiles;
mas01cr@243 228 std::set<std::pair<unsigned int, unsigned int> > *set;
mas01cr@243 229 unsigned int *count;
mas01cr@243 230 };
mas01cr@243 231
mas01cr@243 232 trackSequenceQueryRadReporter::trackSequenceQueryRadReporter(unsigned int trackNN, unsigned int numFiles):
mas01cr@243 233 trackNN(trackNN), numFiles(numFiles) {
mas01cr@243 234 set = new std::set<std::pair<unsigned int, unsigned int> >;
mas01cr@243 235 count = new unsigned int[numFiles];
mas01cr@243 236 for (unsigned i = 0; i < numFiles; i++) {
mas01cr@243 237 count[i] = 0;
mas01cr@243 238 }
mas01cr@243 239 }
mas01cr@243 240
mas01cr@243 241 trackSequenceQueryRadReporter::~trackSequenceQueryRadReporter() {
mas01cr@243 242 delete set;
mas01cr@243 243 delete [] count;
mas01cr@243 244 }
mas01cr@243 245
mas01cr@243 246 void trackSequenceQueryRadReporter::add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist) {
mas01cr@243 247 std::set<std::pair<unsigned int, unsigned int> >::iterator it;
mas01cr@277 248 std::pair<unsigned int, unsigned int> pair = std::make_pair(trackID, qpos); // only count this once
mas01cr@243 249 it = set->find(pair);
mas01cr@243 250 if (it == set->end()) {
mas01cr@243 251 set->insert(pair);
mas01cr@277 252 count[trackID]++; // only count if <tackID,qpos> pair is unique
mas01cr@243 253 }
mas01cr@243 254 }
mas01cr@243 255
mas01cr@243 256 void trackSequenceQueryRadReporter::report(char *fileTable, adb__queryResponse *adbQueryResponse) {
mas01cr@243 257 std::priority_queue < Radresult > result;
mas01cr@243 258 // KLUDGE: doing this backwards in an attempt to get the same
mas01cr@243 259 // tiebreak behaviour as before.
mas01cr@243 260 for (int i = numFiles-1; i >= 0; i--) {
mas01cr@243 261 Radresult r;
mas01cr@243 262 r.trackID = i;
mas01cr@243 263 r.count = count[i];
mas01cr@243 264 if(r.count > 0) {
mas01cr@243 265 result.push(r);
mas01cr@243 266 if (result.size() > trackNN) {
mas01cr@243 267 result.pop();
mas01cr@243 268 }
mas01cr@243 269 }
mas01cr@243 270 }
mas01cr@243 271
mas01cr@243 272 Radresult r;
mas01cr@243 273 std::vector<Radresult> v;
mas01cr@243 274 unsigned int size = result.size();
mas01cr@243 275 for(unsigned int k = 0; k < size; k++) {
mas01cr@243 276 r = result.top();
mas01cr@243 277 v.push_back(r);
mas01cr@243 278 result.pop();
mas01cr@243 279 }
mas01cr@243 280 std::vector<Radresult>::reverse_iterator rit;
mas01cr@243 281
mas01cr@243 282 if(adbQueryResponse==0) {
mas01cr@243 283 for(rit = v.rbegin(); rit < v.rend(); rit++) {
mas01cr@243 284 r = *rit;
mas01cr@277 285 std::cout << fileTable + r.trackID*O2_FILETABLE_ENTRY_SIZE << " " << r.count << std::endl;
mas01cr@243 286 }
mas01cr@243 287 } else {
mas01cr@243 288 // FIXME
mas01cr@243 289 }
mas01cr@243 290 }
mas01cr@277 291
mas01cr@277 292 // Another type of trackAveragingReporter that reports all pointNN nearest neighbours
mas01cr@277 293 template <class T> class trackSequenceQueryNNReporter : public trackAveragingReporter<T> {
mas01cr@277 294 protected:
mas01cr@277 295 using trackAveragingReporter<T>::numFiles;
mas01cr@277 296 using trackAveragingReporter<T>::queues;
mas01cr@277 297 using trackAveragingReporter<T>::trackNN;
mas01cr@277 298 using trackAveragingReporter<T>::pointNN;
mas01cr@277 299 public:
mas01cr@277 300 trackSequenceQueryNNReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
mas01cr@277 301 void report(char *fileTable, adb__queryResponse *adbQueryResponse);
mas01cr@277 302 };
mas01cr@277 303
mas01cr@277 304 template <class T> trackSequenceQueryNNReporter<T>::trackSequenceQueryNNReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles)
mas01cr@277 305 :trackAveragingReporter<T>(pointNN, trackNN, numFiles){}
mas01cr@277 306
mas01cr@277 307 template <class T> void trackSequenceQueryNNReporter<T>::report(char *fileTable, adb__queryResponse *adbQueryResponse) {
mas01cr@277 308 std::priority_queue < NNresult, std::vector< NNresult>, T> result;
mas01cr@277 309 std::priority_queue< NNresult, std::vector< NNresult>, std::less<NNresult> > *point_queues = new std::priority_queue< NNresult, std::vector< NNresult>, std::less<NNresult> >[numFiles];
mas01cr@277 310
mas01cr@277 311 for (int i = numFiles-1; i >= 0; i--) {
mas01cr@277 312 unsigned int size = queues[i].size();
mas01cr@277 313 if (size > 0) {
mas01cr@277 314 NNresult r;
mas01cr@277 315 double dist = 0;
mas01cr@277 316 NNresult oldr = queues[i].top();
mas01cr@277 317 for (unsigned int j = 0; j < size; j++) {
mas01cr@277 318 r = queues[i].top();
mas01cr@277 319 dist += r.dist;
mas01cr@277 320 point_queues[i].push(r);
mas01cr@277 321 queues[i].pop();
mas01cr@277 322 if (r.dist == oldr.dist) {
mas01cr@277 323 r.qpos = oldr.qpos;
mas01cr@277 324 r.spos = oldr.spos;
mas01cr@277 325 } else {
mas01cr@277 326 oldr = r;
mas01cr@277 327 }
mas01cr@277 328 }
mas01cr@277 329 dist /= size;
mas01cr@277 330 r.dist = dist; // trackID, qpos and spos are magically right already.
mas01cr@277 331 result.push(r);
mas01cr@277 332 if (result.size() > trackNN) {
mas01cr@277 333 result.pop();
mas01cr@277 334 }
mas01cr@277 335 }
mas01cr@277 336 }
mas01cr@277 337
mas01cr@277 338 NNresult r;
mas01cr@277 339 std::vector<NNresult> v;
mas01cr@277 340 unsigned int size = result.size();
mas01cr@277 341 for(unsigned int k = 0; k < size; k++) {
mas01cr@277 342 r = result.top();
mas01cr@277 343 v.push_back(r);
mas01cr@277 344 result.pop();
mas01cr@277 345 }
mas01cr@277 346 std::vector<NNresult>::reverse_iterator rit;
mas01cr@277 347 std::priority_queue< NNresult, std::vector< NNresult>, std::greater<NNresult> > point_queue;
mas01cr@277 348
mas01cr@277 349 if(adbQueryResponse==0) {
mas01cr@277 350 for(rit = v.rbegin(); rit < v.rend(); rit++) {
mas01cr@277 351 r = *rit;
mas01cr@277 352 std::cout << fileTable + r.trackID*O2_FILETABLE_ENTRY_SIZE << " " << r.dist << std::endl;
mas01cr@277 353 unsigned int qsize = point_queues[r.trackID].size();
mas01cr@277 354 // Reverse the order of the points stored in point_queues
mas01cr@277 355 for(unsigned int k=0; k < qsize; k++){
mas01cr@277 356 point_queue.push( point_queues[r.trackID].top() );
mas01cr@277 357 point_queues[r.trackID].pop();
mas01cr@277 358 }
mas01cr@277 359
mas01cr@277 360 for(unsigned int k = 0; k < qsize; k++) {
mas01cr@277 361 NNresult rk = point_queue.top();
mas01cr@277 362 std::cout << rk.dist << " " << rk.qpos << " " << rk.spos << std::endl;
mas01cr@277 363 point_queue.pop();
mas01cr@277 364 }
mas01cr@277 365 }
mas01cr@277 366 } else {
mas01cr@277 367 adbQueryResponse->result.__sizeRlist=size;
mas01cr@277 368 adbQueryResponse->result.__sizeDist=size;
mas01cr@277 369 adbQueryResponse->result.__sizeQpos=size;
mas01cr@277 370 adbQueryResponse->result.__sizeSpos=size;
mas01cr@277 371 adbQueryResponse->result.Rlist= new char*[size];
mas01cr@277 372 adbQueryResponse->result.Dist = new double[size];
mas01cr@277 373 adbQueryResponse->result.Qpos = new unsigned int[size];
mas01cr@277 374 adbQueryResponse->result.Spos = new unsigned int[size];
mas01cr@277 375 unsigned int k = 0;
mas01cr@277 376 for(rit = v.rbegin(); rit < v.rend(); rit++, k++) {
mas01cr@277 377 r = *rit;
mas01cr@277 378 adbQueryResponse->result.Rlist[k] = new char[O2_MAXFILESTR];
mas01cr@277 379 adbQueryResponse->result.Dist[k] = r.dist;
mas01cr@277 380 adbQueryResponse->result.Qpos[k] = r.qpos;
mas01cr@277 381 adbQueryResponse->result.Spos[k] = r.spos;
mas01cr@277 382 snprintf(adbQueryResponse->result.Rlist[k], O2_MAXFILESTR, "%s", fileTable+r.trackID*O2_FILETABLE_ENTRY_SIZE);
mas01cr@277 383 }
mas01cr@277 384 }
mas01cr@277 385 // clean up
mas01cr@277 386 delete[] point_queues;
mas01cr@277 387 }
mas01cr@277 388
mas01cr@277 389
mas01cr@277 390 // track Sequence Query Radius NN Reporter
mas01cr@277 391 // retrieve tracks ordered by query-point matches (one per track per query point)
mas01cr@277 392 //
mas01cr@277 393 // as well as sorted n-NN points per retrieved track
mas01cr@277 394 class trackSequenceQueryRadNNReporter : public Reporter {
mas01cr@277 395 public:
mas01cr@277 396 trackSequenceQueryRadNNReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
mas01cr@277 397 ~trackSequenceQueryRadNNReporter();
mas01cr@277 398 void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
mas01cr@277 399 void report(char *fileTable, adb__queryResponse *adbQueryResponse);
mas01cr@277 400 protected:
mas01cr@277 401 unsigned int pointNN;
mas01cr@277 402 unsigned int trackNN;
mas01cr@277 403 unsigned int numFiles;
mas01cr@277 404 std::set< NNresult > *set;
mas01cr@277 405 std::priority_queue< NNresult, std::vector< NNresult>, std::less<NNresult> > *point_queues;
mas01cr@277 406 unsigned int *count;
mas01cr@277 407 };
mas01cr@277 408
mas01cr@277 409 trackSequenceQueryRadNNReporter::trackSequenceQueryRadNNReporter(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles):
mas01cr@277 410 pointNN(pointNN), trackNN(trackNN), numFiles(numFiles) {
mas01cr@277 411 // Where to count Radius track matches (one-to-one)
mas01cr@277 412 set = new std::set< NNresult >;
mas01cr@277 413 // Where to insert individual point matches (one-to-many)
mas01cr@277 414 point_queues = new std::priority_queue< NNresult, std::vector< NNresult>, std::less<NNresult> >[numFiles];
mas01cr@277 415
mas01cr@277 416 count = new unsigned int[numFiles];
mas01cr@277 417 for (unsigned i = 0; i < numFiles; i++) {
mas01cr@277 418 count[i] = 0;
mas01cr@277 419 }
mas01cr@277 420 }
mas01cr@277 421
mas01cr@277 422 trackSequenceQueryRadNNReporter::~trackSequenceQueryRadNNReporter() {
mas01cr@277 423 delete set;
mas01cr@277 424 delete [] count;
mas01cr@277 425 }
mas01cr@277 426
mas01cr@277 427 void trackSequenceQueryRadNNReporter::add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist) {
mas01cr@277 428 std::set< NNresult >::iterator it;
mas01cr@277 429 NNresult r;
mas01cr@277 430 r.trackID = trackID;
mas01cr@277 431 r.qpos = qpos;
mas01cr@277 432 r.dist = dist;
mas01cr@277 433 r.spos = spos;
mas01cr@277 434
mas01cr@277 435 // Record all matching points (within radius)
mas01cr@277 436 if (!isnan(dist)) {
mas01cr@277 437 point_queues[trackID].push(r);
mas01cr@277 438 if(point_queues[trackID].size() > pointNN)
mas01cr@277 439 point_queues[trackID].pop();
mas01cr@277 440 }
mas01cr@277 441
mas01cr@277 442 // Record counts of <trackID,qpos> pairs
mas01cr@277 443 it = set->find(r);
mas01cr@277 444 if (it == set->end()) {
mas01cr@277 445 set->insert(r);
mas01cr@277 446 count[trackID]++;
mas01cr@277 447 }
mas01cr@277 448 }
mas01cr@277 449
mas01cr@277 450 void trackSequenceQueryRadNNReporter::report(char *fileTable, adb__queryResponse *adbQueryResponse) {
mas01cr@277 451 std::priority_queue < Radresult > result;
mas01cr@277 452 // KLUDGE: doing this backwards in an attempt to get the same
mas01cr@277 453 // tiebreak behaviour as before.
mas01cr@277 454 for (int i = numFiles-1; i >= 0; i--) {
mas01cr@277 455 Radresult r;
mas01cr@277 456 r.trackID = i;
mas01cr@277 457 r.count = count[i];
mas01cr@277 458 if(r.count > 0) {
mas01cr@277 459 result.push(r);
mas01cr@277 460 if (result.size() > trackNN) {
mas01cr@277 461 result.pop();
mas01cr@277 462 }
mas01cr@277 463 }
mas01cr@277 464 }
mas01cr@277 465
mas01cr@277 466 Radresult r;
mas01cr@277 467 std::vector<Radresult> v;
mas01cr@277 468 unsigned int size = result.size();
mas01cr@277 469 for(unsigned int k = 0; k < size; k++) {
mas01cr@277 470 r = result.top();
mas01cr@277 471 v.push_back(r);
mas01cr@277 472 result.pop();
mas01cr@277 473 }
mas01cr@277 474
mas01cr@277 475
mas01cr@277 476 // Traverse tracks in descending order of count cardinality
mas01cr@277 477 std::vector<Radresult>::reverse_iterator rit;
mas01cr@277 478 std::priority_queue< NNresult, std::vector< NNresult>, std::greater<NNresult> > point_queue;
mas01cr@277 479
mas01cr@277 480 if(adbQueryResponse==0) {
mas01cr@277 481 for(rit = v.rbegin(); rit < v.rend(); rit++) {
mas01cr@277 482 r = *rit;
mas01cr@277 483 std::cout << fileTable + r.trackID*O2_FILETABLE_ENTRY_SIZE << " " << r.count << std::endl;
mas01cr@277 484
mas01cr@277 485 // Reverse the order of the points stored in point_queues
mas01cr@277 486 unsigned int qsize=point_queues[r.trackID].size();
mas01cr@277 487 for(unsigned int k=0; k < qsize; k++){
mas01cr@277 488 point_queue.push(point_queues[r.trackID].top());
mas01cr@277 489 point_queues[r.trackID].pop();
mas01cr@277 490 }
mas01cr@277 491
mas01cr@277 492 for(unsigned int k=0; k < qsize; k++){
mas01cr@277 493 NNresult rk = point_queue.top();
mas01cr@277 494 std::cout << rk.dist << " " << rk.qpos << " " << rk.spos << std::endl;
mas01cr@277 495 point_queue.pop();
mas01cr@277 496 }
mas01cr@277 497 }
mas01cr@277 498 } else {
mas01cr@277 499 // FIXME
mas01cr@277 500 }
mas01cr@277 501 delete[] point_queues;
mas01cr@277 502 }
mas01cr@277 503
mas01cr@277 504
mas01cr@277 505 /********** ONE-TO-ONE REPORTERS *****************/
mas01cr@277 506
mas01cr@277 507 // track Sequence Query Radius NN Reporter One-to-One
mas01cr@277 508 // for each query point find the single best matching target point in all database
mas01cr@277 509 // report qpos, spos and trackID
mas01cr@277 510 class trackSequenceQueryRadNNReporterOneToOne : public Reporter {
mas01cr@277 511 public:
mas01cr@277 512 trackSequenceQueryRadNNReporterOneToOne(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles);
mas01cr@277 513 ~trackSequenceQueryRadNNReporterOneToOne();
mas01cr@277 514 void add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist);
mas01cr@277 515 void report(char *fileTable, adb__queryResponse *adbQueryResponse);
mas01cr@277 516 protected:
mas01cr@277 517 unsigned int pointNN;
mas01cr@277 518 unsigned int trackNN;
mas01cr@277 519 unsigned int numFiles;
mas01cr@277 520 std::set< NNresult > *set;
mas01cr@277 521 std::vector< NNresult> *point_queue;
mas01cr@277 522 unsigned int *count;
mas01cr@277 523
mas01cr@277 524 };
mas01cr@277 525
mas01cr@277 526 trackSequenceQueryRadNNReporterOneToOne::trackSequenceQueryRadNNReporterOneToOne(unsigned int pointNN, unsigned int trackNN, unsigned int numFiles):
mas01cr@277 527 pointNN(pointNN), trackNN(trackNN), numFiles(numFiles) {
mas01cr@277 528 // Where to count Radius track matches (one-to-one)
mas01cr@277 529 set = new std::set< NNresult >;
mas01cr@277 530 // Where to insert individual point matches (one-to-many)
mas01cr@277 531 point_queue = new std::vector< NNresult >;
mas01cr@277 532
mas01cr@277 533 count = new unsigned int[numFiles];
mas01cr@277 534 for (unsigned i = 0; i < numFiles; i++) {
mas01cr@277 535 count[i] = 0;
mas01cr@277 536 }
mas01cr@277 537 }
mas01cr@277 538
mas01cr@277 539 trackSequenceQueryRadNNReporterOneToOne::~trackSequenceQueryRadNNReporterOneToOne() {
mas01cr@277 540 delete set;
mas01cr@277 541 delete [] count;
mas01cr@277 542 }
mas01cr@277 543
mas01cr@277 544 void trackSequenceQueryRadNNReporterOneToOne::add_point(unsigned int trackID, unsigned int qpos, unsigned int spos, double dist) {
mas01cr@277 545 std::set< NNresult >::iterator it;
mas01cr@277 546 NNresult r;
mas01cr@277 547
mas01cr@277 548 r.qpos = qpos;
mas01cr@277 549 r.trackID = trackID;
mas01cr@277 550 r.spos = spos;
mas01cr@277 551 r.dist = dist;
mas01cr@277 552
mas01cr@277 553 if(point_queue->size() < r.qpos + 1){
mas01cr@277 554 point_queue->resize( r.qpos + 1 );
mas01cr@277 555 (*point_queue)[r.qpos].dist = 1e6;
mas01cr@277 556 }
mas01cr@277 557
mas01cr@277 558 if (r.dist < (*point_queue)[r.qpos].dist)
mas01cr@277 559 (*point_queue)[r.qpos] = r;
mas01cr@277 560
mas01cr@277 561 }
mas01cr@277 562
mas01cr@277 563 void trackSequenceQueryRadNNReporterOneToOne::report(char *fileTable, adb__queryResponse *adbQueryResponse) {
mas01cr@277 564 if(adbQueryResponse==0) {
mas01cr@277 565 std::vector< NNresult >::iterator vit;
mas01cr@277 566 NNresult rk;
mas01cr@277 567 for( vit = point_queue->begin() ; vit < point_queue->end() ; vit++ ){
mas01cr@277 568 rk = *vit;
mas01cr@277 569 std::cout << rk.dist << " "
mas01cr@277 570 << rk.qpos << " "
mas01cr@277 571 << rk.spos << " "
mas01cr@277 572 << fileTable + rk.trackID*O2_FILETABLE_ENTRY_SIZE
mas01cr@277 573 << std::endl;
mas01cr@277 574 }
mas01cr@277 575 } else {
mas01cr@277 576 // FIXME
mas01cr@277 577 }
mas01cr@277 578 }