Skip to content

Commit 77f791d

Browse files
committed
get_spectra_from_maps: only load necessary alms. get_pseudo2datavec: dont do Bbl (saves disk and Bbl not really needed). get_mcm_bbl_and_pseudosignal: per ell pseudosignal spectra for INKA also computed and saved to disk
1 parent fbad2b9 commit 77f791d

File tree

3 files changed

+59
-34
lines changed

3 files changed

+59
-34
lines changed

project/SO/pISO/python/get_mcm_and_bbl.py renamed to project/SO/pISO/python/get_mcm_bbl_and_pseudosignal.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
mcm_dir = d['mcm_dir']
3030
pspy_utils.create_directory(mcm_dir)
3131

32+
bestfit_dir = d["best_fits_dir"]
33+
pseudo_dir = opj(bestfit_dir, 'pseudo')
34+
pspy_utils.create_directory(pseudo_dir)
35+
36+
spectra = ["TT", "TE", "TB", "ET", "BT", "EE", "EB", "BE", "BB"]
37+
3238
surveys = d["surveys"]
3339
niter = d['niter']
3440
lmax = d["lmax"]
@@ -141,10 +147,32 @@
141147
nbins = len(bin_hi)
142148
Pbl = so_spectra.get_binning_matrix(bin_lo, bin_hi, lmax, type)
143149

144-
# loop over map pairs for the post-processing and saving
145150
for t, task in enumerate(subtasks):
146151
sv1, m1, sv2, m2 = sv1_list[task], m1_list[task], sv2_list[task], m2_list[task]
147-
152+
spec_name = f"{sv1}_{m1}x{sv2}_{m2}"
153+
154+
# we need to get the best-fit pseudosignal spectra for the covariance. we do
155+
# that here to avoid recalculating all the unbinned mcms again in a
156+
# different script. NOTE: we need beamed Cls
157+
l, signal_dict, save_type = so_spectra.read_ps(opj(bestfit_dir, f'cmb_and_fg_{spec_name}.dat'), spectra=spectra, return_type=True)
158+
assert l[0] == 2, f'Bestfit spectra assumed to start at l=2, got l={l[0]}'
159+
160+
if save_type == 'Dl':
161+
fac = 2*np.pi / (l*(l + 1))
162+
else:
163+
assert save_type == 'Cl', f'save_type must be Dl or Cl, got {save_type}'
164+
fac = 1
165+
166+
# trim to match mcm
167+
for k in signal_dict.keys():
168+
signal_dict[k] = signal_dict[k][:lmax-2] * fac[:lmax-2]
169+
l = l[:lmax-2]
170+
171+
# the fully realized mcm matrix would be a lot of memory
172+
pseudosignal_dict = so_spectra.spin2spin_array_matmul_spec_dict(mcm[t], signal_dict)
173+
so_spectra.write_ps(opj(pseudo_dir, f'pseudo_signal_{spec_name}.dat'), l, pseudosignal_dict, 'Cl', spectra=spectra)
174+
175+
# now do the binning
148176
if binned_mcm:
149177
mxx = np.zeros((5, nbins, nbins)) # b x b
150178
Bbl = np.zeros((5, nbins, lmax))
@@ -194,6 +222,5 @@
194222

195223
log.info(f"[{task:02d}] Saving mcm matrix for {sv1}_{m1} x {sv2}_{m2}")
196224

197-
prefix = opj(f"{mcm_dir}", f"{sv1}_{m1}x{sv2}_{m2}")
198-
np.save(prefix + "_mode_coupling_inv.npy", mbl_inv)
199-
np.save(prefix + "_Bbl.npy", Bbl)
225+
np.save(opj(f"{mcm_dir}", spec_name + "_mode_coupling_inv.npy") , mbl_inv)
226+
np.save(opj(f"{mcm_dir}", spec_name + "_Bbl.npy"), Bbl)

project/SO/pISO/python/get_pspipe_operators.py renamed to project/SO/pISO/python/get_pseudo2datavec.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@
9292

9393
log.info(f'[Rank {so_mpi.rank}] Calculating operators for {spec_name}')
9494

95-
# get the mbl_inv, Bbl matrices for this array cross
95+
# get the mbl_inv for this array cross
9696
prefix = opj(f"{mcm_dir}", spec_name)
9797
mbl_inv = np.load(prefix + "_mode_coupling_inv.npy")
98-
Bbl = np.load(prefix + "_Bbl.npy")
9998

100-
# need to splice mbl_inv and Bbl into spectra-ordered arrays
99+
# need to splice mbl_inv into spectra-ordered arrays
101100
# TODO: consider disk-space, memory (could be sparse)
102101
pseudo2datavec = so_mcm.get_spec2spec_array_from_spin2spin_array(mbl_inv, dense=True)
103-
th2datavec = so_mcm.get_spec2spec_array_from_spin2spin_array(Bbl, spin0=not binned_mcm, dense=True)
104102

105103
# get the inv_kspace matrix for this array cross, if necessary
106104
if apply_kspace_filter:
@@ -115,14 +113,12 @@
115113
if d[f"pixwin_{sv2}"]["pix"] == "HEALPIX" and deconvolve_pixwin:
116114
pseudo2datavec /= np.tile(pixwins[sv2], 9)[:, None] # apply on the left
117115

118-
# save
116+
# save and plot
119117
np.save(opj(f'{mcm_dir}', f'pseudo2datavec_{spec_name}.npy'), pseudo2datavec)
120-
np.save(opj(f'{mcm_dir}', f'th2datavec_{spec_name}.npy'), th2datavec)
121-
122-
for name, var in (('pseudo2datavec', pseudo2datavec), ('th2datavec', th2datavec)):
123-
plt.figure(figsize=(10, 8))
124-
plt.imshow(np.log(np.abs(var[:, ::4])), aspect=25)
125-
plt.colorbar()
126-
plt.title(f'{name} {spec_name}')
127-
plt.savefig(opj(f'{plot_dir}', f'{name}_{spec_name}'), bbox_inches='tight')
128-
plt.close()
118+
119+
plt.figure(figsize=(10, 8))
120+
plt.imshow(np.log(np.abs(pseudo2datavec)), aspect=100)
121+
plt.colorbar()
122+
plt.title(f'pseudo2datavec {spec_name}')
123+
plt.savefig(opj(f'{plot_dir}', f'pseudo2datavec_{spec_name}'), bbox_inches='tight')
124+
plt.close()

project/SO/pISO/python/get_spectra_from_maps.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@
364364

365365
# TODO: would be cleaner if could just make ps_map with
366366
# cutoff instead of relying on the mask
367-
ps_mask = so_map.read_map(d[f"ps_mask_{sv}_{m}"])
367+
ps_mask = so_map.read_map(d[f"ps_mask_{sv}_{m}"], geometry=win_T.data.geometry)
368368
ps_map.data *= ps_mask.data
369369
split.data += ps_map.data
370370

@@ -384,7 +384,7 @@
384384

385385
if apply_kspace_filter and deconvolve_pixwin:
386386
if k == 0:
387-
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] Apply kspace filter and inv pixwin on {sv}, {m}, {snk}")
387+
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] Apply kspace filter and inv pixwin on {sv}, {m}")
388388
split = kspace.filter_map(split,
389389
filter,
390390
win_kspace,
@@ -393,7 +393,7 @@
393393
use_ducc_rfft=True)
394394
elif apply_kspace_filter:
395395
if k == 0:
396-
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: apply kspace filter but no inv pixwin on {sv}, {m}, {snk}")
396+
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: apply kspace filter but no inv pixwin on {sv}, {m}")
397397
split = kspace.filter_map(split,
398398
filter,
399399
win_kspace,
@@ -403,14 +403,14 @@
403403

404404
elif deconvolve_pixwin:
405405
if k == 0:
406-
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: inv pixwin but no kspace filter on {sv}, {m}, {snk}")
406+
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: inv pixwin but no kspace filter on {sv}, {m}")
407407
split = so_map.fourier_convolution(split,
408408
inv_pwin,
409409
window=win_kspace,
410410
use_ducc_rfft=True)
411411
else:
412412
if k == 0:
413-
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: no kspace filter and no inv pixwin on {sv}, {m}, {snk}")
413+
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: no kspace filter and no inv pixwin on {sv}, {m}")
414414

415415

416416
elif win_T.pixel == "HEALPIX":
@@ -429,10 +429,10 @@
429429

430430
if deconvolve_pixwin:
431431
if k == 0:
432-
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: inv pixwin but no kspace filter on {sv}, {m}, {snk} (HEALPIX)")
432+
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: inv pixwin but no kspace filter on {sv}, {m} (HEALPIX)")
433433
else:
434434
if k == 0:
435-
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: no kspace filter and no inv pixwin on {sv}, {m}, {snk} (HEALPIX)")
435+
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] WARNING: no kspace filter and no inv pixwin on {sv}, {m} (HEALPIX)")
436436

437437
split = split.calibrate(cal=cal, pol_eff=pol_eff)
438438

@@ -457,15 +457,20 @@
457457

458458
# compute the power spectra
459459

460-
# if data, need to load alms, and so need to wait
461-
# for all alms to be done
460+
# if data, need to load alms, and so need to wait for all alms to be done to
461+
# load safely. NOTE: but, just need to load the alms needed for this task.
462+
# this saves a lot of memory
462463
if which == 'data':
463464
t0 = time.time()
464465
log.info(f"[Rank {so_mpi.rank}] Loading alms")
465466
so_mpi.barrier()
466467

467468
master_alms = {}
468-
for sv, m in zip(sv_list, map_list): # all of them, not just this process
469+
for sv, m in zip(sv_list, map_list):
470+
if (sv not in sv1_iterator) and (sv not in sv2_iterator):
471+
continue
472+
if (m not in m1_iterator) and (m not in m2_iterator):
473+
continue
469474
for k, snk in enumerate(splits_iterator[sv]): # k == split_idx, since data
470475
master_alms[sv, m, snk] = np.load(f"{alms_dir}" + f"alms_{sv}_{m}_set{k}.npy")
471476
log.info(f"[Rank {so_mpi.rank}] Loaded alms: {time.time() - t0} seconds")
@@ -569,8 +574,7 @@
569574
master_alms = None
570575

571576
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] Spectra computation time: {time.time() - t0} seconds")
572-
t0 = time.time()
573-
577+
574578
if which == 'data':
575579
spec_name_all = f"{type}_all_sn_cross_data"
576580

@@ -593,6 +597,4 @@
593597
# each process has separate maps in its mapset
594598
np.save(f"{spec_dir}" + f"{spec_name_all}.npy", ps_dict_all)
595599

596-
ps_dict_all = None
597-
598-
log.info(f"[Rank {so_mpi.rank}, Mapset {iii}] Spectra save time: {time.time() - t0} seconds")
600+
ps_dict_all = None

0 commit comments

Comments
 (0)