Exemple #1
0
def image_data():
    dims = (13, 15)
    rng = np.random.RandomState(seed=10)

    data = {}
    for key in ['image', 'weight', 'bmask', 'ormask', 'noise']:
        data[key] = rng.normal(size=dims)

        if key == 'weight':
            data[key] = np.exp(data[key])
        elif key in ['bmask', 'ormask']:
            data[key] = np.clip(data[key] * 100, 0, np.inf).astype(np.int32)
    data['jacobian'] = DiagonalJacobian(x=7, y=6, scale=0.25)
    data['meta'] = {'pi': 3.14}
    data['psf'] = Observation(image=rng.normal(size=dims),
                              meta={'ispsf': True})
    data['psf_gmix'] = Observation(image=rng.normal(size=dims),
                                   meta={'ispsf': True},
                                   gmix=GMix(pars=rng.uniform(size=6)))
    data['gmix'] = GMix(pars=rng.uniform(size=6))
    return data
Exemple #2
0
def test_observation_set(image_data):
    obs = Observation(image=image_data['image'],
                      weight=image_data['weight'],
                      bmask=image_data['bmask'],
                      ormask=image_data['ormask'],
                      noise=image_data['noise'],
                      jacobian=image_data['jacobian'],
                      gmix=image_data['gmix'],
                      psf=image_data['psf'],
                      meta=image_data['meta'])

    rng = np.random.RandomState(seed=11)

    new_arr = rng.normal(size=image_data['image'].shape)
    assert np.all(obs.image != new_arr)
    obs.image = new_arr
    assert np.all(obs.image == new_arr)

    new_arr = np.exp(rng.normal(size=image_data['image'].shape))
    assert np.all(obs.weight != new_arr)
    obs.weight = new_arr
    assert np.all(obs.weight == new_arr)

    new_arr = (np.exp(rng.normal(size=image_data['image'].shape)) *
               100).astype(np.int32)
    assert np.all(obs.bmask != new_arr)
    obs.bmask = new_arr
    assert np.all(obs.bmask == new_arr)

    new_arr = (np.exp(rng.normal(size=image_data['image'].shape)) *
               100).astype(np.int32)
    assert np.all(obs.ormask != new_arr)
    obs.ormask = new_arr
    assert np.all(obs.ormask == new_arr)

    new_arr = rng.normal(size=image_data['image'].shape)
    assert np.all(obs.noise != new_arr)
    obs.noise = new_arr
    assert np.all(obs.noise == new_arr)

    new_jac = DiagonalJacobian(x=8, y=13, scale=1.2)
    assert new_jac.get_galsim_wcs() != obs.jacobian.get_galsim_wcs()
    obs.jacobian = new_jac
    assert new_jac.get_galsim_wcs() == obs.jacobian.get_galsim_wcs()

    new_meta = {'new': 5}
    assert obs.meta != new_meta
    obs.meta = new_meta
    assert obs.meta == new_meta

    new_meta = {'blue': 10}
    new_meta.update(obs.meta)
    assert obs.meta != new_meta
    obs.update_meta_data({'blue': 10})
    assert obs.meta == new_meta

    new_gmix = GMix(pars=rng.uniform(size=6))
    assert np.all(obs.gmix.get_full_pars() != new_gmix.get_full_pars())
    obs.gmix = new_gmix
    assert np.all(obs.gmix.get_full_pars() == new_gmix.get_full_pars())

    new_psf = Observation(image=rng.normal(size=obs.psf.image.shape),
                          meta={'ispsf': True})
    assert np.all(obs.psf.image != new_psf.image)
    obs.psf = new_psf
    assert np.all(obs.psf.image == new_psf.image)
    def _get_nbrs_data(self,cen_id,band,cutout_index):
        """
        return cen and nbrs info for a given cen_id

        Parameters
        ----------
        cen_id: id of central object
        band: integer for band (e.g., 0 for griz MOF)
        cutout_index: index of epoch in MEDS file for nbrs

        Returns
        -------
        cen_ind: index of central intp returned fit_data
        cen_flags: flags for central from nbrs_data
        cen_psf_gmix: an ngmix GMix for PSF of central
        cen_jac: an ngmix Jacobian for the central
        nbrs_inds: list of indexes into returned fit_data of nbrs
        nbrs_flags: flags of nbrs (only use flags == 0)
        nbrs_psf_gmixes: a list of ngmix GMixes of the PSF models for the nbrs
        nbrs_jacs: a list of ngmix Jacobians for the nbrs
        fit_data: entries of input fit_data corresponding to cen_ind and nbrs_inds
        pixel_scale: the pixel_scale of the jacobian associated with the image

        will return None if thete is an error or if not all quantities are found
        """

        fit_inds = []

        # get cen stuff
        q, = numpy.where(self.fit_data['id'] == cen_id)
        if len(q) != 1:
            return None

        cen_ind = 0
        fit_inds.append(q[0])

        q, = numpy.where(
            (self.nbrs_data['id'] == cen_id)
            & (self.nbrs_data['nbr_id'] == cen_id)
            & (self.nbrs_data['band_num'] == band)
            & (self.nbrs_data['cutout_index'] == cutout_index)
        )
        if len(q) != 1:
            return None

        ind = q[0]
        cen_flags = self.nbrs_data['nbr_flags'][ind]
        if cen_flags == 0:
            cen_jac = Jacobian(
                row=self.nbrs_data['nbr_jac_row0'][ind],
                col=self.nbrs_data['nbr_jac_col0'][ind],
                dudrow=self.nbrs_data['nbr_jac_dudrow'][ind],
                dudcol=self.nbrs_data['nbr_jac_dudcol'][ind],
                dvdrow=self.nbrs_data['nbr_jac_dvdrow'][ind],
                dvdcol=self.nbrs_data['nbr_jac_dvdcol'][ind]
            )
            cen_psf_gmix = GMix(pars=self.nbrs_data['nbr_psf_fit_pars'][ind,:])
        else:
            cen_psf_gmix = None
            cen_jac = None

        pixel_scale = self.nbrs_data['pixel_scale'][ind]

        # get nbr ids
        q, = numpy.where(
            (self.nbrs_data['id'] == cen_id)
            & (self.nbrs_data['nbr_id'] != cen_id)
            & (self.nbrs_data['band_num'] == band)
            & (self.nbrs_data['cutout_index'] == cutout_index)
        )
        if len(q) == 0:
            return None

        n1 = len(numpy.unique(self.nbrs_data['nbr_id'][q]))
        n2 = len(self.nbrs_data['nbr_id'][q])

        mess=("nbrs list for given band %d and cutout_index "
              "%d for object %d is not unique!" % (band,cutout_index,cen_id))
        assert n1==n2, mess

        nbrs_inds = [i+1 for i in xrange(len(q))]

        # find location of nbrs in fit_data
        for nbr_id in self.nbrs_data['nbr_id'][q]:
            qq, = numpy.where(self.fit_data['id'] == nbr_id)
            if len(qq) != 1:
                return None
            fit_inds.append(qq[0])

        # doing fit data
        fit_data = self.fit_data[fit_inds]

        # build flag, psf and jacobian lists
        nbrs_flags = []
        nbrs_psf_gmixes = []
        nbrs_jacs = []
        for i,ind in enumerate(q):
            check=fit_data['id'][i+1] == self.nbrs_data['nbr_id'][ind]
            assert check,"Matching of nbr_ids to ids in fit_data did not work!"
            nbrs_flags.append(self.nbrs_data['nbr_flags'][ind])

            if nbrs_flags[-1] == 0:
                nbrs_jacs.append(
                    Jacobian(
                        row=self.nbrs_data['nbr_jac_row0'][ind],
                        col=self.nbrs_data['nbr_jac_col0'][ind],
                        dudrow=self.nbrs_data['nbr_jac_dudrow'][ind],
                        dudcol=self.nbrs_data['nbr_jac_dudcol'][ind],
                        dvdrow=self.nbrs_data['nbr_jac_dvdrow'][ind],
                        dvdcol=self.nbrs_data['nbr_jac_dvdcol'][ind]
                    )
                )
                tgm = GMix(pars=self.nbrs_data['nbr_psf_fit_pars'][ind,:])
                nbrs_psf_gmixes.append(tgm)
            else:
                nbrs_jacs.append(None)
                nbrs_psf_gmixes.append(None)

        return cen_ind, cen_flags, cen_psf_gmix, cen_jac, \
            nbrs_inds, nbrs_flags, nbrs_psf_gmixes, nbrs_jacs, \
            fit_data, pixel_scale
    def render_central(self,
                       cen_id,
                       meds_data,
                       mindex,
                       cutout_index,
                       model,
                       band,
                       image_shape):
        """
        just render the central.  This is needed since Matt does not
        store data for the central i the nbrs data structure if there
        are no neighbors

        you must send epoch_data= to the constructor
        """
        import esutil as eu

        #
        # set and check some parameters
        #
        if self.epoch_data is None:
            raise RuntimeError(
                "send epoch_data= to the constructor to render centrals"
            )

        pars_tag = '%s_max_pars' % model
        if model == 'cm':
            fracdev_tag = 'cm_fracdev'
            TdByTe_tag = 'cm_TdByTe'
        else:
            fracdev_tag=None
            TdByTe_tag=None

        #
        # get data needed to render the central
        #
        we,=numpy.where(
            (self.epoch_data['id'] == cen_id)
            & (self.epoch_data['cutout_index'] == cutout_index)
        )

        if we.size == 0:
            print("    central not in epochs data")
            return None

        psf_pars=self.epoch_data['psf_fit_pars'][we[0],:]
        psf_gmix = GMix(pars=psf_pars)
        e1,e2,T=psf_gmix.get_e1e2T()
        if T <= 0.0:
            print("    bad psf fit for central")
            return None

        pixel_scale=self.epoch_data['pixel_scale'][we[0]]

        jdict=meds_data.get_jacobian(mindex, cutout_index)
        jac=Jacobian(
            row=jdict['row0'],
            col=jdict['col0'],
            dudrow=jdict['dudrow'],
            dudcol=jdict['dudcol'],
            dvdrow=jdict['dvdrow'],
            dvdcol=jdict['dvdcol'],
        )


        # fit information for central
        wfit, = numpy.where(self.fit_data['id'] == cen_id)
        if wfit.size != 1:
            # not sure why this would happen
            print("    Central not found in fit_data")
            return None

        fit_data = self.fit_data[wfit]
        cen_flags=fit_data['flags'][0]
        if cen_flags != 0:
            print("    bad central fit:",cen_flags)
            return None

        cen_img = RenderNGmixNbrs._render_single(
            model,
            band,
            image_shape,
            pars_tag,
            fit_data,
            psf_gmix,
            jac,
            fracdev_tag=fracdev_tag,TdByTe_tag=TdByTe_tag,
        )

        if cen_img is None:
            return None

        return cen_img, pixel_scale
Exemple #5
0
    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
Exemple #6
0
    def _get_nbrs_data(self, cen_id, band, cutout_index):
        """
        return cen and nbrs info for a given cen_id

        Parameters
        ----------
        cen_id: id of central object
        band: integer for band (e.g., 0 for griz MOF)
        cutout_index: index of epoch in MEDS file for nbrs

        Returns
        -------
        cen_ind: index of central intp returned fit_data
        cen_flags: flags for central from nbrs_data
        cen_psf_gmix: an ngmix GMix for PSF of central
        cen_jac: an ngmix Jacobian for the central
        nbrs_inds: list of indexes into returned fit_data of nbrs
        nbrs_flags: flags of nbrs (only use flags == 0)
        nbrs_psf_gmixes: a list of ngmix GMixes of the PSF models for the nbrs
        nbrs_jacs: a list of ngmix Jacobians for the nbrs
        fit_data: entries of input fit_data corresponding to cen_ind and nbrs_inds
        pixel_scale: the pixel_scale of the jacobian associated with the image

        will return None if thete is an error or if not all quantities are found
        """

        fit_inds = []

        # get cen stuff
        qids, = numpy.where(self.fit_ids == cen_id)
        if len(qids) != 1:
            print("    no cen match in fits")
            return None

        cen_ind = 0
        fit_inds.append(qids[0])

        qnbrs_ids, = numpy.where(self.nbrs_ids == cen_id)
        if len(qnbrs_ids) == 0:
            return None

        # extract just the data we need
        nbrs_data = self._fits['nbrs_data'][qnbrs_ids]

        qcen, = numpy.where((nbrs_data['nbr_id'] == cen_id)
                            & (nbrs_data['band_num'] == band)
                            & (nbrs_data['cutout_index'] == cutout_index))
        if len(qcen) != 1:
            return None

        ind = qcen[0]
        cen_flags = nbrs_data['nbr_flags'][ind]
        if cen_flags == 0:
            cen_jac = Jacobian(row=nbrs_data['nbr_jac_row0'][ind],
                               col=nbrs_data['nbr_jac_col0'][ind],
                               dudrow=nbrs_data['nbr_jac_dudrow'][ind],
                               dudcol=nbrs_data['nbr_jac_dudcol'][ind],
                               dvdrow=nbrs_data['nbr_jac_dvdrow'][ind],
                               dvdcol=nbrs_data['nbr_jac_dvdcol'][ind])
            cen_psf_gmix = GMix(pars=nbrs_data['nbr_psf_fit_pars'][ind, :])
        else:
            cen_psf_gmix = None
            cen_jac = None

        pixel_scale = nbrs_data['pixel_scale'][ind]

        # get nbr ids
        qnbr, = numpy.where((nbrs_data['nbr_id'] != cen_id)
                            & (nbrs_data['band_num'] == band)
                            & (nbrs_data['cutout_index'] == cutout_index))
        if len(qnbr) == 0:
            return None

        n1 = len(numpy.unique(nbrs_data['nbr_id'][qnbr]))
        n2 = len(nbrs_data['nbr_id'][qnbr])

        if n1 != n2:
            # not sure why this happens, but it seems to be very rare.
            # For now ignore
            mess = ("nbrs list for given band %d and cutout_index "
                    "%d for object %d is not unique! %d:%d" %
                    (band, cutout_index, cen_id, n1, n2))
            print(nbrs_data['nbr_id'][qnbr])
            print(mess)
            return None
            #raise RuntimeError(mess)

        nbrs_inds = [i + 1 for i in xrange(len(qnbr))]

        # find location of nbrs in fit_data
        for nbr_id in nbrs_data['nbr_id'][qnbr]:
            qq, = numpy.where(self.fit_ids == nbr_id)
            if len(qq) != 1:
                print("    nbr", nbr_id, "not found in fit data")
                return None
            fit_inds.append(qq[0])

        # doing fit data
        # the data come out in row order, we need to reorder

        fit_data_list = []
        for tmp_ind in fit_inds:
            fit_data_list.append(self._fits['model_fits'][tmp_ind])

        fit_data = eu.numpy_util.combine_arrlist(fit_data_list)

        # build flag, psf and jacobian lists
        nbrs_flags = []
        nbrs_psf_gmixes = []
        nbrs_jacs = []
        for i, ind in enumerate(qnbr):
            check = fit_data['id'][i + 1] == nbrs_data['nbr_id'][ind]
            assert check, "Matching of nbr_ids to ids in fit_data did not work!"
            nbrs_flags.append(nbrs_data['nbr_flags'][ind])

            if nbrs_flags[-1] == 0:
                nbrs_jacs.append(
                    Jacobian(row=nbrs_data['nbr_jac_row0'][ind],
                             col=nbrs_data['nbr_jac_col0'][ind],
                             dudrow=nbrs_data['nbr_jac_dudrow'][ind],
                             dudcol=nbrs_data['nbr_jac_dudcol'][ind],
                             dvdrow=nbrs_data['nbr_jac_dvdrow'][ind],
                             dvdcol=nbrs_data['nbr_jac_dvdcol'][ind]))
                tgm = GMix(pars=nbrs_data['nbr_psf_fit_pars'][ind, :])
                nbrs_psf_gmixes.append(tgm)
            else:
                nbrs_jacs.append(None)
                nbrs_psf_gmixes.append(None)

        return cen_ind, cen_flags, cen_psf_gmix, cen_jac, \
            nbrs_inds, nbrs_flags, nbrs_psf_gmixes, nbrs_jacs, \
            fit_data, pixel_scale