Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 15, 2024
1 parent 96fad81 commit 3e766aa
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class EffectiveInstrumentIngredients(BaseModel):

unmaskedPixelGroup: PixelGroup

model_config = ConfigDict(
Expand Down
10 changes: 4 additions & 6 deletions src/snapred/backend/dao/ingredients/ReductionIngredients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
# These are from the same `__init__` module, so for the moment, we require the full import specifications.
# (That is, not just "from snapred.backend.dao.ingredients import ...".)
from snapred.backend.dao.ingredients.ArtificialNormalizationIngredients import ArtificialNormalizationIngredients
from snapred.backend.dao.ingredients.EffectiveInstrumentIngredients import EffectiveInstrumentIngredients
from snapred.backend.dao.ingredients.GenerateFocussedVanadiumIngredients import GenerateFocussedVanadiumIngredients
from snapred.backend.dao.ingredients.PreprocessReductionIngredients import PreprocessReductionIngredients
from snapred.backend.dao.ingredients.ReductionGroupProcessingIngredients import ReductionGroupProcessingIngredients
from snapred.backend.dao.ingredients.EffectiveInstrumentIngredients import EffectiveInstrumentIngredients
from snapred.backend.dao.state.PixelGroup import PixelGroup


Expand Down Expand Up @@ -64,12 +64,10 @@ def applyNormalization(self, groupingIndex: int) -> ApplyNormalizationIngredient
return ApplyNormalizationIngredients(
pixelGroup=self.pixelGroups[groupingIndex],
)

def effectiveInstrument(self, groupingIndex: int) -> EffectiveInstrumentIngredients:
return EffectiveInstrumentIngredients(
unmaskedPixelGroup=self.unmaskedPixelGroups[groupingIndex]
)

return EffectiveInstrumentIngredients(unmaskedPixelGroup=self.unmaskedPixelGroups[groupingIndex])

model_config = ConfigDict(
extra="forbid",
)
23 changes: 11 additions & 12 deletions src/snapred/backend/data/LocalDataService.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,14 @@ def writeReductionData(self, record: ReductionRecord):
Persists the reduction data associated with a `ReductionRecord`
-- `writeReductionRecord` must have been called prior to this method.
"""

# Implementation notes:
#
# 1) For SNAPRed's current reduction-workflow output implementation:
#
# *`SaveNexusESS` _must_ be used, `SaveNexus` by itself won't work;
# *`SaveNexusESS` _must_ be used, `SaveNexus` by itself won't work;
#
# * ONLY a simplified instrument geometry can be saved,
# * ONLY a simplified instrument geometry can be saved,
# for example, as produced by `EditInstrumentGeometry`:
# this geometry includes no monitors, only a single non-nested detector bank, and no parameter map.
#
Expand All @@ -660,7 +660,7 @@ def writeReductionData(self, record: ReductionRecord):
# Hopefully this will eventually be fixed, but right now this is a limitation of Mantid's
# instrument-I/O implementation (for non XML-based instruments).
#
# 2) For SNAPRed internal use:
# 2) For SNAPRed internal use:
# if `reduction.output.useEffectiveInstrument` is set to false in "application.yml",
# output workspaces will be saved without converting their instruments to the reduced form.
# This case is retained to allow some flexibility in what specifically is saved with the reduction data.
Expand All @@ -677,26 +677,25 @@ def writeReductionData(self, record: ReductionRecord):
raise RuntimeError(f"reduction version directories {filePath.parent} do not exist")

useEffectiveInstrument = Config["reduction.output.useEffectiveInstrument"]

for ws in record.workspaceNames:
# Append workspaces to hdf5 file, in order of the `workspaces` list

if ws.tokens("workspaceType") == wngt.REDUCTION_PIXEL_MASK:

# The mask workspace always uses the non-reduced instrument.
self.mantidSnapper.SaveNexus(
f"Append workspace '{ws}' to reduction output",
InputWorkspace=ws,
Filename=str(filePath),
Append=True,
)
)
self.mantidSnapper.executeQueue()

# Write an additional copy of the combined pixel mask as a separate `SaveDiffCal`-format file
maskFilename = ws + ".h5"
self.writePixelMask(filePath.parent, Path(maskFilename), ws)
else:
if useEffectiveInstrument:
if useEffectiveInstrument:
self.mantidSnapper.SaveNexusESS(
f"Append workspace '{ws}' to reduction output",
InputWorkspace=ws,
Expand All @@ -709,9 +708,9 @@ def writeReductionData(self, record: ReductionRecord):
InputWorkspace=ws,
Filename=str(filePath),
Append=True,
)
)
self.mantidSnapper.executeQueue()

# Append the "metadata" group, containing the `ReductionRecord` metadata
with h5py.File(filePath, "a") as h5:
n5m.insertMetadataGroup(h5, record.dict(), "/metadata")
Expand Down
2 changes: 1 addition & 1 deletion src/snapred/backend/error/ContinueWarning.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def flags(self):
return self.model.flags

def __init__(self, message: str, flags: "Type" = 0):
ContinueWarning.Model.model_rebuild(force=True) # replaces: `update_forward_refs` method
ContinueWarning.Model.model_rebuild(force=True) # replaces: `update_forward_refs` method
self.model = ContinueWarning.Model(message=message, flags=flags)
super().__init__(message)

Expand Down
2 changes: 1 addition & 1 deletion src/snapred/backend/error/RecoverableException.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def data(self):
return self.model.data

def __init__(self, message: str, flags: "Type" = 0, data: Optional[Any] = None):
RecoverableException.Model.model_rebuild(force=True) # replaces: `update_forward_refs` method
RecoverableException.Model.model_rebuild(force=True) # replaces: `update_forward_refs` method
self.model = RecoverableException.Model(message=message, flags=flags, data=data)
logger.error(f"{extractTrueStacktrace()}")
super().__init__(message)
Expand Down
13 changes: 6 additions & 7 deletions src/snapred/backend/recipe/EffectiveInstrumentRecipe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Dict, List, Tuple

import numpy as np

from snapred.backend.dao.ingredients import EffectiveInstrumentIngredients as Ingredients
Expand All @@ -11,14 +12,14 @@
logger = snapredLogger.getLogger(__name__)

Pallet = Tuple[Ingredients, Dict[str, str]]



@Singleton
class EffectiveInstrumentRecipe(Recipe[Ingredients]):

def unbagGroceries(self, groceries: Dict[str, Any]):
self.inputWS = groceries["inputWorkspace"]
self.outputWS = groceries.get("outputWorkspace", groceries["inputWorkspace"])

def chopIngredients(self, ingredients):
self.unmaskedPixelGroup = ingredients.unmaskedPixelGroup

Expand All @@ -30,9 +31,7 @@ def queueAlgos(self):
# `EditInstrumentGeometry` modifies in-place, so we need to clone if a distinct output workspace is required.
if self.outputWS != self.inputWS:
self.mantidSnapper.CloneWorkspace(
"Clone workspace for reduced instrument",
OutputWorkspace=self.outputWS,
InputWorkspace=self.inputWS
"Clone workspace for reduced instrument", OutputWorkspace=self.outputWS, InputWorkspace=self.inputWS
)
self.mantidSnapper.EditInstrumentGeometry(
f"Editing instrument geometry for grouping '{self.unmaskedPixelGroup.focusGroup.name}'",
Expand All @@ -42,7 +41,7 @@ def queueAlgos(self):
Polar=np.rad2deg(self.unmaskedPixelGroup.twoTheta),
Azimuthal=np.rad2deg(self.unmaskedPixelGroup.azimuth),
#
InstrumentName=f"SNAP_{self.unmaskedPixelGroup.focusGroup.name}"
InstrumentName=f"SNAP_{self.unmaskedPixelGroup.focusGroup.name}",
)

def validateInputs(self, ingredients: Ingredients, groceries: Dict[str, WorkspaceName]):
Expand Down
6 changes: 3 additions & 3 deletions src/snapred/backend/recipe/ReductionRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from snapred.backend.dao.ingredients import ReductionIngredients as Ingredients
from snapred.backend.log.logger import snapredLogger
from snapred.backend.recipe.ApplyNormalizationRecipe import ApplyNormalizationRecipe
from snapred.backend.recipe.EffectiveInstrumentRecipe import EffectiveInstrumentRecipe
from snapred.backend.recipe.GenerateFocussedVanadiumRecipe import GenerateFocussedVanadiumRecipe
from snapred.backend.recipe.GenericRecipe import ArtificialNormalizationRecipe
from snapred.backend.recipe.PreprocessReductionRecipe import PreprocessReductionRecipe
from snapred.backend.recipe.EffectiveInstrumentRecipe import EffectiveInstrumentRecipe
from snapred.backend.recipe.Recipe import Recipe, WorkspaceName
from snapred.backend.recipe.ReductionGroupProcessingRecipe import ReductionGroupProcessingRecipe
from snapred.meta.Config import Config
from snapred.meta.mantid.WorkspaceNameGenerator import ValueFormatter as wnvf
from snapred.meta.mantid.WorkspaceNameGenerator import WorkspaceNameGenerator as wng
from snapred.meta.Config import Config

logger = snapredLogger.getLogger(__name__)

Expand Down Expand Up @@ -283,7 +283,7 @@ def execute(self):
self.ingredients.effectiveInstrument(groupingIndex),
inputWorkspace=sampleClone,
)

# Cleanup
outputs.append(sampleClone)

Expand Down
10 changes: 6 additions & 4 deletions src/snapred/backend/service/ReductionService.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Dict, List, Optional
import json
from collections.abc import Iterable
from pathlib import Path
from typing import Any, Dict, List, Optional

from snapred.backend.dao.ingredients import (
ArtificialNormalizationIngredients,
Expand Down Expand Up @@ -308,7 +308,9 @@ def prepCombinedMask(
return combinedMask

@FromString
def prepReductionIngredients(self, request: ReductionRequest, combinedPixelMask: Optional[WorkspaceName] = None) -> ReductionIngredients:
def prepReductionIngredients(
self, request: ReductionRequest, combinedPixelMask: Optional[WorkspaceName] = None
) -> ReductionIngredients:
"""
Prepare the needed ingredients for calculating reduction.
Requires:
Expand Down Expand Up @@ -387,7 +389,7 @@ def fetchReductionGroceries(self, request: ReductionRequest) -> Dict[str, Any]:
raise RuntimeError(
f"reduction pixel mask '{mask}' has unexpected workspace-type '{mask.tokens('workspaceType')}'" # noqa: E501
)
if calVersion is not None: # WARNING: version may be _zero_!
if calVersion is not None: # WARNING: version may be _zero_!
self.groceryClerk.name("diffcalMaskWorkspace").diffcal_mask(request.runNumber, calVersion).useLiteMode(
request.useLiteMode
).add()
Expand All @@ -409,7 +411,7 @@ def fetchReductionGroceries(self, request: ReductionRequest) -> Dict[str, Any]:
request.useLiteMode
).add()

if normVersion is not None: # WARNING: version may be _zero_!
if normVersion is not None: # WARNING: version may be _zero_!
self.groceryClerk.name("normalizationWorkspace").normalization(request.runNumber, normVersion).useLiteMode(
request.useLiteMode
).add()
Expand Down
21 changes: 12 additions & 9 deletions src/snapred/backend/service/SousChef.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List, Optional, Tuple
import os
from copy import deepcopy
from pathlib import Path
from typing import Dict, List, Optional, Tuple

import pydantic

Expand Down Expand Up @@ -29,9 +29,9 @@
from snapred.backend.recipe.PixelGroupingParametersCalculationRecipe import PixelGroupingParametersCalculationRecipe
from snapred.backend.service.CrystallographicInfoService import CrystallographicInfoService
from snapred.backend.service.Service import Service
from snapred.meta.mantid.WorkspaceNameGenerator import WorkspaceName
from snapred.meta.Config import Config
from snapred.meta.decorators.Singleton import Singleton
from snapred.meta.mantid.WorkspaceNameGenerator import WorkspaceName

logger = snapredLogger.getLogger(__name__)

Expand Down Expand Up @@ -95,7 +95,9 @@ def prepFocusGroup(self, ingredients: FarmFreshIngredients) -> FocusGroup:
groupingMap = self.dataFactoryService.getGroupingMap(ingredients.runNumber)
return groupingMap.getMap(ingredients.useLiteMode)[ingredients.focusGroup.name]

def prepPixelGroup(self, ingredients: FarmFreshIngredients, pixelMask: Optional[WorkspaceName] = None) -> PixelGroup:
def prepPixelGroup(
self, ingredients: FarmFreshIngredients, pixelMask: Optional[WorkspaceName] = None
) -> PixelGroup:
groupingSchema = ingredients.focusGroup.name
key = (ingredients.runNumber, ingredients.useLiteMode, groupingSchema, pixelMask)
if key not in self._pixelGroupCache:
Expand All @@ -108,10 +110,7 @@ def prepPixelGroup(self, ingredients: FarmFreshIngredients, pixelMask: Optional[
self.groceryClerk.name("groupingWorkspace").fromRun(ingredients.runNumber).grouping(
focusGroup.name
).useLiteMode(ingredients.useLiteMode).add()
groceries = self.groceryService.fetchGroceryDict(
self.groceryClerk.buildDict(),
maskWorkspace=pixelMask
)
groceries = self.groceryService.fetchGroceryDict(self.groceryClerk.buildDict(), maskWorkspace=pixelMask)
data = PixelGroupingParametersCalculationRecipe().executeRecipe(pixelIngredients, groceries)

self._pixelGroupCache[key] = PixelGroup(
Expand All @@ -122,7 +121,9 @@ def prepPixelGroup(self, ingredients: FarmFreshIngredients, pixelMask: Optional[
)
return deepcopy(self._pixelGroupCache[key])

def prepManyPixelGroups(self, ingredients: FarmFreshIngredients, pixelMask: Optional[WorkspaceName] = None) -> List[PixelGroup]:
def prepManyPixelGroups(
self, ingredients: FarmFreshIngredients, pixelMask: Optional[WorkspaceName] = None
) -> List[PixelGroup]:
pixelGroups = []
ingredients_ = ingredients.model_copy()
for focusGroup in ingredients.focusGroups:
Expand Down Expand Up @@ -248,7 +249,9 @@ def _pullNormalizationRecordFFI(
# TODO: Should smoothing parameter be an ingredient?
return ingredients, smoothingParameter, calibrantSamplePath

def prepReductionIngredients(self, ingredients: FarmFreshIngredients, combinedPixelMask: Optional[WorkspaceName] = None) -> ReductionIngredients:
def prepReductionIngredients(
self, ingredients: FarmFreshIngredients, combinedPixelMask: Optional[WorkspaceName] = None
) -> ReductionIngredients:
ingredients_ = ingredients.model_copy()
# some of the reduction ingredients MUST match those used in the calibration/normalization processes
ingredients_ = self._pullCalibrationRecordFFI(ingredients_)
Expand Down
2 changes: 1 addition & 1 deletion src/snapred/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ reduction:
extension: .nxs
# convert the instrument for the output workspaces into the reduced form
useEffectiveInstrument: true

mantid:
workspace:
nameTemplate:
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ reduction:
extension: .nxs
# convert the instrument for the output workspaces into the reduced form
useEffectiveInstrument: true

mantid:
workspace:
nameTemplate:
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/backend/data/test_DataFactoryService.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import hashlib
import time
import unittest
import unittest.mock as mock
from pathlib import Path
from random import randint
import time

from mantid.simpleapi import CreateSingleValuedWorkspace, DeleteWorkspace, mtd
from snapred.backend.dao.calibration import Calibration
Expand All @@ -13,8 +15,6 @@
from snapred.backend.data.DataFactoryService import DataFactoryService
from snapred.backend.data.LocalDataService import LocalDataService

import unittest
import unittest.mock as mock

class TestDataFactoryService(unittest.TestCase):
def expected(cls, *args):
Expand Down
Loading

0 comments on commit 3e766aa

Please sign in to comment.