Skip to content

Commit

Permalink
export
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Jan 6, 2025
1 parent 1176a9c commit 1f9622e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 48 deletions.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ This library bundles several third-party codes with various licenses compatible
- KS distribution from Cephes library (lib/src/Uncertainty/Distribution/cephes/*) under BSD license, see COPYING.cephes
- TNC optimization solver (lib/src/Base/Optim/algotnc.*) under Expat license, see COPYING.tnc
- Gauss Legendre quadrature from FastGL library (lib/src/Base/Algo/fastgl*) under BSD license, see COPYING.fastGL
- Rapidcsv parser library (lib/src/Base/Stat/rapidcsv.h) under BSD license, see COPYING.rapidcsv
56 changes: 10 additions & 46 deletions lib/src/Base/Stat/SampleImplementation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,12 @@ class CSVReader
SampleImplementation SampleImplementation::BuildFromTextFile(const FileName & fileName,
const String & separator,
const UnsignedInteger skippedLines,
const String & numSeparator)
const String & decimalSeparator)
{
if (separator.size() != 1) throw InvalidArgumentException(HERE) << "Expected a separator with one character, got separator=" << separator;
const char theSeparator = separator[0];
if (decimalSeparator.size() != 1)
throw InvalidArgumentException(HERE) << "Decimal separator must be a string of size 1, got " << decimalSeparator.size();

const String commentMarkers(ResourceMap::GetAsString("Sample-CommentMarkers"));
SampleImplementation impl(0, 0);
Expand All @@ -644,7 +646,7 @@ SampleImplementation SampleImplementation::BuildFromTextFile(const FileName & fi
reader.setAllowComments(true);
reader.setAllowEmptyLines(true);
reader.setSkippedLinesNumber(skippedLines);
reader.setNumericalSeparator(numSeparator[0]);
reader.setNumericalSeparator(decimalSeparator[0]);
return reader.load();
}

Expand Down Expand Up @@ -2303,27 +2305,19 @@ Pointer<SampleImplementation> SampleImplementation::select(const UnsignedInteger
/* Save to CSV file */
void SampleImplementation::exportToCSVFile(const FileName & filename,
const String & csvSeparator,
const String & numSeparator,
const String & decimalSeparator,
const UnsignedInteger precision,
const String & format) const
{
if (csvSeparator == numSeparator)
if (decimalSeparator.size() != 1)
throw InvalidArgumentException(HERE) << "Decimal separator must be a string of size 1, got " << decimalSeparator.size();
if (csvSeparator == decimalSeparator)
throw InvalidArgumentException(HERE) << "Column and decimal separators cannot be identical";
std::ofstream csvFile(filename.c_str());
if (csvFile.fail())
throw FileOpenException(HERE) << "Could not open file " << filename;
#ifndef __MINGW32__
if (numSeparator == ",")
#ifdef _WIN32
csvFile.imbue(std::locale("fra_FRA.1252"));
#else
csvFile.imbue(std::locale("fr_FR.utf-8"));
#endif
else
csvFile.imbue(std::locale("C"));
#else
csvFile.imbue(std::locale("C"));
#endif

csvFile.imbue(std::locale(std::locale::classic(), new CSVNumericalFormat(decimalSeparator[0])));

// Export the description
if (!p_description_.isNull())
Expand Down Expand Up @@ -2351,43 +2345,13 @@ void SampleImplementation::exportToCSVFile(const FileName & filename,

// Write the data
UnsignedInteger index = 0;
#ifdef __MINGW32__
// MinGW fails with std::locale("fr_FR.utf-8")
std::stringstream ss;
ss.imbue(std::locale("C"));
ss.precision(precision);
if (format == "scientific")
ss << std::scientific;
else if (format == "fixed")
ss << std::fixed;
#endif
for(UnsignedInteger i = 0; i < size_; ++i)
{
#ifndef __MINGW32__
csvFile << data_[index];
#else
// manually replace decimal separator
ss.str("");
ss << data_[index];
std::string str(ss.str());
if (numSeparator == ",")
str = regex_replace(str, std::regex("\\."), ",");
csvFile << str;
#endif
++index;
for(UnsignedInteger j = 1; j < dimension_; ++j)
{
#ifndef __MINGW32__
csvFile << csvSeparator << data_[index];
#else
// manually replace decimal separator
ss.str("");
ss << data_[index];
str = ss.str();
if (numSeparator == ",")
str = regex_replace(str, std::regex("\\."), ",");
csvFile << csvSeparator << str;
#endif
++index;
} // j
csvFile << "\n";
Expand Down
4 changes: 2 additions & 2 deletions lib/src/Base/Stat/openturns/SampleImplementation.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public:
static SampleImplementation BuildFromTextFile(const FileName & fileName,
const String & separator = " ",
const UnsignedInteger skippedLines = 0,
const String & numSeparator = ".");
const String & decimalSeparator = ".");

protected:

Expand Down Expand Up @@ -787,7 +787,7 @@ public:
/** Save to CSV file */
void exportToCSVFile(const FileName & filename,
const String & csvSeparator = ResourceMap::GetAsString("Sample-CSVFileSeparator"),
const String & numSeparator = ".",
const String & decimalSeparator = ".",
const UnsignedInteger precision = ResourceMap::GetAsUnsignedInteger("Sample-CSVPrecision"),
const String & format = ResourceMap::Get("Sample-CSVFormat")) const;

Expand Down

0 comments on commit 1f9622e

Please sign in to comment.