LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
ptcDataset.py
Go to the documentation of this file.
2# LSST Data Management System
3# Copyright 2008-2017 AURA/LSST.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
22"""
23Define dataset class for MeasurePhotonTransferCurve task
24"""
25import numpy as np
26from astropy.table import Table
27
28from lsst.ip.isr import IsrCalib
29
30__all__ = ['PhotonTransferCurveDataset']
31
32
34 """A simple class to hold the output data from the PTC task.
35 The dataset is made up of a dictionary for each item, keyed by the
36 amplifiers' names, which much be supplied at construction time.
37 New items cannot be added to the class to save accidentally saving to the
38 wrong property, and the class can be frozen if desired.
39 inputExpIdPairs records the exposures used to produce the data.
40 When fitPtc() or fitCovariancesAstier() is run, a mask is built up, which
41 is by definition always the same length as inputExpIdPairs, rawExpTimes,
42 rawMeans and rawVars, and is a list of bools, which are incrementally set
43 to False as points are discarded from the fits.
44 PTC fit parameters for polynomials are stored in a list in ascending order
45 of polynomial term, i.e. par[0]*x^0 + par[1]*x + par[2]*x^2 etc
46 with the length of the list corresponding to the order of the polynomial
47 plus one.
48
49 Parameters
50 ----------
51 ampNames : `list`
52 List with the names of the amplifiers of the detector at hand.
53
54 ptcFitType : `str`
55 Type of model fitted to the PTC: "POLYNOMIAL", "EXPAPPROXIMATION",
56 or "FULLCOVARIANCE".
57
58 covMatrixSide : `int`
59 Maximum lag of covariances (size of square covariance matrices).
60
61 kwargs : `dict`, optional
62 Other keyword arguments to pass to the parent init.
63
64 Notes
65 -----
66 The stored attributes are:
67 badAmps : `list`
68 List with bad amplifiers names.
69 inputExpIdPairs : `dict`, [`str`, `list`]
70 Dictionary keyed by amp names containing the input exposures IDs.
71 expIdMask : `dict`, [`str`, `list`]
72 Dictionary keyed by amp names containing the mask produced after
73 outlier rejection. The mask produced by the "FULLCOVARIANCE"
74 option may differ from the one produced in the other two PTC
75 fit types.
76 rawExpTimes : `dict`, [`str`, `list`]
77 Dictionary keyed by amp names containing the unmasked exposure times.
78 rawMeans : `dict`, [`str`, `list`]
79 Dictionary keyed by amp namescontaining the unmasked average of the
80 means of the exposures in each flat pair.
81 rawVars : `dict`, [`str`, `list`]
82 Dictionary keyed by amp names containing the variance of the
83 difference image of the exposures in each flat pair.
84 gain : `dict`, [`str`, `list`]
85 Dictionary keyed by amp names containing the fitted gains.
86 gainErr : `dict`, [`str`, `list`]
87 Dictionary keyed by amp names containing the errors on the
88 fitted gains.
89 noise : `dict`, [`str`, `list`]
90 Dictionary keyed by amp names containing the fitted noise.
91 noiseErr : `dict`, [`str`, `list`]
92 Dictionary keyed by amp names containing the errors on the fitted
93 noise.
94 ptcFitPars : `dict`, [`str`, `list`]
95 Dictionary keyed by amp names containing the fitted parameters of the
96 PTC model for ptcFitTye in ["POLYNOMIAL", "EXPAPPROXIMATION"].
97 ptcFitParsError : `dict`, [`str`, `list`]
98 Dictionary keyed by amp names containing the errors on the fitted
99 parameters of the PTC model for ptcFitTye in
100 ["POLYNOMIAL", "EXPAPPROXIMATION"].
101 ptcFitChiSq : `dict`, [`str`, `list`]
102 Dictionary keyed by amp names containing the reduced chi squared
103 of the fit for ptcFitTye in ["POLYNOMIAL", "EXPAPPROXIMATION"].
104 ptcTurnoff : `float`
105 Flux value (in ADU) where the variance of the PTC curve starts
106 decreasing consistently.
107 covariances : `dict`, [`str`, `list`]
108 Dictionary keyed by amp names containing a list of measured
109 covariances per mean flux.
110 covariancesModel : `dict`, [`str`, `list`]
111 Dictionary keyed by amp names containinging covariances model
112 (Eq. 20 of Astier+19) per mean flux.
113 covariancesSqrtWeights : `dict`, [`str`, `list`]
114 Dictionary keyed by amp names containinging sqrt. of covariances
115 weights.
116 aMatrix : `dict`, [`str`, `list`]
117 Dictionary keyed by amp names containing the "a" parameters from
118 the model in Eq. 20 of Astier+19.
119 bMatrix : `dict`, [`str`, `list`]
120 Dictionary keyed by amp names containing the "b" parameters from
121 the model in Eq. 20 of Astier+19.
122 covariancesModelNoB : `dict`, [`str`, `list`]
123 Dictionary keyed by amp names containing covariances model
124 (with 'b'=0 in Eq. 20 of Astier+19)
125 per mean flux.
126 aMatrixNoB : `dict`, [`str`, `list`]
127 Dictionary keyed by amp names containing the "a" parameters from the
128 model in Eq. 20 of Astier+19
129 (and 'b' = 0).
130 finalVars : `dict`, [`str`, `list`]
131 Dictionary keyed by amp names containing the masked variance of the
132 difference image of each flat
133 pair. If needed, each array will be right-padded with
134 np.nan to match the length of rawExpTimes.
135 finalModelVars : `dict`, [`str`, `list`]
136 Dictionary keyed by amp names containing the masked modeled
137 variance of the difference image of each flat pair. If needed, each
138 array will be right-padded with np.nan to match the length of
139 rawExpTimes.
140 finalMeans : `dict`, [`str`, `list`]
141 Dictionary keyed by amp names containing the masked average of the
142 means of the exposures in each flat pair. If needed, each array
143 will be right-padded with np.nan to match the length of
144 rawExpTimes.
145 photoCharge : `dict`, [`str`, `list`]
146 Dictionary keyed by amp names containing the integrated photocharge
147 for linearity calibration.
148
149 Returns
150 -------
151 `lsst.cp.pipe.ptc.PhotonTransferCurveDataset`
152 Output dataset from MeasurePhotonTransferCurveTask.
153
154 Notes
155 -----
156 Version 1.1 adds the `ptcTurnoff` attribute.
157 """
158
159 _OBSTYPE = 'PTC'
160 _SCHEMA = 'Gen3 Photon Transfer Curve'
161 _VERSION = 1.1
162
163 def __init__(self, ampNames=[], ptcFitType=None, covMatrixSide=1, **kwargs):
164
165 self.ptcFitTypeptcFitType = ptcFitType
166 self.ampNamesampNames = ampNames
167 self.covMatrixSidecovMatrixSide = covMatrixSide
168
169 self.badAmpsbadAmps = [np.nan]
170
171 self.inputExpIdPairsinputExpIdPairs = {ampName: [] for ampName in ampNames}
172 self.expIdMaskexpIdMask = {ampName: [] for ampName in ampNames}
173 self.rawExpTimesrawExpTimes = {ampName: [] for ampName in ampNames}
174 self.rawMeansrawMeans = {ampName: [] for ampName in ampNames}
175 self.rawVarsrawVars = {ampName: [] for ampName in ampNames}
176 self.photoChargephotoCharge = {ampName: [] for ampName in ampNames}
177
178 self.gaingain = {ampName: np.nan for ampName in ampNames}
179 self.gainErrgainErr = {ampName: np.nan for ampName in ampNames}
180 self.noisenoise = {ampName: np.nan for ampName in ampNames}
181 self.noiseErrnoiseErr = {ampName: np.nan for ampName in ampNames}
182
183 self.ptcFitParsptcFitPars = {ampName: [] for ampName in ampNames}
184 self.ptcFitParsErrorptcFitParsError = {ampName: [] for ampName in ampNames}
185 self.ptcFitChiSqptcFitChiSq = {ampName: np.nan for ampName in ampNames}
186 self.ptcTurnoffptcTurnoff = {ampName: np.nan for ampName in ampNames}
187
188 self.covariancescovariances = {ampName: [] for ampName in ampNames}
189 self.covariancesModelcovariancesModel = {ampName: [] for ampName in ampNames}
190 self.covariancesSqrtWeightscovariancesSqrtWeights = {ampName: [] for ampName in ampNames}
191 self.aMatrixaMatrix = {ampName: np.nan for ampName in ampNames}
192 self.bMatrixbMatrix = {ampName: np.nan for ampName in ampNames}
193 self.covariancesModelNoBcovariancesModelNoB = {ampName: [] for ampName in ampNames}
194 self.aMatrixNoBaMatrixNoB = {ampName: np.nan for ampName in ampNames}
195
196 self.finalVarsfinalVars = {ampName: [] for ampName in ampNames}
197 self.finalModelVarsfinalModelVars = {ampName: [] for ampName in ampNames}
198 self.finalMeansfinalMeans = {ampName: [] for ampName in ampNames}
199
200 super().__init__(**kwargs)
201 self.requiredAttributesrequiredAttributesrequiredAttributesrequiredAttributes.update(['badAmps', 'inputExpIdPairs', 'expIdMask', 'rawExpTimes',
202 'rawMeans', 'rawVars', 'gain', 'gainErr', 'noise', 'noiseErr',
203 'ptcFitPars', 'ptcFitParsError', 'ptcFitChiSq', 'ptcTurnoff',
204 'aMatrixNoB', 'covariances', 'covariancesModel',
205 'covariancesSqrtWeights', 'covariancesModelNoB',
206 'aMatrix', 'bMatrix', 'finalVars', 'finalModelVars', 'finalMeans',
207 'photoCharge'])
208
209 def setAmpValues(self, ampName, inputExpIdPair=[(np.nan, np.nan)], expIdMask=[np.nan],
210 rawExpTime=[np.nan], rawMean=[np.nan], rawVar=[np.nan], photoCharge=[np.nan],
211 gain=np.nan, gainErr=np.nan, noise=np.nan, noiseErr=np.nan, ptcFitPars=[np.nan],
212 ptcFitParsError=[np.nan], ptcFitChiSq=np.nan, ptcTurnoff=np.nan, covArray=[],
213 covArrayModel=[], covSqrtWeights=[], aMatrix=[], bMatrix=[], covArrayModelNoB=[],
214 aMatrixNoB=[], finalVar=[np.nan], finalModelVar=[np.nan], finalMean=[np.nan]):
215 """Function to initialize an amp of a PhotonTransferCurveDataset.
216
217 Notes
218 -----
219 The parameters are all documented in `init`.
220 """
221 nanMatrix = np.full((self.covMatrixSidecovMatrixSide, self.covMatrixSidecovMatrixSide), np.nan)
222 if len(covArray) == 0:
223 covArray = [nanMatrix]
224 if len(covArrayModel) == 0:
225 covArrayModel = [nanMatrix]
226 if len(covSqrtWeights) == 0:
227 covSqrtWeights = [nanMatrix]
228 if len(covArrayModelNoB) == 0:
229 covArrayModelNoB = [nanMatrix]
230 if len(aMatrix) == 0:
231 aMatrix = nanMatrix
232 if len(bMatrix) == 0:
233 bMatrix = nanMatrix
234 if len(aMatrixNoB) == 0:
235 aMatrixNoB = nanMatrix
236
237 self.inputExpIdPairsinputExpIdPairs[ampName] = inputExpIdPair
238 self.expIdMaskexpIdMask[ampName] = expIdMask
239 self.rawExpTimesrawExpTimes[ampName] = rawExpTime
240 self.rawMeansrawMeans[ampName] = rawMean
241 self.rawVarsrawVars[ampName] = rawVar
242 self.photoChargephotoCharge[ampName] = photoCharge
243 self.gaingain[ampName] = gain
244 self.gainErrgainErr[ampName] = gainErr
245 self.noisenoise[ampName] = noise
246 self.noiseErrnoiseErr[ampName] = noiseErr
247 self.ptcFitParsptcFitPars[ampName] = ptcFitPars
248 self.ptcFitParsErrorptcFitParsError[ampName] = ptcFitParsError
249 self.ptcFitChiSqptcFitChiSq[ampName] = ptcFitChiSq
250 self.ptcTurnoffptcTurnoff[ampName] = ptcTurnoff
251 self.covariancescovariances[ampName] = covArray
252 self.covariancesSqrtWeightscovariancesSqrtWeights[ampName] = covSqrtWeights
253 self.covariancesModelcovariancesModel[ampName] = covArrayModel
254 self.covariancesModelNoBcovariancesModelNoB[ampName] = covArrayModelNoB
255 self.aMatrixaMatrix[ampName] = aMatrix
256 self.bMatrixbMatrix[ampName] = bMatrix
257 self.aMatrixNoBaMatrixNoB[ampName] = aMatrixNoB
258 self.ptcFitParsptcFitPars[ampName] = ptcFitPars
259 self.ptcFitParsErrorptcFitParsError[ampName] = ptcFitParsError
260 self.ptcFitChiSqptcFitChiSq[ampName] = ptcFitChiSq
261 self.finalVarsfinalVars[ampName] = finalVar
262 self.finalModelVarsfinalModelVars[ampName] = finalModelVar
263 self.finalMeansfinalMeans[ampName] = finalMean
264
265 def updateMetadata(self, setDate=False, **kwargs):
266 """Update calibration metadata.
267 This calls the base class's method after ensuring the required
268 calibration keywords will be saved.
269 Parameters
270 ----------
271 setDate : `bool`, optional
272 Update the CALIBDATE fields in the metadata to the current
273 time. Defaults to False.
274 kwargs :
275 Other keyword parameters to set in the metadata.
276 """
277 kwargs['PTC_FIT_TYPE'] = self.ptcFitTypeptcFitType
278
279 super().updateMetadata(setDate=setDate, **kwargs)
280
281 @classmethod
282 def fromDict(cls, dictionary):
283 """Construct a calibration from a dictionary of properties.
284 Must be implemented by the specific calibration subclasses.
285 Parameters
286 ----------
287 dictionary : `dict`
288 Dictionary of properties.
289 Returns
290 -------
291 calib : `lsst.ip.isr.CalibType`
292 Constructed calibration.
293 Raises
294 ------
295 RuntimeError :
296 Raised if the supplied dictionary is for a different
297 calibration.
298 """
299 calib = cls()
300 if calib._OBSTYPE != dictionary['metadata']['OBSTYPE']:
301 raise RuntimeError(f"Incorrect Photon Transfer Curve dataset supplied. "
302 f"Expected {calib._OBSTYPE}, found {dictionary['metadata']['OBSTYPE']}")
303 calib.setMetadata(dictionary['metadata'])
304 calib.ptcFitType = dictionary['ptcFitType']
305 calib.covMatrixSide = dictionary['covMatrixSide']
306 calib.badAmps = np.array(dictionary['badAmps'], 'str').tolist()
307 # The cov matrices are square
308 covMatrixSide = calib.covMatrixSide
309 # Number of final signal levels
310 covDimensionsProduct = len(np.array(list(dictionary['covariances'].values())[0]).ravel())
311 nSignalPoints = int(covDimensionsProduct/(covMatrixSide*covMatrixSide))
312
313 for ampName in dictionary['ampNames']:
314 covsAmp = np.array(dictionary['covariances'][ampName]).reshape((nSignalPoints, covMatrixSide,
315 covMatrixSide))
316
317 # After cpPtcExtract runs in the PTC pipeline, the datasets
318 # created ('PARTIAL' and 'DUMMY') have a single measurement.
319 # Apply the maskign to the final ptcDataset, after running
320 # cpPtcSolve.
321 if len(covsAmp) > 1:
322 # Masks for covariances padding in `toTable`
323 maskCovsAmp = np.array([~np.isnan(entry).all() for entry in covsAmp])
324 maskAmp = ~np.isnan(np.array(dictionary['finalMeans'][ampName]))
325 else:
326 maskCovsAmp = np.array([True])
327 maskAmp = np.array([True])
328
329 calib.ampNames.append(ampName)
330 calib.inputExpIdPairs[ampName] = np.array(dictionary['inputExpIdPairs'][ampName]).tolist()
331 calib.expIdMask[ampName] = np.array(dictionary['expIdMask'][ampName]).tolist()
332 calib.rawExpTimes[ampName] = np.array(dictionary['rawExpTimes'][ampName]).tolist()
333 calib.rawMeans[ampName] = np.array(dictionary['rawMeans'][ampName]).tolist()
334 calib.rawVars[ampName] = np.array(dictionary['rawVars'][ampName]).tolist()
335 calib.gain[ampName] = np.array(dictionary['gain'][ampName]).tolist()
336 calib.gainErr[ampName] = np.array(dictionary['gainErr'][ampName]).tolist()
337 calib.noise[ampName] = np.array(dictionary['noise'][ampName]).tolist()
338 calib.noiseErr[ampName] = np.array(dictionary['noiseErr'][ampName]).tolist()
339 calib.ptcFitPars[ampName] = np.array(dictionary['ptcFitPars'][ampName]).tolist()
340 calib.ptcFitParsError[ampName] = np.array(dictionary['ptcFitParsError'][ampName]).tolist()
341 calib.ptcFitChiSq[ampName] = np.array(dictionary['ptcFitChiSq'][ampName]).tolist()
342 calib.ptcTurnoff[ampName] = np.array(dictionary['ptcTurnoff'][ampName]).tolist()
343 calib.covariances[ampName] = covsAmp[maskCovsAmp].tolist()
344 calib.covariancesModel[ampName] = np.array(
345 dictionary['covariancesModel'][ampName]).reshape(
346 (nSignalPoints, covMatrixSide, covMatrixSide))[maskCovsAmp].tolist()
347 calib.covariancesSqrtWeights[ampName] = np.array(
348 dictionary['covariancesSqrtWeights'][ampName]).reshape(
349 (nSignalPoints, covMatrixSide, covMatrixSide))[maskCovsAmp].tolist()
350 calib.aMatrix[ampName] = np.array(dictionary['aMatrix'][ampName]).reshape(
351 (covMatrixSide, covMatrixSide)).tolist()
352 calib.bMatrix[ampName] = np.array(dictionary['bMatrix'][ampName]).reshape(
353 (covMatrixSide, covMatrixSide)).tolist()
354 calib.covariancesModelNoB[ampName] = np.array(
355 dictionary['covariancesModelNoB'][ampName]).reshape(
356 (nSignalPoints, covMatrixSide, covMatrixSide))[maskCovsAmp].tolist()
357 calib.aMatrixNoB[ampName] = np.array(
358 dictionary['aMatrixNoB'][ampName]).reshape((covMatrixSide, covMatrixSide)).tolist()
359 calib.finalVars[ampName] = np.array(dictionary['finalVars'][ampName])[maskAmp].tolist()
360 calib.finalModelVars[ampName] = np.array(dictionary['finalModelVars'][ampName])[maskAmp].tolist()
361 calib.finalMeans[ampName] = np.array(dictionary['finalMeans'][ampName])[maskAmp].tolist()
362 calib.photoCharge[ampName] = np.array(dictionary['photoCharge'][ampName]).tolist()
363 calib.updateMetadata()
364 return calib
365
366 def toDict(self):
367 """Return a dictionary containing the calibration properties.
368 The dictionary should be able to be round-tripped through
369 `fromDict`.
370 Returns
371 -------
372 dictionary : `dict`
373 Dictionary of properties.
374 """
375 self.updateMetadataupdateMetadataupdateMetadata()
376
377 outDict = dict()
378 metadata = self.getMetadatagetMetadata()
379 outDict['metadata'] = metadata
380
381 outDict['ptcFitType'] = self.ptcFitTypeptcFitType
382 outDict['covMatrixSide'] = self.covMatrixSidecovMatrixSide
383 outDict['ampNames'] = self.ampNamesampNames
384 outDict['badAmps'] = self.badAmpsbadAmps
385 outDict['inputExpIdPairs'] = self.inputExpIdPairsinputExpIdPairs
386 outDict['expIdMask'] = self.expIdMaskexpIdMask
387 outDict['rawExpTimes'] = self.rawExpTimesrawExpTimes
388 outDict['rawMeans'] = self.rawMeansrawMeans
389 outDict['rawVars'] = self.rawVarsrawVars
390 outDict['gain'] = self.gaingain
391 outDict['gainErr'] = self.gainErrgainErr
392 outDict['noise'] = self.noisenoise
393 outDict['noiseErr'] = self.noiseErrnoiseErr
394 outDict['ptcFitPars'] = self.ptcFitParsptcFitPars
395 outDict['ptcFitParsError'] = self.ptcFitParsErrorptcFitParsError
396 outDict['ptcFitChiSq'] = self.ptcFitChiSqptcFitChiSq
397 outDict['ptcTurnoff'] = self.ptcTurnoffptcTurnoff
398 outDict['covariances'] = self.covariancescovariances
399 outDict['covariancesModel'] = self.covariancesModelcovariancesModel
400 outDict['covariancesSqrtWeights'] = self.covariancesSqrtWeightscovariancesSqrtWeights
401 outDict['aMatrix'] = self.aMatrixaMatrix
402 outDict['bMatrix'] = self.bMatrixbMatrix
403 outDict['covariancesModelNoB'] = self.covariancesModelNoBcovariancesModelNoB
404 outDict['aMatrixNoB'] = self.aMatrixNoBaMatrixNoB
405 outDict['finalVars'] = self.finalVarsfinalVars
406 outDict['finalModelVars'] = self.finalModelVarsfinalModelVars
407 outDict['finalMeans'] = self.finalMeansfinalMeans
408 outDict['photoCharge'] = self.photoChargephotoCharge
409
410 return outDict
411
412 @classmethod
413 def fromTable(cls, tableList):
414 """Construct calibration from a list of tables.
415 This method uses the `fromDict` method to create the
416 calibration, after constructing an appropriate dictionary from
417 the input tables.
418 Parameters
419 ----------
420 tableList : `list` [`lsst.afw.table.Table`]
421 List of tables to use to construct the datasetPtc.
422 Returns
423 -------
424 calib : `lsst.cp.pipe.`
425 The calibration defined in the tables.
426 """
427 ptcTable = tableList[0]
428
429 metadata = ptcTable.meta
430 inDict = dict()
431 inDict['metadata'] = metadata
432 inDict['ampNames'] = []
433 inDict['ptcFitType'] = []
434 inDict['covMatrixSide'] = []
435 inDict['inputExpIdPairs'] = dict()
436 inDict['expIdMask'] = dict()
437 inDict['rawExpTimes'] = dict()
438 inDict['rawMeans'] = dict()
439 inDict['rawVars'] = dict()
440 inDict['gain'] = dict()
441 inDict['gainErr'] = dict()
442 inDict['noise'] = dict()
443 inDict['noiseErr'] = dict()
444 inDict['ptcFitPars'] = dict()
445 inDict['ptcFitParsError'] = dict()
446 inDict['ptcFitChiSq'] = dict()
447 inDict['ptcTurnoff'] = dict()
448 inDict['covariances'] = dict()
449 inDict['covariancesModel'] = dict()
450 inDict['covariancesSqrtWeights'] = dict()
451 inDict['aMatrix'] = dict()
452 inDict['bMatrix'] = dict()
453 inDict['covariancesModelNoB'] = dict()
454 inDict['aMatrixNoB'] = dict()
455 inDict['finalVars'] = dict()
456 inDict['finalModelVars'] = dict()
457 inDict['finalMeans'] = dict()
458 inDict['badAmps'] = []
459 inDict['photoCharge'] = dict()
460
461 calibVersion = metadata['PTC_VERSION']
462 if calibVersion == 1.0:
463 cls().log.warning(f"Previous version found for PTC dataset: {calibVersion}. "
464 f"Setting 'ptcTurnoff' in all amps to last value in 'finalMeans'.")
465 for record in ptcTable:
466 ampName = record['AMPLIFIER_NAME']
467
468 inDict['ptcFitType'] = record['PTC_FIT_TYPE']
469 inDict['covMatrixSide'] = record['COV_MATRIX_SIDE']
470 inDict['ampNames'].append(ampName)
471 inDict['inputExpIdPairs'][ampName] = record['INPUT_EXP_ID_PAIRS']
472 inDict['expIdMask'][ampName] = record['EXP_ID_MASK']
473 inDict['rawExpTimes'][ampName] = record['RAW_EXP_TIMES']
474 inDict['rawMeans'][ampName] = record['RAW_MEANS']
475 inDict['rawVars'][ampName] = record['RAW_VARS']
476 inDict['gain'][ampName] = record['GAIN']
477 inDict['gainErr'][ampName] = record['GAIN_ERR']
478 inDict['noise'][ampName] = record['NOISE']
479 inDict['noiseErr'][ampName] = record['NOISE_ERR']
480 inDict['ptcFitPars'][ampName] = record['PTC_FIT_PARS']
481 inDict['ptcFitParsError'][ampName] = record['PTC_FIT_PARS_ERROR']
482 inDict['ptcFitChiSq'][ampName] = record['PTC_FIT_CHI_SQ']
483 inDict['covariances'][ampName] = record['COVARIANCES']
484 inDict['covariancesModel'][ampName] = record['COVARIANCES_MODEL']
485 inDict['covariancesSqrtWeights'][ampName] = record['COVARIANCES_SQRT_WEIGHTS']
486 inDict['aMatrix'][ampName] = record['A_MATRIX']
487 inDict['bMatrix'][ampName] = record['B_MATRIX']
488 inDict['covariancesModelNoB'][ampName] = record['COVARIANCES_MODEL_NO_B']
489 inDict['aMatrixNoB'][ampName] = record['A_MATRIX_NO_B']
490 inDict['finalVars'][ampName] = record['FINAL_VARS']
491 inDict['finalModelVars'][ampName] = record['FINAL_MODEL_VARS']
492 inDict['finalMeans'][ampName] = record['FINAL_MEANS']
493 inDict['badAmps'] = record['BAD_AMPS']
494 inDict['photoCharge'][ampName] = record['PHOTO_CHARGE']
495 if calibVersion == 1.0:
496 mask = record['FINAL_MEANS'].mask
497 inDict['ptcTurnoff'][ampName] = record['FINAL_MEANS'][~mask][-1]
498 else:
499 inDict['ptcTurnoff'][ampName] = record['PTC_TURNOFF']
500 return cls().fromDict(inDict)
501
502 def toTable(self):
503 """Construct a list of tables containing the information in this
504 calibration.
505
506 The list of tables should create an identical calibration
507 after being passed to this class's fromTable method.
508 Returns
509 -------
510 tableList : `list` [`astropy.table.Table`]
511 List of tables containing the linearity calibration
512 information.
513 """
514 tableList = []
515 self.updateMetadataupdateMetadataupdateMetadata()
516 nPoints = []
517 for i, ampName in enumerate(self.ampNamesampNames):
518 nPoints.append(len(list(self.covariancescovariances.values())[i]))
519 nSignalPoints = max(nPoints)
520 nPadPoints = {}
521 for i, ampName in enumerate(self.ampNamesampNames):
522 nPadPoints[ampName] = nSignalPoints - len(list(self.covariancescovariances.values())[i])
523 covMatrixSide = self.covMatrixSidecovMatrixSide
524
525 catalog = Table([{'AMPLIFIER_NAME': ampName,
526 'PTC_FIT_TYPE': self.ptcFitTypeptcFitType,
527 'COV_MATRIX_SIDE': self.covMatrixSidecovMatrixSide,
528 'INPUT_EXP_ID_PAIRS': self.inputExpIdPairsinputExpIdPairs[ampName]
529 if len(self.expIdMaskexpIdMask[ampName]) else np.nan,
530 'EXP_ID_MASK': self.expIdMaskexpIdMask[ampName]
531 if len(self.expIdMaskexpIdMask[ampName]) else np.nan,
532 'RAW_EXP_TIMES': np.array(self.rawExpTimesrawExpTimes[ampName]).tolist()
533 if len(self.rawExpTimesrawExpTimes[ampName]) else np.nan,
534 'RAW_MEANS': np.array(self.rawMeansrawMeans[ampName]).tolist()
535 if len(self.rawMeansrawMeans[ampName]) else np.nan,
536 'RAW_VARS': np.array(self.rawVarsrawVars[ampName]).tolist()
537 if len(self.rawVarsrawVars[ampName]) else np.nan,
538 'GAIN': self.gaingain[ampName],
539 'GAIN_ERR': self.gainErrgainErr[ampName],
540 'NOISE': self.noisenoise[ampName],
541 'NOISE_ERR': self.noiseErrnoiseErr[ampName],
542 'PTC_FIT_PARS': np.array(self.ptcFitParsptcFitPars[ampName]).tolist(),
543 'PTC_FIT_PARS_ERROR': np.array(self.ptcFitParsErrorptcFitParsError[ampName]).tolist(),
544 'PTC_FIT_CHI_SQ': self.ptcFitChiSqptcFitChiSq[ampName],
545 'PTC_TURNOFF': self.ptcTurnoffptcTurnoff[ampName],
546 'COVARIANCES': np.pad(np.array(self.covariancescovariances[ampName]),
547 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
548 'constant', constant_values=np.nan).reshape(
549 nSignalPoints*covMatrixSide**2).tolist(),
550 'COVARIANCES_MODEL': np.pad(np.array(self.covariancesModelcovariancesModel[ampName]),
551 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
552 'constant', constant_values=np.nan).reshape(
553 nSignalPoints*covMatrixSide**2).tolist(),
554 'COVARIANCES_SQRT_WEIGHTS': np.pad(np.array(self.covariancesSqrtWeightscovariancesSqrtWeights[ampName]),
555 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
556 'constant', constant_values=0.0).reshape(
557 nSignalPoints*covMatrixSide**2).tolist(),
558 'A_MATRIX': np.array(self.aMatrixaMatrix[ampName]).reshape(covMatrixSide**2).tolist(),
559 'B_MATRIX': np.array(self.bMatrixbMatrix[ampName]).reshape(covMatrixSide**2).tolist(),
560 'COVARIANCES_MODEL_NO_B':
561 np.pad(np.array(self.covariancesModelNoBcovariancesModelNoB[ampName]),
562 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
563 'constant', constant_values=np.nan).reshape(
564 nSignalPoints*covMatrixSide**2).tolist(),
565 'A_MATRIX_NO_B': np.array(self.aMatrixNoBaMatrixNoB[ampName]).reshape(
566 covMatrixSide**2).tolist(),
567 'FINAL_VARS': np.pad(np.array(self.finalVarsfinalVars[ampName]), (0, nPadPoints[ampName]),
568 'constant', constant_values=np.nan).tolist(),
569 'FINAL_MODEL_VARS': np.pad(np.array(self.finalModelVarsfinalModelVars[ampName]),
570 (0, nPadPoints[ampName]),
571 'constant', constant_values=np.nan).tolist(),
572 'FINAL_MEANS': np.pad(np.array(self.finalMeansfinalMeans[ampName]),
573 (0, nPadPoints[ampName]),
574 'constant', constant_values=np.nan).tolist(),
575 'BAD_AMPS': np.array(self.badAmpsbadAmps).tolist() if len(self.badAmpsbadAmps) else np.nan,
576 'PHOTO_CHARGE': np.array(self.photoChargephotoCharge[ampName]).tolist(),
577 } for ampName in self.ampNamesampNames])
578 inMeta = self.getMetadatagetMetadata().toDict()
579 outMeta = {k: v for k, v in inMeta.items() if v is not None}
580 outMeta.update({k: "" for k, v in inMeta.items() if v is None})
581 catalog.meta = outMeta
582 tableList.append(catalog)
583
584 return(tableList)
585
586 def getExpIdsUsed(self, ampName):
587 """Get the exposures used, i.e. not discarded, for a given amp.
588 If no mask has been created yet, all exposures are returned.
589 """
590 if len(self.expIdMaskexpIdMask[ampName]) == 0:
591 return self.inputExpIdPairsinputExpIdPairs[ampName]
592
593 # if the mask exists it had better be the same length as the expIdPairs
594 assert len(self.expIdMaskexpIdMask[ampName]) == len(self.inputExpIdPairsinputExpIdPairs[ampName])
595
596 pairs = self.inputExpIdPairsinputExpIdPairs[ampName]
597 mask = self.expIdMaskexpIdMask[ampName]
598 # cast to bool required because numpy
599 return [(exp1, exp2) for ((exp1, exp2), m) in zip(pairs, mask) if bool(m) is True]
600
601 def getGoodAmps(self):
602 return [amp for amp in self.ampNamesampNames if amp not in self.badAmpsbadAmps]
int max
table::Key< int > to
def requiredAttributes(self, value)
Definition: calibType.py:142
def updateMetadata(self, camera=None, detector=None, filterName=None, setCalibId=False, setCalibInfo=False, setDate=False, **kwargs)
Definition: calibType.py:181
def __init__(self, ampNames=[], ptcFitType=None, covMatrixSide=1, **kwargs)
Definition: ptcDataset.py:163
def updateMetadata(self, setDate=False, **kwargs)
Definition: ptcDataset.py:265
def setAmpValues(self, ampName, inputExpIdPair=[(np.nan, np.nan)], expIdMask=[np.nan], rawExpTime=[np.nan], rawMean=[np.nan], rawVar=[np.nan], photoCharge=[np.nan], gain=np.nan, gainErr=np.nan, noise=np.nan, noiseErr=np.nan, ptcFitPars=[np.nan], ptcFitParsError=[np.nan], ptcFitChiSq=np.nan, ptcTurnoff=np.nan, covArray=[], covArrayModel=[], covSqrtWeights=[], aMatrix=[], bMatrix=[], covArrayModelNoB=[], aMatrixNoB=[], finalVar=[np.nan], finalModelVar=[np.nan], finalMean=[np.nan])
Definition: ptcDataset.py:214
daf::base::PropertyList * list
Definition: fits.cc:913
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Definition: functional.cc:33
bool all(CoordinateExpr< N > const &expr) noexcept
Return true if all elements are true.