From 6e9019abbaaddd22748165c73dc5ab6a9186c538 Mon Sep 17 00:00:00 2001 From: Owen Arnold Date: Fri, 14 Nov 2014 12:22:59 +0000 Subject: [PATCH] refs #10530. Add more tests. Also needed to expose columnnames properly via ITableWorskpace --- .../api/src/Exports/ITableWorkspace.cpp | 7 +++- .../algorithms/WorkflowAlgorithms/CutMD.py | 14 ++++--- .../test/python/mantid/api/CutMDTest.py | 38 ++++++++++++++++++- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/ITableWorkspace.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/ITableWorkspace.cpp index 30b9fda82942..c285c19b1905 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/ITableWorkspace.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/ITableWorkspace.cpp @@ -6,6 +6,7 @@ #include "MantidPythonInterface/kernel/Converters/PySequenceToVector.h" #include "MantidPythonInterface/kernel/Converters/CloneToNumpy.h" #include "MantidPythonInterface/kernel/Registry/DataItemInterface.h" +#include "MantidPythonInterface/kernel/Policies/VectorToNumpy.h" #include #include @@ -339,6 +340,8 @@ namespace void export_ITableWorkspace() { + using Mantid::PythonInterface::Policies::VectorToNumpy; + std::string iTableWorkspace_docstring = "Most of the information from a table workspace is returned "; iTableWorkspace_docstring += "as native copies. All of the column accessors return lists while the "; iTableWorkspace_docstring += "rows return dicts. This object does support the idom 'for row in "; @@ -364,9 +367,9 @@ void export_ITableWorkspace() .def("__len__", &ITableWorkspace::rowCount, "Returns the number of rows within the workspace") - .def("getColumnNames",&ITableWorkspace::getColumnNames, "Return a list of the column names") + .def("getColumnNames",&ITableWorkspace::getColumnNames, boost::python::return_value_policy(),"Return a list of the column names") - .def("keys", &ITableWorkspace::getColumnNames, "Return a list of the column names") + .def("keys", &ITableWorkspace::getColumnNames, boost::python::return_value_policy(), "Return a list of the column names") .def("column", &column, "Return all values of a specific column as a list") diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py index 1f1beaf79832..a6398425c6ce 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CutMD.py @@ -105,9 +105,13 @@ def __uvw_from_projection_table(self, projection_table): if not isinstance(projection_table, ITableWorkspace): I = np.identity(3) return (I[0, :], I[1, :], I[2, :]) + column_names = projection_table.getColumnNames() u = np.array(projection_table.column('u')) v = np.array(projection_table.column('v')) - w = np.cross(v,u) + if not 'w' in column_names: + w = np.cross(v,u) + else: + w = np.array(projection_table.column('w')) return (u, v, w) def __make_labels(self, projection): @@ -149,11 +153,11 @@ def PyExec(self): projection_table = self.getProperty("Projection").value if isinstance(projection_table, ITableWorkspace): column_names = set(projection_table.getColumnNames()) - logger.warning(str(column_names)) + logger.warning(str(column_names)) if not column_names == set(['u', 'v', 'type']): - if not column_names == set(['u', 'v', 'offsets', 'type']): - if not column_names == set(['u', 'v', 'w', 'offsets', 'type']): - raise ValueError("Projection table schema is wrong") + if not column_names == set(['u', 'v', 'offsets', 'type']): + if not column_names == set(['u', 'v', 'w', 'offsets', 'type']): + raise ValueError("Projection table schema is wrong! Column names received: " + str(column_names) ) if projection_table.rowCount() != 3: raise ValueError("Projection table expects 3 rows") 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 3a13fddddc08..49640a272178 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CutMDTest.py @@ -57,7 +57,7 @@ def test_wrong_table_workspace_format_wrong_row_numbers(self): # Incorrect number of rows i.e. zero in this case as none added. self.assertRaises(RuntimeError, CutMD, InputWorkspace=self.__in_md, Projection=projection, OutputWorkspace="out_ws", P1Bin=[0.1], P2Bin=[0.1], P3Bin=[0.1]) - def test_non_orthogonal_slice_with_scaling(self): + def test_orthogonal_slice_with_scaling(self): # We create a fake workspace around and check to see that the extents get scaled with the new coordinate system when sliced to_cut = CreateMDWorkspace(Dimensions=3, Extents=[-1,1,-1,1,-1,1], Names='H,K,L', Units='U,U,U') @@ -91,6 +91,42 @@ def test_non_orthogonal_slice_with_scaling(self): self.assertEquals("['2.00zeta', 0, 0]", out_md.getDimension(0).getName() ) self.assertEquals("[0, '2.00eta', 0]", out_md.getDimension(1).getName() ) self.assertEquals("[0, 0, '-4.00xi']", out_md.getDimension(2).getName() ) + + + def test_non_orthogonal_slice_with_scaling(self): + # We create a fake workspace around and check to see that the extents get scaled with the new coordinate system when sliced + to_cut = CreateMDWorkspace(Dimensions=3, Extents=[-1,1,-1,1,-1,1], Names='H,K,L', Units='U,U,U') + + SetSpecialCoordinates(InputWorkspace=to_cut, SpecialCoordinates='HKL') + + projection = CreateEmptyTableWorkspace() + # Correct number of columns, and names + projection.addColumn("double", "u") + projection.addColumn("double", "v") + projection.addColumn("double", "w") + projection.addColumn("double", "offsets") + projection.addColumn("str", "type") + projection.addRow([1,-1, 0, 0, "aaa"]) + projection.addRow([1, 1, 0, 0, "aaa"]) + projection.addRow([0, 0, 1, 0, "aaa"]) + + out_md = CutMD(to_cut, Projection=projection, P1Bin=[0.1], P2Bin=[0.1], P3Bin=[0.1]) + + ''' + Here we check that the corners in HKL end up in the expected positions when transformed into the new scaled basis + provided by the W transform (projection table) + ''' + self.assertEquals(-1, out_md.getDimension(0).getMinimum()) + self.assertEquals(1, out_md.getDimension(0).getMaximum()) + self.assertEquals(-1, out_md.getDimension(1).getMinimum()) + self.assertEquals(1, out_md.getDimension(1).getMaximum()) + self.assertEquals(-1, out_md.getDimension(2).getMinimum()) + self.assertEquals(1, out_md.getDimension(2).getMaximum()) + self.assertEquals("['zeta', 'zeta', 0]", out_md.getDimension(0).getName() ) + self.assertEquals("['-eta', 'eta', 0]", out_md.getDimension(1).getName() ) + self.assertEquals("[0, 0, 'xi']", out_md.getDimension(2).getName() ) + + if __name__ == '__main__':