23 inline bool isDoubleStartChar(
char c)
25 return isdigit(c) || c ==
'-' || c ==
'+';
32 std::vector<std::vector<double>> data;
35 while (std::getline(input_stream, line)) {
37 if (line.empty() || !isDoubleStartChar(line[0]))
42 data.push_back(dataInRow);
49 size_t nrows = data.size();
52 ncols = data[0].size();
55 throw std::runtime_error(
"OutputDataReadNumpyTXTStrategy::readOutputData() -> Error. "
58 for (
size_t row = 0; row < nrows; row++) {
59 if (data[row].size() != ncols)
60 throw std::runtime_error(
"OutputDataReadNumpyTXTStrategy::readOutputData() -> Error. "
61 "Number of elements is different from row to row.");
68 const size_t size = data.size();
69 std::vector<double> vector1d(size);
70 for (
size_t i = 0; i < size; ++i)
71 vector1d[i] = data[i][0];
79 std::ostream& output_stream)
81 output_stream <<
"# BornAgain Intensity Data" << std::endl;
82 output_stream <<
"# Simple array suitable for numpy, matlab etc." << std::endl;
84 const size_t dim = data.
rank();
93 throw std::runtime_error(
"Error in OutputDataWriteNumpyTXTStrategy::writeOutputData: data "
94 "of unsupported dimensions");
99 std::ostream& output_stream)
101 output_stream <<
"# coordinates intensities" << std::endl;
102 output_stream.imbue(std::locale::classic());
105 const std::vector<double> axis_values = data.
axis(0).
binCenters();
108 for (
size_t i = 0, nrows = axis_values.size(); i < nrows; ++i)
113 std::ostream& output_stream)
115 const size_t nrows = data.
axis(1).
size();
116 const size_t ncols = data.
axis(0).
size();
118 output_stream <<
"# [nrows=" << nrows <<
", ncols=" << ncols <<
"]" << std::endl;
121 output_stream.imbue(std::locale::classic());
124 for (
size_t i = 0; i < nrows; i++) {
125 for (
size_t j = 0; j < ncols; j++) {
126 double z_value = dataArray[i][j];
129 output_stream << std::endl;
135 return (std::fpclassify(value) == FP_SUBNORMAL) ? 0.0 : value;
Defines various functions to interact from numpy on Python side.
Defines a few helper functions.
Defines OutputDataReadWriteNumpyTXT.
virtual std::vector< double > binCenters() const
virtual size_t size() const =0
retrieve the number of bins
static double ignoreDenormalized(double value)
static void write2DRepresentation(const OutputData< double > &data, std::ostream &output_stream)
void writeOutputData(const OutputData< double > &data, std::ostream &output_stream)
static void write1DRepresentation(const OutputData< double > &data, std::ostream &output_stream)
OutputData< double > * readOutputData(std::istream &input_stream)
size_t rank() const
Returns number of dimensions.
const IAxis & axis(size_t serial_number) const
returns axis with given serial number
decltype(auto) createVector2D(const T &data)
Creates 2D vector from OutputData.
CreateDataImpl::ReturnType< T > createData(const T &vec)
Creates OutputData array from input vector.
std::string trim(const std::string &str, const std::string &whitespace=" \t")
Cuts any of the chars given in whitespace from start and end of str.
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.