def testArrays(self): image1 = afwImage.MaskU(afwGeom.ExtentI(5,6)) # could use MaskU(5, 6) but check extent(5, 6) form too array1 = image1.getArray() self.assertEqual(array1.shape[0], image1.getHeight()) self.assertEqual(array1.shape[1], image1.getWidth()) image2 = afwImage.MaskU(array1, False) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) image3 = afwImage.makeMaskFromArray(array1) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) self.assertEqual(type(image3), afwImage.MaskU) array1[:,:] = np.random.uniform(low=0, high=10, size=array1.shape) self.assertMasksEqual(image1, array1)
def testArrays(self): image1 = afwImage.MaskU(afwGeom.ExtentI( 5, 6)) # could use MaskU(5, 6) but check extent(5, 6) form too array1 = image1.getArray() self.assertEqual(array1.shape[0], image1.getHeight()) self.assertEqual(array1.shape[1], image1.getWidth()) image2 = afwImage.MaskU(array1, False) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) image3 = afwImage.makeMaskFromArray(array1) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) self.assertEqual(type(image3), afwImage.MaskU) array1[:, :] = np.random.uniform(low=0, high=10, size=array1.shape) self.assertMasksEqual(image1, array1)
def testArrays(self): # could use Mask(5, 6) but check extent(5, 6) form too image1 = afwImage.Mask(lsst.geom.ExtentI(5, 6)) array1 = image1.getArray() self.assertEqual(array1.shape[0], image1.getHeight()) self.assertEqual(array1.shape[1], image1.getWidth()) image2 = afwImage.Mask(array1, False) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) image3 = afwImage.makeMaskFromArray(array1) self.assertEqual(array1.shape[0], image2.getHeight()) self.assertEqual(array1.shape[1], image2.getWidth()) self.assertEqual(type(image3), afwImage.Mask[afwImage.MaskPixel]) array1[:, :] = np.random.uniform(low=0, high=10, size=array1.shape) self.assertMasksEqual(image1, array1) array2 = image1.array np.testing.assert_array_equal(image1.array, array2) array3 = np.random.uniform(low=0, high=10, size=array1.shape).astype(array1.dtype) image1.array = array3 np.testing.assert_array_equal(array1, array3)
def run(self, coaddExposures, bbox, wcs, dataIds, **kwargs): """Warp coadds from multiple tracts to form a template for image diff. Where the tracts overlap, the resulting template image is averaged. The PSF on the template is created by combining the CoaddPsf on each template image into a meta-CoaddPsf. Parameters ---------- coaddExposures : `list` of `lsst.afw.image.Exposure` Coadds to be mosaicked bbox : `lsst.geom.Box2I` Template Bounding box of the detector geometry onto which to resample the coaddExposures wcs : `lsst.afw.geom.SkyWcs` Template WCS onto which to resample the coaddExposures dataIds : `list` of `lsst.daf.butler.DataCoordinate` Record of the tract and patch of each coaddExposure. **kwargs Any additional keyword parameters. Returns ------- result : `lsst.pipe.base.Struct` containing - ``outputExposure`` : a template coadd exposure assembled out of patches """ # Table for CoaddPSF tractsSchema = afwTable.ExposureTable.makeMinimalSchema() tractKey = tractsSchema.addField('tract', type=np.int32, doc='Which tract') patchKey = tractsSchema.addField('patch', type=np.int32, doc='Which patch') weightKey = tractsSchema.addField( 'weight', type=float, doc='Weight for each tract, should be 1') tractsCatalog = afwTable.ExposureCatalog(tractsSchema) finalWcs = wcs bbox.grow(self.config.templateBorderSize) finalBBox = bbox nPatchesFound = 0 maskedImageList = [] weightList = [] for coaddExposure, dataId in zip(coaddExposures, dataIds): # warp to detector WCS warped = self.warper.warpExposure(finalWcs, coaddExposure, maxBBox=finalBBox) # Check if warped image is viable if not np.any(np.isfinite(warped.image.array)): self.log.info("No overlap for warped %s. Skipping" % dataId) continue exp = afwImage.ExposureF(finalBBox, finalWcs) exp.maskedImage.set(np.nan, afwImage.Mask.getPlaneBitMask("NO_DATA"), np.nan) exp.maskedImage.assign(warped.maskedImage, warped.getBBox()) maskedImageList.append(exp.maskedImage) weightList.append(1) record = tractsCatalog.addNew() record.setPsf(coaddExposure.getPsf()) record.setWcs(coaddExposure.getWcs()) record.setPhotoCalib(coaddExposure.getPhotoCalib()) record.setBBox(coaddExposure.getBBox()) record.setValidPolygon( afwGeom.Polygon( geom.Box2D(coaddExposure.getBBox()).getCorners())) record.set(tractKey, dataId['tract']) record.set(patchKey, dataId['patch']) record.set(weightKey, 1.) nPatchesFound += 1 if nPatchesFound == 0: raise pipeBase.NoWorkFound("No patches found to overlap detector") # Combine images from individual patches together statsFlags = afwMath.stringToStatisticsProperty('MEAN') statsCtrl = afwMath.StatisticsControl() statsCtrl.setNanSafe(True) statsCtrl.setWeighted(True) statsCtrl.setCalcErrorFromInputVariance(True) templateExposure = afwImage.ExposureF(finalBBox, finalWcs) templateExposure.maskedImage.set( np.nan, afwImage.Mask.getPlaneBitMask("NO_DATA"), np.nan) xy0 = templateExposure.getXY0() # Do not mask any values templateExposure.maskedImage = afwMath.statisticsStack(maskedImageList, statsFlags, statsCtrl, weightList, clipped=0, maskMap=[]) templateExposure.maskedImage.setXY0(xy0) # CoaddPsf centroid not only must overlap image, but must overlap the part of # image with data. Use centroid of region with data boolmask = templateExposure.mask.array & templateExposure.mask.getPlaneBitMask( 'NO_DATA') == 0 maskx = afwImage.makeMaskFromArray(boolmask.astype(afwImage.MaskPixel)) centerCoord = afwGeom.SpanSet.fromMask(maskx, 1).computeCentroid() ctrl = self.config.coaddPsf.makeControl() coaddPsf = CoaddPsf(tractsCatalog, finalWcs, centerCoord, ctrl.warpingKernelName, ctrl.cacheSize) if coaddPsf is None: raise RuntimeError("CoaddPsf could not be constructed") templateExposure.setPsf(coaddPsf) templateExposure.setFilter(coaddExposure.getFilter()) templateExposure.setPhotoCalib(coaddExposure.getPhotoCalib()) return pipeBase.Struct(outputExposure=templateExposure)