From 52acb587fd9481e076f59c63148528c56191e1b6 Mon Sep 17 00:00:00 2001 From: Owen Arnold Date: Tue, 11 Nov 2014 15:14:33 +0000 Subject: [PATCH] refs #10530. Start new algorithm. Needed to expose a few more property types to get this started. All tested. --- .../PythonInterface/mantid/api/CMakeLists.txt | 2 ++ .../src/Exports/IMDEventWorkspaceProperty.cpp | 9 ++++++ .../mantid/api/src/Exports/IMDWorkspace.cpp | 8 ++++- .../api/src/Exports/IMDWorkspaceProperty.cpp | 9 ++++++ .../algorithms/WorkflowAlgorithms/CutMD.py | 32 +++++++++++++++++++ .../test/python/mantid/api/CMakeLists.txt | 1 + .../test/python/mantid/api/CutMDTest.py | 25 +++++++++++++++ .../mantid/api/WorkspacePropertiesTest.py | 11 +++++-- 8 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDEventWorkspaceProperty.cpp create mode 100644 Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspaceProperty.cpp create mode 100644 Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py create mode 100644 Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/mantid/api/CMakeLists.txt index 1ec123979841..dd9154d6873b 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/CMakeLists.txt @@ -34,9 +34,11 @@ set ( EXPORT_FILES src/Exports/ITableWorkspaceProperty.cpp src/Exports/MDGeometry.cpp src/Exports/IMDWorkspace.cpp + src/Exports/IMDWorkspaceProperty.cpp src/Exports/IMDHistoWorkspace.cpp src/Exports/IMDHistoWorkspaceProperty.cpp src/Exports/IMDEventWorkspace.cpp + src/Exports/IMDEventWorkspaceProperty.cpp src/Exports/MatrixWorkspace.cpp src/Exports/MatrixWorkspaceProperty.cpp src/Exports/IEventWorkspace.cpp diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDEventWorkspaceProperty.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDEventWorkspaceProperty.cpp new file mode 100644 index 000000000000..c0169c29fc04 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDEventWorkspaceProperty.cpp @@ -0,0 +1,9 @@ +#include "MantidPythonInterface/api/WorkspacePropertyExporter.h" +#include "MantidAPI/IMDEventWorkspace.h" + +void export_IMDEventWorkspaceProperty() +{ + using Mantid::API::IMDEventWorkspace; + using Mantid::PythonInterface::WorkspacePropertyExporter; + WorkspacePropertyExporter::define("IMDEventWorkspaceProperty"); +} diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspace.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspace.cpp index 40298057e32c..602eb47f6a7b 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspace.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspace.cpp @@ -16,11 +16,17 @@ void export_IMDWorkspace() .value("VolumeNormalization", Mantid::API::VolumeNormalization) .value("NumEventsNormalization", Mantid::API::NumEventsNormalization); + boost::python::enum_("SpecialCoordinateSystem") + .value("None", Mantid::API::None) + .value("QLab", Mantid::API::QLab) + .value("QSample", Mantid::API::QSample) + .value("HKL", Mantid::API::HKL); + // EventWorkspace class class_< IMDWorkspace, bases, boost::noncopyable >("IMDWorkspace", no_init) .def("getNPoints", &IMDWorkspace::getNPoints, args("self"), "Returns the total number of points within the workspace") .def("getNEvents", &IMDWorkspace::getNEvents, args("self"), "Returns the total number of events, contributed to the workspace") - ; + .def("getSpecialCoordinateSystem", &IMDWorkspace::getSpecialCoordinateSystem, args("self"), "Returns the special coordinate system of the workspace"); DataItemInterface(); } diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspaceProperty.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspaceProperty.cpp new file mode 100644 index 000000000000..23299662ed1e --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IMDWorkspaceProperty.cpp @@ -0,0 +1,9 @@ +#include "MantidPythonInterface/api/WorkspacePropertyExporter.h" +#include "MantidAPI/IMDWorkspace.h" + +void export_IMDWorkspaceProperty() +{ + using Mantid::API::IMDWorkspace; + using Mantid::PythonInterface::WorkspacePropertyExporter; + WorkspacePropertyExporter::define("IMDWorkspaceProperty"); +} diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py new file mode 100644 index 000000000000..57349227e2a6 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py @@ -0,0 +1,32 @@ +from mantid.kernel import * +from mantid.api import * +from mantid.simpleapi import * +import os.path + + +class CutMD(DataProcessorAlgorithm): + + def category(self): + return 'MDAlgorithms' + + def summary(self): + return 'Slices multidimensional workspaces using input projection information' + + def PyInit(self): + self.declareProperty(IMDEventWorkspaceProperty('InputWorkspace', '', direction=Direction.Input), + doc='MDWorkspace to slice') + + self.declareProperty(IMDWorkspaceProperty('OutputWorkspace', '', + direction=Direction.Output), + doc='Output cut workspace') + + def PyExec(self): + to_cut = self.getProperty("InputWorkspace").value + + coord_system = to_cut.getSpecialCoordinateSystem() + if not coord_system == SpecialCoordinateSystem.HKL: + raise ValueError("Input Workspace must be in reciprocal lattice dimensions (HKL)") + + self.setProperty("OutputWorkspace", to_cut) + +AlgorithmFactory.subscribe(CutMD) diff --git a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CMakeLists.txt index 6fac95545c88..2a35c8f661ce 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CMakeLists.txt @@ -10,6 +10,7 @@ set ( TEST_PY_FILES AnalysisDataServiceTest.py AxisTest.py CatalogManagerTest.py + CutMDTest.py DataProcessorAlgorithmTest.py DeprecatedAlgorithmCheckerTest.py ExperimentInfoTest.py diff --git a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py new file mode 100644 index 000000000000..eac802477c44 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py @@ -0,0 +1,25 @@ +import unittest +import testhelpers + +from mantid.simpleapi import CutMD, CreateMDWorkspace, SetSpecialCoordinates, CompareMDWorkspaces + + +class CutMDTest(unittest.TestCase): + + def test_exec_throws_if_not_a_hkl_workspace(self): + test_md = CreateMDWorkspace(Dimensions=3, Extents="-10,10,-10,10,-10,10", Names="A,B,C", Units="U,U,U") + SetSpecialCoordinates(InputWorkspace=test_md, SpecialCoordinates='Q (lab frame)') + self.assertRaises(RuntimeError, CutMD, InputWorkspace=test_md, OutputWorkspace="out_ws") + + def test_slice_to_original(self): + in_md = CreateMDWorkspace(Dimensions=3, Extents="-10,10,-10,10,-10,10", Names="A,B,C", Units="U,U,U") + SetSpecialCoordinates(InputWorkspace=in_md, SpecialCoordinates='HKL') + out_md = CutMD(in_md) + comparison = CompareMDWorkspaces(Workspace1=in_md, Workspace2=out_md) + self.assertTrue(comparison, "Input and output workspaces should be identical" ) + + def test_run_it(self): + pass + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/WorkspacePropertiesTest.py b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/WorkspacePropertiesTest.py index fc302d2482ec..f12bb112c364 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/WorkspacePropertiesTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/WorkspacePropertiesTest.py @@ -4,7 +4,8 @@ import unittest import testhelpers from mantid.api import (WorkspaceProperty, WorkspaceGroupProperty, MatrixWorkspaceProperty, - IEventWorkspaceProperty, ITableWorkspaceProperty, IMDHistoWorkspaceProperty, + IEventWorkspaceProperty, ITableWorkspaceProperty, + IMDWorkspaceProperty, IMDHistoWorkspaceProperty, IMDEventWorkspaceProperty, PropertyMode, LockMode) from mantid.kernel import Direction, Property @@ -54,8 +55,14 @@ def test_IEventWorkspaceProperty_can_be_instantiated(self): def test_ITableWorkspaceProperty_can_be_instantiated(self): self._do_test(ITableWorkspaceProperty) - def test_IHistoWorkspaceProperty_can_be_instantiated(self): + def test_IMDHistoWorkspaceProperty_can_be_instantiated(self): self._do_test(IMDHistoWorkspaceProperty) + + def test_IMDEventWorkspaceProperty_can_be_instantiated(self): + self._do_test(IMDEventWorkspaceProperty) + + def test_IMDWorkspaceProperty_can_be_instantiated(self): + self._do_test(IMDWorkspaceProperty) if __name__ == "__main__": unittest.main() \ No newline at end of file