diff --git a/Framework/API/inc/MantidAPI/MatrixWorkspace.h b/Framework/API/inc/MantidAPI/MatrixWorkspace.h index e918da25a367..adcee4b15cd0 100644 --- a/Framework/API/inc/MantidAPI/MatrixWorkspace.h +++ b/Framework/API/inc/MantidAPI/MatrixWorkspace.h @@ -12,6 +12,7 @@ #include "MantidAPI/ISpectrum.h" #include "MantidAPI/MatrixWorkspace_fwd.h" #include "MantidKernel/EmptyValues.h" +#include "MantidKernel/EnumeratedString.h" #include #include @@ -32,6 +33,13 @@ namespace Geometry { class ParameterMap; } +namespace { +/// The allowed plot types for MatrixWorkspace +enum class PlotTypeEnum { PLOT, SCATTER, HISTOGRAM, ERRORBAR, enum_count }; +const std::vector plotTypeNames{"plot", "scatter", "histogram", "errorbar"}; +typedef Kernel::EnumeratedString PLOTTYPE; +} // namespace + namespace API { class Axis; class SpectrumDetectorMapping; @@ -134,6 +142,11 @@ class MANTID_API_DLL MatrixWorkspace : public IMDWorkspace, public ExperimentInf /// Gets MatrixWorkspace title (same as Run object run_title property) const std::string getTitle() const override; + /// Sets MatrixWorkspace plot_type + void setPlotType(const std::string &); + /// Gets MatrixWorkspace plot_type + const PLOTTYPE getPlotType() const; + virtual Types::Core::DateAndTime getFirstPulseTime() const; Types::Core::DateAndTime getLastPulseTime() const; @@ -334,7 +347,7 @@ class MANTID_API_DLL MatrixWorkspace : public IMDWorkspace, public ExperimentInf /// Returns true if the workspace has common, integer X bins virtual bool isIntegerBins() const; - std::string YUnit() const; + const std::string &YUnit() const; void setYUnit(const std::string &newUnit); std::string YUnitLabel(bool useLatex = false, bool plotAsDistribution = false) const; void setYUnitLabel(const std::string &newLabel); @@ -496,6 +509,9 @@ class MANTID_API_DLL MatrixWorkspace : public IMDWorkspace, public ExperimentInf /// A text label for use when plotting spectra std::string m_YUnitLabel; + /// The plot style for the MatrixWorkspace + PLOTTYPE m_plotType = std::string("plot"); + /// Flag indicating if the common bins flag is in a valid state mutable std::atomic m_isCommonBinsFlagValid{false}; /// Flag indicating whether the data has common bins diff --git a/Framework/API/src/MatrixWorkspace.cpp b/Framework/API/src/MatrixWorkspace.cpp index f249209e217b..e3e5dfe36a15 100644 --- a/Framework/API/src/MatrixWorkspace.cpp +++ b/Framework/API/src/MatrixWorkspace.cpp @@ -22,6 +22,7 @@ #include "MantidGeometry/MDGeometry/MDFrame.h" #include "MantidIndexing/GlobalSpectrumIndex.h" #include "MantidIndexing/IndexInfo.h" +#include "MantidKernel/ListValidator.h" #include "MantidKernel/MDUnit.h" #include "MantidKernel/MultiThreaded.h" #include "MantidKernel/Strings.h" @@ -38,6 +39,7 @@ #include #include +using Mantid::Kernel::StringListValidator; using Mantid::Kernel::TimeSeriesProperty; using Mantid::Types::Core::DateAndTime; @@ -321,6 +323,24 @@ const std::string MatrixWorkspace::getTitle() const { return Workspace::getTitle(); } +/** Set the plot type of the workspace + * + * @param t :: The plot type. Must be an accepted type + */ +void MatrixWorkspace::setPlotType(const std::string &t) { + + StringListValidator v(plotTypeNames); + + if (v.isValid(t) == "") + this->m_plotType = t; +} + +/** Get the plot type + * + * @return The plot type + */ +const PLOTTYPE MatrixWorkspace::getPlotType() const { return this->m_plotType; } + void MatrixWorkspace::updateSpectraUsing(const SpectrumDetectorMapping &map) { for (size_t j = 0; j < getNumberHistograms(); ++j) { auto &spec = getSpectrum(j); @@ -930,7 +950,7 @@ bool MatrixWorkspace::isCommonLogBins() const { size_t MatrixWorkspace::numberOfAxis() const { return m_axes.size(); } /// Returns the units of the data in the workspace -std::string MatrixWorkspace::YUnit() const { return m_YUnit; } +const std::string &MatrixWorkspace::YUnit() const { return m_YUnit; } /// Sets a new unit for the data (Y axis) in the workspace void MatrixWorkspace::setYUnit(const std::string &newUnit) { m_YUnit = newUnit; } diff --git a/Framework/API/test/MatrixWorkspaceTest.h b/Framework/API/test/MatrixWorkspaceTest.h index f53882f87154..b33a14c3e3a1 100644 --- a/Framework/API/test/MatrixWorkspaceTest.h +++ b/Framework/API/test/MatrixWorkspaceTest.h @@ -402,6 +402,19 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite { ws->setTitle(""); } + void testGetSetPlotType() { + // test default + TS_ASSERT_EQUALS(ws->getPlotType(), "plot"); + + // test invalid is rejected + ws->setPlotType("invalid"); + TS_ASSERT_EQUALS(ws->getPlotType(), "plot"); + + // test valid is accepted + ws->setPlotType("scatter"); + TS_ASSERT_EQUALS(ws->getPlotType(), "scatter"); + } + void testGetSetComment() { TS_ASSERT_EQUALS(ws->getComment(), ""); ws->setComment("commenting"); diff --git a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp index 208e29c07d25..3236793e29b5 100644 --- a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp +++ b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp @@ -332,6 +332,7 @@ void export_MatrixWorkspace() { "data).") .def("getNumberHistograms", &MatrixWorkspace::getNumberHistograms, arg("self"), "Returns the number of spectra in the workspace") + .def("getPlotType", &MatrixWorkspace::getPlotType, arg("self"), "Returns the plot type of the workspace") .def("getSpectrumNumbers", &getSpectrumNumbers, arg("self"), "Returns a list of all spectrum numbers in the workspace") .def("yIndexOfX", &MatrixWorkspace::yIndexOfX, @@ -396,6 +397,8 @@ void export_MatrixWorkspace() { //--------------------------------------- Setters //------------------------------------ + .def("setPlotType", &MatrixWorkspace::setPlotType, (arg("self"), arg("newType")), + "Sets a new plot type for the workspace") .def("setYUnitLabel", &MatrixWorkspace::setYUnitLabel, (arg("self"), arg("newLabel")), "Sets a new caption for the data (Y axis) in the workspace") .def("setYUnit", &MatrixWorkspace::setYUnit, (arg("self"), arg("newUnit")), diff --git a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py index c48b683acc3c..e8ff4a55877e 100644 --- a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py +++ b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py @@ -40,6 +40,7 @@ def test_meta_information(self): self.assertEqual(self._test_ws.id(), "Workspace2D") self.assertEqual(self._test_ws.name(), "") self.assertEqual(self._test_ws.getTitle(), "Test histogram") + self.assertEqual(self._test_ws.getPlotType(), "plot") self.assertEqual(self._test_ws.getComment(), "") self.assertEqual(self._test_ws.isDirty(), False) self.assertGreater(self._test_ws.getMemorySize(), 0.0) @@ -395,6 +396,21 @@ def test_setTitleAndComment(self): self.assertEqual(comment, ws1.getComment()) AnalysisDataService.remove(ws1.name()) + def test_setPlotType(self): + run_algorithm("CreateWorkspace", OutputWorkspace="ws1", DataX=[1.0, 2.0, 3.0], DataY=[2.0, 3.0], DataE=[2.0, 3.0], UnitX="TOF") + ws1 = AnalysisDataService["ws1"] + + # test default + self.assertEqual("plot", ws1.getPlotType()) + + # test invalid doesn't take + ws1.setPlotType("invalid") + self.assertEqual("plot", ws1.getPlotType()) + + # test valid takes + ws1.setPlotType("scatter") + self.assertEqual("scatter", ws1.getPlotType()) + def test_setGetMonitorWS(self): run_algorithm("CreateWorkspace", OutputWorkspace="ws1", DataX=[1.0, 2.0, 3.0], DataY=[2.0, 3.0], DataE=[2.0, 3.0], UnitX="TOF") run_algorithm("CreateWorkspace", OutputWorkspace="ws_mon", DataX=[1.0, 2.0, 3.0], DataY=[2.0, 3.0], DataE=[2.0, 3.0], UnitX="TOF") diff --git a/buildconfig/CMake/CppCheck_Suppressions.txt.in b/buildconfig/CMake/CppCheck_Suppressions.txt.in index 52481dbe1bb5..e4fba2b9b4ec 100644 --- a/buildconfig/CMake/CppCheck_Suppressions.txt.in +++ b/buildconfig/CMake/CppCheck_Suppressions.txt.in @@ -431,7 +431,6 @@ constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/FindClusterFace constParameterPointer:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/FindClusterFaces.cpp:117 constParameterReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/IndexSXPeaks.cpp:88 constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/IndexSXPeaks.cpp:181 -returnByReference:${CMAKE_SOURCE_DIR}/Framework/API/inc/MantidAPI/MatrixWorkspace.h:337 missingOverride:${CMAKE_SOURCE_DIR}/Framework/Crystal/inc/MantidCrystal/PeaksIntersection.h:33 constParameterReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/PeakStatisticsTools.cpp:209 constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Crystal/src/PeakHKLErrors.cpp:73