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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

# 

# LSST Data Management System 

# Copyright 2008-2016 LSST Corporation. 

# 

# This product includes software developed by the 

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

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

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

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import numpy as np 

 

from lsst.pipe.base import Struct 

import lsst.pex.config as pexConfig 

import lsst.afw.display.ds9 as ds9 

import lsst.meas.algorithms as measAlg 

 

__all__ = ["DiaCatalogSourceSelectorConfig", "DiaCatalogSourceSelectorTask"] 

 

 

class DiaCatalogSourceSelectorConfig(measAlg.BaseStarSelectorConfig): 

# Selection cuts on the input source catalog 

fluxLim = pexConfig.Field( 

doc="specify the minimum psfFlux for good Kernel Candidates", 

dtype=float, 

default=0.0, 

check=lambda x: x >= 0.0, 

) 

fluxMax = pexConfig.Field( 

doc="specify the maximum psfFlux for good Kernel Candidates (ignored if == 0)", 

dtype=float, 

default=0.0, 

check=lambda x: x >= 0.0, 

) 

# Selection cuts on the reference catalog 

selectStar = pexConfig.Field( 

doc="Select objects that are flagged as stars", 

dtype=bool, 

default=True 

) 

selectGalaxy = pexConfig.Field( 

doc="Select objects that are flagged as galaxies", 

dtype=bool, 

default=False 

) 

includeVariable = pexConfig.Field( 

doc="Include objects that are known to be variable", 

dtype=bool, 

default=False 

) 

grMin = pexConfig.Field( 

doc="Minimum g-r color for selection (inclusive)", 

dtype=float, 

default=0.0 

) 

grMax = pexConfig.Field( 

doc="Maximum g-r color for selection (inclusive)", 

dtype=float, 

default=3.0 

) 

 

def setDefaults(self): 

measAlg.BaseStarSelectorConfig.setDefaults(self) 

self.badFlags = [ 

"base_PixelFlags_flag_edge", 

"base_PixelFlags_flag_interpolatedCenter", 

"base_PixelFlags_flag_saturatedCenter", 

"slot_Centroid_flag", 

] 

 

 

class CheckSource(object): 

"""A functor to check whether a source has any flags set that should cause it to be labeled bad.""" 

 

def __init__(self, table, fluxLim, fluxMax, badFlags): 

self.keys = [table.getSchema().find(name).key for name in badFlags] 

self.fluxLim = fluxLim 

self.fluxMax = fluxMax 

 

def __call__(self, source): 

for k in self.keys: 

if source.get(k): 

return False 

96 ↛ 97line 96 didn't jump to line 97, because the condition on line 96 was never true if self.fluxLim is not None and source.getPsfInstFlux() < self.fluxLim: # ignore faint objects 

return False 

98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never true if self.fluxMax != 0.0 and source.getPsfInstFlux() > self.fluxMax: # ignore bright objects 

return False 

return True 

 

## @addtogroup LSST_task_documentation 

## @{ 

## @page DiaCatalogSourceSelectorTask 

## @ref DiaCatalogSourceSelectorTask_ "DiaCatalogSourceSelectorTask" 

## @copybrief DiaCatalogSourceSelectorTask 

## @} 

 

 

@pexConfig.registerConfigurable("diaCatalog", measAlg.sourceSelectorRegistry) 

class DiaCatalogSourceSelectorTask(measAlg.BaseSourceSelectorTask): 

"""!Select sources for Kernel candidates 

 

@anchor DiaCatalogSourceSelectorTask_ 

 

@section ip_diffim_diaCatalogSourceSelector_Contents Contents 

 

- @ref ip_diffim_diaCatalogSourceSelector_Purpose 

- @ref ip_diffim_diaCatalogSourceSelector_Initialize 

- @ref ip_diffim_diaCatalogSourceSelector_IO 

- @ref ip_diffim_diaCatalogSourceSelector_Config 

- @ref ip_diffim_diaCatalogSourceSelector_Debug 

 

@section ip_diffim_diaCatalogSourceSelector_Purpose Description 

 

A naive star selector based on second moments. Use with caution. 

 

@section ip_diffim_diaCatalogSourceSelector_Initialize Task initialisation 

 

@copydoc \_\_init\_\_ 

 

@section ip_diffim_diaCatalogSourceSelector_IO Invoking the Task 

 

Like all star selectors, the main method is `run`. 

 

@section ip_diffim_diaCatalogSourceSelector_Config Configuration parameters 

 

See @ref DiaCatalogSourceSelectorConfig 

 

@section ip_diffim_diaCatalogSourceSelector_Debug Debug variables 

 

DiaCatalogSourceSelectorTask has a debug dictionary with the following keys: 

<dl> 

<dt>display 

<dd>bool; if True display debug information 

<dt>displayExposure 

<dd>bool; if True display exposure 

<dt>pauseAtEnd 

<dd>bool; if True wait after displaying everything and wait for user input 

</dl> 

 

For example, put something like: 

@code{.py} 

import lsstDebug 

def DebugInfo(name): 

di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively 

if name.endswith("catalogStarSelector"): 

di.display = True 

 

return di 

 

lsstDebug.Info = DebugInfo 

@endcode 

into your `debug.py` file and run your task with the `--debug` flag. 

""" 

ConfigClass = DiaCatalogSourceSelectorConfig 

usesMatches = True # selectStars uses (requires) its matches argument 

 

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

"""Return a selection of sources for Kernel candidates. 

 

Parameters: 

----------- 

sourceCat : `lsst.afw.table.SourceCatalog` 

Catalog of sources to select from. 

This catalog must be contiguous in memory. 

matches : `list` of `lsst.afw.table.ReferenceMatch` 

A match vector as produced by meas_astrom. 

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

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

 

Return 

------ 

struct : `lsst.pipe.base.Struct` 

The struct contains the following data: 

 

- selected : `array` of `bool`` 

Boolean array of sources that were selected, same length as 

sourceCat. 

""" 

import lsstDebug 

display = lsstDebug.Info(__name__).display 

displayExposure = lsstDebug.Info(__name__).displayExposure 

pauseAtEnd = lsstDebug.Info(__name__).pauseAtEnd 

 

196 ↛ 197line 196 didn't jump to line 197, because the condition on line 196 was never true if matches is None: 

raise RuntimeError("DiaCatalogSourceSelector requires matches") 

 

mi = exposure.getMaskedImage() 

 

201 ↛ 202line 201 didn't jump to line 202, because the condition on line 201 was never true if display: 

if displayExposure: 

ds9.mtv(mi, title="Kernel candidates", frame=lsstDebug.frame) 

# 

# Look for flags in each Source 

# 

isGoodSource = CheckSource(sourceCat, self.config.fluxLim, self.config.fluxMax, self.config.badFlags) 

 

# Go through and find all the acceptable candidates in the catalogue 

selected = np.zeros(len(sourceCat), dtype=bool) 

 

212 ↛ 213line 212 didn't jump to line 213, because the condition on line 212 was never true if display and displayExposure: 

symbs = [] 

ctypes = [] 

 

doColorCut = True 

 

refSchema = matches[0][0].schema 

rRefFluxField = measAlg.getRefFluxField(refSchema, "r") 

gRefFluxField = measAlg.getRefFluxField(refSchema, "g") 

for i, (ref, source, d) in enumerate(matches): 

if not isGoodSource(source): 

223 ↛ 224line 223 didn't jump to line 224, because the condition on line 223 was never true if display and displayExposure: 

symbs.append("+") 

ctypes.append(ds9.RED) 

else: 

isStar = not ref.get("resolved") 

isVar = not ref.get("photometric") 

gMag = None 

rMag = None 

231 ↛ 242line 231 didn't jump to line 242, because the condition on line 231 was never false if doColorCut: 

try: 

gMag = -2.5 * np.log10(ref.get(gRefFluxField)) 

rMag = -2.5 * np.log10(ref.get(rRefFluxField)) 

except KeyError: 

self.log.warn("Cannot cut on color info; fields 'g' and 'r' do not exist") 

doColorCut = False 

isRightColor = True 

else: 

isRightColor = (gMag-rMag) >= self.config.grMin and (gMag-rMag) <= self.config.grMax 

 

isRightType = (self.config.selectStar and isStar) or (self.config.selectGalaxy and not isStar) 

isRightVar = (self.config.includeVariable) or (self.config.includeVariable is isVar) 

if isRightType and isRightVar and isRightColor: 

selected[i] = True 

246 ↛ 247line 246 didn't jump to line 247, because the condition on line 246 was never true if display and displayExposure: 

symbs.append("+") 

ctypes.append(ds9.GREEN) 

249 ↛ 250line 249 didn't jump to line 250, because the condition on line 249 was never true elif display and displayExposure: 

symbs.append("o") 

ctypes.append(ds9.BLUE) 

 

253 ↛ 254line 253 didn't jump to line 254, because the condition on line 253 was never true if display and displayExposure: 

with ds9.Buffering(): 

for (ref, source, d), symb, ctype in zip(matches, symbs, ctypes): 

if display and displayExposure: 

ds9.dot(symb, source.getX() - mi.getX0(), source.getY() - mi.getY0(), 

size=4, ctype=ctype, frame=lsstDebug.frame) 

 

260 ↛ 261line 260 didn't jump to line 261, because the condition on line 260 was never true if display: 

lsstDebug.frame += 1 

if pauseAtEnd: 

input("Continue? y[es] p[db] ") 

 

return Struct(selected=selected)