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 __init__(self): self.offsets_kpc = [ x[0] for x in readtxtfile.readtxtfile( pkg_resources.resource_filename('nfwfitter', 'data/cccp_offsets.dat')) ]
def parseFile(self, filename): basedir = os.path.dirname(filename) self.redshift = float(readtxtfile.readtxtfile('%s/redshift' % basedir)) with open(filename) as input: lines = input.readlines() interior_particles = int(lines[1].split()[1]) for curline, line in enumerate(lines): if profile_start_template.search(line) is not None: break assert (curline < len(lines)) rawprofile = np.array([map(float,x) for x in [x.split() for x in lines[curline+1:]] \ if x != []]) innermass = interior_particles * m_p diffMass = rawprofile[:, 3] * m_p inner_radius = rawprofile[:, 0] / h #Mpc median_radius = rawprofile[:, 1] / h outer_radius = rawprofile[:, 2] / h diff_radius = outer_radius - inner_radius self.inner_mass = innermass self.inner_radius = inner_radius self.median_radius = median_radius self.outer_radius = outer_radius self.diff_radius = diff_radius self.diff_mass = diffMass
def stackCats(stackfile, configname, answerfile, outfile): filebase = os.path.basename(answerfile) match = re.match('(.+)_answers.pkl', filebase) simtype = match.group(1) with open(answerfile, 'rb') as input: answers = cPickle.load(input) tostack = [x[0] for x in readtxtfile.readtxtfile(stackfile)] config = nfwfit.readConfiguration(configname) simreader = nfwfit.buildSimReader(config) nfwutils.global_cosmology.set_cosmology(simreader.getCosmology()) fitter = nfwfit.buildFitter(config) profile = fitter.profileBuilder if profile.binspacing == 'linear': binedges = np.linspace(profile.minradii, profile.maxradii, profile.nbins + 1) else: binedges = np.logspace(np.log10(profile.minradii), np.log10(profile.maxradii), profile.nbins + 1) stackedprofile = OnlineStatistics(binedges) for catalogname in tostack: filebase = os.path.basename(catalogname) match = idpatterns[simtype].match(filebase) haloid = int(match.group(1)) try: truth = answers[haloid] except KeyError: print 'Failure at {0}'.format(output) raise catalog = nfwfit.readSimCatalog(catalogname, simreader, config) stackedprofile.accumulate(catalog, truth) stackedprofile.writeSimCat(outfile) return stackedprofile
def readMCMC(file): data = readtxtfile(file) c = [] rs = [] ll = [] m = [] for i in xrange(len(data)): for j in xrange(int(data[i, 0])): ll.append(data[i, 1]) c.append(data[i, 2]) rs.append(data[i, 3]) m.append(data[i, 4]) return MCMC(array(c), array(rs), array(ll), array(m))
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 summarizeChains(chaindirs, binnum, delta): mus = [] globalmassbinlow = None globalmassbinhigh = None chainfiles = [psd.gatherChainFiles(chaindir, delta, binnum = binnum)[0] \ for chaindir in chaindirs] assert (len(chainfiles) > 0) for chainfile in chainfiles: chain = load_chains.loadChains([chainfile], trim=True) print chainfile, len(chain['logmu']) if len(chain['logmu'][0, :]) < 5000: print 'Skipping' continue split = int((chain['logmu'].shape[1] + 1000) / 2.) splitlen = split - 1000 c1mean = np.mean(chain['logmu'][0, 1000:split]) c1err = np.std(chain['logmu'][0, 1000:split]) / np.sqrt(splitlen) c2mean = np.mean(chain['logmu'][0, split:]) c2err = np.std(chain['logmu'][0, split:]) / np.sqrt(splitlen) # assert(np.abs(c1mean - c2mean)/np.sqrt(c1err**2 + c2err**2) < 5.) massbinlow, massbinhigh = [ x[0] for x in readtxtfile.readtxtfile('%s.massrange' % chainfile) ] if globalmassbinlow is None: globalmassbinlow = massbinlow globalmassbinhigh = massbinhigh assert (massbinlow == globalmassbinlow and massbinhigh == globalmassbinhigh) mu, muerr = ci.maxDensityConfidenceRegion( np.exp(chain['logmu'][0, 1000::3])) mus.append(mu) return np.array(mus)
def loadChains(chainfilenames, trim=False): chainfiles = [readtxtfile.readtxtfile(x) for x in chainfilenames] takelength = len(chainfiles[0]) if trim is True: takelength = np.min(np.array([len(chainfile) for chainfile in chainfiles])) rawdata = [np.row_stack([map(float, y) for y in x[1:takelength]]) for x in chainfiles] columns = chainfiles[0][0] chain = {} for i, col in enumerate(columns): chain[col] = np.row_stack([x[:,i] for x in rawdata]) return chain
def configure(self, config): assert(isinstance(config['betacalcer'], betacalcer.FixedBeta)) assert(isinstance(config['shearnoiser'], shearnoiser.NoNoise)) self.profilefile = config['profilefile'] self.maxradii = config['profileMax'] self.minradii = config['profileMin'] self.binwidth = config['binwidth'] self.profileCol = config['profilecol'] profile = readtxtfile.readtxtfile(self.profilefile) self.bincenters = np.array([x[0] for x in profile]) self.deltag = np.array([x[2] for x in profile]) self.betas = np.array([x[5] for x in profile]) #not scaled by beta_inf self.magbinids = np.array([x[-1] for x in profile]) mask = np.logical_and(self.bincenters >= self.minradii, self.bincenters < self.maxradii) self.bincenters = self.bincenters[mask] self.deltag = self.deltag[mask] self.betas = self.betas[mask] self.magbinids = self.magbinids[mask] self.nbins = len(self.bincenters) self.useAveForCenter = False if 'centerforbin' in config and config['centerforbin'] == 'ave': self.useAveForCenter = True
if not isGood: print 'Reprocessing {}'.format(outfile) configdir, outputbase = os.path.split(outfile) haloid, outext = os.path.splitext(outputbase) nfwfit.runNFWFit('{}/../{}'.format(configdir, haloid), '{}/config.py'.format(configdir), outfile) def scanConfigs(simdir, configs): for config in configs: configdir = '{}/{}'.format(simdir, config) outfiles = glob.glob('{}/*.out') for outfile in outfiles: verifyOutput(outfile) if __name__ == '__main__': simdir = sys.argv[1] configlist = sys.argv[2] configs = [x[0] for x in readtxtfile.readtxtfile(configlist)] scanConfigs(simdir, configs)
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 __init__(self): self.xvp_offsets_mpc = readtxtfile.readtxtfile( pkg_resources.resource_filename('nfwfitter', 'data/sptxvp_bcgxray'))[:, 0]
def __init__(self): self.sz_xvp_bcg_offsets_deg = readtxtfile.readtxtfile( pkg_resources.resource_filename('nfwfitter', 'data/sptxvp_bcgsz'))[:, 1]
# For now, I'm just going to use the existing file, and not include the info I don't have from Aaron's file ####################### import sys, readtxtfile, cPickle, glob, os, re import astropy.io.ascii as asciireader import numpy as np import readMXXLProfile as rmp ###################### snapnum = int(sys.argv[1]) outfile = sys.argv[2] redshift = readtxtfile.readtxtfile( '/vol/euclid1/euclid1_raid1/dapple/mxxl_lensing/mxxlsnap%d/redshift' % snapnum)[0, 0] clusterinfo = {} for halofile in glob.glob( '/vol/euclid1/euclid1_raid1/dapple/mxxl_lensing/mxxlsnap%d/*.convergence_map' % snapnum): halobase = os.path.basename(halofile) match = re.match('halo_%d_((\d+)_\d)\.convergence_map' % snapnum, halobase) myid = match.group(1) stefan_id = int(match.group(2))
####################### # This file compiles the truth tables for BCC clusters ####################### import h5py, h5pyutils, sys, os, glob, cPickle import astropy.io.fits as pyfits import readtxtfile, ldac ####################### clusterlist = sys.argv[1] outfile = sys.argv[2] clusterfiles = [x[0] for x in readtxtfile.readtxtfile(clusterlist)] clusterstofind = {} clusterinfo = {} for i, clusterfile in enumerate(clusterfiles): root, ext = os.path.splitext(clusterfile) clusterid = float(root.split('_')[1]) clusterstofind[clusterid] = None for halofile in glob.glob('/users/dapple/localdisk/bcc_rawcatalogs/halos/*.fit'): halocat = ldac.LDACCat(pyfits.open(halofile)[1])
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
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