-
Notifications
You must be signed in to change notification settings - Fork 0
/
dacapo-test.py
220 lines (183 loc) · 9.66 KB
/
dacapo-test.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
# -*- encoding: utf-8 -*-
import unittest as ut
import numpy as np
from calibrate import OfsAndGains, MonopoleAndDipole, \
apply_f, apply_ft, compute_diagm, apply_ptilde, apply_ptildet, \
apply_z, apply_A, compute_v, conjugate_gradient, compute_map_corr, \
da_capo, JacobiPreconditioner, FullPreconditioner, compute_rms, \
get_dipole_temperature
def check_vector_match(name, vector1, vector2, rtol=1e-05):
try:
assert np.allclose(vector1, vector2, rtol=rtol)
print('test "{0}" passed'.format(name))
except:
# This catches both failed assertions and mismatches in the
# shapes of vec1/vec2
print('ERROR: test "{0}" failed'.format(name))
print(' vectors {0} and {1} are not the same'
.format(vector1, vector2))
raise
def build_test_matrices(num_of_samples, num_of_pixels, pix_idx,
mc, signal_sum_map, ofs_and_gains):
P = np.zeros((len(pix_idx), num_of_pixels), dtype='int')
for i, pixel in enumerate(pix_idx):
P[i][pixel] = 1
F = np.zeros((num_of_samples, len(
ofs_and_gains.offsets) + len(ofs_and_gains.gains)))
start_idx = 0
for col, ofs_len in enumerate(ofs_and_gains.samples_per_ofsp):
F[start_idx:(start_idx + ofs_len), col] = 1
start_idx += ofs_len
start_idx = 0
for col, gain_len in enumerate(ofs_and_gains.samples_per_gainp):
F[start_idx:(start_idx + gain_len), len(ofs_and_gains.offsets) + col] = \
signal_sum_map[pix_idx[start_idx:(start_idx + gain_len)]]
start_idx += gain_len
Gext = np.repeat(ofs_and_gains.gains, ofs_and_gains.samples_per_gainp)
ptilde = np.array([P[i] * Gext[i] for i in range(len(Gext))])
M = ptilde.T @ ptilde
invM = np.linalg.inv(M)
mat2x2 = mc.T @ invM @ mc
MCminv = invM - invM @ mc @ np.linalg.inv(mat2x2) @ mc.T @ invM
Z = np.eye(num_of_samples) - ptilde @ MCminv @ ptilde.T
A = F.T @ Z @ F
return P, F, ptilde, M, Z, A
class TestDaCapo(ut.TestCase):
def setUp(self):
self.num_of_pixels = 3
self.sky_map = np.array([-0.4, 0.2, 0.2])
self.D = np.sin(2 * np.pi * np.array([0, 1/3, 2/3]))
self.mc = np.array([self.D, [1, 1, 1]]).T
self.mon_and_dip = MonopoleAndDipole(mask=[1, 1, 1], dipole_map=self.D)
self.signal_sum_map = self.D + self.sky_map
self.pix_idx = np.array([0, 0, 1, 0, 1, 2, 2, 2, 0, 1, 0])
self.ofs_and_gains = OfsAndGains(offsets=np.array([10.0, 10.5]),
gains=np.array([4.1, 4.2]),
samples_per_ofsp=[6, 5],
samples_per_gainp=[6, 5])
self.a_vec = self.ofs_and_gains.a_vec
self.Gext = np.repeat(self.ofs_and_gains.gains,
self.ofs_and_gains.samples_per_gainp)
self.bext = np.repeat(self.ofs_and_gains.offsets,
self.ofs_and_gains.samples_per_ofsp)
self.P, self.F, self.ptilde, self.M, self.Z, self.A = \
build_test_matrices(len(self.bext), self.num_of_pixels, self.pix_idx,
self.mc, self.signal_sum_map, self.ofs_and_gains)
self.tod = self.Gext * (self.P @ self.signal_sum_map) + self.bext
def testApplyF(self):
check_vector_match('apply_f',
self.F @ self.a_vec,
apply_f(self.ofs_and_gains, self.pix_idx,
self.D, self.sky_map))
def testApplyFT(self):
check_vector_match('apply_ft',
self.F.T @ self.tod,
apply_ft(self.tod, self.ofs_and_gains, self.pix_idx,
self.D, self.sky_map))
def testComputeDiagM(self):
check_vector_match('compute_diagm',
np.diag(self.M),
compute_diagm(None, self.ofs_and_gains,
self.pix_idx, self.num_of_pixels))
def testApplyPtilde(self):
check_vector_match('apply_ptilde',
self.ptilde @ self.sky_map,
apply_ptilde(self.sky_map, self.ofs_and_gains,
self.pix_idx))
def testApplyPtildet(self):
check_vector_match('apply_ptildet',
self.ptilde.T @ self.tod,
apply_ptildet(None, self.tod, self.ofs_and_gains,
self.pix_idx, self.num_of_pixels))
def testApplyZ(self):
check_vector_match('apply_z',
self.Z @ self.tod,
apply_z(None, self.tod, self.ofs_and_gains,
self.pix_idx, self.mon_and_dip))
def testApplyA(self):
ofs_gains_guess = OfsAndGains(offsets=np.zeros(2),
gains=np.ones(2),
samples_per_ofsp=self.ofs_and_gains.samples_per_ofsp,
samples_per_gainp=self.ofs_and_gains.samples_per_gainp)
check_vector_match('apply_A',
self.A @ ofs_gains_guess.a_vec,
apply_A(None, self.ofs_and_gains, self.sky_map,
self.pix_idx, self.mon_and_dip,
ofs_gains_guess))
def testComputeV(self):
check_vector_match('compute_v',
self.F.T @ self.Z @ self.tod,
compute_v(None, self.tod, self.ofs_and_gains, self.sky_map,
self.pix_idx, self.mon_and_dip))
def testComputeMapCorr(self):
fake_tod = np.arange(len(self.tod))
check_vector_match('compute_map_corr',
np.linalg.inv(self.ptilde.T @ self.ptilde) @
self.ptilde.T @ (fake_tod - self.F @ self.a_vec),
compute_map_corr(None, fake_tod, self.ofs_and_gains,
self.ofs_and_gains,
self.pix_idx, self.D, self.sky_map))
def testDipoleTemperature(self):
# Speed of the Solar System according to Planck 2015, in m/s
solsysspeed = np.array(
[-357948.22884313, 60840.92963476, -71640.11501626])
tcmb = 2.72548
# No quadrupolar correction
assert np.allclose(get_dipole_temperature(tcmb, solsysspeed, [0, 0, 1]),
-0.0006532168171509646)
assert np.allclose(get_dipole_temperature(tcmb, solsysspeed, [0, 0, 1], 30e9),
-0.0006511369998784711)
assert np.allclose(get_dipole_temperature(tcmb, solsysspeed, [0, 0, 1], 44e9),
-0.0006511328936482864)
assert np.allclose(get_dipole_temperature(tcmb, solsysspeed, [0, 0, 1], 70e9),
-0.0006511213786323892)
assert np.allclose(get_dipole_temperature(tcmb, solsysspeed, [0, 0, 1], 143e9),
-0.0006510659240198485)
def precondition_check(self, precObj):
ofs_gains_guess = OfsAndGains(offsets=np.zeros(2),
gains=np.ones(2),
samples_per_ofsp=self.ofs_and_gains.samples_per_ofsp,
samples_per_gainp=self.ofs_and_gains.samples_per_gainp)
P, F, ptilde, M, Z, A = build_test_matrices(len(self.bext),
self.num_of_pixels, self.pix_idx,
self.mc, self.D,
ofs_gains_guess)
if precObj:
pcond = precObj(mc=self.mon_and_dip,
pix_idx=self.pix_idx,
samples_per_ofsp=self.ofs_and_gains.samples_per_ofsp,
samples_per_gainp=self.ofs_and_gains.samples_per_gainp)
else:
pcond = None
cg_a, _ = conjugate_gradient(None, self.tod, ofs_gains_guess,
np.zeros_like(self.D),
self.pix_idx, self.mon_and_dip, pcond=pcond)
check_vector_match('conjugate_gradient',
np.linalg.inv(A) @ F.T @ Z @ self.tod, cg_a.a_vec)
result = da_capo(mpi_comm=None,
voltages=self.tod,
pix_idx=self.pix_idx,
samples_per_ofsp=self.ofs_and_gains.samples_per_ofsp,
samples_per_gainp=self.ofs_and_gains.samples_per_gainp,
mc=self.mon_and_dip,
pcond=pcond)
check_vector_match('da_capo (offsets)',
self.ofs_and_gains.offsets, result.ofs_and_gains.offsets)
check_vector_match('da_capo (gains)',
self.ofs_and_gains.gains, result.ofs_and_gains.gains)
check_vector_match('da_capo (map)',
self.sky_map, result.sky_map)
def testNoPreconditioner(self):
self.precondition_check(None)
def testJacobiPreconditioner(self):
self.precondition_check(JacobiPreconditioner)
def testFullPreconditioner(self):
self.precondition_check(FullPreconditioner)
class TestMiscellanea(ut.TestCase):
def testComputeRMS(self):
chunks = [40000, 70000, 50000]
samples = np.random.randn(np.sum(chunks))
check_vector_match('compute_rms',
np.repeat(1.0, len(chunks)),
compute_rms(samples, chunks),
rtol=5e-2)