Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
959582d
Create new branch fg_like to include updates of the branch xfaster/fg…
annegambrel May 19, 2021
ff2f366
Merge updates of branch 'main' into fg_like
vyluu May 28, 2021
0174c9f
add function bin_cl_template_tmat to xfaster_class
vyluu May 31, 2021
ed57c34
finish adding stuffs from xfaster_class.py from old fg_like branch
vyluu May 31, 2021
259b603
add use_xfer_mat to xfaster_exec.py
vyluu May 31, 2021
6239944
fix typo line 2677 qrb to rqb
vyluu Jun 1, 2021
5e170b9
modifying bin_cl_template_tmat in xfaster_class to work with the curr…
vyluu Jun 2, 2021
0acf9b7
making data structure in bin_cl_template_tmat compliable to the new p…
vyluu Jun 3, 2021
7d718bd
Load up shape spectra properly if including foregrounds
Jun 10, 2021
b45edd0
Merge branch 'main' into fg_like
annegambrel Jul 9, 2021
c08c295
Clean up bin_cl_template_tmat function
annegambrel Jul 12, 2021
491b126
adding anafast l-by-l transfer matrix
vyluu Aug 22, 2021
49db9cc
Adding gcorr_config_null.ini file to script/gcorr folder.
Jul 12, 2021
1c91f21
Modifying gcorr_config_null to be consistent with the example code pa…
Jul 14, 2021
16cf745
Modifying gcorr_config_null.
Jul 14, 2021
94a3a5d
Allow beam/beam error products to have unused fields (#13)
annegambrel Jul 20, 2021
b8ed012
Fixing bugs introduced by mistake in public code (#15)
annegambrel Aug 16, 2021
76f1be7
Update main github action file to match fix for PR one
annegambrel Aug 16, 2021
fc744cc
add pip upgrade
annegambrel Aug 16, 2021
79372d9
specifying versions of some requirements to hopefully reduce python p…
annegambrel Aug 16, 2021
903ca36
again trying to fix CI
annegambrel Aug 16, 2021
064eadd
again trying to fix CI
annegambrel Aug 16, 2021
e66a3d1
again trying to fix CI 3
annegambrel Aug 16, 2021
1d18a0a
again trying to fix CI 4
annegambrel Aug 16, 2021
c3224e7
cherry-pick recent updates in main branch to fg_like
vyluu Aug 25, 2021
8c18e61
Merge branch 'main' into fg_like
arahlin Aug 31, 2021
520a42b
merge detritus
arahlin Aug 31, 2021
1c171f1
Merge branch 'main' into fg_like
arahlin Sep 1, 2021
a1c4156
Merge branch 'main' into fg_like
arahlin Sep 1, 2021
3a65fcd
Merge branch 'main' into fg_like
arahlin Sep 1, 2021
b35cb1c
Rewire transfer matrix handling
arahlin Sep 2, 2021
197329f
bug fix
arahlin Sep 22, 2021
2739e16
Merge branch 'main' into transfer_matrix
arahlin Sep 22, 2021
67b076b
Debugging transfer_matrix branch
vyluu Oct 12, 2021
61e94cf
modify the get_transfer_matrix function to approximate cross1 -> cros…
vyluu Oct 14, 2021
e73b8ef
comment out qb_transfer update in fisher_iterate function
vyluu Oct 14, 2021
4112ab6
debugging
vyluu Oct 14, 2021
a86640c
comment out transfer and qb_transfer
vyluu Oct 15, 2021
efabd96
simplify transfer matrix file handling
arahlin Oct 17, 2021
94fcf85
Merge branch 'main' into transfer_matrix
arahlin Oct 17, 2021
5d0b0a0
adding [] to a list syntax
vyluu Oct 17, 2021
ab3de72
remove debug printing
vyluu Oct 17, 2021
bf53e5a
replace Mll altogether with transfer matrix, rather than introducing …
arahlin Oct 18, 2021
ade66be
Transpose the transfer matrix because the second axis is input
vyluu Oct 19, 2021
4ba398f
script for building a dummy transfer matrix for debugging
arahlin Oct 20, 2021
14554ff
makedirs
arahlin Oct 20, 2021
5401d10
transfer_matrix branch has bug when transfer_matrix_root=None. debug
vyluu Oct 21, 2021
431cb8c
continue debug ..
vyluu Oct 21, 2021
4e70698
Merge branch 'transfer_matrix' of github.com:SPIDER-CMB/xfaster into …
vyluu Oct 21, 2021
41c2c36
remove transpose of the anafast transfer matrix after test on simple …
vyluu Oct 25, 2021
0bbc1d6
move ref_freq and beta_ref from get_bandpower to get_transfer for cod…
vyluu Oct 26, 2021
d3b210d
Merge branch 'main' into transfer_matrix
arahlin Nov 2, 2021
7a30093
correct bug fix for get_transfer with foreground fitting enabled
arahlin Nov 2, 2021
6323412
Merge branch 'main' into transfer_matrix
arahlin Nov 3, 2021
46f8a46
Merge branch 'main' into transfer_matrix
arahlin Nov 30, 2021
3d854e3
Merge branch 'main' into transfer_matrix
arahlin Jan 7, 2022
e0ae617
Merge branch 'main' into transfer_matrix
arahlin Jan 7, 2022
f4a84bf
Merge branch 'main' into transfer_matrix
arahlin Jan 7, 2022
f91f5ff
Merge branch 'main' into transfer_matrix
arahlin Jan 13, 2022
56328db
Merge branch 'main' into transfer_matrix
arahlin Feb 17, 2022
8307efe
use correct tags in kernel_precalc
arahlin Feb 18, 2022
8f4eed7
Merge branch 'main' into transfer_matrix
arahlin Mar 14, 2022
d21c682
Merge branch 'main' into transfer_matrix
arahlin Apr 11, 2022
c92e822
Merge branch 'main' into transfer_matrix
arahlin Jun 30, 2022
3cfd873
Merge branch 'main' into transfer_matrix
arahlin Jul 1, 2022
9499352
Merge branch 'main' into transfer_matrix
arahlin Jul 20, 2022
3f4665c
Merge branch 'main' into transfer_matrix
arahlin Jul 21, 2022
5ad3ce5
fix working directory in github action
arahlin Jul 21, 2022
d0d1bf4
Merge branch 'main' into transfer_matrix
arahlin Jul 21, 2022
4bfe93e
Merge branch 'main' into transfer_matrix
arahlin Jul 21, 2022
f81136a
Merge branch 'main' into transfer_matrix
arahlin Jul 22, 2022
e32739d
Merge branch 'main' into transfer_matrix
arahlin Aug 23, 2022
ddbe03e
Merge branch 'main' into transfer_matrix
arahlin Apr 14, 2023
12fa18b
Merge branch 'main' into transfer_matrix
arahlin Apr 14, 2023
6bb36db
Merge branch 'main' into transfer_matrix
arahlin Sep 19, 2023
4c7caed
Merge branch 'main' into transfer_matrix
arahlin Sep 19, 2023
0d3a9d3
Merge branch 'main' into transfer_matrix
arahlin Oct 3, 2023
ff79b69
Merge branch 'main' into transfer_matrix
arahlin Oct 4, 2023
57bb899
Merge branch 'main' into transfer_matrix
arahlin Jan 25, 2024
d24236b
Merge branch 'main' into transfer_matrix
arahlin Apr 24, 2024
ab76e36
Merge branch 'main' into transfer_matrix
arahlin Apr 24, 2024
53bf89a
Merge branch 'main' into transfer_matrix
arahlin Apr 24, 2024
4c2aceb
Merge branch 'main' into transfer_matrix
arahlin Apr 24, 2024
b5391f8
Merge branch 'main' into transfer_matrix
arahlin Apr 25, 2024
f147fc6
Merge branch 'main' into transfer_matrix
arahlin Apr 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions example/make_example_transfer_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np
import xfaster as xf
import os

kerns = xf.load_and_parse("outputs_example/95x150/kernels_95x150.npz")
beams = xf.load_and_parse("outputs_example/95x150/beams_95x150.npz")
tf = np.loadtxt("maps_example/transfer_example.txt")
tf.shape
lmax = 500
lk = slice(0, lmax + 1)
bw = beams["beam_windows"]

os.makedirs("transfer_matrix")

for xname in kerns["kern"].keys():
tag1, tag2 = xname.split(":")
fname = "transfer_matrix/{}x{}_{{}}_to_{{}}_block.dat".format(tag1, tag2)
for spec in ["tt", "ee", "bb"]:
fb2 = tf[lk] * bw[spec][tag1][lk] * bw[spec][tag2][lk]

if spec == "tt":
k = kerns["kern"][xname][..., lk]
else:
k = kerns["pkern"][xname][..., lk]
mk = kerns["mkern"][xname][..., lk]

mat = k * fb2
su = spec.upper()
np.savetxt(fname.format(su, su), mat.T)

if spec != "tt":
mat = mk * fb2
su2 = "BB" if spec == "ee" else "EE"
np.savetxt(fname.format(su, su2), mat.T)
134 changes: 125 additions & 9 deletions xfaster/xfaster_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class XFaster(object):
"sims_transfer",
"shape_transfer",
"transfer",
"transfer_matrix",
"sims",
"beams",
"data",
Expand All @@ -141,6 +142,7 @@ class XFaster(object):
"sims_transfer": ["transfer"],
"shape_transfer": ["transfer"],
"transfer": ["bandpowers"],
"transfer_matrix": ["bandpowers"],
"sims": ["bandpowers"],
"beams": ["transfer"],
"data": ["bandpowers"],
Expand Down Expand Up @@ -1302,7 +1304,8 @@ def get_filename(
template_type, reference_type.
bp_opts : bool
If True, also check the following attributes (in addition to those
checked if ``data_opts`` is True): weighted_bins, return_cls.
checked if ``data_opts`` is True): weighted_bins, return_cls,
transfer_matrix.

Returns
-------
Expand Down Expand Up @@ -1342,6 +1345,8 @@ def get_filename(
name += ["wbins"]
if getattr(self, "return_cls", False):
name += ["cl"]
if hasattr(self, "transfer_matrix"):
name += ["xfermat"]

if map_tag is not None:
name += ["map", map_tag]
Expand Down Expand Up @@ -4067,7 +4072,8 @@ def get_bin_def(
def kernel_precalc(self, map_tag=None, transfer=False):
"""
Compute the mixing kernels M_ll' = K_ll' * F_l' * B_l'^2. Called by
``bin_cl_template`` to pre-compute kernel terms.
``bin_cl_template`` to pre-compute kernel terms. Constructs M_ll'
directly from the ``transfer_matrix`` attribute, if present.

Arguments
---------
Expand Down Expand Up @@ -4103,6 +4109,12 @@ def kernel_precalc(self, map_tag=None, transfer=False):
lk = slice(0, lmax + 1)
mll = OrderedDict()

use_transfer_matrix = hasattr(self, "transfer_matrix")
if use_transfer_matrix and transfer:
raise ValueError(
"Transfer matrix cannot be used to compute transfer functions."
)

if transfer:
comps = ["cmb"]
else:
Expand All @@ -4118,11 +4130,21 @@ def kernel_precalc(self, map_tag=None, transfer=False):
mstag = "{}_mix".format(stag)
mll[mstag] = OrderedDict()

bw = self.beam_windows[spec]
if not transfer:
tf = self.transfer[stag]
if not use_transfer_matrix:
bw = self.beam_windows[spec]
if not transfer:
tf = self.transfer[stag]

for xname, (m0, m1) in map_pairs.items():
# extract transfer matrix terms
if use_transfer_matrix:
tm = self.transfer_matrix[xname][spec]
mll[stag][xname] = tm[spec][:, lk]
if spec in ["ee", "bb"]:
spec2 = "bb" if spec == "ee" else "ee"
mll[mstag][xname] = tm[spec2][:, lk]
continue

# beams
fb2 = bw[m0][lk] * bw[m1][lk]

Expand Down Expand Up @@ -5734,10 +5756,11 @@ def fisher_iterate(
)

if not transfer:
out.update(
qb_transfer=self.qb_transfer,
transfer=self.transfer,
)
if not hasattr(self, "transfer_matrix"):
out.update(
qb_transfer=self.qb_transfer,
transfer=self.transfer,
)
if self.template_type is not None:
out.update(template_alpha=self.template_alpha)

Expand Down Expand Up @@ -6110,6 +6133,99 @@ def expand_transfer(qb_transfer, bin_def, check_only=False):

return self.transfer

def get_transfer_matrix(self, file_root=None):
"""
Read in an ell-by-ell transfer matrix, constructed from ratios of
`anafast` spectra using an ensemble of simulations. This matrix should
be used in place of the standard XFaster transfer function calculation,
and includes masking, filtering and beam effects.

Arguments
---------
file_root : str
Directory where the transfer matrix blocks are stored. Expects
column text files with naming pattern:
``<tag1>x<tag2>_<spec_in>_to_<spec_out>_block.dat``
Required if the transfer matrix has not already been computed.

Returns
-------
transfer_matrix : OrderedDict
Dictionary of matrix blocks of shape (lmax + 1, lmax + 1),
keyed by map cross (xname), output spectrum, and input spectrum.
This dictionary is also cached in the ``transfer_matrix`` attribute.

Notes
-----
This function is called at the ``transfer_matrix`` checkpoint, and is
meant to be an alternative to the standard ``transfer`` sequence.
"""

tm_shape = (
self.num_corr * (self.num_spec + 2) * (self.lmax + 1),
self.lmax + 1,
)

save_name = "transfer_matrix"
save_attrs = ["transfer_matrix_root", "transfer_matrix"]
ret = self.load_data(
save_name,
"transfer_matrix",
fields=save_attrs,
to_attrs=True,
shape=tm_shape,
shape_ref="transfer_matrix",
)

if ret is not None:
return ret["transfer_matrix"]

transfer_matrix = OrderedDict()
for xname, (tag1, tag2) in self.map_pairs.items():
dx = transfer_matrix.setdefault(xname, OrderedDict())

if tag1 == tag2:
ftag = "{}x{}".format(tag1, tag2)
else:
for t in ["{}x{}".format(tag1, tag2), "{}x{}".format(tag2, tag1)]:
fname = os.path.join(file_root, "{}_*_to_*_block.dat".format(t))
if len(glob.glob(fname)):
ftag = t
break
else:
self.warn("Missing transfer matrix for {}".format(xname))
continue

# file name format string
fname = os.path.join(file_root, "{}_{{}}_to_{{}}_block.dat".format(ftag))

for spec_out in self.specs:
dsi = dx.setdefault(spec_out, OrderedDict())

specs_in = ["ee", "bb"] if spec_out in ["ee", "bb"] else [spec_out]
for spec_in in specs_in:
if spec_out in ["tt", "ee", "bb"]:
# auto- => auto-
fname1 = fname.format(spec_in.upper(), spec_out.upper())
mat = np.loadtxt(fname1)

# populate matrix up to lmax
dsi[spec_in] = np.zeros((self.lmax + 1, self.lmax + 1))
nx, ny = [min([s, self.lmax + 1]) for s in mat.shape]
dsi[spec_in][:nx, :ny] = mat[:nx, :ny]
del mat

else:
# cross1- => cross1-
s1, s2 = [s + s for s in spec_in]
dsi[spec_in] = np.sqrt(np.abs(dx[s1][s1] * dx[s2][s2]))

self.transfer_matrix_root = file_root
self.transfer_matrix = transfer_matrix

self.save_data(save_name, from_attrs=save_attrs)
return transfer_matrix

def get_bandpowers(
self,
map_tag=None,
Expand Down
33 changes: 23 additions & 10 deletions xfaster/xfaster_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def xfaster_run(
return_cls=False,
qb_only=False,
fix_bb_transfer=False,
transfer_matrix_root=None,
null_first_cmb=False,
delta_beta_prior=None,
like_profiles=False,
Expand Down Expand Up @@ -410,6 +411,10 @@ def xfaster_run(
fix_bb_transfer : bool
If True, after transfer functions have been calculated, impose that the
BB transfer function is exactly equal to the EE transfer function.
transfer_matrix_root : str
If set, use the pre-computed ell-by-ell transfer matrix stored in this
directory, instead of the standard transfer function computed by the
XFaster estimator.
null_first_cmb : bool
If True, keep first CMB bandpowers fixed to input shape (qb=1).
delta_beta_prior : float
Expand Down Expand Up @@ -673,6 +678,7 @@ def xfaster_run(
iter_max=iter_max,
save_iters=save_iters,
fix_bb_transfer=fix_bb_transfer,
transfer_matrix_root=transfer_matrix_root,
delta_beta_prior=delta_beta_prior,
cond_noise=cond_noise,
cond_criteria=cond_criteria,
Expand All @@ -687,6 +693,7 @@ def xfaster_run(
config_vars.update(spec_opts, "Spectrum Estimation Options")
config_vars.remove_option("XFaster General", "bandpower_tag")
spec_opts.pop("multi_map")
spec_opts.pop("transfer_matrix_root")
bandpwr_opts = spec_opts.copy()
bandpwr_opts.pop("fix_bb_transfer")
spec_opts.pop("file_tag")
Expand Down Expand Up @@ -746,20 +753,25 @@ def xfaster_run(
gcorr_file=gcorr_file,
)

X.log("Computing kernels...", "notice")
X.get_kernels(window_lmax=window_lmax)
if transfer_matrix_root:
X.log("Loading pre-computed transfer matrix...", "notice")
X.get_transfer_matrix(file_root=transfer_matrix_root)

X.log("Computing beam window functions...", "notice")
X.get_beams(pixwin=pixwin)
else:
X.log("Computing kernels...", "notice")
X.get_kernels(window_lmax=window_lmax)

X.log("Computing beam window functions...", "notice")
X.get_beams(pixwin=pixwin)

X.log("Computing sim ensemble averages for transfer function...", "notice")
X.get_masked_sims(transfer=True, **sim_transfer_opts)
X.log("Computing sim ensemble averages for transfer function...", "notice")
X.get_masked_sims(transfer=True, **sim_transfer_opts)

X.log("Loading spectrum shape for transfer function...", "notice")
X.get_signal_shape(filename=signal_transfer_spec, transfer=True)
X.log("Loading spectrum shape for transfer function...", "notice")
X.get_signal_shape(filename=signal_transfer_spec, transfer=True)

X.log("Computing transfer functions...", "notice")
X.get_transfer(**transfer_opts)
X.log("Computing transfer functions...", "notice")
X.get_transfer(**transfer_opts)

X.log("Computing sim ensemble averages...", "notice")
X.get_masked_sims(**sim_opts)
Expand Down Expand Up @@ -1256,6 +1268,7 @@ def add_arg(
add_arg(G, "return_cls")
add_arg(G, "qb_only")
add_arg(G, "fix_bb_transfer")
add_arg(G, "transfer_matrix_root")
add_arg(G, "null_first_cmb")
add_arg(G, "delta_beta_prior", argtype=float)
add_arg(G, "like_profiles")
Expand Down