def measure(self, measRecord, exposure): center = measRecord.getCentroid() local_lin_wcs = exposure.getWcs().linearizePixelToSky(center, afwGeom.arcseconds) jacobian = local_lin_wcs.getLinear().getMatrix() sky_pos = exposure.getWcs().pixelToSky(center) uvref = (sky_pos.getRa().asArcseconds(), sky_pos.getDec().asArcseconds()) box = measRecord.getFootprint().getBBox() xy_pos = (center.getX() - box.getMinX(), center.getY() - box.getMinY()) bfd_wcs = bfd.WCS(jacobian, xyref=xy_pos, uvref=uvref) factor = exposure.getMetadata().get('variance_scale') noise = np.median(exposure.variance[box].array)/factor image = exposure.image[box].array psf_image = exposure.getPsf().computeKernelImage(center).array kdata = bfd.generalImage(image, uvref, psf_image, wcs=bfd_wcs, pixel_noise=noise, size=self.config.min_size) conjugate = set(np.where(kdata.conjugate.flatten()==False)[0]) kgal = self.bfd.KGalaxy(self.weight, kdata.kval.flatten(), kdata.kx.flatten(), kdata.ky.flatten(), kdata.kvar.flatten(), kdata.d2k, conjugate) #nulled_data, dx, dy = kgal.getNulled(self.config.max_shift) cm = CenterMoment(self.bfd.BFDConfig, kgal) dx, failed, msg = cm.recenter(self.config.weight_sigma) #print(dx,converge,msg) if failed is True: target = kgal.getTarget() measRecord.set(self.flag, 1) measRecord.set(self.centroid_flag, 1) measRecord.set(self.failed_moments, np.array(target.mom.m, dtype=np.float32)) measRecord.set(self.failed_odd, np.array(target.mom.xy, dtype=np.float32)) else: target = kgal.getShifted(dx[0], dx[1]).getTarget() mom_even = target.mom.m mom_odd = target.mom.xy cov_even = target.cov.m cov_odd = target.cov.xy cov_even_save = [] cov_odd_save = [] for ii in range(cov_even.shape[0]): cov_even_save.extend(cov_even[ii][ii:]) for ii in range(cov_odd.shape[0]): cov_odd_save.extend(cov_odd[ii][ii:]) measRecord.set(self.moment, np.array(mom_even, dtype=np.float32)) measRecord.set(self.odd, np.array(mom_odd, dtype=np.float32)) measRecord.set(self.cov_even, np.array(cov_even_save, dtype=np.float32)) measRecord.set(self.cov_odd, np.array(cov_odd_save, dtype=np.float32)) measRecord.set(self.shift, np.array([dx[0], dx[1]], dtype=np.float32))
def measure(self, measRecord, exposure): center = measRecord.getCentroid() local_lin_wcs = exposure.getWcs().linearizePixelToSky( center, afwGeom.arcseconds) jacobian = local_lin_wcs.getLinear().getMatrix() sky_pos = exposure.getWcs().pixelToSky(center) uvref = (sky_pos.getRa().asArcseconds(), sky_pos.getDec().asArcseconds()) box = measRecord.getFootprint().getBBox() xy_pos = (center.getX() - box.getMinX(), center.getY() - box.getMinY()) box.shift(-afwGeom.Extent2I(exposure.getXY0())) bfd_wcs = bfd.WCS(jacobian, xyref=xy_pos, uvref=uvref) noise = np.median(exposure.variance[box].array) image = exposure.image[box].array psf_image = exposure.getPsf().computeKernelImage(center).array kdata = bfd.generalImage(image, uvref, psf_image, wcs=bfd_wcs, pixel_noise=noise) moment_calc = bfd.MomentCalculator(kdata, self.weight, id=measRecord.getId()) xyshift, error, msg = moment_calc.recenter() if error: measRecord.set(self.flag, 1) measRecord.set(self.centroid_flag, 1) return else: covgal = moment_calc.get_covariance() moment = moment_calc.get_moment(0, 0) measRecord.set(self.moment, np.array(moment.even, dtype=np.float32)) cov_even_save = [] cov_odd_save = [] for ii in range(covgal[0].shape[0]): cov_even_save.extend(covgal[0][ii][ii:]) for ii in range(covgal[1].shape[0]): cov_odd_save.extend(covgal[1][ii][ii:]) measRecord.set(self.cov_even, np.array(cov_even_save, dtype=np.float32)) measRecord.set(self.cov_odd, np.array(cov_odd_save, dtype=np.float32)) measRecord.set(self.xy, np.array(xyshift, dtype=np.float32))
def buildKGalaxy(self, record, exposures): center = record.getCentroid() # For wcs use the first band since a single tract should have the same wcs band = self.config.filters[0] local_lin_wcs = exposures[band].getWcs().linearizePixelToSky(center, afwGeom.arcseconds) jacobian = local_lin_wcs.getLinear().getMatrix() sky_pos = exposures[band].getWcs().pixelToSky(center) uvref = (sky_pos.getRa().asArcseconds(), sky_pos.getDec().asArcseconds()) if self.config.footprint_size is None or self.config.footprint_size < 0: box = record.getFootprint().getBBox() else: box = geom.Box2I(geom.Point2I(int(center.getX()), int(center.getY())), geom.Extent2I(1,1)) box.grow(self.config.footprint_size//2) xy_pos = (center.getX() - box.getMinX(), center.getY() - box.getMinY()) bfd_wcs = bfd.WCS(jacobian, xyref=xy_pos, uvref=uvref) kgals = [] for band in self.config.filters: exposure = exposures[band] factor = exposure.getMetadata().get('variance_scale') exp_box = geom.Box2I(box) exp_box.clip(exposure.getBBox()) noise = np.sqrt(np.median(exposure.variance[exp_box].array)) image = exposure.image[exp_box].array psf_image = exposure.getPsf().computeKernelImage(center).array kdata = bfd.generalImage(image, uvref, psf_image, wcs=bfd_wcs, pixel_noise=noise, size=self.config.grid_size) conjugate = set(np.where(kdata.conjugate.flatten()==False)[0]) kgal = self.bfd.KGalaxy(self.weight, kdata.kval.flatten(), kdata.kx.flatten(), kdata.ky.flatten(), kdata.kvar.flatten(), kdata.d2k, conjugate) kgals.append(kgal) return kgals
def buildKGalaxy(self, record, exposures): center = record.getCentroid() band = self.config.filters[0] local_lin_wcs = exposures[band].getWcs().linearizePixelToSky( center, afwGeom.arcseconds) jacobian = local_lin_wcs.getLinear().getMatrix() sky_pos = exposures[band].getWcs().pixelToSky(center) uvref = (sky_pos.getRa().asArcseconds(), sky_pos.getDec().asArcseconds()) box = record.getFootprint().getBBox() xy_pos = (center.getX() - box.getMinX(), center.getY() - box.getMinY()) bfd_wcs = bfd.WCS(jacobian, xyref=xy_pos, uvref=uvref) kgals = [] for band in self.config.filters: exposure = exposures[band] factor = exposure.getMetadata().get('variance_scale') noise = np.sqrt(np.median(exposure.variance[box].array) / factor) image = exposure.image[box].array psf_image = exposure.getPsf().computeKernelImage(center).array kdata = bfd.generalImage(image, uvref, psf_image, wcs=bfd_wcs, pixel_noise=noise, size=self.config.grid_size) conjugate = set(np.where(kdata.conjugate.flatten() == False)[0]) kgal = self.bfd.KGalaxy(self.weight, kdata.kval.flatten(), kdata.kx.flatten(), kdata.ky.flatten(), kdata.kvar.flatten(), kdata.d2k, conjugate) kgals.append(kgal) return kgals
(measRecord.getY() - box.getMinY())) sky_pos = exposure.getWcs().pixelToSky(measRecord.getCentroid()) uvref = (sky_pos.getRa().asArcseconds(), sky_pos.getDec().asArcseconds()) xref2 = ra - (jacobian2[0, 0] * (measRecord.getX() - box.getMinX()) + jacobian2[0, 1] * (measRecord.getY() - box.getMinY())) yref2 = dec - (jacobian2[1, 0] * (measRecord.getX() - box.getMinX()) + jacobian2[1, 1] * (measRecord.getY() - box.getMinY())) local_x = measRecord.getX() - box.getMinX() local_y = measRecord.getY() - box.getMinY() bfd_wcs = bfd.WCS(jacobian, xyref=(local_x, local_y), uvref=uvref) bfd_wcs2 = bfd.WCS(jacobian2, xyref=(local_x, local_y), uvref=uvref) noise = measRecord.get("base_Variance_value") kdata = bfd.simpleImage(image, uvref, psf_image.array, wcs=bfd_wcs, pixel_noise=noise) kdata2 = bfd.simpleImage(image, uvref, psf_image.array, wcs=bfd_wcs2, pixel_noise=noise) moment_calc = bfd.MomentCalculator(kdata,
def measure(self, measRecord, exposure): center = measRecord.getCentroid() # currently box must be square for python based measurements orig_box = measRecord.getFootprint().getBBox() w, h = orig_box.getWidth(), orig_box.getHeight() box_size = max(w, h) if box_size % 2 == 1: box_size += 1 box = afwGeom.Box2I( afwGeom.Point2I(center.getX() - box_size / 2, center.getY() - box_size / 2), afwGeom.Extent2I(box_size, box_size)) if not exposure.getBBox().contains(box): box_size = min(w, h) if box_size % 2 == 1: box_size += 1 box = afwGeom.Box2I( afwGeom.Point2I(center.getX() - box_size / 2, center.getY() - box_size / 2), afwGeom.Extent2I(box_size, box_size)) if not exposure.getBBox().contains(box): measRecord.set(self.flag, 1) return image = exposure.image[box].array # PSF image must also be the same size psf_image_base = galsim.ImageF( exposure.getPsf().computeKernelImage(center).array) psf_image_interp = galsim.InterpolatedImage(psf_image_base, scale=1) psf_image = galsim.ImageF(box_size, box_size) psf_image_interp.drawImage(psf_image, method='no_pixel') ra = measRecord.get('coord_ra').asArcseconds() dec = measRecord.get('coord_dec').asArcseconds() local_lin_wcs = exposure.getWcs().linearizePixelToSky( center, geom.arcseconds) jacobian = local_lin_wcs.getLinear().getMatrix() sky_pos = exposure.getWcs().pixelToSky(center) uvref = (sky_pos.getRa().asArcseconds(), sky_pos.getDec().asArcseconds()) xy_pos = (center.getX() - box.getMinX(), center.getY() - box.getMinY()) bfd_wcs = bfd.WCS(jacobian, xyref=xy_pos, uvref=uvref) noise = measRecord.get("base_Variance_value") kdata = bfd.simpleImage(image, uvref, psf_image.array, wcs=bfd_wcs, pixel_noise=noise) moment_calc = bfd.MomentCalculator(kdata, self.weight, id=measRecord.getId()) xyshift, error, msg = moment_calc.recenter() if error: measRecord.set(self.flag, 1) measRecord.set(self.centroid_flag, 1) return else: #cov_even, cov_odd = moment_calc.get_covariance() covgal = moment_calc.get_covariance() moment = moment_calc.get_moment(0, 0) measRecord.set(self.moment, np.array(moment.even, dtype=np.float32)) cov_even_save = [] cov_odd_save = [] for ii in range(covgal[0].shape[0]): cov_even_save.extend(covgal[0][ii][ii:]) for ii in range(covgal[1].shape[0]): cov_odd_save.extend(covgal[1][ii][ii:]) measRecord.set(self.cov_even, np.array(cov_even_save, dtype=np.float32)) measRecord.set(self.cov_odd, np.array(cov_odd_save, dtype=np.float32)) measRecord.set(self.xy, np.array(xyshift, dtype=np.float32))
def measure(self, measRecord, exposure): center = measRecord.getCentroid() psf_image = exposure.getPsf().computeKernelImage(center).array ra = measRecord.get('coord_ra').asArcseconds() dec = measRecord.get('coord_dec').asArcseconds() if usingHSC: local_lin_wcs = exposure.getWcs().linearizePixelToSky(center, afwGeom.arcseconds) else: local_lin_wcs = exposure.getWcs().linearizePixelToSky(center, geom.arcseconds) jacobian = local_lin_wcs.getLinear().getMatrix() sky_pos = exposure.getWcs().pixelToSky(center) uvref = (sky_pos.getRa().asArcseconds(), sky_pos.getDec().asArcseconds()) if usingHSC: box = measRecord.getFootprint().getBBox() xy_pos = (center.getX() - box.getMinX(), center.getY() - box.getMinY()) box.shift(-afwGeom.Extent2I(exposure.getXY0())) else: box = measRecord.getFootprint().getBBox(afwImage.PARENT) xy_pos = (center.getX() - box.getMinX(), center.getY() - box.getMinY()) bfd_wcs = bfd.WCS(jacobian, xyref=xy_pos, uvref=uvref) image = exposure.image[box].array kdata = bfd.generalImage(image, uvref, psf_image, wcs=bfd_wcs, pixel_noise=1) conjugate = set(np.where(kdata.conjugate.flatten()==False)[0]) kgal = self.bfd_config.KGalaxy(self.weight, kdata.kval.flatten(), kdata.kx.flatten(), kdata.ky.flatten(), kdata.kvar.flatten(), kdata.d2k, conjugate); g1 = np.random.rand()*(2*self.config.max_g) - self.config.max_g g2 = np.random.rand()*(2*self.config.max_g) - self.config.max_g mu = np.random.rand()*(2*self.config.max_mu) - self.config.max_mu shift_dx = np.random.randn()*self.config.sigma_dx shift_dy = np.random.randn()*self.config.sigma_dx nulled_data, dx, dy = kgal.getNulled(4) if nulled_data is None: measRecord.set(self.flag, True) measRecord.set(self.centroid_flag, True) return target = nulled_data.getTarget() shifted_data = nulled_data.getShifted(shift_dx, shift_dy) sheared_target = shifted_data.getShearedTarget(g1, g2, mu, False) moment = target.mom.m sheared_moment = sheared_target.mom.m measRecord.set(self.moment, np.array(moment, dtype=np.float32)) measRecord.set(self.sheared_moment, np.array(sheared_moment, dtype=np.float32)) measRecord.set(self.g1, g1) measRecord.set(self.g2, g2) measRecord.set(self.mu, mu) measRecord.set(self.dx, shift_dx) measRecord.set(self.dy, shift_dy)
origin=galsim.PositionD((N / 2), (N / 2))) wcs1 = galsim.TanWCS(affine1, sky_center) image1 = galsim.ImageF(N, N) psf_image1 = galsim.ImageF(N, N) psf.drawImage(image=psf_image1, wcs=wcs1) gal.drawImage(image=image1, wcs=wcs1) weight = bfd.KSigmaWeight(4, 2 * scale) uvref = (0, 0) jacobian1 = np.array([[d11, d12], [d21, d22]]) bfd_wcs = bfd.WCS(jacobian1, xyref=(N / 2, N / 2), uvref=uvref) noise = 1 kval, kx, ky, d2k, conjugate, kvar = simpleImage(image1.array, uvref, psf_image1.array, wcs=bfd_wcs) kshift = rshift(kval) weight.set_k(kx, ky) wshift = rshift(weight.w) kdata2 = bfd.simpleImage(image1.array, uvref, psf_image1.array, wcs=bfd_wcs, pixel_noise=noise)