Skip to content

Commit

Permalink
Save predictions to sacc (#349)
Browse files Browse the repository at this point in the history
* Improve type usage, now UpdatableCollection allows the user to specify the type of its contents.
* Reorganized theory_vector and data_vector set/get and computation.
* Combining new state machine and new methods.
* Fixed reset calls in cosmosis connector, must be called after all possible gets in the end of execute().
* Cleaning all quantities computed after updated.
* Testing new methods of GaussFamily (and older not tested ones).
* Adding optional noise to the realizations and testing it.
* Updated documentation.
* More tests for Statistics.
* Added make_realization_vector to compute a new realization vector.
* Added make_realization to generate a new sacc with or without noise.
* Simplify checking of RE match
* Add COMPUTED state to GaussFamily
* Address failure to test line gauss_family:202
* Require 100% coverage on changed lines (testing codecov configuration)
* Remove needless call to super().__init__
* Support getting covariance for list of statistics

---------

Co-authored-by: Sandro Dias Pinto Vitenti <vitenti@uel.br>
Co-authored-by: Marc Paterno <paterno@fnal.gov>
  • Loading branch information
3 people committed Jan 30, 2024
1 parent b978402 commit d39ba1f
Show file tree
Hide file tree
Showing 23 changed files with 677 additions and 112 deletions.
5 changes: 3 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ coverage:
patch:
default:
# basic
target: auto
target: 100%
threshold: 0%

# advanced
Expand Down Expand Up @@ -63,4 +63,5 @@ coverage:
removed_code_behavior: fully_covered_patch #off, removals_only, adjust_base",

github_checks:
annotations: true #,false
annotations: true #,false

9 changes: 7 additions & 2 deletions examples/des_y1_3x2pt/des_y1_3x2pt_PT.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class CclSetup:

@dataclass
class CElls:
"""A package of related C_ell values, to reduce the number of variables
used in the :meth:`run_likelihood` method."""

GG: np.ndarray
GI: np.ndarray
II: np.ndarray
Expand Down Expand Up @@ -234,6 +237,7 @@ def run_likelihood() -> None:
assert likelihood.cov is not None

stat0 = likelihood.statistics[0].statistic
assert isinstance(stat0, TwoPoint)

# x = likelihood.statistics[0].ell_or_theta_
# y_data = likelihood.statistics[0].measured_statistic_
Expand All @@ -243,11 +247,12 @@ def run_likelihood() -> None:

print(list(stat0.cells.keys()))

stat2 = likelihood.statistics[2].statistic
stat2 = likelihood.statistics[2].statistic # pylint: disable=no-member
assert isinstance(stat2, TwoPoint)
print(list(stat2.cells.keys()))

stat3 = likelihood.statistics[3].statistic
stat3 = likelihood.statistics[3].statistic # pylint: disable=no-member
assert isinstance(stat3, TwoPoint)
print(list(stat3.cells.keys()))

plot_predicted_and_measured_statistics(
Expand Down
1 change: 1 addition & 0 deletions examples/des_y1_3x2pt/des_y1_cosmic_shear_TATT.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def run_likelihood() -> None:
print(f"Log-like = {log_like:.1f}")

# Plot the predicted and measured statistic
assert isinstance(likelihood, ConstGaussian)
two_point_0 = likelihood.statistics[0].statistic
assert isinstance(two_point_0, TwoPoint)

Expand Down
3 changes: 1 addition & 2 deletions examples/des_y1_3x2pt/des_y1_cosmic_shear_pk_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,9 @@ def run_likelihood() -> None:
print(f"Log-like = {log_like:.1f}")

# Plot the predicted and measured statistic
assert isinstance(likelihood, ConstGaussian)
two_point_0 = likelihood.statistics[0].statistic
assert isinstance(two_point_0, TwoPoint)

assert isinstance(likelihood, ConstGaussian)
assert likelihood.cov is not None

# Predict CCL Cl
Expand Down
46 changes: 27 additions & 19 deletions firecrown/connector/cosmosis/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class FirecrownLikelihood:
:param config: current CosmoSIS datablock
"""

likelihood: Likelihood
map: MappingCosmoSIS

def __init__(self, config: cosmosis.datablock):
"""Create the FirecrownLikelihood object from the given configuration."""
likelihood_source = config.get_string(option_section, "likelihood_source", "")
Expand All @@ -61,6 +58,7 @@ def __init__(self, config: cosmosis.datablock):

self.firecrown_module_name = option_section
self.sampling_sections = sections
self.likelihood: Likelihood
try:
self.likelihood, self.tools = load_likelihood(
likelihood_source, build_parameters
Expand All @@ -70,7 +68,7 @@ def __init__(self, config: cosmosis.datablock):
print(f"The Firecrown likelihood needs a required parameter: {err}")
print("*" * 30)
raise
self.map = mapping_builder(
self.map: MappingCosmoSIS = mapping_builder(
input_style="CosmoSIS", require_nonlinear_pk=require_nonlinear_pk
)

Expand Down Expand Up @@ -127,23 +125,31 @@ def execute(self, sample: cosmosis.datablock) -> int:
for section, name, val in derived_params_collection:
sample.put(section, name, val)

self.likelihood.reset()
self.tools.reset()
if not isinstance(self.likelihood, GaussFamily):
self.likelihood.reset()
self.tools.reset()
return 0

# Save concatenated data vector and inverse covariance to enable support
# If we get here, we have a GaussFamily likelihood, and we need to
# save concatenated data vector and inverse covariance to enable support
# for the CosmoSIS Fisher sampler. This can only work for likelihoods
# that have these quantities. Currently, this is only GaussFamily.

if isinstance(self.likelihood, GaussFamily):
sample.put(
"data_vector", "firecrown_theory", self.likelihood.predicted_data_vector
)
sample.put(
"data_vector", "firecrown_data", self.likelihood.measured_data_vector
)
sample.put(
"data_vector", "firecrown_inverse_covariance", self.likelihood.inv_cov
)
sample.put(
"data_vector",
"firecrown_theory",
self.likelihood.get_theory_vector(),
)
sample.put(
"data_vector",
"firecrown_data",
self.likelihood.get_data_vector(),
)
sample.put(
"data_vector",
"firecrown_inverse_covariance",
self.likelihood.inv_cov,
)

# Write out theory and data vectors to the data block the ease
# debugging.
Expand All @@ -164,14 +170,16 @@ def execute(self, sample: cosmosis.datablock) -> int:
sample.put(
"data_vector",
f"theory_{stat.sacc_data_type}_{tracer}",
stat.predicted_statistic_,
stat.get_theory_vector(),
)
sample.put(
"data_vector",
f"data_{stat.sacc_data_type}_{tracer}",
stat.measured_statistic_,
stat.get_data_vector(),
)

self.likelihood.reset()
self.tools.reset()
return 0

def form_error_message(self, exc: MissingSamplerParameterError) -> str:
Expand Down
Loading

0 comments on commit d39ba1f

Please sign in to comment.