Ejemplo n.º 1
0
    def __call__(self, profile):


        #only want to define a scan for a 1d model at this point.
        assert(isinstance(self.model, NFW_MC_Model))

        self.model.setData(profile.beta_s, profile.beta_s2, profile.zcluster, zlens = profile.zlens)


        pdfs = {}

        masses = self.masses

        for delta in self.deltas:

            logprob = np.zeros(len(masses))

            if delta == 200:
                workingmasses = masses
                c200s = np.array([self.model.massconRelation(np.abs(curm)*nfwutils.global_cosmology.h, 
                                                      profile.zcluster, float(delta)) for curm in workingmasses])
            elif delta != 200:
                workingmasses = np.zeros_like(masses)
                c200s = np.zeros_like(masses)
                for i, curm in enumerate(masses):
                    c200 = self.model.massconRelation(np.abs(curm)*nfwutils.global_cosmology.h, 
                                                      profile.zcluster, float(delta))
                    rscale = nfwutils.rscaleConstM(np.abs(curm), c200, profile.zcluster, float(delta))
                    m200 = nfwutils.Mdelta(rscale, c200, profile.zcluster, 200)
                    if curm < 0:
                        m200 = -m200
                    workingmasses[i] = m200
                    c200s[i] = c200

            for i in range(len(workingmasses)):
                mass = workingmasses[i]
                c200 = c200s[i]

                logprob[i] = tools.shearprofile_like(mass, c200,
                                                    profile.r_mpc,
                                                    profile.ghat,
                                                    profile.sigma_ghat,
                                                    self.model.beta_s,
                                                    self.model.beta_s2,
                                                    self.model.rho_c,
                                                    self.model.rho_c_over_sigma_c,
                                                    200.)





            pdf = np.exp(logprob - np.max(logprob))
            pdf = pdf/scipy.integrate.trapz(pdf, masses)
            pdfs[delta] = pdf

            if np.any(np.logical_not(np.isfinite(pdf))):
                raise BadPDFException

        return (masses, pdfs)
Ejemplo n.º 2
0
def convertLikelihoodScan(model, delta, masses, pdf200, zcluster):

    #treats input pdf as a likelihood scan & rescales axis. Does not transform like a PDF!!!

    targetmasses = np.zeros_like(masses)
    for i, curm in enumerate(masses):
        c200 = model.massconRelation(
            np.abs(curm) * nfwutils.global_cosmology.h, zcluster, 200.)
        r200 = nfwutils.rdeltaConstM(np.abs(curm), zcluster, 200.)
        rscale = r200 / c200
        m_target = nfwutils.Mdelta(rscale, c200, zcluster, delta)
        if curm < 0:
            m_target = -m_target
        targetmasses[i] = m_target

    targetpdf = np.interp(masses, targetmasses, pdf200)

    targetpdf = targetpdf / np.trapz(targetpdf, masses)

    return targetpdf
Ejemplo n.º 3
0
def consolidateFits(workdir, simtype, outdir):

    failfile = open('{0}/fails'.format(outdir), 'w')

    idpattern = idpatterns[simtype]

    answers = cPickle.load(
        open('{0}/{1}_answers.pkl'.format(workdir, simtype), 'rb'))

    outputfiles = glob.glob('%s/*.out' % outdir)
    nhalos = len(outputfiles)

    ids = []
    measured_m200s = np.zeros(nhalos)
    measured_m200errs = np.zeros(nhalos)
    measured_m500s = np.zeros(nhalos)
    measured_m500errs = np.zeros(nhalos)
    measured_cs = np.zeros(nhalos)
    measured_rs = np.zeros(nhalos)

    true_m200s = np.zeros(nhalos)
    true_m500s = np.zeros(nhalos)
    true_cs = np.zeros(nhalos)
    redshifts = np.zeros(nhalos)

    results = dict(ids=ids,
                   measured_m200s=measured_m200s,
                   measured_m200errs=measured_m200errs,
                   measured_m500s=measured_m500s,
                   measured_m500errss=measured_m500errs,
                   measured_cs=measured_cs,
                   measured_rs=measured_rs,
                   true_m200s=true_m200s,
                   true_m500s=true_m500s,
                   true_cs=true_cs,
                   redshifts=redshifts)

    class WeirdException(Exception):
        pass

    #load up the environment for cosmology, and mc relation if used
    config = nfwfit.readConfiguration('{0}/config.sh'.format(outdir))
    simreader = nfwfit.buildSimReader(config)
    nfwutils.global_cosmology.set_cosmology(simreader.getCosmology())
    fitter = nfwfit.buildFitter(config)

    configname = os.path.basename(outdir)

    for i, output in enumerate(outputfiles):

        filebase = os.path.basename(output)

        match = idpattern.match(filebase)

        try:
            haloid = int(match.group(1))
        except AttributeError as e:
            print filebase
            raise e
        except ValueError:
            haloid = match.group(1)

        try:
            truth = answers[haloid]
        except KeyError:
            print 'Failure at {0}'.format(output)
            raise
        ids.append(haloid)
        true_m200s[i] = truth['m200']
        true_m500s[i] = truth['m500']
        true_cs[i] = truth['concen']
        redshifts[i] = truth['redshift']

        input = open(output)
        measured = cPickle.load(input)
        input.close()

        if measured is None:
            print 'Fail {0} {1}'.format(configname, haloid)
            failfile.write('Fail {0} {1}\n'.format(configname, haloid))
            continue

        measured_m200s[i] = measured[0][
            'm200'] * fitter.model.massScale * nfwutils.global_cosmology.h
        measured_m200errs[i] = np.mean(np.abs(
            measured[1]
            ['m200'])) * fitter.model.massScale * nfwutils.global_cosmology.h
        if 'c200' in measured:
            measured_cs[i] = measured['c200']
        else:
            ## need to dig up the mc relation
            measured_cs[i] = fitter.model.massconRelation(
                np.abs(measured_m200s[i]), redshifts[i],
                fitter.model.overdensity)

        #####
        #calculate m500

        measured_rs[i] = nfwutils.rscaleConstM(np.abs(measured_m200s[i]),
                                               measured_cs[i], redshifts[i],
                                               fitter.model.overdensity)
        measured_m500s[i] = nfwutils.Mdelta(measured_rs[i], measured_cs[i],
                                            redshifts[i], 500)

        if measured_m200s[i] < 0:
            measured_m500s[i] = -measured_m500s[i]

        if not np.isfinite(measured_m500s[i]):
            print 'NOT FINITE'
            print haloid
            print measured

    cPickle.dump(results, open('%s/consolidated.pkl' % outdir, 'w'))

    failfile.close()
Ejemplo n.º 4
0
#
#    simdat1 = data['c4'][radrange]
#    simdat2 = data['cfree'][radrange]
#
#    massbin1, ratiobin1, ratioerr1 = bbb.summary2DMass(simdat1)
#    massbin2, ratiobin2, ratioerr2 = bbb.summary2DMass(simdat2)
#
#    massbins2.append(massbin1)
#    offset.append((np.array(ratiobin1)/ np.array(ratiobin2)) - 1.)
#
##

figure()

mlow500 = nfwutils.Mdelta(
    nfwutils.rscaleConstM(10**massbins[0][0], 4.0, 1.0, 200), 4.0, 1.0,
    500) / 1e14
mid500 = nfwutils.Mdelta(
    nfwutils.rscaleConstM(10**massbins[0][8], 4.0, 1.0, 200), 4.0, 1.0,
    500) / 1e14
mhigh500 = nfwutils.Mdelta(
    nfwutils.rscaleConstM(10**massbins[0][-1], 4.0, 1.0, 200), 4.0, 1.0,
    500) / 1e14

plot(outerrange[:-1],
     100 * np.array([scatter[8] for scatter in scatters[:-1]]),
     c='k',
     linewidth=1.8,
     label='Intrinsic Scatter')

twincolor = '#B366B3'