Commit 17fa5df0 authored by Gemma Guest's avatar Gemma Guest
Browse files

Implement loading in the preprocessing algorithm

Re #32125
parent 3d6d228b
...@@ -5,16 +5,17 @@ ...@@ -5,16 +5,17 @@
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 + # SPDX - License - Identifier: GPL - 3.0 +
from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm) from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, MatrixWorkspaceProperty, WorkspaceGroup)
from mantid.kernel import (CompositeValidator, StringArrayLengthValidator, StringArrayMandatoryValidator,
StringArrayProperty, Direction)
class _Prop: from mantid.simpleapi import LoadEventNexus, LoadNexus
RUNS = 'InputRunList'
GROUP_TOF = 'GroupTOFWorkspaces'
OUTPUT_WS = 'OutputWorkspace'
class ReflectometryISISPreprocess(DataProcessorAlgorithm): class ReflectometryISISPreprocess(DataProcessorAlgorithm):
_RUNS = 'InputRunList'
_GROUP_TOF = 'GroupTOFWorkspaces'
_OUTPUT_WS = 'OutputWorkspace'
_EVENT_MODE = "EventMode"
def __init__(self): def __init__(self):
"""Initialize an instance of the algorithm.""" """Initialize an instance of the algorithm."""
...@@ -36,15 +37,52 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm): ...@@ -36,15 +37,52 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm):
"""Return a list of related algorithm names.""" """Return a list of related algorithm names."""
return ['ReflectometryISISLoadAndProcess', 'ReflectometryReductionOneAuto'] return ['ReflectometryISISLoadAndProcess', 'ReflectometryReductionOneAuto']
def version(self):
"""Return the version of the algorithm."""
return 1
def PyInit(self): def PyInit(self):
pass self.declareProperty(
StringArrayProperty(self._RUNS, values=[], validator=self._get_input_runs_validator()),
doc='A list of run numbers or workspace names to load and preprocess')
self.declareProperty(self._EVENT_MODE, False, direction=Direction.Input,
doc='If true, load the input workspaces as event data')
self.declareProperty(
MatrixWorkspaceProperty(self._OUTPUT_WS, '', direction=Direction.Output),
doc='The preprocessed output workspace. If multiple input runs are specified '
'they will be summed into a single output workspace.')
@staticmethod
def _get_input_runs_validator():
mandatoryInputRuns = CompositeValidator()
mandatoryInputRuns.add(StringArrayMandatoryValidator())
lenValidator = StringArrayLengthValidator()
lenValidator.setLengthMin(1)
mandatoryInputRuns.add(lenValidator)
return mandatoryInputRuns
def PyExec(self): def PyExec(self):
pass workspace = self._loadRun(self.getPropertyValue(self._RUNS))
self.setProperty(self._OUTPUT_WS, workspace)
def _loadRun(self, run):
"""Load a run as an event workspace if slicing is requested, or a histogram
workspace otherwise. Transmission runs are always loaded as histogram workspaces."""
event_mode = self.getProperty(self._EVENT_MODE).value
if event_mode:
ws = LoadEventNexus(Filename=run, LoadMonitors=True, StoreInADS=False).OutputWorkspace
self._validate_event_ws(ws)
self.log().information('Loaded event workspace')
else:
ws = LoadNexus(Filename=run, StoreInADS=False)
self.log().information('Loaded workspace ')
return ws
@staticmethod
def _validate_event_ws(workspace):
if isinstance(workspace, WorkspaceGroup):
# Our reduction algorithm doesn't currently support this due to slicing
# (which would result in a group of groups)
raise RuntimeError('Loading Workspace Groups in event mode is not supported currently.')
if not workspace.run().hasProperty('proton_charge'):
# Reduction algorithm requires proton_charge
raise RuntimeError('Event workspaces must contain proton_charge')
AlgorithmFactory.subscribe(ReflectometryISISPreprocess) AlgorithmFactory.subscribe(ReflectometryISISPreprocess)
...@@ -6,24 +6,35 @@ ...@@ -6,24 +6,35 @@
# SPDX - License - Identifier: GPL - 3.0 + # SPDX - License - Identifier: GPL - 3.0 +
import unittest import unittest
from mantid.api import MatrixWorkspace from mantid.api import IEventWorkspace, MatrixWorkspace
from testhelpers import create_algorithm from mantid.simpleapi import ReflectometryISISPreprocess
class ReflectometryISISLoadAndProcessTest(unittest.TestCase): class ReflectometryISISPreprocessTest(unittest.TestCase):
ALG_NAME = "ReflectometryISISPreprocess" def test_input_run_is_loaded_histo_mode_by_default(self):
args = {'InputRunList': '13460',
"OutputWorkspace": "ws"}
output_ws = ReflectometryISISPreprocess(**args)
self.assertIsInstance(output_ws, MatrixWorkspace)
self.assertEqual("Workspace2D", output_ws.id())
def test_algorithm_executes(self): def test_input_run_is_loaded_histo_mode(self):
alg = create_algorithm(self.ALG_NAME) args = {'InputRunList': '13460',
alg.execute() "EventMode": False,
self.assertTrue(alg.isExecuted()) "OutputWorkspace": "ws"}
output_ws = ReflectometryISISPreprocess(**args)
self.assertIsInstance(output_ws, MatrixWorkspace)
self.assertEqual("Workspace2D", output_ws.id())
def test_input_run_is_loaded(self): def test_input_run_is_loaded_event_mode(self):
args = {'InputRunList': '13460'} args = {'InputRunList': '13460',
alg = create_algorithm(self.ALG_NAME, **args) "EventMode": True,
alg.execute() "OutputWorkspace": "ws"}
output_ws = alg.getProperty("OutputWorkspace") output_ws = ReflectometryISISPreprocess(**args)
self.assertIsInstance(MatrixWorkspace, output_ws) self.assertIsInstance(output_ws, IEventWorkspace)
def test_validation_of_event_workspaces(self):
pass
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment