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

Collection of utils for 1D and 2D image processing (background, peaks, ets). More...

Functions

bool agreesWithReference (const SimulationResult &dat, const std::string &refFileName, double tol)
 Returns true if SimulatioResult agrees with data from reference file. More...
 
std::vector< std::pair< double, double > > FindPeaks (const Histogram2D &hist, double sigma=2, const std::string &option={}, double threshold=0.05)
 Returns vector of peak center coordinates, for peaks in given histogram. More...
 
double getRelativeDifference (const IHistogram &dat, const IHistogram &ref)
 
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) ( and zero if a-b=0 ) More...
 

Detailed Description

Collection of utils for 1D and 2D image processing (background, peaks, ets).

Function Documentation

◆ agreesWithReference()

bool HistoUtils::agreesWithReference ( const SimulationResult dat,
const std::string &  refFileName,
double  tol 
)

Returns true if SimulatioResult agrees with data from reference file.

Definition at line 80 of file HistoUtils.cpp.

82 {
83  std::unique_ptr<OutputData<double>> refDat{IntensityDataIOFactory::readOutputData(refFileName)};
84  if (!refDat) {
85  std::cerr << "Could not read reference data from file " << refFileName << std::endl;
86  return false;
87  }
88  std::unique_ptr<OutputData<double>> datDat(dat.data());
89  return DataUtils::checkRelativeDifference(*datDat, *refDat, tol);
90 }
static OutputData< double > * readOutputData(const std::string &file_name)
Reads file and returns newly created OutputData object.
std::unique_ptr< OutputData< double > > data(Axes::Units units=Axes::Units::DEFAULT) const
bool checkRelativeDifference(const OutputData< double > &dat, const OutputData< double > &ref, const double threshold)
Returns true is relative difference is below threshold; prints informative output.
Definition: DataUtils.cpp:51

References DataUtils::checkRelativeDifference(), SimulationResult::data(), and IntensityDataIOFactory::readOutputData().

Here is the call graph for this function:

◆ FindPeaks()

std::vector< std::pair< double, double > > HistoUtils::FindPeaks ( const Histogram2D hist,
double  sigma = 2,
const std::string &  option = {},
double  threshold = 0.05 
)

Returns vector of peak center coordinates, for peaks in given histogram.

Definition at line 25 of file HistoUtils.cpp.

28 {
29  std::unique_ptr<OutputData<double>> data(hist.createOutputData());
30  std::vector<std::vector<double>> arr = ArrayUtils::createVector2D(*data);
31  tspectrum::Spectrum2D spec;
32  auto peaks = spec.find_peaks(arr, sigma, option, threshold);
33 
34  // coordinates of peaks in histogram axes units
35  std::vector<std::pair<double, double>> result;
36 
37  for (const auto& p : peaks) {
38  double row_value = p.first;
39  double col_value = p.second;
40 
41  size_t xaxis_index = static_cast<size_t>(col_value);
42  size_t yaxis_index = hist.yAxis().size() - 1 - static_cast<size_t>(row_value);
43 
44  Bin1D xbin = hist.xAxis().bin(xaxis_index);
45  Bin1D ybin = hist.yAxis().bin(yaxis_index);
46 
47  double dx = col_value - static_cast<size_t>(col_value);
48  double dy = -1.0 * (row_value - static_cast<size_t>(row_value));
49 
50  double x = xbin.center() + xbin.binSize() * dx;
51  double y = ybin.center() + ybin.binSize() * dy;
52 
53  result.push_back(std::make_pair(x, y));
54  }
55  return result;
56 }
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
const IAxis & yAxis() const
returns y-axis for 2D histograms
Definition: IHistogram.cpp:50
const IAxis & xAxis() const
returns x-axis
Definition: IHistogram.cpp:44
OutputData< double > * createOutputData(DataType dataType=DataType::INTEGRAL) const
creates new OutputData with histogram's shape and values corresponding to DataType
Definition: IHistogram.cpp:343
decltype(auto) createVector2D(const T &data)
Creates 2D vector from OutputData.
Definition: Bin.h:20
double center() const
Definition: Bin.h:25
double binSize() const
Definition: Bin.h:26

References IAxis::bin(), Bin1D::binSize(), Bin1D::center(), IHistogram::createOutputData(), ArrayUtils::createVector2D(), IAxis::size(), IHistogram::xAxis(), and IHistogram::yAxis().

Here is the call graph for this function:

◆ getRelativeDifference()

double HistoUtils::getRelativeDifference ( const IHistogram dat,
const IHistogram ref 
)

Definition at line 73 of file HistoUtils.cpp.

74 {
76  *std::unique_ptr<OutputData<double>>(dat.getData().meanValues()),
77  *std::unique_ptr<OutputData<double>>(ref.getData().meanValues()));
78 }
const OutputData< CumulativeValue > & getData() const
Definition: IHistogram.cpp:126
OutputData< double > * meanValues() const
Definition: OutputData.h:285
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 IHistogram::getData(), OutputData< T >::meanValues(), and DataUtils::relativeDataDifference().

Here is the call graph for this function:

◆ RelativeDifference()

double HistoUtils::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) ( and zero if a-b=0 )

Returns sum of relative differences between each pair of elements: (a, b) -> 2*abs(a - b)/(|a| + |b|) ( and zero if a=b=0 within epsilon )

Definition at line 60 of file HistoUtils.cpp.

61 {
62  if (dat.size() != ref.size())
63  throw std::runtime_error("Error in HistoUtils::RelativeDifference: "
64  "different number of elements");
65  if (dat.empty())
66  return 0.0;
67  double sum_of_diff = 0.0;
68  for (size_t i = 0; i < dat.size(); ++i)
69  sum_of_diff += Numeric::GetRelativeDifference(dat[i], ref[i]);
70  return sum_of_diff / dat.size();
71 }
bool empty() const
size_t size() const
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 SimulationResult::empty(), Numeric::GetRelativeDifference(), and SimulationResult::size().

Here is the call graph for this function: