annotate data/fileio/CSVFormat.cpp @ 1512:c9c2aa17439a import-audio-data

Add small, signed
author Chris Cannam
date Thu, 06 Sep 2018 16:26:19 +0100
parents 5f1b2a117a4f
children ffec849858a7
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@629 36 guessFormatFor(path);
Chris@629 37 }
Chris@629 38
Chris@629 39 void
Chris@629 40 CSVFormat::guessFormatFor(QString path)
Chris@629 41 {
Chris@629 42 m_modelType = TwoDimensionalModel;
Chris@629 43 m_timingType = ExplicitTiming;
Chris@629 44 m_timeUnits = TimeSeconds;
Chris@629 45
Chris@629 46 m_maxExampleCols = 0;
Chris@629 47 m_columnCount = 0;
Chris@629 48 m_variableColumnCount = false;
Chris@629 49
Chris@629 50 m_example.clear();
Chris@629 51 m_columnQualities.clear();
Chris@629 52 m_columnPurposes.clear();
Chris@629 53 m_prevValues.clear();
Chris@629 54
Chris@629 55 QFile file(path);
Chris@392 56 if (!file.exists()) return;
Chris@392 57 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
Chris@392 58
Chris@392 59 QTextStream in(&file);
Chris@392 60 in.seek(0);
Chris@392 61
Chris@629 62 int lineno = 0;
Chris@392 63
Chris@392 64 while (!in.atEnd()) {
Chris@392 65
Chris@392 66 // See comment about line endings in CSVFileReader::load()
Chris@392 67
Chris@392 68 QString chunk = in.readLine();
Chris@392 69 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
Chris@392 70
Chris@897 71 for (int li = 0; li < lines.size(); ++li) {
Chris@392 72
Chris@392 73 QString line = lines[li];
Chris@1512 74 if (line.startsWith("#") || line == "") {
Chris@1512 75 continue;
Chris@1512 76 }
Chris@392 77
Chris@629 78 guessQualities(line, lineno);
Chris@392 79
Chris@840 80 ++lineno;
Chris@629 81 }
Chris@840 82
Chris@1512 83 if (lineno >= 150) break;
Chris@629 84 }
Chris@392 85
Chris@629 86 guessPurposes();
Chris@629 87 }
Chris@629 88
Chris@629 89 void
Chris@629 90 CSVFormat::guessSeparator(QString line)
Chris@629 91 {
Chris@629 92 char candidates[] = { ',', '\t', ' ', '|', '/', ':' };
Chris@897 93 for (int i = 0; i < int(sizeof(candidates)/sizeof(candidates[0])); ++i) {
Chris@629 94 if (StringBits::split(line, candidates[i], m_allowQuoting).size() >= 2) {
Chris@629 95 m_separator = candidates[i];
Chris@1510 96 SVDEBUG << "Estimated column separator: '" << m_separator
Chris@1510 97 << "'" << endl;
Chris@629 98 return;
Chris@629 99 }
Chris@629 100 }
Chris@629 101 }
Chris@629 102
Chris@629 103 void
Chris@629 104 CSVFormat::guessQualities(QString line, int lineno)
Chris@629 105 {
Chris@629 106 if (m_separator == "") guessSeparator(line);
Chris@629 107
Chris@1362 108 QStringList list = StringBits::split(line, getSeparator(), m_allowQuoting);
Chris@629 109
Chris@629 110 int cols = list.size();
Chris@991 111 if (lineno == 0 || (cols > m_columnCount)) m_columnCount = cols;
Chris@629 112 if (cols != m_columnCount) m_variableColumnCount = true;
Chris@629 113
Chris@629 114 // All columns are regarded as having these qualities until we see
Chris@629 115 // something that indicates otherwise:
Chris@629 116
Chris@629 117 ColumnQualities defaultQualities =
Chris@1512 118 ColumnNumeric | ColumnIntegral | ColumnSmall |
Chris@1512 119 ColumnIncreasing | ColumnNearEmpty;
Chris@629 120
Chris@629 121 for (int i = 0; i < cols; ++i) {
Chris@1429 122
Chris@629 123 while (m_columnQualities.size() <= i) {
Chris@629 124 m_columnQualities.push_back(defaultQualities);
Chris@629 125 m_prevValues.push_back(0.f);
Chris@629 126 }
Chris@629 127
Chris@629 128 QString s(list[i]);
Chris@629 129 bool ok = false;
Chris@629 130
Chris@629 131 ColumnQualities qualities = m_columnQualities[i];
Chris@629 132
Chris@629 133 bool numeric = (qualities & ColumnNumeric);
Chris@629 134 bool integral = (qualities & ColumnIntegral);
Chris@629 135 bool increasing = (qualities & ColumnIncreasing);
Chris@1512 136 bool small = (qualities & ColumnSmall);
Chris@629 137 bool large = (qualities & ColumnLarge); // this one defaults to off
Chris@1512 138 bool signd = (qualities & ColumnSigned); // also defaults to off
Chris@1021 139 bool emptyish = (qualities & ColumnNearEmpty);
Chris@629 140
Chris@1021 141 if (lineno > 1 && s.trimmed() != "") {
Chris@1021 142 emptyish = false;
Chris@1021 143 }
Chris@1021 144
Chris@629 145 float value = 0.f;
Chris@629 146
Chris@629 147 //!!! how to take into account headers?
Chris@629 148
Chris@629 149 if (numeric) {
Chris@629 150 value = s.toFloat(&ok);
Chris@629 151 if (!ok) {
Chris@629 152 value = (float)StringBits::stringToDoubleLocaleFree(s, &ok);
Chris@629 153 }
Chris@629 154 if (ok) {
Chris@1512 155 if (lineno < 2 && value > 1000.f) {
Chris@1512 156 large = true;
Chris@1512 157 }
Chris@1512 158 if (value < 0.f) {
Chris@1512 159 signd = true;
Chris@1512 160 }
Chris@1512 161 if (value < -1.f || value > 1.f) {
Chris@1512 162 small = false;
Chris@1512 163 }
Chris@629 164 } else {
Chris@629 165 numeric = false;
Chris@629 166 }
Chris@629 167 }
Chris@629 168
Chris@629 169 if (numeric) {
Chris@629 170
Chris@629 171 if (integral) {
Chris@629 172 if (s.contains('.') || s.contains(',')) {
Chris@629 173 integral = false;
Chris@392 174 }
Chris@392 175 }
Chris@392 176
Chris@629 177 if (increasing) {
Chris@629 178 if (lineno > 0 && value <= m_prevValues[i]) {
Chris@629 179 increasing = false;
Chris@392 180 }
Chris@392 181 }
Chris@392 182
Chris@629 183 m_prevValues[i] = value;
Chris@629 184 }
Chris@392 185
Chris@629 186 m_columnQualities[i] =
Chris@629 187 (numeric ? ColumnNumeric : 0) |
Chris@629 188 (integral ? ColumnIntegral : 0) |
Chris@629 189 (increasing ? ColumnIncreasing : 0) |
Chris@1512 190 (small ? ColumnSmall : 0) |
Chris@1021 191 (large ? ColumnLarge : 0) |
Chris@1512 192 (signd ? ColumnSigned : 0) |
Chris@1021 193 (emptyish ? ColumnNearEmpty : 0);
Chris@629 194 }
Chris@392 195
Chris@629 196 if (lineno < 10) {
Chris@629 197 m_example.push_back(list);
Chris@629 198 if (lineno == 0 || cols > m_maxExampleCols) {
Chris@629 199 m_maxExampleCols = cols;
Chris@392 200 }
Chris@392 201 }
Chris@392 202
Chris@1362 203 if (lineno < 10) {
Chris@1362 204 SVDEBUG << "Estimated column qualities for line " << lineno << " (reporting up to first 10): ";
Chris@1362 205 for (int i = 0; i < m_columnCount; ++i) {
Chris@1362 206 SVDEBUG << int(m_columnQualities[i]) << " ";
Chris@1362 207 }
Chris@1362 208 SVDEBUG << endl;
Chris@1362 209 }
Chris@629 210 }
Chris@629 211
Chris@629 212 void
Chris@629 213 CSVFormat::guessPurposes()
Chris@629 214 {
Chris@629 215 m_timingType = CSVFormat::ImplicitTiming;
Chris@629 216 m_timeUnits = CSVFormat::TimeWindows;
Chris@1429 217
Chris@629 218 int timingColumnCount = 0;
Chris@1021 219
Chris@1510 220 SVDEBUG << "Estimated column qualities overall: ";
Chris@1510 221 for (int i = 0; i < m_columnCount; ++i) {
Chris@1510 222 SVDEBUG << int(m_columnQualities[i]) << " ";
Chris@1510 223 }
Chris@1510 224 SVDEBUG << endl;
Chris@1510 225
Chris@1021 226 // if our first column has zero or one entries in it and the rest
Chris@1021 227 // have more, then we'll default to ignoring the first column and
Chris@1021 228 // counting the next one as primary. (e.g. Sonic Annotator output
Chris@1021 229 // with filename at start of first column.)
Chris@1021 230
Chris@1021 231 int primaryColumnNo = 0;
Chris@1021 232
Chris@1021 233 if (m_columnCount >= 2) {
Chris@1021 234 if ( (m_columnQualities[0] & ColumnNearEmpty) &&
Chris@1021 235 !(m_columnQualities[1] & ColumnNearEmpty)) {
Chris@1021 236 primaryColumnNo = 1;
Chris@1021 237 }
Chris@1021 238 }
Chris@629 239
Chris@629 240 for (int i = 0; i < m_columnCount; ++i) {
Chris@629 241
Chris@629 242 ColumnPurpose purpose = ColumnUnknown;
Chris@1021 243
Chris@1021 244 if (i < primaryColumnNo) {
Chris@1021 245 setColumnPurpose(i, purpose);
Chris@1021 246 continue;
Chris@1021 247 }
Chris@1021 248
Chris@1021 249 bool primary = (i == primaryColumnNo);
Chris@392 250
Chris@629 251 ColumnQualities qualities = m_columnQualities[i];
Chris@392 252
Chris@629 253 bool numeric = (qualities & ColumnNumeric);
Chris@629 254 bool integral = (qualities & ColumnIntegral);
Chris@629 255 bool increasing = (qualities & ColumnIncreasing);
Chris@629 256 bool large = (qualities & ColumnLarge);
Chris@629 257
Chris@629 258 bool timingColumn = (numeric && increasing);
Chris@629 259
Chris@629 260 if (timingColumn) {
Chris@629 261
Chris@629 262 ++timingColumnCount;
Chris@629 263
Chris@629 264 if (primary) {
Chris@629 265
Chris@629 266 purpose = ColumnStartTime;
Chris@629 267
Chris@629 268 m_timingType = ExplicitTiming;
Chris@629 269
Chris@629 270 if (integral && large) {
Chris@629 271 m_timeUnits = TimeAudioFrames;
Chris@629 272 } else {
Chris@629 273 m_timeUnits = TimeSeconds;
Chris@629 274 }
Chris@629 275
Chris@629 276 } else {
Chris@629 277
Chris@629 278 if (timingColumnCount == 2 && m_timingType == ExplicitTiming) {
Chris@629 279 purpose = ColumnEndTime;
Chris@629 280 }
Chris@629 281 }
Chris@629 282 }
Chris@629 283
Chris@629 284 if (purpose == ColumnUnknown) {
Chris@629 285 if (numeric) {
Chris@629 286 purpose = ColumnValue;
Chris@629 287 } else {
Chris@629 288 purpose = ColumnLabel;
Chris@629 289 }
Chris@629 290 }
Chris@629 291
Chris@631 292 setColumnPurpose(i, purpose);
Chris@629 293 }
Chris@629 294
Chris@629 295 int valueCount = 0;
Chris@629 296 for (int i = 0; i < m_columnCount; ++i) {
Chris@629 297 if (m_columnPurposes[i] == ColumnValue) ++valueCount;
Chris@629 298 }
Chris@629 299
Chris@630 300 if (valueCount == 2 && timingColumnCount == 1) {
Chris@630 301 // If we have exactly two apparent value columns and only one
Chris@630 302 // timing column, but one value column is integral and the
Chris@630 303 // other is not, guess that whichever one matches the integral
Chris@630 304 // status of the time column is either duration or end time
Chris@630 305 if (m_timingType == ExplicitTiming) {
Chris@630 306 int a = -1, b = -1;
Chris@630 307 for (int i = 0; i < m_columnCount; ++i) {
Chris@630 308 if (m_columnPurposes[i] == ColumnValue) {
Chris@630 309 if (a == -1) a = i;
Chris@630 310 else b = i;
Chris@630 311 }
Chris@630 312 }
Chris@630 313 if ((m_columnQualities[a] & ColumnIntegral) !=
Chris@630 314 (m_columnQualities[b] & ColumnIntegral)) {
Chris@630 315 int timecol = a;
Chris@630 316 if ((m_columnQualities[a] & ColumnIntegral) !=
Chris@630 317 (m_columnQualities[0] & ColumnIntegral)) {
Chris@630 318 timecol = b;
Chris@630 319 }
Chris@630 320 if (m_columnQualities[timecol] & ColumnIncreasing) {
Chris@630 321 // This shouldn't happen; should have been settled above
Chris@630 322 m_columnPurposes[timecol] = ColumnEndTime;
Chris@630 323 } else {
Chris@630 324 m_columnPurposes[timecol] = ColumnDuration;
Chris@630 325 }
Chris@630 326 --valueCount;
Chris@630 327 }
Chris@630 328 }
Chris@630 329 }
Chris@630 330
Chris@631 331 if (timingColumnCount > 1) {
Chris@631 332 m_modelType = TwoDimensionalModelWithDuration;
Chris@392 333 } else {
Chris@631 334 if (valueCount == 0) {
Chris@631 335 m_modelType = OneDimensionalModel;
Chris@631 336 } else if (valueCount == 1) {
Chris@631 337 m_modelType = TwoDimensionalModel;
Chris@631 338 } else {
Chris@631 339 m_modelType = ThreeDimensionalModel;
Chris@631 340 }
Chris@629 341 }
Chris@392 342
Chris@1362 343 SVDEBUG << "Estimated column purposes: ";
Chris@1362 344 for (int i = 0; i < m_columnCount; ++i) {
Chris@1362 345 SVDEBUG << int(m_columnPurposes[i]) << " ";
Chris@1362 346 }
Chris@1362 347 SVDEBUG << endl;
Chris@392 348
Chris@1362 349 SVDEBUG << "Estimated model type: " << m_modelType << endl;
Chris@1362 350 SVDEBUG << "Estimated timing type: " << m_timingType << endl;
Chris@1362 351 SVDEBUG << "Estimated units: " << m_timeUnits << endl;
Chris@392 352 }
Chris@392 353
Chris@631 354 CSVFormat::ColumnPurpose
Chris@631 355 CSVFormat::getColumnPurpose(int i)
Chris@631 356 {
Chris@631 357 while (m_columnPurposes.size() <= i) {
Chris@631 358 m_columnPurposes.push_back(ColumnUnknown);
Chris@631 359 }
Chris@631 360 return m_columnPurposes[i];
Chris@631 361 }
Chris@629 362
Chris@631 363 CSVFormat::ColumnPurpose
Chris@631 364 CSVFormat::getColumnPurpose(int i) const
Chris@631 365 {
Chris@668 366 if (m_columnPurposes.size() <= i) {
Chris@668 367 return ColumnUnknown;
Chris@668 368 }
Chris@631 369 return m_columnPurposes[i];
Chris@631 370 }
Chris@631 371
Chris@631 372 void
Chris@631 373 CSVFormat::setColumnPurpose(int i, ColumnPurpose p)
Chris@631 374 {
Chris@631 375 while (m_columnPurposes.size() <= i) {
Chris@631 376 m_columnPurposes.push_back(ColumnUnknown);
Chris@631 377 }
Chris@631 378 m_columnPurposes[i] = p;
Chris@631 379 }
Chris@631 380
Chris@631 381
Chris@631 382
Chris@631 383