13#include "MantidHistogramData/HistogramY.h"
14#include "MantidIndexing/IndexInfo.h"
26#include <boost/python/class.hpp>
27#include <boost/python/copy_const_reference.hpp>
28#include <boost/python/implicit.hpp>
29#include <boost/python/list.hpp>
30#include <boost/python/overloads.hpp>
31#include <boost/python/register_ptr_to_python.hpp>
32#include <boost/python/suite/indexing/map_indexing_suite.hpp>
33#include <boost/python/tuple.hpp>
35#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
36#define NO_IMPORT_ARRAY
37#include <numpy/arrayobject.h>
55using return_readonly_numpy = return_value_policy<VectorRefToNumpy<WrapReadOnly>>;
57using return_readwrite_numpy = return_value_policy<VectorRefToNumpy<WrapReadWrite>>;
80void setSpectrumFromPyObject(
MatrixWorkspace &self, data_modifier accessor,
const size_t wsIndex,
81 const boost::python::object &values) {
84 converter.
copyTo((self.*accessor)(wsIndex));
87 converter.
copyTo((self.*accessor)(wsIndex));
126 const auto &spectrumNums = self.
indexInfo().spectrumNumbers();
129 for (
const auto &
index : spectrumNums) {
130 spectra.append(
static_cast<int32_t
>(
index));
142void setXFromPyObject(
MatrixWorkspace &self,
const size_t wsIndex,
const boost::python::object &values) {
152void setYFromPyObject(
MatrixWorkspace &self,
const size_t wsIndex,
const boost::python::object &values) {
172void setDxFromPyObject(
MatrixWorkspace &self,
const size_t wsIndex,
const boost::python::object &values) {
183 PyErr_Warn(PyExc_DeprecationWarning,
"``getSampleDetails`` is deprecated, use ``getRun`` instead.");
194 PyErr_Warn(PyExc_DeprecationWarning,
"``getNumberBins`` is deprecated, use ``blocksize`` instead.");
208std::size_t binIndexOfDeprecated(
MatrixWorkspace &self,
const double xValue,
const std::size_t
index = 0) {
209 PyErr_Warn(PyExc_DeprecationWarning,
"``binIndexOf`` is deprecated, use ``yIndexOfX`` instead.");
218BOOST_PYTHON_FUNCTION_OVERLOADS(binIndexOfDeprecatedOverloads, binIndexOfDeprecated, 2, 3)
238void pythonReplaceAxis(
MatrixWorkspace &self,
const std::size_t &axisIndex,
Axis *newAxis) {
252 throw std::invalid_argument(
"MatrixWorkspace::getSignalAtCoord - Input "
253 "array must have shape (n, 2)");
256 Py_intptr_t length = len(npCoords);
263 for (
int i = 0; i < length; ++i) {
264 std::array<Mantid::coord_t, 2> coord = {{coords[2 * i], coords[2 * i + 1]}};
267 PyObject *npSignalArray =
269 return object(handle<>(npSignalArray));
273 int64_t first = extract<int64_t>(start[0]);
274 int64_t second = extract<int64_t>(start[1]);
275 auto idx = self.
findY(
value, std::make_pair(first, second));
276 return make_tuple(idx.first, idx.second);
288 const size_t setIndex) {
301 const size_t setIndex) {
305std::vector<size_t> getIndicesFromDetectorIDs(
MatrixWorkspace &self,
const boost::python::list &detIDs) {
316 class_<MatrixWorkspace, boost::python::bases<ExperimentInfo, IMDWorkspace>, boost::noncopyable>(
"MatrixWorkspace",
321 "Returns true if the workspace is ragged (has differently sized "
325 "Returns the number of bins for a given histogram index.")
326 .def(
"getNumberBins", &getNumberBinsDeprecated, arg(
"self"),
327 "Returns size of the Y data array (deprecated, use "
328 ":class:`~mantid.api.MatrixWorkspace.blocksize` "
331 "Returns the maximum number of bins in a workspace (works on ragged "
334 "Returns the number of spectra in the workspace")
335 .def(
"getSpectrumNumbers", &getSpectrumNumbers, arg(
"self"),
336 "Returns a list of all spectrum numbers in the workspace")
338 MatrixWorkspace_yIndexOfXOverloads((arg(
"self"), arg(
"xvalue"), arg(
"workspaceIndex"), arg(
"tolerance")),
339 "Returns the y index which corresponds to the X Value provided. "
340 "The workspace_index [default=0] and tolerance [default=0.0] is "
343 "Returns the two theta value for a given detector")
345 "Returns the signed two theta value for given detector")
347 (arg(
"self"), arg(
"workspaceIndex")), return_internal_reference<>(),
348 "Return the spectra at the given workspace index.")
350 "Returns workspace index correspondent to the given spectrum "
351 "number. Throws if no such spectrum is present in the workspace")
352 .def(
"getIndicesFromDetectorIDs", &getIndicesFromDetectorIDs, (arg(
"self"), arg(
"detID_list")),
353 "Returns a list of workspace indices from the corrresponding "
356 (arg(
"self"), arg(
"workspaceIndex")),
357 "Return the :class:`~mantid.geometry.Detector` or "
358 ":class:`~mantid.geometry.DetectorGroup` that is linked to "
359 "the given workspace index")
361 "Return the :class:`~mantid.api.Run` object for this workspace")
362 .def(
"axes", &
MatrixWorkspace::axes, arg(
"self"),
"Returns the number of axes attached to the workspace")
364 "Get a pointer to a workspace axis")
366 "Returns ``True`` if this is considered to be binned data.")
368 "Returns the status of the distribution flag")
370 "Returns the current Y unit for the data (Y axis) in the workspace")
372 MatrixWorkspace_YUnitLabelOverloads((arg(
"self"), arg(
"useLatex"), arg(
"plotAsDistribution")),
373 "Returns the caption for the Y axis"))
375 "Returns true if any of the bins in this workspace are masked.")
377 "Returns true if this spectrum contains any masked bins")
378 .def(
"maskedBinsIndices", &maskedBinsIndices, (arg(
"self"), arg(
"workspaceIndex")),
379 "Returns all the masked bins' indices at the workspace index. "
380 ":class:`~mantid.api.MatrixWorkspace.hasMaskedBins` MUST be called "
381 "first to check if any bins are "
382 "masked, otherwise an exception will be thrown")
383 .def(
"findY", &findY, (arg(
"self"), arg(
"value"), arg(
"start") = make_tuple(0, 0)),
384 "Find first index in Y equal to value. Start may be specified to "
385 "begin at a specifc index. Returns tuple with the "
386 "histogram and bin indices.")
387 .def(
"getSampleDetails", &getSampleDetailsDeprecated, arg(
"self"), return_internal_reference<>(),
388 "Return the Run object for this workspace (deprecated, use "
389 ":class:`~mantid.api.MatrixWorkspace.getRun` "
391 .def(
"binIndexOf", &binIndexOfDeprecated,
392 binIndexOfDeprecatedOverloads((arg(
"self"), arg(
"xvalue"), arg(
"workspaceIndex")),
393 "Returns the index of the bin containing the given xvalue "
395 ":class:`~mantid.api.MatrixWorkspace.yIndexOfX` instead)"))
400 "Sets a new caption for the data (Y axis) in the workspace")
402 "Sets a new unit for the data (Y axis) in the workspace")
404 "Set distribution flag. If True the workspace has been divided by "
406 .def(
"replaceAxis", &pythonReplaceAxis, (arg(
"self"), arg(
"axisIndex"), arg(
"newAxis")),
407 "Replaces one of the workspace's axes with the new one provided. "
408 "The axis is cloned.")
409 .def(
"applyBinEdgesFromAnotherWorkspace", &applyBinEdgesFromAnotherWorkspace,
410 (arg(
"self"), arg(
"ws"), arg(
"getIndex"), arg(
"setIndex")),
411 "Sets the bin edges at setIndex to be the bin edges of ws at "
413 .def(
"applyPointsFromAnotherWorkspace", &applyPointsFromAnotherWorkspace,
414 (arg(
"self"), arg(
"ws"), arg(
"getIndex"), arg(
"setIndex")),
415 "Sets the points at setIndex to be the points of ws at getIndex.")
420 "Creates a read-only numpy wrapper "
421 "around the original X data at the "
424 "Creates a read-only numpy wrapper "
425 "around the original Y data at the "
428 "Creates a read-only numpy wrapper "
429 "around the original E data at the "
432 "Creates a read-only numpy wrapper "
433 "around the original Dx data at the "
436 "Returns True if the spectrum uses the DX (X Error) array, else "
440 .def(
"dataX", (data_modifier)&
MatrixWorkspace::dataX, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
441 "Creates a writable numpy wrapper around the original X data at the "
443 .def(
"dataY", (data_modifier)&
MatrixWorkspace::dataY, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
444 "Creates a writable numpy wrapper around the original Y data at the "
446 .def(
"dataE", (data_modifier)&
MatrixWorkspace::dataE, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
447 "Creates a writable numpy wrapper around the original E data at the "
449 .def(
"dataDx", (data_modifier)&
MatrixWorkspace::dataDx, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
450 "Creates a writable numpy wrapper around the original Dx data at "
452 .def(
"setX", &setXFromPyObject, args(
"self",
"workspaceIndex",
"x"),
453 "Set X values from a python list or numpy array. It performs a "
454 "simple copy into the array.")
455 .def(
"setY", &setYFromPyObject, args(
"self",
"workspaceIndex",
"y"),
456 "Set Y values from a python list or numpy array. It performs a "
457 "simple copy into the array.")
458 .def(
"setE", &setEFromPyObject, args(
"self",
"workspaceIndex",
"e"),
459 "Set E values from a python list or numpy array. It performs a "
460 "simple copy into the array.")
461 .def(
"setDx", &setDxFromPyObject, args(
"self",
"workspaceIndex",
"dX"),
462 "Set Dx values from a python list or numpy array. It performs a "
463 "simple copy into the array.")
468 "Extracts (copies) the X data from the workspace into a 2D numpy "
470 "Note: This can fail for large workspaces as numpy will require a "
472 "of memory free that will fit all of the data.")
474 "Extracts (copies) the Y data from the workspace into a 2D numpy "
476 "Note: This can fail for large workspaces as numpy will require a "
478 "of memory free that will fit all of the data.")
480 "Extracts (copies) the E data from the workspace into a 2D numpy "
482 "Note: This can fail for large workspaces as numpy will require a "
484 "of memory free that will fit all of the data.")
486 "Extracts (copies) the E data from the workspace into a 2D numpy "
488 "Note: This can fail for large workspaces as numpy will require a "
490 "of memory free that will fit all of the data.")
491 .def(
"getSignalAtCoord", &getSignalAtCoord, args(
"self",
"coords",
"normalization"),
492 "Return signal for array of coordinates")
496 "Performs a comparison operation on two workspaces, using the "
497 "CompareWorkspaces algorithm")
499 .def(
"getMonitorWorkspace", &getMonitorWorkspace, args(
"self"),
500 "Return internal monitor workspace bound to current workspace.")
501 .def(
"setMonitorWorkspace", &setMonitorWorkspace, args(
"self",
"MonitorWS"),
502 "Set specified workspace as monitor workspace for"
503 "current workspace. "
504 "Note: The workspace does not have to contain monitors though "
505 "some subsequent algorithms may expect it to be "
506 "monitor workspace later.")
507 .def(
"clearMonitorWorkspace", &clearMonitorWorkspace, args(
"self"),
508 "Forget about monitor workspace, attached to the current workspace")
511 "Returns true if the workspace has common X bins with log spacing.");
double value
The value of the point.
#define GET_POINTER_SPECIALIZATION(TYPE)
std::map< DeltaEMode::Type, std::string > index
void export_MatrixWorkspace()
Python exports of the Mantid::API::MatrixWorkspace class.
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2) void export_Property()
#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 Axis * clone(const MatrixWorkspace *const parentWorkspace)=0
Virtual constructor.
Run & mutableRun()
Writable version of the run object.
A "spectrum" is an object that holds the data for a particular spectrum, in particular:
Base MatrixWorkspace Abstract Class.
std::string YUnitLabel(bool useLatex=false, bool plotAsDistribution=false) const
Returns a caption for the units of the data in the workspace.
virtual std::pair< int64_t, int64_t > findY(double value, const std::pair< int64_t, int64_t > &idx={0, 0}) const
Find first index in Y equal to value.
const MantidVec & readE(std::size_t const index) const
Deprecated, use e() instead.
virtual ISpectrum & getSpectrum(const size_t index)=0
Return the underlying ISpectrum ptr at the given workspace index.
double detectorSignedTwoTheta(const Geometry::IDetector &det) const
Returns the signed 2Theta scattering angle for a detector.
const MantidVec & readDx(size_t const index) const
Deprecated, use dx() instead.
bool hasAnyMaskedBins() const
Does this workspace contain any masked bins.
virtual MantidVec & dataDx(const std::size_t index)
Deprecated, use mutableDx() instead. Returns the x error data.
virtual bool isCommonLogBins() const
Returns true if the workspace contains common X bins with log spacing.
HistogramData::BinEdges binEdges(const size_t index) const
void setPoints(const size_t index, T &&...data) &
HistogramData::Points points(const size_t index) const
double detectorTwoTheta(const Geometry::IDetector &det) const
Returns the 2Theta scattering angle for a detector.
virtual std::size_t blocksize() const =0
Returns the size of each block of data returned by the dataY accessors.
signal_t getSignalAtCoord(const coord_t *coords, const Mantid::API::MDNormalization &normalization) const override
Get the signal at a coordinate in the workspace.
virtual MantidVec & dataX(const std::size_t index)
Deprecated, use mutableX() instead. Returns the x data.
virtual void setMonitorWorkspace(const std::shared_ptr< MatrixWorkspace > &monitorWS)
Sets the internal monitor workspace to the provided workspace.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
bool hasMaskedBins(const size_t &workspaceIndex) const
Does this spectrum contain any masked bins.
std::size_t yIndexOfX(const double xValue, const std::size_t &index=0, const double tolerance=0.0) const
Returns the y index which corresponds to the X Value provided.
virtual MantidVec & dataE(const std::size_t index)
Deprecated, use mutableE() instead. Returns the error data.
bool isDistribution() const
Are the Y-values dimensioned?
std::shared_ptr< MatrixWorkspace > monitorWorkspace() const
Returns a pointer to the internal monitor workspace.
virtual bool isRaggedWorkspace() const =0
Returns true if the workspace is ragged (has differently sized spectra).
std::vector< size_t > maskedBinsIndices(const size_t &workspaceIndex) const
std::vector< size_t > getIndicesFromDetectorIDs(const std::vector< detid_t > &detIdList) const
Converts a list of detector IDs to the corresponding workspace indices.
void setDistribution(bool newValue)
Set the flag for whether the Y-values are dimensioned.
const Indexing::IndexInfo & indexInfo() const
Returns a const reference to the IndexInfo object of the workspace.
virtual bool hasDx(const std::size_t index) const
Probes if DX (X Error) values were set on a particular spectrum.
virtual std::size_t getMaxNumberBins() const =0
Returns the maximum number of bins in a workspace (works on ragged data).
const MantidVec & readY(std::size_t const index) const
Deprecated, use y() instead.
virtual Axis * getAxis(const std::size_t &axisIndex) const
Get a non owning pointer to a workspace axis.
virtual std::size_t getNumberBins(const std::size_t &index) const =0
Returns the number of bins for a given histogram index.
const MantidVec & readX(std::size_t const index) const
Deprecated, use x() instead.
size_t getIndexFromSpectrumNumber(const specnum_t specNo) const
Given a spectrum number, find the corresponding workspace index.
void replaceAxis(const std::size_t &axisIndex, std::unique_ptr< Axis > newAxis)
Replaces one of the workspace's axes with the new one provided.
std::string YUnit() const
Returns the units of the data in the workspace.
virtual MantidVec & dataY(const std::size_t index)
Deprecated, use mutableY() instead. Returns the y data.
void setYUnitLabel(const std::string &newLabel)
Sets a new caption for the data (Y axis) in the workspace.
virtual bool isCommonBins() const
Returns true if the workspace contains common X bins.
std::shared_ptr< const Geometry::IDetector > getDetector(const size_t workspaceIndex) const
Get the effective detector for the given spectrum.
void setYUnit(const std::string &newUnit)
Sets a new unit for the data (Y axis) in the workspace.
virtual bool isHistogramData() const
Returns true if the workspace contains data in histogram form (as opposed to point-like)
void setBinEdges(const size_t index, T &&...data) &
This class stores information regarding an experimental run as a series of log entries.
Thin object wrapper around a numpy array.
static bool check(const boost::python::object &obj)
Check if a python object points to an array type object.
Py_intptr_t const * get_shape() const
bool MANTID_API_DLL equals(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs, double tolerance=0.0)
Performs a comparison operation on two workspaces, using the CompareWorkspaces algorithm.
MDNormalization
Enum describing different ways to normalize the signal in a MDWorkspace.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
PyObject * wrapWithNDArray(const ElementType *, const int ndims, Py_intptr_t *dims, const NumpyWrapMode mode, const OwnershipMode oMode=OwnershipMode::Cpp)
Defines the wrapWithNDArray specialization for C array types.
PyObject * cloneE(const API::MatrixWorkspace &self)
Create a numpy array from the E values of the given workspace reference.
PyObject * cloneY(const API::MatrixWorkspace &self)
Create a numpy array from the Y values of the given workspace reference.
PyObject * cloneDx(const API::MatrixWorkspace &self)
Create a numpy array from the E values of the given workspace reference.
PyObject * cloneX(const API::MatrixWorkspace &self)
{ Create a numpy array from the X values of the given workspace reference
double signal_t
Typedef for the signal recorded in a MDBox, etc.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Converter taking an input numpy array and converting it to a std::vector.
void copyTo(TypedVector &dest) const
Fill the container with data from the array.
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
void copyTo(TypedVector &dest)
Fill the container with data from the array.
Encapsulates the registration required for an interface type T that sits on top of a Kernel::DataItem...