Skip to content

Commit

Permalink
Refs #4083 exposed the not operation and allowed compound expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed Nov 10, 2011
1 parent f36fc87 commit 017d71f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/src/NotMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Any non-zero signal is changed to 0.0 (meaning false).
== Usage ==
B = ~A
A = ~A
See [[MDHistoWorkspace#Boolean_Operations|this page]] for examples on using boolean operations.
*WIKI*/
Expand Down
47 changes: 47 additions & 0 deletions Code/Mantid/Framework/PythonAPI/MantidFramework.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,53 @@ def __xor__(self, rhs):
lhs = lhs_info()
return self.__do_operation('Xor', rhs, inplace=False, reverse=False,
lhs_vars=lhs)




def __do_unary_operation(self, op, lhs_vars):
"""
Perform the unary operation
@param op :: name of the algorithm to run
@param lhs_vars :: is expected to be a tuple containing the number of lhs variables and
their names as the first and second element respectively
"""
global _binary_op_tmps

if lhs_vars[0] > 0:
# Assume the first and clear the tempoaries as this
# must be the final assignment
output_name = lhs_vars[1][0]
clear_tmps = True
else:
# Give it a temporary name and keep track of it
clear_tmps = False
output_name = _binary_op_prefix + str(len(_binary_op_tmps))
_binary_op_tmps.append(output_name)

# Do the operation
alg = mtd.createAlgorithm(op)
alg.setPropertyValue("InputWorkspace", self.getName())
alg.setPropertyValue("OutputWorkspace", output_name)
alg.execute()
resultws = alg.workspace()

if clear_tmps:
for name in _binary_op_tmps:
if mtd.workspaceExists(name) and output_name != name:
mtd.deleteWorkspace(name)
_binary_op_tmps = []

return resultws

def __invert__(self):
"""
Return the inversion (NOT operator) on self
"""
lhs = lhs_info()
return self.__do_unary_operation('NotMD', lhs_vars=lhs)


def equals(self, rhs, tol):
"""
Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/PythonAPI/src/api_exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,10 @@ using namespace boost::python;
;
class_< WorkspaceProperty<IEventWorkspace>, bases<Kernel::Property,API::IWorkspaceProperty>, boost::noncopyable>("EventWorkspaceProperty", no_init)
;
class_< WorkspaceProperty<IMDWorkspace>, bases<Kernel::Property,API::IWorkspaceProperty>, boost::noncopyable>("MDWorkspaceProperty", no_init)
;
class_< WorkspaceProperty<IMDHistoWorkspace>, bases<Kernel::Property,API::IWorkspaceProperty>, boost::noncopyable>("MDHistoWorkspaceProperty", no_init)
;

}

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

def test_compound_arithmetic(self):
A = mtd['A']
B = mtd['B']
C = (A + B) / (A - B)
self.assertIsNotNone( C )

""" boolean_workspace = MDHistoWorkspace < MDHistoWorkspace """
def test_comparisons_and_boolean_operations(self):
A = mtd['A']
Expand Down Expand Up @@ -85,7 +91,7 @@ def test_comparisons_histo_scalar(self):
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']
Expand All @@ -102,6 +108,36 @@ def test_inplace_boolean_operations(self):
C ^= C
self.assertEqual( C.signalAt(0), 0.0)

def test_not_operator(self):
A = mtd['A']
A *= 0
self.assertEqual( A.signalAt(0), 0.0)
# Do with a copy
B = ~A
self.assertEqual( B.signalAt(0), 1.0)
# Do in-place
A = ~A
self.assertEqual( A.signalAt(0), 1.0)

def test_compound_comparison(self):
A = mtd['A']
B = mtd['B']
C = (A > B) & (A > 123) & (B < 2345)
self.assertIsNotNone( C )


def test_compound_boolean_operations(self):
A = mtd['A']
A *= 0
B = A + 1
C = ~(A | B)
self.assertEqual( C.signalAt(0), 0.0)
C = ~(A | B) | B
self.assertEqual( C.signalAt(0), 1.0)
C = ~(A | B) | ~A
self.assertEqual( C.signalAt(0), 1.0)
C = ~(A | B) | ~(A & B)
self.assertEqual( C.signalAt(0), 1.0)

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

0 comments on commit 017d71f

Please sign in to comment.