diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CreateMD.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CreateMD.py index c459a31bbb98c4b28e1b64b0c2a9cc08dbe580e4..0a27eb6ab3b46ad774f809522227a9a064251e6d 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CreateMD.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/CreateMD.py @@ -11,9 +11,9 @@ class CreateMD(DataProcessorAlgorithm): def __possible_emodes(self): return ['Elastic', 'Direct', 'Indirect'] - def __single_gen_sqw(self, input_workspace, emode, alatt=[], angdeg=[], u=[], v=[], psi=None, gl=None, gs=None): + def __single_run(self, input_workspace, emode, alatt=[], angdeg=[], u=[], v=[], psi=None, gl=None, gs=None): import numpy as np - ub_params = [any(alatt), any(angdeg), any(u), any(v)] + ub_params = map(any, [alatt, angdeg, u, v]) goniometer_params = [psi, gl, gs] if any(ub_params) and not all(ub_params): raise ValueError("Either specify all of alatt, angledeg, u, v or none of them") @@ -36,8 +36,8 @@ class CreateMD(DataProcessorAlgorithm): SetGoniometer(Workspace=input_workspace, Axis0=axis0, Axis1=axis1, Axis2=axis2) min_extents, max_extents = ConvertToMDMinMaxLocal(InputWorkspace=input_workspace,QDimensions='Q3D',dEAnalysisMode=emode) - output_workspace = ConvertToMD(InputWorkspace=input_workspace, QDimensions='Q3D', QConversionScales='HKL',dEAnalysisMode=emode, MinValues=min_extents, MaxValues=max_extents) - return output_workspace + output_run = ConvertToMD(InputWorkspace=input_workspace, QDimensions='Q3D', QConversionScales='HKL',dEAnalysisMode=emode, MinValues=min_extents, MaxValues=max_extents) + return output_run def category(self): return 'MDAlgorithms' @@ -84,23 +84,23 @@ class CreateMD(DataProcessorAlgorithm): if len(input_workspaces) < 1: raise ValueError("Need one or more input workspace") - - if not emode in self.__possible_emodes(): raise ValueError("Unknown emode %s Allowed values are %s" % (emode, self.__possible_emodes())) - output_workspaces = list() + output_workspace = None + run_md = None for ws in input_workspaces: - out_ws = self.__single_gen_sqw(input_workspace=ws, emode=emode, alatt=alatt, angdeg=angdeg, u=u, v=v, psi=psi, gl=gl, gs=gs) - output_workspaces.append(out_ws) - # TODO. Need to merge runs. - - if len(input_workspaces) > 1: - raise RuntimeError("Merging not implmented yet") - # TODO We will need to merge everything - # TODO We should offer to merge file-backed - - self.setProperty("OutputWorkspace", output_workspaces[0]) + run_md = self.__single_run(input_workspace=ws, emode=emode, alatt=alatt, angdeg=angdeg, u=u, v=v, psi=psi, gl=gl, gs=gs) + + if not output_workspace: + output_workspace = run_md.rename() + else: + print output_workspace.name() + print run_md.name() + output_workspace += run_md # Accumulate results via PlusMD. TODO, will need to find the best performance method for doing this. + + self.setProperty("OutputWorkspace", output_workspace) + diff --git a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CreateMDTest.py b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CreateMDTest.py index 76f79668705b2c062c01fd1b9d76bf904edd5bc8..1f7375fb0e8d4e9ca710f60820cc8a578f4e8a5f 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CreateMDTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/CreateMDTest.py @@ -6,14 +6,6 @@ from mantid.api import AlgorithmManager, IMDHistoWorkspace, IMDEventWorkspace class CreateMDTest(unittest.TestCase): - - - def setUp(self): - pass - - def tearDown(self): - pass - #DeleteWorkspace(self.__in_md ) def test_init(self): alg = AlgorithmManager.create("CreateMD") @@ -39,7 +31,8 @@ class CreateMDTest(unittest.TestCase): alg.setProperty("v", [1,0,0]) def test_execute_single_workspace(self): - input_workspace = CreateSampleWorkspace() + + input_workspace = CreateSampleWorkspace(NumBanks=1, BinWidth=2000) AddSampleLog(input_workspace, LogName='Ei', LogText='12.0', LogType='Number') alg = AlgorithmManager.create("CreateMD") @@ -56,6 +49,34 @@ class CreateMDTest(unittest.TestCase): out_ws = alg.getProperty("OutputWorkspace").value self.assertTrue(isinstance(out_ws, IMDEventWorkspace), "Expected an MDEventWorkspace back") + DeleteWorkspace(input_workspace) + + def test_execute_multiple_runs(self): + input_workspace1 = CreateSampleWorkspace(NumBanks=1, BinWidth=2000) + AddSampleLog(input_workspace1, LogName='Ei', LogText='12.0', LogType='Number') + input_workspace2 = CreateSampleWorkspace(NumBanks=1, BinWidth=2000) + AddSampleLog(input_workspace2, LogName='Ei', LogText='12.0', LogType='Number') + + alg = AlgorithmManager.create("CreateMD") + alg.setRethrows(True) + alg.setChild(True) + alg.initialize() + alg.setPropertyValue("OutputWorkspace", "mdworkspace") + alg.setProperty("InputWorkspaces", [input_workspace1.name(), input_workspace2.name()]) # Two input workspaces + alg.setProperty("Alatt", [1,1,1]) + alg.setProperty("Angdeg", [90,90,90]) + alg.setProperty("u", [0,0,1]) + alg.setProperty("v", [1,0,0]) + alg.execute() + out_ws = alg.getProperty("OutputWorkspace").value + + lastAlg = out_ws.getHistory().lastAlgorithm() + self.assertEqual("PlusMD", lastAlg.name(), "Last operation should have been to merge individually converted runs together.") + + self.assertTrue(isinstance(out_ws, IMDEventWorkspace), "Expected an MDEventWorkspace back") + DeleteWorkspace(input_workspace1) + DeleteWorkspace(input_workspace2) +