Ejemplo n.º 1
0
def photoobj_to_celestepy_src(photoobj_row):
    """Conversion between tractor source object and our source object...."""
    u = photoobj_row[['ra', 'dec']].values

    # brightnesses are stored in mags (gotta convert to nanomaggies)
    mags = photoobj_row[['psfMag_%s' % b
                         for b in ['u', 'g', 'r', 'i', 'z']]].values
    fluxes = [mags2nanomaggies(m) for m in mags]

    # photoobj type 3 are gals, type 6 are stars
    if photoobj_row.type == 6:
        return SrcParams(u, a=0, fluxes=fluxes)
    else:

        # compute frac dev/exp
        prob_dev = np.mean(photoobj_row[['fracDeV_%s' % b for b in BANDS]])

        # galaxy A/B estimate, angle, and radius
        devAB = np.mean(photoobj_row[['deVAB_%s' % b for b in BANDS]])
        devRad = np.mean(photoobj_row[['deVRad_%s' % b for b in BANDS]])
        devPhi = np.mean(photoobj_row[['deVPhi_%s' % b for b in BANDS]])
        expAB = np.mean(photoobj_row[['expAB_%s' % b for b in BANDS]])
        expRad = np.mean(photoobj_row[['expRad_%s' % b for b in BANDS]])
        expPhi = np.mean(photoobj_row[['expPhi_%s' % b for b in BANDS]])

        #estimate - mix over frac dev/exp
        AB = prob_dev * devAB + (1. - prob_dev) * expAB
        Rad = prob_dev * devRad + (1. - prob_dev) * expRad
        Phi = prob_dev * devPhi + (1. - prob_dev) * expPhi

        ## galaxy flux esimates
        devFlux = np.array([
            mags2nanomaggies(m)
            for m in photoobj_row[['deVMag_%s' % b for b in BANDS]]
        ])
        expFlux = np.array([
            mags2nanomaggies(m)
            for m in photoobj_row[['expMag_%s' % b for b in BANDS]]
        ])
        fluxes = prob_dev * devFlux + (1. - prob_dev) * expFlux

        #theta : exponential mixture weight. (1 - theta = devac mixture weight)
        #sigma : radius of galaxy object (in arcsc > 0)
        #rho   : axis ratio, dimensionless, in [0,1]
        #phi   : radians, "E of N" 0=direction of increasing Dec, 90=direction of increasting RAab
        return SrcParams(
            u,
            a=1,
            v=u,
            theta=1.0 - prob_dev,
            phi=-1. * Phi,  #(Phi * np.pi / 180.), # + np.pi / 2) % np.pi,
            sigma=Rad,
            rho=AB,
            fluxes=fluxes)
Ejemplo n.º 2
0
def propose_from_gal_prior(star, fluxes_prior, shape_prior,
                           test=False):
    re_prior, ab_prior, phi_prior = shape_prior
    fluxes = fluxes_prior.sample()[0]

    # do reverse transformation
    re = re_prior.sample()[0,0]
    ab = ab_prior.sample()[0,0]
    phi = phi_prior.sample()[0,0]

    # propose star-like shape parameters
    if test:
        phi = 0.01
        rho = 0.01
        sigma = 0.01
    else:
        sigma = np.exp(re)
        rho = np.exp(ab) / (1 + np.exp(ab))
        phi = np.pi * np.exp(phi) / (1 + np.exp(phi))


    return SrcParams(star.params.u,
                     a      = 1,
                     v      = star.params.u,
                     theta  = 0.5,
                     phi    = phi,
                     sigma  = sigma,
                     rho    = rho,
                     fluxes = np.exp(fluxes))
Ejemplo n.º 3
0
    def prior_sample(self, src_type, u=None):
        params = SrcParams(u=u)
        if src_type == 'star':
            # TODO SET a with atoken
            params.a = 0
            color   = self.star_flux_prior.rvs(size=1)[0]
            logprob = self.star_flux_prior.logpdf(color)
            params.fluxes = np.exp(self.star_flux_prior.to_fluxes(color))
            return params, logprob

        elif src_type == 'galaxy':
            params.a = 1
            color   = self.galaxy_flux_prior.rvs(size=1)[0]
            logprob = self.galaxy_flux_prior.logpdf(color)
            params.fluxes = np.exp(self.galaxy_flux_prior.to_fluxes(color))

            sample_ab = self.galaxy_ab_prior.rvs(size=1)[0,0]
            sample_ab = np.exp(sample_ab) / (1.+np.exp(sample_ab))

            params.shape  = np.array([np.random.random(),
                                      np.exp(self.galaxy_re_prior.rvs(size=1)[0,0]),
                                      np.random.random() * np.pi,
                                      sample_ab])

            logprob_re    = self.galaxy_re_prior.logpdf(params.sigma)
            logprob_ab    = self.galaxy_ab_prior.logpdf(params.rho)
            logprob_shape = -np.log(np.pi) + logprob_re + logprob_ab
            return params, logprob
Ejemplo n.º 4
0
def tractor_src_to_celestepy_src(tsrc):
    """Conversion between tractor source object and our source object...."""
    pos = tsrc.getPosition()
    u = np.array([p for p in pos])

    # brightnesses are stored in mags (gotta convert to nanomaggies)
    def mags2nanomaggies(mags):
        return np.power(10., (mags - 22.5) / -2.5)

    fluxes = tsrc.getBrightnesses()[0]
    fluxes = [mags2nanomaggies(flux) for flux in fluxes]

    if type(tsrc) == PointSource:
        return SrcParams(u, a=0, fluxes=fluxes)
    else:
        if type(tsrc) == ExpGalaxy:
            shape = tsrc.getShape()
            theta = 1.
        elif type(tsrc) == DevGalaxy:
            shape = tsrc.getShape()
            theta = 0.
        elif type(tsrc) == CompositeGalaxy:
            shape = tsrc.shapeExp
            theta = 0.5
        else:
            pass

        return SrcParams(
            u,
            a=1,
            v=u,
            theta=theta,
            phi=shape[
                2],  #-1*(shape[2]-90), # * np.pi / 180., #(shape[2] + 180) * np.pi / 180,
            sigma=shape[0],
            rho=shape[1],
            fluxes=fluxes)
Ejemplo n.º 5
0
    def linear_propose_other_type(self):
        """ based on linear regression of fluxes and conditional distribution
        of galaxy shapes, propose parameters of the other type and report
        the log probability of generating that proposal

        Returns:
            - proposal params
            - log prob of proposal
            - log prob of implied reverse proposal
            - log determinant of the transformation |d(x',u')/d(x,u)|

        """
        params = SrcParams(u=self.params.u)
        if self.is_star():
            params.a = 1

            # fluxes
            residual = mvn.rvs(cov=star_mag_proposal.res_covariance)
            ll_prop_fluxes = mvn.logpdf(residual, mean=None, cov=star_mag_proposal.res_covariance)
            gal_mag = star_mag_proposal.predict(self.params.mags.reshape((1,-1))) + residual
            params.fluxes = du.mags2nanomaggies(gal_mag).flatten()

            # compute reverse ll
            res   = gal_mag_proposal.predict(gal_mag) - self.params.mags
            llrev = mvn.logpdf(res, mean=None, cov=gal_mag_proposal.res_covariance)

            # shape
            sample_re = star_rad_proposal.rvs(size=1)[0]
            ll_shape  = star_rad_proposal.logpdf(sample_re)
            params.shape = np.array([np.random.rand(),
                                     np.exp(sample_re),
                                     np.random.rand() * np.pi,
                                     np.random.rand()])
            _, logdet = np.linalg.slogdet(star_mag_proposal.coef_)
            return params, ll_prop_fluxes + ll_shape, llrev, logdet

        elif self.is_galaxy():
            params.a = 0
            # fluxes
            residual = mvn.rvs(cov=gal_mag_proposal.res_covariance)
            llprob   = mvn.logpdf(residual, mean=None, cov=gal_mag_proposal.res_covariance)
            star_mag = gal_mag_proposal.predict(self.params.mags.reshape((1, -1))) + residual
            params.fluxes = du.mags2nanomaggies(star_mag).flatten()

            res   = star_mag_proposal.predict(star_mag) - self.params.mags
            llrev = mvn.logpdf(res, mean=None, cov=star_mag_proposal.res_covariance)
            ll_re = star_rad_proposal.logpdf(np.log(self.params.sigma))

            _, logdet = np.linalg.slogdet(gal_mag_proposal.coef_)
            return params, llprob, llrev + ll_re, logdet
Ejemplo n.º 6
0
def propose_from_star_prior(gal, prior, test=False):
    fluxes = prior.sample()[0]
    return SrcParams(gal.params.u,
                     a      = 0,
                     fluxes = np.exp(fluxes))
Ejemplo n.º 7
0
 def star_arg_squared_loss(fluxes, galaxy_src, images):
     star = SrcParams(src.u, a=0, fluxes=np.exp(fluxes))
     return squared_loss(galaxy_src, star, images)
Ejemplo n.º 8
0
    # medium 1
    def star_arg_squared_loss_single_im(fluxes, galaxy_src, image):
        star = SrcParams(src.u, a=0, fluxes=np.exp(fluxes))
        return squared_loss_single_im(galaxy_src, star, image)

    def star_arg_squared_loss(fluxes, galaxy_src, images):
        star = SrcParams(src.u, a=0, fluxes=np.exp(fluxes))
        return squared_loss(galaxy_src, star, images)


    # do gradient descent
    for src in [srcs[10]]:
        if src.a == 0:
            continue

        star = SrcParams(src.u, a=0, fluxes=src.fluxes)
        print "loss, galaxy with itself:", squared_loss(src, src, imgs)
        print "loss, galaxy with star:", squared_loss(src, star, imgs)

        final_fluxes = np.zeros(len(BANDS))
        for bi,b in enumerate(BANDS):
            res = minimize(star_arg_squared_loss_single_im,
                           np.log(src.fluxes),
                           args=(src, imgs[bi]),
                           method='Nelder-Mead',
                           options={'maxiter':10})

            print "original result:", squared_loss_single_im(src, star, imgs[bi])
            print "opt result for band", bi, ":",  res

            final_fluxes[bi] = np.exp(res.x[bi])