Mantid
Loading...
Searching...
No Matches
MatrixWorkspace.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 +
8
9#include "MantidAPI/Axis.h"
10#include "MantidAPI/Run.h"
13#include "MantidHistogramData/HistogramDx.h"
14#include "MantidHistogramData/HistogramE.h"
15#include "MantidHistogramData/HistogramX.h"
16#include "MantidHistogramData/HistogramY.h"
17#include "MantidIndexing/IndexInfo.h"
20
29
30#include <boost/python/class.hpp>
31#include <boost/python/copy_const_reference.hpp>
32#include <boost/python/dict.hpp>
33#include <boost/python/implicit.hpp>
34#include <boost/python/list.hpp>
35#include <boost/python/overloads.hpp>
36#include <boost/python/register_ptr_to_python.hpp>
37#include <boost/python/suite/indexing/map_indexing_suite.hpp>
38#include <boost/python/tuple.hpp>
39
40#define PY_ARRAY_UNIQUE_SYMBOL API_ARRAY_API
41#define NO_IMPORT_ARRAY
42#include <numpy/arrayobject.h>
43
44using namespace Mantid::API;
45using namespace Mantid::Geometry;
46using namespace Mantid::Kernel;
47using namespace Mantid::PythonInterface;
51using namespace boost::python;
52
54
55namespace {
57using return_readonly_numpy = return_value_policy<VectorRefToNumpy<WrapReadOnly>>;
59using return_readwrite_numpy = return_value_policy<VectorRefToNumpy<WrapReadWrite>>;
60
61//------------------------------- Overload macros ---------------------------
62GNU_DIAG_OFF("unused-local-typedef")
63// Ignore -Wconversion warnings coming from boost::python
64// Seen with GCC 7.1.1 and Boost 1.63.0
65GNU_DIAG_OFF("conversion")
66// Overloads for yIndexOfX function which has 2 optional argument
67// cppcheck-suppress unknownMacro
68BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(MatrixWorkspace_yIndexOfXOverloads, MatrixWorkspace::yIndexOfX, 1, 3)
69// Overloads for YUnitLabel which has 1 optional argument
70BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(MatrixWorkspace_YUnitLabelOverloads, YUnitLabel, 0, 1)
71GNU_DIAG_ON("conversion")
72GNU_DIAG_ON("unused-local-typedef")
73
74
85template <typename HistogramType>
86void setSpectrumFromPyObject(HistogramType &histogram, const boost::python::object &values) {
87 auto data = histogram.rawData();
88 if (NDArray::check(values)) {
89 NDArrayToVector<double> converter(values);
90 converter.copyTo(data);
91 } else {
92 PySequenceToVector<double> converter(values);
93 converter.copyTo(data);
94 }
95 histogram = HistogramType(std::move(data));
96}
97
104void setMonitorWorkspace(MatrixWorkspace &self, const boost::python::object &value) {
106 std::dynamic_pointer_cast<MatrixWorkspace>(Mantid::PythonInterface::ExtractSharedPtr<Workspace>(value)());
107 self.setMonitorWorkspace(monWS);
108}
114std::weak_ptr<Workspace> getMonitorWorkspace(MatrixWorkspace &self) {
115 return std::weak_ptr<Workspace>(self.monitorWorkspace());
116}
122void clearMonitorWorkspace(MatrixWorkspace &self) {
124 self.setMonitorWorkspace(monWS);
125}
126
132list getSpectrumNumbers(const MatrixWorkspace &self) {
133 const auto &spectrumNums = self.indexInfo().spectrumNumbers();
134 list spectra;
135
136 for (const auto &index : spectrumNums) {
137 spectra.append(static_cast<int32_t>(index));
138 }
139
140 return spectra;
141}
142
152const Mantid::MantidVec &xData(MatrixWorkspace &self, const size_t index) { return self.x(index).rawData(); }
153const Mantid::MantidVec &yData(MatrixWorkspace &self, const size_t index) { return self.y(index).rawData(); }
154const Mantid::MantidVec &eData(MatrixWorkspace &self, const size_t index) { return self.e(index).rawData(); }
155const Mantid::MantidVec &dxData(MatrixWorkspace &self, const size_t index) { return self.dx(index).rawData(); }
156
157const Mantid::MantidVec &mutableXData(MatrixWorkspace &self, const size_t index) {
158 return self.mutableX(index).rawData();
159}
160const Mantid::MantidVec &mutableYData(MatrixWorkspace &self, const size_t index) {
161 return self.mutableY(index).rawData();
162}
163const Mantid::MantidVec &mutableEData(MatrixWorkspace &self, const size_t index) {
164 return self.mutableE(index).rawData();
165}
166const Mantid::MantidVec &mutableDxData(MatrixWorkspace &self, const size_t index) {
167 return self.mutableDx(index).rawData();
168}
169
176const Mantid::MantidVec &readXDeprecated(MatrixWorkspace &self, const size_t index) {
177 PyErr_Warn(PyExc_DeprecationWarning,
178 "`MatrixWorkspace.readX()` is deprecated in Mantid 7.0, use `MatrixWorkspace.x()` instead. "
179 "For more information, see the Histogram data concept page: "
180 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
181 return xData(self, index);
182}
183
190const Mantid::MantidVec &readYDeprecated(MatrixWorkspace &self, const size_t index) {
191 PyErr_Warn(PyExc_DeprecationWarning,
192 "`MatrixWorkspace.readY()` is deprecated in Mantid 7.0, use `MatrixWorkspace.y()` instead. "
193 "For more information, see the Histogram data concept page: "
194 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
195 return yData(self, index);
196}
197
204const Mantid::MantidVec &readEDeprecated(MatrixWorkspace &self, const size_t index) {
205 PyErr_Warn(PyExc_DeprecationWarning,
206 "`MatrixWorkspace.readE()` is deprecated in Mantid 7.0, use `MatrixWorkspace.e()` instead. "
207 "For more information, see the Histogram data concept page: "
208 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
209 return eData(self, index);
210}
211
218const Mantid::MantidVec &readDxDeprecated(MatrixWorkspace &self, const size_t index) {
219 PyErr_Warn(PyExc_DeprecationWarning,
220 "`MatrixWorkspace.readDx()` is deprecated in Mantid 7.0, use `MatrixWorkspace.dx()` instead. "
221 "For more information, see the Histogram data concept page: "
222 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
223 return dxData(self, index);
224}
225
232const Mantid::MantidVec &dataXDeprecated(MatrixWorkspace &self, const size_t index) {
233 PyErr_Warn(PyExc_DeprecationWarning,
234 "`MatrixWorkspace.dataX()` is deprecated in Mantid 7.0, use `MatrixWorkspace.mutableX()` instead. "
235 "For more information, see the Histogram data concept page: "
236 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
237 return mutableXData(self, index);
238}
239
246const Mantid::MantidVec &dataYDeprecated(MatrixWorkspace &self, const size_t index) {
247 PyErr_Warn(PyExc_DeprecationWarning,
248 "`MatrixWorkspace.dataY()` is deprecated in Mantid 7.0, use `MatrixWorkspace.mutableY()` instead. "
249 "For more information, see the Histogram data concept page: "
250 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
251 return mutableYData(self, index);
252}
253
260const Mantid::MantidVec &dataEDeprecated(MatrixWorkspace &self, const size_t index) {
261 PyErr_Warn(PyExc_DeprecationWarning,
262 "`MatrixWorkspace.dataE()` is deprecated in Mantid 7.0, use `MatrixWorkspace.mutableE()` instead. "
263 "For more information, see the Histogram data concept page: "
264 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
265 return mutableEData(self, index);
266}
267
274const Mantid::MantidVec &dataDxDeprecated(MatrixWorkspace &self, const size_t index) {
275 PyErr_Warn(PyExc_DeprecationWarning,
276 "`MatrixWorkspace.dataDx()` is deprecated in Mantid 7.0, use `MatrixWorkspace.mutableDx()` instead. "
277 "For more information, see the Histogram data concept page: "
278 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
279 return mutableDxData(self, index);
280}
281
288void setXFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
289 setSpectrumFromPyObject(self.mutableX(wsIndex), values);
290}
291
298void setYFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
299 setSpectrumFromPyObject(self.mutableY(wsIndex), values);
300}
301
308void setEFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
309 setSpectrumFromPyObject(self.mutableE(wsIndex), values);
310}
311
318void setDxFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
319 setSpectrumFromPyObject(self.mutableDx(wsIndex), values);
320}
321
328void setXDeprecated(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
329 PyErr_Warn(PyExc_DeprecationWarning,
330 "`MatrixWorkspace.setX()` is deprecated in Mantid 7.0, use `MatrixWorkspace.setSharedX()` instead. "
331 "For more information, see the Histogram data concept page: "
332 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
333 setXFromPyObject(self, wsIndex, values);
334}
335
342void setYDeprecated(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
343 PyErr_Warn(PyExc_DeprecationWarning,
344 "`MatrixWorkspace.setY()` is deprecated in Mantid 7.0, use `MatrixWorkspace.setSharedY()` instead. "
345 "For more information, see the Histogram data concept page: "
346 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
347 setYFromPyObject(self, wsIndex, values);
348}
349
356void setEDeprecated(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
357 PyErr_Warn(PyExc_DeprecationWarning,
358 "`MatrixWorkspace.setE()` is deprecated in Mantid 7.0, use `MatrixWorkspace.setSharedE()` instead. "
359 "For more information, see the Histogram data concept page: "
360 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
361 setEFromPyObject(self, wsIndex, values);
362}
363
370void setDxDeprecated(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
371 PyErr_Warn(PyExc_DeprecationWarning,
372 "`MatrixWorkspace.setDx()` is deprecated in Mantid 7.0, use `MatrixWorkspace.setSharedDx()` instead. "
373 "For more information, see the Histogram data concept page: "
374 "https://docs.mantidproject.org/nightly/concepts/HistogramData.html");
375 setDxFromPyObject(self, wsIndex, values);
376}
377
383std::vector<double> extractVectorFromPyObject(const boost::python::object &values) {
384 if (NDArray::check(values)) {
385 return NDArrayToVector<double>(values)();
386 }
387 return PySequenceToVector<double>(values)();
388}
389
396void setSharedXFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
397 self.setSharedX(wsIndex, make_cow<Mantid::HistogramData::HistogramX>(extractVectorFromPyObject(values)));
398}
399
406void setSharedYFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
407 self.setSharedY(wsIndex, make_cow<Mantid::HistogramData::HistogramY>(extractVectorFromPyObject(values)));
408}
409
416void setSharedEFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
417 self.setSharedE(wsIndex, make_cow<Mantid::HistogramData::HistogramE>(extractVectorFromPyObject(values)));
418}
419
426void setSharedDxFromPyObject(MatrixWorkspace &self, const size_t wsIndex, const boost::python::object &values) {
427 self.setSharedDx(wsIndex, make_cow<Mantid::HistogramData::HistogramDx>(extractVectorFromPyObject(values)));
428}
429
430std::vector<double> getIntegratedCountsForWorkspaceIndices(MatrixWorkspace &self,
431 const boost::python::object &workspaceIndices,
432 const size_t numberOfWorkspaces, const double minX,
433 const double maxX, const bool entireRange) {
434
435 std::vector<size_t> sWorkspaceIndices(numberOfWorkspaces);
436 if (NDArray::check(workspaceIndices)) {
437 NDArrayToVector<size_t> converter(workspaceIndices);
438 converter.copyTo(sWorkspaceIndices);
439 } else {
440 PySequenceToVector<size_t> converter(workspaceIndices);
441 converter.copyTo(sWorkspaceIndices);
442 }
443
444 return self.getIntegratedCountsForWorkspaceIndices(sWorkspaceIndices, minX, maxX, entireRange);
445}
446
453Mantid::API::Run &getSampleDetailsDeprecated(MatrixWorkspace &self) {
454 PyErr_Warn(PyExc_DeprecationWarning, "``getSampleDetails`` is deprecated, use ``getRun`` instead.");
455 return self.mutableRun();
456}
457
464std::size_t getNumberBinsDeprecated(MatrixWorkspace &self) {
465 PyErr_Warn(PyExc_DeprecationWarning, "``getNumberBins`` is deprecated, use ``blocksize`` instead.");
466 return self.blocksize();
467}
468
479std::size_t binIndexOfDeprecated(MatrixWorkspace &self, const double xValue, const std::size_t index = 0) {
480 PyErr_Warn(PyExc_DeprecationWarning, "``binIndexOf`` is deprecated, use ``yIndexOfX`` instead.");
481 return self.yIndexOfX(xValue, index);
482}
483
484GNU_DIAG_OFF("unused-local-typedef")
485// Ignore -Wconversion warnings coming from boost::python
486// Seen with GCC 7.1.1 and Boost 1.63.0
487GNU_DIAG_OFF("conversion")
488// Overloads for binIndexOfDeprecated function which has 1 optional argument
489BOOST_PYTHON_FUNCTION_OVERLOADS(binIndexOfDeprecatedOverloads, binIndexOfDeprecated, 2, 3)
490GNU_DIAG_ON("conversion")
491GNU_DIAG_ON("unused-local-typedef")
492
493
500std::vector<size_t> maskedBinsIndices(MatrixWorkspace &self, const int i) { return self.maskedBinsIndices(i); }
501
509void pythonReplaceAxis(MatrixWorkspace &self, const std::size_t &axisIndex, Axis *newAxis) {
510 self.replaceAxis(axisIndex, std::unique_ptr<Axis>(newAxis->clone(&self)));
511}
512
520object getSignalAtCoord(MatrixWorkspace &self, const NDArray &npCoords,
521 const Mantid::API::MDNormalization &normalization) {
522 if (npCoords.get_shape()[1] != 2) {
523 throw std::invalid_argument("MatrixWorkspace::getSignalAtCoord - Input "
524 "array must have shape (n, 2)");
525 }
526 // Create our output array
527 Py_intptr_t length = len(npCoords);
528 auto *signalValues = new Mantid::signal_t[length];
529
530 // Convert coords to a vector
531 std::vector<Mantid::coord_t> coords = NDArrayToVector<Mantid::coord_t>(npCoords)();
532
533 // Fill output array
534 for (int i = 0; i < length; ++i) {
535 std::array<Mantid::coord_t, 2> coord = {{coords[2 * i], coords[2 * i + 1]}};
536 signalValues[i] = self.getSignalAtCoord(coord.data(), normalization);
537 }
538 PyObject *npSignalArray =
539 Impl::wrapWithNDArray(signalValues, 1, &length, NumpyWrapMode::ReadOnly, OwnershipMode::Python);
540 return object(handle<>(npSignalArray));
541}
542
543boost::python::tuple findY(MatrixWorkspace &self, double value, tuple start) {
544 int64_t first = extract<int64_t>(start[0]);
545 int64_t second = extract<int64_t>(start[1]);
546 auto idx = self.findY(value, std::make_pair(first, second));
547 return make_tuple(idx.first, idx.second);
548}
549
558void applyBinEdgesFromAnotherWorkspace(MatrixWorkspace &self, const MatrixWorkspace &ws, const size_t getIndex,
559 const size_t setIndex) {
560 self.setBinEdges(setIndex, ws.binEdges(getIndex));
561}
562
571void applyPointsFromAnotherWorkspace(MatrixWorkspace &self, const MatrixWorkspace &ws, const size_t getIndex,
572 const size_t setIndex) {
573 self.setPoints(setIndex, ws.points(getIndex));
574}
575
576std::vector<size_t> getIndicesFromDetectorIDs(MatrixWorkspace &self, const boost::python::list &detIDs) {
578}
579
580boost::python::dict getDetectorIDToWorkspaceIndexMap(MatrixWorkspace &self, bool throwIfMultipleDets,
581 bool ignoreIfNoValidDets) {
582 const auto unorderedMap = self.getDetectorIDToWorkspaceIndexMap(throwIfMultipleDets, ignoreIfNoValidDets);
583 boost::python::dict pythonDict;
584 for (const auto &[key, value] : unorderedMap) {
585 pythonDict[key] = value;
586 }
587
588 return pythonDict;
589}
590
591} // namespace
592
597
598 class_<MatrixWorkspace, boost::python::bases<ExperimentInfo, IMDWorkspace>, boost::noncopyable>("MatrixWorkspace",
599 no_init)
600 //--------------------------------------- Meta information
601 //-----------------------------------------------------------------------
602 .def("isRaggedWorkspace", &MatrixWorkspace::isRaggedWorkspace, arg("self"),
603 "Returns true if the workspace is ragged (has differently sized "
604 "spectra).")
605 .def("blocksize", &MatrixWorkspace::blocksize, arg("self"), "Returns size of the Y data array")
606 .def("getNumberBins", &MatrixWorkspace::getNumberBins, (arg("self"), arg("index")),
607 "Returns the number of bins for a given histogram index.")
608 .def("getNumberBins", &getNumberBinsDeprecated, arg("self"),
609 "Returns size of the Y data array (deprecated, use "
610 ":class:`~mantid.api.MatrixWorkspace.blocksize` "
611 "instead)")
612 .def("getMaxNumberBins", &MatrixWorkspace::getMaxNumberBins, arg("self"),
613 "Returns the maximum number of bins in a workspace (works on ragged "
614 "data).")
615 .def("getNumberHistograms", &MatrixWorkspace::getNumberHistograms, arg("self"),
616 "Returns the number of spectra in the workspace")
617 .def("getPlotType", &MatrixWorkspace::getPlotType, arg("self"), "Returns the plot type of the workspace")
618 .def("getMarkerStyle", &MatrixWorkspace::getMarkerStyle, arg("self"), "Return the marker style for the workspace")
619 .def("getMarkerSize", &MatrixWorkspace::getMarkerSize, arg("self"), "Returns the marker size for the workspace")
620 .def("getSpectrumNumbers", &getSpectrumNumbers, arg("self"),
621 "Returns a list of all spectrum numbers in the workspace")
622 .def("yIndexOfX", &MatrixWorkspace::yIndexOfX,
623 MatrixWorkspace_yIndexOfXOverloads((arg("self"), arg("xvalue"), arg("workspaceIndex"), arg("tolerance")),
624 "Returns the y index which corresponds to the X Value provided. "
625 "The workspace_index [default=0] and tolerance [default=0.0] is "
626 "optional."))
627 .def("detectorTwoTheta", &MatrixWorkspace::detectorTwoTheta, (arg("self"), arg("det")),
628 "Returns the two theta value for a given detector")
629 .def("detectorSignedTwoTheta", &MatrixWorkspace::detectorSignedTwoTheta, (arg("self"), arg("det")),
630 "Returns the signed two theta value for given detector")
631 .def("getSpectrum", (ISpectrum & (MatrixWorkspace::*)(const size_t)) & MatrixWorkspace::getSpectrum,
632 (arg("self"), arg("workspaceIndex")), return_internal_reference<>(),
633 "Return the spectra at the given workspace index.")
634 .def("getIndexFromSpectrumNumber", &MatrixWorkspace::getIndexFromSpectrumNumber, (arg("self"), arg("spec_no")),
635 "Returns workspace index correspondent to the given spectrum "
636 "number. Throws if no such spectrum is present in the workspace")
637 .def("getIndicesFromDetectorIDs", &getIndicesFromDetectorIDs, (arg("self"), arg("detID_list")),
638 "Returns a list of workspace indices from the corrresponding "
639 "detector IDs.")
640 .def("getDetectorIDToWorkspaceIndexMap", &getDetectorIDToWorkspaceIndexMap,
641 (arg("self"), arg("throwIfMultipleDets"), arg("ignoreIfNoValidDets")),
642 " Return a map where the key is detector ID (pixel ID), and the value at that index = the corresponding "
643 "workspace index")
644 .def("getDetector", &MatrixWorkspace::getDetector, return_value_policy<RemoveConstSharedPtr>(),
645 (arg("self"), arg("workspaceIndex")),
646 "Return the :class:`~mantid.geometry.Detector` or "
647 ":class:`~mantid.geometry.DetectorGroup` that is linked to "
648 "the given workspace index")
649 .def("getRun", &MatrixWorkspace::mutableRun, arg("self"), return_internal_reference<>(),
650 "Return the :class:`~mantid.api.Run` object for this workspace")
651 .def("axes", &MatrixWorkspace::axes, arg("self"), "Returns the number of axes attached to the workspace")
652 .def("getAxis", &MatrixWorkspace::getAxis, (arg("self"), arg("axis_index")), return_internal_reference<>(),
653 "Get a pointer to a workspace axis")
654 .def("isHistogramData", &MatrixWorkspace::isHistogramData, arg("self"),
655 "Returns ``True`` if this is considered to be binned data.")
656 .def("isDistribution", (bool (MatrixWorkspace::*)() const) & MatrixWorkspace::isDistribution, arg("self"),
657 "Returns the status of the distribution flag")
658 .def("YUnit", &MatrixWorkspace::YUnit, arg("self"), return_value_policy<copy_const_reference>(),
659 "Returns the current Y unit for the data (Y axis) in the workspace")
660 .def("YUnitLabel", &MatrixWorkspace::YUnitLabel,
661 MatrixWorkspace_YUnitLabelOverloads((arg("self"), arg("useLatex")), "Returns the caption for the Y axis"))
662 .def("hasAnyMaskedBins", &MatrixWorkspace::hasAnyMaskedBins, (arg("self")),
663 "Returns true if any of the bins in this workspace are masked.")
664 .def("hasMaskedBins", &MatrixWorkspace::hasMaskedBins, (arg("self"), arg("workspaceIndex")),
665 "Returns true if this spectrum contains any masked bins")
666 .def("maskedBinsIndices", &maskedBinsIndices, (arg("self"), arg("workspaceIndex")),
667 "Returns all the masked bins' indices at the workspace index. "
668 ":class:`~mantid.api.MatrixWorkspace.hasMaskedBins` MUST be called "
669 "first to check if any bins are "
670 "masked, otherwise an exception will be thrown")
671 .def("findY", &findY, (arg("self"), arg("value"), arg("start") = make_tuple(0, 0)),
672 "Find first index in Y equal to value. Start may be specified to "
673 "begin at a specifc index. Returns tuple with the "
674 "histogram and bin indices.")
675 .def("getSampleDetails", &getSampleDetailsDeprecated, arg("self"), return_internal_reference<>(),
676 "Return the Run object for this workspace (deprecated, use "
677 ":class:`~mantid.api.MatrixWorkspace.getRun` "
678 "instead)")
679 .def("binIndexOf", &binIndexOfDeprecated,
680 binIndexOfDeprecatedOverloads((arg("self"), arg("xvalue"), arg("workspaceIndex")),
681 "Returns the index of the bin containing the given xvalue "
682 "(deprecated, use "
683 ":class:`~mantid.api.MatrixWorkspace.yIndexOfX` instead)"))
684
685 //--------------------------------------- Setters
686 //------------------------------------
687 .def("setPlotType", &MatrixWorkspace::setPlotType, (arg("self"), arg("newType")),
688 "Sets a new plot type for the workspace")
689 .def("setMarkerStyle", &MatrixWorkspace::setMarkerStyle, (arg("self"), arg("markerType")),
690 "Sets the marker type for the workspace")
691 .def("setMarkerSize", &MatrixWorkspace::setMarkerSize, (arg("self"), arg("markerSize")),
692 "Sets the size of the marker for the workspace")
693 .def("setYUnitLabel", &MatrixWorkspace::setYUnitLabel, (arg("self"), arg("newLabel")),
694 "Sets a new caption for the data (Y axis) in the workspace")
695 .def("setYUnit", &MatrixWorkspace::setYUnit, (arg("self"), arg("newUnit")),
696 "Sets a new unit for the data (Y axis) in the workspace")
697 .def("setDistribution", &MatrixWorkspace::setDistribution, (arg("self"), arg("newVal")),
698 "Set distribution flag. If True the workspace has been divided by "
699 "the bin-width.")
700 .def("replaceAxis", &pythonReplaceAxis, (arg("self"), arg("axisIndex"), arg("newAxis")),
701 "Replaces one of the workspace's axes with the new one provided. "
702 "The axis is cloned.")
703 .def("applyBinEdgesFromAnotherWorkspace", &applyBinEdgesFromAnotherWorkspace,
704 (arg("self"), arg("ws"), arg("getIndex"), arg("setIndex")),
705 "Sets the bin edges at setIndex to be the bin edges of ws at "
706 "getIndex.")
707 .def("applyPointsFromAnotherWorkspace", &applyPointsFromAnotherWorkspace,
708 (arg("self"), arg("ws"), arg("getIndex"), arg("setIndex")),
709 "Sets the points at setIndex to be the points of ws at getIndex.")
710
711 //--------------------------------------- Read spectrum data
712 //-------------------------
713 .def("x", &xData, return_readonly_numpy(), args("self", "workspaceIndex"),
714 "Creates a read-only numpy wrapper around the X data at the given index")
715 .def("y", &yData, return_readonly_numpy(), args("self", "workspaceIndex"),
716 "Creates a read-only numpy wrapper around the Y data at the given index")
717 .def("e", &eData, return_readonly_numpy(), args("self", "workspaceIndex"),
718 "Creates a read-only numpy wrapper around the E data at the given index")
719 .def("dx", &dxData, return_readonly_numpy(), args("self", "workspaceIndex"),
720 "Creates a read-only numpy wrapper around the Dx data at the given index. "
721 "If :class:`~mantid.api.MatrixWorkspace.hasDx` is False for this index, "
722 "the Dx data is first initialized to zeros.")
723 .def("readX", &readXDeprecated, (arg("self"), arg("workspaceIndex")), return_readonly_numpy(),
724 "Creates a read-only numpy wrapper "
725 "around the original X data at the "
726 "given index (deprecated, use "
727 ":class:`~mantid.api.MatrixWorkspace.x` instead)")
728 .def("readY", &readYDeprecated, return_readonly_numpy(), args("self", "workspaceIndex"),
729 "Creates a read-only numpy wrapper "
730 "around the original Y data at the "
731 "given index (deprecated, use "
732 ":class:`~mantid.api.MatrixWorkspace.y` instead)")
733 .def("readE", &readEDeprecated, return_readonly_numpy(), args("self", "workspaceIndex"),
734 "Creates a read-only numpy wrapper "
735 "around the original E data at the "
736 "given index (deprecated, use "
737 ":class:`~mantid.api.MatrixWorkspace.e` instead)")
738 .def("readDx", &readDxDeprecated, return_readonly_numpy(), args("self", "workspaceIndex"),
739 "Creates a read-only numpy wrapper "
740 "around the original Dx data at the "
741 "given index (deprecated, use "
742 ":class:`~mantid.api.MatrixWorkspace.dx` instead)")
743 .def("hasDx", &MatrixWorkspace::hasDx, args("self", "workspaceIndex"),
744 "Returns True if the spectrum uses the DX (X Error) array, else "
745 "False.")
746 //--------------------------------------- Write spectrum data
747 //------------------------
748 .def("mutableX", &mutableXData, return_readwrite_numpy(), args("self", "workspaceIndex"),
749 "Creates a writable numpy wrapper around the X data at the given index")
750 .def("mutableY", &mutableYData, return_readwrite_numpy(), args("self", "workspaceIndex"),
751 "Creates a writable numpy wrapper around the Y data at the given index. "
752 "Raises a RuntimeError for :class:`~mantid.api.IEventWorkspace`, since "
753 "non-const access to Y data is not possible for event data.")
754 .def("mutableE", &mutableEData, return_readwrite_numpy(), args("self", "workspaceIndex"),
755 "Creates a writable numpy wrapper around the E data at the given index. "
756 "Raises a RuntimeError for :class:`~mantid.api.IEventWorkspace`, since "
757 "non-const access to E data is not possible for event data.")
758 .def("mutableDx", &mutableDxData, return_readwrite_numpy(), args("self", "workspaceIndex"),
759 "Creates a writable numpy wrapper around the Dx data at the given index. "
760 "If :class:`~mantid.api.MatrixWorkspace.hasDx` is False for this index, "
761 "the Dx data is first initialized to zeros.")
762 .def("dataX", &dataXDeprecated, return_readwrite_numpy(), args("self", "workspaceIndex"),
763 "Creates a writable numpy wrapper around the original X data at the "
764 "given index (deprecated, use "
765 ":class:`~mantid.api.MatrixWorkspace.mutableX` instead)")
766 .def("dataY", &dataYDeprecated, return_readwrite_numpy(), args("self", "workspaceIndex"),
767 "Creates a writable numpy wrapper around the original Y data at the "
768 "given index (deprecated, use "
769 ":class:`~mantid.api.MatrixWorkspace.mutableY` instead)")
770 .def("dataE", &dataEDeprecated, return_readwrite_numpy(), args("self", "workspaceIndex"),
771 "Creates a writable numpy wrapper around the original E data at the "
772 "given index (deprecated, use "
773 ":class:`~mantid.api.MatrixWorkspace.mutableE` instead)")
774 .def("dataDx", &dataDxDeprecated, return_readwrite_numpy(), args("self", "workspaceIndex"),
775 "Creates a writable numpy wrapper around the original Dx data at "
776 "the given index (deprecated, use "
777 ":class:`~mantid.api.MatrixWorkspace.mutableDx` instead)")
778 .def("setX", &setXDeprecated, args("self", "workspaceIndex", "x"),
779 "Set X values from a python list or numpy array. It performs a "
780 "simple copy into the array (deprecated, use "
781 ":class:`~mantid.api.MatrixWorkspace.setSharedX` instead)")
782 .def("setY", &setYDeprecated, args("self", "workspaceIndex", "y"),
783 "Set Y values from a python list or numpy array. It performs a "
784 "simple copy into the array (deprecated, use "
785 ":class:`~mantid.api.MatrixWorkspace.setSharedY` instead)")
786 .def("setE", &setEDeprecated, args("self", "workspaceIndex", "e"),
787 "Set E values from a python list or numpy array. It performs a "
788 "simple copy into the array (deprecated, use "
789 ":class:`~mantid.api.MatrixWorkspace.setSharedE` instead)")
790 .def("setDx", &setDxDeprecated, args("self", "workspaceIndex", "dX"),
791 "Set Dx values from a python list or numpy array. It performs a "
792 "simple copy into the array (deprecated, use "
793 ":class:`~mantid.api.MatrixWorkspace.setSharedDx` instead)")
794 .def("setSharedX", &setSharedXFromPyObject, args("self", "workspaceIndex", "x"),
795 "Set the X data at the given index by building a new copy-on-write array "
796 "from a python list or numpy array and sharing it into the workspace.")
797 .def("setSharedY", &setSharedYFromPyObject, args("self", "workspaceIndex", "y"),
798 "Set the Y data at the given index by building a new copy-on-write array "
799 "from a python list or numpy array and sharing it into the workspace.")
800 .def("setSharedE", &setSharedEFromPyObject, args("self", "workspaceIndex", "e"),
801 "Set the E data at the given index by building a new copy-on-write array "
802 "from a python list or numpy array and sharing it into the workspace.")
803 .def("setSharedDx", &setSharedDxFromPyObject, args("self", "workspaceIndex", "dX"),
804 "Set the Dx data at the given index by building a new copy-on-write array "
805 "from a python list or numpy array and sharing it into the workspace.")
806
807 // --------------------------------------- Extract data
808 // ------------------------------
809 .def("extractX", Mantid::PythonInterface::cloneX, args("self"),
810 "Extracts (copies) the X data from the workspace into a 2D numpy array. "
811 "Note: This can fail for large workspaces as numpy will require a block of memory free that will fit all of "
812 "the data. "
813 "Note: This will fail for ragged workspaces.")
814 .def("extractY", Mantid::PythonInterface::cloneY, args("self"),
815 "Extracts (copies) the Y data from the workspace into a 2D numpy array. "
816 "Note: This can fail for large workspaces as numpy will require a block of memory free that will fit all of "
817 "the data. "
818 "Note: This will fail for ragged workspaces.")
819 .def("extractE", Mantid::PythonInterface::cloneE, args("self"),
820 "Extracts (copies) the E data from the workspace into a 2D numpy array. "
821 "Note: This can fail for large workspaces as numpy will require a block of memory free that will fit all of "
822 "the data. "
823 "Note: This will fail for ragged workspaces.")
824 .def("extractDx", Mantid::PythonInterface::cloneDx, args("self"),
825 "Extracts (copies) the Dx data from the workspace into a 2D numpy array. "
826 "Note: This can fail for large workspaces as numpy will require a block of memory free that will fit all of "
827 "the data. "
828 "Note: This will fail for ragged workspaces.")
829 .def("getSignalAtCoord", &getSignalAtCoord, args("self", "coords", "normalization"),
830 "Return signal for array of coordinates")
831 .def("getIntegratedCountsForWorkspaceIndices", &getIntegratedCountsForWorkspaceIndices,
832 args("self", "workspaceIndices", "numberOfWorkspaces", "minX", "maxX", "entireRange"),
833 "Return a vector with the integrated counts within the given range for the given workspace indices")
834 //-------------------------------------- Operators
835 //-----------------------------------
836 .def("equals", &Mantid::API::equals, args("self", "other", "tolerance"),
837 "Performs a comparison operation on two workspaces, using the "
838 "CompareWorkspaces algorithm")
839 //--------- monitor workspace --------------------------------------
840 .def("getMonitorWorkspace", &getMonitorWorkspace, args("self"),
841 "Return internal monitor workspace bound to current workspace.")
842 .def("setMonitorWorkspace", &setMonitorWorkspace, args("self", "MonitorWS"),
843 "Set specified workspace as monitor workspace for"
844 "current workspace. "
845 "Note: The workspace does not have to contain monitors though "
846 "some subsequent algorithms may expect it to be "
847 "monitor workspace later.")
848 .def("clearMonitorWorkspace", &clearMonitorWorkspace, args("self"),
849 "Forget about monitor workspace, attached to the current workspace")
850 .def("isCommonBins", &MatrixWorkspace::isCommonBins, "Returns true if the workspace has common X bins.")
851 .def("isCommonLogBins", &MatrixWorkspace::isCommonLogBins,
852 "Returns true if the workspace has common X bins with log spacing.");
853
855}
double value
The value of the point.
Definition FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
std::map< DeltaEMode::Type, std::string > index
void export_MatrixWorkspace()
Python exports of the Mantid::API::MatrixWorkspace class.
#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 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:
Definition ISpectrum.h:38
Base MatrixWorkspace Abstract Class.
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.
void setSharedDx(const size_t index, const Kernel::cow_ptr< HistogramData::HistogramDx > &dx) &
virtual ISpectrum & getSpectrum(const size_t index)=0
Return the underlying ISpectrum ptr at the given workspace index.
const HistogramData::HistogramE & e(const size_t index) const
double detectorSignedTwoTheta(const Geometry::IDetector &det) const
Returns the signed 2Theta scattering angle for a detector.
void setSharedX(const size_t index, const Kernel::cow_ptr< HistogramData::HistogramX > &x) &
bool hasAnyMaskedBins() const
Does this workspace contain any masked bins.
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.
HistogramData::HistogramX & mutableX(const size_t index) &
const HistogramData::HistogramDx & dx(const size_t index) const
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 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.
void setSharedE(const size_t index, const Kernel::cow_ptr< HistogramData::HistogramE > &e) &
const HistogramData::HistogramX & x(const size_t index) const
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.
void setSharedY(const size_t index, const Kernel::cow_ptr< HistogramData::HistogramY > &y) &
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.
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.
HistogramData::HistogramE & mutableE(const size_t index) &
HistogramData::HistogramDx & mutableDx(const size_t index) &
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.
HistogramData::HistogramY & mutableY(const size_t index) &
std::string YUnitLabel(bool useLatex=false) const
Returns a caption for the units of the data in the workspace.
void setYUnitLabel(const std::string &newLabel)
Sets a new caption for the data (Y axis) in the workspace.
const HistogramData::HistogramY & y(const size_t index) const
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.
Definition Run.h:36
Thin object wrapper around a numpy array.
Definition NDArray.h:31
static bool check(const boost::python::object &obj)
Check if a python object points to an array type object.
Definition NDArray.cpp:49
Py_intptr_t const * get_shape() const
Definition NDArray.cpp:61
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.
Definition IMDIterator.h:25
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.
Definition MDTypes.h:36
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition cow_ptr.h:172
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...