Beispiel #1
0
 def _get_jacobians(self,source_id = None):
     """
     jlist is a dict, jac is a list of ngmix Jacobians
     """
     jlist = self.medsObj.get_jacobian_list(source_id)
     jac = [ngmix.Jacobian(row=jj['row0'],col=jj['col0'],dvdrow = jj['dvdrow'],dvdcol=jj['dvdcol'],dudrow=jj['dudrow'],dudcol=jj['dudcol']) for jj in jlist]
     return jac
Beispiel #2
0
    def get_shifted_jacobian(
        self,
        ra,
        dec,
        orig_row,
        orig_col,
        orig_cutout_row,
        orig_cutout_col,
    ):
        """
        get a jacobian with new elements as well as any
        required shift

        This doesn't work because of the absolute shift between
        the original coadd RA,DEC and the truth
        """
        gs_jac, row, col = self._get_gs_jacobian_and_rowcol(ra, dec)

        row_shift = row - orig_row
        col_shift = col - orig_col

        print("    position shift:", row_shift, col_shift)

        cutout_row = orig_cutout_row + row_shift
        cutout_col = orig_cutout_col + col_shift

        return ngmix.Jacobian(
            row=cutout_row,
            col=cutout_col,
            dudrow=gs_jac.dudy,
            dudcol=gs_jac.dudx,
            dvdrow=gs_jac.dvdy,
            dvdcol=gs_jac.dvdx,
        )
Beispiel #3
0
    def _get_jac(self, *, cenx, ceny):
        """
        get jacobian at the coadd image center

        make an ngmix jacobian with specified center specified (this is not the
        position used to evaluate the jacobian)

        Parameters
        ----------
        cenx: float
            Center for the output ngmix jacobian (not place of evaluation)
        ceny: float
            Center for the output ngmix jacobian (not place of evaluation)
        """

        coadd_wcs = self.coadd_exp.getWcs()

        dm_jac = coadd_wcs.linearizePixelToSky(self.coadd_cen, geom.arcseconds)
        matrix = dm_jac.getLinear().getMatrix()

        # note convention differences
        return ngmix.Jacobian(
            x=cenx,
            y=ceny,
            dudx=matrix[1, 1],
            dudy=-matrix[1, 0],
            dvdx=matrix[0, 1],
            dvdy=-matrix[0, 0],
        )
Beispiel #4
0
def main(args):
    import matplotlib.pyplot as plt
    if ( ( len(args) < 4) or (args == '-h') or (args == '--help') ):
        print("\n### \n### ngmix_fit_testing is a routine which takes a medsfile as\
        its input and outputs shear-calibrated object shapes to a table\n###\n\n  \
        python ngmix_fit_testing.py medsfile start_index end_index outfilename\n \n")
    else:
        pass
    
    medsfile = args[1]
    index_start  = np.int(args[2])
    index_end = np.int(args[3])
    outfilename = args[4]
    meds_info = {'meds_file':args[1]}
    BITfitter = SuperBITNgmixFitter(meds_info) # MEDS file
    bootfit=[]                                 # Array to hold parameters from ExpectMax fit to object
    mcal=[]                                 # Array to hold Metacal fit parameters
    identifying=[]                          # Array to hold identifying information for object
    
    for i in range(index_start, index_end):
        try:
            metacal_fit,gmix_fit = BITfitter._fit_one(i)
            mcal_fit_pars = BITfitter.get_mcalib_shears(i)
            mcal.append(mcal_fit_pars)
            try:
                bootfit.append(gmix_fit.get_result()['pars'])
            except:
                print("failed to append holding values")
                bootfit.append(np.array([-99999, -99999, -99999, -99999, -99999, -99999]))
                  
            identifying.append([BITfitter.medsObj['id'][i],BITfitter.medsObj['ra'][i],BITfitter.medsObj['dec'][i]])
            
            
            # Now for some plotting!
            image = BITfitter.medsObj.get_cutout(i,0)
            jac = BITfitter.medsObj.get_jacobian(i,0)
            jj = ngmix.Jacobian(row=jac['row0'],col=jac['col0'],dvdrow = jac['dvdrow'],
                                    dvdcol=jac['dvdcol'],dudrow=jac['dudrow'],dudcol=jac['dudcol'])
            try:
                gmix_model = gmix_fit.get_convolved_gmix()
                model_image = gmix_model.make_image(image.shape,jacobian=jj)
                fig,(ax1,ax2,ax3) = plt.subplots(nrows=1,ncols=3,figsize=(21,7))
                ax1.imshow(image)
                ax2.imshow(model_image)
                ax3.imshow(image - model_image)
                fig.savefig('diagnostics_plots/diagnostics'+str(i)+'.png')
                plt.close(fig)
            except:
                # EM probably failed
                print("Bad gmix model, no image made :(")

        
            
        except:
            ("object %d failed, skipping..." % i)
             
    make_output_table(outfilename, bootfit, mcal, identifying)
Beispiel #5
0
def make_data(
    rng,
    counts=100.0,
    g1=0.0,
    g2=0.0,
    noise=0.0,
):

    pixel_scale = 0.263

    dims = [32] * 2
    cen = (np.array(dims) - 1) / 2

    dvdrow, dvdcol, dudrow, dudcol = pixel_scale, -0.02, 0.01, pixel_scale
    jacobian = ngmix.Jacobian(
        row=cen[0],
        col=cen[1],
        dvdrow=dvdrow,
        dvdcol=dvdcol,
        dudrow=dudrow,
        dudcol=dudcol,
    )

    pars1 = [
        -3.25 * pixel_scale,
        -3.25 * pixel_scale,
        0.05,
        0.025,
        0.55,
        0.4 * counts,
    ]
    pars2 = [
        3.0 * pixel_scale,
        0.5 * pixel_scale,
        -0.1,
        -0.05,
        0.27,
        0.6 * counts,
    ]

    gm1 = ngmix.GMixModel(pars1, "gauss")
    gm2 = ngmix.GMixModel(pars2, "gauss")

    full_pars = np.hstack([gm1.get_full_pars(), gm2.get_full_pars()])

    gm = ngmix.GMix(pars=full_pars)

    im0 = gm.make_image(dims, jacobian=jacobian)

    im = im0 + rng.normal(size=im0.shape, scale=noise)

    obs = ngmix.Observation(image=im, jacobian=jacobian)

    return obs, gm
Beispiel #6
0
 def _get_jacobians(source_id=None):
     jlist = medsObj.get_jacobian_list(source_id)
     jac = [
         ngmix.Jacobian(row=jj['row0'],
                        col=jj['col0'],
                        dvdrow=jj['dvdrow'],
                        dvdcol=jj['dvdcol'],
                        dudrow=jj['dudrow'],
                        dudcol=jj['dudcol']) for jj in jlist
     ]
     return jac
Beispiel #7
0
    def _get_jacobian(self, cen=None):
        jdict = self.meds.get_jacobian(self.mindex, 0)

        if cen is None:
            cen = [jdict['row0'], jdict['col0']]

        jacobian = ngmix.Jacobian(cen[0], cen[1], jdict['dudrow'],
                                  jdict['dudcol'], jdict['dvdrow'],
                                  jdict['dvdcol'])

        return jacobian
Beispiel #8
0
    def _get_source_observations(self, source_id=None, psf_noise=1e-6):
        source_id = index
        jlist = medsObj.get_jacobian_list(source_id)
        jaclist = [ngmix.Jacobian(row=jj['row0'],col=jj['col0'],dvdrow = jj['dvdrow'],dvdcol=jj['dvdcol'],\
                                      dudrow=jj['dudrow'],dudcol=jj['dudcol']) for jj in jlist]
        psf_cutouts = medsObj.get_cutout_list(source_id, type='psf')
        weight_cutouts = medsObj.get_cutout_list(source_id, type='weight')
        image_cutouts = medsObj.get_cutout_list(source_id, type='image')

        image_obslist = ngmix.observation.ObsList()

        psf = make_psf()
        psf_pixel_scale = 0.01
        gpsf_cutout = psf.drawImage(scale=psf_pixel_scale, nx=256, ny=256)

        for i in range(len(image_cutouts)):

            # Apparently it likes to add noise to the psf.
            #this_psf = psf_cutouts[i] + psf_noise*np.random.randn(psf_cutouts[i].shape[0],psf_cutouts[i].shape[1])
            this_psf = gpsf_cutout.array
            this_psf_weight = np.zeros_like(this_psf) + 1. / psf_noise**2

            this_image = image_cutouts[i]

            sky_sigma = (0.057 *
                         300)**2  # exp_time = 300, sky_var = 0.0957 ADU/pix/s
            this_weight = np.zeros_like(this_image) + 1. / sky_sigma
            #this_weight = weight_cutouts[i]

            # for some reason, PSF obs doesn't appear to like the Jacobian jacobian...
            jj_psf = ngmix.jacobian.DiagonalJacobian(scale=psf_pixel_scale,
                                                     x=this_psf.shape[0] / 2,
                                                     y=this_psf.shape[1] / 2)
            #jj_im = ngmix.jacobian.DiagonalJacobian(scale=0.206,x=this_image.shape[0]/2,y=this_image.shape[1]/2)
            jj = jaclist[i]

            psfObs = ngmix.observation.Observation(this_psf,
                                                   weight=this_psf_weight,
                                                   jacobian=jj_psf)
            imageObs = ngmix.observation.Observation(this_image,
                                                     weight=this_weight,
                                                     jacobian=jj,
                                                     psf=psfObs)
            #imageObs.psf_nopix = imageObs.psf

            image_obslist.append(imageObs)

        return image_obslist
Beispiel #9
0
    def get_jacobian(
        self,
        ra,
        dec,
        cutout_row,
        cutout_col,
    ):
        """
        get a jacobian with new elements as well as any
        required shift
        """
        gs_jac, _, _ = self._get_gs_jacobian_and_rowcol(ra, dec)

        return ngmix.Jacobian(
            row=cutout_row,
            col=cutout_col,
            dudrow=gs_jac.dudy,
            dudcol=gs_jac.dudx,
            dvdrow=gs_jac.dvdy,
            dvdcol=gs_jac.dvdx,
        )
Beispiel #10
0
def get_jac(wcs, cenx, ceny):
    """
    get jacobian at the coadd image center, and make
    an ngmix jacobian with center specified (this is not the
    position used to evaluate the jacobian)
    """
    import galsim

    crpix = wcs.crpix
    galsim_pos = galsim.PositionD(x=crpix[0], y=crpix[1])

    galsim_jac = wcs.jacobian(image_pos=galsim_pos)

    return ngmix.Jacobian(
        x=cenx,
        y=ceny,
        dudx=galsim_jac.dudx,
        dudy=galsim_jac.dudy,
        dvdx=galsim_jac.dvdx,
        dvdy=galsim_jac.dvdy,
    )
Beispiel #11
0
def make_data(
    rng,
    counts=100.0,
    fwhm=1.2,
    g1=0.0,
    g2=0.0,
    noise=0.0,
):

    pixel_scale = 0.263

    T = ngmix.moments.fwhm_to_T(fwhm)
    sigma = np.sqrt(T / 2)
    dim = int(2 * 5 * sigma / pixel_scale)
    dims = [dim] * 2
    cen = [dims[0] / 2.0, dims[1] / 2.0]

    dvdrow, dvdcol, dudrow, dudcol = pixel_scale, -0.02, 0.01, pixel_scale
    jacobian = ngmix.Jacobian(
        row=cen[0],
        col=cen[1],
        dvdrow=dvdrow,
        dvdcol=dvdcol,
        dudrow=dudrow,
        dudcol=dudcol,
    )

    pars = [0.0, 0.0, g1, g2, T, counts]
    gm = ngmix.GMixModel(pars, "gauss")

    im0 = gm.make_image(dims, jacobian=jacobian)

    im = im0 + rng.normal(size=im0.shape, scale=noise)

    obs = ngmix.Observation(image=im, jacobian=jacobian)

    return obs, gm
Beispiel #12
0
    def _get_jacobian(self, band, midrow, midcol):
        """
        get the ngmix jacobian at the specified location

        "center" of the jacobian will be 0, 0 which is most useful when the
        objects can be anywhere in the image
        """

        wcs = self.wcs_list[band]

        jdata = wcs.get_jacobian(
            x=midcol+self._coord_offset,
            y=midrow+self._coord_offset,
        )

        jacob = ngmix.Jacobian(
            row=0,
            col=0,
            dudcol=jdata[0],
            dudrow=jdata[1],
            dvdcol=jdata[2],
            dvdrow=jdata[3],
        )
        return jacob
Beispiel #13
0
def make_data(
    rng,
    counts=100.0,
    g1=0.0,
    g2=0.0,
    noise=0.0,
):

    pixel_scale = 0.263
    psf_fwhm = 0.9

    dims = [32]*2
    cen = (np.array(dims)-1)/2

    dvdrow, dvdcol, dudrow, dudcol = pixel_scale, -0.02, 0.01, pixel_scale
    jacobian = ngmix.Jacobian(
        row=cen[0],
        col=cen[1],
        dvdrow=dvdrow,
        dvdcol=dvdcol,
        dudrow=dudrow,
        dudcol=dudcol,
    )

    T1 = 0.2
    T2 = 0.05

    pars1 = [
        -3.25*pixel_scale, -3.25*pixel_scale, 0.1, 0.05, T1, 0.4*counts,
    ]
    pars2 = [
        3.0*pixel_scale, 0.5*pixel_scale, -0.2, -0.1, T2, 0.6*counts,
    ]

    gm1 = ngmix.GMixModel(pars1, "gauss")
    gm2 = ngmix.GMixModel(pars2, "gauss")

    full_pars = np.hstack([gm1.get_full_pars(), gm2.get_full_pars()])

    gm0 = ngmix.GMix(pars=full_pars)

    Tpsf = ngmix.moments.fwhm_to_T(psf_fwhm)
    psf_pars = [0.0, 0.0, 0.01, -0.005, Tpsf, 1.0]
    psf = ngmix.GMixModel(pars=psf_pars, model="turb")
    psf_im = psf.make_image(dims, jacobian=jacobian)

    gm = gm0.convolve(psf)

    im0 = gm.make_image(dims, jacobian=jacobian)

    im = im0 + rng.normal(size=im0.shape, scale=noise)

    # to fit with psfs, the psf observation must be set and it
    # must have a gaussian mixture set
    psf_obs = ngmix.Observation(
        image=psf_im, jacobian=jacobian, gmix=psf,
    )

    obs = ngmix.Observation(image=im, jacobian=jacobian, psf=psf_obs)

    return obs, gm0
Beispiel #14
0
def test_ml_fitting_galsim(wcs_g1, wcs_g2, model, use_prior):

    rng = np.random.RandomState(seed=2312)
    scale = 0.263
    prior = get_prior_galsimfit(model=model, rng=rng, scale=scale)

    psf_fwhm = 0.9
    psf_image_size = 33
    image_size = 51

    noise = 0.001

    hlr = 0.1
    flux = 400
    gs_wcs = galsim.ShearWCS(
        scale,
        galsim.Shear(g1=wcs_g1, g2=wcs_g2),
    ).jacobian()

    psf_im = galsim.Gaussian(fwhm=psf_fwhm).drawImage(
        nx=psf_image_size,
        ny=psf_image_size,
        wcs=gs_wcs,
    ).array

    psf_cen = (psf_image_size - 1.0) / 2.0
    psf_jac = ngmix.Jacobian(
        y=psf_cen,
        x=psf_cen,
        dudx=gs_wcs.dudx,
        dudy=gs_wcs.dudy,
        dvdx=gs_wcs.dvdx,
        dvdy=gs_wcs.dvdy,
    )

    psf_obs = ngmix.Observation(
        image=psf_im,
        jacobian=psf_jac,
    )

    guess = prior.sample()
    g1arr = []
    g2arr = []
    farr = []
    xarr = []
    yarr = []

    if use_prior:
        send_prior = prior
    else:
        send_prior = None

    fitter = ngmix.fitting.GalsimFitter(model=model, prior=send_prior)

    if model == 'exp' and use_prior:
        ntrial = 50
    else:
        ntrial = 1

    for _ in range(ntrial):
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        g1_true, g2_true = prior.g_prior.sample2d()
        gal = galsim.Exponential(half_light_radius=hlr, ).shear(
            g1=g1_true, g2=g2_true).withFlux(flux, )

        obj = galsim.Convolve([gal, galsim.Gaussian(fwhm=psf_fwhm)])

        im = obj.shift(dx=shift[0], dy=shift[1]).drawImage(
            nx=image_size,
            ny=image_size,
            wcs=gs_wcs,
            dtype=np.float64,
        ).array
        wgt = np.ones_like(im) / noise**2

        cen = (np.array(im.shape) - 1) / 2
        jac = ngmix.Jacobian(
            y=cen[0] + xy.y,
            x=cen[1] + xy.x,
            dudx=gs_wcs.dudx,
            dudy=gs_wcs.dudy,
            dvdx=gs_wcs.dvdx,
            dvdy=gs_wcs.dvdy,
        )

        _im = im + (rng.normal(size=im.shape) * noise)
        obs = ngmix.Observation(
            image=_im,
            weight=wgt,
            jacobian=jac,
            psf=psf_obs,
        )

        guess[0] = rng.uniform(low=-0.1, high=0.1)
        guess[1] = rng.uniform(low=-0.1, high=0.1)
        guess[2] = rng.uniform(low=-0.1, high=0.1)
        guess[3] = rng.uniform(low=-0.1, high=0.1)
        guess[4] = hlr * rng.uniform(low=0.9, high=1.1)
        guess[5] = flux * rng.uniform(low=0.9, high=1.1)

        res = fitter.go(obs=obs,
                        guess=guess + rng.normal(size=guess.size) * 0.01)
        if res['flags'] == 0:
            _g1, _g2, _ = res['g'][0], res['g'][1], res['pars'][4]
            g1arr.append(_g1 - g1_true)
            g2arr.append(_g2 - g2_true)
            farr.append(res['pars'][5])
            xarr.append(res['pars'][1])
            yarr.append(res['pars'][0])

    if model == 'exp' and use_prior:
        g1diff = np.mean(g1arr)
        g2diff = np.mean(g2arr)

        print('g1vals')
        print(g1arr)
        print(g1diff)
        print('g2vals')
        print(g2arr)
        print(g2diff)

        gtol = 1.0e-5

        assert np.abs(g1diff) < gtol
        assert np.abs(g2diff) < gtol

        xerr = np.std(xarr) / np.sqrt(len(xarr))
        assert np.abs(np.mean(xarr)) < xerr * 5
        yerr = np.std(yarr) / np.sqrt(len(yarr))
        assert np.abs(np.mean(yarr)) < yerr * 5
Beispiel #15
0
##  (this is basically ngmix_fit_superbit, copied over for ipython sesh)
##
########################################################################

medsObj=meds.MEDS('/Users/jemcclea/Research/SuperBIT/superbit-ngmix/scripts/cluster3-debug/opticsSigmaJitter_noDilate/cluster3_debug_2hr.meds')
index=12010
psf = medsObj.get_cutout(index,20,type='psf')
im = medsObj.get_cutout(index,20,type='image') 
weight = medsObj.get_cutout(index,20,type='weight')
plt.figure()
plt.imshow(im)
plt.figure()
plt.imshow(psf)

jj = medsObj.get_jacobian(index,0)
jac = ngmix.Jacobian(row=jj['row0'],col=jj['col0'],dvdrow = jj['dvdrow'],dvdcol=jj['dvdcol'],dudrow=jj['dudrow'],dudcol=jj['dudcol'])

psf_noise = 1e-6
this_psf = psf + 1e-6*np.random.randn(psf.shape[0],psf.shape[1])
this_psf_weight = np.zeros_like(this_psf) + 1./psf_noise**2

psfObs = ngmix.observation.Observation(this_psf,weight = this_psf_weight, jacobian = jac) # can I add a kw for no_pix here???
imageObs = ngmix.observation.Observation(image=im,weight = weight, jacobian = jac, psf = psfObs)

# This contains all 1p/1m 2p/2m noshear information, like im, jac,
# and contains spaces where gmix information will be stored
metaobs = ngmix.metacal.get_all_metacal(imageObs)
mcb=ngmix.bootstrap.MaxMetacalBootstrapper(imageObs)

# This will return sigma, etc. of psf of medsObj
# Then multiply by pixscale & scale to get FWHM
Beispiel #16
0
def _run_moments(*, n_sims, rng, dudx, dudy, dvdx, dvdy):
    """Run metacal on an image composed of stamps w/ constant noise.

    Parameters
    ----------
    n_sims : int
        The number of objects to run.
    rng : np.random.RandomState
        An RNG to use.
    dudx : float
        The du/dx Jacobian component.
    dudy : float
        The du/dy Jacobian component.
    dydx : float
        The dv/dx Jacobian component.
    dvdy : float
        The dv/dy Jacobian component.

    Returns
    -------
    result : dict
        A dictionary with each of the metacal catalogs.
    """

    method = 'no_pixel'

    stamp_size = 33
    psf_stamp_size = 33

    cen = (stamp_size - 1) / 2
    psf_cen = (psf_stamp_size - 1) / 2

    s2n = 1e16
    flux = 1e6

    galsim_jac = galsim.JacobianWCS(dudx=dudx, dudy=dudy, dvdx=dvdx, dvdy=dvdy)

    gal = galsim.Exponential(half_light_radius=0.5).withFlux(flux)
    psf = galsim.Gaussian(fwhm=0.9).withFlux(1)
    obj = galsim.Convolve(gal, psf)
    obj_im = obj.drawImage(nx=111, ny=111).array
    noise = np.sqrt(np.sum(obj_im**2)) / s2n

    data = []
    for ind in tqdm.trange(n_sims):
        ################################
        # make the obs

        # psf
        psf_im = psf.drawImage(nx=psf_stamp_size,
                               ny=psf_stamp_size,
                               wcs=galsim_jac,
                               method=method).array
        psf_noise = np.sqrt(np.sum(psf_im**2)) / 10000
        wgt = np.ones_like(psf_im) / psf_noise**2
        psf_im += (rng.normal(size=psf_im.shape) * psf_noise)
        psf_jac = ngmix.Jacobian(x=psf_cen,
                                 y=psf_cen,
                                 dudx=dudx,
                                 dudy=dudy,
                                 dvdx=dvdx,
                                 dvdy=dvdy)
        psf_obs = ngmix.Observation(image=psf_im, weight=wgt, jacobian=psf_jac)

        # now render object
        scale = psf_jac.scale
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        _obj = obj.shift(dx=shift[0], dy=shift[1])
        xy = galsim_jac.toImage(galsim.PositionD(shift))
        im = _obj.drawImage(nx=stamp_size,
                            ny=stamp_size,
                            wcs=galsim_jac,
                            method=method).array
        jac = ngmix.Jacobian(x=cen + xy.x,
                             y=cen + xy.y,
                             dudx=dudx,
                             dudy=dudy,
                             dvdx=dvdx,
                             dvdy=dvdy)
        wgt = np.ones_like(im) / noise**2
        nse = rng.normal(size=im.shape) * noise
        im += (rng.normal(size=im.shape) * noise)
        obs = ngmix.Observation(image=im,
                                weight=wgt,
                                noise=nse,
                                bmask=np.zeros_like(im, dtype=np.int32),
                                ormask=np.zeros_like(im, dtype=np.int32),
                                jacobian=jac,
                                psf=psf_obs)

        # build the mbobs
        mbobs = ngmix.MultiBandObsList()
        obslist = ngmix.ObsList()
        obslist.append(obs)
        mbobs.append(obslist)

        mbobs.meta['id'] = ind + 1
        # these settings do not matter that much I think
        mbobs[0].meta['Tsky'] = 1
        mbobs[0].meta['magzp_ref'] = 26.5
        mbobs[0][0].meta['orig_col'] = ind + 1
        mbobs[0][0].meta['orig_row'] = ind + 1

        ################################
        # run the fitters
        try:
            res = _run_moments_fitter(mbobs, rng)
        except Exception as e:
            print(e)
            res = None

        if res is not None:
            data.append(res)

    if len(data) > 0:
        res = data
    else:
        res = None

    return res
Beispiel #17
0
def run_metacal(n_sims, stamp_size, psf_stamp_size, rng, jacobian_dict,
                gauss_psf):
    """Run metacal on an image composed of stamps w/ constant noise.

    Parameters
    ----------
    n_sims : int
        The number of objects to run.
    stamp_size : int
        The size of each stamp.
    psf_stamp_size : int
        The size of each PSF stamp.
    rng : np.random.RandomState
        An RNG to use.
    jacobian_dict : dict
        A dictonary with the components of the image jacobian.
    gauss_psf : bool
        If True, test with a Gaussian PSF.

    Returns
    -------
    result : dict
        A dictionary with each of the metacal catalogs.
    """

    method = 'auto'

    cen = (stamp_size - 1) / 2
    psf_cen = (psf_stamp_size - 1) / 2
    noise = 1
    flux = 64000
    gal = galsim.Exponential(half_light_radius=0.5).withFlux(flux).shear(
        g1=0.02, g2=0.0)

    if not gauss_psf:
        piff_cats = np.loadtxt('piff_cat.txt', dtype=str)
        piff_file = rng.choice(piff_cats)
        psf_model = DES_Piff(piff_file)
        print('piff file:', piff_file)

    if jacobian_dict['dudy'] != 0 or jacobian_dict['dvdx'] != 0:
        print('jacobian:', jacobian_dict)

    galsim_jac = galsim.JacobianWCS(dudx=jacobian_dict['dudx'],
                                    dudy=jacobian_dict['dudy'],
                                    dvdx=jacobian_dict['dvdx'],
                                    dvdy=jacobian_dict['dvdy'])

    data = []
    for ind in tqdm.trange(n_sims):
        ################################
        # make the obs

        # first get PSF
        x = rng.uniform(low=1, high=2048)
        y = rng.uniform(low=1, high=2048)

        if gauss_psf:
            psf = galsim.Gaussian(fwhm=0.9).withFlux(1)
        else:
            psf = psf_model.getPSF(galsim.PositionD(x=x, y=y), galsim_jac)

        psf_im = psf.drawImage(nx=psf_stamp_size,
                               ny=psf_stamp_size,
                               wcs=galsim_jac,
                               method=method).array

        psf_noise = np.sqrt(np.sum(psf_im**2)) / 500
        wgt = 0.0 * psf_im + 1.0 / psf_noise**2
        psf_jac = ngmix.Jacobian(x=psf_cen,
                                 y=psf_cen,
                                 dudx=jacobian_dict['dudx'],
                                 dudy=jacobian_dict['dudy'],
                                 dvdx=jacobian_dict['dvdx'],
                                 dvdy=jacobian_dict['dvdy'])
        psf_obs = ngmix.Observation(image=psf_im, weight=wgt, jacobian=psf_jac)

        # now render object
        obj = galsim.Convolve(gal, psf)
        offset = rng.uniform(low=-0.5, high=0.5, size=2)
        im = obj.drawImage(nx=stamp_size,
                           ny=stamp_size,
                           wcs=galsim_jac,
                           offset=offset,
                           method=method).array
        jac = ngmix.Jacobian(x=cen + offset[0],
                             y=cen + offset[1],
                             dudx=jacobian_dict['dudx'],
                             dudy=jacobian_dict['dudy'],
                             dvdx=jacobian_dict['dvdx'],
                             dvdy=jacobian_dict['dvdy'])
        wgt = np.ones_like(im) / noise**2
        nse = rng.normal(size=im.shape) * noise
        im += (rng.normal(size=im.shape) * noise)
        obs = ngmix.Observation(image=im,
                                weight=wgt,
                                noise=nse,
                                bmask=np.zeros_like(im, dtype=np.int32),
                                ormask=np.zeros_like(im, dtype=np.int32),
                                jacobian=jac,
                                psf=psf_obs)

        # build the mbobs
        mbobs = ngmix.MultiBandObsList()
        obslist = ngmix.ObsList()
        obslist.append(obs)
        mbobs.append(obslist)

        mbobs.meta['id'] = ind + 1
        # these settings do not matter that much I think
        mbobs[0].meta['Tsky'] = 1
        mbobs[0].meta['magzp_ref'] = 26.5
        mbobs[0][0].meta['orig_col'] = ind + 1
        mbobs[0][0].meta['orig_row'] = ind + 1

        ################################
        # run the fitter
        mcal = MetacalFitter(CONFIG, 1, rng)
        mcal.go([mbobs])
        res = mcal.result

        if res is not None:
            data.append(res)

    if len(data) > 0:
        res = eu.numpy_util.combine_arrlist(data)
        result = _result_to_dict(res)
    else:
        result = None

    return result
Beispiel #18
0
def ngmix_fit(im, wt, fwhm, x, y, logger, psfflag):     
    flag = 0
    dx, dy, g1, g2, flux = 0., 0., 0., 0., 0.
    T_guess = (fwhm / 2.35482)**2 * 2.
    T = T_guess
    #print('fwhm = %s, T_guess = %s'%(fwhm, T_guess))
    if psfflag==0:
        
            #hsm_dx,hsm_dy,hsm_g1,hsm_g2,hsm_T,hsm_flux,hsm_flag = hsm(im, None, logger)
            #logger.info('hsm: %s, %s, %s, %s, %s, %s, %s',hsm_dx,hsm_dy,hsm_g1,hsm_g2,hsm_T,hsm_flux,hsm_flag)
            #if hsm_flag != 0:
                #print('hsm: ',g1,g2,T,flux,hsm_flag)
                #print('Bad hsm measurement.  Reverting to g=(0,0) and T=T_guess = %s'%(T_guess))
                #T = T_guess
            #elif np.abs(np.log(T/T_guess)) > 0.5:
                #print('hsm: ',g1,g2,T,flux,hsm_flag)
                #print('T = %s is not near T_guess = %s.  Reverting to T_guess'%(T,T_guess))
            #T = T_guess
        #print("before wcs.local")
        #print(im.wcs.local)
        #print("before im.center")
        #print(im.true_center)
        #print("this line ", im.wcs.local(im.true_center))
        wcs = im.wcs.local(im.true_center)
        #print(wcs)
        try:
            #print("going to make prior", T,wcs.minLinearScale())
            prior = make_ngmix_prior(T, wcs.minLinearScale())
            #print("prior", prior)
        
            if galsim.__version__ >= '1.5.1':
                cen = im.true_center - im.origin
            else:
                cen = im.trueCenter() - im.origin()
            jac = ngmix.Jacobian(wcs=wcs, x=cen.x + x - int(x+0.5), y=cen.y + y - int(y+0.5))
            if wt is None:
                obs = ngmix.Observation(image=im.array, jacobian=jac)
            else:
                obs = ngmix.Observation(image=im.array, weight=wt.array, jacobian=jac)

            lm_pars = {'maxfev':4000}
            runner=ngmix.bootstrap.PSFRunner(obs, 'gauss', T, lm_pars, prior=prior)
            runner.go(ntry=3)

            ngmix_flag = runner.fitter.get_result()['flags']
            gmix = runner.fitter.get_gmix()
        except Exception as e:
            logger.info(e)
            logger.info(' *** Bad measurement (caught exception).  Mask this one.')
            print(' *** Bad measurement (caught exception).  Mask this one.')
            flag |= BAD_MEASUREMENT
            return dx,dy,g1,g2,T,flux,flag

        if ngmix_flag != 0:
            logger.info(' *** Bad measurement (ngmix flag = %d).  Mask this one.',ngmix_flag)
            flag |= BAD_MEASUREMENT
            print(' *** Bad measurement (ngmix flag = %d).  Mask this one.',ngmix_flag)

        dx, dy = gmix.get_cen()
        if dx**2 + dy**2 > MAX_CENTROID_SHIFT**2:
            logger.info(' *** Centroid shifted by %f,%f in ngmix.  Mask this one.',dx,dy)
            flag |= CENTROID_SHIFT
            print(' *** Centroid shifted by %f,%f in ngmix.  Mask this one.',dx,dy)

        g1, g2, T = gmix.get_g1g2T()
        if abs(g1) > 0.5 or abs(g2) > 0.5:
            logger.info(' *** Bad shape measurement (%f,%f).  Mask this one.',g1,g2)
            flag |= BAD_MEASUREMENT

        flux = gmix.get_flux() / wcs.pixelArea()  # flux is in ADU.  Should ~ match sum of pixels
    #logger.info('ngmix: %s %s %s %s %s %s %s',dx,dy,g1,g2,T,flux,flag)
    return dx, dy, g1, g2, T, flux, flag
Beispiel #19
0
    def get_obs(self, iobj, icutout, weight_type='weight'):
        """
        get an ngmix Observation

        parameters
        ----------
        iobj:
            Index of the object
        icutout:
            Index of the cutout for this object.
        weight_type: string, optional
            Weight type. can be one of
                'weight': the actual weight map
                'uberseg': uberseg modified weight map
            Default is 'weight'

        returns
        -------
        an ngmix Observation
        """

        import ngmix
        im = self.get_cutout(iobj, icutout, type='image')
        bmask = self.get_cutout(iobj, icutout, type='bmask')
        jd = self.get_jacobian(iobj, icutout)

        if weight_type == 'uberseg':
            wt = self.get_uberseg(iobj, icutout)
        elif weight_type == 'cweight':
            wt = self.get_cweight_cutout(iobj, icutout, restrict_to_seg=True)
        elif weight_type == 'weight':
            wt = self.get_cutout(iobj, icutout, type='weight')
        elif weight_type == 'cseg':
            wt = self.get_cseg_weight(iobj, icutout)
        elif weight_type == 'cseg-canonical':
            wt = self.get_cseg_weight(iobj, icutout, use_canonical_cen=True)
        else:
            raise ValueError("bad weight type '%s'" % weight_type)

        jacobian = ngmix.Jacobian(
            row=jd['row0'],
            col=jd['col0'],
            dudrow=jd['dudrow'],
            dudcol=jd['dudcol'],
            dvdrow=jd['dvdrow'],
            dvdcol=jd['dvdcol'],
        )
        c = self._cat

        x2 = c['x2'][iobj]
        y2 = c['y2'][iobj]
        T = (x2 + y2)

        if 'flux_auto' in c.dtype.names:
            flux = c['flux_auto'][iobj]
        else:
            flux = -9999.0

        meta = dict(
            id=c['id'][iobj],
            T=T,
            flux=flux,
            index=iobj,
            number=c['number'][iobj],
            icut=icutout,
            cutout_index=icutout,
            file_id=c['file_id'][iobj, icutout],
            orig_row=c['orig_row'][iobj, icutout],
            orig_col=c['orig_col'][iobj, icutout],
            orig_start_row=c['orig_start_row'][iobj, icutout],
            orig_start_col=c['orig_start_col'][iobj, icutout],
            orig_end_row=c['orig_end_row'][iobj, icutout],
            orig_end_col=c['orig_end_col'][iobj, icutout],
        )
        obs = ngmix.Observation(
            im,
            weight=wt,
            bmask=bmask,
            meta=meta,
            jacobian=jacobian,
        )

        return obs
Beispiel #20
0
def main(args):
    import matplotlib.pyplot as plt
    if ( ( len(args) < 4) or (args == '-h') or (args == '--help') ):
        print("\n### \n### ngmix_fit_testing is a routine which takes a medsfile as its input \
        and outputs shear-calibrated object shapes to a table\n###\n\n  python ngmix_fit_testing.py \
        medsfile start_index end_index outfilename\n \n")
    else:
        pass
    
    medsfile = args[1]
    index_start  = np.int(args[2])
    index_end = np.int(args[3])
    outfilename = args[4]
    meds_info = {'meds_file':args[1]}
    BITfitter = SuperBITNgmixFitter(meds_info) # MEDS file
    bootfit=[]                              # Array to hold parameters from Bootstrapper fit to object
    mcal=[]                                 # Array to hold Metacalibration fit parameters
    identifying=[]                          # Array to hold identifying information for object
    
    for i in range(index_start, index_end):

        identifying.append([BITfitter.medsObj['id'][i],BITfitter.medsObj['ra'][i],BITfitter.medsObj['dec'][i]])
        try:
            
            # metacal_fit is shear calibrated, gmix_fit is just Bootstrapper 
            metcal_fit = BITfitter._fit_one(i)           
            mcal_fit_pars = BITfitter.get_mcalib_shears(i)

            mcal.append(mcal_fit_pars)

            # Now for some plotting!
            image_cutout = BITfitter.imc    
            jdict = BITfitter.medsObj.get_jacobian_list(i)[0]
            jj = ngmix.Jacobian(row=jdict['row0'],col=jdict['col0'],dvdrow = jdict['dvdrow'],dvdcol=jdict['dvdcol'],dudrow=jdict['dudrow'],dudcol=jdict['dudcol'])
            #jj_im = ngmix.jacobian.DiagonalJacobian(scale=0.206,x=(image_cutout.shape[0])/2,y=(image_cutout.shape[1])/2)
            
            try:
                gmix = metcal_fit.get_convolved_gmix()
                model_image = gmix.make_image(image_cutout.shape,jacobian=jj)
                #pdb.set_trace()
                fig,(ax1,ax2,ax3) = plt.subplots(nrows=1,ncols=3,figsize=(21,7))
                ax1.imshow(image_cutout)#,vmin=0,vmax=(image_cutout.max()*.90))
                ax2.imshow(model_image)#,vmin=0,vmax=(image_cutout.max()*.90))
                x,y=gmix.get_cen()
                ax2.plot((x+image_cutout.shape[0]/2),(y+image_cutout.shape[1]/2),'Xr',markersize=10)
                ax3.imshow(image_cutout - model_image)
                fig.savefig('diagnostics_plots/diagnostics-ngmix3-'+str(i)+'.png')
                plt.close(fig)
                
            except:
                # EM probably failed
                print("Bad gmix model, no image made :(")
                

        
        except:
            pdb.set_trace()
            print("object %d failed, skipping..." % i)
            
            
    make_output_table(outfilename, bootfit, mcal, identifying)
Beispiel #21
0
def test_ml_fitting_galsim_spergel_smoke():

    rng = np.random.RandomState(seed=2312)
    scale = 0.263
    prior = get_prior_galsimfit(model='spergel', rng=rng, scale=scale)

    psf_fwhm = 0.9
    psf_image_size = 33
    image_size = 51

    noise = 0.001

    hlr = 0.1
    flux = 400

    gs_wcs = galsim.ShearWCS(
        scale,
        galsim.Shear(g1=0.01, g2=-0.01),
    ).jacobian()

    psf_im = galsim.Gaussian(fwhm=psf_fwhm).drawImage(
        nx=psf_image_size,
        ny=psf_image_size,
        wcs=gs_wcs,
    ).array

    psf_cen = (psf_image_size - 1.0) / 2.0
    psf_jac = ngmix.Jacobian(
        y=psf_cen,
        x=psf_cen,
        dudx=gs_wcs.dudx,
        dudy=gs_wcs.dudy,
        dvdx=gs_wcs.dvdx,
        dvdy=gs_wcs.dvdy,
    )

    psf_obs = ngmix.Observation(
        image=psf_im,
        jacobian=psf_jac,
    )

    guess = prior.sample()

    fitter = ngmix.fitting.GalsimSpergelFitter(prior=prior)

    shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
    xy = gs_wcs.toImage(galsim.PositionD(shift))

    g1_true, g2_true = prior.g_prior.sample2d()
    gal = galsim.Exponential(half_light_radius=hlr, ).shear(
        g1=g1_true, g2=g2_true).withFlux(flux, )

    obj = galsim.Convolve([gal, galsim.Gaussian(fwhm=psf_fwhm)])

    im = obj.shift(dx=shift[0], dy=shift[1]).drawImage(
        nx=image_size,
        ny=image_size,
        wcs=gs_wcs,
        dtype=np.float64,
    ).array
    wgt = np.ones_like(im) / noise**2

    cen = (np.array(im.shape) - 1) / 2
    jac = ngmix.Jacobian(
        y=cen[0] + xy.y,
        x=cen[1] + xy.x,
        dudx=gs_wcs.dudx,
        dudy=gs_wcs.dudy,
        dvdx=gs_wcs.dvdx,
        dvdy=gs_wcs.dvdy,
    )

    _im = im + (rng.normal(size=im.shape) * noise)
    obs = ngmix.Observation(
        image=_im,
        weight=wgt,
        jacobian=jac,
        psf=psf_obs,
    )

    guess[0] = rng.uniform(low=-0.1, high=0.1)
    guess[1] = rng.uniform(low=-0.1, high=0.1)
    guess[2] = rng.uniform(low=-0.1, high=0.1)
    guess[3] = rng.uniform(low=-0.1, high=0.1)
    guess[4] = hlr * rng.uniform(low=0.9, high=1.1)
    guess[5] = rng.uniform(low=0, high=2)
    guess[6] = flux * rng.uniform(low=0.9, high=1.1)

    res = fitter.go(obs=obs, guess=guess + rng.normal(size=guess.size) * 0.01)
    assert res['flags'] == 0