Skip to content

Commit

Permalink
Merge pull request #85 from mantidproject/feature/10530_pre_cut_md
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Dec 8, 2014
2 parents 773627a + cb1a3c1 commit c0e3414
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 29 deletions.
Expand Up @@ -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
Expand Down
@@ -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<IMDEventWorkspace>::define("IMDEventWorkspaceProperty");
}
Expand Up @@ -16,11 +16,17 @@ void export_IMDWorkspace()
.value("VolumeNormalization", Mantid::API::VolumeNormalization)
.value("NumEventsNormalization", Mantid::API::NumEventsNormalization);

boost::python::enum_<Mantid::API::SpecialCoordinateSystem>("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<Workspace, MDGeometry>, 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<IMDWorkspace>();
}
Expand Down
@@ -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<IMDWorkspace>::define("IMDWorkspaceProperty");
}
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 @@ -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 @@ -19,7 +19,7 @@ def test_meta_information_is_correct(self):

column_names = self._test_ws.getColumnNames()
self.assertEquals(len(column_names), 19)
self.assertEquals(type(column_names), std_vector_str)
self.assertEquals(type(column_names), list)

def test_cell_access_returns_variables_as_native_python_types(self):
self.assertAlmostEquals(self._test_ws.cell('r_gd_prtn_chrg',0), 10.040912628173828, 15)
Expand Down
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Up @@ -87,7 +87,7 @@ Output:

.. testoutput:: CreateLogPropertyTable

Column names are: ['run_title','ImportantParameter']
Column names are: ['run_title', 'ImportantParameter']
The values of the ImportantParameter are: ['1', '2', '3']

.. categories::
Expand Up @@ -82,7 +82,7 @@ Usage
.. testoutput:: ExPreprocessDetectoresToMD

The resulting table has the following columns:
['DetDirections','L2','TwoTheta','Azimuthal','DetectorID','detIDMap','spec2detMap','detMask','eFixed']
['DetDirections', 'L2', 'TwoTheta', 'Azimuthal', 'DetectorID', 'detIDMap', 'spec2detMap', 'detMask', 'eFixed']
The number of rows in the workspace is : 918
The polar angle for detector N 10 is 0.314159 rad
The table workspace logs (properties) are currently not availible from python
Expand Down

0 comments on commit c0e3414

Please sign in to comment.