From 22323578684e411fdf4b90b10ba78b123acdfc3b Mon Sep 17 00:00:00 2001 From: Kort Travis Date: Mon, 28 Oct 2024 13:01:27 -0400 Subject: [PATCH 1/2] reduction: apply pixelmask to unfocused data This commit includes the following changes: * If a pixel mask exists, and the unfocused data is being retained, apply the mask to the unfocussed data. --- src/snapred/backend/recipe/ReductionRecipe.py | 28 +++++--- .../backend/recipe/test_ReductionRecipe.py | 65 ++++++++++++++----- 2 files changed, 65 insertions(+), 28 deletions(-) diff --git a/src/snapred/backend/recipe/ReductionRecipe.py b/src/snapred/backend/recipe/ReductionRecipe.py index 0eccc4d2e..fb679cd06 100644 --- a/src/snapred/backend/recipe/ReductionRecipe.py +++ b/src/snapred/backend/recipe/ReductionRecipe.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Set, Tuple, Type +from typing import Any, Dict, List, Optional, Set, Tuple, Type from snapred.backend.dao.ingredients import ReductionIngredients as Ingredients from snapred.backend.log.logger import snapredLogger @@ -87,7 +87,7 @@ def _deleteWorkspace(self, workspace: str): ) self.mantidSnapper.executeQueue() - def _cloneAndConvertWorkspace(self, workspace: WorkspaceName, units: str) -> WorkspaceName: + def _prepareUnfocusedData(self, workspace: WorkspaceName, mask: Optional[WorkspaceName], units: str) -> WorkspaceName: unitsAbrev = "" match units: case "Wavelength": @@ -102,17 +102,24 @@ def _cloneAndConvertWorkspace(self, workspace: WorkspaceName, units: str) -> Wor raise ValueError(f"cannot convert to unit '{units}'") runNumber, liteMode = workspace.tokens("runNumber", "lite") - self.unfocWS = wng.run().runNumber(runNumber).lite(liteMode).unit(unitsAbrev).group(wng.Groups.UNFOC).build() - self._cloneWorkspace(workspace, self.unfocWS) - + self.unfocWs = wng.run().runNumber(runNumber).lite(liteMode).unit(unitsAbrev).group(wng.Groups.UNFOC).build() + self._cloneWorkspace(workspace, self.unfocWs) + + if mask: + self.mantidSnapper.MaskDetectorFlags( + "Applying pixel mask to unfocused data", + MaskWorkspace=mask, + OutputWorkspace=self.unfocWs, + ) + self.mantidSnapper.ConvertUnits( - f"Convert unfocused workspace to {units} units", - InputWorkspace=workspace, - OutputWorkspace=self.unfocWS, + f"Converting unfocused data to {units} units", + InputWorkspace=self.unfocWs, + OutputWorkspace=self.unfocWs, Target=units, ) self.mantidSnapper.executeQueue() - return self.unfocWS + return self.unfocWs def _applyRecipe(self, recipe: Type[Recipe], ingredients_, **kwargs): if "inputWorkspace" in kwargs: @@ -164,8 +171,9 @@ def queueAlgos(self): def execute(self): data: Dict[str, Any] = {"result": False} + # Retain unfocused data for comparison. if self.keepUnfocused: - data["unfocusedWS"] = self._cloneAndConvertWorkspace(self.sampleWs, self.convertUnitsTo) + data["unfocusedWS"] = self._prepareUnfocusedData(self.sampleWs, self.maskWs, self.convertUnitsTo) # 1. PreprocessReductionRecipe outputs = [] diff --git a/tests/unit/backend/recipe/test_ReductionRecipe.py b/tests/unit/backend/recipe/test_ReductionRecipe.py index 8d1627c89..b51956843 100644 --- a/tests/unit/backend/recipe/test_ReductionRecipe.py +++ b/tests/unit/backend/recipe/test_ReductionRecipe.py @@ -80,55 +80,84 @@ def test_deleteWorkspace(self): recipe.mantidSnapper.DeleteWorkspace.assert_called_once_with(mock.ANY, Workspace=workspace) recipe.mantidSnapper.executeQueue.assert_called() - def test_cloneAndConvertWorkspace(self): + def test_prepareUnfocusedData(self): recipe = ReductionRecipe() recipe.mantidSnapper = mock.Mock() workspace = wng.run().runNumber("555").lite(True).build() # TODO: All units are tested here for now, this will be changed when EWM 6615 is completed units = "dSpacing" - msg = f"Convert unfocused workspace to {units} units" - recipe._cloneAndConvertWorkspace(workspace, units) + msg = f"Converting unfocused data to {units} units" + recipe._prepareUnfocusedData(workspace, None, units) outputWs = wng.run().runNumber("555").lite(True).unit(wng.Units.DSP).group(wng.Groups.UNFOC).build() recipe.mantidSnapper.ConvertUnits.assert_called_once_with( - msg, InputWorkspace=workspace, OutputWorkspace=outputWs, Target=units + msg, InputWorkspace=outputWs, OutputWorkspace=outputWs, Target=units ) recipe.mantidSnapper.executeQueue.assert_called() recipe.mantidSnapper.reset_mock() units = "MomentumTransfer" - msg = f"Convert unfocused workspace to {units} units" - recipe._cloneAndConvertWorkspace(workspace, units) + msg = f"Converting unfocused data to {units} units" + recipe._prepareUnfocusedData(workspace, None, units) outputWs = wng.run().runNumber("555").lite(True).unit(wng.Units.QSP).group(wng.Groups.UNFOC).build() recipe.mantidSnapper.ConvertUnits.assert_called_once_with( - msg, InputWorkspace=workspace, OutputWorkspace=outputWs, Target=units + msg, InputWorkspace=outputWs, OutputWorkspace=outputWs, Target=units ) recipe.mantidSnapper.executeQueue.assert_called() recipe.mantidSnapper.reset_mock() units = "Wavelength" - msg = f"Convert unfocused workspace to {units} units" - recipe._cloneAndConvertWorkspace(workspace, units) + msg = f"Converting unfocused data to {units} units" + recipe._prepareUnfocusedData(workspace, None, units) outputWs = wng.run().runNumber("555").lite(True).unit(wng.Units.LAM).group(wng.Groups.UNFOC).build() recipe.mantidSnapper.ConvertUnits.assert_called_once_with( - msg, InputWorkspace=workspace, OutputWorkspace=outputWs, Target=units + msg, InputWorkspace=outputWs, OutputWorkspace=outputWs, Target=units ) recipe.mantidSnapper.executeQueue.assert_called() recipe.mantidSnapper.reset_mock() units = "TOF" - msg = f"Convert unfocused workspace to {units} units" - recipe._cloneAndConvertWorkspace(workspace, units) + msg = f"Converting unfocused data to {units} units" + recipe._prepareUnfocusedData(workspace, None, units) outputWs = wng.run().runNumber("555").lite(True).unit(wng.Units.TOF).group(wng.Groups.UNFOC).build() recipe.mantidSnapper.ConvertUnits.assert_called_once_with( - msg, InputWorkspace=workspace, OutputWorkspace=outputWs, Target=units + msg, InputWorkspace=outputWs, OutputWorkspace=outputWs, Target=units ) recipe.mantidSnapper.executeQueue.assert_called() recipe.mantidSnapper.reset_mock() units = "NOT_A_UNIT" with pytest.raises(ValueError, match=r"cannot convert to unit.*"): - recipe._cloneAndConvertWorkspace(workspace, units) + recipe._prepareUnfocusedData(workspace, None, units) + + def test_prepareUnfocusedData_masking(self): + recipe = ReductionRecipe() + recipe.mantidSnapper = mock.Mock() + + units = "dSpacing" + workspace = wng.run().runNumber("555").lite(True).build() + maskWs = wng.reductionUserPixelMask().numberTag(2).build() + outputWs = wng.run().runNumber("555").lite(True).unit(wng.Units.DSP).group(wng.Groups.UNFOC).build() + + recipe._prepareUnfocusedData(workspace, None, units) + recipe.mantidSnapper.MaskDetectorFlags.assert_not_called() + recipe.mantidSnapper.reset_mock() + + recipe._prepareUnfocusedData(workspace, maskWs, units) + + recipe.mantidSnapper.MaskDetectorFlags.assert_called_once_with( + "Applying pixel mask to unfocused data", + MaskWorkspace=maskWs, + OutputWorkspace=outputWs + ) + recipe.mantidSnapper.ConvertUnits.assert_called_once_with( + f"Converting unfocused data to {units} units", + InputWorkspace=outputWs, + OutputWorkspace=outputWs, + Target=units + ) + recipe.mantidSnapper.executeQueue.assert_called() + recipe.mantidSnapper.reset_mock() @mock.patch("mantid.simpleapi.mtd", create=True) def test_keepUnfocusedData(self, mockMtd): @@ -164,7 +193,7 @@ def test_keepUnfocusedData(self, mockMtd): recipe._applyRecipe = mock.Mock() recipe._cloneIntermediateWorkspace = mock.Mock() recipe._deleteWorkspace = mock.Mock() - recipe._cloneAndConvertWorkspace = mock.Mock() + recipe._prepareUnfocusedData = mock.Mock() recipe._prepGroupingWorkspaces = mock.Mock() recipe._prepGroupingWorkspaces.return_value = ("sample_grouped", "norm_grouped") @@ -180,7 +209,7 @@ def test_keepUnfocusedData(self, mockMtd): result = recipe.execute() # Assertions - recipe._cloneAndConvertWorkspace.assert_called_once_with("sample", "dSpacing") + recipe._prepareUnfocusedData.assert_called_once_with("sample", "mask", "dSpacing") assert recipe._deleteWorkspace.call_count == len(recipe._prepGroupingWorkspaces.return_value) recipe._deleteWorkspace.assert_called_with("norm_grouped") assert result["outputs"][0] == "sample_grouped" @@ -341,7 +370,7 @@ def test_execute(self, mockMtd): recipe._applyRecipe = mock.Mock() recipe._cloneIntermediateWorkspace = mock.Mock() recipe._deleteWorkspace = mock.Mock() - recipe._cloneAndConvertWorkspace = mock.Mock() + recipe._prepareUnfocusedData = mock.Mock() recipe._prepGroupingWorkspaces = mock.Mock() recipe._prepGroupingWorkspaces.return_value = ("sample_grouped", "norm_grouped") @@ -480,7 +509,7 @@ def test_execute_with_fully_masked_group(self, mockMtd): recipe._applyRecipe = mock.Mock() recipe._cloneIntermediateWorkspace = mock.Mock() recipe._deleteWorkspace = mock.Mock() - recipe._cloneAndConvertWorkspace = mock.Mock() + recipe._prepareUnfocusedData = mock.Mock() recipe._prepGroupingWorkspaces = mock.Mock() recipe._prepGroupingWorkspaces.return_value = ("sample_grouped", "norm_grouped") From 41023a8e743ae9f405391032f562a8beea392f98 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:16:27 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/snapred/backend/recipe/ReductionRecipe.py | 6 ++++-- tests/unit/backend/recipe/test_ReductionRecipe.py | 8 +++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/snapred/backend/recipe/ReductionRecipe.py b/src/snapred/backend/recipe/ReductionRecipe.py index fb679cd06..7d38a264e 100644 --- a/src/snapred/backend/recipe/ReductionRecipe.py +++ b/src/snapred/backend/recipe/ReductionRecipe.py @@ -87,7 +87,9 @@ def _deleteWorkspace(self, workspace: str): ) self.mantidSnapper.executeQueue() - def _prepareUnfocusedData(self, workspace: WorkspaceName, mask: Optional[WorkspaceName], units: str) -> WorkspaceName: + def _prepareUnfocusedData( + self, workspace: WorkspaceName, mask: Optional[WorkspaceName], units: str + ) -> WorkspaceName: unitsAbrev = "" match units: case "Wavelength": @@ -111,7 +113,7 @@ def _prepareUnfocusedData(self, workspace: WorkspaceName, mask: Optional[Workspa MaskWorkspace=mask, OutputWorkspace=self.unfocWs, ) - + self.mantidSnapper.ConvertUnits( f"Converting unfocused data to {units} units", InputWorkspace=self.unfocWs, diff --git a/tests/unit/backend/recipe/test_ReductionRecipe.py b/tests/unit/backend/recipe/test_ReductionRecipe.py index b51956843..e7c8472e3 100644 --- a/tests/unit/backend/recipe/test_ReductionRecipe.py +++ b/tests/unit/backend/recipe/test_ReductionRecipe.py @@ -142,19 +142,17 @@ def test_prepareUnfocusedData_masking(self): recipe._prepareUnfocusedData(workspace, None, units) recipe.mantidSnapper.MaskDetectorFlags.assert_not_called() recipe.mantidSnapper.reset_mock() - + recipe._prepareUnfocusedData(workspace, maskWs, units) recipe.mantidSnapper.MaskDetectorFlags.assert_called_once_with( - "Applying pixel mask to unfocused data", - MaskWorkspace=maskWs, - OutputWorkspace=outputWs + "Applying pixel mask to unfocused data", MaskWorkspace=maskWs, OutputWorkspace=outputWs ) recipe.mantidSnapper.ConvertUnits.assert_called_once_with( f"Converting unfocused data to {units} units", InputWorkspace=outputWs, OutputWorkspace=outputWs, - Target=units + Target=units, ) recipe.mantidSnapper.executeQueue.assert_called() recipe.mantidSnapper.reset_mock()