From 104f5577f11a37f3b266d9461c427454e8e96aa4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 20:40:45 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../EffectiveInstrumentIngredients.py | 1 - .../dao/ingredients/ReductionIngredients.py | 11 ++--- .../recipe/EffectiveInstrumentRecipe.py | 4 +- src/snapred/backend/recipe/ReductionRecipe.py | 4 +- .../backend/service/ReductionService.py | 6 ++- src/snapred/backend/service/SousChef.py | 21 ++++++---- .../recipe/test_EffectiveInstrumentRecipe.py | 41 ++++++++----------- .../recipe/test_PreprocessReductionRecipe.py | 5 ++- .../backend/recipe/test_ReductionRecipe.py | 4 +- tests/unit/backend/service/test_SousChef.py | 11 +++-- tests/util/SculleryBoy.py | 6 ++- 11 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/snapred/backend/dao/ingredients/EffectiveInstrumentIngredients.py b/src/snapred/backend/dao/ingredients/EffectiveInstrumentIngredients.py index 356c70cb4..04cdef59f 100644 --- a/src/snapred/backend/dao/ingredients/EffectiveInstrumentIngredients.py +++ b/src/snapred/backend/dao/ingredients/EffectiveInstrumentIngredients.py @@ -4,7 +4,6 @@ class EffectiveInstrumentIngredients(BaseModel): - unmaskedPixelGroup: PixelGroup model_config = ConfigDict( diff --git a/src/snapred/backend/dao/ingredients/ReductionIngredients.py b/src/snapred/backend/dao/ingredients/ReductionIngredients.py index 566d0d8a9..17c2fb3b3 100644 --- a/src/snapred/backend/dao/ingredients/ReductionIngredients.py +++ b/src/snapred/backend/dao/ingredients/ReductionIngredients.py @@ -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 @@ -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", ) diff --git a/src/snapred/backend/recipe/EffectiveInstrumentRecipe.py b/src/snapred/backend/recipe/EffectiveInstrumentRecipe.py index 0feaf2a4c..92cd06bcf 100644 --- a/src/snapred/backend/recipe/EffectiveInstrumentRecipe.py +++ b/src/snapred/backend/recipe/EffectiveInstrumentRecipe.py @@ -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 @@ -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 diff --git a/src/snapred/backend/recipe/ReductionRecipe.py b/src/snapred/backend/recipe/ReductionRecipe.py index 79d2bdfa3..7050927be 100644 --- a/src/snapred/backend/recipe/ReductionRecipe.py +++ b/src/snapred/backend/recipe/ReductionRecipe.py @@ -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 @@ -213,7 +213,7 @@ def execute(self): self.ingredients.effectiveInstrument(groupingIndex), inputWorkspace=sampleClone, ) - + # Cleanup outputs.append(sampleClone) diff --git a/src/snapred/backend/service/ReductionService.py b/src/snapred/backend/service/ReductionService.py index 65addc0e3..3860d106a 100644 --- a/src/snapred/backend/service/ReductionService.py +++ b/src/snapred/backend/service/ReductionService.py @@ -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 @@ -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: diff --git a/src/snapred/backend/service/SousChef.py b/src/snapred/backend/service/SousChef.py index 994d6acdd..e2247a185 100644 --- a/src/snapred/backend/service/SousChef.py +++ b/src/snapred/backend/service/SousChef.py @@ -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 @@ -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__) @@ -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: @@ -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( @@ -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: @@ -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_) diff --git a/tests/unit/backend/recipe/test_EffectiveInstrumentRecipe.py b/tests/unit/backend/recipe/test_EffectiveInstrumentRecipe.py index a5cf89c76..d9927549f 100644 --- a/tests/unit/backend/recipe/test_EffectiveInstrumentRecipe.py +++ b/tests/unit/backend/recipe/test_EffectiveInstrumentRecipe.py @@ -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") @@ -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, @@ -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 @@ -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"] @@ -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): @@ -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)) @@ -122,7 +116,7 @@ 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}'", @@ -130,6 +124,5 @@ def test_cater(self): 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}", ) - diff --git a/tests/unit/backend/recipe/test_PreprocessReductionRecipe.py b/tests/unit/backend/recipe/test_PreprocessReductionRecipe.py index f5eabb50a..4e0001098 100644 --- a/tests/unit/backend/recipe/test_PreprocessReductionRecipe.py +++ b/tests/unit/backend/recipe/test_PreprocessReductionRecipe.py @@ -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") diff --git a/tests/unit/backend/recipe/test_ReductionRecipe.py b/tests/unit/backend/recipe/test_ReductionRecipe.py index 868ac02f5..b992973c7 100644 --- a/tests/unit/backend/recipe/test_ReductionRecipe.py +++ b/tests/unit/backend/recipe/test_ReductionRecipe.py @@ -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 ( @@ -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() diff --git a/tests/unit/backend/service/test_SousChef.py b/tests/unit/backend/service/test_SousChef.py index 029778851..f037e968b 100644 --- a/tests/unit/backend/service/test_SousChef.py +++ b/tests/unit/backend/service/test_SousChef.py @@ -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() @@ -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, diff --git a/tests/util/SculleryBoy.py b/tests/util/SculleryBoy.py index e3efb8d84..ea64a87c3 100644 --- a/tests/util/SculleryBoy.py +++ b/tests/util/SculleryBoy.py @@ -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 @@ -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)