24 from __future__
import division
27 from optparse
import OptionParser
42 """Parse the command line options."""
43 parser = OptionParser(
44 usage=
"""%prog cdDiffSources crDiffExposure
46 Read in sources and test for junk""")
47 options, args = parser.parse_args()
49 parser.error(
"incorrect number of arguments")
54 storageList = dafPersist.StorageList()
57 storageList.append(persistence.getRetrieveStorage(
"BoostStorage", loc))
58 psvptr = persistence.unsafeRetrieve(
"PersistableSourceVector", storageList, additionalData)
59 psv = afwDet.PersistableSourceVector.swigConvert(psvptr)
60 return psv.getSources()
63 srcBadMaskPlanes = pexConfig.ListField(
65 doc =
"""Mask planes that lead to an invalid detection.
66 Options: EDGE SAT BAD CR INTRP
67 E.g. : EDGE SAT BAD allows CR-masked and interpolated pixels""",
68 default = (
"EDGE",
"SAT",
"BAD")
70 fBadPixels = pexConfig.Field(
72 doc =
"Fraction of bad pixels allowed in footprint",
75 fluxPolarityRatio = pexConfig.Field(
77 doc =
"Minimum fraction of flux in correct-polarity pixels",
80 nPolarityRatio = pexConfig.Field(
82 doc =
"Minimum fraction of correct-polarity pixels in unmasked subset",
85 nMaskedRatio = pexConfig.Field(
87 doc =
"Minimum fraction of correct-polarity unmasked to masked pixels",
90 nGoodRatio = pexConfig.Field(
92 doc =
"Minimum fraction of correct-polarity unmasked to all pixels",
102 srcBadMaskPlanes = self.config.srcBadMaskPlanes
103 for maskPlane
in srcBadMaskPlanes:
104 self.
bitMask |= afwImage.MaskU_getPlaneBitMask(maskPlane)
107 idxP = num.where(mask & afwImage.MaskU_getPlaneBitMask(
"DETECTED"))
108 idxN = num.where(mask & afwImage.MaskU_getPlaneBitMask(
"DETECTED_NEGATIVE"))
109 return len(idxP[0]), len(idxN[0])
112 idxM = num.where(mask & self.
bitMask)
116 unmasked = ((mask & self.
bitMask) == 0)
117 idxP = num.where( (pixels >= 0) & unmasked)
118 idxN = num.where( (pixels < 0) & unmasked)
119 fluxP = num.sum(pixels[idxP])
120 fluxN = num.sum(pixels[idxN])
122 return len(idxP[0]), len(idxN[0]), fluxP, fluxN
125 imArr, maArr, varArr = subMi.getArrays()
126 flux = source.getApFlux()
128 nPixels = subMi.getWidth() * subMi.getHeight()
132 assert (nPixels == (nMasked + nPos + nNeg))
135 fMasked = (nMasked / nPixels)
136 fMaskedTol = self.config.fBadPixels
137 if fMasked > fMaskedTol:
139 "Candidate %d : BAD fBadPixels %.2f > %.2f" % (source.getId(), fMasked, fMaskedTol))
144 fluxRatio = fPos / (fPos + abs(fNeg))
145 ngoodRatio = nPos / nPixels
146 maskRatio = nPos / (nPos + nMasked)
147 npolRatio = nPos / (nPos + nNeg)
150 fluxRatio = abs(fNeg)/ (fPos + abs(fNeg))
151 ngoodRatio = nNeg / nPixels
152 maskRatio = nNeg / (nNeg + nMasked)
153 npolRatio = nNeg / (nNeg + nPos)
156 fluxRatioTolerance = self.config.fluxPolarityRatio
157 if fluxRatio < fluxRatioTolerance:
159 "Candidate %d : BAD flux polarity %.2f < %.2f (pos=%.2f neg=%.2f)" % (source.getId(),
160 fluxRatio, fluxRatioTolerance, fPos, fNeg))
164 polarityTolerance = self.config.nPolarityRatio
165 if npolRatio < polarityTolerance:
167 "Candidate %d : BAD polarity count %.2f < %.2f (pos=%d neg=%d)" % (source.getId(),
168 npolRatio, polarityTolerance, nPos, nNeg))
172 maskedTolerance = self.config.nMaskedRatio
173 if maskRatio < maskedTolerance:
175 "Candidate %d : BAD unmasked count %.2f < %.2f (pos=%d neg=%d mask=%d)" % (source.getId(),
176 maskRatio, maskedTolerance, nPos, nNeg, nMasked))
180 ngoodTolerance = self.config.nGoodRatio
181 if ngoodRatio < ngoodTolerance:
183 "Candidate %d : BAD good pixel count %.2f < %.2f (pos=%d neg=%d tot=%d)" % (source.getId(),
184 ngoodRatio, ngoodTolerance, nPos, nNeg, nPixels))
188 "Candidate %d : OK flux=%.2f nPos=%d nNeg=%d nTot=%d nDetPos=%d nDetNeg=%d fPos=%.2f fNeg=%2f" % (source.getId(),
189 flux, nPos, nNeg, nPixels, nDetPos, nDetNeg, fPos, fNeg))
196 (crDiffSourceFile, crDiffExposureFile) = args
199 crDiffExposure = afwImage.ExposureF(crDiffExposureFile)
204 expX0 = crDiffExposure.getX0()
205 expY0 = crDiffExposure.getY0()
206 expX1 = expX0 + crDiffExposure.getWidth() - 1
207 expY1 = expY0 + crDiffExposure.getHeight() - 1
209 for i
in range(crDiffSources.size()):
210 crDiffSource = crDiffSources[i]
216 xAstrom = crDiffSource.getXAstrom()
217 yAstrom = crDiffSource.getYAstrom()
218 Ixx =
max(1.0, crDiffSource.getIxx())
219 Iyy =
max(1.0, crDiffSource.getIyy())
220 x0 =
max(expX0, int(xAstrom - scaling * Ixx))
221 x1 =
min(expX1, int(xAstrom + scaling * Ixx))
222 y0 =
max(expY0, int(yAstrom - scaling * Iyy))
223 y1 =
min(expY1, int(yAstrom + scaling * Iyy))
226 subExp = afwImage.ExposureF(crDiffExposure, bbox)
227 subMi = subExp.getMaskedImage()
228 imArr, maArr, varArr = subMi.getArrays()
230 if analyst.testSource(crDiffSource, subMi):
231 ds9.mtv(subExp, frame=1)
233 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.