Skip to content

Commit

Permalink
Refs #4083 exposed boolean and comparison operations
Browse files Browse the repository at this point in the history
for MDHistoWorkspaces to python. Tested in VanillaPythonTest.

È
git commit -a -m Refs
  • Loading branch information
Janik Zikovsky committed Nov 10, 2011
1 parent a6d9b0d commit f05199f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 4 deletions.
46 changes: 45 additions & 1 deletion Code/Mantid/Framework/API/src/IMDHistoWorkspace.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidKernel/System.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/IPropertyManager.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;
Expand Down Expand Up @@ -27,4 +29,46 @@ namespace API


} // namespace Mantid
} // namespace API
} // namespace API


namespace Mantid
{
namespace Kernel
{
/** In order to be able to cast PropertyWithValue classes correctly a definition for the PropertyWithValue<IMDEventWorkspace> is required */
template<> MANTID_API_DLL
Mantid::API::IMDHistoWorkspace_sptr IPropertyManager::getValue<Mantid::API::IMDHistoWorkspace_sptr>(const std::string &name) const
{
PropertyWithValue<Mantid::API::IMDHistoWorkspace_sptr>* prop =
dynamic_cast<PropertyWithValue<Mantid::API::IMDHistoWorkspace_sptr>*>(getPointerToProperty(name));
if (prop)
{
return *prop;
}
else
{
std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected IMDHistoWorkspace.";
throw std::runtime_error(message);
}
}

/** In order to be able to cast PropertyWithValue classes correctly a definition for the PropertyWithValue<IMDWorkspace_const_sptr> is required */
template<> MANTID_API_DLL
Mantid::API::IMDHistoWorkspace_const_sptr IPropertyManager::getValue<Mantid::API::IMDHistoWorkspace_const_sptr>(const std::string &name) const
{
PropertyWithValue<Mantid::API::IMDHistoWorkspace_sptr>* prop =
dynamic_cast<PropertyWithValue<Mantid::API::IMDHistoWorkspace_sptr>*>(getPointerToProperty(name));
if (prop)
{
return prop->operator()();
}
else
{
std::string message = "Attempt to assign property "+ name +" to incorrect type. Expected const IMDHistoWorkspace.";
throw std::runtime_error(message);
}
}

} // namespace Kernel
} // namespace Mantid
8 changes: 8 additions & 0 deletions Code/Mantid/Framework/API/src/WorkspaceOpOverloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "MantidAPI/SpectraAxis.h"
#include <numeric>
#include "MantidAPI/IMDWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"

namespace Mantid
{
Expand Down Expand Up @@ -115,6 +116,13 @@ namespace OperatorOverloads
template DLLExport IMDWorkspace_sptr executeBinaryOperation(const std::string &, const MatrixWorkspace_sptr, const IMDWorkspace_sptr, bool,
bool, const std::string &, bool);

template DLLExport IMDHistoWorkspace_sptr executeBinaryOperation(const std::string &, const IMDHistoWorkspace_sptr, const IMDHistoWorkspace_sptr, bool,
bool, const std::string &, bool);
template DLLExport IMDHistoWorkspace_sptr executeBinaryOperation(const std::string &, const IMDHistoWorkspace_sptr, const MatrixWorkspace_sptr, bool,
bool, const std::string &, bool);
template DLLExport IMDHistoWorkspace_sptr executeBinaryOperation(const std::string &, const MatrixWorkspace_sptr, const IMDHistoWorkspace_sptr, bool,
bool, const std::string &, bool);

} // namespace OperatorOverloads


Expand Down
37 changes: 35 additions & 2 deletions Code/Mantid/Framework/PythonAPI/MantidFramework.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def _decompile(code_object):
'INPLACE_DIVIDE', 'INPLACE_TRUE_DIVIDE','INPLACE_FLOOR_DIVIDE',
'INPLACE_MODULO', 'INPLACE_ADD', 'INPLACE_SUBTRACT',
'INPLACE_LSHIFT','INPLACE_RSHIFT','INPLACE_AND', 'INPLACE_XOR',
'INPLACE_OR'])
'INPLACE_OR',
'COMPARE_OP'])

#-------------------------------------------------------------------------------

Expand Down Expand Up @@ -601,7 +602,39 @@ def __lt__(self, rhs):
Do the (self < rhs) comparison and return a new proxy managing that object
"""
lhs = lhs_info()
return self.__do_operation('LessThan', rhs, inplace=True, reverse=False,
return self.__do_operation('LessThan', rhs, inplace=False, reverse=False,
lhs_vars=lhs)

def __gt__(self, rhs):
"""
Do the (self > rhs) comparison and return a new proxy managing that object
"""
lhs = lhs_info()
return self.__do_operation('GreaterThan', rhs, inplace=False, reverse=False,
lhs_vars=lhs)

def __or__(self, rhs):
"""
Do the (self || rhs) comparison and return a new proxy managing that object
"""
lhs = lhs_info()
return self.__do_operation('Or', rhs, inplace=False, reverse=False,
lhs_vars=lhs)

def __and__(self, rhs):
"""
Do the (self && rhs) comparison and return a new proxy managing that object
"""
lhs = lhs_info()
return self.__do_operation('And', rhs, inplace=False, reverse=False,
lhs_vars=lhs)

def __xor__(self, rhs):
"""
Do the (self ^ rhs) comparison and return a new proxy managing that object
"""
lhs = lhs_info()
return self.__do_operation('Xor', rhs, inplace=False, reverse=False,
lhs_vars=lhs)

def equals(self, rhs, tol):
Expand Down
9 changes: 9 additions & 0 deletions Code/Mantid/Framework/PythonAPI/src/WorkspaceProxies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/IMDWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"

using namespace Mantid::API;

Expand Down Expand Up @@ -194,9 +195,17 @@ namespace Mantid
template WorkspaceGroup_sptr performBinaryOp(const WorkspaceGroup_sptr, const WorkspaceGroup_sptr, const std::string& , const std::string & name,
bool, bool);

template IMDHistoWorkspace_sptr performBinaryOp(const IMDHistoWorkspace_sptr, const IMDHistoWorkspace_sptr, const std::string& , const std::string & name,
bool, bool);
template IMDHistoWorkspace_sptr performBinaryOp(const IMDHistoWorkspace_sptr, const MatrixWorkspace_sptr, const std::string& , const std::string & name,
bool, bool);


// Double variants
template IMDWorkspace_sptr performBinaryOpWithDouble(const IMDWorkspace_sptr, const double, const std::string& op,
const std::string &, bool, bool);
template IMDHistoWorkspace_sptr performBinaryOpWithDouble(const IMDHistoWorkspace_sptr, const double, const std::string& op,
const std::string &, bool, bool);
template WorkspaceGroup_sptr performBinaryOpWithDouble(const WorkspaceGroup_sptr, const double, const std::string& op,
const std::string &, bool, bool);

Expand Down
5 changes: 5 additions & 0 deletions Code/Mantid/Framework/PythonAPI/src/api_exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,21 @@ using namespace boost::python;
typedef WorkspaceGroup_sptr(*binary_fn_gp_md)(const API::WorkspaceGroup_sptr, const API::IMDWorkspace_sptr, const std::string &,const std::string &,bool, bool);
typedef WorkspaceGroup_sptr(*binary_fn_gp_gp)(const API::WorkspaceGroup_sptr, const API::WorkspaceGroup_sptr, const std::string &,const std::string &,bool, bool);

typedef IMDHistoWorkspace_sptr(*binary_fn_mh_mh)(const API::IMDHistoWorkspace_sptr, const API::IMDHistoWorkspace_sptr, const std::string &,const std::string &,bool, bool);

typedef IMDWorkspace_sptr(*binary_fn_md_db)(const API::IMDWorkspace_sptr, double, const std::string&,const std::string &,bool,bool);
typedef IMDHistoWorkspace_sptr(*binary_fn_mh_db)(const API::IMDHistoWorkspace_sptr, double, const std::string&,const std::string &,bool,bool);
typedef WorkspaceGroup_sptr(*binary_fn_gp_db)(const API::WorkspaceGroup_sptr, double, const std::string&,const std::string &,bool,bool);

// Binary operations helpers
def("_binary_op", (binary_fn_md_md)&PythonAPI::performBinaryOp);
def("_binary_op", (binary_fn_md_gp)&PythonAPI::performBinaryOp);
def("_binary_op", (binary_fn_gp_md)&PythonAPI::performBinaryOp);
def("_binary_op", (binary_fn_gp_gp)&PythonAPI::performBinaryOp);
def("_binary_op", (binary_fn_mh_mh)&PythonAPI::performBinaryOp);

def("_binary_op", (binary_fn_md_db)&PythonAPI::performBinaryOpWithDouble);
def("_binary_op", (binary_fn_mh_db)&PythonAPI::performBinaryOpWithDouble);
def("_binary_op", (binary_fn_gp_db)&PythonAPI::performBinaryOpWithDouble);

}
Expand Down
51 changes: 50 additions & 1 deletion Code/Mantid/Framework/PythonAPI/test/MDHistoWorkspaceTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,56 @@ def test_operators_md_double(self):
A /= B
A -= B


""" boolean_workspace = MDHistoWorkspace < MDHistoWorkspace """
def test_comparisons_and_boolean_operations(self):
A = mtd['A']
B = mtd['B']
B += 1
C = A < B
self.assertEqual( C.getName(), 'C')
self.assertEqual( C.signalAt(0), 1.0)
D = A > B
self.assertEqual( D.getName(), 'D')
self.assertEqual( D.signalAt(0), 0.0)
E = C | D
self.assertEqual( E.getName(), 'E')
self.assertEqual( E.signalAt(0), 1.0)
F = C & D
self.assertEqual( F.getName(), 'F')
self.assertEqual( F.signalAt(0), 0.0)
G = C ^ C
self.assertEqual( G.getName(), 'G')
self.assertEqual( G.signalAt(0), 0.0)
H = C ^ D
self.assertEqual( H.getName(), 'H')
self.assertEqual( H.signalAt(0), 1.0)

def test_comparisons_histo_scalar(self):
A = mtd['A']
C = A < 1000.0
self.assertEqual( C.getName(), 'C')
self.assertEqual( C.signalAt(0), 1.0)
D = A > 1.0
self.assertEqual( D.getName(), 'D')
self.assertEqual( D.signalAt(0), 1.0)

def test_inplace_boolean_operations(self):
A = mtd['A']
B = mtd['B']
B += 1
C = A < B # all 1 (true)
D = A > B # all 0 (false)

C |= D
self.assertEqual( C.signalAt(0), 1.0)
C &= D
self.assertEqual( C.signalAt(0), 0.0)
C += 1
self.assertEqual( C.signalAt(0), 1.0)
C ^= C
self.assertEqual( C.signalAt(0), 0.0)


if __name__ == '__main__':
unittest.main()

Expand Down

0 comments on commit f05199f

Please sign in to comment.