31 if (dat.size() != ref.size())
32 throw std::runtime_error(
"Error in IntensityDataFunctions::RelativeDifference: "
33 "different number of elements");
36 double sum_of_diff = 0.0;
37 for (
size_t i = 0; i < dat.size(); ++i) {
40 return sum_of_diff / dat.size();
49 "IntensityDataFunctions::getRelativeDifference() -> "
50 "Error. Different dimensions of data and reference.");
71 const double threshold)
74 if (diff > threshold) {
75 std::cerr <<
" => FAILED: relative deviation of dat from ref is " << diff
76 <<
", above given threshold " << threshold <<
"\n";
80 std::cerr <<
" => OK: relative deviation of dat from ref is " << diff
81 <<
", within given threshold " << threshold <<
"\n";
83 std::cout <<
" => OK: dat = ref\n";
87 std::unique_ptr<OutputData<double>>
93 "IntensityDataFunctions::createRelativeDifferenceData() -> "
94 "Error. Different dimensions of data and reference.");
95 std::unique_ptr<OutputData<double>> result(reference.clone());
96 for (
size_t i = 0; i < result->getAllocatedSize(); ++i)
101 std::unique_ptr<OutputData<double>>
106 " -> Error! Works only on two-dimensional data");
109 return std::unique_ptr<OutputData<double>>(data.clone());
115 output->addAxis(n == 2 ? x_axis : y_axis);
116 output->addAxis(n == 2 ? y_axis : x_axis);
119 std::function<void(std::vector<int>&)> index_mapping;
121 const int end_bin_x =
static_cast<int>(x_axis.
size()) - 1;
122 const int end_bin_y =
static_cast<int>(y_axis.
size()) - 1;
123 index_mapping = [end_bin_x, end_bin_y](std::vector<int>& inds) {
124 inds[0] = end_bin_x - inds[0];
125 inds[1] = end_bin_y - inds[1];
128 const size_t rev_axis_i = n % 3;
129 const size_t end_bin = data.
getAxis(rev_axis_i).
size() - 1;
130 index_mapping = [rev_axis_i, end_bin](std::vector<int>& inds) {
131 const int tmp_index = inds[rev_axis_i];
132 inds[rev_axis_i] = inds[rev_axis_i ^ 1];
133 inds[rev_axis_i ^ 1] =
static_cast<int>(end_bin) - tmp_index;
137 for (
size_t index = 0, size = data.
getAllocatedSize(); index < size; ++index) {
139 index_mapping(axis_inds);
140 size_t output_index = output->toGlobalIndex(
141 {
static_cast<unsigned>(axis_inds[0]),
static_cast<unsigned>(axis_inds[1])});
142 (*output)[output_index] = data[index];
147 std::unique_ptr<OutputData<double>>
149 double x2,
double y2)
153 " -> Error! Works only on two-dimensional data");
156 for (
size_t i_axis = 0; i_axis < origin.
getRank(); i_axis++) {
163 result->addAxis(*new_axis);
166 result->setAllTo(0.0);
170 while (it_origin != origin.
end()) {
171 double x = origin.
getAxisValue(it_origin.getIndex(), 0);
172 double y = origin.
getAxisValue(it_origin.getIndex(), 1);
173 if (result->getAxis(0).contains(x) && result->getAxis(1).contains(y)) {
174 *it_result = *it_origin;
193 double f = (coordinate - bin.
m_lower) / bin.getBinSize();
194 return static_cast<double>(index) + f;
199 int index =
static_cast<int>(value);
204 result = bin.
m_lower + value * bin.getBinSize();
205 }
else if (index >=
static_cast<int>(axis.
size())) {
207 result = bin.
m_upper + (value - axis.
size()) * bin.getBinSize();
209 Bin1D bin = axis.
getBin(
static_cast<size_t>(index));
210 result = bin.
m_lower + (value -
static_cast<double>(index)) * bin.getBinSize();
229 std::vector<std::vector<double>>
234 "IntensityDataFunctions::create2DArrayfromOutputData() -> "
235 "Error! Works only on two-dimensional data");
237 std::vector<std::vector<double>> array_2d;
238 std::vector<double> row_vec;
244 for (
size_t row = 0; row < nrows; row++) {
246 for (
size_t col = 0; col < ncols; col++) {
247 row_vec.push_back(data[it]);
250 array_2d.push_back(row_vec);
256 std::vector<std::vector<double>>
260 std::vector<std::vector<double>> fft_array;
261 ft.
fft(signal, fft_array);
269 const std::vector<std::vector<double>>& array_2d)
272 size_t nrows = array_2d.size();
273 size_t ncols = array_2d[0].size();
275 result->addAxis(
"x", nrows, 0.0,
double(nrows));
276 result->addAxis(
"y", ncols, 0.0,
double(ncols));
277 std::vector<unsigned> axes_indices(2);
278 for (
unsigned row = 0; row < nrows; row++) {
279 for (
unsigned col = 0; col < ncols; col++) {
280 axes_indices[0] = row;
281 axes_indices[1] = col;
282 size_t global_index = result->toGlobalIndex(axes_indices);
283 (*result)[global_index] = array_2d[row][col];
290 std::unique_ptr<OutputData<double>>
Defines various functions to interact from numpy on Python side.
Defines class ConvolutionDetectorResolution.
Defines class IHistogram.
Defines class Instrument.
Defines class IntensityDataFunctions.
Defines constants and "almost equal" in namespace Numeric.
Defines class SimulationResult.
Interface for one-dimensional axes.
virtual IAxis * createClippedAxis(double left, double right) const
Creates a new clipped axis.
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value
virtual Bin1D getBin(size_t index) const =0
retrieve a 1d bin for the given index
virtual size_t size() const =0
retrieve the number of bins
Base class for 1D and 2D histograms holding values of double type.
iterator end()
Returns read/write iterator that points to the one past last element.
std::vector< int > getAxesBinIndices(size_t global_index) const
Returns vector of axes indices for given global index.
size_t getRank() const
Returns number of dimensions.
const IAxis & getAxis(size_t serial_number) const
returns axis with given serial number
bool hasSameDimensions(const OutputData< U > &right) const
Returns true if object have same dimensions and number of axes bins.
iterator begin()
Returns read/write iterator that points to the first element.
size_t getAllocatedSize() const
Returns total size of data buffer (product of bin number in every dimension).
double getAxisValue(size_t global_index, size_t i_selected_axis) const
Returns the value of selected axis for given global_index.
Wrapper around OutputData<double> that also provides unit conversions.
double getRelativeDifference(const OutputData< double > &dat, const OutputData< double > &ref)
Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
std::unique_ptr< OutputData< double > > createFFT(const OutputData< double > &data)
Creates Fourier Transform (OutputData format) of intensity map (OutputData format).
double RelativeDifference(const SimulationResult &dat, const SimulationResult &ref)
Returns sum of relative differences between each pair of elements: (a, b) -> 2*abs(a - b)/(a + b) ( a...
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.
double coordinateToBinf(double coordinate, const IAxis &axis)
Transforms coordinate on axis into the bin-fraction-coordinate.
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) ...
bool checkRelativeDifference(const OutputData< double > &dat, const OutputData< double > &ref, const double threshold)
Returns true is relative difference is below threshold; prints informative output.
std::vector< std::vector< double > > create2DArrayfromOutputData(const OutputData< double > &data)
Creates a vector of vectors of double (2D Array) from OutputData.
double coordinateFromBinf(double value, const IAxis &axis)
Transforms bin-fraction-coordinate into axis coordinate.
std::unique_ptr< OutputData< double > > createOutputDatafrom2DArray(const std::vector< std::vector< double >> &array_2d)
Creates OutputData from a 2D Array.
std::vector< std::vector< double > > FT2DArray(const std::vector< std::vector< double >> &signal)
Creates a Fourier Transform of a 2D Array (vector of vectors).
double GetRelativeDifference(double a, double b)
Returns the safe relative difference, which is 2(|a-b|)/(|a|+|b|) except in special cases.
double m_upper
upper bound of the bin
double m_lower
lower bound of the bin