Coverage for python/lsst/meas/algorithms/matcherSourceSelector.py: 40%

47 statements  

« prev     ^ index     » next       coverage.py v7.2.4, created at 2023-04-29 03:34 -0700

1# 

2# LSST Data Management System 

3# 

4# Copyright 2008-2017 AURA/LSST. 

5# 

6# This product includes software developed by the 

7# LSST Project (http://www.lsst.org/). 

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 LSST License Statement and 

20# the GNU General Public License along with this program. If not, 

21# see <https://www.lsstcorp.org/LegalNotices/>. 

22# 

23 

24__all__ = ["MatcherSourceSelectorConfig", "MatcherSourceSelectorTask"] 

25 

26import numpy as np 

27 

28import lsst.pex.config as pexConfig 

29from .sourceSelector import BaseSourceSelectorConfig, BaseSourceSelectorTask, sourceSelectorRegistry 

30from lsst.pipe.base import Struct 

31 

32 

33class MatcherSourceSelectorConfig(BaseSourceSelectorConfig): 

34 sourceFluxType = pexConfig.Field( 

35 doc="Type of source flux; typically one of Ap or Psf", 

36 dtype=str, 

37 default="Ap", 

38 ) 

39 minSnr = pexConfig.Field( 

40 dtype=float, 

41 doc="Minimum allowed signal-to-noise ratio for sources used for matching " 

42 "(in the flux specified by sourceFluxType); <= 0 for no limit", 

43 default=40, 

44 ) 

45 excludePixelFlags = pexConfig.Field( 

46 dtype=bool, 

47 doc="Exclude objects that have saturated, interpolated, or edge " 

48 "pixels using PixelFlags. For matchOptimisticB set this to False " 

49 "to recover previous matcher selector behavior.", 

50 default=True, 

51 ) 

52 

53 

54@pexConfig.registerConfigurable("matcher", sourceSelectorRegistry) 

55class MatcherSourceSelectorTask(BaseSourceSelectorTask): 

56 """Select sources that are useful for matching. 

57 

58 Good matching sources have high signal/noise, are non-blended. They need not 

59 be PSF sources, just have reliable centroids. 

60 

61 Distinguished from astrometrySourceSelector because it is more lenient 

62 (i.e. not checking footprints or bad flags). 

63 """ 

64 ConfigClass = MatcherSourceSelectorConfig 

65 

66 def __init__(self, *args, **kwargs): 

67 BaseSourceSelectorTask.__init__(self, *args, **kwargs) 

68 

69 def selectSources(self, sourceCat, matches=None, exposure=None): 

70 """Return a selection of sources that are useful for matching. 

71 

72 Parameters 

73 ---------- 

74 sourceCat : `lsst.afw.table.SourceCatalog` 

75 Catalog of sources to select from. 

76 This catalog must be contiguous in memory. 

77 matches : `list` of `lsst.afw.table.ReferenceMatch` or None 

78 Ignored in this SourceSelector. 

79 exposure : `lsst.afw.image.Exposure` or None 

80 The exposure the catalog was built from; used for debug display. 

81 

82 Returns 

83 ------- 

84 struct : `lsst.pipe.base.Struct` 

85 The struct contains the following data: 

86 

87 ``selected`` 

88 Boolean array of sources that were selected, same length as 

89 sourceCat. (`numpy.ndarray` of `bool`) 

90 """ 

91 self._getSchemaKeys(sourceCat.schema) 

92 

93 good = self._isUsable(sourceCat) 

94 if self.config.excludePixelFlags: 

95 good = good & self._isGood(sourceCat) 

96 return Struct(selected=good) 

97 

98 def _getSchemaKeys(self, schema): 

99 """Extract and save the necessary keys from schema with asKey. 

100 """ 

101 self.parentKey = schema["parent"].asKey() 

102 self.centroidXKey = schema["slot_Centroid_x"].asKey() 

103 self.centroidYKey = schema["slot_Centroid_y"].asKey() 

104 self.centroidFlagKey = schema["slot_Centroid_flag"].asKey() 

105 

106 fluxPrefix = "slot_%sFlux_" % (self.config.sourceFluxType,) 

107 self.fluxField = fluxPrefix + "instFlux" 

108 self.fluxKey = schema[fluxPrefix + "instFlux"].asKey() 

109 self.fluxFlagKey = schema[fluxPrefix + "flag"].asKey() 

110 self.fluxErrKey = schema[fluxPrefix + "instFluxErr"].asKey() 

111 

112 self.edgeKey = schema["base_PixelFlags_flag_edge"].asKey() 

113 self.interpolatedCenterKey = schema["base_PixelFlags_flag_interpolatedCenter"].asKey() 

114 self.saturatedKey = schema["base_PixelFlags_flag_saturated"].asKey() 

115 

116 def _isParent(self, sourceCat): 

117 """Return True for each source that is the parent source. 

118 """ 

119 test = (sourceCat.get(self.parentKey) == 0) 

120 return test 

121 

122 def _hasCentroid(self, sourceCat): 

123 """Return True for each source that has a valid centroid 

124 """ 

125 return np.isfinite(sourceCat.get(self.centroidXKey)) \ 

126 & np.isfinite(sourceCat.get(self.centroidYKey)) \ 

127 & ~sourceCat.get(self.centroidFlagKey) 

128 

129 def _goodSN(self, sourceCat): 

130 """Return True for each source that has Signal/Noise > config.minSnr. 

131 """ 

132 if self.config.minSnr <= 0: 

133 return True 

134 else: 

135 with np.errstate(invalid="ignore"): # suppress NAN warnings 

136 return sourceCat.get(self.fluxKey)/sourceCat.get(self.fluxErrKey) > self.config.minSnr 

137 

138 def _isUsable(self, sourceCat): 

139 """ 

140 Return True for each source that is usable for matching, even if it may 

141 have a poor centroid. 

142 

143 For a source to be usable it must: 

144 

145 - have a valid centroid 

146 - not be deblended 

147 - have a valid instFlux (of the type specified in this object's constructor) 

148 - have adequate signal-to-noise 

149 """ 

150 return self._hasCentroid(sourceCat) \ 

151 & self._isParent(sourceCat) \ 

152 & self._goodSN(sourceCat) \ 

153 & ~sourceCat.get(self.fluxFlagKey) 

154 

155 def _isGood(self, sourceCat): 

156 """ 

157 Return True for each source that is usable for matching, even if it may 

158 have a poor centroid. 

159 

160 For a source to be usable it must: 

161 

162 - Not be on a CCD edge. 

163 - Not have an interpolated pixel within 3x3 around their centroid. 

164 - Not have a saturated pixel in their footprint. 

165 """ 

166 return ~sourceCat.get(self.edgeKey) & \ 

167 ~sourceCat.get(self.interpolatedCenterKey) & \ 

168 ~sourceCat.get(self.saturatedKey)