Commit 22908264 authored by Wei Chengliang's avatar Wei Chengliang
Browse files

optimize drawImage in subbandpass; improve PSF memory; refactor galaxy size

parent 8a9c0134
Loading
Loading
Loading
Loading
+21 −6
Original line number Diff line number Diff line
@@ -276,7 +276,7 @@ class Catalog(CatalogBase):
        self.lamb_gal = np.linspace(500, 11000, 10501)
        self.pcs = pcs["PC"][()]

    def _load_gals(self, gals, galsSED, pix_id=None, cat_id=0, agnsed_file=""):
    def _load_gals(self, gals, galsSED, pix_id=None, cat_id=0, agnsed_file="", C9SizeModel=True):
        ngals = len(gals["ra"])

        # Apply astrometric modeling
@@ -386,11 +386,26 @@ class Catalog(CatalogBase):
                param["bulgemass"] + param["diskmass"]
            )

            if C9SizeModel:
                param['size']  = gals["size"][igals]
                if param["size"] > self.max_size:
                    self.max_size = param["size"]

                if param['bfrac'] >= 0.6:
                    param['hlr_bulge'] = param['size']
                    param['hlr_disk'] = param['size'] * (1. - param['bfrac'])
                else:
                    param['hlr_disk'] = param['size']
                    param['hlr_bulge'] = param['size'] * param['bfrac']
                param["bulgesize"] = param['hlr_bulge']
                param["disksize"] = param['hlr_disk']
            else:
                angdist = (self.cosmo.angular_diameter_distance(param["z"]) * self.cosmo.h).value
                param["bulgesize"] = np.rad2deg(gals["bulgesize"][igals] / angdist) * 3600.
                param["disksize"] = np.rad2deg(gals["diskradius"][igals] / angdist) * 3600.
                param["hlr_bulge"] = param["bulgesize"]
                param["hlr_disk"] = param["disksize"]

            if param["hlr_bulge"] < 0.01 and param["hlr_disk"] < 0.01:
                continue
            if param["hlr_bulge"] > 5.0 or param["hlr_disk"] > 5.0:
+17 −23
Original line number Diff line number Diff line
@@ -226,9 +226,7 @@ class Galaxy(MockObject):
            # print("nphotons_sub-band_%d = %.2f"%(i, nphotons))

            # Get PSF model
            EXTRA = False
            if self.getMagFilter(filt) <= filt.mag_saturation - 1.0:
                EXTRA = True
            EXTRA = self.getMagFilter(filt) <= filt.mag_saturation - 1.0
            psf, pos_shear = psf_model.get_PSF(
                chip=chip,
                pos_img=pos_img,
@@ -251,20 +249,18 @@ class Galaxy(MockObject):

            gal_temp = gal_temp.withFlux(nphotons)

            if i == 0:
                gal = gal_temp
            else:
                gal = gal + gal_temp
            #   if i == 0:
            #       gal = gal_temp
            #   else:
            #       gal = gal + gal_temp

            # (TEST) Random knots
            # knots = galsim.RandomKnots(npoints=100, profile=disk)
            # kfrac = np.random.random()*(1.0 - self.bfrac)
            # gal = self.bfrac * bulge + (1.0 - self.bfrac - kfrac) * disk + kfrac * knots

        # stamp = gal.drawImage(wcs=chip_wcs_local, method='phot', offset=offset, save_photons=True)
        stamp = gal.drawImage(wcs=chip_wcs_local, offset=offset)
            stamp = gal_temp.drawImage(wcs=chip_wcs_local, offset=offset)
            if np.sum(np.isnan(stamp.array)) > 0:
            # ERROR happens
                return 2, pos_shear
            stamp.setCenter(x_nominal, y_nominal)
            bounds = stamp.bounds & galsim.BoundsI(0, chip.npix_x - 1, 0, chip.npix_y - 1)
@@ -274,6 +270,7 @@ class Galaxy(MockObject):
                is_updated = 1
                chip.img.setOrigin(chip.bound.xmin, chip.bound.ymin)
                del stamp
            del gal_temp

        if is_updated == 0:
            # Return code 0: object photons missed this detector
@@ -281,9 +278,6 @@ class Galaxy(MockObject):
            # if self.logger:
            #     self.logger.info("obj %s missed" % (self.id))
            return 0, pos_shear

        # # [C6 TEST]
        # print("nphotons_sum = ", nphotons_sum)
        return 1, pos_shear

    def drawObj_slitless(
+3 −5
Original line number Diff line number Diff line
@@ -205,9 +205,7 @@ class MockObject(object):
            # print("nphotons_sub-band_%d = %.2f"%(i, nphotons))

            # Get PSF model
            EXTRA = False
            if self.getMagFilter(filt) <= filt.mag_saturation - 1.0:
                EXTRA = True
            EXTRA = self.getMagFilter(filt) <= filt.mag_saturation - 1.0
            psf, pos_shear = psf_model.get_PSF(
                chip=chip,
                pos_img=pos_img,
@@ -225,7 +223,7 @@ class MockObject(object):
            else:
                stamp = star.drawImage(wcs=chip_wcs_local, offset=offset)
            if np.sum(np.isnan(stamp.array)) > 0:
                continue
                return 2, pos_shear
            stamp.setCenter(x_nominal, y_nominal)
            bounds = stamp.bounds & galsim.BoundsI(
                0, chip.npix_x - 1, 0, chip.npix_y - 1
+88 −92
Original line number Diff line number Diff line
@@ -18,9 +18,21 @@ from observation_sim.psf._util import psf_extrapolate
NPSF = 900  # ***# 30*30
PixSizeInMicrons = 5.0  # ***# in microns

def tukey2d_radial(shape, alpha=0.3):
    H, W = int(shape[0]), int(shape[1])
    y, x = np.ogrid[:H, :W]
    cy, cx = (H - 1) / 2.0, (W - 1) / 2.0

    r = np.sqrt((x - cx)**2 + (y - cy)**2)
    r_max = np.sqrt(cx**2 + cy**2)
    r_norm = r / r_max

    t = np.clip((r_norm - (1 - alpha)) / alpha, 0, 1)
    return 0.5 * (1 + np.cos(np.pi * t))


# find neighbors-KDtree #
def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True):
def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True, tree=None):
    """
    find nearest neighbors by 2D-KDTree

@@ -36,6 +48,7 @@ def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True):
    """
    datax = px
    datay = py
    if tree is None:
        tree = spatial.KDTree(list(zip(datax.ravel(), datay.ravel())))

    dataq = []
@@ -152,6 +165,7 @@ def psfMaker_IDW(
    hoc=None,
    hoclist=None,
    PSFCentroidWgt=False,
    tree=None,
):
    """
    psf interpolation by IDW
@@ -178,21 +192,29 @@ def psfMaker_IDW(
    if OnlyNeighbors is True:
        if hoc is None:
            neigh = findNeighbors(
                px, py, cen_col, cen_row, dr=5.0, dn=4, OnlyDistance=False
                px, py, cen_col, cen_row, dr=5.0, dn=4, OnlyDistance=False, tree=tree
            )
        if hoc is not None:
            neigh = findNeighbors_hoclist(
                cen_col, cen_row, tx=px, ty=py, dn=4, hoc=hoc, hoclist=hoclist
            )

        neighFlag = np.zeros(npsf)
        neighFlag[neigh] = 1

        neigh = np.unique(np.asarray(neigh))
        for ipsf in neigh:
            dist = np.sqrt((ref_col - cen_col[ipsf])
                           ** 2 + (ref_row - cen_row[ipsf])**2)
            if IDWindex == 1:
                psfWeight[ipsf] = dist
            if IDWindex == 2:
                psfWeight[ipsf] = dist**2
            if IDWindex == 3:
                psfWeight[ipsf] = dist**3
            if IDWindex == 4:
                psfWeight[ipsf] = dist**4
            psfWeight[ipsf] = max(psfWeight[ipsf], minimum_psf_weight)
            psfWeight[ipsf] = 1./psfWeight[ipsf]
    else:
        for ipsf in range(npsf):
        if OnlyNeighbors is True:
            if neighFlag[ipsf] != 1:
                continue

            dist = np.sqrt((ref_col - cen_col[ipsf]) ** 2 + (ref_row - cen_row[ipsf]) ** 2)
            if IDWindex == 1:
                psfWeight[ipsf] = dist
@@ -207,17 +229,17 @@ def psfMaker_IDW(
    psfWeight /= np.sum(psfWeight)

    psfMaker = np.zeros([ngy, ngx], dtype=np.float32)
    for ipsf in range(npsf):
    if OnlyNeighbors is True:
            if neighFlag[ipsf] != 1:
                continue

        # PSFMat[ipsf] is only read, no copy needed
        for ipsf in neigh:
            psfMaker += PSFMat[ipsf] * psfWeight[ipsf]
    else:
        for ipsf in range(npsf):
            iPSFMat = PSFMat[ipsf, :, :].copy()
            ipsfWeight = psfWeight[ipsf]

            psfMaker += iPSFMat * ipsfWeight
    psfMaker /= np.nansum(psfMaker)

    return psfMaker


@@ -256,6 +278,8 @@ class PSFInterp(PSFModel):

        self.nwave = self._getPSFwave(self.iccd, PSF_data_file, PSF_data_prefix)
        self.npsf = npsf
        ngy, ngx = self._getPSFshape(self.iccd, PSF_data_file, PSF_data_prefix)
        self.psfMat = np.zeros([self.nwave, self.npsf, ngy, ngx], dtype=np.float32)
        self.PSF_data = self._loadPSF(self.iccd, PSF_data_file, PSF_data_prefix)

        if self.LOG_DEBUG:
@@ -267,18 +291,14 @@ class PSFInterp(PSFModel):
                flush=True,
            )

        ngy, ngx = self.PSF_data[0][0]["psfMat"].shape
        self.psfMat = np.zeros([self.nwave, self.npsf, ngy, ngx], dtype=np.float32)
        self.cen_col = np.zeros([self.nwave, self.npsf], dtype=np.float32)
        self.cen_row = np.zeros([self.nwave, self.npsf], dtype=np.float32)
        self.hoc = []
        self.hoclist = []
        self.tree_list = [None] * self.nwave

        for twave in range(self.nwave):
            for tpsf in range(self.npsf):
                self.psfMat[twave, tpsf, :, :] = self.PSF_data[twave][tpsf]["psfMat"]
                self.PSF_data[twave][tpsf]["psfMat"] = 0  # free psfMat

                self.pixsize = self.PSF_data[twave][tpsf]["pixsize"] * 1e-3  # mm
                self.cen_col[twave, tpsf] = (
                    self.PSF_data[twave][tpsf]["image_x"]
@@ -309,6 +329,13 @@ class PSFInterp(PSFModel):
        fq.close()
        return nwave

    def _getPSFshape(self, iccd, PSF_data_file, PSF_data_prefix):
        fq = h5py.File(PSF_data_file+'/' + PSF_data_prefix +
                       'psfCube_{:}.h5'.format(iccd), 'r')
        ngy, ngx = fq['w_1']['psf_1']['psfMat'].shape
        fq.close()
        return ngy, ngx

    def _loadPSF(self, iccd, PSF_data_file, PSF_data_prefix):
        psfSet = []
        # fq = h5py.File(PSF_data_file+'/' +PSF_data_prefix +'psfCube_ccd{:}.h5'.format(iccd), 'r')
@@ -320,10 +347,11 @@ class PSFInterp(PSFModel):
            psfWave = []

            fq_iwave = fq["w_{:}".format(iwave)]
            wavelength = fq_iwave['wavelength'][()]
            for jj in range(self.npsf):
                ipsf = jj + 1
                psfInfo = {}
                psfInfo["wavelength"] = fq_iwave["wavelength"][()]
                psfInfo["wavelength"] = wavelength

                fq_iwave_ipsf = fq_iwave["psf_{:}".format(ipsf)]
                psfInfo["pixsize"] = PixSizeInMicrons
@@ -333,7 +361,7 @@ class PSFInterp(PSFModel):
                psfInfo["image_y"] = fq_iwave_ipsf["image_y"][()]
                psfInfo["centroid_x"] = fq_iwave_ipsf["cx"][()]
                psfInfo["centroid_y"] = fq_iwave_ipsf["cy"][()]
                psfInfo["psfMat"] = fq_iwave_ipsf["psfMat"][()]
                fq_iwave_ipsf['psfMat'].read_direct(self.psfMat[ii, jj])

                psfWave.append(psfInfo)
            psfSet.append(psfWave)
@@ -355,6 +383,13 @@ class PSFInterp(PSFModel):
                return twave
        return -1

    def _get_tree(self, twave):
        if self.tree_list[twave] is None:
            self.tree_list[twave] = spatial.KDTree(
                np.column_stack((self.cen_col[twave],
                                 self.cen_row[twave])))
        return self.tree_list[twave]

    def get_PSF(
        self,
        chip,
@@ -402,6 +437,7 @@ class PSFInterp(PSFModel):
                IDWindex=2,
                OnlyNeighbors=True,
                PSFCentroidWgt=True,
                tree=self._get_tree(twave),
            )
        if findNeighMode == "hoclistFind":
            assert self.hoc != 0, "hoclist should be built correctly!"
@@ -418,73 +454,33 @@ class PSFInterp(PSFModel):
                PSFCentroidWgt=True,
            )

        if extrapolate is True:
            ccdList = [
                0,
                6,
                7,
                8,
                9,
                11,
                12,
                13,
                14,
                15,
                16,
                17,
                18,
                19,
                20,
                22,
                23,
                24,
                25,
            ]
            rr_trim_list = [
                72,
                72,
                64,
                96,
                88,
                64,
                72,
                72,
                76,
                72,
                72,
                76,
                72,
                72,
                64,
                88,
                96,
                64,
                72,
            ]
            imPSF = psf_extrapolate(
                imPSF, rr_trim=rr_trim_list[ccdList.index(chip.chipID)], ngg=ngg
            )
        if extrapolate:
            RR_TRIM_MAP = {
                0: 72, 6: 72, 7: 64, 8: 96, 9: 88, 11: 64, 12: 72, 13: 72, 14: 76,
                15: 72, 16: 72, 17: 76, 18: 72, 19: 72, 20: 64, 22: 88, 23: 96,
                24: 64, 25: 72,
            }
            imPSF = psf_extrapolate(imPSF, rr_trim=RR_TRIM_MAP[chip.chipID], ngg=ngg)
            imPSF = imPSF * tukey2d_radial(imPSF.shape, alpha=0.3)

        imPSF = imPSF / np.nansum(imPSF)
        if galsimGSObject:
            if extrapolate is True:
            if extrapolate:
                imPSFt = np.zeros([ngg + 1, ngg + 1])
                imPSFt[:-1, :-1] = imPSF
                pad_factor=1.5
            else:
                imPSFt = np.zeros([257, 257])
                imPSFt[0:256, 0:256] = imPSF
                pad_factor=2

            img = galsim.ImageF(imPSFt, scale=pixSize)
            gsp = galsim.GSParams(folding_threshold=folding_threshold)
            # TEST: START
            # Use sheared PSF to test the PSF orientation
            # self.psf = galsim.InterpolatedImage(img, gsparams=gsp).shear(g1=0.8, g2=0.)
            # TEST: END
            self.psf = galsim.InterpolatedImage(img, gsparams=gsp)
            self.psf = galsim.InterpolatedImage(img, gsparams=gsp, pad_factor=pad_factor)
            wcs = chip.img.wcs.local(pos_img)
            scale = galsim.PixelScale(0.074)
            self.psf = wcs.toWorld(scale.toImage(self.psf), image_pos=(pos_img))

            # return self.PSFspin(x=px/0.01, y=py/0.01)
            return self.psf, galsim.Shear(e=0.0, beta=(np.pi / 2) * galsim.radians)
        return imPSF