Mantid
Loading...
Searching...
No Matches
Axis.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include "MantidAPI/Axis.h"
12#include "MantidAPI/TextAxis.h"
15
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> //For Py_ssize_t. We can get rid of this when RHEL5 goes
21
22// See
23// http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PY_ARRAY_UNIQUE_SYMBOL
24#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
25#define NO_IMPORT_ARRAY
26#include <numpy/arrayobject.h>
27
28using namespace Mantid::API;
31using namespace boost::python;
32
34
35namespace {
36
37//------------------------------- Overload macros ---------------------------
38GNU_DIAG_OFF("unused-local-typedef")
39// Ignore -Wconversion warnings coming from boost::python
40// Seen with GCC 7.1.1 and Boost 1.63.0
41GNU_DIAG_OFF("conversion")
42
43// Overloads for operator() function which has 1 optional argument
45
46GNU_DIAG_ON("conversion")
47GNU_DIAG_ON("unused-local-typedef")
55PyObject *extractAxisValues(Axis &self) {
56 const auto nvalues = static_cast<npy_intp>(self.length());
57 npy_intp arrayDims[1] = {nvalues};
58
59 // Pick the correct element type base on the Axis type
60 PyObject *array;
61 bool numeric(true);
62 if (self.isNumeric() || self.isSpectra()) {
63 array = PyArray_SimpleNew(1, arrayDims, NPY_DOUBLE);
64 } else if (self.isText()) {
65 array = PyList_New(static_cast<Py_ssize_t>(nvalues));
66 numeric = false;
67 } else {
68 throw std::invalid_argument("Unknown axis type. Cannot extract to Numpy");
69 }
70
71 // Fill the array
72 for (npy_intp i = 0; i < nvalues; ++i) {
73 if (numeric) {
74 PyObject *value = PyFloat_FromDouble(self.getValue(static_cast<size_t>(i)));
75 void *pos = PyArray_GETPTR1((PyArrayObject *)array, i);
76 PyArray_SETITEM(reinterpret_cast<PyArrayObject *>(array), reinterpret_cast<char *>(pos), value);
77 } else {
78 const std::string s = self.label(static_cast<size_t>(i));
79 PyObject *value = to_python_value<const std::string &>()(s);
80 PyList_SetItem(array, (Py_ssize_t)i, value);
81 }
82 }
83 return array;
84}
85} // namespace
86
88 register_ptr_to_python<Axis *>();
89
90 // Class
91 class_<Axis, boost::noncopyable>("MantidAxis", no_init)
92 .def("length", &Axis::length, arg("self"), "Returns the length of the axis")
93 .def("title", (const std::string &(Axis::*)() const) & Axis::title, arg("self"),
94 return_value_policy<copy_const_reference>(), "Get the axis title")
95 .def("isSpectra", &Axis::isSpectra, arg("self"), "Returns true if this is a SpectraAxis")
96 .def("isNumeric", &Axis::isNumeric, arg("self"), "Returns true if this is a NumericAxis")
97 .def("isText", &Axis::isText, arg("self"), "Returns true if this is a TextAxis")
98 .def("label", &Axis::label, (arg("self"), arg("index")), "Return the axis label")
99 .def("getUnit", (const Unit_sptr &(Axis::*)() const) & Axis::unit, arg("self"),
100 return_value_policy<copy_const_reference>(), "Returns the unit object for the axis")
101 .def("getValue", &Axis::getValue,
102 Axis_getValue((arg("self"), arg("index"), arg("vertical_index")),
103 "Returns the value at the given point on the Axis. "
104 "The vertical axis index [default=0]"))
105 .def("indexOfValue", &Axis::indexOfValue,
106 ((arg("self"), arg("value")), return_value_policy<copy_const_reference>(),
107 "Returns the index of the given value on the Axis. "))
108 .def("extractValues", &extractAxisValues, arg("self"), "Return a numpy array of the axis values")
109 .def("indexOfValue", &Axis::indexOfValue, (arg("value")),
110 "Returns the index of the closest to the given value on the axis")
111 .def("setUnit", &Axis::setUnit, (arg("self"), arg("unit_name")), return_value_policy<copy_const_reference>(),
112 "Set the unit for this axis by name.")
113 .def("setValue", &Axis::setValue, (arg("self"), arg("index"), arg("value")), "Set a value at the given index")
114 .def("getMin", &Axis::getMin, arg("self"), "Get min value specified on the axis")
115 .def("getMax", &Axis::getMax, arg("self"), "Get max value specified on the axis")
116 //------------------------------------ Special methods
117 //------------------------------------
118 .def("__len__", &Axis::length, arg("self"));
119}
120
121// --------------------------------------------------------------------------------------------
122// SpectraAxis
123// --------------------------------------------------------------------------------------------
129Axis *createSpectraAxis(const MatrixWorkspace *const ws) { return new SpectraAxis(ws); }
130
134 class_<SpectraAxis, bases<Axis>, boost::noncopyable>("SpectraAxis", no_init)
135 .def("create", &createSpectraAxis, arg("workspace"), return_internal_reference<>(),
136 "Creates a new SpectraAxis referencing the given workspace")
137 .staticmethod("create");
138}
139
140// --------------------------------------------------------------------------------------------
141// NumericAxis
142// --------------------------------------------------------------------------------------------
148Axis *createNumericAxis(int length) { return new NumericAxis(length); }
149
153 class_<NumericAxis, bases<Axis>, boost::noncopyable>("NumericAxis", no_init)
154 .def("create", &createNumericAxis, arg("length"), return_internal_reference<>(),
155 "Creates a new NumericAxis of a specified length")
156 .staticmethod("create");
157}
158
159// --------------------------------------------------------------------------------------------
160// BinEdgeAxis
161// --------------------------------------------------------------------------------------------
162
168Axis *createBinEdgeAxis(int length) { return new BinEdgeAxis(length); }
169
173 class_<BinEdgeAxis, bases<NumericAxis>, boost::noncopyable>("BinEdgeAxis", no_init)
174 .def("create", &createBinEdgeAxis, arg("length"), return_internal_reference<>(),
175 "Creates a new BinEdgeAxis of a specified length")
176 .staticmethod("create");
177}
178
179// --------------------------------------------------------------------------------------------
180// TextAxis
181// --------------------------------------------------------------------------------------------
182
188Axis *createTextAxis(int length) { return new TextAxis(length); }
189
191 class_<TextAxis, bases<Axis>, boost::noncopyable>("TextAxis", no_init)
192 .def("setLabel", &TextAxis::setLabel, (arg("self"), arg("index"), arg("label")),
193 "Set the label at the given entry")
194 .def("label", &TextAxis::label, (arg("self"), arg("index")), "Return the label at the given position")
195 .def("create", &createTextAxis, arg("length"), return_internal_reference<>(),
196 "Creates a new TextAxis of a specified length")
197 .staticmethod("create");
198}
double value
The value of the point.
Definition: FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
tagPyArrayObject PyArrayObject
Axis * createBinEdgeAxis(int length)
Creates a BinEdgeAxis.
Definition: Axis.cpp:168
void export_BinEdgeAxis()
Definition: Axis.cpp:170
void export_NumericAxis()
Definition: Axis.cpp:150
void export_Axis()
Definition: Axis.cpp:87
void export_SpectraAxis()
Definition: Axis.cpp:131
Axis * createTextAxis(int length)
Creates a TextAxis.
Definition: Axis.cpp:188
Axis * createSpectraAxis(const MatrixWorkspace *const ws)
Creates a SpectraAxis referencing a given workspace.
Definition: Axis.cpp:129
Axis * createNumericAxis(int length)
Creates a NumericAxis.
Definition: Axis.cpp:148
void export_TextAxis()
Definition: Axis.cpp:190
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2) void export_Property()
Definition: Property.cpp:102
#define GNU_DIAG_ON(x)
#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.
Definition: Axis.h:30
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.
Definition: Axis.cpp:20
virtual const std::shared_ptr< Kernel::Unit > & setUnit(const std::string &unitName)
Set the unit on the Axis.
Definition: Axis.cpp:39
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.
Definition: Axis.h:54
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.
Definition: Axis.h:52
const std::shared_ptr< Kernel::Unit > & unit() const
The unit for this axis.
Definition: Axis.cpp:28
double getValue(const std::size_t &index, const std::size_t &verticalIndex=0) const
Gets the value at the specified index.
Definition: Axis.cpp:51
virtual bool isSpectra() const
Returns true is the axis is a Spectra axis.
Definition: Axis.h:50
Stores numeric values that are assumed to be bin edge values.
Definition: BinEdgeAxis.h:20
Base MatrixWorkspace Abstract Class.
Class to represent a numeric axis of a workspace.
Definition: NumericAxis.h:29
Class to represent the spectra axis of a workspace.
Definition: SpectraAxis.h:31
Class to represent a text axis of a workspace.
Definition: TextAxis.h:36
void setLabel(const std::size_t &index, const std::string &lbl)
Set the label at the given index.
Definition: TextAxis.cpp:104
std::string label(const std::size_t &index) const override
Get the label at the specified index.
Definition: TextAxis.cpp:97
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
Definition: Unit.h:229
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16