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