diff --git a/src/snapred/backend/recipe/ApplyNormalizationRecipe.py b/src/snapred/backend/recipe/ApplyNormalizationRecipe.py index ffa568a67..25d467d9b 100644 --- a/src/snapred/backend/recipe/ApplyNormalizationRecipe.py +++ b/src/snapred/backend/recipe/ApplyNormalizationRecipe.py @@ -23,8 +23,19 @@ def chopIngredients(self, ingredients: Ingredients): We are mostly concerned about the drange for a ResampleX operation. """ self.pixelGroup = ingredients.pixelGroup - self.dMin = self.pixelGroup.dMin() - self.dMax = self.pixelGroup.dMax() + # The adjustment below is a temp fix, will be permanently fixed in EWM 6262 + lowdSpacingCrop = Config["constants.CropFactors.lowdSpacingCrop"] + if lowdSpacingCrop < 0: + raise ValueError("Low d-spacing crop factor must be positive") + highdSpacingCrop = Config["constants.CropFactors.highdSpacingCrop"] + if highdSpacingCrop < 0: + raise ValueError("High d-spacing crop factor must be positive") + dMin = [x + lowdSpacingCrop for x in self.pixelGroup.dMin()] + dMax = [x - highdSpacingCrop for x in self.pixelGroup.dMax()] + if not dMax > dMin: + raise ValueError("d-spacing crop factors are too large -- resultant dMax must be > resultant dMin") + self.dMin = dMin + self.dMax = dMax def unbagGroceries(self, groceries: Dict[str, WorkspaceName]): """ diff --git a/src/snapred/resources/application.yml b/src/snapred/resources/application.yml index d5ff2c623..fb53d49a4 100644 --- a/src/snapred/resources/application.yml +++ b/src/snapred/resources/application.yml @@ -166,6 +166,8 @@ constants: CropFactors: lowWavelengthCrop: 0.05 + lowdSpacingCrop: 0.1 + highdSpacingCrop: 0.15 docs: user: diff --git a/tests/resources/application.yml b/tests/resources/application.yml index c95a74f01..78c5a70f8 100644 --- a/tests/resources/application.yml +++ b/tests/resources/application.yml @@ -187,6 +187,8 @@ constants: CropFactors: lowWavelengthCrop: 0.05 + lowdSpacingCrop: 0.1 + highdSpacingCrop: 0.15 metadata: tagPrefix: testSNAPfuntime_ diff --git a/tests/unit/backend/recipe/test_ApplyNormalizationRecipe.py b/tests/unit/backend/recipe/test_ApplyNormalizationRecipe.py index ebc3bf5db..d496a5ca9 100644 --- a/tests/unit/backend/recipe/test_ApplyNormalizationRecipe.py +++ b/tests/unit/backend/recipe/test_ApplyNormalizationRecipe.py @@ -4,6 +4,8 @@ from mantid.simpleapi import CreateSingleValuedWorkspace, mtd from snapred.backend.recipe.algorithm.Utensils import Utensils from snapred.backend.recipe.ApplyNormalizationRecipe import ApplyNormalizationRecipe, Ingredients +from snapred.meta.Config import Config +from util.Config_helpers import Config_override from util.SculleryBoy import SculleryBoy @@ -35,8 +37,11 @@ def test_chopIngredients(self): ingredients = Ingredients(pixelGroup=self.sculleryBoy.prepPixelGroup()) recipe.chopIngredients(ingredients) assert recipe.pixelGroup == ingredients.pixelGroup - assert recipe.dMin == ingredients.pixelGroup.dMin() - assert recipe.dMax == ingredients.pixelGroup.dMax() + # Apply adjustment that happens in chopIngredients step + dMin = [x + Config["constants.CropFactors.lowdSpacingCrop"] for x in ingredients.pixelGroup.dMin()] + dMax = [x - Config["constants.CropFactors.highdSpacingCrop"] for x in ingredients.pixelGroup.dMax()] + assert recipe.dMin == dMin + assert recipe.dMax == dMax def test_unbagGroceries(self): recipe = ApplyNormalizationRecipe() @@ -97,8 +102,11 @@ def test_queueAlgos(self): assert divideTuple[2]["LHSWorkspace"] == groceries["inputWorkspace"] assert divideTuple[2]["RHSWorkspace"] == groceries["normalizationWorkspace"] assert resampleTuple[2]["InputWorkspace"] == groceries["inputWorkspace"] - assert resampleTuple[2]["XMin"] == ingredients.pixelGroup.dMin() - assert resampleTuple[2]["XMax"] == ingredients.pixelGroup.dMax() + # Apply adjustment that happens in chopIngredients step + dMin = [x + Config["constants.CropFactors.lowdSpacingCrop"] for x in ingredients.pixelGroup.dMin()] + dMax = [x - Config["constants.CropFactors.highdSpacingCrop"] for x in ingredients.pixelGroup.dMax()] + assert resampleTuple[2]["XMin"] == dMin + assert resampleTuple[2]["XMax"] == dMax def test_cook(self): untensils = Utensils() @@ -137,3 +145,25 @@ def test_cater(self): assert mockSnapper.executeQueue.called assert mockSnapper.Divide.called assert mockSnapper.ResampleX.called + + def test_badChopIngredients(self): + recipe = ApplyNormalizationRecipe() + ingredients = Ingredients(pixelGroup=self.sculleryBoy.prepPixelGroup()) + with ( + Config_override("constants.CropFactors.lowdSpacingCrop", 500.0), + Config_override("constants.CropFactors.highdSpacingCrop", 1000.0), + pytest.raises(ValueError, match="d-spacing crop factors are too large"), + ): + recipe.chopIngredients(ingredients) + + with ( + Config_override("constants.CropFactors.lowdSpacingCrop", -10.0), + pytest.raises(ValueError, match="Low d-spacing crop factor must be positive"), + ): + recipe.chopIngredients(ingredients) + + with ( + Config_override("constants.CropFactors.highdSpacingCrop", -10.0), + pytest.raises(ValueError, match="High d-spacing crop factor must be positive"), + ): + recipe.chopIngredients(ingredients) diff --git a/tests/util/SculleryBoy.py b/tests/util/SculleryBoy.py index 260083c96..ba31cbb43 100644 --- a/tests/util/SculleryBoy.py +++ b/tests/util/SculleryBoy.py @@ -14,6 +14,7 @@ PeakIngredients, ReductionIngredients, ) +from snapred.backend.dao.Limit import Limit from snapred.backend.dao.request.FarmFreshIngredients import FarmFreshIngredients from snapred.backend.dao.RunConfig import RunConfig from snapred.backend.dao.state.CalibrantSample import ( @@ -25,6 +26,7 @@ ) from snapred.backend.dao.state.FocusGroup import FocusGroup from snapred.backend.dao.state.PixelGroup import PixelGroup +from snapred.backend.dao.state.PixelGroupingParameters import PixelGroupingParameters from snapred.backend.recipe.GenericRecipe import DetectorPeakPredictorRecipe from snapred.meta.Config import Resource @@ -70,8 +72,18 @@ def prepFocusGroup(self, ingredients: FarmFreshIngredients) -> FocusGroup: # no ) def prepPixelGroup(self, ingredients: FarmFreshIngredients = None): # noqa ARG002 + params = PixelGroupingParameters( + groupID=1, + isMasked=False, + L2=10.0, + twoTheta=3.14, + azimuth=0.0, + dResolution=Limit(minimum=0.1, maximum=1.0), + dRelativeResolution=0.01, + ) + return PixelGroup( - pixelGroupingParameters=[], + pixelGroupingParameters=[params], nBinsAcrossPeakWidth=7, focusGroup=self.prepFocusGroup(ingredients), timeOfFlight={"minimum": 0.001, "maximum": 100, "binWidth": 1, "binnindMode": -1},