28 #ifndef PY_HISTOGRAM_H 29 #define PY_HISTOGRAM_H 32 #include <tomographerpy/common.h> 60 bins(py::cast(Eigen::VectorXd::Zero(params_.
num_bins))),
61 off_chart(py::cast(0.0))
66 : params(min, max, num_bins),
67 bins(py::cast(Eigen::VectorXd::Zero(num_bins))),
68 off_chart(py::cast(0.0))
72 template<
typename Scalar,
typename CountType>
75 bins(py::cast(h.
bins)),
84 inline operator X ()
const 86 typedef typename X::Scalar Scalar;
87 typedef typename X::CountType CountType;
88 return toCxxHistogram<Scalar,CountType>();
91 template<
typename Scalar,
typename CountType>
96 h.
off_chart = off_chart.cast<CountType>();
100 inline void set_bins(py::object newbins)
102 if (py::len(newbins.attr(
"shape")) != 1) {
103 throw py::type_error(
"Expected 1-D NumPy array for assignment to Histogram.bins");
105 if ((Eigen::Index)py::len(newbins) != params.
num_bins) {
106 throw py::type_error(
streamstr(
"Expected "<<params.
num_bins<<
" elements for assignment to Histogram.bins," 107 " got "<<py::len(newbins)));
111 inline void set_off_chart(py::object o)
113 auto np = py::module::import(
"numpy");
114 if ( ! np.attr(
"isscalar")(o).cast<bool>() ) {
115 throw py::type_error(
"Expected scalar for assignment to Histogram.off_chart");
120 inline void load(py::object x, py::object o)
126 inline py::object normalization()
const {
127 auto np = py::module::import(
"numpy");
129 return np.attr(
"add")(off_chart, np.attr(
"multiply")(py::cast(params.
binResolution()), bins.attr(
"sum")()));
132 inline py::object totalCounts()
const {
133 auto np = py::module::import(
"numpy");
135 return np.attr(
"add")(off_chart, bins.attr(
"sum")());
138 HistogramParams params;
140 py::object off_chart;
142 enum { HasErrorBars = 0 };
151 delta(py::cast(Eigen::VectorXd::Zero(params_.
num_bins)))
157 delta(py::cast(Eigen::VectorXd::Zero(num_bins)))
161 template<
typename Scalar,
typename CountType>
164 delta(py::cast(h.
delta))
172 inline operator X ()
const 174 typedef typename X::Scalar Scalar;
175 typedef typename X::CountType CountType;
176 return toCxxHistogram<Scalar,CountType>();
179 template<
typename Scalar,
typename CountType>
185 h.
off_chart = off_chart.cast<CountType>();
189 inline void set_delta(py::object newdelta)
191 if (py::len(newdelta.attr(
"shape")) != 1) {
192 throw py::type_error(
"Expected 1-D NumPy array for assignment to HistogramWithErrorBars.delta");
194 if ((Eigen::Index)py::len(newdelta) != params.num_bins) {
195 throw py::type_error(
streamstr(
"Expected "<<params.num_bins<<
" elements for assignment to HistogramWithErrorBars.delta," 196 " got "<<py::len(newdelta)));
201 inline void load(py::object x, py::object err, py::object o)
210 enum { HasErrorBars = 1 };
Eigen::Array< CountType, Eigen::Dynamic, 1 > delta
The error bars associated with each histogram bin.
Tomographer::HistogramParams< RealType > HistogramParams
Histogram Params. See Tomographer::HistogramParams.
Histogram class like Tomographer::Histogram, but with NumPy arrays storage.
Scalar binResolution() const
Returns the width of a bin.
Stores a histogram along with error bars.
Params params
Parameters of this histogram (range and # of bins)
double RealType
Real type for template arguments (double)
A Histogram with real counts and error bars. See Tomographer::HistogramWithErrorBars.
Eigen::Array< CountType, Eigen::Dynamic, 1 > bins
The counts for each bin.
CountType off_chart
The number of points that fell outside of the given range.
Some C++ utilities, with a tad of C++11 tricks.
Definitions for Histogram Types.
#define streamstr(tokens)
Utility macro to format stream tokens to a std::string.
C++ Classes and Utilities for Python Modules.
Eigen::Index num_bins
Number of bins to split the range into.
internal::cast_return_type< Array< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols >, const CwiseUnaryOp< internal::scalar_cast_op< typename internal::traits< Array< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Scalar, NewType >, const Array< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > > >::type cast() const