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 Sep 26, 2024
1 parent 377ae18 commit 104f557
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 56 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
11 changes: 4 additions & 7 deletions src/snapred/backend/dao/ingredients/ReductionIngredients.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

# 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.ApplyNormalizationIngredients import ApplyNormalizationIngredients
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 @@ -61,12 +60,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",
)
4 changes: 2 additions & 2 deletions src/snapred/backend/recipe/EffectiveInstrumentRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 @@ -32,7 +32,7 @@ def queueAlgos(self):
L2=self.unmaskedPixelGroup.L2,
Polar=self.unmaskedPixelGroup.twoTheta,
Azimuthal=self.unmaskedPixelGroup.azimuth,
InstrumentName=f"SNAP_{self.unmaskedPixelGroup.focusGroup.name}"
InstrumentName=f"SNAP_{self.unmaskedPixelGroup.focusGroup.name}",
)
self.outputWS = self.inputWS

Expand Down
4 changes: 2 additions & 2 deletions src/snapred/backend/recipe/ReductionRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
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.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.mantid.WorkspaceNameGenerator import ValueFormatter as wnvf
Expand Down Expand Up @@ -213,7 +213,7 @@ def execute(self):
self.ingredients.effectiveInstrument(groupingIndex),
inputWorkspace=sampleClone,
)

# Cleanup
outputs.append(sampleClone)

Expand Down
6 changes: 4 additions & 2 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 GroceryListItem, ReductionIngredients
from snapred.backend.dao.reduction.ReductionRecord import ReductionRecord
Expand Down Expand Up @@ -257,7 +257,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
21 changes: 12 additions & 9 deletions src/snapred/backend/service/SousChef.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict, List, Optional, Tuple
import os
from pathlib import Path
from typing import Dict, List, Optional, Tuple

import pydantic

Expand Down Expand Up @@ -28,9 +28,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 @@ -94,7 +94,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 @@ -107,10 +109,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 @@ -121,7 +120,9 @@ def prepPixelGroup(self, ingredients: FarmFreshIngredients, pixelMask: Optional[
)
return 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 @@ -242,7 +243,9 @@ def _pullNormalizationRecordFFI(
smoothingParameter = normalizationRecord.smoothingParameter
return ingredients, smoothingParameter

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
41 changes: 17 additions & 24 deletions tests/unit/backend/recipe/test_EffectiveInstrumentRecipe.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from snapred.backend.recipe.algorithm.Utensils import Utensils
from snapred.backend.recipe.EffectiveInstrumentRecipe import EffectiveInstrumentRecipe
from unittest import mock

import pytest
from snapred.backend.dao.ingredients import EffectiveInstrumentIngredients as Ingredients
from snapred.backend.dao.state.FocusGroup import FocusGroup
from snapred.backend.dao.state.PixelGroup import PixelGroup
from snapred.backend.recipe.algorithm.Utensils import Utensils
from snapred.backend.recipe.EffectiveInstrumentRecipe import EffectiveInstrumentRecipe
from snapred.meta.Config import Resource
from util.SculleryBoy import SculleryBoy

from unittest import mock
import pytest

class TestEffectiveInstrumentRecipe:
fakeInstrumentFilePath = Resource.getPath("inputs/testInstrument/fakeSNAP_Definition.xml")
Expand All @@ -22,11 +23,8 @@ def setup(self):
L2=mock.Mock(),
twoTheta=mock.Mock(),
azimuth=mock.Mock(),
focusGroup=FocusGroup(
name="a_grouping",
definition="a/grouping/path"
)
)
focusGroup=FocusGroup(name="a_grouping", definition="a/grouping/path"),
),
)
self.ingredients1 = mock.Mock(
spec=Ingredients,
Expand All @@ -35,20 +33,16 @@ def setup(self):
L2=mock.Mock(),
twoTheta=mock.Mock(),
azimuth=mock.Mock(),
focusGroup=FocusGroup(
name="another_grouping",
definition="another/grouping/path"
)
)
focusGroup=FocusGroup(name="another_grouping", definition="another/grouping/path"),
),
)
self.ingredientss = [self.ingredients, self.ingredients1]



yield

# teardown follows ...
pass

def test_chopIngredients(self):
recipe = EffectiveInstrumentRecipe()
ingredients = self.ingredients
Expand Down Expand Up @@ -78,7 +72,7 @@ def test_queueAlgos(self):

queuedAlgos = recipe.mantidSnapper._algorithmQueue
editInstrumentGeometryTuple = queuedAlgos[0]

assert editInstrumentGeometryTuple[0] == "EditInstrumentGeometry"
assert editInstrumentGeometryTuple[2]["Workspace"] == groceries["inputWorkspace"]

Expand All @@ -101,7 +95,7 @@ def test_cook(self):
L2=ingredients.unmaskedPixelGroup.L2,
Polar=ingredients.unmaskedPixelGroup.twoTheta,
Azimuthal=ingredients.unmaskedPixelGroup.azimuth,
InstrumentName=f"SNAP_{ingredients.unmaskedPixelGroup.focusGroup.name}"
InstrumentName=f"SNAP_{ingredients.unmaskedPixelGroup.focusGroup.name}",
)

def test_cater(self):
Expand All @@ -110,7 +104,7 @@ def test_cater(self):
untensils.mantidSnapper = mockSnapper
recipe = EffectiveInstrumentRecipe(utensils=untensils)
ingredientss = self.ingredientss

groceriess = [{"inputWorkspace": mock.Mock()}, {"inputWorkspace": mock.Mock()}]

output = recipe.cater(zip(ingredientss, groceriess))
Expand All @@ -122,14 +116,13 @@ def test_cater(self):
L2=ingredientss[0].unmaskedPixelGroup.L2,
Polar=ingredientss[0].unmaskedPixelGroup.twoTheta,
Azimuthal=ingredientss[0].unmaskedPixelGroup.azimuth,
InstrumentName=f"SNAP_{ingredientss[0].unmaskedPixelGroup.focusGroup.name}"
InstrumentName=f"SNAP_{ingredientss[0].unmaskedPixelGroup.focusGroup.name}",
)
mockSnapper.EditInstrumentGeometry.assert_any_call(
f"Editing instrument geometry for grouping '{ingredientss[1].unmaskedPixelGroup.focusGroup.name}'",
Workspace=groceriess[1]["inputWorkspace"],
L2=ingredientss[1].unmaskedPixelGroup.L2,
Polar=ingredientss[1].unmaskedPixelGroup.twoTheta,
Azimuthal=ingredientss[1].unmaskedPixelGroup.azimuth,
InstrumentName=f"SNAP_{ingredientss[1].unmaskedPixelGroup.focusGroup.name}"
InstrumentName=f"SNAP_{ingredientss[1].unmaskedPixelGroup.focusGroup.name}",
)

5 changes: 3 additions & 2 deletions tests/unit/backend/recipe/test_PreprocessReductionRecipe.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import unittest

from mantid.simpleapi import (
CreateEmptyTableWorkspace,
CreateSampleWorkspace,
LoadInstrument,
mtd,
)
from snapred.backend.dao.ingredients import PreprocessReductionIngredients as Ingredients
from snapred.backend.recipe.algorithm.Utensils import Utensils
from snapred.backend.recipe.PreprocessReductionRecipe import PreprocessReductionRecipe
from snapred.backend.dao.ingredients import PreprocessReductionIngredients as Ingredients
from snapred.meta.Config import Resource
from util.helpers import createCompatibleMask
from util.SculleryBoy import SculleryBoy

import unittest

class PreprocessReductionRecipeTest(unittest.TestCase):
fakeInstrumentFilePath = Resource.getPath("inputs/testInstrument/fakeSNAP_Definition.xml")
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/backend/recipe/test_ReductionRecipe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time
from unittest import TestCase, mock

import pytest
from mantid.simpleapi import CreateSingleValuedWorkspace, mtd
from snapred.backend.dao.ingredients import ReductionIngredients
from snapred.backend.recipe.ReductionRecipe import (
Expand All @@ -13,8 +15,6 @@
from snapred.meta.mantid.WorkspaceNameGenerator import WorkspaceNameGenerator as wng
from util.SculleryBoy import SculleryBoy

from unittest import TestCase, mock
import pytest

class ReductionRecipeTest(TestCase):
sculleryBoy = SculleryBoy()
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/backend/service/test_SousChef.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ def test_prepPixelGroup_nocache(

@mock.patch(thisService + "PixelGroupingParametersCalculationRecipe")
def test_prepPixelGroup_cache(self, PixelGroupingParametersCalculationRecipe):
key = (self.ingredients.runNumber, self.ingredients.useLiteMode, self.ingredients.focusGroup.name, self.pixelMask)
key = (
self.ingredients.runNumber,
self.ingredients.useLiteMode,
self.ingredients.focusGroup.name,
self.pixelMask,
)
# ensure the cache is prepared
self.instance._pixelGroupCache[key] = mock.Mock()

Expand Down Expand Up @@ -396,14 +401,14 @@ def test_prepReductionIngredients(self, ReductionIngredients):
ingredients_.peakIntensityThreshold = self.instance._getThresholdFromCalibrantSample(
"calibrationCalibrantSamplePath"
)

combinedMask = mock.Mock()
result = self.instance.prepReductionIngredients(ingredients_, combinedMask)

assert self.instance.prepManyPixelGroups.call_count == 2
self.instance.prepManyPixelGroups.assert_any_call(ingredients_)
self.instance.prepManyPixelGroups.assert_any_call(ingredients_, combinedMask)

self.instance.dataFactoryService.getCifFilePath.assert_called_once_with("sample")
ReductionIngredients.assert_called_once_with(
runNumber=ingredients_.runNumber,
Expand Down
6 changes: 4 additions & 2 deletions tests/util/SculleryBoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
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.mantid.WorkspaceNameGenerator import WorkspaceName
from snapred.meta.Config import Resource
from snapred.meta.mantid.WorkspaceNameGenerator import WorkspaceName
from snapred.meta.redantic import parse_file_as
from util.dao import DAOFactory

Expand Down Expand Up @@ -92,7 +92,9 @@ def prepDetectorPeaks(self, ingredients: FarmFreshIngredients, purgePeaks=False)
except (TypeError, AttributeError):
return [mock.Mock(spec_set=GroupPeakList)]

def prepReductionIngredients(self, ingredients: FarmFreshIngredients, combinedPixelMask: Optional[WorkspaceName] = None): # noqa ARG002
def prepReductionIngredients(
self, ingredients: FarmFreshIngredients, combinedPixelMask: Optional[WorkspaceName] = None
): # noqa ARG002
path = Resource.getPath("/inputs/calibration/ReductionIngredients.json")
return parse_file_as(ReductionIngredients, path)

Expand Down

0 comments on commit 104f557

Please sign in to comment.