BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
OutputDataReadWriteNumpyTXT Class Reference

Class for reading and writing OutputData from simple ASCII file with the layout as in numpy.savetxt. More...

Public Member Functions

OutputData< double > * readOutputData (std::istream &input_stream)
 
void writeOutputData (const OutputData< double > &data, std::ostream &output_stream)
 

Static Private Member Functions

static double ignoreDenormalized (double value)
 
static void write1DRepresentation (const OutputData< double > &data, std::ostream &output_stream)
 
static void write2DRepresentation (const OutputData< double > &data, std::ostream &output_stream)
 

Detailed Description

Class for reading and writing OutputData from simple ASCII file with the layout as in numpy.savetxt.

Definition at line 31 of file OutputDataReadWriteNumpyTXT.h.

Member Function Documentation

◆ ignoreDenormalized()

double OutputDataReadWriteNumpyTXT::ignoreDenormalized ( double  value)
staticprivate

Definition at line 133 of file OutputDataReadWriteNumpyTXT.cpp.

134 {
135  return (std::fpclassify(value) == FP_SUBNORMAL) ? 0.0 : value;
136 }

Referenced by write1DRepresentation(), and write2DRepresentation().

◆ readOutputData()

OutputData< double > * OutputDataReadWriteNumpyTXT::readOutputData ( std::istream &  input_stream)

Definition at line 29 of file OutputDataReadWriteNumpyTXT.cpp.

30 {
31  std::string line;
32  std::vector<std::vector<double>> data;
33 
34  // Read numbers from input stream:
35  while (std::getline(input_stream, line)) {
36  line = StringUtils::trim(line);
37  if (line.empty() || !isDoubleStartChar(line[0]))
38  continue;
39 
40  try {
41  std::vector<double> dataInRow = DataFormatUtils::parse_doubles(line);
42  data.push_back(dataInRow);
43  } catch (...) {
44  continue;
45  }
46  }
47 
48  // validating
49  size_t nrows = data.size();
50  size_t ncols(0);
51  if (nrows)
52  ncols = data[0].size();
53 
54  if (ncols == 0)
55  throw std::runtime_error("OutputDataReadNumpyTXTStrategy::readOutputData() -> Error. "
56  "Can't parse file");
57 
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.");
62  }
63 
64  if (nrows < 2)
65  return ArrayUtils::createData(std::move(data[0])).release();
66 
67  if (ncols < 2) {
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];
72  return ArrayUtils::createData(std::move(vector1d)).release();
73  }
74 
75  return ArrayUtils::createData(data).release();
76 }
CreateDataImpl::ReturnType< T > createData(const T &vec)
Creates OutputData array from input vector.
Definition: ArrayUtils.h:65
std::vector< double > parse_doubles(const std::string &str)
Parse double values from string to vector of double.
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.
Definition: StringUtils.cpp:98

References ArrayUtils::createData(), DataFormatUtils::parse_doubles(), and StringUtils::trim().

Referenced by IntensityDataIOFactory::readOutputData().

Here is the call graph for this function:

◆ write1DRepresentation()

void OutputDataReadWriteNumpyTXT::write1DRepresentation ( const OutputData< double > &  data,
std::ostream &  output_stream 
)
staticprivate

Definition at line 98 of file OutputDataReadWriteNumpyTXT.cpp.

100 {
101  output_stream << "# coordinates intensities" << std::endl;
102  output_stream.imbue(std::locale::classic());
103  output_stream << std::scientific << std::setprecision(12);
104 
105  const std::vector<double> axis_values = data.axis(0).binCenters();
106 
107  // printing coordinate and associated intensity
108  for (size_t i = 0, nrows = axis_values.size(); i < nrows; ++i)
109  output_stream << axis_values[i] << " " << ignoreDenormalized(data[i]) << std::endl;
110 }
virtual std::vector< double > binCenters() const
Definition: IAxis.cpp:22
static double ignoreDenormalized(double value)
const IAxis & axis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:318
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:61

References OutputData< T >::axis(), IAxis::binCenters(), ignoreDenormalized(), and StringUtils::scientific().

Referenced by writeOutputData().

Here is the call graph for this function:

◆ write2DRepresentation()

void OutputDataReadWriteNumpyTXT::write2DRepresentation ( const OutputData< double > &  data,
std::ostream &  output_stream 
)
staticprivate

Definition at line 112 of file OutputDataReadWriteNumpyTXT.cpp.

114 {
115  const size_t nrows = data.axis(1).size();
116  const size_t ncols = data.axis(0).size();
117 
118  output_stream << "# [nrows=" << nrows << ", ncols=" << ncols << "]" << std::endl;
119 
120  std::vector<std::vector<double>> dataArray = ArrayUtils::createVector2D(data);
121  output_stream.imbue(std::locale::classic());
122  output_stream << std::scientific << std::setprecision(12);
123 
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];
127  output_stream << ignoreDenormalized(z_value) << " ";
128  }
129  output_stream << std::endl;
130  }
131 }
virtual size_t size() const =0
retrieve the number of bins
decltype(auto) createVector2D(const T &data)
Creates 2D vector from OutputData.

References OutputData< T >::axis(), ArrayUtils::createVector2D(), ignoreDenormalized(), StringUtils::scientific(), and IAxis::size().

Referenced by writeOutputData().

Here is the call graph for this function:

◆ writeOutputData()

void OutputDataReadWriteNumpyTXT::writeOutputData ( const OutputData< double > &  data,
std::ostream &  output_stream 
)

Definition at line 78 of file OutputDataReadWriteNumpyTXT.cpp.

80 {
81  output_stream << "# BornAgain Intensity Data" << std::endl;
82  output_stream << "# Simple array suitable for numpy, matlab etc." << std::endl;
83 
84  const size_t dim = data.rank();
85  switch (dim) {
86  case 1:
87  write1DRepresentation(data, output_stream);
88  break;
89  case 2:
90  write2DRepresentation(data, output_stream);
91  break;
92  default:
93  throw std::runtime_error("Error in OutputDataWriteNumpyTXTStrategy::writeOutputData: data "
94  "of unsupported dimensions");
95  }
96 }
static void write2DRepresentation(const OutputData< double > &data, std::ostream &output_stream)
static void write1DRepresentation(const OutputData< double > &data, std::ostream &output_stream)
size_t rank() const
Returns number of dimensions.
Definition: OutputData.h:56

References OutputData< T >::rank(), write1DRepresentation(), and write2DRepresentation().

Referenced by IntensityDataIOFactory::writeOutputData().

Here is the call graph for this function:

The documentation for this class was generated from the following files: