LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa Class Reference
Inheritance diagram for lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa:

Public Member Functions

def __init__ (self, nKernelSpatial)
 
def makeMetricMap (self)
 
def addToSchema (self, inSourceCatalog)
 
def apply (cls, candidateList, spatialKernel, spatialBackground, dof=0)
 

Static Public Member Functions

def aggregate (sourceCatalog, metadata, wcsresids, diaSources=None)
 

Public Attributes

 fields
 

Detailed Description

Quality Assessment class for Kernel Candidates

Definition at line 36 of file kernelCandidateQa.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa.__init__ (   self,
  nKernelSpatial 
)
Class to undertake QA of KernelCandidates after modeling of
the Psf-matching kernel.  Both directly--fitted diffim (LOCAL)
and spatially--interpolated kernel diffim (SPATIAL) metrics
are calculated, based on the distribution of residuals in the
KernelCandidates stamp.

Parameters
----------
nKernelSpatial : `int`
    Number of terms in the spatial model; needed to initialize per-basis QA arrays

Definition at line 39 of file kernelCandidateQa.py.

39  def __init__(self, nKernelSpatial):
40  """Class to undertake QA of KernelCandidates after modeling of
41  the Psf-matching kernel. Both directly--fitted diffim (LOCAL)
42  and spatially--interpolated kernel diffim (SPATIAL) metrics
43  are calculated, based on the distribution of residuals in the
44  KernelCandidates stamp.
45 
46  Parameters
47  ----------
48  nKernelSpatial : `int`
49  Number of terms in the spatial model; needed to initialize per-basis QA arrays
50  """
51  self.fields = []
52  self.fields.append(afwTable.Field["PointD"](
53  "RegisterRefPosition",
54  "Position of reference object for registration (radians)."))
55  # TODO check units of the following angles
56  self.fields.append(afwTable.Field["Angle"]("RegisterResidualBearing",
57  "Angle of residual wrt declination parallel in radians"))
58 
59  self.fields.append(afwTable.Field["Angle"]("RegisterResidualDistance",
60  "Offset of residual in radians"))
61  metricMap = self.makeMetricMap()
62 
63  for kType in ("LOCAL", "SPATIAL"):
64  for k in metricMap:
65  commentAndUnit = metricMap[k]['comment']
66  self.fields.append(afwTable.Field[metricMap[k]['type']](k%(kType), *commentAndUnit))
67 
68  self.fields.append(afwTable.Field["I"]("KCKernelStatus_LOCAL",
69  "Status of the KernelCandidate"))
70 
71  self.fields.append(afwTable.Field["ArrayD"]("KernelCoeffValues_LOCAL",
72  "Original basis coefficients",
73  nKernelSpatial))
74 
75  self.fields.append(afwTable.Field["F"]("BackgroundValue_LOCAL",
76  "Evaluation of background model at this point"))
77 
78  self.fields.append(afwTable.Field["F"]("KCDiffimMseKernel_SPATIAL",
79  "Mean squared error of spatial kernel estimate"))
80 
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Definition: functional.cc:33
A description of a field in a table.
Definition: Field.h:24

Member Function Documentation

◆ addToSchema()

def lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa.addToSchema (   self,
  inSourceCatalog 
)
Add the to-be-generated QA keys to the Source schema

Definition at line 113 of file kernelCandidateQa.py.

113  def addToSchema(self, inSourceCatalog):
114  """Add the to-be-generated QA keys to the Source schema"""
115  schema = inSourceCatalog.getSchema()
116  inKeys = []
117  psfDef = inSourceCatalog.schema.getAliasMap().get("slot_PsfFlux")
118  centroidDef = inSourceCatalog.getCentroidDefinition()
119  shapeDef = inSourceCatalog.getShapeDefinition()
120  for n in schema.getNames():
121  inKeys.append(schema[n].asKey())
122 
123  for field in self.fields:
124  schema.addField(field)
125  outSourceCatalog = afwTable.SourceCatalog(schema)
126  for source in inSourceCatalog:
127  rec = outSourceCatalog.addNew()
128  for k in inKeys:
129  if k.getTypeString() == 'Coord':
130  rec.setCoord(source.getCoord())
131  else:
132  setter = getattr(rec, "set"+k.getTypeString())
133  getter = getattr(source, "get"+k.getTypeString())
134  setter(k, getter(k))
135  outSourceCatalog.definePsfFlux(psfDef)
136  outSourceCatalog.defineCentroid(centroidDef)
137  outSourceCatalog.defineShape(shapeDef)
138  return outSourceCatalog
139 

◆ aggregate()

def lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa.aggregate (   sourceCatalog,
  metadata,
  wcsresids,
  diaSources = None 
)
static
Generate aggregate metrics (e.g. total numbers of false
positives) from all the Sources in the sourceCatalog

Definition at line 296 of file kernelCandidateQa.py.

296  def aggregate(sourceCatalog, metadata, wcsresids, diaSources=None):
297  """Generate aggregate metrics (e.g. total numbers of false
298  positives) from all the Sources in the sourceCatalog"""
299  for source in sourceCatalog:
300  sourceId = source.getId()
301  if sourceId in wcsresids:
302  # Note that the residuals are not delta RA, delta Dec
303  # From the source code "bearing (angle wrt a declination parallel) and distance
304  coord, resids = wcsresids[sourceId]
305  key = source.schema["RegisterResidualBearing"].asKey()
306  setter = getattr(source, "set"+key.getTypeString())
307  setter(key, resids[0])
308  key = source.schema["RegisterResidualDistance"].asKey()
309  setter = getattr(source, "set"+key.getTypeString())
310  setter(key, resids[1])
311  key = source.schema["RegisterRefPosition"].asKey()
312  setter = getattr(source, "set"+key.getTypeString())
313  setter(key, geom.Point2D(coord.getRa().asRadians(),
314  coord.getDec().asRadians()))
315  if diaSources:
316  metadata.add("NFalsePositivesTotal", len(diaSources))
317  nRefMatch = 0
318  nSrcMatch = 0
319  nunmatched = 0
320  for source in diaSources:
321  refId = source.get("refMatchId")
322  srcId = source.get("srcMatchId")
323  if refId > 0:
324  nRefMatch += 1
325  if srcId > 0:
326  nSrcMatch += 1
327  if refId == 0 and srcId == 0:
328  nunmatched += 1
329  metadata.add("NFalsePositivesRefAssociated", nRefMatch)
330  metadata.add("NFalsePositivesSrcAssociated", nSrcMatch)
331  metadata.add("NFalsePositivesUnassociated", nunmatched)
332  for kType in ("LOCAL", "SPATIAL"):
333  for sName in ("KCDiffimMean", "KCDiffimMedian", "KCDiffimIQR", "KCDiffimStDev",
334  "KCDiffimKSProb", "KCDiffimADSig", "KCDiffimChiSq",
335  "KCDiffimMseResids", "KCDiffimMseKernel"):
336  if sName == "KCDiffimMseKernel" and kType == "LOCAL":
337  continue
338  kName = "%s_%s" % (sName, kType)
339  vals = np.array([s.get(kName) for s in sourceCatalog])
340  idx = np.isfinite(vals)
341  metadata.add("%s_MEAN" % (kName), np.mean(vals[idx]))
342  metadata.add("%s_MEDIAN" % (kName), np.median(vals[idx]))
343  metadata.add("%s_STDEV" % (kName), np.std(vals[idx]))

◆ apply()

def lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa.apply (   cls,
  candidateList,
  spatialKernel,
  spatialBackground,
  dof = 0 
)
Evaluate the QA metrics for all KernelCandidates in the
candidateList; set the values of the metrics in their
associated Sources

Definition at line 200 of file kernelCandidateQa.py.

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  # Calculate ORIG stats (original basis fit)
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.getArray())
217  stdx, stdy = calcWidth(lkim.getArray(), centx, centy)
218  # NOTE
219  # What is the difference between kernelValues and solution?
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  # Calculate spatial model evaluated at each position, for
252  # all candidates
253  skim = afwImage.ImageD(spatialKernel.getDimensions())
254  spatialKernel.computeImage(skim, False, kernelCandidate.getXCenter(),
255  kernelCandidate.getYCenter())
256  centx, centy = calcCentroid(skim.getArray())
257  stdx, stdy = calcWidth(skim.getArray(), centx, centy)
258 
259  sk = afwMath.FixedKernel(skim)
260  sbg = spatialBackground(kernelCandidate.getXCenter(), kernelCandidate.getYCenter())
261  di = kernelCandidate.getDifferenceImage(sk, sbg)
262  spatialResults = cls._calculateStats(di, dof=dof)
263 
264  # Kernel mse
265  if lkim is not None:
266  skim -= lkim
267  bias = np.mean(skim.getArray())
268  variance = np.mean(np.power(skim.getArray(), 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.
Definition: Kernel.h:471
def calcWidth(arr, centx, centy)
Definition: utils.py:829
def calcCentroid(arr)
Definition: utils.py:814

◆ makeMetricMap()

def lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa.makeMetricMap (   self)

Definition at line 81 of file kernelCandidateQa.py.

81  def makeMetricMap(self):
82  nameList = ['KCDiffimMean_%s', 'KCDiffimMedian_%s', 'KCDiffimIQR_%s', 'KCDiffimStDev_%s',
83  'KCDiffimKSD_%s', 'KCDiffimKSProb_%s', 'KCDiffimADA2_%s', 'KCDiffimADCrit_%s',
84  'KCDiffimADSig_%s', 'KCDiffimChiSq_%s', 'KCDiffimMseResids_%s', 'KCKernelCentX_%s',
85  'KCKernelCentY_%s', 'KCKernelStdX_%s', 'KCKernelStdY_%s', 'KernelCandidateId_%s']
86  typeList = ['F', 'F', 'F', 'F', 'F', 'F', 'F', 'ArrayD', 'ArrayD', 'F', 'F', 'F',
87  'F', 'F', 'F', 'I']
88  commentList = [
89  ("Mean of KernelCandidate diffim", "sigma"),
90  ("Median of KernelCandidate diffim", "sigma"),
91  ("Inner quartile range of KernelCandidate diffim", "sigma"),
92  ("Standard deviation of KernelCandidate diffim", "sigma"),
93  ("D from K-S test of diffim pixels relative to Normal", ),
94  ("Prob from K-S test of diffim pixels relative to Normal", "likelihood"),
95  ("Anderson-Darling test statistic of diffim pixels relative to Normal", ),
96  ("Critical values for the significance levels in KCDiffimADSig. If A2 is greater "
97  "than this number, hypothesis that the distributions are similar can be rejected.", 5),
98  ("Anderson-Darling significance levels for the Normal distribution", 5),
99  ("Reduced chi^2 of the residual.", "likelihood"),
100  ("Mean squared error in diffim : Variance + Bias**2",),
101  ("Centroid in X for this Kernel", "pixel"),
102  ("Centroid in Y for this Kernel", "pixel"),
103  ("Standard deviation in X for this Kernel", "pixel"),
104  ("Standard deviation in Y for this Kernel", "pixel"),
105  ("Id for this KernelCandidate",),
106  ]
107  metricMap = {}
108  for name, mtype, comment in zip(nameList, typeList, commentList):
109  metricMap[name] = {'type': mtype, 'comment': comment}
110 
111  return metricMap
112 

Member Data Documentation

◆ fields

lsst.ip.diffim.kernelCandidateQa.KernelCandidateQa.fields

Definition at line 51 of file kernelCandidateQa.py.


The documentation for this class was generated from the following file: