Skip to content

Commit

Permalink
Ewm6383 truncate edges staging (#434)
Browse files Browse the repository at this point in the history
* Adjust calculation of dMin/Max in ApplyNormalization

Updated high dSpacing crop value

Added check to make sure dMax is greater than dMin

Fixed unit tests

Added test for coverage

* Added checks and tests for application.yml values
  • Loading branch information
dlcaballero16 authored Aug 5, 2024
1 parent 2b9051a commit e903c1b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/snapred/backend/recipe/ApplyNormalizationRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
"""
Expand Down
2 changes: 2 additions & 0 deletions src/snapred/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ constants:

CropFactors:
lowWavelengthCrop: 0.05
lowdSpacingCrop: 0.1
highdSpacingCrop: 0.15

docs:
user:
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ constants:

CropFactors:
lowWavelengthCrop: 0.05
lowdSpacingCrop: 0.1
highdSpacingCrop: 0.15

metadata:
tagPrefix: testSNAPfuntime_
38 changes: 34 additions & 4 deletions tests/unit/backend/recipe/test_ApplyNormalizationRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
14 changes: 13 additions & 1 deletion tests/util/SculleryBoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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

Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit e903c1b

Please # to comment.