Skip to content
Snippets Groups Projects
Commit 4fa1b6af authored by David Fairbrother's avatar David Fairbrother
Browse files

Re #28181 Add Python API to get SpecNums

Adds a new API to getSpectrumNumbers from a Matrix workspace. This was
implemented as a indirect call to IndexInfo rather than exposing
IndexInfo, since there are few other things a user could want to call
parent 0e7db6d2
No related branches found
No related tags found
No related merge requests found
......@@ -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})
......
......@@ -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"),
......
......@@ -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)
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment