Skip to content

Ewm6378 fix full masking in reduction #479

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

Merged
merged 9 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions src/snapred/backend/error/ContinueWarning.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Type(Flag):
MISSING_NORMALIZATION = auto()
LOW_PEAK_COUNT = auto()
NO_WRITE_PERMISSIONS = auto()
FULLY_MASKED_GROUP = auto()

class Model(BaseModel):
message: str
Expand Down
25 changes: 24 additions & 1 deletion src/snapred/backend/recipe/ReductionRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@
self.groceries["normalizationWorkspace"] = normalizationClone
return sampleClone, normalizationClone

def _isGroupFullyMasked(self, groupingWorkspace: str) -> bool:
maskWorkspace = self.mantidSnapper.mtd[self.maskWs]
groupWorkspace = self.mantidSnapper.mtd[groupingWorkspace]

totalMaskedPixels = 0
totalGroupPixels = 0

for i in range(groupWorkspace.getNumberHistograms()):
group_spectra = groupWorkspace.readY(i)
for spectrumIndex in group_spectra:
if maskWorkspace.readY(int(spectrumIndex))[0] == 1:
totalMaskedPixels += 1

Check warning on line 157 in src/snapred/backend/recipe/ReductionRecipe.py

View check run for this annotation

Codecov / codecov/patch

src/snapred/backend/recipe/ReductionRecipe.py#L157

Added line #L157 was not covered by tests
totalGroupPixels += 1
return totalMaskedPixels == totalGroupPixels

def queueAlgos(self):
pass

Expand Down Expand Up @@ -172,7 +187,15 @@
for groupingIndex, groupingWs in enumerate(self.groupingWorkspaces):
self.groceries["groupingWorkspace"] = groupingWs

# Clone
if self.maskWs and self._isGroupFullyMasked(groupingWs):
# Notify the user of a fully masked group, but continue with the workflow
self.logger().warning(

Check warning on line 192 in src/snapred/backend/recipe/ReductionRecipe.py

View check run for this annotation

Codecov / codecov/patch

src/snapred/backend/recipe/ReductionRecipe.py#L192

Added line #L192 was not covered by tests
f"\nAll pixels masked within {groupingWs} schema.\n"
+ "Skipping all algorithm execution for this group.\n"
+ "This will affect future reductions."
)
continue

Check warning on line 197 in src/snapred/backend/recipe/ReductionRecipe.py

View check run for this annotation

Codecov / codecov/patch

src/snapred/backend/recipe/ReductionRecipe.py#L197

Added line #L197 was not covered by tests

sampleClone, normalizationClone = self._prepGroupingWorkspaces(groupingIndex)

# 2. ReductionGroupProcessingRecipe
Expand Down
10 changes: 10 additions & 0 deletions src/snapred/ui/handler/SNAPResponseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ def _handleContinueWarning(continueInfo: ContinueWarning.Model, view):

traceback.print_stack()

if ContinueWarning.Type.FULLY_MASKED_GROUP in continueInfo.flags:
QMessageBox.information(
view,
"Warning",
continueInfo.message,
buttons=QMessageBox.Ok,
defaultButton=QMessageBox.Ok,
)
return True

continueAnyway = QMessageBox.warning(
view,
"Warning",
Expand Down
16 changes: 8 additions & 8 deletions tests/cis_tests/diffcal_masking_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@
maskSpectra,
setGroupSpectraToZero,
maskGroups,
pause
)
from util.IPTS_override import datasearch_directories

## If required: override the IPTS search directories: ##
instrumentHome = "/mnt/R5_data1/data1/workspaces/ORNL-work/SNAPRed/SNS_root/SNAP"
instrumentHome = "/SNS/SNAP"
ConfigService.Instance().setDataSearchDirs(datasearch_directories(instrumentHome))
Config._config["instrument"]["home"] = instrumentHome + os.sep
########################################################
Expand All @@ -88,8 +87,8 @@
#User input ###########################
runNumber = "58882"
groupingScheme = 'Column'
cifPath = f"{instrumentHome}/shared/Calibration/CalibrantSamples/Silicon_NIST_640d.cif"
calibrantSamplePath = f"{instrumentHome}/shared/Calibration/CalibrationSamples/Silicon_NIST_640D_001.json"
cifPath = "/home/dzj/Calibration_next/CalibrantSamples/cif/Silicon_NIST_640d.cif"
calibrantSamplePath = "/home/dzj/Calibration_next/CalibrantSamples/Silicon_NIST_001.json"
peakThreshold = 0.05
offsetConvergenceLimit = 0.1
isLite = True
Expand All @@ -105,7 +104,6 @@
focusGroups=[{"name": groupingScheme, "definition": ""}],
cifPath=cifPath,
calibrantSamplePath=calibrantSamplePath,
peakIntensityThresold=peakThreshold,
convergenceThreshold=offsetConvergenceLimit,
maxOffset=100.0,
)
Expand All @@ -127,10 +125,12 @@

### Here any specific spectra or isolated detectors can be masked in the input, if required for testing...
# ---
# maskWS = mtd[maskWSName]
# inputWS = mtd[inputWSName]
# groupingWS = mtd[groupingWSName]
maskWS = mtd[maskWSName]
inputWS = mtd[inputWSName]
groupingWS = mtd[groupingWSName]

allSpectra = list(range(inputWS.getNumberHistograms()))
maskSpectra(maskWS, inputWS, allSpectra)
# # mask all detectors contributing to spectra 10, 20, and 30:
# spectraToMask = (10, 20, 30)
# maskSpectra(maskWS, inputWS, spectraToMask)
Expand Down
53 changes: 45 additions & 8 deletions tests/unit/backend/recipe/test_ReductionRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,25 @@ def test_cloneAndConvertWorkspace(self):
with pytest.raises(ValueError, match=r"cannot convert to unit.*"):
recipe._cloneAndConvertWorkspace(workspace, units)

def test_keepUnfocusedData(self):
# Prepare recipe for testing
@mock.patch("mantid.simpleapi.mtd", create=True)
def test_keepUnfocusedData(self, mockMtd):
mockMantidSnapper = mock.Mock()

mockMaskWorkspace = mock.Mock()
mockGroupWorkspace = mock.Mock()

mockGroupWorkspace.getNumberHistograms.return_value = 10
mockGroupWorkspace.readY.return_value = [0] * 10
mockMaskWorkspace.readY.return_value = [0] * 10

# Mock mtd to return mask and group workspaces
mockMtd.__getitem__.side_effect = lambda ws_name: mockMaskWorkspace if ws_name == "mask" else mockGroupWorkspace
recipe = ReductionRecipe()
recipe.groceries = {}
recipe.mantidSnapper = mockMantidSnapper
recipe.mantidSnapper.mtd = mockMtd

# Set up ingredients and other variables for the recipe
recipe.groceries = {}
recipe.ingredients = mock.Mock()
recipe.ingredients.groupProcessing = mock.Mock(
return_value=lambda groupingIndex: f"groupProcessing_{groupingIndex}"
Expand All @@ -146,22 +160,26 @@ def test_keepUnfocusedData(self):
return_value=lambda groupingIndex: f"applyNormalization_{groupingIndex}"
)

# Mock internal methods of recipe
recipe._applyRecipe = mock.Mock()
recipe._cloneIntermediateWorkspace = mock.Mock()
recipe._deleteWorkspace = mock.Mock()
recipe._cloneAndConvertWorkspace = mock.Mock()
recipe._prepGroupingWorkspaces = mock.Mock()
recipe._prepGroupingWorkspaces.return_value = ("sample_grouped", "norm_grouped")

# Set up other recipe variables
recipe.sampleWs = "sample"
recipe.maskWs = "mask"
recipe.normalizationWs = "norm"
recipe.groupingWorkspaces = ["group1", "group2"]
recipe.keepUnfocused = True

# Test keeping unfocused data in dSpacing units
recipe.convertUnitsTo = "dSpacing"

# Execute the recipe
result = recipe.execute()

# Assertions
recipe._cloneAndConvertWorkspace.assert_called_once_with("sample", "dSpacing")
assert recipe._deleteWorkspace.call_count == len(recipe._prepGroupingWorkspaces.return_value)
recipe._deleteWorkspace.assert_called_with("norm_grouped")
Expand Down Expand Up @@ -289,12 +307,26 @@ def test_cloneIntermediateWorkspace(self):
mock.ANY, InputWorkspace="input", OutputWorkspace="output"
)

def test_execute(self):
@mock.patch("mantid.simpleapi.mtd", create=True)
def test_execute(self, mockMtd):
mockMantidSnapper = mock.Mock()

mockMaskworkspace = mock.Mock()
mockGroupWorkspace = mock.Mock()

mockGroupWorkspace.getNumberHistograms.return_value = 10
mockGroupWorkspace.readY.return_value = [0] * 10
mockMaskworkspace.readY.return_value = [0] * 10

mockMtd.__getitem__.side_effect = lambda ws_name: mockMaskworkspace if ws_name == "mask" else mockGroupWorkspace

recipe = ReductionRecipe()
recipe.groceries = {}
recipe.mantidSnapper = mockMantidSnapper
recipe.mantidSnapper.mtd = mockMtd

# Set up ingredients and other variables for the recipe
recipe.groceries = {}
recipe.ingredients = mock.Mock()
# recipe.ingredients.preprocess = mock.Mock()
recipe.ingredients.groupProcessing = mock.Mock(
return_value=lambda groupingIndex: f"groupProcessing_{groupingIndex}"
)
Expand All @@ -305,21 +337,26 @@ def test_execute(self):
return_value=lambda groupingIndex: f"applyNormalization_{groupingIndex}"
)

# Mock internal methods of recipe
recipe._applyRecipe = mock.Mock()
recipe._cloneIntermediateWorkspace = mock.Mock()
recipe._deleteWorkspace = mock.Mock()
recipe._cloneAndConvertWorkspace = mock.Mock()
recipe._prepGroupingWorkspaces = mock.Mock()
recipe._prepGroupingWorkspaces.return_value = ("sample_grouped", "norm_grouped")

# Set up other recipe variables
recipe.sampleWs = "sample"
recipe.maskWs = "mask"
recipe.normalizationWs = "norm"
recipe.groupingWorkspaces = ["group1", "group2"]
recipe.keepUnfocused = True
recipe.convertUnitsTo = "TOF"

# Execute the recipe
result = recipe.execute()

# Perform assertions
recipe._applyRecipe.assert_any_call(
PreprocessReductionRecipe,
recipe.ingredients.preprocess(),
Expand Down