14#include <boost/python/class.hpp>
15#include <boost/python/copy_non_const_reference.hpp>
16#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
17#define NO_IMPORT_ARRAY
18#include <numpy/arrayobject.h>
41PyObject *WrapReadOnlyNumpyFArray(
const Mantid::signal_t *arr, std::vector<Py_intptr_t> dims) {
43#if NPY_API_VERSION >= 0x00000007
45 PyArray_New(&PyArray_Type,
static_cast<int>(dims.size()), &dims[0], datatype,
nullptr,
46 static_cast<void *
>(
const_cast<double *
>(arr)), 0, NPY_ARRAY_FARRAY,
nullptr));
47 PyArray_CLEARFLAGS(nparray, NPY_ARRAY_WRITEABLE);
50 (
PyArrayObject *)PyArray_New(&PyArray_Type,
static_cast<int>(dims.size()), &dims[0], datatype,
nullptr,
51 static_cast<void *
>(
const_cast<double *
>(arr)), 0, NPY_FARRAY,
nullptr);
52 nparray->flags &= ~NPY_WRITEABLE;
54 return reinterpret_cast<PyObject *
>(nparray);
64 std::vector<size_t> nd;
68 for (
size_t i = 0; i < ndims; ++i) {
73 std::vector<Py_intptr_t> dims(ndims);
74 for (
size_t i = 0; i < ndims; ++i)
75 dims[i] =
static_cast<Py_intptr_t
>(nd[i]);
78 throw std::runtime_error(
"Workspace has zero dimensions!");
89 auto dims = countDimensions(self);
98 auto dims = countDimensions(self);
107 auto dims = countDimensions(self);
119 auto wsShape = countDimensions(self);
120 const size_t ndims = wsShape.size();
121 auto arrShape = signal.attr(
"shape");
122 if (ndims !=
static_cast<size_t>(len(arrShape))) {
123 std::ostringstream os;
125 <<
": The number of dimensions doe not match the current "
126 "workspace size. Workspace="
127 << ndims <<
" array=" << len(arrShape);
128 throw std::invalid_argument(os.str());
131 for (
size_t i = 0; i < ndims; ++i) {
132 int arrDim = extract<int>(arrShape[i])();
133 if (wsShape[i] != arrDim) {
134 std::ostringstream os;
135 os << fnLabel <<
": The dimension size for the " <<
std::to_string(i) <<
"th dimension do not match. "
136 <<
"Workspace dimension size=" << wsShape[i] <<
", array size=" << arrDim;
137 throw std::invalid_argument(os.str());
150 throwIfSizeIncorrect(self, signalValues,
"setSignalArray");
151 object rav = signalValues.attr(
"ravel")(
"F");
152 object flattened = rav.attr(
"flat");
153 auto length = len(flattened);
154 for (
auto i = 0; i < length; ++i) {
155 self.
setSignalAt(i, extract<double>(flattened[i])());
167 throwIfSizeIncorrect(self, errorSquared,
"setErrorSquaredArray");
168 object rav = errorSquared.attr(
"ravel")(
"F");
169 object flattened = rav.attr(
"flat");
170 auto length = len(flattened);
171 for (
auto i = 0; i < length; ++i) {
181 throw std::invalid_argument(
"setSignalAt: The index is greater than the "
182 "number of bins in the workspace");
190 class_<IMDHistoWorkspace, bases<IMDWorkspace, MultipleExperimentInfos>, boost::noncopyable>(
"IMDHistoWorkspace",
192 .def(
"getSignalArray", &getSignalArrayAsNumpyArray, arg(
"self"),
193 "Returns a read-only numpy array containing the signal values")
195 .def(
"getErrorSquaredArray", &getErrorSquaredArrayAsNumpyArray, arg(
"self"),
196 "Returns a read-only numpy array containing the square of the error "
199 .def(
"getNumEventsArray", &getNumEventsArrayAsNumpyArray, arg(
"self"),
200 "Returns a read-only numpy array containing the number of MD events "
204 return_value_policy<copy_non_const_reference>(),
"Return a reference to the signal at the linear index")
207 return_value_policy<copy_non_const_reference>(),
"Return the squared-errors at the linear index")
209 .def(
"setSignalAt", &setSignalAt, (arg(
"self"), arg(
"index"), arg(
"value")),
210 "Sets the signal at the specified index.")
213 "Sets the squared-error at the specified index.")
215 .def(
"setSignalArray", &setSignalArray, (arg(
"self"), arg(
"signalValues")),
216 "Sets the signal from a numpy array. The sizes must match the "
217 "current workspace sizes. A ValueError is thrown if not")
219 .def(
"setErrorSquaredArray", &setErrorSquaredArray, (arg(
"self"), arg(
"errorSquared")),
220 "Sets the square of the errors from a numpy array. The sizes must "
221 "match the current workspace sizes. A ValueError is thrown if not")
224 "Sets all signals/errors in the workspace to the given values")
227 return_value_policy<return_by_value>(),
"Return the inverse of volume of EACH cell in the workspace.")
230 (arg(
"self"), arg(
"index1"), arg(
"index2")), return_value_policy<return_by_value>(),
231 "Get the 1D linear index from the 2D array")
233 .def(
"getLinearIndex",
235 (arg(
"self"), arg(
"index1"), arg(
"index2"), arg(
"index3")), return_value_policy<return_by_value>(),
236 "Get the 1D linear index from the 3D array")
238 .def(
"getLinearIndex",
240 (arg(
"self"), arg(
"index1"), arg(
"index2"), arg(
"index3"), arg(
"index4")),
241 return_value_policy<return_by_value>(),
"Get the 1D linear index from the 4D array")
244 return_value_policy<return_by_value>(),
"Return the position of the center of a bin at a given position")
247 "Sets the visual normalization of"
double value
The value of the point.
#define GET_POINTER_SPECIALIZATION(TYPE)
std::map< DeltaEMode::Type, std::string > index
tagPyArrayObject PyArrayObject
void export_IMDHistoWorkspace()
Abstract interface to MDHistoWorkspace, for use in exposing to Python.
virtual signal_t & errorSquaredAt(size_t index)=0
virtual Mantid::Kernel::VMD getCenter(size_t linearIndex) const =0
virtual const signal_t * getErrorSquaredArray() const =0
virtual const signal_t * getNumEventsArray() const =0
virtual signal_t & signalAt(size_t index)=0
virtual void setErrorSquaredAt(size_t index, signal_t value)=0
virtual const signal_t * getSignalArray() const =0
virtual void setDisplayNormalization(const Mantid::API::MDNormalization &preferredNormalization)=0
virtual void setSignalAt(size_t index, signal_t value)=0
virtual coord_t getInverseVolume() const =0
See the MDHistoWorkspace definition for descriptions of these.
virtual size_t getLinearIndex(size_t index1, size_t index2) const =0
virtual void setTo(signal_t signal, signal_t errorSquared, signal_t numEvents)=0
virtual uint64_t getNPoints() const =0
Get the number of points associated with the workspace.
virtual std::shared_ptr< const Mantid::Geometry::IMDDimension > getDimension(size_t index) const
Get a dimension.
virtual size_t getNumDims() const
Thin object wrapper around a numpy array.
double signal_t
Typedef for the signal recorded in a MDBox, etc.
std::string to_string(const wide_integer< Bits, Signed > &n)
Defines a mapping between C++ type given by the template parameter and numpy type enum NPY_TYPES.
Encapsulates the registration required for an interface type T that sits on top of a Kernel::DataItem...