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

#!/usr/bin/env python 

 

# 

# 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/>. 

# 

 

__all__ = ["DiaSourceAnalystConfig", "DiaSourceAnalyst"] 

 

import lsst.afw.image as afwImage 

from lsst.log import Log 

import numpy as num 

import lsst.pex.config as pexConfig 

 

scaling = 5 

 

 

class DiaSourceAnalystConfig(pexConfig.Config): 

srcBadMaskPlanes = pexConfig.ListField( 

dtype=str, 

doc="""Mask planes that lead to an invalid detection. 

Options: NO_DATA EDGE SAT BAD CR INTRP 

E.g. : NO_DATA SAT BAD allows CR-masked and interpolated pixels""", 

default=("NO_DATA", "EDGE", "SAT", "BAD") 

) 

fBadPixels = pexConfig.Field( 

dtype=float, 

doc="Fraction of bad pixels allowed in footprint", 

default=0.1 

) 

fluxPolarityRatio = pexConfig.Field( 

dtype=float, 

doc="Minimum fraction of flux in correct-polarity pixels", 

default=0.75 

) 

nPolarityRatio = pexConfig.Field( 

dtype=float, 

doc="Minimum fraction of correct-polarity pixels in unmasked subset", 

default=0.7 

) 

nMaskedRatio = pexConfig.Field( 

dtype=float, 

doc="Minimum fraction of correct-polarity unmasked to masked pixels", 

default=0.6, 

) 

nGoodRatio = pexConfig.Field( 

dtype=float, 

doc="Minimum fraction of correct-polarity unmasked to all pixels", 

default=0.5 

) 

 

 

class DiaSourceAnalyst(object): 

 

def __init__(self, config): 

self.config = config 

self.log = Log.getLogger("ip.diffim.DiaSourceAnalysis") 

 

self.bitMask = 0 

srcBadMaskPlanes = self.config.srcBadMaskPlanes 

for maskPlane in srcBadMaskPlanes: 

self.bitMask |= afwImage.Mask.getPlaneBitMask(maskPlane) 

 

def countDetected(self, mask): 

idxP = num.where(mask & afwImage.Mask.getPlaneBitMask("DETECTED")) 

idxN = num.where(mask & afwImage.Mask.getPlaneBitMask("DETECTED_NEGATIVE")) 

return len(idxP[0]), len(idxN[0]) 

 

def countMasked(self, mask): 

idxM = num.where(mask & self.bitMask) 

return len(idxM[0]) 

 

def countPolarity(self, mask, pixels): 

unmasked = ((mask & self.bitMask) == 0) 

idxP = num.where((pixels >= 0) & unmasked) 

idxN = num.where((pixels < 0) & unmasked) 

fluxP = num.sum(pixels[idxP]) 

fluxN = num.sum(pixels[idxN]) 

return len(idxP[0]), len(idxN[0]), fluxP, fluxN 

 

def testSource(self, source, subMi): 

imArr, maArr, varArr = subMi.getArrays() 

flux = source.getApFlux() 

 

nPixels = subMi.getWidth() * subMi.getHeight() 

nPos, nNeg, fPos, fNeg = self.countPolarity(maArr, imArr) 

nDetPos, nDetNeg = self.countDetected(maArr) 

nMasked = self.countMasked(maArr) 

assert(nPixels == (nMasked + nPos + nNeg)) 

 

# 1) Too many pixels in the detection are masked 

fMasked = (nMasked / nPixels) 

fMaskedTol = self.config.fBadPixels 

if fMasked > fMaskedTol: 

self.log.debug("Candidate %d : BAD fBadPixels %.2f > %.2f", source.getId(), fMasked, fMaskedTol) 

return False 

 

if flux > 0: 

# positive-going source 

fluxRatio = fPos / (fPos + abs(fNeg)) 

ngoodRatio = nPos / nPixels 

maskRatio = nPos / (nPos + nMasked) 

npolRatio = nPos / (nPos + nNeg) 

else: 

# negative-going source 

fluxRatio = abs(fNeg) / (fPos + abs(fNeg)) 

ngoodRatio = nNeg / nPixels 

maskRatio = nNeg / (nNeg + nMasked) 

npolRatio = nNeg / (nNeg + nPos) 

 

# 2) Not enough flux in unmasked correct-polarity pixels 

fluxRatioTolerance = self.config.fluxPolarityRatio 

if fluxRatio < fluxRatioTolerance: 

self.log.debug("Candidate %d : BAD flux polarity %.2f < %.2f (pos=%.2f neg=%.2f)", 

source.getId(), fluxRatio, fluxRatioTolerance, fPos, fNeg) 

return False 

 

# 3) Not enough unmasked pixels of correct polarity 

polarityTolerance = self.config.nPolarityRatio 

if npolRatio < polarityTolerance: 

self.log.debug("Candidate %d : BAD polarity count %.2f < %.2f (pos=%d neg=%d)", 

source.getId(), npolRatio, polarityTolerance, nPos, nNeg) 

return False 

 

# 4) Too many masked vs. correct polarity pixels 

maskedTolerance = self.config.nMaskedRatio 

if maskRatio < maskedTolerance: 

self.log.debug("Candidate %d : BAD unmasked count %.2f < %.2f (pos=%d neg=%d mask=%d)", 

source.getId(), maskRatio, maskedTolerance, nPos, nNeg, nMasked) 

return False 

 

# 5) Too few unmasked, correct polarity pixels 

ngoodTolerance = self.config.nGoodRatio 

if ngoodRatio < ngoodTolerance: 

self.log.debug("Candidate %d : BAD good pixel count %.2f < %.2f (pos=%d neg=%d tot=%d)", 

source.getId(), ngoodRatio, ngoodTolerance, nPos, nNeg, nPixels) 

return False 

 

self.log.debug("Candidate %d : OK flux=%.2f nPos=%d nNeg=%d nTot=%d nDetPos=%d nDetNeg=%d " 

"fPos=%.2f fNeg=%2f", 

source.getId(), flux, nPos, nNeg, nPixels, nDetPos, nDetNeg, fPos, fNeg) 

return True