Hide keyboard shortcuts

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

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"""Select sources that are useful for astrometry. 

24 

25Such sources have good signal-to-noise, are well centroided, not blended, 

26and not flagged with a handful of "bad" flags. 

27""" 

28 

29import numpy as np 

30 

31import lsst.pex.config as pexConfig 

32from .sourceSelector import BaseSourceSelectorConfig, BaseSourceSelectorTask, sourceSelectorRegistry 

33from lsst.pipe.base import Struct 

34from functools import reduce 

35 

36 

37class AstrometrySourceSelectorConfig(BaseSourceSelectorConfig): 

38 badFlags = pexConfig.ListField( 

39 doc="List of flags which cause a source to be rejected as bad", 

40 dtype=str, 

41 default=[ 

42 "base_PixelFlags_flag_edge", 

43 "base_PixelFlags_flag_interpolatedCenter", 

44 "base_PixelFlags_flag_saturatedCenter", 

45 "base_PixelFlags_flag_crCenter", 

46 "base_PixelFlags_flag_bad", 

47 ], 

48 ) 

49 sourceFluxType = pexConfig.Field( 

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

51 dtype=str, 

52 default="Ap", 

53 ) 

54 minSnr = pexConfig.Field( 

55 dtype=float, 

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

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

58 default=10, 

59 ) 

60 

61 

62@pexConfig.registerConfigurable("astrometry", sourceSelectorRegistry) 

63class AstrometrySourceSelectorTask(BaseSourceSelectorTask): 

64 """Select sources that are useful for astrometry. 

65 

66 Good astrometry sources have high signal/noise, are non-blended, and 

67 did not have certain "bad" flags set during source extraction. They need not 

68 be PSF sources, just have reliable centroids. 

69 """ 

70 ConfigClass = AstrometrySourceSelectorConfig 

71 

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

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

74 

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

76 """Return a selection of sources that are useful for astrometry. 

77 

78 Parameters: 

79 ----------- 

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

81 Catalog of sources to select from. 

82 This catalog must be contiguous in memory. 

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

84 Ignored in this SourceSelector. 

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

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

87 

88 Return 

89 ------ 

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

91 The struct contains the following data: 

92 

93 - selected : `array` of `bool`` 

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

95 sourceCat. 

96 """ 

97 self._getSchemaKeys(sourceCat.schema) 

98 

99 bad = reduce(lambda x, y: np.logical_or(x, sourceCat.get(y)), self.config.badFlags, False) 

100 good = self._isGood(sourceCat) 

101 return Struct(selected=good & ~bad) 

102 

103 def _getSchemaKeys(self, schema): 

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

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

106 self.nChildKey = schema["deblend_nChild"].asKey() 

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

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

109 self.centroidXErrKey = schema["slot_Centroid_xErr"].asKey() 

110 self.centroidYErrKey = schema["slot_Centroid_yErr"].asKey() 

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

112 

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

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

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

116 

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

118 self.instFluxKey = schema[fluxPrefix + "instFlux"].asKey() 

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

120 self.instFluxErrKey = schema[fluxPrefix + "instFluxErr"].asKey() 

121 

122 def _isMultiple(self, sourceCat): 

123 """Return True for each source that is likely multiple sources.""" 

124 test = (sourceCat.get(self.parentKey) != 0) | (sourceCat.get(self.nChildKey) != 0) 

125 # have to currently manage footprints on a source-by-source basis. 

126 for i, cat in enumerate(sourceCat): 

127 footprint = cat.getFootprint() 

128 test[i] |= (footprint is not None) and (len(footprint.getPeaks()) > 1) 

129 return test 

130 

131 def _hasCentroid(self, sourceCat): 

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

133 def checkNonfiniteCentroid(): 

134 """Return True for sources with non-finite centroids.""" 

135 return ~np.isfinite(sourceCat.get(self.centroidXKey)) | \ 

136 ~np.isfinite(sourceCat.get(self.centroidYKey)) 

137 assert ~checkNonfiniteCentroid().any(), \ 

138 "Centroids not finite for %d unflagged sources." % (checkNonfiniteCentroid().sum()) 

139 return np.isfinite(sourceCat.get(self.centroidXErrKey)) \ 

140 & np.isfinite(sourceCat.get(self.centroidYErrKey)) \ 

141 & ~sourceCat.get(self.centroidFlagKey) 

142 

143 def _goodSN(self, sourceCat): 

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

145 if self.config.minSnr <= 0: 

146 return True 

147 else: 

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

149 return sourceCat.get(self.instFluxKey)/sourceCat.get(self.instFluxErrKey) > self.config.minSnr 

150 

151 def _isUsable(self, sourceCat): 

152 """ 

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

154 have a poor centroid. 

155 

156 For a source to be usable it must: 

157 - have a valid centroid 

158 - not be deblended 

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

160 - have adequate signal-to-noise 

161 """ 

162 

163 return self._hasCentroid(sourceCat) \ 

164 & ~self._isMultiple(sourceCat) \ 

165 & self._goodSN(sourceCat) \ 

166 & ~sourceCat.get(self.fluxFlagKey) 

167 

168 def _isGood(self, sourceCat): 

169 """ 

170 Return True for each source that is usable for matching and likely has a 

171 good centroid. 

172 

173 The additional tests for a good centroid, beyond isUsable, are: 

174 - not interpolated in the center 

175 - not saturated 

176 - not near the edge 

177 """ 

178 

179 return self._isUsable(sourceCat) \ 

180 & ~sourceCat.get(self.saturatedKey) \ 

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

182 & ~sourceCat.get(self.edgeKey) 

183 

184 def _isBadFlagged(self, source): 

185 """Return True if any of config.badFlags are set for this source.""" 

186 return any(source.get(flag) for flag in self.config.badFlags)