-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_statistics.py
273 lines (183 loc) · 7.83 KB
/
test_statistics.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import sys
try:
from lenstools import Ensemble
from lenstools.defaults import default_callback_loader,peaks_loader,convergence_measure_all
from lenstools.index import Indexer,MinkowskiAll
except ImportError:
sys.path.append("..")
from lenstools import Ensemble
from lenstools.defaults import default_callback_loader,peaks_loader,convergence_measure_all
from lenstools.index import Indexer,MinkowskiAll
try:
from emcee.utils import MPIPool
MPIPool = MPIPool
except ImportError:
MPIPool = None
import logging
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
logging.basicConfig(level=logging.DEBUG)
if MPIPool is None:
logging.warning("You need to install emcee in order to test the parallel statistics features!!")
try:
logging.debug("Attempting to create MPIPool")
pool = MPIPool()
logging.debug("Succesfully created MPIPool!")
except ValueError:
logging.debug("No reason to create one, one process only!!")
pool = None
except TypeError:
pool = None
#The only parallelized part is the loading of the ensemble (that's the computationally expensive part)
if (pool is not None) and not(pool.is_master()):
pool.wait()
sys.exit(0)
map_list = ["Data/conv1.fit","Data/conv2.fit","Data/conv3.fit","Data/conv4.fit"]
l_edges = np.arange(200.0,50000.0,200.0)
thresholds_pk = np.arange(-1.0,5.0,0.2)
l = 0.5*(l_edges[:-1] + l_edges[1:])
conv_ensemble = Ensemble.fromfilelist(map_list)
conv_ensemble.load(callback_loader=default_callback_loader,pool=pool,l_edges=l_edges)
if pool is not None:
pool.close()
def test_shape():
assert conv_ensemble.num_realizations==len(map_list)
assert conv_ensemble.data.shape==(len(map_list),len(l_edges)-1)
def test_power_plot():
fig,ax = plt.subplots()
for n in range(conv_ensemble.num_realizations):
ax.plot(l,l*(l+1)*conv_ensemble[n]/(2.0*np.pi),label="Map {0}".format(n+1),linestyle="--")
mean = conv_ensemble.mean()
errors = np.sqrt(conv_ensemble.covariance().diagonal())
ax.errorbar(l,l*(l+1)*mean/(2.0*np.pi),yerr=l*(l+1)*errors/(2.0*np.pi),label="Mean")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel(r"$l$")
ax.set_ylabel(r"$l(l+1)P_l/2\pi$")
ax.legend(loc="upper left")
plt.savefig("power_ensemble.png")
plt.clf()
def test_chi2():
conv_ensemble1 = Ensemble.fromfilelist(map_list[0:2])
conv_ensemble1.load(callback_loader=default_callback_loader,pool=None,l_edges=l_edges)
print("chi2 difference = {0}".format(conv_ensemble.compare(conv_ensemble1)))
def test_pca():
pca_ensemble = Ensemble.read("Data/ensemble_pca.npy")
pca = pca_ensemble.principalComponents()
assert len(pca.explained_variance_)==pca_ensemble.data.shape[1]
fig,ax = plt.subplots(1,2,figsize=(16,8))
ax[0].plot(pca.explained_variance_)
ax[1].plot(pca.explained_variance_.cumsum())
ax[0].set_xlabel(r"$n$")
ax[1].set_xlabel(r"$n$")
ax[0].set_ylabel(r"$\lambda_n$")
ax[1].set_ylabel(r"$\sum^n\lambda_n$")
fig.savefig("pca.png")
def test_add():
conv_ensemble1 = Ensemble.fromfilelist(map_list[0:2])
conv_ensemble2 = Ensemble.fromfilelist(map_list[2:])
conv_ensemble1.load(callback_loader=default_callback_loader,pool=None,l_edges=l_edges)
conv_ensemble2.load(callback_loader=default_callback_loader,pool=None,l_edges=l_edges)
conv_ensemble_union = conv_ensemble1 + conv_ensemble2
assert conv_ensemble_union.num_realizations == 4
assert len(conv_ensemble_union.file_list) == 4
assert conv_ensemble_union.data.shape[0] == 4
assert conv_ensemble_union.data.shape[1] == conv_ensemble1.data.shape[1]
def test_multiply():
conv_ensemble_peaks = Ensemble.fromfilelist(map_list)
conv_ensemble_peaks.load(callback_loader=peaks_loader,pool=None,thresholds=thresholds_pk)
conv_ensemble_both = conv_ensemble * conv_ensemble_peaks
assert conv_ensemble_both.num_realizations == 4
assert conv_ensemble_both.data.shape[0] == 4
assert conv_ensemble_both.data.shape[1] == len(l_edges) + len(thresholds_pk) - 2
def test_save_and_load():
conv_ensemble.save("ensemble_saved.npy")
conv_ensemble.save("ensemble_saved",format="matlab",appendmat=True)
conv_ensemble_new = Ensemble.read("ensemble_saved.npy")
assert conv_ensemble_new.num_realizations == conv_ensemble.num_realizations
assert conv_ensemble_new.data.shape == conv_ensemble.data.shape
def test_group():
conv_ensemble_sparse = Ensemble.fromfilelist(map_list)
conv_ensemble_sparse.load(callback_loader=default_callback_loader,pool=pool,l_edges=l_edges)
conv_ensemble_sparse.group(group_size=2,kind="sparse")
assert conv_ensemble_sparse.num_realizations==2
conv_ensemble_contiguous = Ensemble.fromfilelist(map_list)
conv_ensemble_contiguous.load(callback_loader=default_callback_loader,pool=pool,l_edges=l_edges)
conv_ensemble_contiguous.group(group_size=2,kind="contiguous")
assert conv_ensemble_contiguous.num_realizations==2
fig,ax = plt.subplots()
for n in range(conv_ensemble.num_realizations):
ax.plot(l,l*(l+1)*conv_ensemble.data[n]/(2.0*np.pi),label="Original {0}".format(n+1),linestyle="-")
for n in range(conv_ensemble_sparse.num_realizations):
ax.plot(l,l*(l+1)*conv_ensemble_sparse.data[n]/(2.0*np.pi),label="Sparse {0}".format(n+1),linestyle="--")
for n in range(conv_ensemble_contiguous.num_realizations):
ax.plot(l,l*(l+1)*conv_ensemble_contiguous.data[n]/(2.0*np.pi),label="Contiguous {0}".format(n+1),linestyle="-.")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel(r"$l$")
ax.set_ylabel(r"$l(l+1)P_l/2\pi$")
ax.legend(loc="upper left",prop={"size":7})
plt.savefig("power_ensemble_grouped.png")
plt.clf()
return conv_ensemble_sparse._scheme,conv_ensemble_contiguous._scheme
def test_subset():
conv_subset = conv_ensemble.subset(range(2))
assert conv_subset.num_realizations==2
fig,ax = plt.subplots()
ax.plot(l,l*(l+1)*conv_subset[0]/(2.0*np.pi),label="1")
ax.plot(l,l*(l+1)*conv_subset[1]/(2.0*np.pi),label="2")
conv_subset = conv_ensemble.subset(range(2,4))
assert conv_subset.num_realizations==2
ax.plot(l,l*(l+1)*conv_subset[0]/(2.0*np.pi),label="3")
ax.plot(l,l*(l+1)*conv_subset[1]/(2.0*np.pi),label="4")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel(r"$l$")
ax.set_ylabel(r"$l(l+1)P_l/2\pi$")
ax.legend(loc="upper left")
fig.savefig("power_ensemble_subset.png")
def test_cut():
fig,ax = plt.subplots()
ax.plot(l,l*(l+1)*conv_ensemble.mean()/(2.0*np.pi),label="Full")
#Perform the cut
l_cut = conv_ensemble.cut(10000.0,30000.0,feature_label=l)
assert conv_ensemble.data.shape[1] == len(l_cut)
#Plot
ax.plot(l_cut,l_cut*(l_cut+1)*conv_ensemble.mean()/(2.0*np.pi),label="Cut",color="yellow")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel(r"$l$")
ax.set_ylabel(r"$l(l+1)P_l/2\pi$")
ax.legend(loc="upper left")
plt.savefig("power_ensemble_cut.png")
plt.clf()
def test_differentiate():
thresholds = np.arange(-0.04,0.12,0.001)
midpoints = 0.5*(thresholds[:-1] + thresholds[1:])
index = Indexer.stack([MinkowskiAll(thresholds)])
index_separate = Indexer(MinkowskiAll(thresholds).separate())
diff_ensemble = Ensemble.fromfilelist(map_list)
diff_ensemble.load(callback_loader=convergence_measure_all,index=index)
ensemble_0 = diff_ensemble.split(index_separate)[0]
ensemble_pdf = ensemble_0.differentiate(step=thresholds[0]-thresholds[1])
fig,ax = plt.subplots()
for i in range(ensemble_0.num_realizations):
ax.plot(0.5*(midpoints[:-1]+midpoints[1:]),ensemble_pdf[i])
ax.set_xlabel(r"$\kappa$")
ax.set_ylabel(r"$P(\kappa)$")
fig.savefig("ensemble_differentiate.png")
def test_selfChi2():
ens = Ensemble.read("Data/all/Om0.295_Ol0.705_w-1.878_ns0.960_si0.100/subfield1/sigma05/power_spectrum.npy")
chi2 = ens.selfChi2()
assert chi2.shape[0]==ens.data.shape[0]
#Plot histogram
fig,ax = plt.subplots()
n,bins,patch = ax.hist(chi2,bins=50,normed=True,histtype="stepfilled",alpha=0.5)
#Compare to chi2 distribution
ax.plot(stats.chi2.pdf(bins,ens.data.shape[1]))
#Labels
ax.set_xlabel(r"$\chi^2$")
ax.set_ylabel(r"$P(\chi^2)$")
#Save figure
fig.savefig("self_chi2.png")