def detectSources(exposure, threshold, psf=None): """Detect sources above positiveThreshold in the provided exposure returning the sourceList """ if not psf: FWHM = 5 psf = algorithms.createPSF("DoubleGaussian", 15, 15, FWHM/(2*math.sqrt(2*math.log(2)))) # # Subtract background # mi = exposure.getMaskedImage() bctrl = afwMath.BackgroundControl(afwMath.NATURAL_SPLINE); bctrl.setNxSample(int(mi.getWidth()/256) + 1); bctrl.setNySample(int(mi.getHeight()/256) + 1); backobj = afwMath.makeBackground(mi.getImage(), bctrl) img = mi.getImage(); img -= backobj.getImageF(); del img if display: ds9.mtv(exposure) ds = detectFootprints(exposure, threshold) objects = ds.getFootprints() # # Time to actually measure # measPipelineDir = lsst.utils.getPackageDir('meas_pipeline') moPolicy = policy.Policy.createPolicy(os.path.join(measPipelineDir, "policy", "MeasureSources.paf")) moPolicy = moPolicy.getPolicy("measureObjects") measureSources = algorithms.makeMeasureSources(exposure, moPolicy, psf) sourceList = afwDetection.SourceSet() for i in range(len(objects)): source = afwDetection.Source() sourceList.append(source) source.setId(i) source.setFlagForDetection(source.getFlagForDetection() | algorithms.Flags.BINNED1); try: measureSources.apply(source, objects[i]) except Exception: pass if source.getFlagForDetection() & algorithms.Flags.EDGE: continue if display: xc, yc = source.getXAstrom() - mi.getX0(), source.getYAstrom() - mi.getY0() if False: ds9.dot("%.1f %d" % (source.getPsfFlux(), source.getId()), xc, yc+1) ds9.dot("+", xc, yc, size=1) return sourceList
def detectSources(exposure, threshold, psf=None): """Detect sources above positiveThreshold in the provided exposure returning the sourceList """ if not psf: FWHM = 5 psf = algorithms.createPSF("DoubleGaussian", 15, 15, FWHM / (2 * math.sqrt(2 * math.log(2)))) # # Subtract background # mi = exposure.getMaskedImage() bctrl = afwMath.BackgroundControl(afwMath.NATURAL_SPLINE) bctrl.setNxSample(int(mi.getWidth() / 256) + 1) bctrl.setNySample(int(mi.getHeight() / 256) + 1) backobj = afwMath.makeBackground(mi.getImage(), bctrl) img = mi.getImage() img -= backobj.getImageF() del img if display: disp = afwDisplay.Display() disp.mtv(exposure) ds = detectFootprints(exposure, threshold) objects = ds.getFootprints() # # Time to actually measure # measPipelineDir = lsst.utils.getPackageDir('meas_pipeline') moPolicy = policy.Policy.createPolicy( os.path.join(measPipelineDir, "policy", "MeasureSources.paf")) moPolicy = moPolicy.getPolicy("measureObjects") measureSources = algorithms.makeMeasureSources(exposure, moPolicy, psf) sourceList = afwDetection.SourceSet() for i in range(len(objects)): source = afwDetection.Source() sourceList.append(source) source.setId(i) source.setFlagForDetection(source.getFlagForDetection() | algorithms.Flags.BINNED1) try: measureSources.apply(source, objects[i]) except Exception: pass if source.getFlagForDetection() & algorithms.Flags.EDGE: continue if display: xc, yc = source.getXAstrom() - mi.getX0(), source.getYAstrom( ) - mi.getY0() if False: display.dot( "%.1f %d" % (source.getPsfInstFlux(), source.getId()), xc, yc + 1) disp.dot("+", xc, yc, size=1) return sourceList
def run2(self, exposure, defects=None, background=None): assert exposure is not None, "No exposure provided" print('creating fake PSF...') psf, wcs = self.fakePsf(exposure) print('wcs is', wcs) if False: import lsst.meas.utils.sourceDetection as muDetection import lsst.meas.utils.sourceMeasurement as muMeasurement # phot.py : detect() print('Simulating detect()...') policy = self.config['detect'] posSources, negSources = muDetection.detectSources( exposure, psf, policy.getPolicy()) numPos = len( posSources.getFootprints()) if posSources is not None else 0 numNeg = len( negSources.getFootprints()) if negSources is not None else 0 print('found', numPos, 'pos and', numNeg, 'neg footprints') footprintSet = posSources # phot.py : measure() print('Simulating measure()...') policy = self.config['measure'].getPolicy() footprints = [] num = len(footprintSet.getFootprints()) self.log.log(self.log.INFO, "Measuring %d positive footprints" % num) footprints.append([footprintSet.getFootprints(), False]) sources = muMeasurement.sourceMeasurement(exposure, psf, footprints, policy) print('sources:', sources) for s in sources: print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) # sourceMeasurement(): import lsst.meas.algorithms as measAlg import lsst.afw.detection as afwDetection print('Simulating sourceMeasurement()...') exposure.setPsf(psf) measureSources = measAlg.makeMeasureSources(exposure, policy) # print('ms policy', str(measureSources.getPolicy())) # print('ms astrom:', measureSources.getMeasureAstrom()) # print('ms photom:', measureSources.getMeasurePhotom()) # print('ms shape:', measureSources.getMeasureShape()) F = footprints[0][0] for i in range(len(F)): # create a new source, and add it to the list, initialize ... s = afwDetection.Source() f = F[i] print('measuring footprint', f) measureSources.apply(s, f) print('got', s) print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) print('initial photometry...') sources, footprints = self.phot(exposure, psf) # print('got sources', str(sources)) # print('got footprints', str(footprints)) # print('sources:', sources) print('got sources:', len(sources)) for s in sources: print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) print('re-photometry...') sources = self.rephot(exposure, footprints, psf) # print('got sources', str(sources)) print('sources:', len(sources)) for s in sources: print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) return psf, sources, footprints
def run2(self, exposure, defects=None, background=None): assert exposure is not None, "No exposure provided" print('creating fake PSF...') psf, wcs = self.fakePsf(exposure) print('wcs is', wcs) if False: import lsst.meas.utils.sourceDetection as muDetection import lsst.meas.utils.sourceMeasurement as muMeasurement # phot.py : detect() print('Simulating detect()...') policy = self.config['detect'] posSources, negSources = muDetection.detectSources(exposure, psf, policy.getPolicy()) numPos = len(posSources.getFootprints()) if posSources is not None else 0 numNeg = len(negSources.getFootprints()) if negSources is not None else 0 print('found', numPos, 'pos and', numNeg, 'neg footprints') footprintSet = posSources # phot.py : measure() print('Simulating measure()...') policy = self.config['measure'].getPolicy() footprints = [] num = len(footprintSet.getFootprints()) self.log.log(self.log.INFO, "Measuring %d positive footprints" % num) footprints.append([footprintSet.getFootprints(), False]) sources = muMeasurement.sourceMeasurement(exposure, psf, footprints, policy) print('sources:', sources) for s in sources: print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) # sourceMeasurement(): import lsst.meas.algorithms as measAlg import lsst.afw.detection as afwDetection print('Simulating sourceMeasurement()...') exposure.setPsf(psf) measureSources = measAlg.makeMeasureSources(exposure, policy) # print('ms policy', str(measureSources.getPolicy())) # print('ms astrom:', measureSources.getMeasureAstrom()) # print('ms photom:', measureSources.getMeasurePhotom()) # print('ms shape:', measureSources.getMeasureShape()) F = footprints[0][0] for i in range(len(F)): # create a new source, and add it to the list, initialize ... s = afwDetection.Source() f = F[i] print('measuring footprint', f) measureSources.apply(s, f) print('got', s) print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) print('initial photometry...') sources, footprints = self.phot(exposure, psf) # print('got sources', str(sources)) # print('got footprints', str(footprints)) # print('sources:', sources) print('got sources:', len(sources)) for s in sources: print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) print('re-photometry...') sources = self.rephot(exposure, footprints, psf) # print('got sources', str(sources)) print('sources:', len(sources)) for s in sources: print(' ', s, s.getXAstrom(), s.getYAstrom(), s.getPsfFlux(), s.getIxx(), s.getIyy(), s.getIxy()) return psf, sources, footprints
def measure(self): """Detect and measure sources""" mi = self.exposure.getMaskedImage() # # We do a pretty good job of interpolating, so don't propagagate the convolved CR/INTRP bits # (we'll keep them for the original CR/INTRP pixels) # savedMask = mi.getMask().Factory(mi.getMask(), True) saveBits = savedMask.getPlaneBitMask("CR") | \ savedMask.getPlaneBitMask("BAD") | \ savedMask.getPlaneBitMask("INTRP") # Bits to not convolve savedMask &= saveBits msk = mi.getMask(); msk &= ~saveBits; del msk # Clear the saved bits # # Smooth image # cnvImage = mi.Factory(mi.getBBox(afwImage.PARENT)) afwMath.convolve(cnvImage, mi, self.psf.getKernel(), afwMath.ConvolutionControl()) msk = cnvImage.getMask(); msk |= savedMask; del msk # restore the saved bits threshold = afwDetection.Threshold(3, afwDetection.Threshold.STDEV) # # Only search the part of the frame that was PSF-smoothed # llc = afwGeom.PointI(self.psf.getKernel().getWidth()/2, self.psf.getKernel().getHeight()/2) urc = afwGeom.PointI(cnvImage.getWidth() - 1, cnvImage.getHeight() - 1) - afwGeom.ExtentI(llc[0], llc[1]); middle = cnvImage.Factory(cnvImage, afwGeom.BoxI(llc, urc), afwImage.LOCAL) ds = afwDetection.FootprintSetF(middle, threshold, "DETECTED") del middle # # ds only searched the middle but it belongs to the entire MaskedImage # ds.setRegion(mi.getBBox(afwImage.PARENT)) # # We want to grow the detections into the edge by at least one pixel so that it sees the EDGE bit # grow, isotropic = 1, False ds = afwDetection.FootprintSetF(ds, grow, isotropic) ds.setMask(mi.getMask(), "DETECTED") # # Reinstate the saved (e.g. BAD) (and also the DETECTED | EDGE) bits in the unsmoothed image # savedMask <<= cnvImage.getMask() msk = mi.getMask(); msk |= savedMask; del msk del savedMask; savedMask = None #msk = mi.getMask(); msk &= ~0x10; del msk # XXXX if self.display: ds9.mtv(mi, frame = 0, lowOrderBits = True) ds9.mtv(cnvImage, frame = 1) objects = ds.getFootprints() # # Time to actually measure # msPolicy = policy.Policy.createPolicy(policy.DefaultPolicyFile("meas_algorithms", "examples/measureSources.paf")) msPolicy = msPolicy.getPolicy("measureSources") measureSources = measAlg.makeMeasureSources(self.exposure, msPolicy) self.sourceList = afwDetection.SourceSet() for i in range(len(objects)): source = afwDetection.Source() self.sourceList.append(source) source.setId(i) source.setFlagForDetection(source.getFlagForDetection() | measAlg.Flags.BINNED1); try: measureSources.apply(source, objects[i]) except Exception, e: try: print e except Exception, ee: print ee