Skip to content
Snippets Groups Projects
Commit 6c6f6427 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

IntensityDataFunctions methods order reshuffled

parent 26b4c11e
No related branches found
No related tags found
No related merge requests found
...@@ -12,25 +12,25 @@ ...@@ -12,25 +12,25 @@
// //
// ************************************************************************** // // ************************************************************************** //
#include "IntensityDataFunctions.h"
#include "BornAgainNamespace.h" #include "BornAgainNamespace.h"
#include "ConvolutionDetectorResolution.h" #include "ConvolutionDetectorResolution.h"
#include "FourierTransform.h"
#include "IHistogram.h" #include "IHistogram.h"
#include "IntensityDataFunctions.h"
#include "Numeric.h" #include "Numeric.h"
#include "FourierTransform.h"
#include <math.h> #include <math.h>
//! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]). //! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
double IntensityDataFunctions::getRelativeDifference( double IntensityDataFunctions::getRelativeDifference(const OutputData<double>& dat,
const OutputData<double>& dat, const OutputData<double>& ref) const OutputData<double>& ref)
{ {
if(!dat.hasSameDimensions(ref)) if (!dat.hasSameDimensions(ref))
throw Exceptions::RuntimeErrorException( throw Exceptions::RuntimeErrorException(
"IntensityDataFunctions::getRelativeDifference() -> " "IntensityDataFunctions::getRelativeDifference() -> "
"Error. Different dimensions of data and reference."); "Error. Different dimensions of data and reference.");
double diff = 0.0; double diff = 0.0;
for(size_t i=0; i<dat.getAllocatedSize(); ++i) for (size_t i = 0; i < dat.getAllocatedSize(); ++i)
diff += Numeric::get_relative_difference(dat[i], ref[i]); diff += Numeric::get_relative_difference(dat[i], ref[i]);
diff /= dat.getAllocatedSize(); diff /= dat.getAllocatedSize();
...@@ -39,23 +39,22 @@ double IntensityDataFunctions::getRelativeDifference( ...@@ -39,23 +39,22 @@ double IntensityDataFunctions::getRelativeDifference(
return diff; return diff;
} }
double IntensityDataFunctions::getRelativeDifference( double IntensityDataFunctions::getRelativeDifference(const IHistogram& dat, const IHistogram& ref)
const IHistogram& dat, const IHistogram& ref)
{ {
return getRelativeDifference( return getRelativeDifference(*std::unique_ptr<OutputData<double>>(dat.getData().meanValues()),
*std::unique_ptr<OutputData<double>>(dat.getData().meanValues()), *std::unique_ptr<OutputData<double>>(ref.getData().meanValues()));
*std::unique_ptr<OutputData<double>>(ref.getData().meanValues()) );
} }
std::unique_ptr<OutputData<double> > IntensityDataFunctions::createRelativeDifferenceData( std::unique_ptr<OutputData<double>>
const OutputData<double>& data, const OutputData<double>& reference) IntensityDataFunctions::createRelativeDifferenceData(const OutputData<double>& data,
const OutputData<double>& reference)
{ {
if(!data.hasSameDimensions(reference)) if (!data.hasSameDimensions(reference))
throw Exceptions::RuntimeErrorException( throw Exceptions::RuntimeErrorException(
"IntensityDataFunctions::createRelativeDifferenceData() -> " "IntensityDataFunctions::createRelativeDifferenceData() -> "
"Error. Different dimensions of data and reference."); "Error. Different dimensions of data and reference.");
std::unique_ptr<OutputData<double> > result(reference.clone()); std::unique_ptr<OutputData<double>> result(reference.clone());
for(size_t i=0; i<result->getAllocatedSize(); ++i) for (size_t i = 0; i < result->getAllocatedSize(); ++i)
(*result)[i] = Numeric::get_relative_difference(data[i], reference[i]); (*result)[i] = Numeric::get_relative_difference(data[i], reference[i]);
return result; return result;
} }
...@@ -65,7 +64,7 @@ IntensityDataFunctions::createRearrangedDataSet(const OutputData<double>& data, ...@@ -65,7 +64,7 @@ IntensityDataFunctions::createRearrangedDataSet(const OutputData<double>& data,
{ {
if (data.getRank() != 2) if (data.getRank() != 2)
throw Exceptions::LogicErrorException("IntensityDataFunctions::rotateDataByN90Deg()" throw Exceptions::LogicErrorException("IntensityDataFunctions::rotateDataByN90Deg()"
" -> Error! Works only on two-dimensional data"); " -> Error! Works only on two-dimensional data");
n = (4 + n % 4) % 4; n = (4 + n % 4) % 4;
if (n == 0) if (n == 0)
return std::unique_ptr<OutputData<double>>(data.clone()); return std::unique_ptr<OutputData<double>>(data.clone());
...@@ -74,8 +73,8 @@ IntensityDataFunctions::createRearrangedDataSet(const OutputData<double>& data, ...@@ -74,8 +73,8 @@ IntensityDataFunctions::createRearrangedDataSet(const OutputData<double>& data,
// swapping axes if necessary // swapping axes if necessary
const IAxis& x_axis = data.getAxis(0); const IAxis& x_axis = data.getAxis(0);
const IAxis& y_axis = data.getAxis(1); const IAxis& y_axis = data.getAxis(1);
output->addAxis( n == 2 ? x_axis : y_axis); output->addAxis(n == 2 ? x_axis : y_axis);
output->addAxis( n == 2 ? y_axis : x_axis); output->addAxis(n == 2 ? y_axis : x_axis);
// creating index mapping // creating index mapping
std::function<void(std::vector<int>&)> index_mapping; std::function<void(std::vector<int>&)> index_mapping;
...@@ -106,19 +105,19 @@ IntensityDataFunctions::createRearrangedDataSet(const OutputData<double>& data, ...@@ -106,19 +105,19 @@ IntensityDataFunctions::createRearrangedDataSet(const OutputData<double>& data,
return output; return output;
} }
std::unique_ptr<OutputData<double>> IntensityDataFunctions::createClippedDataSet( std::unique_ptr<OutputData<double>>
const OutputData<double>& origin, double x1, double y1, double x2, double y2) IntensityDataFunctions::createClippedDataSet(const OutputData<double>& origin, double x1, double y1,
double x2, double y2)
{ {
if (origin.getRank() != 2) if (origin.getRank() != 2)
throw Exceptions::LogicErrorException( throw Exceptions::LogicErrorException("IntensityDataFunctions::createClippedData()"
"IntensityDataFunctions::createClippedData()" " -> Error! Works only on two-dimensional data");
" -> Error! Works only on two-dimensional data");
std::unique_ptr<OutputData<double> > result(new OutputData<double>); std::unique_ptr<OutputData<double>> result(new OutputData<double>);
for(size_t i_axis=0; i_axis<origin.getRank(); i_axis++) { for (size_t i_axis = 0; i_axis < origin.getRank(); i_axis++) {
const IAxis& axis = origin.getAxis(i_axis); const IAxis& axis = origin.getAxis(i_axis);
IAxis* new_axis; IAxis* new_axis;
if(i_axis == 0) if (i_axis == 0)
new_axis = axis.createClippedAxis(x1, x2); new_axis = axis.createClippedAxis(x1, x2);
else else
new_axis = axis.createClippedAxis(y1, y2); new_axis = axis.createClippedAxis(y1, y2);
...@@ -132,7 +131,7 @@ std::unique_ptr<OutputData<double>> IntensityDataFunctions::createClippedDataSet ...@@ -132,7 +131,7 @@ std::unique_ptr<OutputData<double>> IntensityDataFunctions::createClippedDataSet
while (it_origin != origin.end()) { while (it_origin != origin.end()) {
double x = origin.getAxisValue(it_origin.getIndex(), 0); double x = origin.getAxisValue(it_origin.getIndex(), 0);
double y = origin.getAxisValue(it_origin.getIndex(), 1); double y = origin.getAxisValue(it_origin.getIndex(), 1);
if(result->getAxis(0).contains(x) && result->getAxis(1).contains(y)) { if (result->getAxis(0).contains(x) && result->getAxis(1).contains(y)) {
*it_result = *it_origin; *it_result = *it_origin;
++it_result; ++it_result;
} }
...@@ -152,7 +151,7 @@ double IntensityDataFunctions::coordinateToBinf(double coordinate, const IAxis& ...@@ -152,7 +151,7 @@ double IntensityDataFunctions::coordinateToBinf(double coordinate, const IAxis&
{ {
size_t index = axis.findClosestIndex(coordinate); size_t index = axis.findClosestIndex(coordinate);
Bin1D bin = axis.getBin(index); Bin1D bin = axis.getBin(index);
double f = (coordinate - bin.m_lower)/bin.getBinSize(); double f = (coordinate - bin.m_lower) / bin.getBinSize();
return static_cast<double>(index) + f; return static_cast<double>(index) + f;
} }
...@@ -161,15 +160,15 @@ double IntensityDataFunctions::coordinateFromBinf(double value, const IAxis& axi ...@@ -161,15 +160,15 @@ double IntensityDataFunctions::coordinateFromBinf(double value, const IAxis& axi
int index = static_cast<int>(value); int index = static_cast<int>(value);
double result(0); double result(0);
if(index < 0) { if (index < 0) {
Bin1D bin = axis.getBin(0); Bin1D bin = axis.getBin(0);
result = bin.m_lower + value*bin.getBinSize(); result = bin.m_lower + value * bin.getBinSize();
} else if(index >= static_cast<int>(axis.size())) { } else if (index >= static_cast<int>(axis.size())) {
Bin1D bin = axis.getBin(axis.size()-1); Bin1D bin = axis.getBin(axis.size() - 1);
result = bin.m_upper + (value-axis.size())*bin.getBinSize(); result = bin.m_upper + (value - axis.size()) * bin.getBinSize();
} else { } else {
Bin1D bin = axis.getBin(static_cast<size_t>(index)); Bin1D bin = axis.getBin(static_cast<size_t>(index));
result = bin.m_lower + (value - static_cast<double>(index))*bin.getBinSize(); result = bin.m_lower + (value - static_cast<double>(index)) * bin.getBinSize();
} }
return result; return result;
...@@ -181,20 +180,20 @@ void IntensityDataFunctions::coordinateToBinf(double& x, double& y, const Output ...@@ -181,20 +180,20 @@ void IntensityDataFunctions::coordinateToBinf(double& x, double& y, const Output
y = coordinateToBinf(y, data.getAxis(BornAgain::Y_AXIS_INDEX)); y = coordinateToBinf(y, data.getAxis(BornAgain::Y_AXIS_INDEX));
} }
void IntensityDataFunctions::coordinateFromBinf( void IntensityDataFunctions::coordinateFromBinf(double& x, double& y,
double& x, double& y, const OutputData<double>& data) const OutputData<double>& data)
{ {
x = coordinateFromBinf(x, data.getAxis(BornAgain::X_AXIS_INDEX)); x = coordinateFromBinf(x, data.getAxis(BornAgain::X_AXIS_INDEX));
y = coordinateFromBinf(y, data.getAxis(BornAgain::Y_AXIS_INDEX)); y = coordinateFromBinf(y, data.getAxis(BornAgain::Y_AXIS_INDEX));
} }
std::vector<std::vector<double>> IntensityDataFunctions::create2DArrayfromOutputData( std::vector<std::vector<double>>
const OutputData<double> &data) IntensityDataFunctions::create2DArrayfromOutputData(const OutputData<double>& data)
{ {
if (data.getRank() != 2) if (data.getRank() != 2)
throw Exceptions::LogicErrorException( throw Exceptions::LogicErrorException(
"IntensityDataFunctions::create2DArrayfromOutputData() -> " "IntensityDataFunctions::create2DArrayfromOutputData() -> "
"Error! Works only on two-dimensional data"); "Error! Works only on two-dimensional data");
std::vector<std::vector<double>> array_2d; std::vector<std::vector<double>> array_2d;
std::vector<double> row_vec; // row vector for constructing each row of 2D array std::vector<double> row_vec; // row vector for constructing each row of 2D array
...@@ -203,11 +202,9 @@ std::vector<std::vector<double>> IntensityDataFunctions::create2DArrayfromOutput ...@@ -203,11 +202,9 @@ std::vector<std::vector<double>> IntensityDataFunctions::create2DArrayfromOutput
size_t ncols = data.getAxis(1).size(); size_t ncols = data.getAxis(1).size();
size_t it = 0; // iterator of 'data' size_t it = 0; // iterator of 'data'
for(size_t row=0; row<nrows; row++) for (size_t row = 0; row < nrows; row++) {
{
row_vec.clear(); row_vec.clear();
for(size_t col=0; col<ncols; col++) for (size_t col = 0; col < ncols; col++) {
{
row_vec.push_back(data[it]); row_vec.push_back(data[it]);
it++; it++;
} }
...@@ -217,8 +214,8 @@ std::vector<std::vector<double>> IntensityDataFunctions::create2DArrayfromOutput ...@@ -217,8 +214,8 @@ std::vector<std::vector<double>> IntensityDataFunctions::create2DArrayfromOutput
return array_2d; return array_2d;
} }
std::vector<std::vector<double>> IntensityDataFunctions::FT2DArray( std::vector<std::vector<double>>
const std::vector<std::vector<double>> &signal) IntensityDataFunctions::FT2DArray(const std::vector<std::vector<double>>& signal)
{ {
FourierTransform ft; FourierTransform ft;
std::vector<std::vector<double>> fft_array; std::vector<std::vector<double>> fft_array;
...@@ -229,8 +226,8 @@ std::vector<std::vector<double>> IntensityDataFunctions::FT2DArray( ...@@ -229,8 +226,8 @@ std::vector<std::vector<double>> IntensityDataFunctions::FT2DArray(
return fft_array; return fft_array;
} }
std::unique_ptr<OutputData<double> > IntensityDataFunctions::createOutputDatafrom2DArray( std::unique_ptr<OutputData<double>> IntensityDataFunctions::createOutputDatafrom2DArray(
const std::vector<std::vector<double>> &array_2d) const std::vector<std::vector<double>>& array_2d)
{ {
std::unique_ptr<OutputData<double>> result(new OutputData<double>); std::unique_ptr<OutputData<double>> result(new OutputData<double>);
size_t nrows = array_2d.size(); size_t nrows = array_2d.size();
...@@ -239,8 +236,8 @@ std::unique_ptr<OutputData<double> > IntensityDataFunctions::createOutputDatafro ...@@ -239,8 +236,8 @@ std::unique_ptr<OutputData<double> > IntensityDataFunctions::createOutputDatafro
result->addAxis("x", nrows, 0.0, double(nrows)); result->addAxis("x", nrows, 0.0, double(nrows));
result->addAxis("y", ncols, 0.0, double(ncols)); result->addAxis("y", ncols, 0.0, double(ncols));
std::vector<unsigned> axes_indices(2); std::vector<unsigned> axes_indices(2);
for(unsigned row=0; row<nrows; row++) { for (unsigned row = 0; row < nrows; row++) {
for(unsigned col=0; col<ncols; col++) { for (unsigned col = 0; col < ncols; col++) {
axes_indices[0] = row; axes_indices[0] = row;
axes_indices[1] = col; axes_indices[1] = col;
size_t global_index = result->toGlobalIndex(axes_indices); size_t global_index = result->toGlobalIndex(axes_indices);
...@@ -251,7 +248,8 @@ std::unique_ptr<OutputData<double> > IntensityDataFunctions::createOutputDatafro ...@@ -251,7 +248,8 @@ std::unique_ptr<OutputData<double> > IntensityDataFunctions::createOutputDatafro
return result; return result;
} }
std::unique_ptr<OutputData<double>> IntensityDataFunctions::createFFT(const OutputData<double> &data) std::unique_ptr<OutputData<double>>
IntensityDataFunctions::createFFT(const OutputData<double>& data)
{ {
auto array_2d = IntensityDataFunctions::create2DArrayfromOutputData(data); auto array_2d = IntensityDataFunctions::create2DArrayfromOutputData(data);
auto fft_array_2d = IntensityDataFunctions::FT2DArray(array_2d); auto fft_array_2d = IntensityDataFunctions::FT2DArray(array_2d);
......
...@@ -15,41 +15,23 @@ ...@@ -15,41 +15,23 @@
#ifndef INTENSITYDATAFUNCTIONS_H #ifndef INTENSITYDATAFUNCTIONS_H
#define INTENSITYDATAFUNCTIONS_H #define INTENSITYDATAFUNCTIONS_H
#include <memory>
#include "OutputData.h" #include "OutputData.h"
#include <memory>
class IHistogram; class IHistogram;
class IResolutionFunction2D;
//! Functions to work with intensity data. //! Functions to work with intensity data.
//! @ingroup tools //! @ingroup tools
namespace IntensityDataFunctions { namespace IntensityDataFunctions
{
//! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]). //! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
BA_CORE_API_ double getRelativeDifference( BA_CORE_API_ double getRelativeDifference(const OutputData<double>& dat,
const OutputData<double>& dat, const OutputData<double>& ref); const OutputData<double>& ref);
BA_CORE_API_ double getRelativeDifference(const IHistogram& dat, const IHistogram& ref); BA_CORE_API_ double getRelativeDifference(const IHistogram& dat, const IHistogram& ref);
#ifndef SWIG
BA_CORE_API_ std::unique_ptr<OutputData<double>> createRelativeDifferenceData(
const OutputData<double>& data, const OutputData<double>& reference);
//! Returns a pointer to 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
BA_CORE_API_ std::unique_ptr<OutputData<double>>
createRearrangedDataSet(const OutputData<double>& data, int n);
//! Returns new IntensityData objects which axes clipped to represent the specified rectangle.
BA_CORE_API_ std::unique_ptr<OutputData<double>> createClippedDataSet(
const OutputData<double>& origin, double x1, double y1, double x2, double y2);
#endif // SWIG
//! Transforms coordinate on axis into the bin-fraction-coordinate. //! Transforms coordinate on axis into the bin-fraction-coordinate.
BA_CORE_API_ double coordinateToBinf(double coordinate, const IAxis& axis); BA_CORE_API_ double coordinateToBinf(double coordinate, const IAxis& axis);
...@@ -63,23 +45,37 @@ BA_CORE_API_ void coordinateToBinf(double& x, double& y, const OutputData<double ...@@ -63,23 +45,37 @@ BA_CORE_API_ void coordinateToBinf(double& x, double& y, const OutputData<double
BA_CORE_API_ void coordinateFromBinf(double& x, double& y, const OutputData<double>& data); BA_CORE_API_ void coordinateFromBinf(double& x, double& y, const OutputData<double>& data);
//! Creates a vector of vectors of double (2D Array) from OutputData. //! Creates a vector of vectors of double (2D Array) from OutputData.
BA_CORE_API_ std::vector<std::vector<double>> create2DArrayfromOutputData( BA_CORE_API_ std::vector<std::vector<double>>
const OutputData<double>& data); create2DArrayfromOutputData(const OutputData<double>& data);
//! Creates a Fourier Transform of a 2D Array (vector of vectors). //! Creates a Fourier Transform of a 2D Array (vector of vectors).
BA_CORE_API_ std::vector<std::vector<double>> FT2DArray( BA_CORE_API_ std::vector<std::vector<double>>
const std::vector<std::vector<double>>& signal); FT2DArray(const std::vector<std::vector<double>>& signal);
#ifndef SWIG #ifndef SWIG
BA_CORE_API_ std::unique_ptr<OutputData<double>>
createRelativeDifferenceData(const OutputData<double>& data, const OutputData<double>& reference);
//! 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
BA_CORE_API_ std::unique_ptr<OutputData<double>>
createRearrangedDataSet(const OutputData<double>& data, int n);
//! Returns new IntensityData objects which axes clipped to represent the specified rectangle.
BA_CORE_API_ std::unique_ptr<OutputData<double>>
createClippedDataSet(const OutputData<double>& origin, double x1, double y1, double x2, double y2);
//! Creates OutputData from a 2D Array. //! Creates OutputData from a 2D Array.
BA_CORE_API_ std::unique_ptr<OutputData<double>> createOutputDatafrom2DArray( BA_CORE_API_ std::unique_ptr<OutputData<double>>
const std::vector<std::vector<double>>& array_2d); createOutputDatafrom2DArray(const std::vector<std::vector<double>>& array_2d);
//! Creates Fourier Transform (OutputData format) of intensity map (OutputData format). //! Creates Fourier Transform (OutputData format) of intensity map (OutputData format).
BA_CORE_API_ std::unique_ptr<OutputData<double>> createFFT(const OutputData<double> &data); BA_CORE_API_ std::unique_ptr<OutputData<double>> createFFT(const OutputData<double>& data);
#endif // SWIG #endif // SWIG
}; // namespace IntensityDataFunctions }; // namespace IntensityDataFunctions
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment