comparison data/fileio/CSVFileReader.cpp @ 1113:ed207f89aaef

Fix assignment of values to regions on import, in case where region model contains duplicate points
author Chris Cannam
date Fri, 03 Jul 2015 16:09:14 +0100
parents 26cf6d5251ec
children 39019ce29178
comparison
equal deleted inserted replaced
1110:1517d4c60e88 1113:ed207f89aaef
32 #include <QStringList> 32 #include <QStringList>
33 #include <QTextStream> 33 #include <QTextStream>
34 34
35 #include <iostream> 35 #include <iostream>
36 #include <map> 36 #include <map>
37
38 using namespace std;
37 39
38 CSVFileReader::CSVFileReader(QString path, CSVFormat format, 40 CSVFileReader::CSVFileReader(QString path, CSVFormat format,
39 sv_samplerate_t mainModelSampleRate) : 41 sv_samplerate_t mainModelSampleRate) :
40 m_format(format), 42 m_format(format),
41 m_device(0), 43 m_device(0),
199 bool pitchLooksLikeMIDI = true; 201 bool pitchLooksLikeMIDI = true;
200 202
201 sv_frame_t startFrame = 0; // for calculation of dense model resolution 203 sv_frame_t startFrame = 0; // for calculation of dense model resolution
202 bool firstEverValue = true; 204 bool firstEverValue = true;
203 205
204 std::map<QString, int> labelCountMap; 206 map<QString, int> labelCountMap;
205 207
206 int valueColumns = 0; 208 int valueColumns = 0;
207 for (int i = 0; i < m_format.getColumnCount(); ++i) { 209 for (int i = 0; i < m_format.getColumnCount(); ++i) {
208 if (m_format.getColumnPurpose(i) == CSVFormat::ColumnValue) { 210 if (m_format.getColumnPurpose(i) == CSVFormat::ColumnValue) {
209 ++valueColumns; 211 ++valueColumns;
317 } 319 }
318 break; 320 break;
319 321
320 case CSVFormat::ColumnLabel: 322 case CSVFormat::ColumnLabel:
321 label = s; 323 label = s;
322 ++labelCountMap[label]; 324 break;
323 break; 325 }
324 } 326 }
325 } 327
326 328 ++labelCountMap[label];
329
327 if (haveEndTime) { // ... calculate duration now all cols read 330 if (haveEndTime) { // ... calculate duration now all cols read
328 if (endFrame > frameNo) { 331 if (endFrame > frameNo) {
329 duration = endFrame - frameNo; 332 duration = endFrame - frameNo;
330 } 333 }
331 } 334 }
411 if (!haveAnyValue) { 414 if (!haveAnyValue) {
412 if (model2a) { 415 if (model2a) {
413 // assign values for regions based on label frequency; we 416 // assign values for regions based on label frequency; we
414 // have this in our labelCountMap, sort of 417 // have this in our labelCountMap, sort of
415 418
416 std::map<int, std::map<QString, float> > countLabelValueMap; 419 map<int, map<QString, float> > countLabelValueMap;
417 for (std::map<QString, int>::iterator i = labelCountMap.begin(); 420 for (map<QString, int>::iterator i = labelCountMap.begin();
418 i != labelCountMap.end(); ++i) { 421 i != labelCountMap.end(); ++i) {
419 countLabelValueMap[i->second][i->first] = 0.f; 422 countLabelValueMap[i->second][i->first] = -1.f;
420 } 423 }
421 424
422 float v = 0.f; 425 float v = 0.f;
423 for (std::map<int, std::map<QString, float> >::iterator i = 426 for (map<int, map<QString, float> >::iterator i =
424 countLabelValueMap.end(); i != countLabelValueMap.begin(); ) { 427 countLabelValueMap.end(); i != countLabelValueMap.begin(); ) {
425 --i; 428 --i;
426 for (std::map<QString, float>::iterator j = i->second.begin(); 429 cerr << "count -> " << i->first << endl;
430 for (map<QString, float>::iterator j = i->second.begin();
427 j != i->second.end(); ++j) { 431 j != i->second.end(); ++j) {
428 j->second = v; 432 j->second = v;
433 cerr << "label -> " << j->first << ", value " << v << endl;
429 v = v + 1.f; 434 v = v + 1.f;
430 } 435 }
431 } 436 }
432 437
433 std::map<RegionModel::Point, RegionModel::Point, 438 map<RegionModel::Point, RegionModel::Point,
434 RegionModel::Point::Comparator> pointMap; 439 RegionModel::Point::Comparator> pointMap;
435 for (RegionModel::PointList::const_iterator i = 440 for (RegionModel::PointList::const_iterator i =
436 model2a->getPoints().begin(); 441 model2a->getPoints().begin();
437 i != model2a->getPoints().end(); ++i) { 442 i != model2a->getPoints().end(); ++i) {
438 RegionModel::Point p(*i); 443 RegionModel::Point p(*i);
439 v = countLabelValueMap[labelCountMap[p.label]][p.label]; 444 int count = labelCountMap[p.label];
445 v = countLabelValueMap[count][p.label];
446 cerr << "mapping from label \"" << p.label << "\" (count " << count << ") to value " << v << endl;
440 RegionModel::Point pp(p.frame, v, p.duration, p.label); 447 RegionModel::Point pp(p.frame, v, p.duration, p.label);
441 pointMap[p] = pp; 448 pointMap[p] = pp;
442 } 449 }
443 450
444 for (std::map<RegionModel::Point, RegionModel::Point>::iterator i = 451 for (map<RegionModel::Point, RegionModel::Point>::iterator i =
445 pointMap.begin(); i != pointMap.end(); ++i) { 452 pointMap.begin(); i != pointMap.end(); ++i) {
446 model2a->deletePoint(i->first); 453 // There could be duplicate regions; if so replace
447 model2a->addPoint(i->second); 454 // them all -- but we need to check we're not
455 // replacing a region by itself (or else this will
456 // never terminate)
457 if (i->first.value == i->second.value) {
458 continue;
459 }
460 while (model2a->containsPoint(i->first)) {
461 model2a->deletePoint(i->first);
462 model2a->addPoint(i->second);
463 }
448 } 464 }
449 } 465 }
450 } 466 }
451 467
452 if (model2b) { 468 if (model2b) {