24 from __future__
import division
25 from builtins
import input
26 from builtins
import range
27 from builtins
import object
29 from optparse
import OptionParser
45 """Parse the command line options."""
46 parser = OptionParser(
47 usage=
"""%prog cdDiffSources crDiffExposure
49 Read in sources and test for junk""")
50 options, args = parser.parse_args()
52 parser.error(
"incorrect number of arguments")
58 storageList = dafPersist.StorageList()
61 storageList.append(persistence.getRetrieveStorage(
"BoostStorage", loc))
62 psvptr = persistence.unsafeRetrieve(
"PersistableSourceVector", storageList, additionalData)
63 psv = afwDet.PersistableSourceVector.swigConvert(psvptr)
64 return psv.getSources()
68 srcBadMaskPlanes = pexConfig.ListField(
70 doc=
"""Mask planes that lead to an invalid detection.
71 Options: NO_DATA EDGE SAT BAD CR INTRP
72 E.g. : NO_DATA SAT BAD allows CR-masked and interpolated pixels""",
73 default=(
"NO_DATA",
"EDGE",
"SAT",
"BAD")
75 fBadPixels = pexConfig.Field(
77 doc=
"Fraction of bad pixels allowed in footprint",
80 fluxPolarityRatio = pexConfig.Field(
82 doc=
"Minimum fraction of flux in correct-polarity pixels",
85 nPolarityRatio = pexConfig.Field(
87 doc=
"Minimum fraction of correct-polarity pixels in unmasked subset",
90 nMaskedRatio = pexConfig.Field(
92 doc=
"Minimum fraction of correct-polarity unmasked to masked pixels",
95 nGoodRatio = pexConfig.Field(
97 doc=
"Minimum fraction of correct-polarity unmasked to all pixels",
106 self.
log = Log.getLogger(
"ip.diffim.DiaSourceAnalysis")
109 srcBadMaskPlanes = self.config.srcBadMaskPlanes
110 for maskPlane
in srcBadMaskPlanes:
111 self.
bitMask |= afwImage.MaskU_getPlaneBitMask(maskPlane)
114 idxP = num.where(mask & afwImage.MaskU_getPlaneBitMask(
"DETECTED"))
115 idxN = num.where(mask & afwImage.MaskU_getPlaneBitMask(
"DETECTED_NEGATIVE"))
116 return len(idxP[0]), len(idxN[0])
119 idxM = num.where(mask & self.
bitMask)
123 unmasked = ((mask & self.
bitMask) == 0)
124 idxP = num.where((pixels >= 0) & unmasked)
125 idxN = num.where((pixels < 0) & unmasked)
126 fluxP = num.sum(pixels[idxP])
127 fluxN = num.sum(pixels[idxN])
129 return len(idxP[0]), len(idxN[0]), fluxP, fluxN
132 imArr, maArr, varArr = subMi.getArrays()
133 flux = source.getApFlux()
135 nPixels = subMi.getWidth() * subMi.getHeight()
139 assert(nPixels == (nMasked + nPos + nNeg))
142 fMasked = (nMasked / nPixels)
143 fMaskedTol = self.config.fBadPixels
144 if fMasked > fMaskedTol:
145 self.log.debug(
"Candidate %d : BAD fBadPixels %.2f > %.2f", source.getId(), fMasked, fMaskedTol)
150 fluxRatio = fPos / (fPos + abs(fNeg))
151 ngoodRatio = nPos / nPixels
152 maskRatio = nPos / (nPos + nMasked)
153 npolRatio = nPos / (nPos + nNeg)
156 fluxRatio = abs(fNeg) / (fPos + abs(fNeg))
157 ngoodRatio = nNeg / nPixels
158 maskRatio = nNeg / (nNeg + nMasked)
159 npolRatio = nNeg / (nNeg + nPos)
162 fluxRatioTolerance = self.config.fluxPolarityRatio
163 if fluxRatio < fluxRatioTolerance:
164 self.log.debug(
"Candidate %d : BAD flux polarity %.2f < %.2f (pos=%.2f neg=%.2f)",
165 source.getId(), fluxRatio, fluxRatioTolerance, fPos, fNeg)
169 polarityTolerance = self.config.nPolarityRatio
170 if npolRatio < polarityTolerance:
171 self.log.debug(
"Candidate %d : BAD polarity count %.2f < %.2f (pos=%d neg=%d)",
172 source.getId(), npolRatio, polarityTolerance, nPos, nNeg)
176 maskedTolerance = self.config.nMaskedRatio
177 if maskRatio < maskedTolerance:
178 self.log.debug(
"Candidate %d : BAD unmasked count %.2f < %.2f (pos=%d neg=%d mask=%d)",
179 source.getId(), maskRatio, maskedTolerance, nPos, nNeg, nMasked)
183 ngoodTolerance = self.config.nGoodRatio
184 if ngoodRatio < ngoodTolerance:
185 self.log.debug(
"Candidate %d : BAD good pixel count %.2f < %.2f (pos=%d neg=%d tot=%d)",
186 source.getId(), ngoodRatio, ngoodTolerance, nPos, nNeg, nPixels)
189 self.log.debug(
"Candidate %d : OK flux=%.2f nPos=%d nNeg=%d nTot=%d nDetPos=%d nDetNeg=%d fPos=%.2f fNeg=%2f",
190 source.getId(), flux, nPos, nNeg, nPixels, nDetPos, nDetNeg, fPos, fNeg)
197 (crDiffSourceFile, crDiffExposureFile) = args
200 crDiffExposure = afwImage.ExposureF(crDiffExposureFile)
205 expX0 = crDiffExposure.getX0()
206 expY0 = crDiffExposure.getY0()
207 expX1 = expX0 + crDiffExposure.getWidth() - 1
208 expY1 = expY0 + crDiffExposure.getHeight() - 1
210 for i
in range(crDiffSources.size()):
211 crDiffSource = crDiffSources[i]
217 xAstrom = crDiffSource.getXAstrom()
218 yAstrom = crDiffSource.getYAstrom()
219 Ixx = max(1.0, crDiffSource.getIxx())
220 Iyy = max(1.0, crDiffSource.getIyy())
221 x0 = max(expX0, int(xAstrom - scaling * Ixx))
222 x1 = min(expX1, int(xAstrom + scaling * Ixx))
223 y0 = max(expY0, int(yAstrom - scaling * Iyy))
224 y1 = min(expY1, int(yAstrom + scaling * Iyy))
227 subExp = afwImage.ExposureF(crDiffExposure, bbox)
228 subMi = subExp.getMaskedImage()
229 imArr, maArr, varArr = subMi.getArrays()
231 if analyst.testSource(crDiffSource, subMi):
232 ds9.mtv(subExp, frame=1)
234 if __name__ ==
"__main__":
Class for logical location of a persisted Persistable instance.
a container for holding hierarchical configuration data in memory.
An integer coordinate rectangle.
Class for storing generic metadata.