BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
VariableBinAxis Class Reference

Description

Axis with variable bin size.

Definition at line 23 of file VariableBinAxis.h.

Inheritance diagram for VariableBinAxis:
[legend]
Collaboration diagram for VariableBinAxis:
[legend]

Public Member Functions

 VariableBinAxis (const std::string &name, const std::vector< double > &bin_boundaries)
 VariableBinAxis constructor. More...
 
 ~VariableBinAxis () override=default
 
std::string axisName () const
 Returns the label of the axis. More...
 
Bin1D bin (size_t index) const override
 retrieve a 1d bin for the given index More...
 
std::vector< double > binBoundaries () const override
 
double binCenter (size_t index) const override
 
std::vector< double > binCenters () const override
 
std::pair< double, double > bounds () const
 Returns lower and upper bound in a pair. first is lower, second is upper. More...
 
double center () const
 Returns midpoint of axis. More...
 
void clip (double lower, double upper) override
 Clips this axis to the given values. More...
 
void clip (std::pair< double, double > bounds)
 Convenience overload to clip this axis to the given values. bounds.first is lower, bounds.second is upper value. More...
 
VariableBinAxisclone () const override
 
virtual bool contains (double value) const
 Returns true if axis contains given point. More...
 
size_t findClosestIndex (double value) const override
 find bin index which is best match for given value More...
 
double max () const override
 Returns value of last point of axis. More...
 
double min () const override
 Returns value of first point of axis. More...
 
bool operator!= (const IAxis &right) const
 
bool operator== (const IAxis &right) const
 test for equality More...
 
double operator[] (size_t index) const override
 indexed accessor retrieves a sample More...
 
void setAxisName (std::string name)
 Sets the axis label. More...
 
size_t size () const override
 Returns the number of bins. More...
 
double span () const
 Returns distance from first to last point. More...
 

Protected Member Functions

 VariableBinAxis (const std::string &name, size_t nbins=0)
 
bool equals (const IAxis &other) const override
 
void print (std::ostream &ostr) const override
 
void setBinBoundaries (const std::vector< double > &bin_boundaries)
 

Protected Attributes

size_t m_nbins
 

Private Attributes

std::vector< double > m_bin_boundaries
 vector containing the bin limits More...
 
std::string m_name
 axis name More...
 

Constructor & Destructor Documentation

◆ VariableBinAxis() [1/2]

VariableBinAxis::VariableBinAxis ( const std::string &  name,
const std::vector< double > &  bin_boundaries 
)

VariableBinAxis constructor.

Parameters
nameAxis name
bin_boundariesArray of size nbins+1 containing low-edges for each bin and upper edge of last bin.

Definition at line 21 of file VariableBinAxis.cpp.

22  : IAxis(name)
23  , m_nbins(bin_boundaries.size() - 1)
24 {
25  setBinBoundaries(bin_boundaries);
26 }
IAxis(std::string name)
Definition: IAxis.h:29
void setBinBoundaries(const std::vector< double > &bin_boundaries)

References setBinBoundaries().

Referenced by clone().

Here is the call graph for this function:

◆ ~VariableBinAxis()

VariableBinAxis::~VariableBinAxis ( )
overridedefault

◆ VariableBinAxis() [2/2]

VariableBinAxis::VariableBinAxis ( const std::string &  name,
size_t  nbins = 0 
)
explicitprotected

Definition at line 28 of file VariableBinAxis.cpp.

29  : IAxis(name)
30  , m_nbins(nbins)
31 {
32 }

Member Function Documentation

◆ axisName()

std::string IAxis::axisName ( ) const
inlineinherited

Returns the label of the axis.

Definition at line 61 of file IAxis.h.

61 { return m_name; }
std::string m_name
axis name
Definition: IAxis.h:103

References IAxis::m_name.

Referenced by ConstKBinAxis::clone(), CustomBinAxis::clone(), FixedBinAxis::clone(), PointwiseAxis::clone(), clone(), IAxis::equals(), ConstKBinAxis::print(), CustomBinAxis::print(), FixedBinAxis::print(), PointwiseAxis::print(), and print().

◆ bin()

Bin1D VariableBinAxis::bin ( size_t  index) const
overridevirtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 45 of file VariableBinAxis.cpp.

46 {
47  if (index >= m_nbins)
48  throw std::runtime_error("VariableBinAxis::bin() -> Error. Wrong index.");
49 
50  Bin1D result(m_bin_boundaries[index], m_bin_boundaries[index + 1]);
51  return result;
52 }
Definition: Bin.h:20
std::vector< double > m_bin_boundaries
vector containing the bin limits

References m_bin_boundaries, and m_nbins.

Referenced by binCenter(), binCenters(), ConstKBinAxis::clip(), clip(), and operator[]().

◆ binBoundaries()

std::vector<double> VariableBinAxis::binBoundaries ( ) const
inlineoverridevirtual

Reimplemented from IAxis.

Definition at line 48 of file VariableBinAxis.h.

48 { return m_bin_boundaries; }

References m_bin_boundaries.

Referenced by ConstKBinAxis::clip().

◆ binCenter()

double VariableBinAxis::binCenter ( size_t  index) const
overridevirtual

Implements IAxis.

Definition at line 64 of file VariableBinAxis.cpp.

65 {
66  return bin(index).center();
67 }
double center() const
Definition: Bin.h:30
Bin1D bin(size_t index) const override
retrieve a 1d bin for the given index

References bin(), and Bin1D::center().

Here is the call graph for this function:

◆ binCenters()

std::vector< double > VariableBinAxis::binCenters ( ) const
overridevirtual

Reimplemented from IAxis.

Definition at line 86 of file VariableBinAxis.cpp.

87 {
88  std::vector<double> result;
89  result.resize(size(), 0.0);
90  for (size_t i = 0; i < size(); ++i)
91  result[i] = bin(i).center();
92  return result;
93 }
size_t size() const override
Returns the number of bins.

References bin(), Bin1D::center(), and size().

Here is the call graph for this function:

◆ bounds()

std::pair< double, double > IAxis::bounds ( ) const
inherited

Returns lower and upper bound in a pair. first is lower, second is upper.

Definition at line 50 of file IAxis.cpp.

51 {
52  return {min(), max()};
53 }
virtual double max() const =0
Returns value of last point of axis.
virtual double min() const =0
Returns value of first point of axis.

References IAxis::max(), and IAxis::min().

Referenced by IAxis::clip().

Here is the call graph for this function:

◆ center()

double IAxis::center ( ) const
inherited

Returns midpoint of axis.

Definition at line 60 of file IAxis.cpp.

61 {
62  return (max() + min()) / 2;
63 }

References IAxis::max(), and IAxis::min().

Here is the call graph for this function:

◆ clip() [1/2]

void VariableBinAxis::clip ( double  lower,
double  upper 
)
overridevirtual

Clips this axis to the given values.

Reimplemented from IAxis.

Definition at line 95 of file VariableBinAxis.cpp.

96 {
97  if (lower >= upper)
98  throw std::runtime_error("VariableBinAxis::clip() -> Error. "
99  "'lower' should be smaller than 'upper'");
100 
101  if (lower < min())
102  lower = bin(0).center();
103  if (upper >= max())
104  upper = bin(size() - 1).center();
105 
106  const size_t nbin1 = findClosestIndex(lower);
107  const size_t nbin2 = findClosestIndex(upper);
108  const size_t new_nbins = nbin2 - nbin1 + 1;
109 
110  std::vector<double> new_boundaries;
111  for (size_t i = 0; i < new_nbins + 1; ++i)
112  new_boundaries.push_back(m_bin_boundaries[nbin1 + i]);
113 
114  m_nbins = new_nbins;
115  setBinBoundaries(new_boundaries);
116 }
double max() const override
Returns value of last point of axis.
size_t findClosestIndex(double value) const override
find bin index which is best match for given value
double min() const override
Returns value of first point of axis.

References bin(), Bin1D::center(), findClosestIndex(), m_bin_boundaries, m_nbins, max(), min(), setBinBoundaries(), and size().

Here is the call graph for this function:

◆ clip() [2/2]

void IAxis::clip ( std::pair< double, double >  bounds)
inherited

Convenience overload to clip this axis to the given values. bounds.first is lower, bounds.second is upper value.

Definition at line 40 of file IAxis.cpp.

41 {
42  return clip(bounds.first, bounds.second);
43 }
std::pair< double, double > bounds() const
Returns lower and upper bound in a pair. first is lower, second is upper.
Definition: IAxis.cpp:50
virtual void clip(double lower, double upper)
Clips this axis to the given values.
Definition: IAxis.cpp:35

References IAxis::bounds(), and IAxis::clip().

Here is the call graph for this function:

◆ clone()

VariableBinAxis * VariableBinAxis::clone ( ) const
overridevirtual

Implements IAxis.

Definition at line 34 of file VariableBinAxis.cpp.

35 {
36  auto* result = new VariableBinAxis(axisName(), m_bin_boundaries);
37  return result;
38 }
std::string axisName() const
Returns the label of the axis.
Definition: IAxis.h:61
VariableBinAxis(const std::string &name, const std::vector< double > &bin_boundaries)
VariableBinAxis constructor.

References VariableBinAxis(), IAxis::axisName(), and m_bin_boundaries.

Here is the call graph for this function:

◆ contains()

bool IAxis::contains ( double  value) const
virtualinherited

Returns true if axis contains given point.

Definition at line 45 of file IAxis.cpp.

46 {
47  return value >= min() && value < max();
48 }

References IAxis::max(), and IAxis::min().

Referenced by RectangularDetector::indexOfSpecular(), and SphericalDetector::indexOfSpecular().

Here is the call graph for this function:

◆ equals()

bool VariableBinAxis::equals ( const IAxis other) const
overrideprotectedvirtual

Reimplemented from IAxis.

Definition at line 129 of file VariableBinAxis.cpp.

130 {
131  if (!IAxis::equals(other))
132  return false;
133  if (const auto* p_other_cast = dynamic_cast<const VariableBinAxis*>(&other)) {
134  if (size() != p_other_cast->size())
135  return false;
136  for (size_t i = 0; i < m_bin_boundaries.size(); ++i) {
138  p_other_cast->m_bin_boundaries[i]))
139  return false;
140  }
141  return true;
142  }
143  return false;
144 }
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:20
Axis with variable bin size.
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33

References BaseUtils::algo::almostEqual(), IAxis::equals(), m_bin_boundaries, and size().

Here is the call graph for this function:

◆ findClosestIndex()

size_t VariableBinAxis::findClosestIndex ( double  value) const
overridevirtual

find bin index which is best match for given value

Implements IAxis.

Definition at line 69 of file VariableBinAxis.cpp.

70 {
71  if (m_bin_boundaries.size() < 2)
72  throw std::runtime_error("VariableBinAxis::findClosestIndex() -> Error! "
73  "VariableBinAxis not correctly initialized");
74  if (value < min())
75  return 0;
76  if (value >= max())
77  return m_nbins - 1;
78 
79  auto top_limit = std::lower_bound(m_bin_boundaries.begin(), m_bin_boundaries.end(), value);
80  if (*top_limit != value)
81  --top_limit;
82  size_t nbin = top_limit - m_bin_boundaries.begin();
83  return nbin;
84 }

References m_bin_boundaries, m_nbins, max(), and min().

Referenced by ConstKBinAxis::clip(), and clip().

Here is the call graph for this function:

◆ max()

double VariableBinAxis::max ( ) const
overridevirtual

Returns value of last point of axis.

Implements IAxis.

Definition at line 59 of file VariableBinAxis.cpp.

60 {
61  return m_bin_boundaries.back();
62 }

References m_bin_boundaries.

Referenced by ConstKBinAxis::clip(), clip(), and findClosestIndex().

◆ min()

double VariableBinAxis::min ( ) const
overridevirtual

Returns value of first point of axis.

Implements IAxis.

Definition at line 54 of file VariableBinAxis.cpp.

55 {
56  return m_bin_boundaries.front();
57 }

References m_bin_boundaries.

Referenced by ConstKBinAxis::clip(), clip(), and findClosestIndex().

◆ operator!=()

bool IAxis::operator!= ( const IAxis right) const
inlineinherited

Definition at line 90 of file IAxis.h.

90 { return !(*this == right); }

◆ operator==()

bool IAxis::operator== ( const IAxis right) const
inlineinherited

test for equality

Definition at line 89 of file IAxis.h.

89 { return equals(right); }

References IAxis::equals().

Here is the call graph for this function:

◆ operator[]()

double VariableBinAxis::operator[] ( size_t  index) const
overridevirtual

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 40 of file VariableBinAxis.cpp.

41 {
42  return bin(index).center();
43 }

References bin(), and Bin1D::center().

Here is the call graph for this function:

◆ print()

void VariableBinAxis::print ( std::ostream &  ostr) const
overrideprotectedvirtual

Implements IAxis.

Definition at line 118 of file VariableBinAxis.cpp.

119 {
120  ostr << "VariableBinAxis(\"" << axisName() << "\", " << size() << ", [";
121  for (size_t i = 0; i < m_bin_boundaries.size(); ++i) {
122  ostr << std::setprecision(std::numeric_limits<double>::digits10 + 2) << m_bin_boundaries[i];
123  if (i != m_bin_boundaries.size() - 1)
124  ostr << ", ";
125  }
126  ostr << "])";
127 }

References IAxis::axisName(), m_bin_boundaries, and size().

Here is the call graph for this function:

◆ setAxisName()

void IAxis::setAxisName ( std::string  name)
inlineinherited

Sets the axis label.

Definition at line 39 of file IAxis.h.

39 { m_name = name; }

References IAxis::m_name.

◆ setBinBoundaries()

void VariableBinAxis::setBinBoundaries ( const std::vector< double > &  bin_boundaries)
protected

Definition at line 146 of file VariableBinAxis.cpp.

147 {
148  // checking that values are sorted
149  std::vector<double> vec_sorted = bin_boundaries;
150  std::sort(vec_sorted.begin(), vec_sorted.end());
151  for (size_t i = 0; i < bin_boundaries.size(); ++i) {
152  if (vec_sorted[i] != bin_boundaries[i])
153  throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
154  "Array with bin edges is not sorted.");
155  }
156 
157  std::vector<double> vec = bin_boundaries;
158  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
159 
160  if (vec.size() != bin_boundaries.size())
161  throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
162  "Array with bin edges contains repeating values.");
163 
164  m_bin_boundaries = bin_boundaries;
165 }

References m_bin_boundaries.

Referenced by ConstKBinAxis::ConstKBinAxis(), CustomBinAxis::CustomBinAxis(), VariableBinAxis(), ConstKBinAxis::clip(), and clip().

◆ size()

size_t VariableBinAxis::size ( ) const
inlineoverridevirtual

Returns the number of bins.

Implements IAxis.

Definition at line 34 of file VariableBinAxis.h.

34 { return m_nbins; }

References m_nbins.

Referenced by binCenters(), ConstKBinAxis::clip(), clip(), ConstKBinAxis::equals(), CustomBinAxis::equals(), equals(), ConstKBinAxis::print(), CustomBinAxis::print(), and print().

◆ span()

double IAxis::span ( ) const
inherited

Returns distance from first to last point.

Definition at line 55 of file IAxis.cpp.

56 {
57  return max() - min();
58 }

References IAxis::max(), and IAxis::min().

Referenced by RectangularDetector::height(), and RectangularDetector::width().

Here is the call graph for this function:

Member Data Documentation

◆ m_bin_boundaries

std::vector<double> VariableBinAxis::m_bin_boundaries
private

vector containing the bin limits

Definition at line 61 of file VariableBinAxis.h.

Referenced by bin(), binBoundaries(), clip(), clone(), equals(), findClosestIndex(), max(), min(), print(), and setBinBoundaries().

◆ m_name

std::string IAxis::m_name
privateinherited

axis name

Definition at line 103 of file IAxis.h.

Referenced by IAxis::axisName(), and IAxis::setAxisName().

◆ m_nbins


The documentation for this class was generated from the following files: