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/dict.hpp>
29#include <boost/python/implicit.hpp>
30#include <boost/python/list.hpp>
31#include <boost/python/overloads.hpp>
32#include <boost/python/register_ptr_to_python.hpp>
33#include <boost/python/suite/indexing/map_indexing_suite.hpp>
34#include <boost/python/tuple.hpp>
36#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
37#define NO_IMPORT_ARRAY
38#include <numpy/arrayobject.h>
56using return_readonly_numpy = return_value_policy<VectorRefToNumpy<WrapReadOnly>>;
58using return_readwrite_numpy = return_value_policy<VectorRefToNumpy<WrapReadWrite>>;
69BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(MatrixWorkspace_YUnitLabelOverloads, YUnitLabel, 0, 2)
82void setSpectrumFromPyObject(
MatrixWorkspace &self, data_modifier accessor,
const size_t wsIndex,
83 const boost::python::object &values) {
86 converter.
copyTo((self.*accessor)(wsIndex));
89 converter.
copyTo((self.*accessor)(wsIndex));
128 const auto &spectrumNums = self.
indexInfo().spectrumNumbers();
131 for (
const auto &
index : spectrumNums) {
132 spectra.append(
static_cast<int32_t
>(
index));
144void setXFromPyObject(
MatrixWorkspace &self,
const size_t wsIndex,
const boost::python::object &values) {
154void setYFromPyObject(
MatrixWorkspace &self,
const size_t wsIndex,
const boost::python::object &values) {
164void setEFromPyObject(
MatrixWorkspace &self,
const size_t wsIndex,
const boost::python::object &values) {
174void setDxFromPyObject(
MatrixWorkspace &self,
const size_t wsIndex,
const boost::python::object &values) {
178std::vector<double> getIntegratedCountsForWorkspaceIndices(
MatrixWorkspace &self,
179 const boost::python::object &workspaceIndices,
180 const size_t numberOfWorkspaces,
const double minX,
181 const double maxX,
const bool entireRange) {
183 std::vector<size_t> sWorkspaceIndices(numberOfWorkspaces);
186 converter.
copyTo(sWorkspaceIndices);
189 converter.
copyTo(sWorkspaceIndices);
202 PyErr_Warn(PyExc_DeprecationWarning,
"``getSampleDetails`` is deprecated, use ``getRun`` instead.");
213 PyErr_Warn(PyExc_DeprecationWarning,
"``getNumberBins`` is deprecated, use ``blocksize`` instead.");
227std::size_t binIndexOfDeprecated(
MatrixWorkspace &self,
const double xValue,
const std::size_t
index = 0) {
228 PyErr_Warn(PyExc_DeprecationWarning,
"``binIndexOf`` is deprecated, use ``yIndexOfX`` instead.");
237BOOST_PYTHON_FUNCTION_OVERLOADS(binIndexOfDeprecatedOverloads, binIndexOfDeprecated, 2, 3)
257void pythonReplaceAxis(
MatrixWorkspace &self,
const std::size_t &axisIndex,
Axis *newAxis) {
271 throw std::invalid_argument(
"MatrixWorkspace::getSignalAtCoord - Input "
272 "array must have shape (n, 2)");
275 Py_intptr_t length = len(npCoords);
282 for (
int i = 0; i < length; ++i) {
283 std::array<Mantid::coord_t, 2> coord = {{coords[2 * i], coords[2 * i + 1]}};
286 PyObject *npSignalArray =
288 return object(handle<>(npSignalArray));
292 int64_t first = extract<int64_t>(start[0]);
293 int64_t second = extract<int64_t>(start[1]);
294 auto idx = self.
findY(
value, std::make_pair(first, second));
295 return make_tuple(idx.first, idx.second);
307 const size_t setIndex) {
320 const size_t setIndex) {
324std::vector<size_t> getIndicesFromDetectorIDs(
MatrixWorkspace &self,
const boost::python::list &detIDs) {
328boost::python::dict getDetectorIDToWorkspaceIndexMap(
MatrixWorkspace &self,
bool throwIfMultipleDets,
329 bool ignoreIfNoValidDets) {
331 boost::python::dict pythonDict;
332 for (
const auto &[key,
value] : unorderedMap) {
333 pythonDict[key] =
value;
346 class_<MatrixWorkspace, boost::python::bases<ExperimentInfo, IMDWorkspace>, boost::noncopyable>(
"MatrixWorkspace",
351 "Returns true if the workspace is ragged (has differently sized "
355 "Returns the number of bins for a given histogram index.")
356 .def(
"getNumberBins", &getNumberBinsDeprecated, arg(
"self"),
357 "Returns size of the Y data array (deprecated, use "
358 ":class:`~mantid.api.MatrixWorkspace.blocksize` "
361 "Returns the maximum number of bins in a workspace (works on ragged "
364 "Returns the number of spectra in the workspace")
368 .def(
"getSpectrumNumbers", &getSpectrumNumbers, arg(
"self"),
369 "Returns a list of all spectrum numbers in the workspace")
371 MatrixWorkspace_yIndexOfXOverloads((arg(
"self"), arg(
"xvalue"), arg(
"workspaceIndex"), arg(
"tolerance")),
372 "Returns the y index which corresponds to the X Value provided. "
373 "The workspace_index [default=0] and tolerance [default=0.0] is "
376 "Returns the two theta value for a given detector")
378 "Returns the signed two theta value for given detector")
380 (arg(
"self"), arg(
"workspaceIndex")), return_internal_reference<>(),
381 "Return the spectra at the given workspace index.")
383 "Returns workspace index correspondent to the given spectrum "
384 "number. Throws if no such spectrum is present in the workspace")
385 .def(
"getIndicesFromDetectorIDs", &getIndicesFromDetectorIDs, (arg(
"self"), arg(
"detID_list")),
386 "Returns a list of workspace indices from the corrresponding "
388 .def(
"getDetectorIDToWorkspaceIndexMap", &getDetectorIDToWorkspaceIndexMap,
389 (arg(
"self"), arg(
"throwIfMultipleDets"), arg(
"ignoreIfNoValidDets")),
390 " Return a map where the key is detector ID (pixel ID), and the value at that index = the corresponding "
393 (arg(
"self"), arg(
"workspaceIndex")),
394 "Return the :class:`~mantid.geometry.Detector` or "
395 ":class:`~mantid.geometry.DetectorGroup` that is linked to "
396 "the given workspace index")
398 "Return the :class:`~mantid.api.Run` object for this workspace")
399 .def(
"axes", &
MatrixWorkspace::axes, arg(
"self"),
"Returns the number of axes attached to the workspace")
401 "Get a pointer to a workspace axis")
403 "Returns ``True`` if this is considered to be binned data.")
405 "Returns the status of the distribution flag")
407 "Returns the current Y unit for the data (Y axis) in the workspace")
409 MatrixWorkspace_YUnitLabelOverloads((arg(
"self"), arg(
"useLatex"), arg(
"plotAsDistribution")),
410 "Returns the caption for the Y axis"))
412 "Returns true if any of the bins in this workspace are masked.")
414 "Returns true if this spectrum contains any masked bins")
415 .def(
"maskedBinsIndices", &maskedBinsIndices, (arg(
"self"), arg(
"workspaceIndex")),
416 "Returns all the masked bins' indices at the workspace index. "
417 ":class:`~mantid.api.MatrixWorkspace.hasMaskedBins` MUST be called "
418 "first to check if any bins are "
419 "masked, otherwise an exception will be thrown")
420 .def(
"findY", &findY, (arg(
"self"), arg(
"value"), arg(
"start") = make_tuple(0, 0)),
421 "Find first index in Y equal to value. Start may be specified to "
422 "begin at a specifc index. Returns tuple with the "
423 "histogram and bin indices.")
424 .def(
"getSampleDetails", &getSampleDetailsDeprecated, arg(
"self"), return_internal_reference<>(),
425 "Return the Run object for this workspace (deprecated, use "
426 ":class:`~mantid.api.MatrixWorkspace.getRun` "
428 .def(
"binIndexOf", &binIndexOfDeprecated,
429 binIndexOfDeprecatedOverloads((arg(
"self"), arg(
"xvalue"), arg(
"workspaceIndex")),
430 "Returns the index of the bin containing the given xvalue "
432 ":class:`~mantid.api.MatrixWorkspace.yIndexOfX` instead)"))
437 "Sets a new plot type for the workspace")
439 "Sets the marker type for the workspace")
441 "Sets the size of the marker for the workspace")
443 "Sets a new caption for the data (Y axis) in the workspace")
445 "Sets a new unit for the data (Y axis) in the workspace")
447 "Set distribution flag. If True the workspace has been divided by "
449 .def(
"replaceAxis", &pythonReplaceAxis, (arg(
"self"), arg(
"axisIndex"), arg(
"newAxis")),
450 "Replaces one of the workspace's axes with the new one provided. "
451 "The axis is cloned.")
452 .def(
"applyBinEdgesFromAnotherWorkspace", &applyBinEdgesFromAnotherWorkspace,
453 (arg(
"self"), arg(
"ws"), arg(
"getIndex"), arg(
"setIndex")),
454 "Sets the bin edges at setIndex to be the bin edges of ws at "
456 .def(
"applyPointsFromAnotherWorkspace", &applyPointsFromAnotherWorkspace,
457 (arg(
"self"), arg(
"ws"), arg(
"getIndex"), arg(
"setIndex")),
458 "Sets the points at setIndex to be the points of ws at getIndex.")
463 "Creates a read-only numpy wrapper "
464 "around the original X data at the "
467 "Creates a read-only numpy wrapper "
468 "around the original Y data at the "
471 "Creates a read-only numpy wrapper "
472 "around the original E data at the "
475 "Creates a read-only numpy wrapper "
476 "around the original Dx data at the "
479 "Returns True if the spectrum uses the DX (X Error) array, else "
483 .def(
"dataX", (data_modifier)&
MatrixWorkspace::dataX, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
484 "Creates a writable numpy wrapper around the original X data at the "
486 .def(
"dataY", (data_modifier)&
MatrixWorkspace::dataY, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
487 "Creates a writable numpy wrapper around the original Y data at the "
489 .def(
"dataE", (data_modifier)&
MatrixWorkspace::dataE, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
490 "Creates a writable numpy wrapper around the original E data at the "
492 .def(
"dataDx", (data_modifier)&
MatrixWorkspace::dataDx, return_readwrite_numpy(), args(
"self",
"workspaceIndex"),
493 "Creates a writable numpy wrapper around the original Dx data at "
495 .def(
"setX", &setXFromPyObject, args(
"self",
"workspaceIndex",
"x"),
496 "Set X values from a python list or numpy array. It performs a "
497 "simple copy into the array.")
498 .def(
"setY", &setYFromPyObject, args(
"self",
"workspaceIndex",
"y"),
499 "Set Y values from a python list or numpy array. It performs a "
500 "simple copy into the array.")
501 .def(
"setE", &setEFromPyObject, args(
"self",
"workspaceIndex",
"e"),
502 "Set E values from a python list or numpy array. It performs a "
503 "simple copy into the array.")
504 .def(
"setDx", &setDxFromPyObject, args(
"self",
"workspaceIndex",
"dX"),
505 "Set Dx values from a python list or numpy array. It performs a "
506 "simple copy into the array.")
511 "Extracts (copies) the X data from the workspace into a 2D numpy "
513 "Note: This can fail for large workspaces as numpy will require a "
515 "of memory free that will fit all of the data.")
517 "Extracts (copies) the Y data from the workspace into a 2D numpy "
519 "Note: This can fail for large workspaces as numpy will require a "
521 "of memory free that will fit all of the data.")
523 "Extracts (copies) the E data from the workspace into a 2D numpy "
525 "Note: This can fail for large workspaces as numpy will require a "
527 "of memory free that will fit all of the data.")
529 "Extracts (copies) the E data from the workspace into a 2D numpy "
531 "Note: This can fail for large workspaces as numpy will require a "
533 "of memory free that will fit all of the data.")
534 .def(
"getSignalAtCoord", &getSignalAtCoord, args(
"self",
"coords",
"normalization"),
535 "Return signal for array of coordinates")
536 .def(
"getIntegratedCountsForWorkspaceIndices", &getIntegratedCountsForWorkspaceIndices,
537 args(
"self",
"workspaceIndices",
"numberOfWorkspaces",
"minX",
"maxX",
"entireRange"),
538 "Return a vector with the integrated counts within the given range for the given workspace indices")
542 "Performs a comparison operation on two workspaces, using the "
543 "CompareWorkspaces algorithm")
545 .def(
"getMonitorWorkspace", &getMonitorWorkspace, args(
"self"),
546 "Return internal monitor workspace bound to current workspace.")
547 .def(
"setMonitorWorkspace", &setMonitorWorkspace, args(
"self",
"MonitorWS"),
548 "Set specified workspace as monitor workspace for"
549 "current workspace. "
550 "Note: The workspace does not have to contain monitors though "
551 "some subsequent algorithms may expect it to be "
552 "monitor workspace later.")
553 .def(
"clearMonitorWorkspace", &clearMonitorWorkspace, args(
"self"),
554 "Forget about monitor workspace, attached to the current workspace")
557 "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.
#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.
void setMarkerStyle(const std::string &markerType)
Set the marker style for plotting.
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::string getMarkerStyle() const
Get the marker style for plotting.
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.
float getMarkerSize() const
Get the size of the marker for plotting.
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 std::string & YUnit() const
std::string getPlotType() const
Gets MatrixWorkspace plot_type.
const MantidVec & readY(std::size_t const index) const
Deprecated, use y() instead.
detid2index_map getDetectorIDToWorkspaceIndexMap(bool throwIfMultipleDets=false, bool ignoreIfNoValidDets=false) const
Return a map where: KEY is the DetectorID (pixel ID) VALUE is the Workspace Index.
virtual Axis * getAxis(const std::size_t &axisIndex) const
Get a non owning pointer to a workspace axis.
void setPlotType(const std::string &)
Sets MatrixWorkspace plot_type.
std::vector< double > getIntegratedCountsForWorkspaceIndices(const std::vector< size_t > &workspaceIndices, const double minX, const double maxX, const bool entireRange) const
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.
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.
void setMarkerSize(const float markerSize)
Set the size of the marker for plotting.
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)
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...