Skip to content

Commit

Permalink
refs #10530. Add w_matrix to outgoing workspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Nov 19, 2014
1 parent 5333fd5 commit df6eb0b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
Expand Up @@ -6,6 +6,7 @@
#include "MantidGeometry/Instrument/Goniometer.h"
#include <boost/python/copy_const_reference.hpp>

#include "MantidPythonInterface/kernel/Registry/PropertyWithValueFactory.h"

using Mantid::API::Run;
using Mantid::Kernel::Property;
Expand All @@ -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<Property*> 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<double>(value)(), units, replace);
}
else if( PyInt_Check(value) )
{
self.addProperty(name, extract<long>(value)(), units, replace);
}
else if( PyString_Check(value) )
{
self.addProperty(name, extract<std::string>(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);
}

/**
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Expand Up @@ -263,7 +263,6 @@ def PyExec(self):

projection_labels = self.__make_labels(projection)


'''
Actually perform the binning operation
'''
Expand All @@ -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)
Expand Up @@ -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")
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit df6eb0b

Please sign in to comment.