diff --git a/Framework/PythonInterface/mantid/api/CMakeLists.txt b/Framework/PythonInterface/mantid/api/CMakeLists.txt index bfc5d0d0f0a48cf2de2a5052e4da865f78ac8b55..74819ff88cf7049b44d9e1e8d3f0163c09d04b85 100644 --- a/Framework/PythonInterface/mantid/api/CMakeLists.txt +++ b/Framework/PythonInterface/mantid/api/CMakeLists.txt @@ -133,10 +133,11 @@ target_link_libraries(PythonAPIModule PythonGeometryModule PythonKernelModule API - Types - Kernel - HistogramData Geometry + HistogramData + Indexing + Kernel + Types ${PYTHON_LIBRARIES} ${POCO_LIBRARIES} ${Boost_LIBRARIES}) diff --git a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp index 230b93e0cd3d249382b0f551c48fda5a282d8845..20d86586614d853cdfb7fba4ff70eb20671d78db 100644 --- a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp +++ b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp @@ -5,10 +5,12 @@ // & Institut Laue - Langevin // SPDX - License - Identifier: GPL - 3.0 + #include "MantidAPI/MatrixWorkspace.h" + #include "MantidAPI/Axis.h" #include "MantidAPI/Run.h" #include "MantidAPI/WorkspaceOpOverloads.h" #include "MantidGeometry/IDetector.h" +#include "MantidIndexing/IndexInfo.h" #include "MantidKernel/WarningSuppressions.h" #include "MantidPythonInterface/api/CloneMatrixWorkspace.h" @@ -23,6 +25,7 @@ #include <boost/python/class.hpp> #include <boost/python/copy_const_reference.hpp> #include <boost/python/implicit.hpp> +#include <boost/python/list.hpp> #include <boost/python/overloads.hpp> #include <boost/python/register_ptr_to_python.hpp> #include <boost/python/suite/indexing/map_indexing_suite.hpp> @@ -120,6 +123,22 @@ void clearMonitorWorkspace(MatrixWorkspace &self) { self.setMonitorWorkspace(monWS); } +/** + * @param self :: A reference to the calling object + * + * @return a list of associated spectrum numbers + */ +list getSpectrumNumbers(const MatrixWorkspace &self) { + const auto &spectrumNums = self.indexInfo().spectrumNumbers(); + list spectra; + + for (const auto index : spectrumNums) { + spectra.append(static_cast<int32_t>(index)); + } + + return spectra; +} + /** * Set the X values from an python array-style object * @param self :: A reference to the calling object @@ -283,6 +302,8 @@ void export_MatrixWorkspace() { "Returns size of the Y data array") .def("getNumberHistograms", &MatrixWorkspace::getNumberHistograms, arg("self"), "Returns the number of spectra in the workspace") + .def("getSpectrumNumbers", &getSpectrumNumbers, arg("self"), + "Returns a list of all spectrum numbers in the workspace") .def("yIndexOfX", &MatrixWorkspace::yIndexOfX, MatrixWorkspace_yIndexOfXOverloads( (arg("self"), arg("xvalue"), arg("workspaceIndex"), diff --git a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py index d334cdb21a2dc9d9ea8223a9914fd84ede773205..ab10590e47aeeac62ddf1bcad8162ec24437a5b6 100644 --- a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py +++ b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py @@ -90,6 +90,13 @@ class MatrixWorkspaceTest(unittest.TestCase): for i in range(len(ids)): self.assertEqual(expected[i], ids[i]) + def test_spectrum_numbers_returned(self): + num_vec = 11 + test_ws = WorkspaceFactory.create("Workspace2D", num_vec, 1, 1) + + spec_nums = test_ws.getSpectrumNumbers() + self.assertEqual([x for x in range(1, num_vec + 1)], spec_nums) + def test_detector_two_theta(self): det = self._test_ws.getDetector(1) two_theta = self._test_ws.detectorTwoTheta(det) diff --git a/docs/source/release/v5.1.0/framework.rst b/docs/source/release/v5.1.0/framework.rst index ae2e09328dc32820d41b44a451b196a6d395c685..14f8ae2cd626c38f16abf25c6fead0d0e428cfc4 100644 --- a/docs/source/release/v5.1.0/framework.rst +++ b/docs/source/release/v5.1.0/framework.rst @@ -20,5 +20,7 @@ Data Objects Python ------ +- A list of spectrum numbers can be got by calling getSpectrumNumbers on a + workspace. For example: spec_nums = ws.getSpectrumNumbers() :ref:`Release 5.1.0 <v5.1.0>` \ No newline at end of file