Coverage for python/lsst/cp/verify/verifyPtc.py: 23%

88 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-20 03:20 -0700

1# This file is part of cp_verify. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21import numpy as np 

22import lsst.pex.config as pexConfig 

23 

24import lsst.pipe.base.connectionTypes as cT 

25from .verifyCalib import CpVerifyCalibConfig, CpVerifyCalibTask, CpVerifyCalibConnections 

26 

27__all__ = ['CpVerifyPtcConnections', 'CpVerifyPtcConfig', 'CpVerifyPtcTask'] 

28 

29 

30class CpVerifyPtcConnections(CpVerifyCalibConnections, 

31 dimensions={"instrument", "detector"}, 

32 defaultTemplates={}): 

33 inputCalib = cT.Input( 

34 name="calib", 

35 doc="Input calib to calculate statistics for.", 

36 storageClass="PhotonTransferCurveDataset", 

37 dimensions=["instrument", "detector"], 

38 isCalibration=True 

39 ) 

40 

41 

42class CpVerifyPtcConfig(CpVerifyCalibConfig, 

43 pipelineConnections=CpVerifyPtcConnections): 

44 """Inherits from base CpVerifyCalibConfig.""" 

45 

46 def setDefaults(self): 

47 super().setDefaults() 

48 

49 gainThreshold = pexConfig.Field( 

50 dtype=float, 

51 doc="Maximum percentage difference between PTC gain and nominal amplifier gain.", 

52 default=5.0, 

53 ) 

54 

55 noiseThreshold = pexConfig.Field( 

56 dtype=float, 

57 doc="Maximum percentage difference between PTC readout noise and nominal " 

58 "amplifier readout noise.", 

59 default=5.0, 

60 ) 

61 

62 turnoffThreshold = pexConfig.Field( 

63 dtype=float, 

64 doc="Minimun full well requirement (in electrons). To be compared with the " 

65 "reported PTC turnoff per amplifier.", 

66 default=90000, 

67 ) 

68 

69 a00MinITL = pexConfig.Field( 

70 dtype=float, 

71 doc="Minimum a00 (c.f., Astier+19) for ITL CCDs.", 

72 default=-4.56e-6, 

73 ) 

74 

75 a00MaxITL = pexConfig.Field( 

76 dtype=float, 

77 doc="Maximum a00 (c.f., Astier+19) for ITL CCDs.", 

78 default=6.91e-7, 

79 ) 

80 

81 a00MinE2V = pexConfig.Field( 

82 dtype=float, 

83 doc="Minimum a00 (c.f., Astier+19) for E2V CCDs.", 

84 default=-3.52e-6, 

85 ) 

86 

87 a00MaxE2V = pexConfig.Field( 

88 dtype=float, 

89 doc="Maximum a00 (c.f., Astier+19) for E2V CCDs.", 

90 default=-2.61e-6, 

91 ) 

92 

93 

94class CpVerifyPtcTask(CpVerifyCalibTask): 

95 """PTC verification sub-class, implementing the verify method. 

96 """ 

97 ConfigClass = CpVerifyPtcConfig 

98 _DefaultName = 'cpVerifyPtc' 

99 

100 def detectorStatistics(self, inputCalib, camera=None): 

101 """Calculate detector level statistics from the calibration. 

102 

103 Parameters 

104 ---------- 

105 inputCalib : `lsst.ip.isr.IsrCalib` 

106 The calibration to verify. 

107 

108 Returns 

109 ------- 

110 outputStatistics : `dict` [`str`, scalar] 

111 A dictionary of the statistics measured and their values. 

112 """ 

113 return {} 

114 

115 def amplifierStatistics(self, inputCalib, camera=None): 

116 """Calculate detector level statistics from the calibration. 

117 

118 Parameters 

119 ---------- 

120 inputCalib : `lsst.ip.isr.IsrCalib` 

121 The calibration to verify. 

122 

123 Returns 

124 ------- 

125 outputStatistics : `dict` [`str`, scalar] 

126 A dictionary of the statistics measured and their values. 

127 """ 

128 outputStatistics = {} 

129 calibMetadata = inputCalib.getMetadata().toDict() 

130 detId = calibMetadata['DETECTOR'] 

131 detector = camera[detId] 

132 ptcFitType = calibMetadata['PTC_FIT_TYPE'] 

133 for amp in detector: 

134 ampName = amp.getName() 

135 outputStatistics['PTC_GAIN'] = inputCalib.gain[ampName] 

136 outputStatistics['AMP_GAIN'] = amp.getGain() 

137 outputStatistics['PTC_NOISE'] = inputCalib.noise[ampName] 

138 outputStatistics['AMP_NOISE'] = amp.getReadNoise() 

139 outputStatistics['PTC_TURNOFF'] = inputCalib.ptcTurnoff[ampName] 

140 if ptcFitType == 'EXPAPPROXIMATION': 

141 outputStatistics['PTC_BFE_A00'] = inputCalib.ptcFitPars[ampName][0] 

142 if ptcFitType == 'FULLCOVARIANCE': 

143 outputStatistics['PTC_BFE_A00'] = inputCalib.aMatrix[ampName][0][0] 

144 

145 return outputStatistics 

146 

147 def verify(self, calib, statisticsDict, camera=None): 

148 """Verify that the calibration meets the verification criteria. 

149 

150 Parameters 

151 ---------- 

152 inputCalib : `lsst.ip.isr.IsrCalib` 

153 The calibration to verify. 

154 statisticsDictionary : `dict` [`str`, `dict` [`str`, scalar]], 

155 Dictionary of measured statistics. The inner dictionary 

156 should have keys that are statistic names (`str`) with 

157 values that are some sort of scalar (`int` or `float` are 

158 the mostly likely types). 

159 camera : `lsst.afw.cameraGeom.Camera`, optional 

160 Input camera to get detectors from. 

161 

162 Returns 

163 ------- 

164 outputStatistics : `dict` [`str`, `dict` [`str`, `bool`]] 

165 A dictionary indexed by the amplifier name, containing 

166 dictionaries of the verification criteria. 

167 success : `bool` 

168 A boolean indicating whether all tests have passed. 

169 """ 

170 verifyStats = {} 

171 success = True 

172 calibMetadata = calib.getMetadata().toDict() 

173 detId = calibMetadata['DETECTOR'] 

174 detector = camera[detId] 

175 ptcFitType = calibMetadata['PTC_FIT_TYPE'] 

176 # 'DET_SER' is of the form 'ITL-3800C-229' 

177 detVendor = calibMetadata['DET_SER'].split('-')[0] 

178 

179 for amp in detector: 

180 verify = {} 

181 ampName = amp.getName() 

182 diffGain = np.abs(calib.gain[ampName] - amp.getGain()) / amp.getGain() 

183 diffNoise = np.abs(calib.noise[ampName] - amp.getReadNoise()) / amp.getReadNoise() 

184 

185 # DMTN-101: 16.1 and 16.2 

186 # The fractional relative difference between the fitted PTC and the 

187 # nominal amplifier gain and readout noise values should be less 

188 # than a certain threshold (default: 5%). 

189 verify['GAIN'] = bool(diffGain < self.config.gainThreshold) 

190 verify['NOISE'] = bool(diffNoise < self.config.noiseThreshold) 

191 

192 # DMTN-101: 16.3 

193 # Check that the measured PTC turnoff is at least greater than the 

194 # full-well requirement of 90k e-. 

195 turnoffCut = self.config.turnoffThreshold 

196 verify['PTC_TURNOFF'] = bool(calib.ptcTurnoff[ampName]*calib.gain[ampName] > turnoffCut) 

197 # DMTN-101: 16.4 

198 # Check the a00 value (brighter-fatter effect). 

199 # This is a purely electrostatic parameter that should not change 

200 # unless voltages are changed (e.g., parallel, bias voltages). 

201 # Check that the fitted a00 parameter per CCD vendor is within a 

202 # range motivated by measurements on data (DM-30171). 

203 if ptcFitType in ['EXPAPPROXIMATION', 'FULLCOVARIANCE']: 

204 # a00 is a fit parameter from these models. 

205 if ptcFitType == 'EXPAPPROXIMATION': 

206 a00 = calib.ptcFitPars[ampName][0] 

207 else: 

208 a00 = calib.aMatrix[ampName][0][0] 

209 if detVendor == 'ITL': 

210 a00Max = self.config.a00MaxITL 

211 a00Min = self.config.a00MinITL 

212 verify['BFE_A00'] = bool(a00 > a00Min and a00 < a00Max) 

213 elif detVendor == 'E2V': 

214 a00Max = self.config.a00MaxE2V 

215 a00Min = self.config.a00MinE2V 

216 verify['BFE_A00'] = bool(a00 > a00Min and a00 < a00Max) 

217 else: 

218 raise RuntimeError(f"Detector type {detVendor} not one of 'ITL' or 'E2V'") 

219 # Overall success among all tests for this amp. 

220 verify['SUCCESS'] = bool(np.all(list(verify.values()))) 

221 if verify['SUCCESS'] is False: 

222 success = False 

223 

224 verifyStats[ampName] = verify 

225 

226 # Loop over amps to make a detector summary. 

227 verifyDetStats = {'GAIN': [], 'NOISE': [], 'PTC_TURNOFF': [], 'BFE_A00': []} 

228 for amp in verifyStats: 

229 for testName in verifyStats[amp]: 

230 if testName == 'SUCCESS': 

231 continue 

232 verifyDetStats[testName].append(verifyStats[amp][testName]) 

233 

234 # If ptc model did not fit for a00 (e.g., POLYNOMIAL) 

235 if not len(verifyDetStats['BFE_A00']): 

236 verifyDetStats.pop('BFE_A00') 

237 

238 # VerifyDetStatsFinal has final boolean test over all amps 

239 verifyDetStatsFinal = {} 

240 for testName in verifyDetStats: 

241 testBool = bool(np.all(list(verifyDetStats[testName]))) 

242 # Save the tests that failed 

243 if not testBool: 

244 verifyDetStatsFinal[testName] = bool(np.all(list(verifyDetStats[testName]))) 

245 

246 return verifyDetStatsFinal, bool(success)