From 4fa1b6af73561279113db4f353cb865e67599b02 Mon Sep 17 00:00:00 2001
From: David Fairbrother <DavidFair@users.noreply.github.com>
Date: Tue, 3 Mar 2020 15:57:57 +0000
Subject: [PATCH] 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
---
 .../PythonInterface/mantid/api/CMakeLists.txt |  7 ++++---
 .../api/src/Exports/MatrixWorkspace.cpp       | 21 +++++++++++++++++++
 .../python/mantid/api/MatrixWorkspaceTest.py  |  7 +++++++
 docs/source/release/v5.1.0/framework.rst      |  2 ++
 4 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/Framework/PythonInterface/mantid/api/CMakeLists.txt b/Framework/PythonInterface/mantid/api/CMakeLists.txt
index bfc5d0d0f0a..74819ff88cf 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 230b93e0cd3..20d86586614 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 d334cdb21a2..ab10590e47a 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 ae2e09328dc..14f8ae2cd62 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
-- 
GitLab