23 std::vector<std::vector<double>> FT2DArray(
const std::vector<std::vector<double>>& signal)
26 std::vector<std::vector<double>> ret;
39 throw std::runtime_error(
"OutputData dimension differs from reference");
46 throw std::runtime_error(
"diff=NaN!");
55 if (diff > threshold) {
56 std::cerr <<
"FAILED: relative deviation of dat from ref is " << diff
57 <<
", above given threshold " << threshold << std::endl;
61 std::cerr <<
"- OK: relative deviation of dat from ref is " << diff
62 <<
", within given threshold " << threshold << std::endl;
64 std::cout <<
"- OK: dat = ref\n";
68 std::unique_ptr<OutputData<double>>
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)
81 std::unique_ptr<OutputData<double>>
85 throw std::runtime_error(
"DataUtils::rotateDataByN90Deg()"
86 " -> Error! Works only on two-dimensional data");
89 return std::unique_ptr<OutputData<double>>(data.
clone());
95 output->addAxis(n == 2 ? x_axis : y_axis);
96 output->addAxis(n == 2 ? y_axis : x_axis);
99 std::function<void(std::vector<int>&)> index_mapping;
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];
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;
117 for (
size_t index = 0, size = data.
getAllocatedSize(); index < size; ++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];
127 std::unique_ptr<OutputData<double>>
131 if (origin.
rank() != 2)
132 throw std::runtime_error(
"DataUtils::createClippedData()"
133 " -> Error! Works only on two-dimensional data");
136 for (
size_t i_axis = 0; i_axis < origin.
rank(); i_axis++) {
143 result->addAxis(*new_axis);
146 result->setAllTo(0.0);
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;
174 return static_cast<double>(index) + f;
179 int index =
static_cast<int>(value);
185 }
else if (index >=
static_cast<int>(axis.
size())) {
189 Bin1D bin = axis.
bin(
static_cast<size_t>(index));
190 result = bin.
m_lower + (value -
static_cast<double>(index)) * bin.
binSize();
208 std::vector<std::vector<double>>
211 if (data.
rank() != 2)
212 throw std::runtime_error(
"DataUtils::create2DArrayfromOutputData() -> "
213 "Error! Works only on two-dimensional data");
215 std::vector<std::vector<double>> array_2d;
216 std::vector<double> row_vec;
222 for (
size_t row = 0; row < nrows; row++) {
224 for (
size_t col = 0; col < ncols; col++) {
225 row_vec.push_back(data[it]);
228 array_2d.push_back(row_vec);
234 std::unique_ptr<OutputData<double>>
238 size_t nrows = array_2d.size();
239 size_t ncols = array_2d[0].size();
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];
259 auto fft_array_2d = FT2DArray(array_2d);
Defines various functions to interact from numpy on Python side.
Defines namespace DataUtils.
Defines constants and "almost equal" in namespace Numeric.
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 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
iterator end()
Returns read/write iterator that points to the one past last element.
size_t rank() const
Returns number of dimensions.
const IAxis & axis(size_t serial_number) const
returns axis with given serial number
std::vector< int > getAxesBinIndices(size_t global_index) const
Returns vector of axes indices for given global index.
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).
OutputData * clone() const
double getAxisValue(size_t global_index, size_t i_selected_axis) const
Returns the value of selected axis for given global_index.
CreateDataImpl::ReturnType< T > createData(const T &vec)
Creates OutputData array from input vector.
double coordinateToBinf(double coordinate, const IAxis &axis)
Transforms coordinate on axis into the bin-fraction-coordinate.
bool checkRelativeDifference(const OutputData< double > &dat, const OutputData< double > &ref, const double threshold)
Returns true is relative difference is below threshold; prints informative output.
double relativeDataDifference(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 > > createRelativeDifferenceData(const OutputData< double > &data, const OutputData< double > &reference)
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.
std::unique_ptr< OutputData< double > > createFFT(const OutputData< double > &data)
Creates Fourier Transform (OutputData format) of intensity map (OutputData format).
std::vector< std::vector< double > > create2DArrayfromOutputData(const OutputData< double > &data)
Creates a vector of vectors of double (2D Array) from OutputData.
std::unique_ptr< OutputData< double > > createOutputDatafrom2DArray(const std::vector< std::vector< double >> &array_2d)
Creates OutputData from a 2D Array.
double coordinateFromBinf(double value, const IAxis &axis)
Transforms bin-fraction-coordinate into axis coordinate.
OutputData< double > * importArrayToOutputData(const std::vector< double > &vec)
Reads 1D array of doubles to Python, for use in persistence test.
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) ...
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