Skip to content

Commit

Permalink
Change range to mask in tof to use '-'
Browse files Browse the repository at this point in the history
Previously the range in tof to be masked was being specified by ','.
It makes more sense to use '-' to describe the range.
  • Loading branch information
GuiMacielPereira committed Dec 10, 2024
1 parent 848b516 commit fb567f0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/mvesuvio/util/analysis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ def cropAndMaskWorkspace(ws, firstSpec, lastSpec, maskedDetectors, maskTOFRange)
OutputWorkspace=newWsName,
)

maskBinsWithZeros(wsCrop, maskTOFRange) # Used to mask resonance peaks
mask_time_of_flight_bins_with_zeros(wsCrop, maskTOFRange) # Used to mask resonance peaks

MaskDetectors(Workspace=wsCrop, SpectraList=maskedDetectors)
return wsCrop


def maskBinsWithZeros(ws, maskTOFRange):
def mask_time_of_flight_bins_with_zeros(ws, maskTOFRange):
"""
Masks a given TOF range on ws with zeros on dataY.
Leaves errors dataE unchanged, as they are used by later treatments.
Expand All @@ -220,7 +220,7 @@ def maskBinsWithZeros(ws, maskTOFRange):
return

dataX, dataY, dataE = extractWS(ws)
start, end = [int(s) for s in maskTOFRange.split(",")]
start, end = [float(s) for s in maskTOFRange.split("-")]
assert (
start <= end
), "Start value for masking needs to be smaller or equal than end."
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/analysis/test_analysis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import numpy.testing as nptest
from mock import MagicMock
from mvesuvio.util.analysis_helpers import extractWS, _convert_dict_to_table, \
fix_profile_parameters, calculate_h_ratio, extend_range_of_array, numerical_third_derivative
fix_profile_parameters, calculate_h_ratio, extend_range_of_array, numerical_third_derivative, \
mask_time_of_flight_bins_with_zeros
from mantid.simpleapi import CreateWorkspace, DeleteWorkspace


Expand Down Expand Up @@ -149,5 +150,33 @@ def test_numerical_third_derivative(self):
expected_derivative = np.array([np.gradient(np.gradient(np.gradient(y_i, x_i), x_i), x_i)[6: -6] for y_i, x_i in zip(y, x) ])
np.testing.assert_allclose(numerical_derivative, expected_derivative, atol=1e-6)


def test_mask_time_of_flight_bins_with_zeros(self):
data_x = np.arange(10).reshape(1, -1) * np.ones((3, 1))
data_y = np.ones((3, 10))
data_e = np.ones((3, 10))
workspace_mock = MagicMock()
workspace_mock.extractX.return_value = data_x
workspace_mock.extractY.return_value = data_y
workspace_mock.extractE.return_value = data_e

actual_data_x = np.zeros((3, 10))
actual_data_y = np.zeros((3, 10))
actual_data_e = np.zeros((3, 10))

workspace_mock.dataY.side_effect = lambda i: actual_data_y[i]
workspace_mock.dataX.side_effect = lambda i: actual_data_x[i]
workspace_mock.dataE.side_effect = lambda i: actual_data_e[i]

workspace_mock.getNumberHistograms.return_value = 3
mask_time_of_flight_bins_with_zeros(workspace_mock, '4.5-7.3')

np.testing.assert_allclose(actual_data_x, data_x)
np.testing.assert_allclose(actual_data_e, data_e)
expected_data_y = np.ones((3, 10))
expected_data_y[(data_x >= 4.5) & (data_x <= 7.3)] = 0
np.testing.assert_allclose(actual_data_y, expected_data_y)


if __name__ == "__main__":
unittest.main()

0 comments on commit fb567f0

Please sign in to comment.