diff --git a/src/snapred/backend/recipe/algorithm/PixelDiffractionCalibration.py b/src/snapred/backend/recipe/algorithm/PixelDiffractionCalibration.py index 98b670141..59277577d 100644 --- a/src/snapred/backend/recipe/algorithm/PixelDiffractionCalibration.py +++ b/src/snapred/backend/recipe/algorithm/PixelDiffractionCalibration.py @@ -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( @@ -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, diff --git a/tests/unit/backend/recipe/algorithm/test_PixelDiffractionCalibration.py b/tests/unit/backend/recipe/algorithm/test_PixelDiffractionCalibration.py index d1aa4828d..4320f6447 100644 --- a/tests/unit/backend/recipe/algorithm/test_PixelDiffractionCalibration.py +++ b/tests/unit/backend/recipe/algorithm/test_PixelDiffractionCalibration.py @@ -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] @@ -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))