From df6eb0b9d0c786b3d2be442501305f8773920235 Mon Sep 17 00:00:00 2001 From: Owen Arnold Date: Wed, 19 Nov 2014 08:48:33 +0000 Subject: [PATCH] refs #10530. Add w_matrix to outgoing workspace. --- .../mantid/api/src/Exports/Run.cpp | 31 ++++++------------- .../algorithms/WorkflowAlgorithms/CutMD.py | 11 +++++-- .../test/python/mantid/api/CutMDTest.py | 6 +++- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Run.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Run.cpp index cc323a038306..e83d7784d163 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Run.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Run.cpp @@ -6,6 +6,7 @@ #include "MantidGeometry/Instrument/Goniometer.h" #include +#include "MantidPythonInterface/kernel/Registry/PropertyWithValueFactory.h" using Mantid::API::Run; using Mantid::Kernel::Property; @@ -23,32 +24,20 @@ namespace * @param units A string representing a unit * @param replace If true, replace an existing property with this one else raise an error */ - void addPropertyWithUnit(Run & self, const std::string & name, PyObject *value, const std::string & units, bool replace) + void addPropertyWithUnit(Run & self, const std::string & name, const bpl::object & value, const std::string & units, bool replace) { extract extractor(value); if(extractor.check()) { Property *prop = extractor(); self.addProperty(prop->clone(), replace); // Clone the property as Python owns the one that is passed in + return; } - else if( PyFloat_Check(value) ) - { - self.addProperty(name, extract(value)(), units, replace); - } - else if( PyInt_Check(value) ) - { - self.addProperty(name, extract(value)(), units, replace); - } - else if( PyString_Check(value) ) - { - self.addProperty(name, extract(value)(), units, replace); - } - else - { - std::ostringstream msg; - msg << "Run::addProperty - Unknown value type given: " << value->ob_type->tp_name; - throw std::invalid_argument(msg.str()); - } + + // Use the factory + auto property = Mantid::PythonInterface::Registry::PropertyWithValueFactory::create(name, value, Mantid::Kernel::Direction::Input); + property->setUnits(units); + self.addProperty(property, replace); } /** @@ -58,7 +47,7 @@ namespace * @param value The value of the property * @param replace If true, replace an existing property with this one else raise an error */ - void addProperty(Run & self, const std::string & name, PyObject *value, bool replace) + void addProperty(Run & self, const std::string & name, const bpl::object & value, bool replace) { addPropertyWithUnit(self, name, value, "", replace); } @@ -69,7 +58,7 @@ namespace * @param name The name of the new property * @param value The value of the property */ - void addOrReplaceProperty(Run & self, const std::string & name, PyObject *value) + void addOrReplaceProperty(Run & self, const std::string & name, const bpl::object & value) { addProperty(self, name, value, true); } diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py index 1da69c39512b..ba4f97757229 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py @@ -263,7 +263,6 @@ def PyExec(self): projection_labels = self.__make_labels(projection) - ''' Actually perform the binning operation ''' @@ -290,9 +289,15 @@ def PyExec(self): cut_alg.execute() - # TODO. The projection matrix should be written to the output workspace at this point. - slice = cut_alg.getProperty("OutputWorkspace").value + + # Attach the w-matrix (projection matrix) + if slice.getNumExperimentInfo() > 0: + u, v, w = projection + w_matrix = np.array([u, v, w]).flatten().tolist() + info = slice.getExperimentInfo(0) + info.run().addProperty("W_MATRIX", w_matrix, True) + self.setProperty("OutputWorkspace", slice) AlgorithmFactory.subscribe(CutMD) diff --git a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py index ad313d5abd44..dc9777cb0fc7 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py @@ -67,7 +67,6 @@ def test_truncate_extents(self): self.assertEqual(1, out_md.getDimension(0).getNBins(), "Step is beyond range. Should just be integrated") self.assertEqual(1, out_md.getDimension(1).getNBins(), "Step is beyond range. Should just be integrated") - def test_wrong_projection_workspace_format_wrong_column_numbers(self): projection = CreateEmptyTableWorkspace() projection.addColumn("double", "u") @@ -159,6 +158,11 @@ def test_non_orthogonal_slice(self): self.assertTrue(isinstance(out_md, IMDHistoWorkspace), "Expect that the output was an IMDHistoWorkspace given the NoPix flag.") + run = out_md.getExperimentInfo(0).run() + w_matrix = run.getLogData("W_MATRIX").value + w_matrix = list(w_matrix) + self.assertEquals([1,1,0,-1,1,0,0,0,1], w_matrix, "W-matrix should have been set, but should be an identity matrix") + def test_orthogonal_slice_with_cropping(self): # We create a fake workspace and check to see that using bin inputs for cropping works to_cut = CreateMDWorkspace(Dimensions=3, Extents=[-1,1,-1,1,-1,1], Names='H,K,L', Units='U,U,U')