200 def apply(cls, candidateList, spatialKernel, spatialBackground, dof=0):
201 """Evaluate the QA metrics for all KernelCandidates in the
202 candidateList; set the values of the metrics in their
203 associated Sources"""
204 for kernelCandidate in candidateList:
205 source = kernelCandidate.getSource()
206 schema = source.schema
207
208
209 if kernelCandidate.getStatus() != afwMath.SpatialCellCandidate.UNKNOWN:
210 kType = getattr(diffimLib.KernelCandidateF, "ORIG")
211 di = kernelCandidate.getDifferenceImage(kType)
212 kernelValues = kernelCandidate.getKernel(kType).getKernelParameters()
213 kernelValues = np.asarray(kernelValues)
214
215 lkim = kernelCandidate.getKernelImage(kType)
216 centx, centy = calcCentroid(lkim.array)
217 stdx, stdy = calcWidth(lkim.array, centx, centy)
218
219
220
221 localResults = cls._calculateStats(di, dof=dof)
222
223 metrics = {"KCDiffimMean_LOCAL": localResults["mean"],
224 "KCDiffimMedian_LOCAL": localResults["median"],
225 "KCDiffimIQR_LOCAL": localResults["iqr"],
226 "KCDiffimStDev_LOCAL": localResults["stdev"],
227 "KCDiffimKSD_LOCAL": localResults["D"],
228 "KCDiffimKSProb_LOCAL": localResults["prob"],
229 "KCDiffimADA2_LOCAL": localResults["A2"],
230 "KCDiffimADCrit_LOCAL": localResults["crit"],
231 "KCDiffimADSig_LOCAL": localResults["sig"],
232 "KCDiffimChiSq_LOCAL": localResults["rchisq"],
233 "KCDiffimMseResids_LOCAL": localResults["mseResids"],
234 "KCKernelCentX_LOCAL": centx,
235 "KCKernelCentY_LOCAL": centy,
236 "KCKernelStdX_LOCAL": stdx,
237 "KCKernelStdY_LOCAL": stdy,
238 "KernelCandidateId_LOCAL": kernelCandidate.getId(),
239 "KernelCoeffValues_LOCAL": kernelValues}
240 for k in metrics:
241 key = schema[k].asKey()
242 setter = getattr(source, "set"+key.getTypeString())
243 setter(key, metrics[k])
244 else:
245 try:
246 kType = getattr(diffimLib.KernelCandidateF, "ORIG")
247 lkim = kernelCandidate.getKernelImage(kType)
248 except Exception:
249 lkim = None
250
251
252
253 skim = afwImage.ImageD(spatialKernel.getDimensions())
254 spatialKernel.computeImage(skim, False, kernelCandidate.getXCenter(),
255 kernelCandidate.getYCenter())
256 centx, centy = calcCentroid(skim.array)
257 stdx, stdy = calcWidth(skim.array, centx, centy)
258
260 sbg = spatialBackground(kernelCandidate.getXCenter(), kernelCandidate.getYCenter())
261 di = kernelCandidate.getDifferenceImage(sk, sbg)
262 spatialResults = cls._calculateStats(di, dof=dof)
263
264
265 if lkim is not None:
266 skim -= lkim
267 bias = np.mean(skim.array)
268 variance = np.mean(np.power(skim.array, 2.))
269 mseKernel = bias**2 + variance
270 else:
271 mseKernel = -99.999
272
273 metrics = {"KCDiffimMean_SPATIAL": spatialResults["mean"],
274 "KCDiffimMedian_SPATIAL": spatialResults["median"],
275 "KCDiffimIQR_SPATIAL": spatialResults["iqr"],
276 "KCDiffimStDev_SPATIAL": spatialResults["stdev"],
277 "KCDiffimKSD_SPATIAL": spatialResults["D"],
278 "KCDiffimKSProb_SPATIAL": spatialResults["prob"],
279 "KCDiffimADA2_SPATIAL": spatialResults["A2"],
280 "KCDiffimADCrit_SPATIAL": spatialResults["crit"],
281 "KCDiffimADSig_SPATIAL": spatialResults["sig"],
282 "KCDiffimChiSq_SPATIAL": spatialResults["rchisq"],
283 "KCDiffimMseResids_SPATIAL": spatialResults["mseResids"],
284 "KCDiffimMseKernel_SPATIAL": mseKernel,
285 "KCKernelCentX_SPATIAL": centx,
286 "KCKernelCentY_SPATIAL": centy,
287 "KCKernelStdX_SPATIAL": stdx,
288 "KCKernelStdY_SPATIAL": stdy,
289 "KernelCandidateId_SPATIAL": kernelCandidate.getId()}
290 for k in metrics:
291 key = schema[k].asKey()
292 setter = getattr(source, "set"+key.getTypeString())
293 setter(key, metrics[k])
294
A kernel created from an Image.