forked from SubmissionForPapers/NormalizingFlowsCosmology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
127 lines (85 loc) · 3.79 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import quicklens as ql
import numpy as np
r2d = 180./np.pi
d2r = np.pi/180.
def grab(var):
return var.detach().cpu().numpy()
##################### power spectrum
def estimate_ps_ensemble(samples_true,nx,dx,lbins):
ell_binned = lbins[:-1] + 0.5*np.diff(lbins)
nmaps = samples_true.shape[0]
cl_avg = np.zeros(ell_binned.shape[0])
for map_id in range(nmaps):
tmap = samples_true[map_id]
tmap_cfft = ql.maps.rmap(nx, dx,map=tmap).get_cfft()
cl = tmap_cfft.get_cl(lbins)
cl_avg += cl.cl.real
cl_avg = cl_avg/nmaps
return cl_avg,ell_binned
###################### local non-gaussianity
def estimate_fnl_local_ensemble(samples_nongauss, samples_gauss, cl_theo, nx, dx):
#estimates fnl local on a set of maps. you need to provide also gaussian maps which are used to estimate the variance
#both sets need to be equal size and we need at least 1000 maps to make F converge sufficiently
cl_theo_inv_nonan = np.copy(cl_theo)
#cl_theo_inv_nonan[0]=1.
#cl_theo_inv_nonan[1]=1.
cl_theo_inv_nonan[cl_theo_inv_nonan<0.00001] = 0.00001
cl_theo_inv_nonan = 1./cl_theo_inv_nonan
fnl_unnormed_nongauss = np.zeros(samples_gauss.shape[0])
fnl_unnormed_gauss = np.zeros(samples_gauss.shape[0])
for i in range(samples_gauss.shape[0]):
rmap = samples_nongauss[i]
fnl_unnormed_nongauss[i] = estimate_fnl_local_unnormed_singlemap(rmap, cl_theo_inv_nonan, nx, dx)
rmap = samples_gauss[i]
fnl_unnormed_gauss[i] = estimate_fnl_local_unnormed_singlemap(rmap, cl_theo_inv_nonan, nx, dx)
F = np.var(fnl_unnormed_gauss)
fnl_normed_nongauss = fnl_unnormed_nongauss/F
fnl_normed_gauss = fnl_unnormed_gauss/F
return fnl_normed_nongauss,fnl_normed_gauss
def estimate_fnl_local_unnormed_singlemap(rmap, cl_theo_inv_nonan, nx, dx):
#https://github.com/dhanson/quicklens/blob/master/quicklens/maps.py
#FT conventions as in np.rfft2 irfft2
rmap = ql.maps.rmap(nx, dx,map=rmap)
rfft = rmap.get_rfft()
#A map
A = rmap.copy()
#B map
rfft_B = rfft * cl_theo_inv_nonan
B = rfft_B.get_rmap()
#multiply and integrate
fnl_unnormed = (1./6.)*6*np.sum(A.map*A.map*B.map) * (dx**2)
return fnl_unnormed
###################### equilateral non-gaussianity
def estimate_fnl_equilateral_ensemble(samples_nongauss, samples_gauss, cl_theo_ell, nx, dx):
ell_nonan = np.copy(cl_theo_ell)
ell_nonan[0]=1.
fnlequi_unnormed_nongauss = np.zeros(samples_gauss.shape[0])
fnlequi_unnormed_gauss = np.zeros(samples_gauss.shape[0])
for i in range(samples_gauss.shape[0]):
rmap = samples_nongauss[i]
fnlequi_unnormed_nongauss[i] = estimate_fnl_equilateral_unnormed_singlemap(rmap,ell_nonan, nx, dx)
rmap = samples_gauss[i]
fnlequi_unnormed_gauss[i] = estimate_fnl_equilateral_unnormed_singlemap(rmap,ell_nonan, nx, dx)
Fequi = np.var(fnlequi_unnormed_gauss)
fnlequi_normed_nongauss = fnlequi_unnormed_nongauss/Fequi
fnlequi_normed_gauss = fnlequi_unnormed_gauss/Fequi
return fnlequi_normed_nongauss,fnlequi_normed_gauss
def estimate_fnl_equilateral_unnormed_singlemap(rmap,ell_nonan, nx, dx):
rmap = ql.maps.rmap(nx, dx,map=rmap)
rfft = rmap.get_rfft()
#A map
rfft_A = rfft * np.power(ell_nonan,8/3)
A = rfft_A.get_rmap()
#B map
rfft_B = rfft * np.power(ell_nonan,-1/3)
B = rfft_B.get_rmap()
#C map
rfft_C = rfft * np.power(ell_nonan,5/3)
C = rfft_C.get_rmap()
#D map
rfft_D = rfft * np.power(ell_nonan,2/3)
D = rfft_D.get_rmap()
#multiply and integrate
As = 1. #placeholder
fnl_unnormed = (1./As)*np.sum(-3*A.map*B.map*B.map + 6*B.map*C.map*D.map - 2*D.map*D.map*D.map) * (dx**2)
return fnl_unnormed