Skip to content

Commit

Permalink
add release note and unit test for mantid_algorithm_exec
Browse files Browse the repository at this point in the history
  • Loading branch information
backmari committed Aug 9, 2024
1 parent 2076606 commit 9b869a7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/releasenotes/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down
2 changes: 1 addition & 1 deletion docs/user/dead_time_correction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://en.wikipedia.org/wiki/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
Expand Down
3 changes: 1 addition & 2 deletions reflectivity_ui/interfaces/data_handling/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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

0 comments on commit 9b869a7

Please sign in to comment.