Skip to content

Commit

Permalink
refs #11304. Add the ability to merge md workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Mar 13, 2015
1 parent f0b9e42 commit 1796523
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
Expand Up @@ -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")
Expand All @@ -36,8 +36,8 @@ def __single_gen_sqw(self, input_workspace, emode, alatt=[], angdeg=[], u=[], v=
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'
Expand Down Expand Up @@ -84,23 +84,23 @@ def PyExec(self):
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)




Expand Down
Expand Up @@ -6,14 +6,6 @@


class CreateMDTest(unittest.TestCase):


def setUp(self):
pass

def tearDown(self):
pass
#DeleteWorkspace(self.__in_md )

def test_init(self):
alg = AlgorithmManager.create("CreateMD")
Expand All @@ -39,7 +31,8 @@ def test_set_up_madatory(self):
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")
Expand All @@ -56,6 +49,34 @@ def test_execute_single_workspace(self):
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)




Expand Down

0 comments on commit 1796523

Please sign in to comment.