diff --git a/docs/releasenotes/index.rst b/docs/releasenotes/index.rst index a058463..2a1ace4 100644 --- a/docs/releasenotes/index.rst +++ b/docs/releasenotes/index.rst @@ -12,7 +12,7 @@ Notes for major and minor releases. Notes for Patch releases are deferred. **Of interest to the User**: -- PR #XYZ: one-liner description +- PR #95: Optional dead-time correction (disabled by default) **Of interest to the Developer:** diff --git a/docs/user/dead_time_correction.rst b/docs/user/dead_time_correction.rst index 5070cc8..00af2a9 100644 --- a/docs/user/dead_time_correction.rst +++ b/docs/user/dead_time_correction.rst @@ -22,7 +22,7 @@ for paralyzable detectors, where | :math:`t_{\mathrm{dead}}` = dead time | :math:`t_{\mathrm{bin}}` = TOF bin width | :math:`\mathrm{rate}` = measured count rate -| :math:`\mathrm{W}` = Lambert W function +| :math:`\mathrm{W}` = `Lambert W function `_ The class ``SingleReadoutDeadTimeCorrection`` is a Mantid-style algorithm for computing the dead-time correction for an event workspace. One can optionally include error events in the diff --git a/reflectivity_ui/interfaces/data_handling/instrument.py b/reflectivity_ui/interfaces/data_handling/instrument.py index d0699d6..d75a485 100644 --- a/reflectivity_ui/interfaces/data_handling/instrument.py +++ b/reflectivity_ui/interfaces/data_handling/instrument.py @@ -77,7 +77,7 @@ def mantid_algorithm_exec(algorithm_class, **kwargs): algorithm property ``OutputWorkspace`` will be returned """ algorithm_instance = algorithm_class() - assert algorithm_instance.PyInit, "str(algorithm_class) is not a Mantid Python algorithm" + assert hasattr(algorithm_instance, "PyInit"), f"{algorithm_class} is not a Mantid Python algorithm" algorithm_instance.PyInit() for name, value in kwargs.items(): algorithm_instance.setProperty(name, value) @@ -98,7 +98,6 @@ def get_dead_time_correction(ws, configuration, error_ws=None): tof_min = ws.getTofMin() tof_max = ws.getTofMax() - run_number = ws.getRun().getProperty("run_number").value corr_ws = mantid_algorithm_exec( DeadTimeCorrection.SingleReadoutDeadTimeCorrection, InputWorkspace=ws, diff --git a/test/unit/reflectivity_ui/interfaces/data_handling/test_dead_time_correction.py b/test/unit/reflectivity_ui/interfaces/data_handling/test_dead_time_correction.py index 5c40b94..c773263 100644 --- a/test/unit/reflectivity_ui/interfaces/data_handling/test_dead_time_correction.py +++ b/test/unit/reflectivity_ui/interfaces/data_handling/test_dead_time_correction.py @@ -1,5 +1,6 @@ # package imports from reflectivity_ui.interfaces.data_handling.DeadTimeCorrection import SingleReadoutDeadTimeCorrection +from reflectivity_ui.interfaces.data_handling.instrument import mantid_algorithm_exec # 3rd-party imports import mantid.simpleapi as api @@ -15,14 +16,12 @@ def test_deadtime(is_paralyzable, data_server): """Test of the dead-time correction algorithm SingleReadoutDeadTimeCorrection""" with amend_config(data_dir=data_server.h5_full_path): ws = api.Load("REF_M_42112") - - algo = SingleReadoutDeadTimeCorrection() - algo.PyInit() - algo.setProperty("InputWorkspace", ws) - algo.setProperty("OutputWorkspace", "dead_time_corr") - algo.setProperty("Paralyzable", is_paralyzable) - algo.PyExec() - corr_ws = algo.getProperty("OutputWorkspace").value + corr_ws = mantid_algorithm_exec( + SingleReadoutDeadTimeCorrection, + InputWorkspace=ws, + Paralyzable=is_paralyzable, + OutputWorkspace="dead_time_corr", + ) corr = corr_ws.readY(0) for c in corr: assert 1.0 <= c < 1.001, "value not between 1.0 and 1.001" diff --git a/test/unit/reflectivity_ui/interfaces/data_handling/test_instrument.py b/test/unit/reflectivity_ui/interfaces/data_handling/test_instrument.py index 55bcaed..1849d40 100644 --- a/test/unit/reflectivity_ui/interfaces/data_handling/test_instrument.py +++ b/test/unit/reflectivity_ui/interfaces/data_handling/test_instrument.py @@ -1,7 +1,11 @@ # package imports from reflectivity_ui.interfaces.configuration import Configuration +from reflectivity_ui.interfaces.data_handling.instrument import mantid_algorithm_exec # 3rd party imports +from mantid.api import MatrixWorkspaceProperty, PythonAlgorithm +from mantid.kernel import Direction +from mantid.simpleapi import CreateSingleValuedWorkspace import pytest @@ -27,3 +31,31 @@ def test_load_data_deadtime(data_server): for ws in ws_list: assert "dead_time_applied" not in ws.getRun() assert ws.extractY().sum() == ws.getNumberEvents() + + +def test_mantid_algorithm_exec(): + """Test helper function mantid_algorithm_exec""" + # test wrong type of class + class TestNotMantidAlgo: + pass + + with pytest.raises(AssertionError, match="is not a Mantid Python algorithm"): + mantid_algorithm_exec(TestNotMantidAlgo) + # test Mantid Python algorithm + + class TestMantidAlgo(PythonAlgorithm): + def PyInit(self): + self.declareProperty("Value", 8, "Value in workspace") + self.declareProperty( + MatrixWorkspaceProperty("OutputWorkspace", "", Direction.Output), + "Output workspace", + ) + + def PyExec(self): + value = self.getProperty("Value").value + ws = CreateSingleValuedWorkspace(value) + self.setProperty("OutputWorkspace", ws) + + custom_value = 4 + ws_out = mantid_algorithm_exec(TestMantidAlgo, Value=custom_value, OutputWorkspace="output") + assert ws_out.readY(0)[0] == custom_value