Coverage for python/lsst/faro/utils/filtermatches.py : 5%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import numpy as np
2from lsst.afw.table import GroupView
4__all__ = ("filterMatches", )
7def filterMatches(matchedCatalog, snrMin=None, snrMax=None,
8 extended=None, doFlags=None, isPrimary=None,
9 psfStars=None, photoCalibStars=None,
10 astromCalibStars=None):
12 if snrMin is None:
13 snrMin = 50.0
14 if snrMax is None:
15 snrMax = np.Inf
16 if extended is None:
17 extended = False
18 if doFlags is None:
19 doFlags = True
20 nMatchesRequired = 2
21 if isPrimary is None:
22 isPrimary = True
23 if psfStars is None:
24 psfStars = False
25 if photoCalibStars is None:
26 photoCalibStars = False
27 if astromCalibStars is None:
28 astromCalibStars = False
30 matchedCat = GroupView.build(matchedCatalog)
31 magKey = matchedCat.schema.find('slot_PsfFlux_mag').key
33 def nMatchFilter(cat):
34 if len(cat) < nMatchesRequired:
35 return False
36 return np.isfinite(cat.get(magKey)).all()
38 def snrFilter(cat):
39 # Note that this also implicitly checks for psfSnr being non-nan.
40 snr = cat.get('base_PsfFlux_snr')
41 ok0, = np.where(np.isfinite(snr))
42 medianSnr = np.median(snr[ok0])
43 return snrMin <= medianSnr and medianSnr <= snrMax
45 def ptsrcFilter(cat):
46 ext = cat.get('base_ClassificationExtendedness_value')
47 # Keep only objects that are flagged as "not extended" in *ALL* visits,
48 # (base_ClassificationExtendedness_value = 1 for extended, 0 for point-like)
49 if extended:
50 return np.min(ext) > 0.9
51 else:
52 return np.max(ext) < 0.9
54 def flagFilter(cat):
55 if doFlags:
56 flag_sat = cat.get("base_PixelFlags_flag_saturated")
57 flag_cr = cat.get("base_PixelFlags_flag_cr")
58 flag_bad = cat.get("base_PixelFlags_flag_bad")
59 flag_edge = cat.get("base_PixelFlags_flag_edge")
60 return np.logical_not(np.any([flag_sat, flag_cr, flag_bad, flag_edge]))
61 else:
62 return True
64 def isPrimaryFilter(cat):
65 if isPrimary:
66 flag_isPrimary = cat.get("detect_isPrimary")
67 return np.all(flag_isPrimary)
68 else:
69 return True
71 def fullFilter(cat):
72 return nMatchFilter(cat) and snrFilter(cat) and ptsrcFilter(cat)\
73 and flagFilter(cat) and isPrimaryFilter(cat)
75 return matchedCat.where(fullFilter)