annotate data/fileio/CSVFormat.cpp @ 1524:64ef24ebb19c

Some CSV format tests and minor fixes
author Chris Cannam
date Fri, 14 Sep 2018 09:25:17 +0100
parents c1b2eab6ac51
children a92e94215863
rev   line source
Chris@392 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@392 2
Chris@392 3 /*
Chris@392 4 Sonic Visualiser
Chris@392 5 An audio file viewer and annotation editor.
Chris@392 6 Centre for Digital Music, Queen Mary, University of London.
Chris@392 7 This file copyright 2006 Chris Cannam.
Chris@392 8
Chris@392 9 This program is free software; you can redistribute it and/or
Chris@392 10 modify it under the terms of the GNU General Public License as
Chris@392 11 published by the Free Software Foundation; either version 2 of the
Chris@392 12 License, or (at your option) any later version. See the file
Chris@392 13 COPYING included with this distribution for more information.
Chris@392 14 */
Chris@392 15
Chris@392 16 #include "CSVFormat.h"
Chris@392 17
Chris@629 18 #include "base/StringBits.h"
Chris@629 19
Chris@392 20 #include <QFile>
Chris@392 21 #include <QString>
Chris@392 22 #include <QRegExp>
Chris@392 23 #include <QStringList>
Chris@392 24 #include <QTextStream>
Chris@392 25
Chris@392 26 #include <iostream>
Chris@392 27
Chris@1362 28 #include "base/Debug.h"
Chris@1362 29
Chris@629 30 CSVFormat::CSVFormat(QString path) :
Chris@629 31 m_separator(""),
Chris@392 32 m_sampleRate(44100),
Chris@392 33 m_windowSize(1024),
Chris@629 34 m_allowQuoting(true)
Chris@392 35 {
Chris@1524 36 (void)guessFormatFor(path);
Chris@629 37 }
Chris@629 38
Chris@1524 39 bool
Chris@629 40 CSVFormat::guessFormatFor(QString path)
Chris@629 41 {
Chris@1524 42 m_separator = ""; // to prompt guessing for it
Chris@1524 43
Chris@629 44 m_modelType = TwoDimensionalModel;
Chris@629 45 m_timingType = ExplicitTiming;
Chris@629 46 m_timeUnits = TimeSeconds;
Chris@629 47
Chris@629 48 m_maxExampleCols = 0;
Chris@629 49 m_columnCount = 0;
Chris@629 50 m_variableColumnCount = false;
Chris@629 51
Chris@629 52 m_example.clear();
Chris@629 53 m_columnQualities.clear();
Chris@629 54 m_columnPurposes.clear();
Chris@629 55 m_prevValues.clear();
Chris@629 56
Chris@629 57 QFile file(path);
Chris@1524 58 if (!file.exists()) {
Chris@1524 59 SVCERR << "CSVFormat::guessFormatFor(" << path
Chris@1524 60 << "): File does not exist" << endl;
Chris@1524 61 return false;
Chris@1524 62 }
Chris@1524 63 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@1524 64 SVCERR << "CSVFormat::guessFormatFor(" << path
Chris@1524 65 << "): File could not be opened for reading" << endl;
Chris@1524 66 return false;
Chris@1524 67 }
Chris@1524 68 SVDEBUG << "CSVFormat::guessFormatFor(" << path << ")" << endl;
Chris@392 69
Chris@392 70 QTextStream in(&file);
Chris@392 71 in.seek(0);
Chris@392 72
Chris@629 73 int lineno = 0;
Chris@392 74
Chris@392 75 while (!in.atEnd()) {
Chris@392 76
Chris@392 77 // See comment about line endings in CSVFileReader::load()
Chris@392 78
Chris@392 79 QString chunk = in.readLine();
Chris@392 80 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
Chris@392 81
Chris@897 82 for (int li = 0; li < lines.size(); ++li) {
Chris@392 83
Chris@392 84 QString line = lines[li];
Chris@1512 85 if (line.startsWith("#") || line == "") {
Chris@1512 86 continue;
Chris@1512 87 }
Chris@392 88
Chris@629 89 guessQualities(line, lineno);
Chris@392 90
Chris@840 91 ++lineno;
Chris@629 92 }
Chris@840 93
Chris@1512 94 if (lineno >= 150) break;
Chris@629 95 }
Chris@392 96
Chris@629 97 guessPurposes();
Chris@1515 98 guessAudioSampleRange();
Chris@1524 99
Chris@1524 100 return true;
Chris@629 101 }
Chris@629 102
Chris@629 103 void
Chris@629 104 CSVFormat::guessSeparator(QString line)
Chris@629 105 {
Chris@1524 106 QString candidates = "\t|,/: ";
Chris@1524 107
Chris@1524 108 for (int i = 0; i < candidates.length(); ++i) {
Chris@1524 109 auto bits = StringBits::split(line, candidates[i], m_allowQuoting);
Chris@1524 110 if (bits.size() >= 2) {
Chris@1524 111 SVDEBUG << "Successfully split the line into:" << endl;
Chris@1524 112 for (auto b: bits) {
Chris@1524 113 SVDEBUG << b << endl;
Chris@1524 114 }
Chris@629 115 m_separator = candidates[i];
Chris@1510 116 SVDEBUG << "Estimated column separator: '" << m_separator
Chris@1510 117 << "'" << endl;
Chris@629 118 return;
Chris@629 119 }
Chris@629 120 }
Chris@629 121 }
Chris@629 122
Chris@629 123 void
Chris@629 124 CSVFormat::guessQualities(QString line, int lineno)
Chris@629 125 {
Chris@1524 126 if (m_separator == "") {
Chris@1524 127 guessSeparator(line);
Chris@1524 128 }
Chris@629 129
Chris@1362 130 QStringList list = StringBits::split(line, getSeparator(), m_allowQuoting);
Chris@629 131
Chris@629 132 int cols = list.size();
Chris@991 133 if (lineno == 0 || (cols > m_columnCount)) m_columnCount = cols;
Chris@629 134 if (cols != m_columnCount) m_variableColumnCount = true;
Chris@629 135
Chris@629 136 // All columns are regarded as having these qualities until we see
Chris@629 137 // something that indicates otherwise:
Chris@629 138
Chris@629 139 ColumnQualities defaultQualities =
Chris@1512 140 ColumnNumeric | ColumnIntegral | ColumnSmall |
Chris@1512 141 ColumnIncreasing | ColumnNearEmpty;
Chris@629 142
Chris@629 143 for (int i = 0; i < cols; ++i) {
Chris@1429 144
Chris@629 145 while (m_columnQualities.size() <= i) {
Chris@629 146 m_columnQualities.push_back(defaultQualities);
Chris@629 147 m_prevValues.push_back(0.f);
Chris@629 148 }
Chris@629 149
Chris@629 150 QString s(list[i]);
Chris@629 151 bool ok = false;
Chris@629 152
Chris@629 153 ColumnQualities qualities = m_columnQualities[i];
Chris@629 154
Chris@1523 155 // Looks like this is defined on Windows
Chris@1523 156 #undef small
Chris@1523 157
Chris@629 158 bool numeric = (qualities & ColumnNumeric);
Chris@629 159 bool integral = (qualities & ColumnIntegral);
Chris@629 160 bool increasing = (qualities & ColumnIncreasing);
Chris@1512 161 bool small = (qualities & ColumnSmall);
Chris@629 162 bool large = (qualities & ColumnLarge); // this one defaults to off
Chris@1512 163 bool signd = (qualities & ColumnSigned); // also defaults to off
Chris@1021 164 bool emptyish = (qualities & ColumnNearEmpty);
Chris@629 165
Chris@1021 166 if (lineno > 1 && s.trimmed() != "") {
Chris@1021 167 emptyish = false;
Chris@1021 168 }
Chris@1021 169
Chris@629 170 float value = 0.f;
Chris@629 171
Chris@629 172 //!!! how to take into account headers?
Chris@629 173
Chris@629 174 if (numeric) {
Chris@629 175 value = s.toFloat(&ok);
Chris@629 176 if (!ok) {
Chris@629 177 value = (float)StringBits::stringToDoubleLocaleFree(s, &ok);
Chris@629 178 }
Chris@629 179 if (ok) {
Chris@1512 180 if (lineno < 2 && value > 1000.f) {
Chris@1512 181 large = true;
Chris@1512 182 }
Chris@1512 183 if (value < 0.f) {
Chris@1512 184 signd = true;
Chris@1512 185 }
Chris@1512 186 if (value < -1.f || value > 1.f) {
Chris@1512 187 small = false;
Chris@1512 188 }
Chris@629 189 } else {
Chris@629 190 numeric = false;
Chris@1524 191
Chris@1524 192 // If the column is not numeric, it can't be any of
Chris@1524 193 // these things either
Chris@1524 194 integral = false;
Chris@1524 195 increasing = false;
Chris@1524 196 small = false;
Chris@1524 197 large = false;
Chris@1524 198 signd = false;
Chris@629 199 }
Chris@629 200 }
Chris@629 201
Chris@629 202 if (numeric) {
Chris@629 203
Chris@629 204 if (integral) {
Chris@629 205 if (s.contains('.') || s.contains(',')) {
Chris@629 206 integral = false;
Chris@392 207 }
Chris@392 208 }
Chris@392 209
Chris@629 210 if (increasing) {
Chris@629 211 if (lineno > 0 && value <= m_prevValues[i]) {
Chris@629 212 increasing = false;
Chris@392 213 }
Chris@392 214 }
Chris@392 215
Chris@629 216 m_prevValues[i] = value;
Chris@629 217 }
Chris@1524 218
Chris@629 219 m_columnQualities[i] =
Chris@629 220 (numeric ? ColumnNumeric : 0) |
Chris@629 221 (integral ? ColumnIntegral : 0) |
Chris@629 222 (increasing ? ColumnIncreasing : 0) |
Chris@1512 223 (small ? ColumnSmall : 0) |
Chris@1021 224 (large ? ColumnLarge : 0) |
Chris@1512 225 (signd ? ColumnSigned : 0) |
Chris@1021 226 (emptyish ? ColumnNearEmpty : 0);
Chris@629 227 }
Chris@392 228
Chris@629 229 if (lineno < 10) {
Chris@629 230 m_example.push_back(list);
Chris@629 231 if (lineno == 0 || cols > m_maxExampleCols) {
Chris@629 232 m_maxExampleCols = cols;
Chris@392 233 }
Chris@392 234 }
Chris@392 235
Chris@1362 236 if (lineno < 10) {
Chris@1362 237 SVDEBUG << "Estimated column qualities for line " << lineno << " (reporting up to first 10): ";
Chris@1362 238 for (int i = 0; i < m_columnCount; ++i) {
Chris@1362 239 SVDEBUG << int(m_columnQualities[i]) << " ";
Chris@1362 240 }
Chris@1362 241 SVDEBUG << endl;
Chris@1362 242 }
Chris@629 243 }
Chris@629 244
Chris@629 245 void
Chris@629 246 CSVFormat::guessPurposes()
Chris@629 247 {
Chris@629 248 m_timingType = CSVFormat::ImplicitTiming;
Chris@629 249 m_timeUnits = CSVFormat::TimeWindows;
Chris@1429 250
Chris@629 251 int timingColumnCount = 0;
Chris@1021 252
Chris@1510 253 SVDEBUG << "Estimated column qualities overall: ";
Chris@1510 254 for (int i = 0; i < m_columnCount; ++i) {
Chris@1510 255 SVDEBUG << int(m_columnQualities[i]) << " ";
Chris@1510 256 }
Chris@1510 257 SVDEBUG << endl;
Chris@1510 258
Chris@1021 259 // if our first column has zero or one entries in it and the rest
Chris@1021 260 // have more, then we'll default to ignoring the first column and
Chris@1021 261 // counting the next one as primary. (e.g. Sonic Annotator output
Chris@1021 262 // with filename at start of first column.)
Chris@1021 263
Chris@1021 264 int primaryColumnNo = 0;
Chris@1021 265
Chris@1021 266 if (m_columnCount >= 2) {
Chris@1021 267 if ( (m_columnQualities[0] & ColumnNearEmpty) &&
Chris@1021 268 !(m_columnQualities[1] & ColumnNearEmpty)) {
Chris@1021 269 primaryColumnNo = 1;
Chris@1021 270 }
Chris@1021 271 }
Chris@629 272
Chris@629 273 for (int i = 0; i < m_columnCount; ++i) {
Chris@629 274
Chris@629 275 ColumnPurpose purpose = ColumnUnknown;
Chris@1021 276
Chris@1021 277 if (i < primaryColumnNo) {
Chris@1021 278 setColumnPurpose(i, purpose);
Chris@1021 279 continue;
Chris@1021 280 }
Chris@1021 281
Chris@1021 282 bool primary = (i == primaryColumnNo);
Chris@392 283
Chris@629 284 ColumnQualities qualities = m_columnQualities[i];
Chris@392 285
Chris@629 286 bool numeric = (qualities & ColumnNumeric);
Chris@629 287 bool integral = (qualities & ColumnIntegral);
Chris@629 288 bool increasing = (qualities & ColumnIncreasing);
Chris@629 289 bool large = (qualities & ColumnLarge);
Chris@629 290
Chris@629 291 bool timingColumn = (numeric && increasing);
Chris@629 292
Chris@629 293 if (timingColumn) {
Chris@629 294
Chris@629 295 ++timingColumnCount;
Chris@629 296
Chris@629 297 if (primary) {
Chris@629 298
Chris@629 299 purpose = ColumnStartTime;
Chris@629 300
Chris@629 301 m_timingType = ExplicitTiming;
Chris@629 302
Chris@629 303 if (integral && large) {
Chris@629 304 m_timeUnits = TimeAudioFrames;
Chris@629 305 } else {
Chris@629 306 m_timeUnits = TimeSeconds;
Chris@629 307 }
Chris@629 308
Chris@629 309 } else {
Chris@629 310
Chris@629 311 if (timingColumnCount == 2 && m_timingType == ExplicitTiming) {
Chris@629 312 purpose = ColumnEndTime;
Chris@629 313 }
Chris@629 314 }
Chris@629 315 }
Chris@629 316
Chris@629 317 if (purpose == ColumnUnknown) {
Chris@629 318 if (numeric) {
Chris@629 319 purpose = ColumnValue;
Chris@629 320 } else {
Chris@629 321 purpose = ColumnLabel;
Chris@629 322 }
Chris@629 323 }
Chris@629 324
Chris@631 325 setColumnPurpose(i, purpose);
Chris@629 326 }
Chris@629 327
Chris@629 328 int valueCount = 0;
Chris@629 329 for (int i = 0; i < m_columnCount; ++i) {
Chris@629 330 if (m_columnPurposes[i] == ColumnValue) ++valueCount;
Chris@629 331 }
Chris@629 332
Chris@630 333 if (valueCount == 2 && timingColumnCount == 1) {
Chris@630 334 // If we have exactly two apparent value columns and only one
Chris@630 335 // timing column, but one value column is integral and the
Chris@630 336 // other is not, guess that whichever one matches the integral
Chris@630 337 // status of the time column is either duration or end time
Chris@630 338 if (m_timingType == ExplicitTiming) {
Chris@630 339 int a = -1, b = -1;
Chris@630 340 for (int i = 0; i < m_columnCount; ++i) {
Chris@630 341 if (m_columnPurposes[i] == ColumnValue) {
Chris@630 342 if (a == -1) a = i;
Chris@630 343 else b = i;
Chris@630 344 }
Chris@630 345 }
Chris@630 346 if ((m_columnQualities[a] & ColumnIntegral) !=
Chris@630 347 (m_columnQualities[b] & ColumnIntegral)) {
Chris@630 348 int timecol = a;
Chris@630 349 if ((m_columnQualities[a] & ColumnIntegral) !=
Chris@630 350 (m_columnQualities[0] & ColumnIntegral)) {
Chris@630 351 timecol = b;
Chris@630 352 }
Chris@630 353 if (m_columnQualities[timecol] & ColumnIncreasing) {
Chris@630 354 // This shouldn't happen; should have been settled above
Chris@630 355 m_columnPurposes[timecol] = ColumnEndTime;
Chris@630 356 } else {
Chris@630 357 m_columnPurposes[timecol] = ColumnDuration;
Chris@630 358 }
Chris@630 359 --valueCount;
Chris@630 360 }
Chris@630 361 }
Chris@630 362 }
Chris@630 363
Chris@631 364 if (timingColumnCount > 1) {
Chris@631 365 m_modelType = TwoDimensionalModelWithDuration;
Chris@392 366 } else {
Chris@631 367 if (valueCount == 0) {
Chris@631 368 m_modelType = OneDimensionalModel;
Chris@631 369 } else if (valueCount == 1) {
Chris@631 370 m_modelType = TwoDimensionalModel;
Chris@631 371 } else {
Chris@631 372 m_modelType = ThreeDimensionalModel;
Chris@631 373 }
Chris@629 374 }
Chris@392 375
Chris@1362 376 SVDEBUG << "Estimated column purposes: ";
Chris@1362 377 for (int i = 0; i < m_columnCount; ++i) {
Chris@1362 378 SVDEBUG << int(m_columnPurposes[i]) << " ";
Chris@1362 379 }
Chris@1362 380 SVDEBUG << endl;
Chris@392 381
Chris@1362 382 SVDEBUG << "Estimated model type: " << m_modelType << endl;
Chris@1362 383 SVDEBUG << "Estimated timing type: " << m_timingType << endl;
Chris@1362 384 SVDEBUG << "Estimated units: " << m_timeUnits << endl;
Chris@392 385 }
Chris@392 386
Chris@1515 387 void
Chris@1515 388 CSVFormat::guessAudioSampleRange()
Chris@1515 389 {
Chris@1515 390 AudioSampleRange range = SampleRangeSigned1;
Chris@1515 391
Chris@1515 392 range = SampleRangeSigned1;
Chris@1515 393 bool knownSigned = false;
Chris@1515 394 bool knownNonIntegral = false;
Chris@1521 395
Chris@1521 396 SVDEBUG << "CSVFormat::guessAudioSampleRange: starting with assumption of "
Chris@1521 397 << range << endl;
Chris@1515 398
Chris@1515 399 for (int i = 0; i < m_columnCount; ++i) {
Chris@1521 400 if (m_columnPurposes[i] != ColumnValue) {
Chris@1521 401 SVDEBUG << "... column " << i
Chris@1521 402 << " is not apparently a value, ignoring" << endl;
Chris@1521 403 continue;
Chris@1521 404 }
Chris@1515 405 if (!(m_columnQualities[i] & ColumnIntegral)) {
Chris@1515 406 knownNonIntegral = true;
Chris@1515 407 if (range == SampleRangeUnsigned255 ||
Chris@1515 408 range == SampleRangeSigned32767) {
Chris@1515 409 range = SampleRangeOther;
Chris@1515 410 }
Chris@1521 411 SVDEBUG << "... column " << i
Chris@1521 412 << " is non-integral, updating range to " << range << endl;
Chris@1515 413 }
Chris@1515 414 if (m_columnQualities[i] & ColumnLarge) {
Chris@1515 415 if (range == SampleRangeSigned1 ||
Chris@1515 416 range == SampleRangeUnsigned255) {
Chris@1515 417 if (knownNonIntegral) {
Chris@1515 418 range = SampleRangeOther;
Chris@1515 419 } else {
Chris@1515 420 range = SampleRangeSigned32767;
Chris@1515 421 }
Chris@1515 422 }
Chris@1521 423 SVDEBUG << "... column " << i << " is large, updating range to "
Chris@1521 424 << range << endl;
Chris@1515 425 }
Chris@1515 426 if (m_columnQualities[i] & ColumnSigned) {
Chris@1515 427 knownSigned = true;
Chris@1515 428 if (range == SampleRangeUnsigned255) {
Chris@1515 429 range = SampleRangeSigned32767;
Chris@1515 430 }
Chris@1521 431 SVDEBUG << "... column " << i << " is signed, updating range to "
Chris@1521 432 << range << endl;
Chris@1515 433 }
Chris@1515 434 if (!(m_columnQualities[i] & ColumnSmall)) {
Chris@1515 435 if (range == SampleRangeSigned1) {
Chris@1515 436 if (knownNonIntegral) {
Chris@1515 437 range = SampleRangeOther;
Chris@1515 438 } else if (knownSigned) {
Chris@1515 439 range = SampleRangeSigned32767;
Chris@1515 440 } else {
Chris@1515 441 range = SampleRangeUnsigned255;
Chris@1515 442 }
Chris@1515 443 }
Chris@1521 444 SVDEBUG << "... column " << i << " is not small, updating range to "
Chris@1521 445 << range << endl;
Chris@1515 446 }
Chris@1515 447 }
Chris@1515 448
Chris@1521 449 SVDEBUG << "CSVFormat::guessAudioSampleRange: ended up with range "
Chris@1521 450 << range << endl;
Chris@1521 451
Chris@1515 452 m_audioSampleRange = range;
Chris@1515 453 }
Chris@1515 454
Chris@631 455 CSVFormat::ColumnPurpose
Chris@631 456 CSVFormat::getColumnPurpose(int i)
Chris@631 457 {
Chris@631 458 while (m_columnPurposes.size() <= i) {
Chris@631 459 m_columnPurposes.push_back(ColumnUnknown);
Chris@631 460 }
Chris@631 461 return m_columnPurposes[i];
Chris@631 462 }
Chris@629 463
Chris@631 464 CSVFormat::ColumnPurpose
Chris@631 465 CSVFormat::getColumnPurpose(int i) const
Chris@631 466 {
Chris@668 467 if (m_columnPurposes.size() <= i) {
Chris@668 468 return ColumnUnknown;
Chris@668 469 }
Chris@631 470 return m_columnPurposes[i];
Chris@631 471 }
Chris@631 472
Chris@631 473 void
Chris@631 474 CSVFormat::setColumnPurpose(int i, ColumnPurpose p)
Chris@631 475 {
Chris@631 476 while (m_columnPurposes.size() <= i) {
Chris@631 477 m_columnPurposes.push_back(ColumnUnknown);
Chris@631 478 }
Chris@631 479 m_columnPurposes[i] = p;
Chris@631 480 }
Chris@631 481
Chris@631 482
Chris@631 483
Chris@631 484