Exemplo n.º 1
0
    def _trim_images(self, mbo_input, censky):
        """
        cen in sky coords, relative to jacobian
        center
        """

        mbo = MultiBandObsList()
        for obslist in mbo_input:

            new_obslist=ObsList()
            for obs in obslist:

                j=obs.jacobian
                scale=j.get_scale()
                cenpix=array( j.get_cen() )

                new_cen = cenpix + censky/scale
                #print("cen0:",cenpix,"newcen:",new_cen)

                new_im=_trim_image(obs.image, new_cen)
                new_wt=_trim_image(obs.weight, new_cen)
                new_j = j.copy()
                new_j.set_cen(row=new_cen[0], col=new_cen[1])

                newobs = Observation(
                    new_im,
                    weight=new_wt,
                    jacobian=new_j,
                    psf=obs.psf,
                )

                new_obslist.append( newobs )
            mbo.append( new_obslist )

        return mbo
Exemplo n.º 2
0
    def _check_flags(self, mbobs):
        flags = self['metacal'].get('bmask_flags', None)
        passed_flags = True
        _mbobs = None

        if flags is not None:
            _mbobs = MultiBandObsList()
            _mbobs.update_meta_data(mbobs.meta)
            for obslist in mbobs:
                _obslist = ObsList()
                _obslist.update_meta_data(obslist.meta)

                for obs in obslist:
                    msk = (obs.bmask & flags) != 0
                    if np.any(msk):
                        logger.info("   EDGE HIT")
                    else:
                        _obslist.append(obs)
                        passed_flags = True

                _mbobs.append(_obslist)

            # all bands have to have at least one obs
            for ol in _mbobs:
                if len(ol) == 0:
                    passed_flags = False

        return passed_flags, _mbobs
Exemplo n.º 3
0
def _strip_coadd(mbobs):
    _mbobs = MultiBandObsList()
    _mbobs.update_meta_data(mbobs.meta)
    for ol in mbobs:
        _ol = ObsList()
        _ol.update_meta_data(ol.meta)
        for i in range(1, len(ol)):
            _ol.append(ol[i])
        _mbobs.append(_ol)
    return _mbobs
Exemplo n.º 4
0
def _strip_zero_flux(mbobs):
    _mbobs = MultiBandObsList()
    _mbobs.update_meta_data(mbobs.meta)
    for ol in mbobs:
        _ol = ObsList()
        _ol.update_meta_data(ol.meta)
        for i in range(len(ol)):
            if np.sum(ol[i].image) > 0:
                _ol.append(ol[i])
        _mbobs.append(_ol)
    return _mbobs
Exemplo n.º 5
0
 def _get_good_mb_obs_list(self, mb_obs_list):
     new_mb_obs_list = MultiBandObsList()
     for obs_list in mb_obs_list:
         new_obs_list = ObsList()
         for obs in obs_list:
             if obs.meta['flags'] == 0:
                 new_obs_list.append(obs)
         new_mb_obs_list.append(new_obs_list)
     new_mb_obs_list.update_meta_data(mb_obs_list.meta)
     new_mb_obs_list.update_meta_data({'old_mb_obs_list': mb_obs_list})
     return new_mb_obs_list
Exemplo n.º 6
0
    def _get_band_observations(self, band, mindex):
        """
        Get an ObsList for the coadd observations in each band
        If psf fitting fails, the ObsList will be zero length
        note we have already checked that we have a coadd and a single epoch
        without flags
        """

        meds = self.meds_list[band]
        ncutout = meds['ncutout'][mindex]

        image_flags = self._get_image_flags(band, mindex)

        coadd_obs_list = ObsList()
        obs_list = ObsList()

        fake = numpy.zeros((0, 0))
        for icut in xrange(ncutout):

            flags = 0
            if not self._should_use_obs(band, mindex, icut):
                obs = Observation(fake)
                flags = IMAGE_FLAGS
            else:
                iflags = image_flags[icut]
                if iflags != 0:
                    flags = IMAGE_FLAGS
                    obs = Observation(fake)
                else:
                    obs = self._get_band_observation(band, mindex, icut)
                    if obs is None:
                        flags = IMAGE_FLAGS
                        obs = Observation(fake)

            # fill the meta data
            self._fill_obs_meta_data(obs, band, mindex, icut)

            # set flags
            meta = {'flags': flags}
            obs.update_meta_data(meta)

            if icut == 0:
                coadd_obs_list.append(obs)
            else:
                obs_list.append(obs)

        if self.conf['reject_outliers'] and len(obs_list) > 0:
            self._reject_outliers(obs_list)

        obs_list.update_meta_data({'band_num': band})
        coadd_obs_list.update_meta_data({'band_num': band})

        return coadd_obs_list, obs_list
Exemplo n.º 7
0
    def _get_band_observations(self, band, mindex):
        """
        Get an ObsList for the coadd observations in each band
        If psf fitting fails, the ObsList will be zero length
        note we have already checked that we have a coadd and a single epoch
        without flags
        """

        meds=self.meds_list[band]
        ncutout=meds['ncutout'][mindex]

        image_flags=self._get_image_flags(band, mindex)

        coadd_obs_list = ObsList()
        obs_list       = ObsList()

        fake=numpy.zeros((0,0))
        for icut in xrange(ncutout):

            flags=0
            if not self._should_use_obs(band, mindex, icut):
                obs = Observation(fake)
                flags = IMAGE_FLAGS
            else:
                iflags = image_flags[icut]
                if iflags != 0:
                    flags = IMAGE_FLAGS
                    obs = Observation(fake)
                else:
                    obs = self._get_band_observation(band, mindex, icut)
                    if obs is None:
                        flags = IMAGE_FLAGS
                        obs = Observation(fake)

            # fill the meta data
            self._fill_obs_meta_data(obs,band,mindex,icut)

            # set flags
            meta = {'flags':flags}
            obs.update_meta_data(meta)

            if icut==0:
                coadd_obs_list.append(obs)
            else:
                obs_list.append(obs)

        if self.conf['reject_outliers'] and len(obs_list) > 0:
            self._reject_outliers(obs_list)

        obs_list.update_meta_data({'band_num':band})
        coadd_obs_list.update_meta_data({'band_num':band})

        return coadd_obs_list, obs_list
Exemplo n.º 8
0
def mbobs():
    mbobs = MultiBandObsList()
    for i in range(3):
        ol = ObsList()
        for j in range(4):
            o = Observation(image=np.ones((32, 32)) * (j + 1),
                            weight=np.ones((32, 32)) * (j + 1),
                            jacobian=ngmix.DiagonalJacobian(scale=0.25,
                                                            row=0,
                                                            col=0))
            ol.append(o)
        mbobs.append(ol)
    return mbobs
Exemplo n.º 9
0
    def _get_band_observations(self, band, mindex):
        """
        Get an ObsList for the coadd observations in each band
        If psf fitting fails, the ObsList will be zero length
        note we have already checked that we have a coadd and a single epoch
        without flags
        """

        meds = self.meds_list[band]
        ncutout = meds["ncutout"][mindex]

        image_flags = self._get_image_flags(band, mindex)

        coadd_obs_list = ObsList()
        obs_list = ObsList()

        for icut in xrange(ncutout):
            iflags = image_flags[icut]
            if iflags != 0:
                flags = IMAGE_FLAGS
                obs = Observation(numpy.zeros((0, 0)))
            else:
                obs = self._get_band_observation(band, mindex, icut)
                flags = 0

            # fill the meta data
            self._fill_obs_meta_data(obs, band, mindex, icut)

            # set flags
            meta = {"flags": flags}
            obs.update_meta_data(meta)

            if icut == 0:
                coadd_obs_list.append(obs)
            else:
                obs_list.append(obs)

        """
        if ncutout == 0:
            for o in [coadd_obs_list,obs_list]:
                flags = IMAGE_FLAGS
                obs = Observation(numpy.zeros((0,0)))
                meta = {'flags':flags}
                obs.update_meta_data(meta)            
                o.append(obs)
        """

        if self.conf["reject_outliers"] and len(obs_list) > 0:
            self._reject_outliers(obs_list)

        return coadd_obs_list, obs_list
Exemplo n.º 10
0
    def get_round_band_obslist(self, pars, model, obslist):
        """
        get round version of obslist, with simulated galaxy image
        """
        new_obslist=ObsList()

        gm0=ngmix.GMixModel(pars, model)

        gm0round=gm0.make_round()

        s2n_sum=0.0
        for obs in obslist:
            new_obs = self.get_round_obs(obs, gm0round)
            new_obslist.append( new_obs )

        return new_obslist
Exemplo n.º 11
0
    def get_band_obslist(self, index, band, epoch_data):
        w,=where(  (epoch_data['cutout_index'] > 0)
                 & (epoch_data['psf_fit_flags']==0)
                 & (epoch_data['band_num']==band) )

        print("        found %d good SE cutouts for band %d" % (w.size, band))
        if w.size == 0:
            # we don't process objects without one of the bands
            raise MissingFit("SE band missing")

        obslist = ObsList()

        for ib in xrange(w.size):
            ii = w[ib]
            ed = epoch_data[ii]

            obs = self.get_obs(index, band, ed)

            obslist.append(obs)

        return obslist
Exemplo n.º 12
0
    def _make_mbobs(im, cen):
        dx = cen[0] - int(cen[0] + 0.5)
        dy = cen[1] - int(cen[1] + 0.5)
        xlow = int(int(cen[0] + 0.5) - nstamp_cen)
        ylow = int(int(cen[1] + 0.5) - nstamp_cen)
        _im = im[ylow:ylow + nstamp, xlow:xlow + nstamp]
        jac = DiagonalJacobian(
            scale=wcs.scale,
            x=nstamp_cen + dx,
            y=nstamp_cen + dy,
        )

        obs = Observation(image=_im,
                          jacobian=jac,
                          psf=psf_obs,
                          weight=np.ones_like(_im),
                          noise=rng.normal(size=_im.shape) * noise)
        mbobs = MultiBandObsList()
        obsl = ObsList()
        obsl.append(obs)
        mbobs.append(obsl)
        return mbobs
Exemplo n.º 13
0
def get_model_obs(
    *, rng, model,
    noise=0.0,
    psf_model='turb',
    psf_noise=1.0e-6,
    set_psf_gmix=False,
    set_templates=False,
    set_psf_templates=False,
    nepoch=None, nband=None,
    star=False,
):

    if nband is not None:
        do_mbobs = True
        if nepoch is None:
            nepoch = 1
    else:
        do_mbobs = False
        nband = 1

    if nepoch is not None:
        do_obslist = True
    else:
        do_obslist = False
        nepoch = 1

    if star:
        T = 0.0
    else:
        T = 0.27

    g1 = 0.1
    g2 = 0.05
    flux = 100.0
    off = 0.5

    # not offset from the jacobian center
    pars = [0.0, 0.0, g1, g2, T] + [flux]*nband
    gm = GMixModel(pars[0:6], model)

    mbobs = MultiBandObsList()
    for iband in range(nband):
        obslist = ObsList()
        for i in range(nepoch):

            off1_pix, off2_pix = rng.uniform(low=-off, high=off, size=2)
            dims = [32, 32]
            jcen = (np.array(dims) - 1.0) / 2.0
            jacob = DiagonalJacobian(
                scale=PIXEL_SCALE,
                row=jcen[0] + off1_pix,
                col=jcen[1] + off2_pix,
            )

            psf_ret = get_psf_obs(rng=rng, model=psf_model, noise=psf_noise)
            if set_psf_gmix:
                psf_ret['obs'].set_gmix(psf_ret['gmix'])

            if set_psf_templates:
                psf_ret['obs'].template = psf_ret['gmix'].make_image(
                    dims,
                    jacobian=jacob,
                )

            gmconv = gm.convolve(psf_ret['gmix'])

            im0 = gmconv.make_image(dims, jacobian=jacob)

            im = im0 + rng.normal(size=im0.shape, scale=noise)
            if noise == 0.0:
                weight = im*0 + 1.0/1.0e-12
            else:
                weight = im*0 + 1.0/noise**2

            obs = Observation(
                im,
                weight=weight,
                jacobian=jacob,
                psf=psf_ret['obs'],
            )

            if set_templates:
                obs.template = im0

            obslist.append(obs)

        mbobs.append(obslist)

    ret = {
        'gmix': gm,
        'pars': pars,
        'psf_data': psf_ret,
    }

    if do_mbobs:
        ret['obs'] = mbobs
    else:
        obslist = mbobs[0]
        if not do_obslist:
            obs = obslist[0]
            ret['obs'] = obs
        else:
            ret['obs'] = obslist

    return ret