Skip to content

Commit

Permalink
refs #10530. Add more tests.
Browse files Browse the repository at this point in the history
Also needed to expose columnnames properly via ITableWorskpace
  • Loading branch information
OwenArnold committed Nov 14, 2014
1 parent 7dc0549 commit 6e9019a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
Expand Up @@ -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 <boost/python/class.hpp>
#include <boost/python/list.hpp>
Expand Down Expand Up @@ -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 ";
Expand All @@ -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<VectorToNumpy>(),"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<VectorToNumpy>(), "Return a list of the column names")

.def("column", &column, "Return all values of a specific column as a list")

Expand Down
Expand Up @@ -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):
Expand Down Expand Up @@ -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")

Expand Down
Expand Up @@ -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')

Expand Down Expand Up @@ -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__':
Expand Down

0 comments on commit 6e9019a

Please sign in to comment.