BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
DataUtils Namespace Reference

Functions

bool checkRelativeDifference (const OutputData< double > &dat, const OutputData< double > &ref, const double threshold)
 Returns true is relative difference is below threshold; prints informative output. More...
 
void coordinateFromBinf (double &x, double &y, const OutputData< double > &data)
 Transforms x,y coordinate from bin-fraction-coordinates to OutputData's axes coordinates. More...
 
double coordinateFromBinf (double value, const IAxis &axis)
 Transforms bin-fraction-coordinate into axis coordinate. More...
 
void coordinateToBinf (double &x, double &y, const OutputData< double > &data)
 Transforms x,y coordinate from OutputData axes coordinates to bin-fraction-coordinates. More...
 
double coordinateToBinf (double coordinate, const IAxis &axis)
 Transforms coordinate on axis into the bin-fraction-coordinate. More...
 
std::vector< std::vector< double > > create2DArrayfromOutputData (const OutputData< double > &data)
 Creates a vector of vectors of double (2D Array) from OutputData. More...
 
std::unique_ptr< OutputData< double > > createClippedDataSet (const OutputData< double > &origin, double x1, double y1, double x2, double y2)
 Returns new IntensityData objects which axes clipped to represent the specified rectangle. More...
 
std::unique_ptr< OutputData< double > > createFFT (const OutputData< double > &data)
 Creates Fourier Transform (OutputData format) of intensity map (OutputData format). More...
 
std::unique_ptr< OutputData< double > > createOutputDatafrom2DArray (const std::vector< std::vector< double >> &array_2d)
 Creates OutputData from a 2D Array. More...
 
std::unique_ptr< OutputData< double > > createRearrangedDataSet (const OutputData< double > &data, int n)
 Returns new object with input data rotated by n*90 deg counterclockwise (n > 0) or clockwise (n < 0) Axes are swapped if the data is effectively rotated by 90 or 270 degrees Applicable to 2D arrays only. More...
 
std::unique_ptr< OutputData< double > > createRelativeDifferenceData (const OutputData< double > &data, const OutputData< double > &reference)
 
OutputData< double > * importArrayToOutputData (const std::vector< double > &vec)
 Reads 1D array of doubles to Python, for use in persistence test. More...
 
OutputData< double > * importArrayToOutputData (const std::vector< std::vector< double >> &vec)
 Reads 2D array of doubles to Python, for use in persistence test. More...
 
double relativeDataDifference (const OutputData< double > &dat, const OutputData< double > &ref)
 Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]). More...
 

Function Documentation

◆ checkRelativeDifference()

bool DataUtils::checkRelativeDifference ( const OutputData< double > &  dat,
const OutputData< double > &  ref,
const double  threshold 
)

Returns true is relative difference is below threshold; prints informative output.

Definition at line 51 of file DataUtils.cpp.

53 {
54  const double diff = relativeDataDifference(dat, ref);
55  if (diff > threshold) {
56  std::cerr << "FAILED: relative deviation of dat from ref is " << diff
57  << ", above given threshold " << threshold << std::endl;
58  return false;
59  }
60  if (diff)
61  std::cerr << "- OK: relative deviation of dat from ref is " << diff
62  << ", within given threshold " << threshold << std::endl;
63  else
64  std::cout << "- OK: dat = ref\n";
65  return true;
66 }
double relativeDataDifference(const OutputData< double > &dat, const OutputData< double > &ref)
Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
Definition: DataUtils.cpp:35

References relativeDataDifference().

Referenced by HistoUtils::agreesWithReference().

Here is the call graph for this function:

◆ coordinateFromBinf() [1/2]

void DataUtils::coordinateFromBinf ( double &  x,
double &  y,
const OutputData< double > &  data 
)

Transforms x,y coordinate from bin-fraction-coordinates to OutputData's axes coordinates.

Definition at line 202 of file DataUtils.cpp.

203 {
204  x = coordinateFromBinf(x, data.axis(0));
205  y = coordinateFromBinf(y, data.axis(1));
206 }
const IAxis & axis(size_t serial_number) const
returns axis with given serial number
Definition: OutputData.h:318
double coordinateFromBinf(double value, const IAxis &axis)
Transforms bin-fraction-coordinate into axis coordinate.
Definition: DataUtils.cpp:177

References OutputData< T >::axis(), and coordinateFromBinf().

Here is the call graph for this function:

◆ coordinateFromBinf() [2/2]

double DataUtils::coordinateFromBinf ( double  value,
const IAxis axis 
)

Transforms bin-fraction-coordinate into axis coordinate.

Definition at line 177 of file DataUtils.cpp.

178 {
179  int index = static_cast<int>(value);
180 
181  double result(0);
182  if (index < 0) {
183  Bin1D bin = axis.bin(0);
184  result = bin.m_lower + value * bin.binSize();
185  } else if (index >= static_cast<int>(axis.size())) {
186  Bin1D bin = axis.bin(axis.size() - 1);
187  result = bin.m_upper + (value - axis.size()) * bin.binSize();
188  } else {
189  Bin1D bin = axis.bin(static_cast<size_t>(index));
190  result = bin.m_lower + (value - static_cast<double>(index)) * bin.binSize();
191  }
192 
193  return result;
194 }
virtual Bin1D bin(size_t index) const =0
retrieve a 1d bin for the given index
virtual size_t size() const =0
retrieve the number of bins
Definition: Bin.h:20
double binSize() const
Definition: Bin.h:26
double m_upper
upper bound of the bin
Definition: Bin.h:24
double m_lower
lower bound of the bin
Definition: Bin.h:23

References IAxis::bin(), Bin1D::binSize(), Bin1D::m_lower, Bin1D::m_upper, and IAxis::size().

Referenced by MaskUnitsConverter::convert(), MaskUnitsConverter::convertMask(), and coordinateFromBinf().

Here is the call graph for this function:

◆ coordinateToBinf() [1/2]

void DataUtils::coordinateToBinf ( double &  x,
double &  y,
const OutputData< double > &  data 
)

Transforms x,y coordinate from OutputData axes coordinates to bin-fraction-coordinates.

Definition at line 196 of file DataUtils.cpp.

197 {
198  x = coordinateToBinf(x, data.axis(0));
199  y = coordinateToBinf(y, data.axis(1));
200 }
double coordinateToBinf(double coordinate, const IAxis &axis)
Transforms coordinate on axis into the bin-fraction-coordinate.
Definition: DataUtils.cpp:169

References OutputData< T >::axis(), and coordinateToBinf().

Here is the call graph for this function:

◆ coordinateToBinf() [2/2]

double DataUtils::coordinateToBinf ( double  coordinate,
const IAxis axis 
)

Transforms coordinate on axis into the bin-fraction-coordinate.

Definition at line 169 of file DataUtils.cpp.

170 {
171  size_t index = axis.findClosestIndex(coordinate);
172  Bin1D bin = axis.bin(index);
173  double f = (coordinate - bin.m_lower) / bin.binSize();
174  return static_cast<double>(index) + f;
175 }
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value

References IAxis::bin(), Bin1D::binSize(), IAxis::findClosestIndex(), and Bin1D::m_lower.

Referenced by MaskUnitsConverter::convert(), MaskUnitsConverter::convertMask(), and coordinateToBinf().

Here is the call graph for this function:

◆ create2DArrayfromOutputData()

std::vector< std::vector< double > > DataUtils::create2DArrayfromOutputData ( const OutputData< double > &  data)

Creates a vector of vectors of double (2D Array) from OutputData.

Definition at line 209 of file DataUtils.cpp.

210 {
211  if (data.rank() != 2)
212  throw std::runtime_error("DataUtils::create2DArrayfromOutputData() -> "
213  "Error! Works only on two-dimensional data");
214 
215  std::vector<std::vector<double>> array_2d;
216  std::vector<double> row_vec; // row vector for constructing each row of 2D array
217 
218  size_t nrows = data.axis(0).size();
219  size_t ncols = data.axis(1).size();
220 
221  size_t it = 0; // iterator of 'data'
222  for (size_t row = 0; row < nrows; row++) {
223  row_vec.clear();
224  for (size_t col = 0; col < ncols; col++) {
225  row_vec.push_back(data[it]);
226  it++;
227  }
228  array_2d.push_back(row_vec);
229  }
230 
231  return array_2d;
232 }
size_t rank() const
Returns number of dimensions.
Definition: OutputData.h:56

References OutputData< T >::axis(), OutputData< T >::rank(), and IAxis::size().

Referenced by createFFT().

Here is the call graph for this function:

◆ createClippedDataSet()

std::unique_ptr< OutputData< double > > DataUtils::createClippedDataSet ( const OutputData< double > &  origin,
double  x1,
double  y1,
double  x2,
double  y2 
)

Returns new IntensityData objects which axes clipped to represent the specified rectangle.

Definition at line 128 of file DataUtils.cpp.

130 {
131  if (origin.rank() != 2)
132  throw std::runtime_error("DataUtils::createClippedData()"
133  " -> Error! Works only on two-dimensional data");
134 
135  std::unique_ptr<OutputData<double>> result(new OutputData<double>);
136  for (size_t i_axis = 0; i_axis < origin.rank(); i_axis++) {
137  const IAxis& axis = origin.axis(i_axis);
138  IAxis* new_axis;
139  if (i_axis == 0)
140  new_axis = axis.createClippedAxis(x1, x2);
141  else
142  new_axis = axis.createClippedAxis(y1, y2);
143  result->addAxis(*new_axis);
144  delete new_axis;
145  }
146  result->setAllTo(0.0);
147 
148  OutputData<double>::const_iterator it_origin = origin.begin();
149  OutputData<double>::iterator it_result = result->begin();
150  while (it_origin != origin.end()) {
151  double x = origin.getAxisValue(it_origin.getIndex(), 0);
152  double y = origin.getAxisValue(it_origin.getIndex(), 1);
153  if (result->axis(0).contains(x) && result->axis(1).contains(y)) {
154  *it_result = *it_origin;
155  ++it_result;
156  }
157  ++it_origin;
158  }
159 
160  return result;
161 }
Interface for one-dimensional axes.
Definition: IAxis.h:25
virtual IAxis * createClippedAxis(double left, double right) const
Creates a new clipped axis.
Definition: IAxis.cpp:32
iterator end()
Returns read/write iterator that points to the one past last element.
Definition: OutputData.h:93
iterator begin()
Returns read/write iterator that points to the first element.
Definition: OutputData.h:343
double getAxisValue(size_t global_index, size_t i_selected_axis) const
Returns the value of selected axis for given global_index.
Definition: OutputData.h:430

References OutputData< T >::axis(), OutputData< T >::begin(), IAxis::createClippedAxis(), OutputData< T >::end(), OutputData< T >::getAxisValue(), and OutputData< T >::rank().

Here is the call graph for this function:

◆ createFFT()

std::unique_ptr< OutputData< double > > DataUtils::createFFT ( const OutputData< double > &  data)

Creates Fourier Transform (OutputData format) of intensity map (OutputData format).

Definition at line 256 of file DataUtils.cpp.

257 {
258  auto array_2d = DataUtils::create2DArrayfromOutputData(data);
259  auto fft_array_2d = FT2DArray(array_2d);
260  return DataUtils::createOutputDatafrom2DArray(fft_array_2d);
261 }
std::vector< std::vector< double > > create2DArrayfromOutputData(const OutputData< double > &data)
Creates a vector of vectors of double (2D Array) from OutputData.
Definition: DataUtils.cpp:209
std::unique_ptr< OutputData< double > > createOutputDatafrom2DArray(const std::vector< std::vector< double >> &array_2d)
Creates OutputData from a 2D Array.
Definition: DataUtils.cpp:235

References create2DArrayfromOutputData(), and createOutputDatafrom2DArray().

Referenced by IntensityDataFFTPresenter::fftItem().

Here is the call graph for this function:

◆ createOutputDatafrom2DArray()

std::unique_ptr< OutputData< double > > DataUtils::createOutputDatafrom2DArray ( const std::vector< std::vector< double >> &  array_2d)

Creates OutputData from a 2D Array.

Definition at line 235 of file DataUtils.cpp.

236 {
237  std::unique_ptr<OutputData<double>> result(new OutputData<double>);
238  size_t nrows = array_2d.size();
239  size_t ncols = array_2d[0].size();
240 
241  result->addAxis("x", nrows, 0.0, double(nrows));
242  result->addAxis("y", ncols, 0.0, double(ncols));
243  std::vector<unsigned> axes_indices(2);
244  for (unsigned row = 0; row < nrows; row++) {
245  for (unsigned col = 0; col < ncols; col++) {
246  axes_indices[0] = row;
247  axes_indices[1] = col;
248  size_t global_index = result->toGlobalIndex(axes_indices);
249  (*result)[global_index] = array_2d[row][col];
250  }
251  }
252 
253  return result;
254 }

Referenced by createFFT().

◆ createRearrangedDataSet()

std::unique_ptr< OutputData< double > > DataUtils::createRearrangedDataSet ( const OutputData< double > &  data,
int  n 
)

Returns new object with input data rotated by n*90 deg counterclockwise (n > 0) or clockwise (n < 0) Axes are swapped if the data is effectively rotated by 90 or 270 degrees Applicable to 2D arrays only.

Definition at line 82 of file DataUtils.cpp.

83 {
84  if (data.rank() != 2)
85  throw std::runtime_error("DataUtils::rotateDataByN90Deg()"
86  " -> Error! Works only on two-dimensional data");
87  n = (4 + n % 4) % 4;
88  if (n == 0)
89  return std::unique_ptr<OutputData<double>>(data.clone());
90  std::unique_ptr<OutputData<double>> output(new OutputData<double>());
91 
92  // swapping axes if necessary
93  const IAxis& x_axis = data.axis(0);
94  const IAxis& y_axis = data.axis(1);
95  output->addAxis(n == 2 ? x_axis : y_axis);
96  output->addAxis(n == 2 ? y_axis : x_axis);
97 
98  // creating index mapping
99  std::function<void(std::vector<int>&)> index_mapping;
100  if (n == 2) {
101  const int end_bin_x = static_cast<int>(x_axis.size()) - 1;
102  const int end_bin_y = static_cast<int>(y_axis.size()) - 1;
103  index_mapping = [end_bin_x, end_bin_y](std::vector<int>& inds) {
104  inds[0] = end_bin_x - inds[0];
105  inds[1] = end_bin_y - inds[1];
106  };
107  } else {
108  const size_t rev_axis_i = n % 3;
109  const size_t end_bin = data.axis(rev_axis_i).size() - 1;
110  index_mapping = [rev_axis_i, end_bin](std::vector<int>& inds) {
111  const int tm_index = inds[rev_axis_i];
112  inds[rev_axis_i] = inds[rev_axis_i ^ 1];
113  inds[rev_axis_i ^ 1] = static_cast<int>(end_bin) - tm_index;
114  };
115  }
116 
117  for (size_t index = 0, size = data.getAllocatedSize(); index < size; ++index) {
118  std::vector<int> axis_inds = data.getAxesBinIndices(index);
119  index_mapping(axis_inds);
120  size_t output_index = output->toGlobalIndex(
121  {static_cast<unsigned>(axis_inds[0]), static_cast<unsigned>(axis_inds[1])});
122  (*output)[output_index] = data[index];
123  }
124  return output;
125 }
std::vector< int > getAxesBinIndices(size_t global_index) const
Returns vector of axes indices for given global index.
Definition: OutputData.h:355
size_t getAllocatedSize() const
Returns total size of data buffer (product of bin number in every dimension).
Definition: OutputData.h:59
OutputData * clone() const
Definition: OutputData.h:259

References OutputData< T >::axis(), OutputData< T >::clone(), OutputData< T >::getAllocatedSize(), OutputData< T >::getAxesBinIndices(), OutputData< T >::rank(), and IAxis::size().

Referenced by RealDataItem::rotateData().

Here is the call graph for this function:

◆ createRelativeDifferenceData()

std::unique_ptr< OutputData< double > > DataUtils::createRelativeDifferenceData ( const OutputData< double > &  data,
const OutputData< double > &  reference 
)

Definition at line 69 of file DataUtils.cpp.

71 {
72  if (!data.hasSameDimensions(reference))
73  throw std::runtime_error("DataUtils::createRelativeDifferenceData() -> "
74  "Error. Different dimensions of data and reference.");
75  std::unique_ptr<OutputData<double>> result(reference.clone());
76  for (size_t i = 0; i < result->getAllocatedSize(); ++i)
77  (*result)[i] = Numeric::GetRelativeDifference(data[i], reference[i]);
78  return result;
79 }
bool hasSameDimensions(const OutputData< U > &right) const
Returns true if object have same dimensions and number of axes bins.
Definition: OutputData.h:575
double GetRelativeDifference(double a, double b)
Returns the safe relative difference, which is 2(|a-b|)/(|a|+|b|) except in special cases.
Definition: Numeric.cpp:29

References OutputData< T >::clone(), Numeric::GetRelativeDifference(), and OutputData< T >::hasSameDimensions().

Referenced by DiffItemController::updateDiffData().

Here is the call graph for this function:

◆ importArrayToOutputData() [1/2]

OutputData< double > * DataUtils::importArrayToOutputData ( const std::vector< double > &  vec)

Reads 1D array of doubles to Python, for use in persistence test.

Definition at line 263 of file DataUtils.cpp.

264 {
265  return ArrayUtils::createData(vec).release();
266 }
CreateDataImpl::ReturnType< T > createData(const T &vec)
Creates OutputData array from input vector.
Definition: ArrayUtils.h:65

References ArrayUtils::createData().

Here is the call graph for this function:

◆ importArrayToOutputData() [2/2]

OutputData< double > * DataUtils::importArrayToOutputData ( const std::vector< std::vector< double >> &  vec)

Reads 2D array of doubles to Python, for use in persistence test.

Definition at line 268 of file DataUtils.cpp.

269 {
270  return ArrayUtils::createData(vec).release();
271 }

References ArrayUtils::createData().

Here is the call graph for this function:

◆ relativeDataDifference()

double DataUtils::relativeDataDifference ( const OutputData< double > &  dat,
const OutputData< double > &  ref 
)

Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).

Definition at line 35 of file DataUtils.cpp.

37 {
38  if (!dat.hasSameDimensions(ref))
39  throw std::runtime_error("OutputData dimension differs from reference");
40 
41  double diff = 0.0;
42  for (size_t i = 0; i < dat.getAllocatedSize(); ++i)
43  diff += Numeric::GetRelativeDifference(dat[i], ref[i]);
44  diff /= dat.getAllocatedSize();
45  if (std::isnan(diff))
46  throw std::runtime_error("diff=NaN!");
47  return diff;
48 }

References OutputData< T >::getAllocatedSize(), Numeric::GetRelativeDifference(), and OutputData< T >::hasSameDimensions().

Referenced by checkRelativeDifference(), and HistoUtils::getRelativeDifference().

Here is the call graph for this function: