3131 "CalibPtcCovarScatterTool" ,
3232)
3333
34- from typing import cast
34+ from typing import cast , TypeVar , Callable , Any
3535
36- from lsst .pex .config import Field
36+ from lsst .pex .config import Field , ListField
3737from lsst .pex .config .configurableActions import ConfigurableActionField
3838
3939from ..actions .plot .elements import HistElement , ScatterElement
4040from ..actions .plot .gridPlot import GridPanelConfig , GridPlot
4141from ..interfaces import AnalysisTool , KeyedData , KeyedDataAction , PlotElement , Vector
4242
43+ _CALIB_AMP_NAME_DICT : dict [int , str ] = {
44+ 0 : "C00" ,
45+ 1 : "C01" ,
46+ 2 : "C02" ,
47+ 3 : "C03" ,
48+ 4 : "C04" ,
49+ 5 : "C05" ,
50+ 6 : "C06" ,
51+ 7 : "C07" ,
52+ 8 : "C10" ,
53+ 9 : "C11" ,
54+ 10 : "C12" ,
55+ 11 : "C13" ,
56+ 12 : "C14" ,
57+ 13 : "C15" ,
58+ 14 : "C16" ,
59+ 15 : "C17" ,
60+ }
61+
62+
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
73+
74+
75+
4376
4477class PrepRepacker (KeyedDataAction ):
4578 """Prep action to repack data."""
@@ -50,18 +83,15 @@ class PrepRepacker(KeyedDataAction):
5083 dataKey = Field [str ](
5184 doc = "Data selector. Data will be separated into multiple groups in a single panel based on this key." ,
5285 )
53- quantityKey = Field [str ](
86+ quantityKey = ListField [str ](
5487 doc = "Quantity selector. The actual data quantities to be plotted." ,
55- )
88+ minLength = 1 , optional = False )
5689
5790 def __call__ (self , data : KeyedData , ** kwargs ) -> KeyedData :
58- repackedData = {}
59- # Loop over the length of the data vector and repack row by row
60- for i in range (len (cast (Vector , data [self .panelKey ]))):
61- panelVec = cast (Vector , data [self .panelKey ])
62- dataVec = cast (Vector , data [self .dataKey ])
63- quantityVec = cast (Vector , data [self .quantityKey ])
64- repackedData [f"{ panelVec [i ]} _{ dataVec [i ]} _{ self .quantityKey } " ] = quantityVec [i ]
91+ def rp_loop (rpData : KeyedData , panel , data , quantityName , quantityData ):
92+ qName = f"{ panel } _{ data } _{ quantityName } "
93+ rpData [qName ] = quantityData
94+ repackedData = _repack_loop_helper (self , rp_loop , data )
6595 return repackedData
6696
6797 def getInputSchema (self ) -> KeyedDataSchema :
@@ -84,27 +114,27 @@ class SingleValueRepacker(KeyedDataAction):
84114 dataKey = Field [str ](
85115 doc = "Data selector. Data will be separated into multiple groups in a single panel based on this key." ,
86116 )
87- quantityKey = Field [str ](
117+ quantityKey = ListField [str ](
88118 doc = "Quantity selector. The actual data quantities to be plotted." ,
89- )
119+ minLength = 1 , optional = False )
90120
91121 def __call__ (self , data : KeyedData , ** kwargs ) -> KeyedData :
92- repackedData = {}
93- uniquePanelKeys = list (set (data [self .panelKey ]))
122+ uniquePanelKeys : list = list (set (data [self .panelKey ]))
94123
95- # Loop over data vector to repack information as it is expected.
96- for i in range (len (uniquePanelKeys )):
97- repackedData [f"{ uniquePanelKeys [i ]} _x" ] = []
98- repackedData [f"{ uniquePanelKeys [i ]} " ] = []
124+ repackedData : dict [str , Vector ] = {}
125+ uniquePanelKeys : list = list (set (data [self .panelKey ]))
99126
100- panelVec = cast (Vector , data [self .panelKey ])
101- dataVec = cast (Vector , data [self .dataKey ])
102- quantityVec = cast (Vector , data [self .quantityKey ])
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 } " ] = []
103132
104- for i in range ( len ( panelVec ) ):
105- repackedData [f"{ panelVec [ i ] } _x" ].append (dataVec [ i ] )
106- repackedData [f"{ panelVec [ i ] } " ]. append ( quantityVec [ i ])
133+ def rp_loop ( rpData : KeyedData , panel , data , quantityName , quantityData ):
134+ rpData [f"{ panel } _x" ].append (data )
135+ rpData [f"{ panel } _ { quantityName } " ] = quantityData
107136
137+ repackedData : KeyedData = _repack_loop_helper (self , rp_loop , data )
108138 return repackedData
109139
110140 def getInputSchema (self ) -> KeyedDataSchema :
@@ -152,24 +182,7 @@ def setDefaults(self):
152182 self .produce .plot .numCols = 4
153183
154184 # Values to group by to distinguish between data in differing panels
155- self .produce .plot .valsGroupBy = {
156- 0 : "C00" ,
157- 1 : "C01" ,
158- 2 : "C02" ,
159- 3 : "C03" ,
160- 4 : "C04" ,
161- 5 : "C05" ,
162- 6 : "C06" ,
163- 7 : "C07" ,
164- 8 : "C10" ,
165- 9 : "C11" ,
166- 10 : "C12" ,
167- 11 : "C13" ,
168- 12 : "C14" ,
169- 13 : "C15" ,
170- 14 : "C16" ,
171- 15 : "C17" ,
172- }
185+ self .produce .plot .valsGroupBy = _CALIB_AMP_NAME_DICT
173186
174187 def finalize (self ):
175188 super ().finalize ()
@@ -214,45 +227,10 @@ def setDefaults(self):
214227 self .produce .plot .numCols = 4
215228
216229 # Values to use for x-axis data
217- self .produce .plot .xDataKeys = {
218- 0 : "C00_x" ,
219- 1 : "C01_x" ,
220- 2 : "C02_x" ,
221- 3 : "C03_x" ,
222- 4 : "C04_x" ,
223- 5 : "C05_x" ,
224- 6 : "C06_x" ,
225- 7 : "C07_x" ,
226- 8 : "C10_x" ,
227- 9 : "C11_x" ,
228- 10 : "C12_x" ,
229- 11 : "C13_x" ,
230- 12 : "C14_x" ,
231- 13 : "C15_x" ,
232- 14 : "C16_x" ,
233- 15 : "C17_x" ,
234- }
230+ self .produce .plot .xDataKeys = {k : f"{ v } _x" for k ,v in _CALIB_AMP_NAME_DICT .items ()}
235231
236232 # Values to group by to distinguish between data in differing panels
237- self .produce .plot .valsGroupBy = {
238- 0 : "C00" ,
239- 1 : "C01" ,
240- 2 : "C02" ,
241- 3 : "C03" ,
242- 4 : "C04" ,
243- 5 : "C05" ,
244- 6 : "C06" ,
245- 7 : "C07" ,
246- 8 : "C10" ,
247- 9 : "C11" ,
248- 10 : "C12" ,
249- 11 : "C13" ,
250- 12 : "C14" ,
251- 13 : "C15" ,
252- 14 : "C16" ,
253- 15 : "C17" ,
254- }
255-
233+ self .produce .plot .valsGroupBy = _CALIB_AMP_NAME_DICT
256234 self .prep .panelKey = "amplifier"
257235 self .prep .dataKey = "mjd"
258236
0 commit comments