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 4fba90b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 49 deletions.
29 changes: 29 additions & 0 deletions COPYING.rapidcsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2017, Kristofer Berggren
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
58 changes: 11 additions & 47 deletions lib/src/Base/Stat/SampleImplementation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class CSVReader
// line may be incomplete
}
}
// dont accept empty components
// reject empty components
for (UnsignedInteger j = 0; j < description.getSize(); ++ j)
if (description[j].empty())
{
Expand Down 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 4fba90b

Please sign in to comment.