28 #ifndef PY_HISTOGRAM_H 29 #define PY_HISTOGRAM_H 32 #include <tomographerpy/common.h> 56 bins(py::cast(Eigen::VectorXd::Zero(params_.
num_bins))),
57 off_chart(py::cast(0.0))
62 : params(min, max, num_bins),
63 bins(py::cast(Eigen::VectorXd::Zero(num_bins))),
64 off_chart(py::cast(0.0))
68 template<
typename Scalar,
typename CountType>
71 bins(py::cast(h.
bins)),
80 inline operator X ()
const 82 typedef typename X::Scalar Scalar;
83 typedef typename X::CountType CountType;
84 return toCxxHistogram<Scalar,CountType>();
87 template<
typename Scalar,
typename CountType>
92 h.
off_chart = off_chart.cast<CountType>();
96 inline void set_bins(py::object newbins)
98 if (py::len(newbins.attr(
"shape")) != 1) {
99 throw py::value_error(
"Expected 1-D NumPy array for assignment to Histogram.bins");
101 if ((Eigen::Index)py::len(newbins) != params.
num_bins) {
102 throw py::value_error(
streamstr(
"Expected "<<params.
num_bins<<
" elements for assignment to Histogram.bins," 103 " got "<<py::len(newbins)));
107 inline void set_off_chart(py::object o)
109 auto np = py::module::import(
"numpy");
110 if ( ! np.attr(
"isscalar")(o).cast<bool>() ) {
111 throw py::value_error(
"Expected scalar for assignment to Histogram.off_chart");
116 inline void load(py::object x, py::object o)
122 inline py::object normalization()
const {
123 auto np = py::module::import(
"numpy");
125 return np.attr(
"add")(off_chart, np.attr(
"multiply")(py::cast(params.
binResolution()), bins.attr(
"sum")()));
128 inline py::object totalCounts()
const {
129 auto np = py::module::import(
"numpy");
131 return np.attr(
"add")(off_chart, bins.attr(
"sum")());
134 HistogramParams params;
136 py::object off_chart;
138 enum { HasErrorBars = 0 };
148 delta(py::cast(Eigen::VectorXd::Zero(params_.
num_bins)))
154 delta(py::cast(Eigen::VectorXd::Zero(num_bins)))
158 template<
typename Scalar,
typename CountType>
161 delta(py::cast(h.
delta))
169 inline operator X ()
const 171 typedef typename X::Scalar Scalar;
172 typedef typename X::CountType CountType;
173 return toCxxHistogram<Scalar,CountType>();
176 template<
typename Scalar,
typename CountType>
182 h.
off_chart = off_chart.cast<CountType>();
186 inline void set_delta(py::object newdelta)
188 if (py::len(newdelta.attr(
"shape")) != 1) {
189 throw py::value_error(
"Expected 1-D NumPy array for assignment to HistogramWithErrorBars.delta");
191 if ((Eigen::Index)py::len(newdelta) != params.num_bins) {
192 throw py::value_error(
streamstr(
"Expected " << params.num_bins <<
" elements for assignment to " 193 "HistogramWithErrorBars.delta, got " << py::len(newdelta)));
198 inline void load(py::object x, py::object err, py::object o)
207 enum { HasErrorBars = 1 };
Eigen::Array< CountType, Eigen::Dynamic, 1 > delta
The error bars associated with each histogram bin.
Tomographer::HistogramParams< RealScalar > HistogramParams
Histogram Params. See Tomographer::HistogramParams.
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)
Histogram class like Tomographer::Histogram, but with NumPy arrays storage.
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.
double RealScalar
Real type for general calculation precisions (density matrix elements, log-likelihood value calculati...
Definitions for Histogram Types.
#define streamstr(tokens)
Utility macro to format stream tokens to a std::string.
C++ Classes and Utilities for Python Modules.
A Histogram with real counts and error bars. See Tomographer::HistogramWithErrorBars.
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