Skip to content

Commit

Permalink
refs #11511. Add EFixed and DataSources property
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Apr 10, 2015
1 parent bd44113 commit 9ecb525
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 36 deletions.
Expand Up @@ -11,6 +11,14 @@ class CreateMD(DataProcessorAlgorithm):
def _possible_emodes(self):
return ['Elastic', 'Direct', 'Indirect']

def _load_ws(self, filename, wsname):
load = self.createChildAlgorithm('Load')
load.setProperty('Filename', filename)
load.setPropertyValue('OutputWorkspace', wsname)
load.execute()
ws = load.getProperty('OutputWorkspace').value
return ws

def _add_sample_log(self, workspace, log_name, log_number):
add_log = self.createChildAlgorithm('AddSampleLog')
add_log.setProperty('Workspace', workspace)
Expand Down Expand Up @@ -76,8 +84,7 @@ def _merge_runs(self, to_merge):
merge_alg.execute()
return merge_alg.getProperty('OutputWorkspace').value

def _single_run(self, input_workspace, emode, psi, gl, gs, alatt=None, angdeg=None, u=None, v=None,):
import numpy as np
def _single_run(self, input_workspace, emode, efix, psi, gl, gs, alatt=None, angdeg=None, u=None, v=None,):
ub_params = map(any, [alatt, angdeg, u, v])
goniometer_params = [psi, gl, gs]
if any(ub_params) and not all(ub_params):
Expand All @@ -88,6 +95,9 @@ def _single_run(self, input_workspace, emode, psi, gl, gs, alatt=None, angdeg=N
else:
self._set_ub(workspace=input_workspace, a=alatt[0], b=alatt[1], c=alatt[2], alpha=angdeg[0], beta=angdeg[1], gamma=angdeg[2], u=u, v=v)

if efix > 0.0:
self._add_sample_log(workspace=input_workspace, log_name='Ei',log_number=efix)

if any(goniometer_params):
self._add_sample_log(workspace=input_workspace, log_name='gl', log_number=gl)
self._add_sample_log(workspace=input_workspace, log_name='gs', log_number=gs)
Expand All @@ -105,11 +115,13 @@ def summary(self):
return 'Creates a mutlidimensional workspace by transforming and combining individual runs.'

def PyInit(self):
self.declareProperty(StringArrayProperty('InputWorkspaces', values=[], direction=Direction.Input, validator=StringArrayMandatoryValidator()),
doc='Input workspaces to process')
self.declareProperty(StringArrayProperty('DataSources', values=[], direction=Direction.Input, validator=StringArrayMandatoryValidator()),
doc='Input workspaces to process, or filenames to load and process')

self.declareProperty('Emode', defaultValue='Direct', validator=StringListValidator(self._possible_emodes()), direction=Direction.Input, doc='Analysis mode ' + str(self._possible_emodes()) )
self.declareProperty(FloatArrayProperty('EFix', values=[], direction=Direction.Input), doc='datasource energy values in meV')

self.declareProperty('Emode', defaultValue='Direct', validator=StringListValidator(self._possible_emodes()), direction=Direction.Input, doc='Analysis mode ' + str(self._possible_emodes()) )

self.declareProperty(FloatArrayProperty('Alatt', values=[], validator=FloatArrayMandatoryValidator(), direction=Direction.Input ), doc='Lattice parameters' )

self.declareProperty(FloatArrayProperty('Angdeg', values=[], validator=FloatArrayMandatoryValidator(), direction=Direction.Input ), doc='Lattice angles' )
Expand All @@ -126,6 +138,8 @@ def PyInit(self):

self.declareProperty(IMDWorkspaceProperty('OutputWorkspace', '', direction=Direction.Output ), doc='Output MDWorkspace')

self.declareProperty('FileBacked', defaultValue=False, direction=Direction.Input, doc="Execute in a file-backed mode")

def _validate_inputs(self):

emode = self.getProperty('Emode').value
Expand All @@ -135,14 +149,15 @@ def _validate_inputs(self):
v = self.getProperty('v').value
psi = self.getProperty('Psi').value
gl = self.getProperty('Gl').value
gs = self.getProperty('Gs').value
gs = self.getProperty('Gs').value
efix = self.getProperty('EFix').value

input_workspaces = self.getProperty("InputWorkspaces").value
input_workspaces = self.getProperty("DataSources").value

ws_entries = len(input_workspaces)

if ws_entries < 1:
raise ValueError("Need one or more input workspace")
raise ValueError("Need one or more input datasource")

if len(u) != 3:
raise ValueError("u must have 3 components")
Expand All @@ -160,13 +175,16 @@ def _validate_inputs(self):
raise ValueError("Unknown emode %s Allowed values are %s" % (emode, self._possible_emodes()))

if len(psi) > 0 and len(psi) != ws_entries:
raise ValueError("If Psi is given a entry should be provided for every input workspace")
raise ValueError("If Psi is given a entry should be provided for every input datasource")

if len(gl) > 0 and len(gl) != ws_entries:
raise ValueError("If Gl is given a entry should be provided for every input workspace")
raise ValueError("If Gl is given a entry should be provided for every input datasource")

if len(gs) > 0 and len(gs) != ws_entries:
raise ValueError("If Gs is given a entry should be provided for every input workspace")
raise ValueError("If Gs is given a entry should be provided for every input datasource")

if len(efix) > 1 and len(efix) != ws_entries:
raise ValueError("Either specify a single EFix value, or as many as there are input datasources")


def PyExec(self):
Expand All @@ -180,45 +198,63 @@ def PyExec(self):
v = self.getProperty('v').value
psi = self.getProperty('Psi').value
gl = self.getProperty('Gl').value
gs = self.getProperty('Gs').value
gs = self.getProperty('Gs').value
efix = self.getProperty('EFix').value

input_workspaces = self.getProperty("InputWorkspaces").value
data_sources = self.getProperty("DataSources").value

ws_entries = len(input_workspaces)
entries = len(data_sources)

self._validate_inputs()

''' pad out lists'''
if len(psi) == 0:
psi = [0.0] * ws_entries
psi = [0.0] * entries

if len(gl) == 0:
gl = [0.0] * ws_entries
gl = [0.0] * entries

if len(gs) == 0:
gs = [0.0] * ws_entries
gs = [0.0] * entries

if len(efix) == 0:
efix = [-1.0] * entries
elif len(efix) == 1:
efix = efix * entries

output_workspace = None
run_md = None

to_merge_names = list()

run_data = zip(input_workspaces, psi, gl, gs)
run_data = zip(data_sources, psi, gl, gs, efix)
counter = 0
for run_entry in run_data:
ws_name, psi_entry, gl_entry, gs_entry = run_entry
ws = AnalysisDataService.retrieve(ws_name)
run_md = self._single_run(input_workspace=ws, emode=emode, alatt=alatt, angdeg=angdeg, u=u, v=v, psi=psi_entry, gl=gl_entry, gs=gs_entry)
to_merge_name = ws_name + "_md"
data_source, psi_entry, gl_entry, gs_entry, efix_entry = run_entry
must_load = not AnalysisDataService.doesExist(data_source)
ws = None
if must_load:
ws_name = "%s_md_%i" % ( os.path.splitext(data_source)[0] , counter ) # Strip off any file extensions, and call it _md_{n} where n avoids clashes in the dictionary
ws = self._load_ws(data_source, ws_name)
to_merge_name = ws_name
counter += 1
else:
ws = AnalysisDataService.retrieve(data_source)
to_merge_name = "%s_md" % data_source
run_md = self._single_run(input_workspace=ws, emode=emode, efix=efix_entry, alatt=alatt, angdeg=angdeg, u=u, v=v, psi=psi_entry, gl=gl_entry, gs=gs_entry)

AnalysisDataService.addOrReplace(to_merge_name, run_md)
to_merge_names.append(to_merge_name)


if len(to_merge_names) > 1:
output_workspace = self._merge_runs(to_merge_names)
else:
output_workspace = AnalysisDataService.retrieve(to_merge_names[0])

# Clear out temporary workspaces.
for ws in to_merge_names:
DeleteWorkspace(ws)
# Clear out temporary workspaces. This could be done in an eager fashion.
for mdws in to_merge_names:
DeleteWorkspace(mdws)

self.setProperty("OutputWorkspace", output_workspace)

Expand Down
Expand Up @@ -10,6 +10,7 @@ set ( TEST_PY_FILES
ConjoinSpectraTest.py
CorrectLogTimesTest.py
CreateLeBailFitInputTest.py
CreateMDTest.py
IndirectCalibrationTest.py
CreateWorkspaceTest.py
CylinderPaalmanPingsCorrectionTest.py
Expand Down
Expand Up @@ -15,15 +15,15 @@ def test_must_have_more_than_one_input_workspace(self):
alg = AlgorithmManager.create("CreateMD")
alg.setRethrows(True)
alg.initialize()
self.assertRaises(ValueError, alg.setProperty, "InputWorkspaces", [])
self.assertRaises(ValueError, alg.setProperty, "DataSources", [])

def test_set_up_madatory(self):

alg = AlgorithmManager.create("CreateMD")
alg.setRethrows(True)
alg.initialize()
alg.setPropertyValue("OutputWorkspace", "mdworkspace")
alg.setProperty("InputWorkspaces", ['a', 'b'])
alg.setProperty("DataSources", ['a', 'b'])
alg.setProperty("Emode", "Direct")
alg.setProperty("Alatt", [1,1,1])
alg.setProperty("Angdeg", [90,90,90])
Expand All @@ -33,16 +33,16 @@ def test_set_up_madatory(self):
def test_psi_right_size(self):

input_workspace = CreateSampleWorkspace(NumBanks=1, BinWidth=2000)
AddSampleLog(input_workspace, LogName='Ei', LogText='12.0', LogType='Number')

alg = AlgorithmManager.create("CreateMD")
alg.setRethrows(True)
alg.initialize()
alg.setPropertyValue("OutputWorkspace", "mdworkspace")
alg.setProperty("InputWorkspaces", ['input_workspace'])
alg.setProperty("DataSources", ['input_workspace'])
alg.setProperty("Emode", "Direct")
alg.setProperty("Alatt", [1,1,1])
alg.setProperty("Angdeg", [90,90,90])
alg.setProperty("Efix", 12.0)
alg.setProperty("u", [0,0,1])
alg.setProperty("v", [1,0,0])
alg.setProperty("Psi", [0, 0, 0]) # Too large
Expand All @@ -54,16 +54,16 @@ def test_psi_right_size(self):
def test_gl_right_size(self):

input_workspace = CreateSampleWorkspace(NumBanks=1, BinWidth=2000)
AddSampleLog(input_workspace, LogName='Ei', LogText='12.0', LogType='Number')

alg = AlgorithmManager.create("CreateMD")
alg.setRethrows(True)
alg.initialize()
alg.setPropertyValue("OutputWorkspace", "mdworkspace")
alg.setProperty("InputWorkspaces", ['input_workspace'])
alg.setProperty("DataSources", ['input_workspace'])
alg.setProperty("Emode", "Direct")
alg.setProperty("Alatt", [1,1,1])
alg.setProperty("Angdeg", [90,90,90])
alg.setProperty("Efix", 12.0)
alg.setProperty("u", [0,0,1])
alg.setProperty("v", [1,0,0])
alg.setProperty("Psi", [0]) # Right size
Expand All @@ -75,16 +75,16 @@ def test_gl_right_size(self):
def test_gs_right_size(self):

input_workspace = CreateSampleWorkspace(NumBanks=1, BinWidth=2000)
AddSampleLog(input_workspace, LogName='Ei', LogText='12.0', LogType='Number')

alg = AlgorithmManager.create("CreateMD")
alg.setRethrows(True)
alg.initialize()
alg.setPropertyValue("OutputWorkspace", "mdworkspace")
alg.setProperty("InputWorkspaces", ['input_workspace'])
alg.setProperty("DataSources", ['input_workspace'])
alg.setProperty("Emode", "Direct")
alg.setProperty("Alatt", [1,1,1])
alg.setProperty("Angdeg", [90,90,90])
alg.setProperty("Efix", 12.0)
alg.setProperty("u", [0,0,1])
alg.setProperty("v", [1,0,0])
alg.setProperty("Psi", [0]) # Right size
Expand All @@ -97,15 +97,15 @@ def test_gs_right_size(self):
def test_execute_single_workspace(self):

input_workspace = CreateSampleWorkspace(NumBanks=1, BinWidth=2000)
AddSampleLog(input_workspace, LogName='Ei', LogText='12.0', LogType='Number')


alg = AlgorithmManager.create("CreateMD")
alg.setRethrows(True)
alg.initialize()
alg.setPropertyValue("OutputWorkspace", "mdworkspace")
alg.setProperty("InputWorkspaces", ['input_workspace'])
alg.setProperty("DataSources", ['input_workspace'])
alg.setProperty("Alatt", [1,1,1])
alg.setProperty("Angdeg", [90,90,90])
alg.setProperty("Efix", 12.0)
alg.setProperty("u", [0,0,1])
alg.setProperty("v", [1,0,0])
alg.execute()
Expand All @@ -114,6 +114,24 @@ def test_execute_single_workspace(self):
self.assertTrue(isinstance(out_ws, IMDEventWorkspace), "Expected an MDEventWorkspace back")
DeleteWorkspace(input_workspace)

def test_execute_multi_file(self):
alg = AlgorithmManager.create("CreateMD")
alg.setRethrows(True)
alg.initialize()
alg.setPropertyValue("OutputWorkspace", "mdworkspace")
alg.setProperty("DataSources", ['CNCS_7860_event.nxs', 'CNCS_7860_event.nxs'])
alg.setProperty("Alatt", [1,1,1])
alg.setProperty("Angdeg", [90,90,90])
alg.setProperty("EFix", [12.0, 13.0])
alg.setProperty("u", [0,0,1])
alg.setProperty("v", [1,0,0])
alg.execute()
out_ws = AnalysisDataService.retrieve("mdworkspace")

self.assertTrue(isinstance(out_ws, IMDEventWorkspace), "Expected an MDEventWorkspace back")






Expand Down

0 comments on commit 9ecb525

Please sign in to comment.