BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
PointwiseAxis Class Reference
Inheritance diagram for PointwiseAxis:
Collaboration diagram for PointwiseAxis:

Public Member Functions

template<class String , class Vector >
 PointwiseAxis (String &&name, Vector &&coordinate_values)
 
PointwiseAxisclone () const override
 
 ~PointwiseAxis () override=default
 
size_t size () const override
 
double operator[] (size_t index) const override
 
Bin1D getBin (size_t index) const override
 
double getMin () const override
 
double getMax () const override
 
double getBinCenter (size_t index) const override
 
size_t findClosestIndex (double value) const override
 
std::vector< double > getBinCenters () const override
 
std::vector< double > getBinBoundaries () const override
 
PointwiseAxiscreateClippedAxis (double left, double right) const override
 
std::string pyString (const std::string &units, size_t offset) const final
 
std::string getName () const
 
void setName (std::string name)
 
bool operator== (const IAxis &right) const
 
bool operator!= (const IAxis &right) const
 
virtual bool contains (double value) const
 

Private Member Functions

void print (std::ostream &ostr) const override
 
bool equals (const IAxis &other) const override
 
double lowerBoundary (size_t index) const
 
double upperBoundary (size_t index) const
 
void checkIndex (size_t index) const
 
void sanityCheck () const
 

Private Attributes

std::vector< double > m_coordinates
 
std::string m_name
 

Detailed Description

Axis containing arbitrary (non-equidistant) coordinate values.

Lower boundary of the first bin and upper boundary of the last bin correspond to first and last passed coordinates. Other bin boundaries are computed as arithmetical mean of two adjacent coordinates. One should be aware, that bin centers reported by PointwiseAxis::getBinCenter do not coincide with the values produced by Bin1D::getMidPoint. On-axis values are bounded by minimum/maximum values passed to the constructor.

Definition at line 32 of file PointwiseAxis.h.

Constructor & Destructor Documentation

◆ PointwiseAxis()

template<class String , class Vector >
PointwiseAxis::PointwiseAxis ( String &&  name,
Vector &&  coordinate_values 
)
inline

Definition at line 36 of file PointwiseAxis.h.

37  : IAxis(std::forward<String>(name)), m_coordinates(std::forward<Vector>(coordinate_values))
38  {
39  sanityCheck();
40  }
IAxis(const std::string &name)
constructors
Definition: IAxis.h:28
std::vector< double > m_coordinates
Definition: PointwiseAxis.h:87
void sanityCheck() const

References sanityCheck().

Referenced by clone(), and createClippedAxis().

Here is the call graph for this function:

◆ ~PointwiseAxis()

PointwiseAxis::~PointwiseAxis ( )
overridedefault

Member Function Documentation

◆ clone()

PointwiseAxis * PointwiseAxis::clone ( ) const
overridevirtual

clone function

Implements IAxis.

Definition at line 23 of file PointwiseAxis.cpp.

24 {
25  return new PointwiseAxis(getName(), m_coordinates);
26 }
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40
PointwiseAxis(String &&name, Vector &&coordinate_values)
Definition: PointwiseAxis.h:36

References IAxis::getName(), m_coordinates, and PointwiseAxis().

Here is the call graph for this function:

◆ size()

size_t PointwiseAxis::size ( ) const
inlineoverridevirtual

retrieve the number of bins

Implements IAxis.

Definition at line 48 of file PointwiseAxis.h.

48 { return m_coordinates.size(); }

References m_coordinates.

◆ operator[]()

double PointwiseAxis::operator[] ( size_t  index) const
inlineoverridevirtual

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 51 of file PointwiseAxis.h.

51 { return getBinCenter(index); }
double getBinCenter(size_t index) const override
Returns the coordinate corresponding to the given index.

References getBinCenter().

Here is the call graph for this function:

◆ getBin()

Bin1D PointwiseAxis::getBin ( size_t  index) const
overridevirtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 28 of file PointwiseAxis.cpp.

29 {
30  checkIndex(index);
31  return Bin1D(lowerBoundary(index), upperBoundary(index));
32 }
double upperBoundary(size_t index) const
double lowerBoundary(size_t index) const
void checkIndex(size_t index) const
Definition: Bin.h:20

References checkIndex(), lowerBoundary(), and upperBoundary().

Here is the call graph for this function:

◆ getMin()

double PointwiseAxis::getMin ( ) const
overridevirtual

Returns value of first on-axis point.

Implements IAxis.

Definition at line 34 of file PointwiseAxis.cpp.

35 {
36  return lowerBoundary(0);
37 }

References lowerBoundary().

Here is the call graph for this function:

◆ getMax()

double PointwiseAxis::getMax ( ) const
overridevirtual

Returns value of last on-axis point.

Implements IAxis.

Definition at line 39 of file PointwiseAxis.cpp.

40 {
41  return upperBoundary(m_coordinates.size() - 1);
42 }

References m_coordinates, and upperBoundary().

Here is the call graph for this function:

◆ getBinCenter()

double PointwiseAxis::getBinCenter ( size_t  index) const
overridevirtual

Returns the coordinate corresponding to the given index.

Implements IAxis.

Definition at line 44 of file PointwiseAxis.cpp.

45 {
46  checkIndex(index);
47  return m_coordinates[index];
48 }

References checkIndex(), and m_coordinates.

Referenced by operator[]().

Here is the call graph for this function:

◆ findClosestIndex()

size_t PointwiseAxis::findClosestIndex ( double  value) const
overridevirtual

find index of the coordinate closest to the given value

Implements IAxis.

Definition at line 50 of file PointwiseAxis.cpp.

51 {
52  if (value <= m_coordinates.front())
53  return 0;
54  if (value >= m_coordinates.back())
55  return m_coordinates.size() - 1;
56 
57  const auto begin = m_coordinates.begin();
58  auto result = std::lower_bound(begin, m_coordinates.end(), value);
59  size_t index = static_cast<size_t>(std::distance(begin, result));
60  return value < lowerBoundary(index) ? index - 1 : index;
61 }

References lowerBoundary(), and m_coordinates.

Referenced by createClippedAxis().

Here is the call graph for this function:

◆ getBinCenters()

std::vector<double> PointwiseAxis::getBinCenters ( ) const
inlineoverridevirtual

Reimplemented from IAxis.

Definition at line 69 of file PointwiseAxis.h.

69 { return m_coordinates; }

References m_coordinates.

Referenced by pyString().

◆ getBinBoundaries()

std::vector< double > PointwiseAxis::getBinBoundaries ( ) const
overridevirtual

Reimplemented from IAxis.

Definition at line 63 of file PointwiseAxis.cpp.

64 {
65  std::vector<double> result;
66  const size_t v_size = m_coordinates.size();
67  result.reserve(v_size + 1);
68  for (size_t i = 0; i < v_size; ++i)
69  result.push_back(lowerBoundary(i));
70  result.push_back(upperBoundary(v_size - 1));
71  return result;
72 }

References lowerBoundary(), m_coordinates, and upperBoundary().

Here is the call graph for this function:

◆ createClippedAxis()

PointwiseAxis * PointwiseAxis::createClippedAxis ( double  left,
double  right 
) const
overridevirtual

Creates a new clipped axis.

Reimplemented from IAxis.

Definition at line 74 of file PointwiseAxis.cpp.

75 {
76  if (left >= right)
77  throw std::runtime_error("Error in PointwiseAxis::createClippedAxis: "
78  "'left' should be smaller than 'right'");
79 
80  using diff_t = std::vector<double>::iterator::difference_type;
81  auto begin = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(left));
82  auto end = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(right)) + 1;
83 
84  return new PointwiseAxis(getName(), std::vector<double>(begin, end));
85 }
size_t findClosestIndex(double value) const override
find index of the coordinate closest to the given value

References findClosestIndex(), IAxis::getName(), m_coordinates, and PointwiseAxis().

Here is the call graph for this function:

◆ pyString()

std::string PointwiseAxis::pyString ( const std::string &  units,
size_t  offset 
) const
finalvirtual

Implements IAxis.

Definition at line 87 of file PointwiseAxis.cpp.

88 {
89  std::ostringstream result;
90  const std::string py_def_call = "numpy.asarray([";
91  const size_t total_offset = offset + py_def_call.size();
92  result << py_def_call;
93  std::vector<double> points = getBinCenters();
94  for (auto iter = points.begin(); iter != points.end() - 1; ++iter) {
95  result << pyfmt::printValue(*iter, units) << ",\n";
96  result << pyfmt::indent(total_offset);
97  }
98  result << pyfmt::printValue(points.back(), units) << "])";
99  return result.str();
100 }
std::vector< double > getBinCenters() const override
Definition: PointwiseAxis.h:69
std::string indent(size_t width)
Returns a string of blanks with given width.
Definition: PyFmt.cpp:141
std::string printValue(double value, const std::string &units)
Definition: PyFmt.cpp:104

References getBinCenters(), pyfmt::indent(), and pyfmt::printValue().

Here is the call graph for this function:

◆ print()

void PointwiseAxis::print ( std::ostream &  ostr) const
overrideprivatevirtual

Implements IAxis.

Definition at line 102 of file PointwiseAxis.cpp.

103 {
104  auto precision = std::setprecision(std::numeric_limits<double>::digits10 + 2);
105  ostr << "PointwiseAxis(\"" << getName() << "\", "
106  << ", [";
107  for (size_t i = 0, fin = m_coordinates.size() - 1; i < fin; ++i)
108  ostr << precision << m_coordinates[i] << ",";
109  ostr << precision << m_coordinates.back() << "])";
110 }

References IAxis::getName(), m_coordinates, and anonymous_namespace{OutputDataWriteStrategy.cpp}::precision.

Here is the call graph for this function:

◆ equals()

bool PointwiseAxis::equals ( const IAxis other) const
overrideprivatevirtual

Reimplemented from IAxis.

Definition at line 112 of file PointwiseAxis.cpp.

113 {
114  if (!IAxis::equals(other))
115  return false;
116  if (const PointwiseAxis* otherAxis = dynamic_cast<const PointwiseAxis*>(&other))
117  return m_coordinates == otherAxis->getBinCenters();
118  return false;
119 }
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:18
Axis containing arbitrary (non-equidistant) coordinate values.
Definition: PointwiseAxis.h:33

References IAxis::equals(), and m_coordinates.

Here is the call graph for this function:

◆ lowerBoundary()

double PointwiseAxis::lowerBoundary ( size_t  index) const
private

Definition at line 121 of file PointwiseAxis.cpp.

122 {
123  if (index == 0)
124  return m_coordinates.front();
125  return 0.5 * (m_coordinates[index] + m_coordinates[index - 1]);
126 }

References m_coordinates.

Referenced by findClosestIndex(), getBin(), getBinBoundaries(), and getMin().

◆ upperBoundary()

double PointwiseAxis::upperBoundary ( size_t  index) const
private

Definition at line 128 of file PointwiseAxis.cpp.

129 {
130  if (index + 1 == m_coordinates.size())
131  return m_coordinates.back();
132  return 0.5 * (m_coordinates[index] + m_coordinates[index + 1]);
133 }

References m_coordinates.

Referenced by getBin(), getBinBoundaries(), and getMax().

◆ checkIndex()

void PointwiseAxis::checkIndex ( size_t  index) const
private

Definition at line 135 of file PointwiseAxis.cpp.

136 {
137  if (m_coordinates.size() > index)
138  return;
139  std::string message = "Error in PointwiseAxis::getBinCenter: passed index ";
140  message += std::to_string(index) + " exceeds the size ";
141  message += std::to_string(m_coordinates.size()) + " of the axis";
142  throw std::runtime_error(message);
143 }

References m_coordinates.

Referenced by getBin(), and getBinCenter().

◆ sanityCheck()

void PointwiseAxis::sanityCheck ( ) const
private

Definition at line 145 of file PointwiseAxis.cpp.

146 {
147  if (m_coordinates.size() < min_axis_size)
148  throw std::runtime_error(
149  "Error in PointwiseAxis::PointwiseAxis: the size of passed coordinate array is "
150  "less than minimum acceptable value");
151 
152  const auto begin = m_coordinates.begin();
153  const auto end = m_coordinates.end();
154 
155  if (!std::is_sorted(begin, end))
156  throw std::runtime_error("Error in PointwiseAxis::PointwiseAxis: passed coordinates are "
157  "not sorted in ascending order");
158 
159  if (std::adjacent_find(begin, end) != end)
160  throw std::runtime_error("Error in PointwiseAxis::PointwiseAxis: passed coordinate vector "
161  "contains repeating values");
162 }
const size_t min_axis_size

References m_coordinates, and min_axis_size.

Referenced by PointwiseAxis().

◆ getName()

◆ setName()

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

Sets the axis label.

Definition at line 43 of file IAxis.h.

43 { m_name = name; }

References IAxis::m_name.

◆ operator==()

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

test for equality

Definition at line 63 of file IAxis.h.

63 { return equals(right); }

References IAxis::equals().

Here is the call graph for this function:

◆ operator!=()

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

Definition at line 64 of file IAxis.h.

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

◆ contains()

bool IAxis::contains ( double  value) const
virtualinherited

Returns true if axis contains given point.

Definition at line 40 of file IAxis.cpp.

41 {
42  return value >= getMin() && value < getMax();
43 }
virtual double getMin() const =0
Returns value of first point of axis.
virtual double getMax() const =0
Returns value of last point of axis.

References IAxis::getMax(), and IAxis::getMin().

Referenced by Histogram1D::crop(), Histogram2D::crop(), RectangularDetector::indexOfSpecular(), and SphericalDetector::indexOfSpecular().

Here is the call graph for this function:

Member Data Documentation

◆ m_coordinates

std::vector<double> PointwiseAxis::m_coordinates
private

◆ m_name

std::string IAxis::m_name
privateinherited

axis name

Definition at line 92 of file IAxis.h.

Referenced by IAxis::getName(), and IAxis::setName().


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