Skip to content

Commit 3d50402

Browse files
committed
refactor into base class for Repack actions
1 parent 7255cdc commit 3d50402

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

python/lsst/analysis/tools/atools/calibQuantityProfile.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"CalibPtcCovarScatterTool",
3232
)
3333

34-
from typing import cast, TypeVar, Callable, Any
34+
from typing import cast, TypeVar, Callable, Any, TypeAlias
3535

3636
from lsst.pex.config import Field, ListField
3737
from lsst.pex.config.configurableActions import ConfigurableActionField
@@ -59,22 +59,23 @@
5959
15: "C17",
6060
}
6161

62+
#Dummy class just so we can get the type annotations correct
63+
RepackerLoopFun: TypeAlias = Callable[None, [KeyedData, Any, Any, Any, Any]]
6264

63-
RepackerT = TypeVar("RepackerT", bound=[PrepRepacker, SingleValueRepacker])
64-
RepackerLoopFun = Callable[None, [KeyedData, Any, Any, Any, Any]]
65-
def _repack_loop_helper(obj: RepackerT, repackfun: RepackerLoopFun, data: KeyedData) -> KeyedData:
66-
repackedData: dict[str, Vector] = {}
67-
quantitiesData = [data[_] for _ in obj.quantityKey]
68-
69-
for p, d in zip(data[obj.panelKey], data[obj.dataKey]):
70-
for qName, qD in zip(obj.quantityKey, quantitiesData):
71-
RepackerLoopFun(repackedData, p, d, qName, qD)
72-
return repackedData
65+
class BaseRepacker(KeyedDataAction):
66+
"""Base class for Data Repacking actions. Essentially Just adds some helper functions"""
67+
def _repack_loop_helper(self, repackfun: RepackerLoopFun, data: KeyedData) -> KeyedData:
68+
repackedData: dict[str, Vector] = {}
69+
quantitiesData = [data[_] for _ in obj.quantityKey]
7370

71+
for p, d in zip(data[obj.panelKey], data[obj.dataKey]):
72+
for qName, qD in zip(obj.quantityKey, quantitiesData):
73+
RepackerLoopFun(repackedData, p, d, qName, qD)
74+
return repackedData
7475

7576

7677

77-
class PrepRepacker(KeyedDataAction):
78+
class PrepRepacker(BaseRepacker):
7879
"""Prep action to repack data."""
7980

8081
panelKey = Field[str](
@@ -91,7 +92,7 @@ def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
9192
def rp_loop(rpData: KeyedData, panel, data, quantityName, quantityData):
9293
qName = f"{panel}_{data}_{quantityName}"
9394
rpData[qName] = quantityData
94-
repackedData = _repack_loop_helper(self, rp_loop, data)
95+
repackedData = self._repack_loop_helper(self, rp_loop, data)
9596
return repackedData
9697

9798
def getInputSchema(self) -> KeyedDataSchema:
@@ -105,7 +106,7 @@ def addInputSchema(self, inputSchema: KeyedDataSchema) -> None:
105106
pass
106107

107108

108-
class SingleValueRepacker(KeyedDataAction):
109+
class SingleValueRepacker(BaseRepacker):
109110
"""Prep action to repack data."""
110111

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

121122
def __call__(self, data: KeyedData, **kwargs) -> KeyedData:
122-
uniquePanelKeys: list = list(set(data[self.panelKey]))
123-
124123
repackedData: dict[str, Vector] = {}
125-
uniquePanelKeys: list = list(set(data[self.panelKey]))
126-
127-
# Loop over data vector to repack information as it is expected.
128-
for uk in uniquePanelKeys:
129-
repackedData[f"{uk}_x"] = []
130-
for q in self.quantityKey:
131-
repackedData[f"{uk}_{q}"] = []
132124

133125
def rp_loop(rpData: KeyedData, panel, data, quantityName, quantityData):
134-
rpData[f"{panel}_x"].append(data)
135-
rpData[f"{panel}_{quantityName}"] = quantityData
126+
if (xlab := f"{panel}_x") not in rpData:
127+
rpData[xlab] = [data]
128+
else:
129+
rpData[xlab].append(data)
130+
131+
if (lab := f"{panel}_{quantityName}") not in rpData:
132+
rpData[lab] = [quantityData]
133+
else:
134+
rpData[lab].append(quantityData)
136135

137-
repackedData: KeyedData = _repack_loop_helper(self, rp_loop, data)
136+
repackedData = self._repack_loop_helper(self, rp_loop, data)
138137
return repackedData
139138

140139
def getInputSchema(self) -> KeyedDataSchema:

0 commit comments

Comments
 (0)