23Define dataset class for MeasurePhotonTransferCurve task
26__all__ = [
'PhotonTransferCurveDataset']
29from astropy.table
import Table
35 """A simple class to hold the output data from the PTC task.
37 The dataset is made up of a dictionary
for each item, keyed by the
38 amplifiers
' names, which much be supplied at construction time.
39 New items cannot be added to the class to save accidentally saving
to the
40 wrong property,
and the
class can be frozen if desired.
41 inputExpIdPairs records the exposures used to produce the data.
42 When fitPtc()
or fitCovariancesAstier()
is run, a mask
is built up, which
43 is by definition always the same length
as inputExpIdPairs, rawExpTimes,
44 rawMeans
and rawVars,
and is a list of bools, which are incrementally set
45 to
False as points are discarded
from the fits.
46 PTC fit parameters
for polynomials are stored
in a list
in ascending order
47 of polynomial term, i.e. par[0]*x^0 + par[1]*x + par[2]*x^2 etc
48 with the length of the list corresponding to the order of the polynomial
54 List
with the names of the amplifiers of the detector at hand.
56 Type of model fitted to the PTC:
"POLYNOMIAL",
"EXPAPPROXIMATION",
59 Maximum lag of covariances (size of square covariance matrices).
60 kwargs : `dict`, optional
61 Other keyword arguments to
pass to the parent init.
65 The stored attributes are:
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
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
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
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"].
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
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)
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
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
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
145 photoCharge : `dict`, [`str`, `list`]
146 Dictionary keyed by amp names containing the integrated photocharge
147 for linearity calibration.
149 Version 1.1 adds the `ptcTurnoff` attribute.
153 _SCHEMA =
'Gen3 Photon Transfer Curve'
156 def __init__(self, ampNames=[], ptcFitType=None, covMatrixSide=1, **kwargs):
166 self.
rawMeans = {ampName: []
for ampName
in ampNames}
167 self.
rawVars = {ampName: []
for ampName
in ampNames}
170 self.
gain = {ampName: np.nan
for ampName
in ampNames}
171 self.
gainErr = {ampName: np.nan
for ampName
in ampNames}
172 self.
noise = {ampName: np.nan
for ampName
in ampNames}
173 self.
noiseErr = {ampName: np.nan
for ampName
in ampNames}
183 self.
aMatrix = {ampName: np.nan
for ampName
in ampNames}
184 self.
bMatrix = {ampName: np.nan
for ampName
in ampNames}
194 'rawMeans',
'rawVars',
'gain',
'gainErr',
'noise',
'noiseErr',
195 'ptcFitPars',
'ptcFitParsError',
'ptcFitChiSq',
'ptcTurnoff',
196 'aMatrixNoB',
'covariances',
'covariancesModel',
197 'covariancesSqrtWeights',
'covariancesModelNoB',
198 'aMatrix',
'bMatrix',
'finalVars',
'finalModelVars',
'finalMeans',
203 def setAmpValues(self, ampName, inputExpIdPair=[(np.nan, np.nan)], expIdMask=[np.nan],
204 rawExpTime=[np.nan], rawMean=[np.nan], rawVar=[np.nan], photoCharge=[np.nan],
205 gain=np.nan, gainErr=np.nan, noise=np.nan, noiseErr=np.nan, ptcFitPars=[np.nan],
206 ptcFitParsError=[np.nan], ptcFitChiSq=np.nan, ptcTurnoff=np.nan, covArray=[],
207 covArrayModel=[], covSqrtWeights=[], aMatrix=[], bMatrix=[], covArrayModelNoB=[],
208 aMatrixNoB=[], finalVar=[np.nan], finalModelVar=[np.nan], finalMean=[np.nan]):
209 """Function to initialize an amp of a PhotonTransferCurveDataset.
213 The parameters are all documented in `init`.
216 if len(covArray) == 0:
217 covArray = [nanMatrix]
218 if len(covArrayModel) == 0:
219 covArrayModel = [nanMatrix]
220 if len(covSqrtWeights) == 0:
221 covSqrtWeights = [nanMatrix]
222 if len(covArrayModelNoB) == 0:
223 covArrayModelNoB = [nanMatrix]
224 if len(aMatrix) == 0:
226 if len(bMatrix) == 0:
228 if len(aMatrixNoB) == 0:
229 aMatrixNoB = nanMatrix
237 self.
gain[ampName] = gain
238 self.
gainErr[ampName] = gainErr
239 self.
noise[ampName] = noise
249 self.
aMatrix[ampName] = aMatrix
250 self.
bMatrix[ampName] = bMatrix
260 """Update calibration metadata.
261 This calls the base class's method after ensuring the required
262 calibration keywords will be saved.
266 setDate : `bool`, optional
267 Update the CALIBDATE fields in the metadata to the current
268 time. Defaults to
False.
270 Other keyword parameters to set
in the metadata.
276 """Construct a calibration from a dictionary of properties.
277 Must be implemented by the specific calibration subclasses.
282 Dictionary of properties.
287 Constructed calibration.
292 Raised if the supplied dictionary
is for a different
296 if calib._OBSTYPE != dictionary[
'metadata'][
'OBSTYPE']:
297 raise RuntimeError(f
"Incorrect Photon Transfer Curve dataset supplied. "
298 f
"Expected {calib._OBSTYPE}, found {dictionary['metadata']['OBSTYPE']}")
299 calib.setMetadata(dictionary[
'metadata'])
300 calib.ptcFitType = dictionary[
'ptcFitType']
301 calib.covMatrixSide = dictionary[
'covMatrixSide']
302 calib.badAmps = np.array(dictionary[
'badAmps'],
'str').tolist()
305 covMatrixSide = calib.covMatrixSide
307 covDimensionsProduct = len(np.array(
list(dictionary[
'covariances'].values())[0]).ravel())
308 nSignalPoints = int(covDimensionsProduct/(covMatrixSide*covMatrixSide))
310 for ampName
in dictionary[
'ampNames']:
311 covsAmp = np.array(dictionary[
'covariances'][ampName]).reshape((nSignalPoints, covMatrixSide,
320 maskCovsAmp = np.array([~np.isnan(entry).all()
for entry
in covsAmp])
321 maskAmp = ~np.isnan(np.array(dictionary[
'finalMeans'][ampName]))
323 maskCovsAmp = np.array([
True])
324 maskAmp = np.array([
True])
326 calib.ampNames.append(ampName)
327 calib.inputExpIdPairs[ampName] = np.array(dictionary[
'inputExpIdPairs'][ampName]).tolist()
328 calib.expIdMask[ampName] = np.array(dictionary[
'expIdMask'][ampName]).tolist()
329 calib.rawExpTimes[ampName] = np.array(dictionary[
'rawExpTimes'][ampName]).tolist()
330 calib.rawMeans[ampName] = np.array(dictionary[
'rawMeans'][ampName]).tolist()
331 calib.rawVars[ampName] = np.array(dictionary[
'rawVars'][ampName]).tolist()
332 calib.gain[ampName] = np.array(dictionary[
'gain'][ampName]).tolist()
333 calib.gainErr[ampName] = np.array(dictionary[
'gainErr'][ampName]).tolist()
334 calib.noise[ampName] = np.array(dictionary[
'noise'][ampName]).tolist()
335 calib.noiseErr[ampName] = np.array(dictionary[
'noiseErr'][ampName]).tolist()
336 calib.ptcFitPars[ampName] = np.array(dictionary[
'ptcFitPars'][ampName]).tolist()
337 calib.ptcFitParsError[ampName] = np.array(dictionary[
'ptcFitParsError'][ampName]).tolist()
338 calib.ptcFitChiSq[ampName] = np.array(dictionary[
'ptcFitChiSq'][ampName]).tolist()
339 calib.ptcTurnoff[ampName] = np.array(dictionary[
'ptcTurnoff'][ampName]).tolist()
340 calib.covariances[ampName] = covsAmp[maskCovsAmp].tolist()
341 calib.covariancesModel[ampName] = np.array(
342 dictionary[
'covariancesModel'][ampName]).reshape(
343 (nSignalPoints, covMatrixSide, covMatrixSide))[maskCovsAmp].tolist()
344 calib.covariancesSqrtWeights[ampName] = np.array(
345 dictionary[
'covariancesSqrtWeights'][ampName]).reshape(
346 (nSignalPoints, covMatrixSide, covMatrixSide))[maskCovsAmp].tolist()
347 calib.aMatrix[ampName] = np.array(dictionary[
'aMatrix'][ampName]).reshape(
348 (covMatrixSide, covMatrixSide)).tolist()
349 calib.bMatrix[ampName] = np.array(dictionary[
'bMatrix'][ampName]).reshape(
350 (covMatrixSide, covMatrixSide)).tolist()
351 calib.covariancesModelNoB[ampName] = np.array(
352 dictionary[
'covariancesModelNoB'][ampName]).reshape(
353 (nSignalPoints, covMatrixSide, covMatrixSide))[maskCovsAmp].tolist()
354 calib.aMatrixNoB[ampName] = np.array(
355 dictionary[
'aMatrixNoB'][ampName]).reshape((covMatrixSide, covMatrixSide)).tolist()
356 calib.finalVars[ampName] = np.array(dictionary[
'finalVars'][ampName])[maskAmp].tolist()
357 calib.finalModelVars[ampName] = np.array(dictionary[
'finalModelVars'][ampName])[maskAmp].tolist()
358 calib.finalMeans[ampName] = np.array(dictionary[
'finalMeans'][ampName])[maskAmp].tolist()
359 calib.photoCharge[ampName] = np.array(dictionary[
'photoCharge'][ampName]).tolist()
360 calib.updateMetadata()
364 """Return a dictionary containing the calibration properties.
365 The dictionary should be able to be round-tripped through
371 Dictionary of properties.
377 outDict['metadata'] = metadata
382 outDict[
'badAmps'] = self.
badAmps
387 outDict[
'rawVars'] = self.
rawVars
388 outDict[
'gain'] = self.
gain
389 outDict[
'gainErr'] = self.
gainErr
390 outDict[
'noise'] = self.
noise
399 outDict[
'aMatrix'] = self.
aMatrix
400 outDict[
'bMatrix'] = self.
bMatrix
412 """Construct calibration from a list of tables.
413 This method uses the `fromDict` method to create the
414 calibration, after constructing an appropriate dictionary from
419 tableList : `list` [`lsst.afw.table.Table`]
420 List of tables to use to construct the datasetPtc.
425 The calibration defined
in the tables.
427 ptcTable = tableList[0]
429 metadata = ptcTable.meta
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()
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']
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 array = record[
'FINAL_MEANS'][~mask]
499 inDict[
'ptcTurnoff'][ampName] = record[
'FINAL_MEANS'][~mask][-1]
501 inDict[
'ptcTurnoff'][ampName] = np.nan
503 inDict[
'ptcTurnoff'][ampName] = record[
'PTC_TURNOFF']
507 """Construct a list of tables containing the information in this
510 The list of tables should create an identical calibration
511 after being passed to this class's fromTable method.
515 tableList : `list` [`astropy.table.Table`]
516 List of tables containing the linearity calibration
522 for i, ampName
in enumerate(self.
ampNames):
524 nSignalPoints =
max(nPoints)
526 for i, ampName
in enumerate(self.
ampNames):
527 nPadPoints[ampName] = nSignalPoints - len(
list(self.
covariances.values())[i])
530 catalog = Table([{
'AMPLIFIER_NAME': ampName,
534 if len(self.
expIdMask[ampName])
else np.nan,
536 if len(self.
expIdMask[ampName])
else np.nan,
537 'RAW_EXP_TIMES': np.array(self.
rawExpTimes[ampName]).tolist()
539 'RAW_MEANS': np.array(self.
rawMeans[ampName]).tolist()
540 if len(self.
rawMeans[ampName])
else np.nan,
541 'RAW_VARS': np.array(self.
rawVars[ampName]).tolist()
542 if len(self.
rawVars[ampName])
else np.nan,
543 'GAIN': self.
gain[ampName],
544 'GAIN_ERR': self.
gainErr[ampName],
545 'NOISE': self.
noise[ampName],
546 'NOISE_ERR': self.
noiseErr[ampName],
547 'PTC_FIT_PARS': np.array(self.
ptcFitPars[ampName]).tolist(),
548 'PTC_FIT_PARS_ERROR': np.array(self.
ptcFitParsError[ampName]).tolist(),
551 'COVARIANCES': np.pad(np.array(self.
covariances[ampName]),
552 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
553 'constant', constant_values=np.nan).reshape(
554 nSignalPoints*covMatrixSide**2).tolist(),
556 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
557 'constant', constant_values=np.nan).reshape(
558 nSignalPoints*covMatrixSide**2).tolist(),
560 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
561 'constant', constant_values=0.0).reshape(
562 nSignalPoints*covMatrixSide**2).tolist(),
563 'A_MATRIX': np.array(self.
aMatrix[ampName]).reshape(covMatrixSide**2).tolist(),
564 'B_MATRIX': np.array(self.
bMatrix[ampName]).reshape(covMatrixSide**2).tolist(),
565 'COVARIANCES_MODEL_NO_B':
567 ((0, nPadPoints[ampName]), (0, 0), (0, 0)),
568 'constant', constant_values=np.nan).reshape(
569 nSignalPoints*covMatrixSide**2).tolist(),
570 'A_MATRIX_NO_B': np.array(self.
aMatrixNoB[ampName]).reshape(
571 covMatrixSide**2).tolist(),
572 'FINAL_VARS': np.pad(np.array(self.
finalVars[ampName]), (0, nPadPoints[ampName]),
573 'constant', constant_values=np.nan).tolist(),
574 'FINAL_MODEL_VARS': np.pad(np.array(self.
finalModelVars[ampName]),
575 (0, nPadPoints[ampName]),
576 'constant', constant_values=np.nan).tolist(),
577 'FINAL_MEANS': np.pad(np.array(self.
finalMeans[ampName]),
578 (0, nPadPoints[ampName]),
579 'constant', constant_values=np.nan).tolist(),
580 'BAD_AMPS': np.array(self.
badAmps).tolist()
if len(self.
badAmps)
else np.nan,
581 'PHOTO_CHARGE': np.array(self.
photoCharge[ampName]).tolist(),
584 outMeta = {k: v
for k, v
in inMeta.items()
if v
is not None}
585 outMeta.update({k:
"" for k, v
in inMeta.items()
if v
is None})
586 catalog.meta = outMeta
587 tableList.append(catalog)
592 """Read metadata parameters from a detector.
596 detector : `lsst.afw.cameraGeom.detector`
597 Input detector with parameters to use.
602 The calibration constructed
from the detector.
608 """Get the exposures used, i.e. not discarded, for a given amp.
609 If no mask has been created yet, all exposures are returned.
620 return [(exp1, exp2)
for ((exp1, exp2), m)
in zip(pairs, mask)
if bool(m)
is True]
def requiredAttributes(self, value)
def updateMetadata(self, camera=None, detector=None, filterName=None, setCalibId=False, setCalibInfo=False, setDate=False, **kwargs)
def requiredAttributes(self)
def __init__(self, ampNames=[], ptcFitType=None, covMatrixSide=1, **kwargs)
def getExpIdsUsed(self, ampName)
def fromDict(cls, dictionary)
def fromDetector(self, detector)
def updateMetadata(self, **kwargs)
def fromTable(cls, tableList)
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])
daf::base::PropertyList * list