21 const size_t min_axis_size = 2;
31 return Bin1D(lowerBoundary(index), upperBoundary(index));
36 return lowerBoundary(0);
41 return upperBoundary(m_coordinates.size() - 1);
47 return m_coordinates[index];
52 if (value <= m_coordinates.front())
54 if (value >= m_coordinates.back())
55 return m_coordinates.size() - 1;
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;
63 std::vector<double> PointwiseAxis::getBinBoundaries()
const
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));
77 throw std::runtime_error(
"Error in PointwiseAxis::createClippedAxis: "
78 "'left' should be smaller than 'right'");
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;
87 std::string PointwiseAxis::pyString(
const std::string& units,
size_t offset)
const
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";
98 result << pyfmt::printValue(points.back(), units) <<
"])";
102 void PointwiseAxis::print(std::ostream& ostr)
const
104 auto precision = std::setprecision(std::numeric_limits<double>::digits10 + 2);
105 ostr <<
"PointwiseAxis(\"" <<
getName() <<
"\", "
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() <<
"])";
112 bool PointwiseAxis::equals(
const IAxis& other)
const
114 if (!IAxis::equals(other))
117 return m_coordinates == otherAxis->getBinCenters();
121 double PointwiseAxis::lowerBoundary(
size_t index)
const
124 return m_coordinates.front();
125 return 0.5 * (m_coordinates[index] + m_coordinates[index - 1]);
128 double PointwiseAxis::upperBoundary(
size_t index)
const
130 if (index + 1 == m_coordinates.size())
131 return m_coordinates.back();
132 return 0.5 * (m_coordinates[index] + m_coordinates[index + 1]);
135 void PointwiseAxis::checkIndex(
size_t index)
const
137 if (m_coordinates.size() > index)
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);
145 void PointwiseAxis::sanityCheck()
const
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");
152 const auto begin = m_coordinates.begin();
153 const auto end = m_coordinates.end();
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");
159 if (std::adjacent_find(begin, end) != end)
160 throw std::runtime_error(
"Error in PointwiseAxis::PointwiseAxis: passed coordinate vector "
161 "contains repeating values");
Defines class PointwiseAxis.
Defines functions in namespace pyfmt.
Interface for one-dimensional axes.
std::string getName() const
retrieve the label of the axis
Axis containing arbitrary (non-equidistant) coordinate values.
Bin1D getBin(size_t index) const override
retrieve a 1d bin for the given index
PointwiseAxis * clone() const override
clone function
double getBinCenter(size_t index) const override
Returns the coordinate corresponding to the given index.
PointwiseAxis * createClippedAxis(double left, double right) const override
Creates a new clipped axis.
double getMax() const override
Returns value of last on-axis point.
size_t findClosestIndex(double value) const override
find index of the coordinate closest to the given value
double getMin() const override
Returns value of first on-axis point.
std::string indent(size_t width)
Returns a string of blanks with given width.