Initial import

This commit is contained in:
Guilhem Lavaux 2023-05-29 10:41:03 +02:00
commit 56a50eead3
820 changed files with 192077 additions and 0 deletions

View file

@ -0,0 +1,57 @@
import h5py as h5
import cosmotool as ct
import numpy as np
import ares_tools as at
#result = at.read_chain_avg_dev(".",["final_density", "v_field"],step=20*4,prefix="output",do_dev=True,start=20000)
if False:
mean_p,dev_p = at.read_chain_complex_avg_dev(".",lambda ff: (1+ff['final_density'][...])*ff['v_field'][...],step=10,prefix="output",do_dev=True,start=200, pattern="%s_%04d.h5")
mean_d,dev_d= at.read_chain_complex_avg_dev(".",lambda ff: ff['final_density'][...],step=10,prefix="output",do_dev=True,start=200,pattern="%s_%04d.h5")
np.savez("means.npz", mean_p=mean_p, dev_p=dev_p, mean_d=mean_d,dev_d=dev_d)
else:
x=np.load("means.npz")
mean_d = x['mean_d']
mean_p = x['mean_p']
with h5.File("output_2200.h5", mode="r") as ff:
one_d = ff['final_density'][...]
one_p = ff['v_field'][...]#(ff['final_density'][...]+1)*ff['v_field'][...]
Nside=256
L=4000
N=256
Dmin=10
Dmax=128
#ix=np.arange(N)*L/N - 0.5*L
ix=np.arange(N)*L/N
# These are the box corners
corner_x=-2200
corner_y=-2000
corner_z=-300
shifter=-np.array([corner_x,corner_y,corner_z])*N/L - 0.5*N
shifter2=np.array([corner_x,corner_y,corner_z])
mm=ct.spherical_projection(Nside, mean_d, Dmin, Dmax,shifter=shifter,integrator_id=1,booster=100)
one_mm=ct.spherical_projection(Nside, one_d, Dmin, Dmax,shifter=shifter,integrator_id=1,booster=100)
x = ix[:,None,None].repeat(N,axis=1).repeat(N,axis=2) + shifter2[0]
y = ix[None,:,None].repeat(N,axis=0).repeat(N,axis=2) + shifter2[1]
z = ix[None,None,:].repeat(N,axis=0).repeat(N,axis=1) + shifter2[2]
r = np.sqrt(x**2+y**2+z**2)
cond = r>0
pr = np.where(cond, (mean_p[0,...] * x + mean_p[1,...] * y + mean_p[2,...]*z)/r,0)
one_pr = np.where(cond,(one_p[0,...] * x + one_p[1,...] * y + one_p[2,...]*z)/r,0)
one = np.ones(pr.shape)
mpr=ct.spherical_projection(Nside, pr, Dmin, Dmax,shifter=shifter,integrator_id=1,booster=100)
one_mpr=ct.spherical_projection(Nside, one_pr, Dmin, Dmax,shifter=shifter,integrator_id=1,booster=100)
mdist=ct.spherical_projection(Nside, one, Dmin, Dmax,shifter=shifter,integrator_id=1,booster=100)
np.savez("sky.npz", d=mm,pr=mpr,r=r,dist=mdist,one_d=one_mm,one_pr=one_mpr)

View file

@ -0,0 +1,117 @@
from tqdm import tqdm
import math
import os
import numpy as np
import matplotlib as mpl
import matplotlib.cm as cm
mpl.use('Agg')
import ares_tools as at
import matplotlib.pyplot as plt
import h5py as h5
import numba as nb
startMC=0
suffix="ref"
ss = at.analysis(".")
opts=dict(Nbins=256,range=(0,ss.kmodes.max()))
names=[]
PP=[]
Fmax=0
while True:
try:
os.stat("mcmc_%d.h5" % Fmax)
except:
break
names.append(Fmax)
Fmax += 1
print(Fmax)
def handle_likelihood():
def bias_func(rho, bias):
a=ne.evaluate('exp(b0*log(rho) - b1*rho**(-b2))', dict(b0=bias[0],b1=bias[1],b2=bias[2],rho=rho))
return a
@nb.jit(parallel=True,nopython=True)
def compute_likelihood(S,nmean,bias,density,data):
N0,N1,N2 = density.shape
alpha,r0,eps = bias
L = 0
for p in nb.prange(N0*N1*N2):
k = p % N2
j = (p//N2) % N1
i = (p//N2//N1)
if S[i,j,k] <= 0:
continue
rho = 1+density[i,j,k]+1e-6
x = r0*rho**(-eps)
lrho = math.log(rho)
rho_g = nmean * rho**alpha * math.exp(-x)
log_rho_g = math.log(nmean) + lrho*alpha - x
lam = S[i,j,k] * rho_g
log_lam = math.log(S[i,j,k]) + log_rho_g
L += data[i,j,k]*log_lam - lam
return L
try:
Likelihood = list(np.load("Like_%s.npy" % suffix))
loc_names = names[len(Likelihood):]
except:
Likelihood = []
loc_names = list(names)
print(loc_names)
if len(loc_names) == 0:
return
data = []
selection = []
for d_no in range(16):
print("Load data %d" % (d_no,))
data.append(ss.get_data(d_no))
selection.append(ss.get_mask(d_no))
for mc_id in tqdm(loc_names):
with h5.File("mcmc_%d.h5" % mc_id, mode="r") as f:
density = f['/scalars/BORG_final_density'][...]
L=[]
for i,(D,S) in enumerate(zip(data,selection)):
nmean = f['/scalars/galaxy_nmean_%d' % i][0]
bias = f['/scalars/galaxy_bias_%d' % i][...]
L.append( compute_likelihood(S, nmean, bias, density, D) )
Likelihood.append(L)
np.save("Like.npy", Likelihood)
def handle_power():
Pref = ss.rebin_power_spectrum(startMC, **opts)
try:
data = np.load("power_%s.npz" % suffix,allow_pickle=True)
print("Found previous run")
loc_names = names[len(data['P']):]
print(loc_names)
PP = list(data['P'])
except Exception as exc:
print(exc)
print("no previous run")
PP = []
loc_names = list(names)
print(loc_names)
if len(loc_names) == 0:
return
for i in tqdm(loc_names):
PP.append(ss.compute_power_shat_spectrum(i, **opts))
bins = 0.5*(Pref[2][1:]+Pref[2][:-1])
np.savez("power_%s.npz" % suffix, bins=bins, P=PP, startMC=startMC, Fmax=Fmax, Pref=Pref)
#handle_likelihood()
handle_power()