-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrate.py
executable file
·1076 lines (876 loc) · 43.6 KB
/
calibrate.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import io
import sys
from copy import copy
from collections import namedtuple
from configparser import ConfigParser
from datetime import datetime
import click
from typing import List, Any
import numpy as np
import scipy
import healpy
import logging as log
from numba import jit
from index import int_or_str, flag_mask, TODFileInfo, IndexFile
from astropy.io import fits
from mpi4py import MPI
import ftnroutines
__version__ = '1.1.1'
class Profiler:
def __init__(self):
self.start_time = None
self.tic()
def tic(self):
'''Record the current time.'''
self.start_time = datetime.now()
def toc(self):
'''Return the elapsed time (in seconds) since the last call to tic/toc.'''
now = datetime.now()
diff = now - self.start_time
self.start_time = now
return diff.total_seconds()
def gather_arrays(mpi_comm, array: Any, root=0) -> Any:
lengths = mpi_comm.gather(len(array), root=root)
if mpi_comm.Get_rank() == root:
recvbuf = np.empty(sum(lengths), dtype=array.dtype)
else:
recvbuf = None
mpi_comm.Gatherv(sendbuf=array, recvbuf=(recvbuf, lengths), root=root)
return recvbuf
CalibrateConfiguration = namedtuple('CalibrateConfiguration',
['index_file',
'first_tod_index', 'last_tod_index',
'signal_hdu', 'signal_column',
'pointing_hdu', 'pointing_columns',
't_cmb_k', 'solsys_speed_vec_m_s',
'frequency_hz',
'nside', 'mask_file_path',
'periods_per_cal_constant',
'cg_stop', 'cg_maxiter',
'dacapo_stop', 'dacapo_maxiter',
'pcond', 'output_file_name', 'save_map',
'save_convergence',
'comment',
'parameter_file_contents'])
class OfsAndGains:
def __init__(self, offsets, gains, samples_per_ofsp, samples_per_gainp):
self.a_vec = np.concatenate((offsets, gains))
self.samples_per_ofsp = np.array(samples_per_ofsp, dtype='int')
self.samples_per_gainp = np.array(samples_per_gainp, dtype='int')
self.ofsp_per_gainp = OfsAndGains.calc_ofsp_per_gainp(samples_per_ofsp,
samples_per_gainp)
def __copy__(self):
return OfsAndGains(offsets=np.copy(self.offsets),
gains=np.copy(self.gains),
samples_per_ofsp=np.copy(self.samples_per_ofsp),
samples_per_gainp=np.copy(self.samples_per_gainp))
def __repr__(self):
return 'a: {0} (offsets: {1}, gains: {2})'.format(self.a_vec,
self.offsets,
self.gains)
@property
def offsets(self):
return self.a_vec[0:len(self.samples_per_ofsp)]
@property
def gains(self):
return self.a_vec[len(self.samples_per_ofsp):]
@staticmethod
def calc_ofsp_per_gainp(samples_per_ofsp, samples_per_gainp):
log.debug('entering calc_ofsp_per_gainp')
ofsp_per_gainp = []
cur_ofsp_idx = 0
for samples_in_cur_gainp in samples_per_gainp:
ofsp_in_cur_gainp = 0
sample_count = 0
while sample_count < samples_in_cur_gainp:
sample_count += samples_per_ofsp[cur_ofsp_idx]
cur_ofsp_idx += 1
ofsp_in_cur_gainp += 1
assert sample_count == samples_in_cur_gainp
ofsp_per_gainp.append(ofsp_in_cur_gainp)
return ofsp_per_gainp
def ofs_and_gains_with_same_lengths(source: OfsAndGains, a_vec):
result = copy(source)
result.a_vec = np.copy(a_vec)
return result
class MonopoleAndDipole:
def __init__(self, mask, dipole_map):
if mask is not None:
self.monopole_map = np.array(mask, dtype='float')
else:
self.monopole_map = np.ones_like(dipole_map)
self.dipole_map = np.array(dipole_map) * np.array(self.monopole_map)
def split_into_n(length: int, num_of_segments: int):
log.debug('entering split_into_n')
assert (num_of_segments > 0), \
"num_of_segments={0} is not positive".format(num_of_segments)
assert (length >= num_of_segments), \
"length={0} is smaller than num_of_segments={1}".format(
length, num_of_segments)
start_positions = np.array([int(i * length / num_of_segments)
for i in range(num_of_segments + 1)],
dtype='int')
return start_positions[1:] - start_positions[0:-1]
def split(length, sublength: int):
log.debug('entering split')
assert (sublength > 0), "sublength={0} is not positive".format(sublength)
assert (sublength < length), \
"sublength={0} is not smaller than length={1}".format(
sublength, length)
return split_into_n(length=length,
num_of_segments=int(np.ceil(length / sublength)))
class TOD:
def __init__(self, signal, pix_idx, num_of_pixels):
self.signal = signal
self.pix_idx = pix_idx
self.num_of_pixels = num_of_pixels
TODSubrange = namedtuple('TODSubrange',
['file_info',
'first_idx',
'num_of_samples'])
def assign_files_to_processes(samples_per_process: Any,
tod_info: List[TODFileInfo]) -> List[List[TODSubrange]]:
log.debug('entering assign_files_to_processes')
result = [] # type: List[List[TODSubrange]]
file_idx = 0
file_sample_idx = 0
samples_in_file = tod_info[file_idx].num_of_unflagged_samples
for samples_for_this_MPI_proc in samples_per_process:
samples_left = samples_for_this_MPI_proc
MPI_proc_subranges = []
while (samples_left > 0) and (file_idx < len(tod_info)):
if samples_in_file > samples_left:
MPI_proc_subranges.append(TODSubrange(file_info=tod_info[file_idx],
first_idx=file_sample_idx,
num_of_samples=samples_left))
file_sample_idx += samples_left
samples_in_file -= samples_left
samples_left = 0
else:
MPI_proc_subranges.append(TODSubrange(file_info=tod_info[file_idx],
first_idx=file_sample_idx,
num_of_samples=samples_in_file))
samples_left -= samples_in_file
samples_in_file = 0
if samples_in_file == 0:
if file_idx + 1 == len(tod_info):
break # No more files, exit the while loop
file_sample_idx = 0
file_idx += 1
samples_in_file = tod_info[file_idx].num_of_unflagged_samples
result.append(MPI_proc_subranges)
return result
def load_subrange(subrange: TODSubrange,
index: IndexFile,
configuration: CalibrateConfiguration) -> TOD:
log.debug('entering load_subrange')
with fits.open(subrange.file_info.file_name) as f:
signal = f[configuration.signal_hdu].data.field(
configuration.signal_column)
if len(signal) != subrange.file_info.num_of_samples:
log.error('expected %d samples in file "%s", but %d found: '
'you should rebuild the index file',
subrange.file_info.num_of_samples,
subrange.file_info.file_name,
len(signal))
sys.exit(1)
pix_idx = [f[configuration.pointing_hdu].data.field(x)
for x in configuration.pointing_columns]
if len(pix_idx) == 2:
theta, phi = pix_idx
pix_idx = healpy.ang2pix(configuration.nside, theta, phi)
del theta, phi
elif len(configuration.pointing_columns) == 1:
pix_idx = pix_idx[0]
else:
log.error('one or two columns are expected for the pointings (got %s)',
', '.join([str(x) for x in configuration.pointing_columns]))
sys.exit(1)
if index.flagging is not None:
flags = f[index.flag_hdu].data.field(index.flag_column)
mask = flag_mask(flags, index.flagging)
signal = signal[mask]
pix_idx = pix_idx[mask]
if len(signal) != subrange.file_info.num_of_unflagged_samples:
log.error('expected %d unflagged samples in file "%s", but %d found: '
'you should rebuild the index file',
subrange.file_info.num_of_unflagged_samples,
subrange.file_info.file_name,
len(signal))
sys.exit(1)
start, end = (subrange.first_idx,
(subrange.first_idx + subrange.num_of_samples))
signal = signal[start:end]
pix_idx = pix_idx[start:end]
return TOD(signal=signal, pix_idx=pix_idx,
num_of_pixels=healpy.nside2npix(configuration.nside))
def load_tod(tod_list: List[TODSubrange],
index: IndexFile,
configuration: CalibrateConfiguration) -> TOD:
log.debug('entering load_tod')
result = TOD(signal=np.array([], dtype='float'),
pix_idx=np.array([], dtype='int'),
num_of_pixels=healpy.nside2npix(configuration.nside))
for cur_subrange in tod_list:
cur_tod = load_subrange(cur_subrange, index, configuration)
result.signal = np.concatenate((result.signal, cur_tod.signal))
result.pix_idx = np.concatenate((result.pix_idx, cur_tod.pix_idx))
return result
def read_calibrate_conf_file(file_name: str) -> CalibrateConfiguration:
log.debug('entering read_calibrate_conf_file')
conf_file = ConfigParser()
conf_file.read(file_name)
try:
input_sect = conf_file['input_files']
index_file = input_sect.get('index_file', None)
first_tod_index = input_sect.get('first_tod_index', -1)
last_tod_index = input_sect.get('last_tod_index', -1)
signal_hdu = int_or_str(input_sect.get('signal_hdu'))
signal_column = int_or_str(input_sect.get('signal_column'))
pointing_hdu = int_or_str(input_sect.get('pointing_hdu'))
pointing_columns = [int_or_str(x.strip())
for x in input_sect.get('pointing_columns').split(',')]
dacapo_sect = conf_file['dacapo']
t_cmb_k = dacapo_sect.getfloat('t_cmb_k')
solsysdir_ecl_colat_rad = dacapo_sect.getfloat(
'solsysdir_ecl_colat_rad')
solsysdir_ecl_long_rad = dacapo_sect.getfloat('solsysdir_ecl_long_rad')
solsysspeed_m_s = dacapo_sect.getfloat('solsysspeed_m_s')
solsys_speed_vec_m_s = solsysspeed_m_s * \
np.array([np.sin(solsysdir_ecl_colat_rad) * np.cos(solsysdir_ecl_long_rad),
np.sin(solsysdir_ecl_colat_rad) *
np.sin(solsysdir_ecl_long_rad),
np.cos(solsysdir_ecl_colat_rad)])
freq_str = dacapo_sect.get('frequency_hz', fallback='none')
if freq_str.lower() in ['', 'none', 'nan', 'no']:
frequency_hz = None
else:
frequency_hz = float(freq_str)
nside = dacapo_sect.getint('nside')
mask_file_path = dacapo_sect.get('mask', fallback=None)
if mask_file_path.strip() == '':
mask_file_path = None
periods_per_cal_constant = dacapo_sect.getint(
'periods_per_cal_constant')
cg_stop = dacapo_sect.getfloat('cg_stop_value', 1e-9)
cg_maxiter = dacapo_sect.getint('cg_max_iterations', 100)
dacapo_stop = dacapo_sect.getfloat('dacapo_stop_value', 1e-9)
dacapo_maxiter = dacapo_sect.getint('dacapo_max_iterations', 20)
pcond = dacapo_sect.get('pcond').lower()
try:
if not healpy.isnsideok(nside):
raise ValueError('invalid NSIDE = {0}'.format(nside))
if cg_stop < 0.0:
raise ValueError('cg_stop_value ({0:.3e}) should not be negative'
.format(cg_stop))
if dacapo_stop < 0:
raise ValueError('dacapo_stop_value ({0:.3e}) should not be negative'
.format(dacapo_stop))
if periods_per_cal_constant < 1:
raise ValueError('periods_per_cal_constant ({0}) should be greater than zero'
.format(periods_per_cal_constant))
if cg_maxiter < 0:
raise ValueError(
'cg_maxiter (%d) cannot be negative'.format(cg_maxiter))
if dacapo_maxiter < 0:
raise ValueError(
'dacapo_maxiter (%d) cannot be negative'.format(dacapo_maxiter))
except ValueError as e:
log.error(e)
sys.exit(1)
output_sect = conf_file['output']
output_file_name = output_sect.get('file_name')
save_map = output_sect.getboolean('save_map', fallback=True)
save_convergence = output_sect.getboolean(
'save_convergence_information', fallback=True)
comment = output_sect.get('comment', fallback=None)
except ValueError as e:
log.error('invalid value found in one of the entries in "%s": %s',
file_name, e)
param_file_contents = io.StringIO()
conf_file.write(param_file_contents)
param_file_contents = np.array(
list(param_file_contents.getvalue().encode('utf-8')))
return CalibrateConfiguration(index_file=index_file,
first_tod_index=first_tod_index,
last_tod_index=last_tod_index,
signal_hdu=signal_hdu,
signal_column=signal_column,
pointing_hdu=pointing_hdu,
pointing_columns=pointing_columns,
t_cmb_k=t_cmb_k,
solsys_speed_vec_m_s=solsys_speed_vec_m_s,
frequency_hz=frequency_hz,
nside=nside,
mask_file_path=mask_file_path,
periods_per_cal_constant=periods_per_cal_constant,
cg_stop=cg_stop,
cg_maxiter=cg_maxiter,
dacapo_stop=dacapo_stop,
dacapo_maxiter=dacapo_maxiter,
pcond=pcond,
output_file_name=output_file_name,
save_map=save_map,
save_convergence=save_convergence,
comment=comment,
parameter_file_contents=param_file_contents)
SPEED_OF_LIGHT_M_S = 2.99792458e8
PLANCK_H_MKS = 6.62606896e-34
BOLTZMANN_K_MKS = 1.3806504e-23
def get_dipole_temperature(t_cmb_k: float, solsys_speed_vec_m_s, directions, freq=None):
'''Given one or more one-length versors, return the intensity of the CMB dipole
The vectors must be expressed in the Ecliptic coordinate system.
If "freq" (frequency in Hz) is specified, the formulation will use the
quadrupolar correction.
'''
log.debug('entering get_dipole_temperature')
beta = solsys_speed_vec_m_s / SPEED_OF_LIGHT_M_S
if freq:
fact = PLANCK_H_MKS * freq / (BOLTZMANN_K_MKS * t_cmb_k)
expfact = np.exp(fact)
q = (fact / 2) * (expfact + 1) / (expfact - 1)
dotprod = np.dot(beta, directions)
return t_cmb_k * (dotprod + q * dotprod**2)
else:
gamma = (1 - np.dot(beta, beta))**(-0.5)
return t_cmb_k * (1.0 / (gamma * (1 - np.dot(beta, directions))) - 1.0)
def apply_f(a: OfsAndGains, pix_idx, dipole_map, sky_map):
log.debug('entering apply_f')
return ftnroutines.apply_f(a.offsets, a.gains,
a.samples_per_ofsp, a.samples_per_gainp,
pix_idx, dipole_map, sky_map)
def apply_ft(vector, a: OfsAndGains, pix_idx, dipole_map, sky_map):
log.debug('entering apply_ft')
return ftnroutines.apply_ft(vector, a.offsets, a.gains,
a.samples_per_ofsp, a.samples_per_gainp,
pix_idx, dipole_map, sky_map)
def sum_local_results(mpi_comm, function, **arguments):
log.debug('entering sum_local_results')
result = function(**arguments)
if mpi_comm:
totals = np.zeros_like(result)
mpi_comm.Allreduce(sendbuf=result, recvbuf=totals, op=MPI.SUM)
return totals
else:
return result
def compute_diagm_locally(a: OfsAndGains, pix_idx, num_of_pixels: int):
log.debug('entering compute_diagm_locally')
result = np.empty(num_of_pixels, dtype='float')
ftnroutines.compute_diagm_locally(
a.gains, a.samples_per_gainp, pix_idx, result)
return result
def compute_diagm(mpi_comm, a: OfsAndGains, pix_idx, num_of_pixels: int):
log.debug('entering compute_diagm')
return sum_local_results(mpi_comm, function=compute_diagm_locally,
a=a,
pix_idx=pix_idx,
num_of_pixels=num_of_pixels)
def apply_ptilde(map_pixels, a: OfsAndGains, pix_idx):
log.debug('entering apply_ptilde')
return ftnroutines.apply_ptilde(map_pixels, a.gains, a.samples_per_gainp, pix_idx)
def apply_ptildet_locally(vector, a: OfsAndGains, pix_idx, num_of_pixels: int):
log.debug('entering apply_ptildet_locally')
result = np.empty(num_of_pixels, dtype='float')
ftnroutines.apply_ptildet_locally(vector, a.gains,
a.samples_per_gainp,
pix_idx, result)
return result
def apply_ptildet(mpi_comm, vector, a: OfsAndGains, pix_idx,
num_of_pixels: int):
log.debug('entering apply_ptildet')
return sum_local_results(mpi_comm, function=apply_ptildet_locally,
vector=vector,
a=a,
pix_idx=pix_idx,
num_of_pixels=num_of_pixels)
def apply_z(mpi_comm, vector, a: OfsAndGains, pix_idx, mc: MonopoleAndDipole):
log.debug('entering apply_z')
binned_map = apply_ptildet(mpi_comm, vector, a, pix_idx,
len(mc.dipole_map))
diagM = compute_diagm(mpi_comm, a, pix_idx, len(mc.dipole_map))
nonzero_hit_mask = diagM != 0
inv_diagM = np.zeros_like(diagM)
inv_diagM[nonzero_hit_mask] = 1.0 / diagM[nonzero_hit_mask]
binned_map = np.multiply(binned_map, inv_diagM)
monopole_dot = np.dot(mc.monopole_map, binned_map)
dipole_dot = np.dot(mc.dipole_map, binned_map)
# Compute (m_c^T M^-1 m_c)
small_matr = np.array([[np.dot(mc.dipole_map,
np.multiply(inv_diagM, mc.dipole_map)),
np.dot(mc.dipole_map, inv_diagM)],
[np.dot(mc.monopole_map,
np.multiply(inv_diagM, mc.dipole_map)),
np.dot(mc.monopole_map, inv_diagM)]])
small_matr_prod = np.linalg.inv(small_matr) @ np.array([dipole_dot,
monopole_dot])
ftnroutines.clean_binned_map(inv_diagM, mc.dipole_map, mc.monopole_map,
small_matr_prod, binned_map)
return vector - apply_ptilde(binned_map, a, pix_idx)
def apply_A(mpi_comm, a: OfsAndGains, sky_map, pix_idx,
mc: MonopoleAndDipole, x: OfsAndGains):
log.debug('entering apply_A')
vector1 = apply_f(x, pix_idx, mc.dipole_map, sky_map)
vector2 = apply_z(mpi_comm, vector1, a, pix_idx, mc)
return apply_ft(vector2, a, pix_idx, mc.dipole_map, sky_map)
def compute_v(mpi_comm, voltages, a: OfsAndGains, sky_map, pix_idx,
mc: MonopoleAndDipole):
log.debug('entering compute_v')
vector = apply_z(mpi_comm, voltages, a, pix_idx, mc)
return apply_ft(vector, a, pix_idx, mc.dipole_map, sky_map)
def mpi_dot_prod(mpi_comm, x, y):
log.debug('entering mpi_dot_prod')
local_sum = np.dot(x, y)
if mpi_comm is not None:
return mpi_comm.allreduce(local_sum, op=MPI.SUM)
else:
return local_sum
def conjugate_gradient(mpi_comm, voltages, start_a: OfsAndGains, sky_map,
pix_idx, mc: MonopoleAndDipole, pcond=None,
threshold=1e-9, max_iter=100):
log.debug('entering conjugate_gradient')
a = copy(start_a)
residual = (compute_v(mpi_comm, voltages, a, sky_map, pix_idx, mc) -
apply_A(mpi_comm, a, sky_map, pix_idx, mc, a))
r = ofs_and_gains_with_same_lengths(source=start_a,
a_vec=residual)
k = 0
list_of_stopping_factors = []
stopping_factor = np.sqrt(mpi_dot_prod(mpi_comm, r.a_vec, r.a_vec))
log.info('conjugate_gradient: iteration %d/%d, stopping criterion: %.5e',
k, max_iter, stopping_factor)
list_of_stopping_factors.append(stopping_factor)
if stopping_factor < threshold:
return a, list_of_stopping_factors
if pcond is not None:
z = pcond.apply_to(r)
else:
z = copy(r)
old_r_dot = mpi_dot_prod(mpi_comm, z.a_vec, r.a_vec)
p = copy(z)
best_stopping_factor = stopping_factor
best_a = a
while True:
k += 1
if k >= max_iter:
return best_a, list_of_stopping_factors
Ap = apply_A(mpi_comm, a, sky_map, pix_idx, mc, p)
gamma = old_r_dot / mpi_dot_prod(mpi_comm, p.a_vec, Ap)
a.a_vec += gamma * p.a_vec
r.a_vec -= gamma * Ap
stopping_factor = np.sqrt(mpi_dot_prod(mpi_comm, r.a_vec, r.a_vec))
log.info('conjugate_gradient: iteration %d/%d, stopping criterion: %.5e',
k, max_iter, stopping_factor)
list_of_stopping_factors.append(stopping_factor)
if stopping_factor < threshold:
return a, list_of_stopping_factors
if stopping_factor < best_stopping_factor:
best_stopping_factor, best_a = stopping_factor, copy(a)
if pcond is not None:
z = pcond.apply_to(r)
else:
z = copy(r)
new_r_dot = mpi_dot_prod(mpi_comm, z.a_vec, r.a_vec)
p.a_vec = z.a_vec + (new_r_dot / old_r_dot) * p.a_vec
old_r_dot = new_r_dot
@jit
def compute_rms(signal: Any, samples_per_period: List[int]) -> Any:
result = np.empty(len(samples_per_period))
start_idx = 0
for i, cur_samples in enumerate(samples_per_period):
subarray = signal[start_idx:start_idx + cur_samples]
if cur_samples % 2 > 0:
result[i] = 0.5 * (np.var(subarray[1::2] - subarray[0:-1:2]))
else:
result[i] = 0.5 * (np.var(subarray[1::2] - subarray[0::2]))
start_idx += cur_samples
return result
class FullPreconditioner:
def __init__(self, mc: MonopoleAndDipole, pix_idx,
samples_per_ofsp, samples_per_gainp):
assert sum(samples_per_ofsp) == len(pix_idx)
self.samples_per_ofsp = samples_per_ofsp
self.samples_per_gainp = samples_per_gainp
self.ofsp_per_gainp = \
OfsAndGains.calc_ofsp_per_gainp(
samples_per_ofsp, samples_per_gainp)
assert sum(self.ofsp_per_gainp) == len(self.samples_per_ofsp)
self.matrices = []
cur_ofsp_idx = 0
cur_sample_idx = 0
for ofsp_in_cur_gainp in self.ofsp_per_gainp:
cur_matrix = np.zeros((ofsp_in_cur_gainp + 1,
ofsp_in_cur_gainp + 1))
first_sample = cur_sample_idx
for i, cur_ofsp in enumerate(samples_per_ofsp[cur_ofsp_idx:(cur_ofsp_idx +
ofsp_in_cur_gainp)]):
cur_monopole = mc.monopole_map[pix_idx[cur_sample_idx:(
cur_sample_idx + cur_ofsp)]]
cur_dipole = mc.dipole_map[pix_idx[cur_sample_idx:(
cur_sample_idx + cur_ofsp)]]
cur_matrix[i, i] = np.sum(cur_monopole)
cur_matrix[ofsp_in_cur_gainp, i] = cur_matrix[i, ofsp_in_cur_gainp] = \
np.sum(cur_dipole)
cur_sample_idx += cur_ofsp
cur_matrix[ofsp_in_cur_gainp, ofsp_in_cur_gainp] = \
np.sum(mc.dipole_map[pix_idx[first_sample:cur_sample_idx]]**2)
# If the determinant is not positive, the matrix is not positive definite!
assert np.linalg.det(cur_matrix) > 0
self.matrices.append(np.linalg.inv(cur_matrix))
def apply_to(self, a: OfsAndGains) -> OfsAndGains:
cur_ofsp_idx = 0
cur_gainp_idx = 0
a_vec = np.empty(len(a.a_vec))
result = ofs_and_gains_with_same_lengths(a, a_vec)
gains = a.gains
offsets = a.offsets
for cur_gainp_idx, num_of_ofsp in enumerate(self.ofsp_per_gainp):
# y = M^-1 x for each block in F^T F
x = np.empty(num_of_ofsp + 1)
x[0:num_of_ofsp] = offsets[cur_ofsp_idx:(
cur_ofsp_idx + num_of_ofsp)]
x[num_of_ofsp] = gains[cur_gainp_idx]
y = self.matrices[cur_gainp_idx] @ x
result.offsets[cur_ofsp_idx:(
cur_ofsp_idx + num_of_ofsp)] = y[0:num_of_ofsp]
result.gains[cur_gainp_idx] = y[num_of_ofsp]
cur_ofsp_idx += num_of_ofsp
cur_gainp_idx += 1
return result
def compute_offset_errors(self, voltages, samples_per_ofsp):
rms = compute_rms(voltages, samples_per_ofsp)
return np.sqrt(rms * np.array([np.diag(x)[:-1] for x in self.matrices]).flatten())
def compute_gain_errors(self, voltages, samples_per_gainp):
rms = compute_rms(voltages, samples_per_gainp)
return np.sqrt(rms * np.array([np.diag(x)[-1] for x in self.matrices]))
class JacobiPreconditioner:
def __init__(self, mc: MonopoleAndDipole, pix_idx,
samples_per_ofsp, samples_per_gainp):
self.diagonal = OfsAndGains(offsets=np.zeros(len(samples_per_ofsp)),
gains=np.zeros(len(samples_per_gainp)),
samples_per_ofsp=samples_per_ofsp,
samples_per_gainp=samples_per_gainp)
cur_sample_idx = 0
offsets = self.diagonal.offsets
for row_idx, cur_sample_num in enumerate(samples_per_ofsp):
cur_sum = np.sum(mc.monopole_map[pix_idx[cur_sample_idx:(cur_sample_idx +
cur_sample_num)]])
if cur_sum != 0.0:
offsets[row_idx] = 1.0 / cur_sum
else:
offsets[row_idx] = 1.0
cur_sample_idx += cur_sample_num
cur_sample_idx = 0
gains = self.diagonal.gains
for row_idx, cur_sample_num in enumerate(samples_per_gainp):
cur_sum = np.sum(mc.dipole_map[pix_idx[cur_sample_idx:(cur_sample_idx +
cur_sample_num)]]**2)
if cur_sum != 0.0:
gains[row_idx] = 1.0 / cur_sum
else:
gains[row_idx] = 1.0
cur_sample_idx += cur_sample_num
def apply_to(self, a: OfsAndGains) -> OfsAndGains:
return ofs_and_gains_with_same_lengths(a, a.a_vec * self.diagonal.a_vec)
def compute_offset_errors(self, voltages, samples_per_ofsp):
rms = compute_rms(voltages, samples_per_ofsp)
return np.sqrt(rms * self.diagonal.offsets)
def compute_gain_errors(self, voltages, samples_per_gainp):
rms = compute_rms(voltages, samples_per_gainp)
return np.sqrt(rms * self.diagonal.gains)
PCOND_DICT = {'none': None,
'full': FullPreconditioner,
'jacobi': JacobiPreconditioner}
def guess_gains(voltages, pix_idx, dipole_map, samples_per_gainp):
log.debug('entering guess_gains')
result = np.empty(len(samples_per_gainp), dtype='float')
cal_start = 0
for gainp_idx, gainp_len in enumerate(samples_per_gainp):
cal_end = cal_start + gainp_len
cur_fit = scipy.polyfit(x=dipole_map[pix_idx[cal_start:cal_end]],
y=voltages[cal_start:cal_end],
deg=1)
result[gainp_idx] = cur_fit[0]
cal_start += gainp_len
return result
DaCapoResults = namedtuple('DaCapoResults',
['ofs_and_gains',
'sky_map',
'list_of_cg_rz',
'list_of_dacapo_rz',
'cg_wall_times',
'converged',
'dacapo_wall_time'])
def da_capo(mpi_comm, voltages, pix_idx, samples_per_ofsp, samples_per_gainp,
mc: MonopoleAndDipole, mask=None, pcond=None, threshold=1e-9, max_iter=10,
cg_threshold=1e-9, max_cg_iter=100) -> DaCapoResults:
log.debug('entering da_capo')
dacapo_prof = Profiler()
sky_map = np.zeros_like(mc.dipole_map)
start_gains = guess_gains(
voltages, pix_idx, mc.dipole_map, samples_per_gainp)
old_a = OfsAndGains(offsets=np.zeros(len(samples_per_ofsp)),
gains=start_gains,
samples_per_ofsp=samples_per_ofsp,
samples_per_gainp=samples_per_gainp)
iteration = 0
cg_wall_times = []
list_of_cg_rz = []
list_of_dacapo_rz = []
while True:
log.info('da_capo: iteration %d/%d', iteration + 1, max_iter)
cg_prof = Profiler()
new_a, rz = conjugate_gradient(mpi_comm, voltages, old_a, sky_map, pix_idx,
mc, pcond=pcond, threshold=cg_threshold,
max_iter=max_cg_iter)
list_of_cg_rz.append(rz)
cg_wall_times.append(cg_prof.toc())
sky_map_corr = compute_map_corr(mpi_comm, voltages, old_a, new_a,
pix_idx, mc.dipole_map, sky_map)
sky_map += sky_map_corr
stopping_factor = mpi_abs_max(mpi_comm, new_a.a_vec - old_a.a_vec)
list_of_dacapo_rz.append(stopping_factor)
log.info('da_capo: stopping factor %.3e (threshold is %.3e)',
stopping_factor, threshold)
if stopping_factor < threshold:
log.info('da_capo: convergence reached after %d steps', iteration)
return DaCapoResults(ofs_and_gains=new_a,
sky_map=sky_map,
list_of_cg_rz=list_of_cg_rz,
list_of_dacapo_rz=list_of_dacapo_rz,
converged=True,
cg_wall_times=cg_wall_times,
dacapo_wall_time=dacapo_prof.toc())
old_a = new_a
iteration += 1
if iteration >= max_iter:
log.info('da_capo: maximum number of iterations reached (%d)', max_iter)
return DaCapoResults(ofs_and_gains=new_a,
sky_map=sky_map,
list_of_cg_rz=list_of_cg_rz,
list_of_dacapo_rz=list_of_dacapo_rz,
converged=False,
cg_wall_times=cg_wall_times,
dacapo_wall_time=dacapo_prof.toc())
def mpi_abs_max(mpi_comm, vec):
log.debug('entering mpi_abs_max')
local_max = np.max(np.abs(vec))
if mpi_comm is not None:
return mpi_comm.allreduce(local_max, op=MPI.MAX)
else:
return local_max
def compute_map_corr(mpi_comm, voltages, old_a: OfsAndGains, new_a: OfsAndGains,
pix_idx, dipole_map, sky_map):
log.debug('entering compute_map_corr')
diff_tod = voltages - apply_f(new_a, pix_idx, dipole_map, sky_map)
map_corr = apply_ptildet(mpi_comm, diff_tod, old_a,
pix_idx, len(dipole_map))
normalization = compute_diagm(mpi_comm, old_a, pix_idx, len(dipole_map))
result = np.ma.array(map_corr, mask=(np.abs(normalization) < 1e-9),
fill_value=0.0)
return (result / normalization).filled()
DEFAULT_LOGFILE_MASK = 'calibrate_%04d.log'
@click.command()
@click.argument('configuration_file')
@click.option('--debug/--no-debug', 'debug_flag',
help='Print more debugging information during the execution')
@click.option('--full-log/--no-full-log', 'full_log_flag',
help='Make every MPI process write log message to files'
' (use --logfile to specify the file name)')
@click.option('-i', '--index-file', 'indexfile_path', default=None, type=str,
help='Specify the path to the index file to use '
'(overrides the one specified in the parameter file).')
@click.option('--logfile', 'logfile_mask', default=DEFAULT_LOGFILE_MASK,
help='Prints (a subset of) logging messages on the screen'
' (default is "{0}")'.format(DEFAULT_LOGFILE_MASK))
def calibrate_main(configuration_file: str, debug_flag: bool,
full_log_flag: bool, logfile_mask: str,
indexfile_path: str):
mpi_comm = MPI.COMM_WORLD
mpi_size = mpi_comm.Get_size()
mpi_rank = mpi_comm.Get_rank()
if debug_flag:
log_level = log.DEBUG
else:
log_level = log.INFO
log_format = '[%(asctime)s %(levelname)s MPI#{0:04d}] %(message)s'.format(
mpi_rank)
if full_log_flag:
log.basicConfig(level=log_level, filename=(logfile_mask % mpi_rank),
filemode='w', format=log_format)
else:
if mpi_rank == 0:
log.basicConfig(level=log_level, format=log_format)
else:
log.basicConfig(level=log.CRITICAL)
log.info('reading configuration file "%s"', configuration_file)
configuration = read_calibrate_conf_file(configuration_file)
log.info('configuration file read successfully')
if indexfile_path is not None:
configuration.index_file = indexfile_path
if configuration.index_file is None:
log.error('error: you must specify an index file, either in the '
'parameter file or using the --index-file switch')
sys.exit(1)
index = IndexFile()
index.load_from_fits(configuration.index_file,
first_idx=configuration.first_tod_index,
last_idx=configuration.last_tod_index)
log.info('%d files are going to be loaded', len(index.tod_info))
samples_per_ofsp = index.periods
gainp_lengths = split(length=len(samples_per_ofsp),
sublength=configuration.periods_per_cal_constant)
samples_per_gainp = ftnroutines.sum_subranges(samples_per_ofsp,
gainp_lengths)
log.info('number of offset periods: %d; number of gain periods: %d',
len(samples_per_ofsp), len(gainp_lengths))
gainp_per_process = split_into_n(length=len(samples_per_gainp),
num_of_segments=mpi_size)
samples_per_process = ftnroutines.sum_subranges(samples_per_gainp,
gainp_per_process)
gainp_idx_start = sum(gainp_per_process[0:mpi_rank])
gainp_idx_end = gainp_idx_start + gainp_per_process[mpi_rank]
ofsp_idx_start = sum(gainp_lengths[0:gainp_idx_start])
ofsp_idx_end = ofsp_idx_start + \
sum(gainp_lengths[gainp_idx_start:gainp_idx_end])
local_samples_per_ofsp = samples_per_ofsp[ofsp_idx_start:ofsp_idx_end]
local_samples_per_gainp = samples_per_gainp[gainp_idx_start:gainp_idx_end]
files_per_process = assign_files_to_processes(
samples_per_process, index.tod_info)
tod = load_tod(files_per_process[mpi_rank], index, configuration)
assert len(tod.signal) == sum(local_samples_per_ofsp)
assert len(tod.signal) == sum(local_samples_per_gainp)
overall_num_of_samples = mpi_comm.allreduce(len(tod.signal), op=MPI.SUM)
log.info('elements in the TOD: %d (split among %d processes)',
overall_num_of_samples, mpi_size)
if configuration.mask_file_path is not None:
mask = healpy.read_map(configuration.mask_file_path, verbose=False)
mask = np.array(healpy.ud_grade(
mask, configuration.nside), dtype='int')
else:
mask = None
directions = healpy.pix2vec(configuration.nside,
np.arange(healpy.nside2npix(configuration.nside)))
dipole_map = get_dipole_temperature(t_cmb_k=configuration.t_cmb_k,
solsys_speed_vec_m_s=configuration.solsys_speed_vec_m_s,
directions=directions,
freq=configuration.frequency_hz)
mc = MonopoleAndDipole(mask=mask, dipole_map=dipole_map)
try:
pcond_class = PCOND_DICT[configuration.pcond]
except KeyError:
log.error('Unknown preconditioner "%s", valid choices are: %s',
configuration.pcond,
', '.join(['"{0}"'.format(x) for x in PCOND_DICT.keys()]))
sys.exit(1)
if pcond_class is not None:
pcond = pcond_class(mc=mc,
pix_idx=tod.pix_idx,
samples_per_ofsp=local_samples_per_ofsp,
samples_per_gainp=local_samples_per_gainp)
else:
pcond = None
da_capo_results = da_capo(mpi_comm,
voltages=tod.signal, pix_idx=tod.pix_idx,
samples_per_ofsp=local_samples_per_ofsp,
samples_per_gainp=local_samples_per_gainp,
mc=mc,
mask=mask,
threshold=configuration.dacapo_stop,
max_iter=configuration.dacapo_maxiter,
max_cg_iter=configuration.cg_maxiter,
cg_threshold=configuration.cg_stop,
pcond=pcond)
coll_gains = gather_arrays(mpi_comm, da_capo_results.ofs_and_gains.gains)
coll_offsets = gather_arrays(
mpi_comm, da_capo_results.ofs_and_gains.offsets)
if pcond is not None:
coll_gain_errors = gather_arrays(mpi_comm,
pcond.compute_gain_errors(tod.signal,
local_samples_per_gainp))
coll_offset_errors = gather_arrays(mpi_comm,
pcond.compute_offset_errors(tod.signal,
local_samples_per_ofsp))
else:
log.warning(
'no preconditioner used, all offset/gain errors will be set to zero')
coll_offset_errors = np.zeros_like(coll_offsets)
coll_gain_errors = np.zeros_like(coll_gains)
if mpi_rank == 0:
primary_hdu = fits.PrimaryHDU(
data=configuration.parameter_file_contents)
primary_hdu.header.add_comment(configuration.comment)
primary_hdu.header['WTIME'] = (da_capo_results.dacapo_wall_time,
'Wall clock time [s]')
primary_hdu.header['MPIPROC'] = (mpi_comm.Get_size(),
'Number of MPI processes used')