Skip to content

Commit

Permalink
Refs #4399. Exported the worksapce validators to python
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Mar 14, 2012
1 parent 56be1ef commit db206fb
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 15 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ set ( TEST_PY_FILES
test/python/WorkspaceTest.py
test/python/WorkspaceGroupTest.py
test/python/WorkspacePropertiesTest.py
test/python/WorkspaceValidatorsTest.py
)

set ( PYTEST_HELPERS test/python/testhelpers.py )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef MANTID_PYTHONINTERFACE_TYPEDVALIDATOREXPORT_MACRO_H_
#define MANTID_PYTHONINTERFACE_TYPEDVALIDATOREXPORT_MACRO_H_
/*
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/TypedValidator.h"
#include <boost/python/class.hpp>

/**
* Defines an export for a validator of the given type
*/
#define EXPORT_TYPEDVALIDATOR(Type) \
boost::python::class_<Mantid::Kernel::TypedValidator<Type>,\
boost::python::bases<Mantid::Kernel::IValidator>,\
boost::noncopyable\
>("TypedValidator_"#Type, boost::python::no_init)\
.def("isValid", &Mantid::Kernel::TypedValidator<Type>::isValid, \
"Returns an empty string if the value is considered valid, otherwise a string defining the error is returned.")

#endif // MANTID_PYTHONINTERFACE_TYPEDVALIDATOREXPORT_MACRO_H_
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ set ( EXPORT_FILES
src/Exports/IPeaksWorkspace.cpp
src/Exports/BinaryOperations.cpp
src/Exports/WorkspaceGroup.cpp
src/Exports/WorkspaceValidators.cpp
src/Exports/InstrumentValidator.cpp
src/Exports/Axis.cpp
src/Exports/IPeak.cpp
src/Exports/BoxController.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "MantidAPI/WorkspaceValidators.h"
#include "MantidPythonInterface/kernel/TypedValidatorExportMacro.h"
#include <boost/python/class.hpp>

// This is typed on the ExperimentInfo class
void export_InstrumentValidator()
{
using namespace Mantid::API;
using namespace boost::python;
EXPORT_TYPEDVALIDATOR(ExperimentInfo_sptr);
class_<InstrumentValidator, bases<Mantid::Kernel::TypedValidator<ExperimentInfo_sptr>>,
boost::noncopyable
>("InstrumentValidator", init<>("Checks that the workspace has an instrument defined"))
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "MantidAPI/WorkspaceValidators.h" // They are all defined in one file so export them in one
#include "MantidPythonInterface/kernel/TypedValidatorExportMacro.h"
#include <boost/python/class.hpp>

using Mantid::Kernel::TypedValidator;
using namespace boost::python;

/// This is the base TypedValidator for most of the WorkspaceValidators
void export_MatrixWorkspaceValidator()
{
using Mantid::API::MatrixWorkspace_sptr;
using Mantid::API::MatrixWorkspaceValidator;
EXPORT_TYPEDVALIDATOR(MatrixWorkspace_sptr);
class_<MatrixWorkspaceValidator,
bases<TypedValidator<MatrixWorkspace_sptr>>,
boost::noncopyable>("MatrixWorkspaceValidator", no_init)
;
}
namespace
{
/// Export a validator derived from a MatrixWorkspaceValidator that has a no-arg constructor
#define EXPORT_WKSP_VALIDATOR_NO_ARG(ValidatorType, DocString) \
class_<ValidatorType, \
bases<MatrixWorkspaceValidator>, \
boost::noncopyable\
>(#ValidatorType, \
init<>(DocString))\
;
/// Export a validator derived from a MatrixWorkspaceValidator that has a single-arg constructor
#define EXPORT_WKSP_VALIDATOR_ARG(ValidatorType, ArgType, ArgName, DocString) \
class_<ValidatorType, \
bases<MatrixWorkspaceValidator>, \
boost::noncopyable\
>(#ValidatorType, \
init<ArgType>(arg(ArgName), DocString))\
;
/// Export a validator derived from a MatrixWorkspaceValidator that has a single-arg constructor
/// with a default argument
#define EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(ValidatorType, ArgType, ArgName, DefaultValue, DocString) \
class_<ValidatorType, \
bases<MatrixWorkspaceValidator>, \
boost::noncopyable\
>(#ValidatorType, \
init<ArgType>(arg(ArgName)=DefaultValue, DocString))\
;

}

void export_WorkspaceValidators()
{
using namespace Mantid::API;

EXPORT_WKSP_VALIDATOR_ARG(WorkspaceUnitValidator, std::string, "unit", "Checks the workspace has the given unit along the X-axis");
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(HistogramValidator, bool, "mustBeHistogram", true,
"If mustBeHistogram=True then the workspace must be a histogram otherwise it must be point data.");
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(RawCountValidator, bool, "mustNotBeDistribution", true,
"If mustNotBeDistribution=True then the workspace must not have been divided by the bin-width");
EXPORT_WKSP_VALIDATOR_NO_ARG(CommonBinsValidator, "A tentative check that the bins are common across the workspace");
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(SpectraAxisValidator, int, "axisNumber", 1,
"Checks whether the axis specified by axisNumber is a SpectraAxis");
EXPORT_WKSP_VALIDATOR_DEFAULT_ARG(NumericAxisValidator, int, "axisNumber", 1,
"Checks whether the axis specified by axisNumber is a NumericAxis");

}

Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ set ( INC_FILES
${HEADER_DIR}/kernel/PropertyWithValue.h
${HEADER_DIR}/kernel/Environment/CallStack.h
${HEADER_DIR}/kernel/StlExportDefinitions.h
${HEADER_DIR}/kernel/TypedValidatorExportMacro.h
${HEADER_DIR}/kernel/Environment/WrapperHelpers.h
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NotAnAlgorithm(object):
class AlgorithmManagerTest(unittest.TestCase):

def test_create_default_version(self):
alg = testhelpers.assert_raises_nothing(self, AlgorithmManager.Instance().create, "ConvertUnits")
alg = testhelpers.assertRaisesNothing(self, AlgorithmManager.Instance().create, "ConvertUnits")
# Tests
self.assertNotEqual(alg, None)
self.assertEquals(alg.name(), "ConvertUnits")
Expand Down Expand Up @@ -43,7 +43,7 @@ def test_pyalg_isinstance_of_Algorithm(self):
self.assertTrue(isinstance(alg, IAlgorithm))

def test_algorithm_registration_with_valid_object_succeeds(self):
testhelpers.assert_raises_nothing(self, registerAlgorithm, IsAnAlgorithm)
testhelpers.assertRaisesNothing(self, registerAlgorithm, IsAnAlgorithm)

def test_algorithm_registration_with_invalid_object_throws(self):
self.assertRaises(ValueError, registerAlgorithm, NotAnAlgorithm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
class BoundedValidatorTest(object):

def test_construction_does_not_raise_error_when_both_are_floats(self):
testhelpers.assert_raises_nothing(self, BoundedValidator, 1.0, 2.0)
testhelpers.assertRaisesNothing(self, BoundedValidator, 1.0, 2.0)

def test_construction_does_not_raise_error_when_both_are_ints(self):
testhelpers.assert_raises_nothing(self, BoundedValidator, 1, 20)
testhelpers.assertRaisesNothing(self, BoundedValidator, 1, 20)

def test_construction_raises_error_when_called_with_no_params(self):
self.assertRaises(TypeError, BoundedValidator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class FrameworkManagerTest(unittest.TestCase):

def test_clear_functions_do_not_throw(self):
# Test they don't throw for now
testhelpers.assert_raises_nothing(self, FrameworkManager.Instance().clear)
testhelpers.assert_raises_nothing(self, FrameworkManager.Instance().clearData)
testhelpers.assert_raises_nothing(self, FrameworkManager.Instance().clearAlgorithms)
testhelpers.assert_raises_nothing(self, FrameworkManager.Instance().clearInstruments)
testhelpers.assertRaisesNothing(self, FrameworkManager.Instance().clear)
testhelpers.assertRaisesNothing(self, FrameworkManager.Instance().clearData)
testhelpers.assertRaisesNothing(self, FrameworkManager.Instance().clearAlgorithms)
testhelpers.assertRaisesNothing(self, FrameworkManager.Instance().clearInstruments)

def _is_managed_test(self, alg, version):
self.assertTrue(alg.isInitialized())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_simple_values(self):
def test_setu_matrix_from_vectors(self):
def run_test(v1, v2):
cell = OrientedLattice()
testhelpers.assert_raises_nothing(self, cell.setUFromVectors, v1, v2)
testhelpers.assertRaisesNothing(self, cell.setUFromVectors, v1, v2)
rot = cell.getUB();
expected = np.array([(0,1.,0.), (0.,0.,1.), (1.,0.,0.)])
np.testing.assert_array_almost_equal(expected, rot, 8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def PyExec(self):
self.assertEquals(def_dir.direction, Direction.Input)
self.assertNotEquals("", def_dir.isValid())
self.assertRaises(ValueError, alg.setProperty, "NumPropWithDefaultDir", -10)
testhelpers.assert_raises_nothing(self, alg.setProperty, "NumPropWithDefaultDir", 11)
testhelpers.assertRaisesNothing(self, alg.setProperty, "NumPropWithDefaultDir", 11)

def test_specialized_property_declaration(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ def test_unmanaged_alg_is_descendent_of_PythonAlgorithm(self):
self.assertTrue(isinstance(alg, IAlgorithm))

def test_alg_with_default_attrs(self):
testhelpers.assert_raises_nothing(self,AlgorithmManager.Instance().createUnmanaged, "TestPyAlgDefaultAttrs")
testhelpers.assertRaisesNothing(self,AlgorithmManager.Instance().createUnmanaged, "TestPyAlgDefaultAttrs")
alg = AlgorithmManager.Instance().createUnmanaged("TestPyAlgDefaultAttrs")
testhelpers.assert_raises_nothing(self,alg.initialize)
testhelpers.assertRaisesNothing(self,alg.initialize)

self.assertEquals(alg.name(), "TestPyAlgDefaultAttrs")
self.assertEquals(alg.version(), 1)
self.assertEquals(alg.category(), "PythonAlgorithms")

def test_alg_with_overridden_attrs(self):
testhelpers.assert_raises_nothing(self,AlgorithmManager.Instance().createUnmanaged, "CoolAlgorithm")
testhelpers.assertRaisesNothing(self,AlgorithmManager.Instance().createUnmanaged, "CoolAlgorithm")
alg = AlgorithmManager.Instance().createUnmanaged("CoolAlgorithm")
self.assertEquals(alg.name(), "CoolAlgorithm")
self.assertEquals(alg.version(), 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_numpy_array_conversion(self):
gstar = np.array( [row0,row1,row2] )

u = UnitCell()
testhelpers.assert_raises_nothing(self, u.recalculateFromGstar, gstar)
testhelpers.assertRaisesNothing(self, u.recalculateFromGstar, gstar)
self._check_cell(u)

def _check_cell(self, cell):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
Test construction of the WorkspaceValidators
"""
import unittest
import testhelpers
from mantid.kernel import IValidator
from mantid import (WorkspaceUnitValidator, HistogramValidator,
RawCountValidator, CommonBinsValidator,
SpectraAxisValidator, NumericAxisValidator,
InstrumentValidator)

class WorkspaceValidatorsTest(unittest.TestCase):

def test_WorkspaceUnitValidator_construction(self):
"""
Test that the WorkspaceUnitValidator can be constructed
with a single string
"""
testhelpers.assertRaisesNothing(self, WorkspaceUnitValidator,"DeltaE")
self.assertRaises(Exception, WorkspaceUnitValidator)

def test_CommonBinsValidator_construction(self):
"""
Test that the CommonBinsValidator can be constructed
with no args
"""
testhelpers.assertRaisesNothing(self, CommonBinsValidator)

def test_HistogramValidator_construction(self):
"""
Test that the HistogramValidator can be constructed
with no args or a single bool
"""
testhelpers.assertRaisesNothing(self, HistogramValidator)
testhelpers.assertRaisesNothing(self, HistogramValidator, False)

def test_RawCountValidator_construction(self):
"""
Test that the HistogramValidator can be constructed
with no args or a single bool
"""
testhelpers.assertRaisesNothing(self, RawCountValidator)
testhelpers.assertRaisesNothing(self, RawCountValidator, False)

def test_SpectraAxisValidator_construction(self):
"""
Test that the SpectraAxis can be constructed
with no args or a single integer
"""
testhelpers.assertRaisesNothing(self, SpectraAxisValidator)
testhelpers.assertRaisesNothing(self, SpectraAxisValidator, 0)

def test_NumericAxisValidator_construction(self):
"""
Test that the NumericAxis can be constructed
with no args or a single integer
"""
testhelpers.assertRaisesNothing(self, NumericAxisValidator)
testhelpers.assertRaisesNothing(self, NumericAxisValidator, 0)

def test_InstrumentValidator_construction(self):
"""
Test that the InstrumentValidator can be constructed
with no args
"""
testhelpers.assertRaisesNothing(self, InstrumentValidator)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def run_algorithm(name, **kwargs):
alg.execute()
return alg

def assert_raises_nothing(testobj, callable, *args):
# Case difference is to be consistent with the unittest module
def assertRaisesNothing(testobj, callable, *args):
"""
unittest does not have an assertRaisesNothing. This
provides that functionality
Expand Down

0 comments on commit db206fb

Please sign in to comment.