From ace77daf6c0afccb96e728c878539aa5f1628fa8 Mon Sep 17 00:00:00 2001 From: Javier Sanchez Date: Wed, 5 Jun 2024 11:50:33 -0400 Subject: [PATCH 1/5] added more flexible binning --- tjpcov/covariance_builder.py | 31 ++++++++++++++++++++++++ tjpcov/covariance_gaussian_fsky.py | 38 +++++++++++++++++++----------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/tjpcov/covariance_builder.py b/tjpcov/covariance_builder.py index d42bf411..f420b924 100644 --- a/tjpcov/covariance_builder.py +++ b/tjpcov/covariance_builder.py @@ -723,6 +723,37 @@ def get_ell_eff(self): ell, _ = sacc_file.get_ell_cl(dtype, *tracers) 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]).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. diff --git a/tjpcov/covariance_gaussian_fsky.py b/tjpcov/covariance_gaussian_fsky.py index 5c2f19d3..14e0f289 100644 --- a/tjpcov/covariance_gaussian_fsky.py +++ b/tjpcov/covariance_gaussian_fsky.py @@ -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 @@ -41,24 +42,33 @@ 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 + # https://github.com/LSSTDESC/TXPipe/blob/a9dfdb7809ac7ed6c162fd3930c643a67a + out = super().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: + 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] + 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_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") + 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, From d8e6c3d0a52a2cec951f901dcc55ec0e7b4d123e Mon Sep 17 00:00:00 2001 From: Javier Sanchez Date: Wed, 5 Jun 2024 13:06:19 -0400 Subject: [PATCH 2/5] used blacker formatting --- tjpcov/covariance_builder.py | 5 +++-- tjpcov/covariance_gaussian_fsky.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tjpcov/covariance_builder.py b/tjpcov/covariance_builder.py index f420b924..e0afa5b0 100644 --- a/tjpcov/covariance_builder.py +++ b/tjpcov/covariance_builder.py @@ -723,7 +723,7 @@ def get_ell_eff(self): ell, _ = sacc_file.get_ell_cl(dtype, *tracers) 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. @@ -744,7 +744,8 @@ def get_binning_info(self): 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]).reshape(bpw.weight.shape) + ells = np.repeat(bpw.values, bpw.weight.shape[1]).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]): diff --git a/tjpcov/covariance_gaussian_fsky.py b/tjpcov/covariance_gaussian_fsky.py index 14e0f289..bbe445a5 100644 --- a/tjpcov/covariance_gaussian_fsky.py +++ b/tjpcov/covariance_gaussian_fsky.py @@ -66,7 +66,8 @@ def get_binning_info(self, binning="linear"): 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") + raise NotImplementedError( + f"Binning {binning} not implemented yet") return ell, ell_eff, ell_edges From cbfd8ed04cda67d579ec3887191f74e02599d00c Mon Sep 17 00:00:00 2001 From: Javier Sanchez Date: Wed, 5 Jun 2024 13:12:48 -0400 Subject: [PATCH 3/5] trying to make CI happy --- tjpcov/covariance_builder.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tjpcov/covariance_builder.py b/tjpcov/covariance_builder.py index e0afa5b0..d279e7d4 100644 --- a/tjpcov/covariance_builder.py +++ b/tjpcov/covariance_builder.py @@ -725,8 +725,8 @@ 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. + """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. @@ -744,13 +744,13 @@ def get_binning_info(self): 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]).reshape( - bpw.weight.shape) + 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: + 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: From adff79bbbcd1f54efc40a571e1c489a48eda4270 Mon Sep 17 00:00:00 2001 From: Javier Sanchez Date: Wed, 5 Jun 2024 13:21:56 -0400 Subject: [PATCH 4/5] trying to make CI happy v2 --- tjpcov/covariance_builder.py | 2 +- tjpcov/covariance_gaussian_fsky.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tjpcov/covariance_builder.py b/tjpcov/covariance_builder.py index d279e7d4..7fbf1ef0 100644 --- a/tjpcov/covariance_builder.py +++ b/tjpcov/covariance_builder.py @@ -751,7 +751,7 @@ def get_binning_info(self): 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] + ell_edges[i + 1] = bpw.values[bpw.weight[:, i] > 0][-1] return bpw.values, ell_eff, ell_edges else: return None diff --git a/tjpcov/covariance_gaussian_fsky.py b/tjpcov/covariance_gaussian_fsky.py index bbe445a5..80fcc363 100644 --- a/tjpcov/covariance_gaussian_fsky.py +++ b/tjpcov/covariance_gaussian_fsky.py @@ -50,8 +50,10 @@ def get_binning_info(self, binning="linear"): ell_edges = out[2] return ell, ell_eff, ell_edges else: - warnings.warn("No bandpower windows found, \ - falling back to linear method") + warnings.warn( + "No bandpower windows found, \ + falling back to linear method" + ) ell_eff = self.get_ell_eff() nbpw = ell_eff.size @@ -67,7 +69,8 @@ def get_binning_info(self, binning="linear"): ell = np.arange(ell_min, ell_max + ell_delta - 2) else: raise NotImplementedError( - f"Binning {binning} not implemented yet") + f"Binning {binning} not implemented yet" + ) return ell, ell_eff, ell_edges From aa7c0b7f64708ebca4257e5689bae2cd8ebbe76f Mon Sep 17 00:00:00 2001 From: Javier Sanchez Date: Mon, 22 Jul 2024 10:53:41 -0700 Subject: [PATCH 5/5] addressing Carlos' comments --- tjpcov/covariance_gaussian_fsky.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tjpcov/covariance_gaussian_fsky.py b/tjpcov/covariance_gaussian_fsky.py index 80fcc363..972252e7 100644 --- a/tjpcov/covariance_gaussian_fsky.py +++ b/tjpcov/covariance_gaussian_fsky.py @@ -43,7 +43,7 @@ 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/a9dfdb7809ac7ed6c162fd3930c643a67a - out = super().get_binning_info() + out = self.get_binning_info() if out is not None: ell = out[0] ell_eff = out[1]