예제 #1
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
예제 #2
0
def test_gmix_errors():
    with pytest.raises(ValueError):
        ngmix.GMix()

    with pytest.raises(ValueError):
        ngmix.GMixModel([1] * 6, "blah")

    with pytest.raises(ValueError):
        ngmix.GMix(pars=[1] * 10)

    with pytest.raises(ValueError):
        ngmix.GMix(ngauss=-1)

    with pytest.raises(ValueError):
        ngmix.GMixModel([1] * 10, "gauss")

    pars = [0, 0, 0, 0, 10, 1]
    gm = ngmix.GMixModel(pars, "gauss")
    with pytest.raises(ValueError):
        gm.get_sheared(0.1)

    with pytest.raises(TypeError):
        gm.convolve(3)

    with pytest.raises(ValueError):
        gm.make_image([1])

    obs = ngmix.Observation(np.zeros((10, 10)))
    with pytest.raises(ValueError):
        gm.fill_fdiff(obs, np.zeros((10, 10)), start=100000)

    with pytest.raises(TypeError):
        gm.make_galsim_object(gsparams=3)

    with pytest.raises(ValueError):
        gm = ngmix.GMixCoellip([1] * 3)

    with pytest.raises(ValueError):
        ngmix.gmix.get_model_name(-1)

    with pytest.raises(ValueError):
        ngmix.gmix.get_model_num('asdfsa')

    with pytest.raises(ValueError):
        ngmix.gmix.get_model_ngauss('asdfsa')

    with pytest.raises(ValueError):
        ngmix.gmix.get_model_npars('asdfsa')

    with pytest.raises(ValueError):
        tgm = gm.copy()
        tgm._model_name = 'sdfasd'
        tgm._set_fill_func()
예제 #3
0
def get_weight_gmix(*, T, g1, g2):
    """
    get the weight gaussian, normalized such that
    the value at the origin is 1

    Parameters
    ----------
    T: float
        The T value for the gaussian
    g1: float
        The g1 value for the gaussian
    g2: float
        The g2 value for the gaussian

    Returns
    -------
    ngmix.GMixModel of type gauss
    """
    pars = [0.0, 0.0, g1, g2, T, 1.0]

    wt = ngmix.GMixModel(pars, "gauss")

    wt.set_norms()
    norm = wt.get_data()['norm'][0]
    wt.set_flux(1.0/norm)
    wt.set_norms()

    return wt
예제 #4
0
 def _set_psf(self):
     """
     set the psf as a gaussian
     """
     import ngmix
     T = ngmix.moments.fwhm_to_T(self.psf_fwhm)
     pars = [0.0, 0.0, 0.0, 0.0, T, 1.0]
     self.psf = ngmix.GMixModel(pars, 'gauss')
예제 #5
0
def test_gmix_s2n_smoke():
    pars = [0, 0, 0, 0, 5, 1]
    gm = ngmix.GMixModel(pars, "gauss")
    im = gm.make_image([10, 10])

    obs = ngmix.Observation(im)

    gm.get_model_s2n(obs)
예제 #6
0
파일: gaussap.py 프로젝트: NiallMac/ngmixer
    def _get_gauss_model(self, fwhm):
        sigma = ngmix.moments.fwhm_to_sigma(fwhm)
        T = 2 * sigma**2

        pars = [
            0.0,
            0.0,
            0.0,
            0.0,
            T,
            1.0,
        ]
        return ngmix.GMixModel(pars, "gauss")
예제 #7
0
파일: measure.py 프로젝트: esheldon/deconv
    def _get_weight_object(self, gs_kimage):
        import ngmix

        dk = gs_kimage.scale
        dims = gs_kimage.array.shape
        cen = util.get_canonical_kcenter(dims)

        sigma = self.sigma_weight
        sigmak = 1.0 / sigma

        # the k space image does not have unit pixel size
        sigmak *= (1.0 / dk)

        Tk = 2.0 * sigmak**2
        pars = [0.0, 0.0, 0.0, 0.0, Tk, 1.0]
        kwt = ngmix.GMixModel(pars, 'gauss')

        return kwt, cen, dims
예제 #8
0
파일: gaussmom.py 프로젝트: aguinot/ngmix
    def _set_mompars(self):
        T = ngmix.moments.fwhm_to_T(self.fwhm)

        # the weight is always centered at 0, 0 or the
        # center of the coordinate system as defined
        # by the jacobian

        weight = ngmix.GMixModel(
            [0.0, 0.0, 0.0, 0.0, T, 1.0],
            'gauss',
        )

        # make the max of the weight 1.0 to get better
        # fluxes

        weight.set_norms()
        norm = weight.get_data()['norm'][0]
        weight.set_flux(1.0 / norm)

        self.weight = weight
예제 #9
0
def test_extra():
    """
    some extra stuff to get coverage
    """
    pars = [0, 0, 0, 0, 10, 1]
    gm = ngmix.GMixModel(pars, "gauss")

    fwhm = 0.9
    T = ngmix.moments.fwhm_to_T(fwhm)
    sigma = ngmix.moments.fwhm_to_sigma(fwhm)

    apflux = gm.get_gaussap_flux(fwhm=fwhm)
    np.allclose(apflux, gm.get_gaussap_flux(T=T))
    np.allclose(apflux, gm.get_gaussap_flux(sigma=sigma))

    g = ngmix.gmix.GMixCM(0.5, 1.0, pars)
    g.copy()

    npars = ngmix.gmix.get_coellip_npars(5)
    assert npars == 4 + 2 * 5
    assert ngmix.gmix.get_coellip_ngauss(npars) == (npars - 4) // 2
예제 #10
0
def test_simobs_errors():
    rng = np.random.RandomState(0)

    with pytest.raises(ValueError):
        ngmix.simobs.simulate_obs(obs=None, gmix=None)

    with pytest.raises(ValueError):
        ngmix.simobs.simulate_obs(obs=None, gmix=3)

    nband = 3
    data = get_model_obs(
        rng=rng,
        model='gauss',
        noise=0.1,
        nepoch=2,
        nband=nband,
        set_psf_gmix=True,
    )
    obs = data['obs']

    with pytest.raises(ValueError):
        ngmix.simobs.simulate_obs(obs=obs, gmix=None)

    with pytest.raises(ValueError):
        ngmix.simobs.simulate_obs(obs=obs, gmix=3)

    with pytest.raises(ValueError):
        ngmix.simobs.simulate_obs(obs=obs, gmix=[None])

    gm = ngmix.GMixModel([0, 0, 0, 0, 4, 1], "gauss")
    with pytest.raises(ValueError):
        ngmix.simobs.simulate_obs(obs=obs, gmix=[gm] * (nband + 1))

    obs[0][0].psf.set_gmix(None)
    with pytest.raises(RuntimeError):
        ngmix.simobs.simulate_obs(obs=obs, gmix=[gm] * nband)

    obs[0][0].set_psf(None)
    with pytest.raises(RuntimeError):
        ngmix.simobs.simulate_obs(obs=obs, gmix=[gm] * nband)
예제 #11
0
    def get_object(self):
        import ngmix

        row, col = self.get_position()

        g1, g2 = self.get_shape()

        fwhm = self.get_fwhm()
        T = ngmix.moments.fwhm_to_T(fwhm)

        flux = self.get_flux()

        pars = [
            row * self.scale,
            col * self.scale,
            g1,
            g2,
            T,
            flux,
        ]
        gm0 = ngmix.GMixModel(pars, 'gauss')
        return gm0.convolve(self.psf)
예제 #12
0
파일: em_1gauss.py 프로젝트: aguinot/ngmix
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
예제 #13
0
    def _set_weight_function(self):
        import ngmix

        T = ngmix.moments.fwhm_to_T(self.fwhm)
        pars = [
            0.0,
            0.0,
            0.0,
            0.0,
            T,
            1.0,
        ]
        weight = ngmix.GMixModel(pars, 'gauss')

        weight.set_norms()

        # set so max is 1
        data = weight.get_data()
        weight.set_flux(1 / data['norm'][0])
        weight.set_norms()
        self.weight = weight

        sigma = np.sqrt(T / 2)
        self.maxrad = 5 * sigma
예제 #14
0
def meas_one_im(*, g1, g2, seed, n_stars=0, sym_nfold=None, interp=False):
    rng = np.random.RandomState(seed=seed)

    obj = galsim.Exponential(half_light_radius=0.5).shear(g1=g1, g2=g2)
    psf = galsim.Gaussian(fwhm=0.9).withFlux(1e6)
    obj = galsim.Convolve([obj, psf])
    dim = 53
    cen = (dim - 1) // 2
    offset = rng.uniform(low=-0.5, high=0.5, size=2)
    im = obj.drawImage(nx=dim, ny=dim, scale=0.2, offset=offset).array
    jac = jac = ngmix.DiagonalJacobian(
        scale=0.2,
        row=cen + offset[1],
        col=cen + offset[0],
    )
    psf_im = psf.drawImage(nx=dim, ny=dim, scale=0.2).array
    psf_jac = ngmix.DiagonalJacobian(scale=0.2, row=cen, col=cen)
    psf_obs = ngmix.Observation(
        image=psf_im,
        weight=np.ones_like(psf_im),
        jacobian=psf_jac,
    )
    wgt = np.ones_like(im)

    bmask = np.zeros_like(im, dtype=np.int32)

    if True:
        for _ in range(n_stars):
            srad = np.power(10, rng.uniform(low=1, high=3))
            ang = rng.uniform(low=0, high=2.0 * np.pi)
            lrad = rng.uniform(low=(srad - (dim / 2 - 3)),
                               high=srad - (dim / 2 - 10)) + dim / 2
            xc = lrad * np.cos(ang) + dim / 2
            yc = lrad * np.sin(ang) + dim / 2
            srad2 = srad * srad
            x, y = np.meshgrid(np.arange(dim), np.arange(dim))
            msk = ((x - xc)**2 + (y - yc)**2) < srad2
            bmask[msk] = 1
    else:
        import scipy.ndimage
        msk = np.zeros_like(bmask)
        angle = rng.uniform(low=0, high=360) * 0
        col = int(rng.uniform(low=dim / 2, high=dim - 1))
        msk[:, col:] = 1
        msk = scipy.ndimage.rotate(
            msk,
            angle,
            reshape=False,
            order=1,
            mode='constant',
            cval=1.0,
        )
        bmask[msk == 1] = 1
    if sym_nfold is not None:
        bmask = symmetrize_bmask_nfold(bmask=bmask, nfolds=sym_nfold)

    msk = bmask != 0
    wgt[msk] = 0
    im[msk] = np.nan

    if interp:
        nse = rng.normal(size=im.shape)
        iim, inse = interpolate_image_and_noise(image=im,
                                                noises=[nse],
                                                weight=wgt,
                                                bmask=bmask,
                                                bad_flags=1,
                                                rng=rng,
                                                fill_isolated_with_noise=True)

    obs = ngmix.Observation(
        image=im,
        weight=wgt,
        jacobian=jac,
        psf=psf_obs,
        bmask=bmask,
    )
    mom = GaussMom(fwhm=1.2)
    res = mom.go(obs=obs)

    gauss_wgt = ngmix.GMixModel(
        [0, 0, 0, 0, ngmix.moments.fwhm_to_T(1.2), 1],
        'gauss',
    )
    cobs = obs.copy()
    cobs.image = 1.0 - wgt
    cobs.weight = np.ones_like(wgt)
    stats = gauss_wgt.get_weighted_sums(
        cobs,
        1.2 * 2,
    )
    mfrac = stats["sums"][5] / stats["wsum"]
    return res["e"][0], res["e"][1], obs, mfrac
예제 #15
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
예제 #16
0
파일: em.py 프로젝트: esheldon/shredder
    def go(self, gmix_guess, sky):
        """
        Run the em algorithm from the input starting guesses

        parameters
        ----------
        gmix_guess: GMix
            A gaussian mixture (GMix or child class) representing a starting
            guess for the algorithm.  This should be *before* psf convolution.
        sky: number
            The sky value added to the image
        """

        if hasattr(self, '_gm'):
            del self._gm
            del self._gm_conv

        obs = self._obs

        # makes a copy
        if not obs.has_psf() or not obs.psf.has_gmix():
            logger.debug('NO PSF SET')
            gmix_psf = ngmix.GMixModel([0., 0., 0., 0., 0., 1.0], 'gauss')
        else:
            gmix_psf = obs.psf.gmix
            gmix_psf.set_flux(1.0)

        conf = self._make_conf()
        conf['sky'] = sky

        gm = gmix_guess.copy()
        gm_conv = gm.convolve(gmix_psf)

        sums = self._make_sums(len(gm))

        pixels = obs.pixels.copy()

        if np.any(pixels['ierr'] <= 0.0):
            fill_zero_weight = True
        else:
            fill_zero_weight = False

        flags = 0
        try:
            numiter, fdiff, sky = self._runner(
                conf,
                pixels,
                sums,
                gm.get_data(),
                gmix_psf.get_data(),
                gm_conv.get_data(),
                fill_zero_weight=fill_zero_weight,
            )

            pars = gm.get_full_pars()
            pars_conv = gm_conv.get_full_pars()
            self._gm = GMix(pars=pars)
            self._gm_conv = GMix(pars=pars_conv)

            if numiter >= self.maxiter:
                flags = EM_MAXITER
                message = 'maxit'
            else:
                message = 'OK'

            result = {
                'flags': flags,
                'numiter': numiter,
                'fdiff': fdiff,
                'sky': sky,
                'message': message,
            }

        except (GMixRangeError, ZeroDivisionError) as err:
            message = str(err)
            logger.info(message)
            result = {
                'flags': EM_RANGE_ERROR,
                'message': message,
            }

        self._result = result
예제 #17
0
파일: gaussap.py 프로젝트: NiallMac/ngmixer
 def _get_gmix(self, pars):
     gm0 = ngmix.GMixModel(pars, model)
     gm = gm0.convolve(self.psf)
     return gm