changeset 1142:870acd589a38

Add & test the digits option in the CSV file writer
author Chris Cannam
date Tue, 03 Nov 2015 14:31:58 +0000
parents 99acfd2d9f5d
children d649818fc249
files transform/CSVFeatureWriter.cpp transform/CSVFeatureWriter.h
diffstat 2 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/transform/CSVFeatureWriter.cpp	Tue Nov 03 12:41:15 2015 +0000
+++ b/transform/CSVFeatureWriter.cpp	Tue Nov 03 14:31:58 2015 +0000
@@ -37,7 +37,8 @@
     m_sampleTiming(false),
     m_endTimes(false),
     m_forceEnd(false),
-    m_omitFilename(false)
+    m_omitFilename(false),
+    m_digits(6)
 {
 }
 
@@ -82,6 +83,11 @@
     p.hasArg = false;
     pl.push_back(p);
 
+    p.name = "digits";
+    p.description = "Specify the number of significant digits to use when printing transform outputs. Outputs are represented internally using single-precision floating-point, so digits beyond the 8th or 9th place are usually meaningless. The default is 6.";
+    p.hasArg = true;
+    pl.push_back(p);
+
     return pl;
 }
 
@@ -108,6 +114,14 @@
             m_forceEnd = true;
         } else if (i->first == "omit-filename") {
             m_omitFilename = true;
+        } else if (i->first == "digits") {
+            int digits = atoi(i->second.c_str());
+            if (digits <= 0 || digits > 100) {
+                cerr << "CSVFeatureWriter: ERROR: Invalid or out-of-range value for number of significant digits: " << i->second << endl;
+                cerr << "CSVFeatureWriter: NOTE: Continuing with default settings" << endl;
+            } else {
+                m_digits = digits;
+            }
         }
     }
 }
@@ -262,7 +276,7 @@
     }
     
     for (unsigned int j = 0; j < f.values.size(); ++j) {
-        stream << m_separator << f.values[j];
+        stream << m_separator << QString("%1").arg(f.values[j], 0, 'g', m_digits);
     }
     
     if (f.label != "") {
--- a/transform/CSVFeatureWriter.h	Tue Nov 03 12:41:15 2015 +0000
+++ b/transform/CSVFeatureWriter.h	Tue Nov 03 14:31:58 2015 +0000
@@ -74,6 +74,8 @@
                       const Vamp::Plugin::Feature &f,
                       const Vamp::Plugin::Feature *optionalNextFeature,
                       std::string summaryType);
+
+    int m_digits;
 };
 
 #endif