Commit f8bb044e authored by Gemma Guest's avatar Gemma Guest
Browse files

Change new workflow algorithm to use child algorithms

Previously it was calling other algorithms directly which will not play
well with the history

Re #32125
parent 1fc7158b
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, MatrixWorkspaceProperty, MatrixWorkspace, WorkspaceGroup) from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, MatrixWorkspaceProperty, MatrixWorkspace, WorkspaceGroup)
from mantid.kernel import (CompositeValidator, StringArrayLengthValidator, StringArrayMandatoryValidator, from mantid.kernel import (CompositeValidator, StringArrayLengthValidator, StringArrayMandatoryValidator,
StringArrayProperty, Direction) StringArrayProperty, Direction)
from mantid.simpleapi import LoadEventNexus, LoadNexus
class ReflectometryISISPreprocess(DataProcessorAlgorithm): class ReflectometryISISPreprocess(DataProcessorAlgorithm):
...@@ -66,14 +65,17 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm): ...@@ -66,14 +65,17 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm):
workspace otherwise. Transmission runs are always loaded as histogram workspaces.""" workspace otherwise. Transmission runs are always loaded as histogram workspaces."""
event_mode = self.getProperty(self._EVENT_MODE).value event_mode = self.getProperty(self._EVENT_MODE).value
if event_mode: if event_mode:
alg = LoadEventNexus(Filename=run, LoadMonitors=True, StoreInADS=False) alg = self.createChildAlgorithm('LoadEventNexus', Filename=run, LoadMonitors=True)
ws = alg.OutputWorkspace alg.execute()
ws = alg.getProperty('OutputWorkspace').value
# TODO # TODO
# monitors = alg.MonitorWorkspace # monitors = alg.MonitorWorkspace
self._validate_event_ws(ws) self._validate_event_ws(ws)
self.log().information('Loaded event workspace') self.log().information('Loaded event workspace')
else: else:
ws = LoadNexus(Filename=run, StoreInADS=False) alg = self.createChildAlgorithm('LoadNexus', Filename=run)
alg.execute()
ws = alg.getProperty('OutputWorkspace').value
self.log().information('Loaded workspace ') self.log().information('Loaded workspace ')
return ws return ws
......
...@@ -5,17 +5,18 @@ ...@@ -5,17 +5,18 @@
# 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 +
import unittest import unittest
from unittest import mock
from mantid.api import IEventWorkspace, MatrixWorkspace, WorkspaceGroup from mantid.api import IEventWorkspace, MatrixWorkspace, WorkspaceGroup
from mantid.simpleapi import CreateSampleWorkspace, ReflectometryISISPreprocess from mantid.simpleapi import CreateSampleWorkspace
from plugins.algorithms.WorkflowAlgorithms.ReflectometryISISPreprocess import ReflectometryISISPreprocess
from testhelpers import create_algorithm
class ReflectometryISISPreprocessTest(unittest.TestCase): class ReflectometryISISPreprocessTest(unittest.TestCase):
def test_input_run_is_loaded_histo_mode_by_default(self): def test_input_run_is_loaded_histo_mode_by_default(self):
args = {'InputRunList': '13460', args = {'InputRunList': '13460',
"OutputWorkspace": "ws"} "OutputWorkspace": "ws"}
output_ws = ReflectometryISISPreprocess(**args) output_ws = self._run_test(args)
self.assertIsInstance(output_ws, MatrixWorkspace) self.assertIsInstance(output_ws, MatrixWorkspace)
self.assertEqual("Workspace2D", output_ws.id()) self.assertEqual("Workspace2D", output_ws.id())
...@@ -23,7 +24,7 @@ class ReflectometryISISPreprocessTest(unittest.TestCase): ...@@ -23,7 +24,7 @@ class ReflectometryISISPreprocessTest(unittest.TestCase):
args = {'InputRunList': '13460', args = {'InputRunList': '13460',
"EventMode": False, "EventMode": False,
"OutputWorkspace": "ws"} "OutputWorkspace": "ws"}
output_ws = ReflectometryISISPreprocess(**args) output_ws = self._run_test(args)
self.assertIsInstance(output_ws, MatrixWorkspace) self.assertIsInstance(output_ws, MatrixWorkspace)
self.assertEqual("Workspace2D", output_ws.id()) self.assertEqual("Workspace2D", output_ws.id())
...@@ -31,30 +32,28 @@ class ReflectometryISISPreprocessTest(unittest.TestCase): ...@@ -31,30 +32,28 @@ class ReflectometryISISPreprocessTest(unittest.TestCase):
args = {'InputRunList': '13460', args = {'InputRunList': '13460',
"EventMode": True, "EventMode": True,
"OutputWorkspace": "ws"} "OutputWorkspace": "ws"}
output_ws = ReflectometryISISPreprocess(**args) output_ws = self._run_test(args)
self.assertIsInstance(output_ws, IEventWorkspace) self.assertIsInstance(output_ws, IEventWorkspace)
@mock.patch("ReflectometryISISPreprocess.LoadEventNexus") def test_validation_of_event_workspaces_without_proton_charge_throws(self):
def test_validation_of_event_workspaces_without_proton_charge_throws(self, mocked_loader):
args = {'InputRunList': '13460',
"EventMode": True,
"OutputWorkspace": "ws"}
ws = CreateSampleWorkspace() ws = CreateSampleWorkspace()
mocked_loader.return_value.OutputWorkspace = ws
with self.assertRaisesRegex(RuntimeError, "proton_charge"):
ReflectometryISISPreprocess(**args)
@mock.patch("ReflectometryISISPreprocess.LoadEventNexus") with self.assertRaisesRegex(RuntimeError, "proton_charge"):
def test_validation_of_event_workspace_group_throws(self, mocked_loader): ReflectometryISISPreprocess._validate_event_ws(ws)
args = {'InputRunList': '13460',
"EventMode": True,
"OutputWorkspace": "ws"}
def test_validation_of_event_workspace_group_throws(self):
ws = WorkspaceGroup() ws = WorkspaceGroup()
mocked_loader.return_value.OutputWorkspace = ws
with self.assertRaisesRegex(RuntimeError, "Workspace Groups"): with self.assertRaisesRegex(RuntimeError, "Workspace Groups"):
ReflectometryISISPreprocess(**args) ReflectometryISISPreprocess._validate_event_ws(ws)
def _run_test(self, args):
alg = create_algorithm('ReflectometryISISPreprocess', **args)
alg.setChild(True)
alg.setRethrows(True)
alg.execute()
output_ws = alg.getProperty('OutputWorkspace').value
return output_ws
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