Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added more flexible binning #109

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 32 additions & 0 deletions tjpcov/covariance_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,38 @@ def get_ell_eff(self):

return ell

def get_binning_info(self):
"""Return all ells used for the different bandpowers, their effective
ell and bandpower edges based on the SACC information.

It assume that all tracers have the same bandpower windows.

Args:
sacc_data (:obj:`sacc.sacc.Sacc`): Data Sacc instance

Returns:
ells: Array with the ells to pass to theory prediction.
ell_eff: Array with effective ells from sacc
ell_edges: Array with bandpower edges (assumed to be contiguous).
"""
sacc_file = self.io.get_sacc_file()
tr1, tr2 = sacc_file.get_tracer_combinations()[0]
dt = sacc_file.get_data_types(tracers=(tr1, tr2))[0]
inds = sacc_file.indices(data_type=dt, tracers=(tr1, tr2))
bpw = sacc_file.get_bandpower_windows(inds)
if bpw is not None:
ells = np.repeat(bpw.values, bpw.weight.shape[1])
ells = ells.reshape(bpw.weight.shape)
ell_eff = np.average(ells, weights=bpw.weight, axis=0)
ell_edges = np.zeros(bpw.weight.shape[1] + 1)
for i in range(bpw.weight.shape[1]):
ell_edges[i] = bpw.values[bpw.weight[:, i] > 0][0]
if i == bpw.weight.shape[1] - 1:
ell_edges[i + 1] = bpw.values[bpw.weight[:, i] > 0][-1]
return bpw.values, ell_eff, ell_edges
else:
return None

def get_sacc_with_concise_dtypes(self):
"""Return a copy of the sacc file with concise data types.

Expand Down
46 changes: 30 additions & 16 deletions tjpcov/covariance_gaussian_fsky.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pyccl as ccl
import warnings

from .wigner_transform import bin_cov
from .covariance_builder import CovarianceFourier, CovarianceProjectedReal
Expand Down Expand Up @@ -41,24 +42,37 @@ def get_binning_info(self, binning="linear"):
"""
# TODO: This should be obtained from the sacc file or the input
# configuration. Check how it is done in TXPipe:
# https://github.com/LSSTDESC/TXPipe/blob/a9dfdb7809ac7ed6c162fd3930c643a67afcd881/txpipe/covariance.py#L23
ell_eff = self.get_ell_eff()
nbpw = ell_eff.size

ellb_min, ellb_max = ell_eff.min(), ell_eff.max()
if binning == "linear":
del_ell = (ell_eff[1:] - ell_eff[:-1])[0]

ell_min = ellb_min - del_ell / 2
ell_max = ellb_max + del_ell / 2

ell_delta = (ell_max - ell_min) // nbpw
ell_edges = np.arange(ell_min, ell_max + 1, ell_delta)
ell = np.arange(ell_min, ell_max + ell_delta - 2)
# https://github.com/LSSTDESC/TXPipe/blob/a9dfdb7809ac7ed6c162fd3930c643a67a
out = self.get_binning_info()
if out is not None:
ell = out[0]
ell_eff = out[1]
ell_edges = out[2]
return ell, ell_eff, ell_edges
else:
raise NotImplementedError(f"Binning {binning} not implemented yet")
warnings.warn(
"No bandpower windows found, \
falling back to linear method"
)
ell_eff = self.get_ell_eff()
nbpw = ell_eff.size

ellb_min, ellb_max = ell_eff.min(), ell_eff.max()
if binning == "linear":
del_ell = (ell_eff[1:] - ell_eff[:-1])[0]

ell_min = ellb_min - del_ell / 2
ell_max = ellb_max + del_ell / 2

ell_delta = (ell_max - ell_min) // nbpw
ell_edges = np.arange(ell_min, ell_max + 1, ell_delta)
ell = np.arange(ell_min, ell_max + ell_delta - 2)
else:
raise NotImplementedError(
f"Binning {binning} not implemented yet"
)

return ell, ell_eff, ell_edges
return ell, ell_eff, ell_edges

def get_covariance_block(
self,
Expand Down
Loading