24 from __future__
import division
26 from optparse
import OptionParser
41 """Parse the command line options."""
42 parser = OptionParser(
43 usage=
"""%prog cdDiffSources crDiffExposure
45 Read in sources and test for junk""")
46 options, args = parser.parse_args()
48 parser.error(
"incorrect number of arguments")
53 storageList = dafPersist.StorageList()
56 storageList.append(persistence.getRetrieveStorage(
"BoostStorage", loc))
57 psvptr = persistence.unsafeRetrieve(
"PersistableSourceVector", storageList, additionalData)
58 psv = afwDet.PersistableSourceVector.swigConvert(psvptr)
59 return psv.getSources()
62 srcBadMaskPlanes = pexConfig.ListField(
64 doc =
"""Mask planes that lead to an invalid detection.
65 Options: NO_DATA EDGE SAT BAD CR INTRP
66 E.g. : NO_DATA SAT BAD allows CR-masked and interpolated pixels""",
67 default = (
"NO_DATA",
"EDGE",
"SAT",
"BAD")
69 fBadPixels = pexConfig.Field(
71 doc =
"Fraction of bad pixels allowed in footprint",
74 fluxPolarityRatio = pexConfig.Field(
76 doc =
"Minimum fraction of flux in correct-polarity pixels",
79 nPolarityRatio = pexConfig.Field(
81 doc =
"Minimum fraction of correct-polarity pixels in unmasked subset",
84 nMaskedRatio = pexConfig.Field(
86 doc =
"Minimum fraction of correct-polarity unmasked to masked pixels",
89 nGoodRatio = pexConfig.Field(
91 doc =
"Minimum fraction of correct-polarity unmasked to all pixels",
101 srcBadMaskPlanes = self.config.srcBadMaskPlanes
102 for maskPlane
in srcBadMaskPlanes:
103 self.
bitMask |= afwImage.MaskU_getPlaneBitMask(maskPlane)
106 idxP = num.where(mask & afwImage.MaskU_getPlaneBitMask(
"DETECTED"))
107 idxN = num.where(mask & afwImage.MaskU_getPlaneBitMask(
"DETECTED_NEGATIVE"))
108 return len(idxP[0]), len(idxN[0])
111 idxM = num.where(mask & self.
bitMask)
115 unmasked = ((mask & self.
bitMask) == 0)
116 idxP = num.where( (pixels >= 0) & unmasked)
117 idxN = num.where( (pixels < 0) & unmasked)
118 fluxP = num.sum(pixels[idxP])
119 fluxN = num.sum(pixels[idxN])
121 return len(idxP[0]), len(idxN[0]), fluxP, fluxN
124 imArr, maArr, varArr = subMi.getArrays()
125 flux = source.getApFlux()
127 nPixels = subMi.getWidth() * subMi.getHeight()
131 assert (nPixels == (nMasked + nPos + nNeg))
134 fMasked = (nMasked / nPixels)
135 fMaskedTol = self.config.fBadPixels
136 if fMasked > fMaskedTol:
138 "Candidate %d : BAD fBadPixels %.2f > %.2f" % (source.getId(), fMasked, fMaskedTol))
143 fluxRatio = fPos / (fPos + abs(fNeg))
144 ngoodRatio = nPos / nPixels
145 maskRatio = nPos / (nPos + nMasked)
146 npolRatio = nPos / (nPos + nNeg)
149 fluxRatio = abs(fNeg)/ (fPos + abs(fNeg))
150 ngoodRatio = nNeg / nPixels
151 maskRatio = nNeg / (nNeg + nMasked)
152 npolRatio = nNeg / (nNeg + nPos)
155 fluxRatioTolerance = self.config.fluxPolarityRatio
156 if fluxRatio < fluxRatioTolerance:
158 "Candidate %d : BAD flux polarity %.2f < %.2f (pos=%.2f neg=%.2f)" % (source.getId(),
159 fluxRatio, fluxRatioTolerance, fPos, fNeg))
163 polarityTolerance = self.config.nPolarityRatio
164 if npolRatio < polarityTolerance:
166 "Candidate %d : BAD polarity count %.2f < %.2f (pos=%d neg=%d)" % (source.getId(),
167 npolRatio, polarityTolerance, nPos, nNeg))
171 maskedTolerance = self.config.nMaskedRatio
172 if maskRatio < maskedTolerance:
174 "Candidate %d : BAD unmasked count %.2f < %.2f (pos=%d neg=%d mask=%d)" % (source.getId(),
175 maskRatio, maskedTolerance, nPos, nNeg, nMasked))
179 ngoodTolerance = self.config.nGoodRatio
180 if ngoodRatio < ngoodTolerance:
182 "Candidate %d : BAD good pixel count %.2f < %.2f (pos=%d neg=%d tot=%d)" % (source.getId(),
183 ngoodRatio, ngoodTolerance, nPos, nNeg, nPixels))
187 "Candidate %d : OK flux=%.2f nPos=%d nNeg=%d nTot=%d nDetPos=%d nDetNeg=%d fPos=%.2f fNeg=%2f" % (source.getId(),
188 flux, nPos, nNeg, nPixels, nDetPos, nDetNeg, fPos, fNeg))
195 (crDiffSourceFile, crDiffExposureFile) = args
198 crDiffExposure = afwImage.ExposureF(crDiffExposureFile)
203 expX0 = crDiffExposure.getX0()
204 expY0 = crDiffExposure.getY0()
205 expX1 = expX0 + crDiffExposure.getWidth() - 1
206 expY1 = expY0 + crDiffExposure.getHeight() - 1
208 for i
in range(crDiffSources.size()):
209 crDiffSource = crDiffSources[i]
215 xAstrom = crDiffSource.getXAstrom()
216 yAstrom = crDiffSource.getYAstrom()
217 Ixx = max(1.0, crDiffSource.getIxx())
218 Iyy = max(1.0, crDiffSource.getIyy())
219 x0 = max(expX0, int(xAstrom - scaling * Ixx))
220 x1 = min(expX1, int(xAstrom + scaling * Ixx))
221 y0 = max(expY0, int(yAstrom - scaling * Iyy))
222 y1 = min(expY1, int(yAstrom + scaling * Iyy))
225 subExp = afwImage.ExposureF(crDiffExposure, bbox)
226 subMi = subExp.getMaskedImage()
227 imArr, maArr, varArr = subMi.getArrays()
229 if analyst.testSource(crDiffSource, subMi):
230 ds9.mtv(subExp, frame=1)
232 if __name__ ==
"__main__":
Class for logical location of a persisted Persistable instance.
a container for holding hierarchical configuration data in memory.
limited backward compatibility to the DC2 run-time trace facilities
An integer coordinate rectangle.
Class for storing generic metadata.