16#include <boost/python/class.hpp>
17#include <boost/python/copy_const_reference.hpp>
18#include <boost/python/overloads.hpp>
19#include <boost/python/register_ptr_to_python.hpp>
20#include <boost/python/ssize_t.hpp>
24#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
25#define NO_IMPORT_ARRAY
26#include <numpy/arrayobject.h>
45BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Axis_getValue,
Axis::getValue, 1, 2)
56PyObject *extractAxisValues(
Axis &self) {
57 const auto nvalues =
static_cast<npy_intp
>(self.
length());
58 npy_intp arrayDims[1] = {nvalues};
64 array = PyArray_SimpleNew(1, arrayDims, NPY_DOUBLE);
65 }
else if (self.
isText()) {
66 array = PyList_New(
static_cast<Py_ssize_t
>(nvalues));
69 throw std::invalid_argument(
"Unknown axis type. Cannot extract to Numpy");
73 for (npy_intp i = 0; i < nvalues; ++i) {
75 PyObject *
value = PyFloat_FromDouble(self.
getValue(
static_cast<size_t>(i)));
77 PyArray_SETITEM(
reinterpret_cast<PyArrayObject *
>(array),
reinterpret_cast<char *
>(pos),
value);
79 const std::string s = self.
label(
static_cast<size_t>(i));
80 PyObject *
value = to_python_value<const std::string &>()(s);
81 PyList_SetItem(array, (Py_ssize_t)i,
value);
89 register_ptr_to_python<Axis *>();
92 class_<Axis, boost::noncopyable>(
"MantidAxis", no_init)
93 .def(
"length", &
Axis::length, arg(
"self"),
"Returns the length of the axis")
94 .def(
"title", (
const std::string &(
Axis::*)()
const) &
Axis::title, arg(
"self"),
95 return_value_policy<copy_const_reference>(),
"Get the axis title")
96 .def(
"isSpectra", &
Axis::isSpectra, arg(
"self"),
"Returns true if this is a SpectraAxis")
97 .def(
"isNumeric", &
Axis::isNumeric, arg(
"self"),
"Returns true if this is a NumericAxis")
98 .def(
"isText", &
Axis::isText, arg(
"self"),
"Returns true if this is a TextAxis")
99 .def(
"label", &
Axis::label, (arg(
"self"), arg(
"index")),
"Return the axis label")
101 return_value_policy<copy_const_reference>(),
"Returns the unit object for the axis")
103 Axis_getValue((arg(
"self"), arg(
"index"), arg(
"vertical_index")),
104 "Returns the value at the given point on the Axis. "
105 "The vertical axis index [default=0]"))
107 ((arg(
"self"), arg(
"value")), return_value_policy<copy_const_reference>(),
108 "Returns the index of the given value on the Axis. "))
109 .def(
"extractValues", &extractAxisValues, arg(
"self"),
"Return a numpy array of the axis values")
111 "Returns the index of the closest to the given value on the axis")
112 .def(
"setUnit", &
Axis::setUnit, (arg(
"self"), arg(
"unit_name")), return_value_policy<copy_const_reference>(),
113 "Set the unit for this axis by name.")
114 .def(
"setValue", &
Axis::setValue, (arg(
"self"), arg(
"index"), arg(
"value")),
"Set a value at the given index")
115 .def(
"getMin", &
Axis::getMin, arg(
"self"),
"Get min value specified on the axis")
116 .def(
"getMax", &
Axis::getMax, arg(
"self"),
"Get max value specified on the axis")
135 class_<SpectraAxis, bases<Axis>, boost::noncopyable>(
"SpectraAxis", no_init)
136 .def(
"create", &
createSpectraAxis, arg(
"workspace"), return_internal_reference<>(),
137 "Creates a new SpectraAxis referencing the given workspace")
138 .staticmethod(
"create");
154 class_<NumericAxis, bases<Axis>, boost::noncopyable>(
"NumericAxis", no_init)
156 "Creates a new NumericAxis of a specified length")
157 .staticmethod(
"create");
174 class_<BinEdgeAxis, bases<NumericAxis>, boost::noncopyable>(
"BinEdgeAxis", no_init)
176 "Creates a new BinEdgeAxis of a specified length")
177 .staticmethod(
"create");
192 class_<TextAxis, bases<Axis>, boost::noncopyable>(
"TextAxis", no_init)
194 "Set the label at the given entry")
195 .def(
"label", &
TextAxis::label, (arg(
"self"), arg(
"index")),
"Return the label at the given position")
196 .def(
"create", &
createTextAxis, arg(
"length"), return_internal_reference<>(),
197 "Creates a new TextAxis of a specified length")
198 .staticmethod(
"create");
double value
The value of the point.
#define GET_POINTER_SPECIALIZATION(TYPE)
tagPyArrayObject PyArrayObject
Axis * createBinEdgeAxis(int length)
Creates a BinEdgeAxis.
void export_BinEdgeAxis()
void export_NumericAxis()
void export_SpectraAxis()
Axis * createTextAxis(int length)
Creates a TextAxis.
Axis * createSpectraAxis(const MatrixWorkspace *const ws)
Creates a SpectraAxis referencing a given workspace.
Axis * createNumericAxis(int length)
Creates a NumericAxis.
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Class to represent the axis of a workspace.
virtual double getMin() const =0
returns min value defined on axis
virtual double getMax() const =0
returns max value defined on axis
const std::string & title() const
Returns the user-defined title for this axis.
virtual const std::shared_ptr< Kernel::Unit > & setUnit(const std::string &unitName)
Set the unit on the Axis.
virtual std::string label(const std::size_t &index) const =0
Returns a text label of for a value Note that the index here is not the index of a value,...
virtual bool isText() const
Returns true if the axis is Text.
virtual size_t indexOfValue(const double value) const =0
Find the index of the given double value.
virtual std::size_t length() const =0
Get the length of the axis.
virtual void setValue(const std::size_t &index, const double &value)=0
Sets the value at the specified index.
virtual bool isNumeric() const
Returns true if the axis is numeric.
const std::shared_ptr< Kernel::Unit > & unit() const
The unit for this axis.
double getValue(const std::size_t &index, const std::size_t &verticalIndex=0) const
Gets the value at the specified index.
virtual bool isSpectra() const
Returns true is the axis is a Spectra axis.
Stores numeric values that are assumed to be bin edge values.
Base MatrixWorkspace Abstract Class.
Class to represent a numeric axis of a workspace.
Class to represent the spectra axis of a workspace.
Class to represent a text axis of a workspace.
void setLabel(const std::size_t &index, const std::string &lbl)
Set the label at the given index.
std::string label(const std::size_t &index) const override
Get the label at the specified index.
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
int32_t specnum_t
Typedef for a spectrum Number.