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