/
test_convergence.py
211 lines (141 loc) · 4.38 KB
/
test_convergence.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
try:
from lenstools import ConvergenceMap
except ImportError:
import sys
sys.path.append("..")
from lenstools import ConvergenceMap
import numpy as np
from astropy.units import deg,rad
import matplotlib.pyplot as plt
test_map = ConvergenceMap.load("Data/conv.fit")
#Set bin edges
l_edges = np.arange(200.0,50000.0,200.0)
thresholds_mf = np.arange(-2.0,2.0,0.2)
thresholds_pk = np.arange(-1.0,5.0,0.2)
def test_visualize():
assert test_map.data.dtype == np.float
test_map.setAngularUnits(deg)
test_map.visualize()
test_map.savefig("map.png")
test_map.setAngularUnits(deg)
def test_save():
test_map.save("conv_save.fits")
def test_power():
#Compute
l,Pl = test_map.powerSpectrum(l_edges)
assert type(l)==np.ndarray
assert type(Pl)==np.ndarray
#Visualize
fig,ax = plt.subplots()
ax.plot(l,l*(l+1)*Pl/(2.0*np.pi))
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel(r"$l$")
ax.set_ylabel(r"$l(l+1)P_l/2\pi$")
plt.savefig("power_spectrum.png")
plt.clf()
def test_cross():
#Load
conv1 = ConvergenceMap.load("Data/conv1.fit")
conv2 = ConvergenceMap.load("Data/conv2.fit")
#Cross
l,Pl = conv1.cross(conv2,l_edges=l_edges)
#Visualize
fig,ax = plt.subplots()
ax.plot(l,np.abs(l*(l+1)*Pl/(2.0*np.pi)))
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel(r"$l$")
ax.set_ylabel(r"$l(l+1)P_l/2\pi$")
plt.savefig("cross_spectrum.png")
plt.clf()
def test_pdf():
#Compute
v,p = test_map.pdf(thresholds_mf,norm=True)
#Visualize
fig,ax = plt.subplots()
ax.plot(v,p)
ax.set_xlabel(r"$\nu=\kappa/\sigma$")
ax.set_ylabel(r"$P(\nu)$")
plt.savefig("pdf.png")
plt.clf()
def test_minkowski():
#Compute
nu,V0,V1,V2 = test_map.minkowskiFunctionals(thresholds_mf,norm=True)
#Assert computation went OK
assert hasattr(test_map,"gradient_x")
assert hasattr(test_map,"gradient_y")
assert hasattr(test_map,"hessian_xx")
assert hasattr(test_map,"hessian_yy")
assert hasattr(test_map,"hessian_xy")
#Visualize
fig,ax = plt.subplots(1,3,figsize=(24,8))
ax[0].plot(nu,V0)
ax[1].plot(nu,V1)
ax[2].plot(nu,V2)
ax[0].set_xlabel(r"$\nu=\kappa/\sigma$")
ax[0].set_ylabel(r"$V_0(\nu)$")
ax[1].set_xlabel(r"$\nu=\kappa/\sigma$")
ax[1].set_ylabel(r"$V_1(\nu)$")
ax[2].set_xlabel(r"$\nu=\kappa/\sigma$")
ax[2].set_ylabel(r"$V_2(\nu)$")
fig.tight_layout()
plt.savefig("minkowski.png")
plt.clf()
def test_peaks():
#Compute
nu,pk = test_map.peakCount(thresholds_pk,norm=True)
#Check if computation went OK
assert type(nu)==np.ndarray
assert type(pk)==np.ndarray
#Visualize
fig,ax = plt.subplots()
ax.plot(nu,pk)
ax.set_xlabel(r"$\nu=\kappa/\sigma$")
ax.set_ylabel(r"$dN/d\nu$")
plt.savefig("peaks.png")
def test_peak_locations():
#Thresholds for high peaks
high_thresholds = np.arange(0.3,0.6,0.01)
#Find the peak locations
values,locations = test_map.locatePeaks(high_thresholds)
#Visualize the result
fig,ax = plt.subplots(1,2,figsize=(16,8))
test_map.visualize(fig=fig,ax=ax[0],colorbar=True)
test_map.visualize(fig=fig,ax=ax[1])
ax[1].scatter(locations[:,0].value,locations[:,1].value,color="black")
ax[1].set_xlim(0.0,test_map.side_angle.value)
ax[1].set_ylim(0.0,test_map.side_angle.value)
#And save it
fig.tight_layout()
fig.savefig("peak_locations.png")
def test_getValues():
b = np.linspace(0.0,test_map.side_angle.value,test_map.data.shape[0])
xx,yy = np.meshgrid(b,b) * deg
new_values = test_map.getValues(xx,yy)
assert (new_values==test_map.data)[:-1,:-1].all()
def test_gradient_partial():
b = np.linspace(0.0,test_map.side_angle.value,test_map.data.shape[0])
xx,yy = np.meshgrid(b,b) * deg
gx,gy = test_map.gradient()
gxp,gyp = test_map.gradient(xx,yy)
assert (gx==gxp)[:-1,:-1].all()
assert (gy==gyp)[:-1,:-1].all()
def test_hessian_partial():
b = np.linspace(0.0,test_map.side_angle.value,test_map.data.shape[0])
xx,yy = np.meshgrid(b,b) * deg
hxx,hyy,hxy = test_map.hessian()
hxxp,hyyp,hxyp = test_map.hessian(xx,yy)
assert (hxx==hxxp)[:-1,:-1].all()
assert (hyy==hyyp)[:-1,:-1].all()
assert (hxy==hxyp)[:-1,:-1].all()
def test_cut():
b = np.array([0.0,test_map.side_angle.value/2,0.0,test_map.side_angle.value/2]) * deg
cut_map = test_map.cutRegion(b)
cut_map.visualize()
cut_map.savefig("map_cut.png")
def test_translate():
b = np.array([0.5,test_map.side_angle.value+0.5,0.0,test_map.side_angle.value]) * deg
translated_map = test_map.cutRegion(b)
translated_map.visualize()
translated_map.savefig("map_translated.png")