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)
def plotProfile(datfilebase, c, label, labelZ = True): mass, concen, redshift = readtxtfile.readtxtfile('%s.dat' % datfilebase)[0] cat = ldac.openObjectFile('%s.cat' % datfilebase) zlens = cat.hdu.header['ZLENS'] rscale = nfwutils.rscaleConstM(mass/nfwutils.global_cosmology.h, concen, zlens, 200) r_mpc = np.ascontiguousarray(cat['r_mpc'], dtype='<d') clean = r_mpc > 0.05 rho_c_over_sigma_c = 1.5 * nfwutils.global_cosmology.angulardist(zlens) * nfwutils.global_cosmology.beta([1e6], zlens)[0] * nfwutils.global_cosmology.hubble2(zlens) / nfwutils.global_cosmology.v_c**2 gamma = nfwmodeltools.NFWShear(r_mpc[clean], concen, rscale, rho_c_over_sigma_c) kappa = nfwmodeltools.NFWKappa(r_mpc[clean], concen, rscale,rho_c_over_sigma_c) gpred = cat['beta_s'][clean]*gamma / (1 - (cat['beta_s2'][clean]*kappa/cat['beta_s'][clean])) signalpred = gpred / (cat['beta_s'][clean]*nfwutils.global_cosmology.beta([1e6], zlens)*nfwutils.global_cosmology.angulardist(zlens)) # print signalpred if labelZ: flabel='%s z=%1.2f' % (label, zlens) else: flabel =label pylab.errorbar(cat['r_mpc']*nfwutils.global_cosmology.h, cat['ghat'], cat['ghatdistrosigma']/(np.sqrt(cat['ndat'])), linestyle='None', marker='o', color=c, label=flabel) pylab.plot(cat['r_mpc'][clean]*nfwutils.global_cosmology.h, signalpred, marker='None', linestyle=':', color=c, linewidth=2)
def reducedShear(scaledm200): m200 = 1e14 * scaledm200 c = duffy(m200, zcluster) rs = nfwutils.rscaleConstM(m200, c, zcluster, 200) gamma = tools.NFWShear(r_mpc, c, rs, rho_c_over_sigma_c) kappa = tools.NFWKappa(r_mpc, c, rs, rho_c_over_sigma_c) g = beta_s * gamma / (1 - beta_cor * kappa) return g
def createPerfectProfile(m200, c, zcluster, r_mpc, beta_s): rho_c_over_sigma_c = 1.5 * nfwutils.global_cosmology.angulardist( zcluster) * nfwutils.global_cosmology.beta( [1e6], zcluster)[0] * nfwutils.global_cosmology.hubble2( zcluster) / nfwutils.global_cosmology.v_c**2 r_scale = nfwutils.rscaleConstM(m200, c, zcluster, 200) nfw_shear_inf = tools.NFWShear(r_mpc, c, r_scale, rho_c_over_sigma_c) nfw_kappa_inf = tools.NFWKappa(r_mpc, c, r_scale, rho_c_over_sigma_c) g = beta_s * nfw_shear_inf / (1 - (beta_s * nfw_kappa_inf)) return g
def stackResiduals(sims, outdir): configfile = '%s/config.sh' % outdir config = nfwfit_bcc.readConfiguration(configfile) fitter = nfwfit_bcc.buildFitter(config) radial_points = [] amp_points = [] for simfile in sims: base = os.path.basename(simfile) root, ext = os.path.splitext(base) outfile = '%s/%s.out' % (outdir, root) simcat = nfwfit_bcc.readSimCatalog(simfile, config) r_mpc, ghat, sigma_ghat = fitter.profileBuilder(simcat, config) m200s, nfails = cPickle.load(open(outfile)) fitM200 = np.median(m200s) trueM200 = simcat.hdu.header['M200C'] zcluster = simcat.hdu.header['ZLENS'] concentration = fitter.massconRelation(trueM200, zcluster, 200) r_scale = nfwutils.rscaleConstM(trueM200, concentration, zcluster, 200) profile_g = profileFromMass(fitter.massconRelation, fitM200, 200, r_mpc, simcat) # residuals = profile_g - ghat # scaled_r = r_mpc / r_scale ratio = profile_g / ghat # scaled_residuals = residuals / (r_scale*nfwutils.deltaC(concentration)*nfwutils.global_cosmology.angulardist(zcluster) * nfwutils.global_cosmology.beta([1e6], zcluster) * nfwutils.global_cosmology.hubble2(zcluster)) radial_points.extend(r_mpc) amp_points.extend(ratio) return radial_points, amp_points
def residual(binbase): mass, concen, redshift = readtxtfile.readtxtfile('%s.dat' % binbase)[0] cat = ldac.openObjectFile('%s.cat' % binbase) zlens = cat.hdu.header['ZLENS'] rscale = nfwutils.rscaleConstM(mass/nfwutils.global_cosmology.h, concen, zlens, 200) gamma = nfwmodeltools.NFWShear(cat['r_mpc'], concen, rscale, zlens) kappa = nfwmodeltools.NFWKappa(cat['r_mpc'], concen, rscale, zlens) gpred = cat['beta_s']*gamma / (1 - (cat['beta_s2']*kappa/cat['beta_s'])) fig = pylab.figure() # pylab.errorbar(cat['r_mpc']*nfwutils.global_cosmology.h, cat['ghat']/gpred, cat['ghatdistrosigma']/(np.sqrt(cat['ndat'])*gpred), fmt='bo') # pylab.errorbar(cat['r_mpc']*nfwutils.global_cosmology.h, cat['ghat']/gpred, cat['ghatdistrosigma']/(gpred), fmt='bo') pylab.errorbar(cat['r_mpc'], cat['ghat'], cat['ghatdistrosigma']/np.sqrt(cat['ndat']), fmt='bo') # ax = pylab.gca() ax.set_xscale('log') pylab.axhline(0.0, c='k', linewidth=2) pylab.xlabel('Radius [Mpc/h]', fontsize=16) pylab.ylabel('<g_meas/g_pred> - 1)', fontsize=16) # pylab.ylabel('<g_meas - g_pred>', fontsize=16) pylab.axis([0.05, 10, -.55, 0.35]) # ax2 = ax.twinx() # pylab.plot(cat['r_mpc'], gpred, 'k--') # ax2.errorbar(cat['r_mpc'], cat['ghat'], cat['ghatdistrosigma']/np.sqrt(cat['ndat']), fmt='rs') # ax2.set_ylabel('<g_meas - g_pred>', color='r', fontsize=16) # ax2.set_ylim(-0.2, 0.1) # # pylab.title('Redshift=%1.2f Mass=%1.2fx10^14 Concen=%1.2f' % (zlens, mass/1e14, concen)) # pylab.tight_layout() pylab.savefig('%s.png' % binbase) return fig
def profileFromMass(mcrelation, mdelta, delta, r_mpc, simcat): zcluster = simcat.hdu.header['ZLENS'] concentration = mcrelation(mdelta, zcluster, delta) r_scale = nfwutils.rscaleConstM(mdelta, concentration, zcluster, delta) nfw_shear_inf = tools.NFWShear(r_mpc, concentration, r_scale, zcluster) nfw_kappa_inf = tools.NFWKappa(r_mpc, concentration, r_scale, zcluster) beta_s = np.mean(simcat['beta_s']) beta_s2 = np.mean(simcat['beta_s']**2) g = beta_s * nfw_shear_inf / (1 - ((beta_s2 / beta_s) * nfw_kappa_inf)) return g
def __call__(self, x, m200, c200): if m200 == 0.: return np.zeros_like(x) isNegative=m200 < 0 if isNegative: m200 = np.abs(m200) r_scale = nfwutils.rscaleConstM(m200*self.massScale, c200, self.zcluster, self.overdensity) nfw_shear_inf = tools.NFWShear(x, c200, r_scale, self.rho_c_over_sigma_c) nfw_kappa_inf = tools.NFWKappa(x, c200, r_scale, self.rho_c_over_sigma_c) if isNegative: nfw_shear_inf = -nfw_shear_inf g = self.beta_s*nfw_shear_inf / (1 - ((self.beta_s2/self.beta_s)*nfw_kappa_inf) ) return g
def __init__(self, filebase): print 'Loading %s' % filebase #the file in this case is a configuration file with information about mass, concentration, redshift, etc super(AnalyticSim, self).__init__() with open(filebase) as input: config = yaml.load(input) max_dist = config['max_dist'] gridlength = config['gridlength'] zcluster = config['zcluster'] m200 = config['m200'] c200 = config['c200'] rscale = nfwutils.rscaleConstM(m200, c200, zcluster, 200.) xs = np.linspace(-max_dist, max_dist, gridlength) ys = np.linspace(-max_dist, max_dist, gridlength) x_mpc, y_mpc = [grid.flatten() for grid in np.meshgrid(xs, ys, indexing='ij')] r_mpc = np.sqrt(x_mpc**2 + y_mpc**2) Dl = nfwutils.global_cosmology.angulardist(zcluster) x_arcmin = (x_mpc/Dl)*(180./np.pi)*60. y_arcmin = (y_mpc/Dl)*(180./np.pi)*60. rho_c_over_sigma_c = 1.5 * nfwutils.global_cosmology.angulardist(zcluster) * nfwutils.global_cosmology.beta([1e6], zcluster)[0] * nfwutils.global_cosmology.hubble2(zcluster) / nfwutils.global_cosmology.v_c**2 gamma_t_inf = nfwmodeltools.NFWShear(r_mpc, c200, rscale, rho_c_over_sigma_c) kappa_inf = nfwmodeltools.NFWKappa(r_mpc, c200, rscale, rho_c_over_sigma_c) posangle = np.arctan2(y_mpc, x_mpc) cos2phi = np.cos(2*posangle) sin2phi = np.sin(2*posangle) gamma1_inf = -gamma_t_inf*cos2phi gamma2_inf = -gamma_t_inf*sin2phi self.zcluster = zcluster self.x_mpc = x_mpc self.y_mpc = y_mpc self.x_arcmin = x_arcmin self.y_arcmin = y_arcmin self.gamma1_inf = gamma1_inf self.gamma2_inf = gamma2_inf self.kappa_inf = kappa_inf
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()
# # 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'
def MXXLmultibinresidualoverplot(binbase, fig = None): setMXXL() if fig is None: matplotlib.rcParams['figure.figsize'] = [16,8] fig = pylab.figure() curplot = 5 for curc in range(4): pylab.subplot(2,4,curplot) colori = 0 for curm in range(2): try: mass, concen = readtxtfile.readtxtfile('%s_%d_%d.dat' % (binbase, curm, curc))[0] cat = ldac.openObjectFile('%s_%d_%d.cat' % (binbase, curm, curc)) zlens = cat.hdu.header['ZLENS'] rscale = nfwutils.rscaleConstM(mass/nfwutils.global_cosmology.h, concen, zlens, 200) gamma = nfwmodeltools.NFWShear(cat['r_mpc'], concen, rscale, zlens) kappa = nfwmodeltools.NFWKappa(cat['r_mpc'], concen, rscale, zlens) gpred = cat['beta_s']*gamma / (1 - (cat['beta_s2']*kappa/cat['beta_s'])) pylab.errorbar(cat['r_mpc']*nfwutils.global_cosmology.h, cat['ghat'], cat['ghatdistrosigma']/(np.sqrt(cat['ndat'])), linestyle='None', marker='o', color=c[colori], label='M=%1.1fx10^14' % (mass/1e14)) pylab.plot(cat['r_mpc']*nfwutils.global_cosmology.h, gpred, 'k-', linewidth=2) ax = pylab.gca() ax.set_xscale('log') pylab.axhline(0.0, c='k', linewidth=2) pylab.axis([0.05, 10, -0.03, 0.4]) except: pass colori+= 1 pylab.title('C=%1.1f' % (concen)) curplot += 1 for i in range(4): pylab.subplot(2,4,4+i+1) pylab.xlabel('Radius [Mpc/h]') pylab.text(0.1, 0.03, 'MXXL') pylab.minorticks_on() pylab.subplot(2,4,5) pylab.ylabel('<g_m/g_p-1>') pylab.subplot(2,4,8) pylab.legend(loc='upper right') pylab.tight_layout() pylab.savefig('%s_shearstack.png' % binbase) return fig
def BK11multibinresidual(dirbase): setBK11() matplotlib.rcParams['figure.figsize'] = [16,16] fig = pylab.figure() curplot = 1 for curm in range(4): for curc in range(4): pylab.subplot(4,4,curplot) colori = 1 for snap in [124,141]: try: mass, concen, redshift = readtxtfile.readtxtfile('%s_%d/bk11stack_%d_%d.dat' % (dirbase, snap, curm, curc))[0] cat = ldac.openObjectFile('%s_%d/bk11stack_%d_%d.cat' % (dirbase, snap, curm, curc)) zlens = cat.hdu.header['ZLENS'] rscale = nfwutils.rscaleConstM(mass/nfwutils.global_cosmology.h, concen, zlens, 200) gamma = nfwmodeltools.NFWShear(cat['r_mpc'], concen, rscale, zlens) kappa = nfwmodeltools.NFWKappa(cat['r_mpc'], concen, rscale, zlens) gpred = cat['beta_s']*gamma / (1 - (cat['beta_s2']*kappa/cat['beta_s'])) pylab.errorbar(cat['r_mpc']*nfwutils.global_cosmology.h, cat['ghat'], cat['ghatdistrosigma']/(np.sqrt(cat['ndat'])), linestyle='None', marker='o', color=c[colori], label='M=%1.1fx10^14' % (mass/1e14)) pylab.plot(cat['r_mpc']*nfwutils.global_cosmology.h, gpred, 'k-', linewidth=2) ax = pylab.gca() ax.set_xscale('log') pylab.axhline(0.0, c='k', linewidth=2) pylab.axis([0.05, 10, -0.03, 0.4]) ax = pylab.gca() ax.set_xscale('log') pylab.axhline(0.0, c='k', linewidth=2) # pylab.legend(loc='lower center', fontsize=10) # pylab.title('M=%1.1fx10^14 C=%1.1f' % ( mass/1e14, concen)) except: pass colori+= 1 curplot += 1 for i in range(4): pylab.subplot(4,4,13+i) pylab.xlabel('Radius [Mpc/h]') pylab.subplot(4,4,4*i+1) pylab.ylabel('<g_m/g_p - 1>') pylab.tight_layout() pylab.savefig('%s_multibin_shearprofile.png' % dirbase) # pylab.savefig('%s_multibin_resid.png' % binbase) return fig
def multibinresidual(binbase, fig = None): matplotlib.rcParams['figure.figsize'] = [16,16] if fig is None: fig = pylab.figure() curplot = 1 for curm in range(4): for curc in range(4): pylab.subplot(4,4,curplot) colori = 0 # for curz in range(3): for curz in [0]: mass, concen, redshift = readtxtfile.readtxtfile('%s_%d_%d_%d.dat' % (binbase, curz, curm, curc))[0] cat = ldac.openObjectFile('%s_%d_%d_%d.cat' % (binbase, curz, curm, curc)) zlens = cat.hdu.header['ZLENS'] rscale = nfwutils.rscaleConstM(mass/nfwutils.global_cosmology.h, concen, zlens, 200) gamma = nfwmodeltools.NFWShear(cat['r_mpc'], concen, rscale, zlens) kappa = nfwmodeltools.NFWKappa(cat['r_mpc'], concen, rscale, zlens) gpred = cat['beta_s']*gamma / (1 - (cat['beta_s2']*kappa/cat['beta_s'])) pylab.errorbar(cat['r_mpc']*0.72, cat['ghat'], cat['ghatdistrosigma']/(np.sqrt(cat['ndat'])), linestyle='None', marker='o', color=c[colori], label='BCC z=%1.1f' % redshift) pylab.plot(cat['r_mpc']*0.72, gpred, 'k-', linewidth=2) ax = pylab.gca() ax.set_xscale('log') pylab.axhline(0.0, c='k', linewidth=2) pylab.legend(loc='lower center', fontsize=10) pylab.axis([0.05, 10, -.55, 0.35]) # pylab.axis([0.05, 10, -.10, 0.05]) colori+= 1 pylab.title('M=%1.1fx10^14 C=%1.1f' % ( mass/1e14, concen)) curplot += 1 for i in range(4): pylab.subplot(4,4,13+i) pylab.xlabel('Radius [Mpc/h]') pylab.subplot(4,4,4*i+1) # pylab.ylabel('<g_m-g_p>/g_p(<M>,<c>)') # pylab.ylabel('<g_m/g_p - 1>') pylab.tight_layout() # pylab.savefig('%s_multibin_fracresid.png' % binbase) pylab.savefig('%s_multibin_resid.png' % binbase) return fig