Skip to content

Commit

Permalink
refactor into base class for Repack actions
Browse files Browse the repository at this point in the history
  • Loading branch information
weatherhead99 committed Jun 30, 2024
1 parent 32f0f17 commit 6593043
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions python/lsst/analysis/tools/atools/calibQuantityProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"CalibPtcCovarScatterTool",
)

from typing import cast, TypeVar, Callable, Any
from typing import cast, TypeVar, Callable, Any, TypeAlias

Check failure on line 34 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F401

'typing.cast' imported but unused

Check failure on line 34 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F401

'typing.TypeVar' imported but unused

from lsst.pex.config import Field, ListField
from lsst.pex.config.configurableActions import ConfigurableActionField
Expand Down Expand Up @@ -59,22 +59,23 @@
15: "C17",
}

#Dummy class just so we can get the type annotations correct

Check failure on line 62 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

E265

block comment should start with '# '
RepackerLoopFun: TypeAlias = Callable[None, [KeyedData, Any, Any, Any, Any]]

RepackerT = TypeVar("RepackerT", bound=[PrepRepacker, SingleValueRepacker])
RepackerLoopFun = Callable[None, [KeyedData, Any, Any, Any, Any]]
def _repack_loop_helper(obj: RepackerT, repackfun: RepackerLoopFun, data: KeyedData) -> KeyedData:
repackedData: dict[str, Vector] = {}
quantitiesData = [data[_] for _ in obj.quantityKey]

for p, d in zip(data[obj.panelKey], data[obj.dataKey]):
for qName, qD in zip(obj.quantityKey, quantitiesData):
RepackerLoopFun(repackedData, p, d, qName, qD)
return repackedData
class BaseRepacker(KeyedDataAction):

Check failure on line 65 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

E302

expected 2 blank lines, found 1
"""Base class for Data Repacking actions. Essentially Just adds some helper functions"""

Check failure on line 66 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

W505

doc line too long (92 > 79 characters)
def _repack_loop_helper(self, repackfun: RepackerLoopFun, data: KeyedData) -> KeyedData:
repackedData: dict[str, Vector] = {}
quantitiesData = [data[_] for _ in obj.quantityKey]

Check failure on line 69 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F821

undefined name 'obj'

for p, d in zip(data[obj.panelKey], data[obj.dataKey]):

Check failure on line 71 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F821

undefined name 'obj'

Check failure on line 71 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F821

undefined name 'obj'
for qName, qD in zip(obj.quantityKey, quantitiesData):

Check failure on line 72 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

F821

undefined name 'obj'
RepackerLoopFun(repackedData, p, d, qName, qD)
return repackedData



class PrepRepacker(KeyedDataAction):
class PrepRepacker(BaseRepacker):

Check failure on line 78 in python/lsst/analysis/tools/atools/calibQuantityProfile.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

E303

too many blank lines (3)
"""Prep action to repack data."""

panelKey = Field[str](
Expand All @@ -91,7 +92,7 @@ def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
def rp_loop(rpData: KeyedData, panel, data, quantityName, quantityData):
qName = f"{panel}_{data}_{quantityName}"
rpData[qName] = quantityData
repackedData = _repack_loop_helper(self, rp_loop, data)
repackedData = self._repack_loop_helper(self, rp_loop, data)
return repackedData

def getInputSchema(self) -> KeyedDataSchema:
Expand All @@ -105,7 +106,7 @@ def addInputSchema(self, inputSchema: KeyedDataSchema) -> None:
pass


class SingleValueRepacker(KeyedDataAction):
class SingleValueRepacker(BaseRepacker):
"""Prep action to repack data."""

panelKey = Field[str](
Expand All @@ -119,22 +120,20 @@ class SingleValueRepacker(KeyedDataAction):
minLength=1, optional=False)

def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
uniquePanelKeys: list = list(set(data[self.panelKey]))

repackedData: dict[str, Vector] = {}
uniquePanelKeys: list = list(set(data[self.panelKey]))

# Loop over data vector to repack information as it is expected.
for uk in uniquePanelKeys:
repackedData[f"{uk}_x"] = []
for q in self.quantityKey:
repackedData[f"{uk}_{q}"] = []

def rp_loop(rpData: KeyedData, panel, data, quantityName, quantityData):
rpData[f"{panel}_x"].append(data)
rpData[f"{panel}_{quantityName}"] = quantityData
if (xlab := f"{panel}_x") not in rpData:
rpData[xlab] = [data]
else:
rpData[xlab].append(data)

if (lab := f"{panel}_{quantityName}") not in rpData:
rpData[lab] = [quantityData]
else:
rpData[lab].append(quantityData)

repackedData: KeyedData = _repack_loop_helper(self, rp_loop, data)
repackedData = self._repack_loop_helper(self, rp_loop, data)
return repackedData

def getInputSchema(self) -> KeyedDataSchema:
Expand Down

0 comments on commit 6593043

Please sign in to comment.