Coverage for python/lsst/faro/utils/filtermatches.py: 5%
53 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-22 04:22 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-22 04:22 -0800
1# This file is part of faro.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
22import numpy as np
23from lsst.afw.table import GroupView
25__all__ = ("filterMatches",)
28def filterMatches(
29 matchedCatalog,
30 snrMin=None,
31 snrMax=None,
32 extended=None,
33 doFlags=None,
34 isPrimary=None,
35 psfStars=None,
36 photoCalibStars=None,
37 astromCalibStars=None,
38):
40 if snrMin is None:
41 snrMin = 50.0
42 if snrMax is None:
43 snrMax = np.Inf
44 if extended is None:
45 extended = False
46 if doFlags is None:
47 doFlags = True
48 nMatchesRequired = 2
49 if isPrimary is None:
50 isPrimary = True
51 if psfStars is None:
52 psfStars = False
53 if photoCalibStars is None:
54 photoCalibStars = False
55 if astromCalibStars is None:
56 astromCalibStars = False
58 matchedCat = GroupView.build(matchedCatalog)
59 magKey = matchedCat.schema.find("slot_PsfFlux_mag").key
61 def nMatchFilter(cat):
62 if len(cat) < nMatchesRequired:
63 return False
64 return np.isfinite(cat.get(magKey)).all()
66 def snrFilter(cat):
67 # Note that this also implicitly checks for psfSnr being non-nan.
68 snr = cat.get("base_PsfFlux_snr")
69 (ok0,) = np.where(np.isfinite(snr))
70 medianSnr = np.median(snr[ok0])
71 return snrMin <= medianSnr and medianSnr <= snrMax
73 def ptsrcFilter(cat):
74 ext = cat.get("base_ClassificationExtendedness_value")
75 # Keep only objects that are flagged as "not extended" in *ALL* visits,
76 # (base_ClassificationExtendedness_value = 1 for extended, 0 for point-like)
77 if extended:
78 return np.min(ext) > 0.9
79 else:
80 return np.max(ext) < 0.9
82 def flagFilter(cat):
83 if doFlags:
84 flag_sat = cat.get("base_PixelFlags_flag_saturated")
85 flag_cr = cat.get("base_PixelFlags_flag_cr")
86 flag_bad = cat.get("base_PixelFlags_flag_bad")
87 flag_edge = cat.get("base_PixelFlags_flag_edge")
88 return np.logical_not(np.any([flag_sat, flag_cr, flag_bad, flag_edge]))
89 else:
90 return True
92 def isPrimaryFilter(cat):
93 if isPrimary:
94 flag_isPrimary = cat.get("detect_isPrimary")
95 return np.all(flag_isPrimary)
96 else:
97 return True
99 def fullFilter(cat):
100 return (
101 nMatchFilter(cat)
102 and snrFilter(cat)
103 and ptsrcFilter(cat)
104 and flagFilter(cat)
105 and isPrimaryFilter(cat)
106 )
108 return matchedCat.where(fullFilter)