Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ewm7115 fix remove event background undersampling #456

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def unbagGroceries(self, groceries: Dict[str, WorkspaceName]) -> None: # noqa A
self.maskWS = groceries["maskWorkspace"]
# the name of the output calibration table
self.DIFCpixel = groceries["calibrationTable"]

self.isEventWs = self.mantidSnapper.mtd[self.wsTOF].id() == "EventWorkspace"
# the input data converted to d-spacing
self.wsDSP = wng.diffCalInputDSP().runNumber(self.runNumber).build()
self.mantidSnapper.ConvertUnits(
Expand Down Expand Up @@ -147,11 +147,12 @@ def stripBackground(self, peaks: List[Any], inputWS: WorkspaceName, groupingWS:
GroupingWorkspace=groupingWS,
DetectorPeaks=peaks,
)
self.mantidSnapper.ConvertToMatrixWorkspace(
"Converting TOF data to MatrixWorkspace...",
InputWorkspace=inputWS,
OutputWorkspace=inputWS,
)
if self.isEventWs:
self.mantidSnapper.ConvertToEventWorkspace(
"Converting TOF data to EventWorkspace...",
InputWorkspace=wsBG,
OutputWorkspace=wsBG,
)
self.mantidSnapper.Minus(
"Subtracting background from input data",
LHSWorkspace=inputWS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,27 @@ def test_reexecution_and_convergence(self):
assert allOffsets[-1] <= self.ingredients.convergenceThreshold

def test_execute_ordered(self):
# produce 4, 2, 1, 0.5
# Mock workspace and check for event workspace using id()
mockWorkspace = mock.MagicMock()
mockWorkspace.id.return_value = "EventWorkspace"

# Use MagicMock to allow item assignment
rx = Recipe()
rx.mantidSnapper = mock.Mock()
rx.mantidSnapper.GroupedDetectorIDs.return_value = {}
rx.mantidSnapper.OffsetStatistics.side_effect = [{"medianOffset": 4 * 2 ** (-i)} for i in range(10)]
rx.mantidSnapper = mock.MagicMock()
rx.mantidSnapper.mtd[self.groceries["inputWorkspace"]] = mockWorkspace

# Mock OffsetStatistics to return different values for "medianOffset" in each iteration
rx.mantidSnapper.OffsetStatistics.side_effect = [
{"medianOffset": 4.0},
{"medianOffset": 2.0},
{"medianOffset": 1.0},
{"medianOffset": 0.5},
]

# Run the cook method
result = rx.cook(self.ingredients, self.groceries)

# Assert the result
assert result.result
assert result.medianOffsets == [4, 2, 1, 0.5]

Expand All @@ -102,28 +117,33 @@ def test_ensure_monotonic(self):
If the median offsets do not converge monotonically, the recipe stops
"""
rx = Recipe()
rx.mantidSnapper = mock.Mock()
rx.mantidSnapper = mock.MagicMock()
rx.mantidSnapper.GroupedDetectorIDs.return_value = {}
rx.mantidSnapper.OffsetStatistics.side_effect = [{"medianOffset": x} for x in [2, 1, 2, 0]]

result = rx.cook(self.ingredients, self.groceries)
assert result.result
assert result.medianOffsets == [2, 1]

def test_hard_cap_at_five(self):
maxIterations = Config["calibration.diffraction.maximumIterations"]
# Mock workspace and check for event workspace using id()
mockWorkspace = mock.MagicMock()
mockWorkspace.id.return_value = "EventWorkspace"

# Mock mtd and ensure it returns the mockWorkspace when accessed
rx = Recipe()
rx.mantidSnapper = mock.Mock()
rx.mantidSnapper = mock.MagicMock() # Use MagicMock here
rx.mantidSnapper.mtd = mock.MagicMock() # Use MagicMock for mtd to support item assignment
rx.mantidSnapper.mtd[self.groceries["inputWorkspace"]] = mockWorkspace

# Simulate maximum iterations and offsets
maxIterations = Config["calibration.diffraction.maximumIterations"]
rx.mantidSnapper.GroupedDetectorIDs.return_value = {}
rx.mantidSnapper.OffsetStatistics.side_effect = [{"medianOffset": x} for x in range(10 * maxIterations, 5, -1)]

# Run the test
result = rx.cook(self.ingredients, self.groceries)
assert result.result
assert result.medianOffsets == list(range(10 * maxIterations, 9 * maxIterations, -1))
# change the config then run again
maxIterations = 7
Config._config["calibration"]["diffraction"]["maximumIterations"] = maxIterations
rx._counts = 0
rx.mantidSnapper.OffsetStatistics.side_effect = [{"medianOffset": x} for x in range(10 * maxIterations, 5, -1)]
result = rx.cook(self.ingredients, self.groceries)

assert result.result
assert result.medianOffsets == list(range(10 * maxIterations, 9 * maxIterations, -1))

Expand Down