From 934a6e191e3834adb4d0fb3c7d065bc6ef2007f1 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 07:19:03 -0300 Subject: [PATCH 01/25] Turning tests into a package to allow importing. --- tests/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb From f51762b233819a7f4f173cd6e7749c90b407f6d5 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 07:25:19 -0300 Subject: [PATCH 02/25] Introducing CCL creation modes. Adapating code from PR #458 to allow CCL MG models. --- firecrown/ccl_factory.py | 120 ++++++++++++++++++++- firecrown/connector/cobaya/likelihood.py | 52 +++++---- firecrown/connector/cosmosis/likelihood.py | 7 +- firecrown/likelihood/factories.py | 4 +- 4 files changed, 158 insertions(+), 25 deletions(-) diff --git a/firecrown/ccl_factory.py b/firecrown/ccl_factory.py index 14f03e003..9f59de1be 100644 --- a/firecrown/ccl_factory.py +++ b/firecrown/ccl_factory.py @@ -25,6 +25,7 @@ import pyccl from pyccl.neutrinos import NeutrinoMassSplits +from pyccl.modified_gravity import MuSigmaMG from firecrown.updatable import Updatable from firecrown.parameters import register_new_updatable_parameter @@ -96,6 +97,79 @@ def _validate_neutrino_mass_splits(value): return value +class CCLCreationMode(YAMLSerializable, str, Enum): + """This class defines the CCL instance creation mode. + + The DEFAULT mode represents the current CCL behavior. It will use CCL's calculator + mode if `prepare` is called with a `CCLCalculatorArgs` object. Otherwise, it will + use the default CCL mode. + + The MU_SIGMA_ISITGR mode enables the mu-sigma modified gravity model with the ISiTGR + transfer function, it is not compatible with the Calculator mode. + + The PURE_CCL_MODE mode will create a CCL instance with the default parameters. It is + not compatible with the Calculator mode. + """ + + @staticmethod + def _generate_next_value_(name, _start, _count, _last_values): + return name.lower() + + DEFAULT = auto() + MU_SIGMA_ISITGR = auto() + PURE_CCL_MODE = auto() + + +def _validate_ccl_creation_mode(value): + if isinstance(value, str): + try: + return CCLCreationMode(value.lower()) # Convert from string to Enum + except ValueError as exc: + raise ValueError(f"Invalid value for CCLCreationMode: {value}") from exc + return value + + +class MuSigmaModel(Updatable): + """Model for the mu-sigma modified gravity model.""" + + def __init__(self): + """Initialize the MuSigmaModel object.""" + super().__init__(parameter_prefix="mu_sigma") + + self.mu = register_new_updatable_parameter(default_value=1.0) + self.sigma = register_new_updatable_parameter(default_value=0.0) + self.c1 = register_new_updatable_parameter(default_value=1.0) + self.c2 = register_new_updatable_parameter(default_value=1.0) + self.lambda_mg = register_new_updatable_parameter(default_value=0.0) + + def create(self) -> MuSigmaMG: + """Create a `pyccl.modified_gravity.MuSigmaMG` object.""" + if not self.is_updated(): + raise ValueError("Parameters have not been updated yet.") + + return MuSigmaMG(self.mu, self.sigma, self.c1, self.c2, self.lambda_mg) + + +class CAMBExtraParams(BaseModel): + """Extra parameters for CAMB.""" + + model_config = ConfigDict(extra="forbid") + + halofit_version: Annotated[str | None, Field(frozen=True)] = None + HMCode_A_baryon: Annotated[float | None, Field(frozen=True)] = None + HMCode_eta_baryon: Annotated[float | None, Field(frozen=True)] = None + HMCode_logT_AGN: Annotated[float | None, Field(frozen=True)] = None + kmax: Annotated[float | None, Field(frozen=True)] = None + lmax: Annotated[int | None, Field(frozen=True)] = None + dark_energy_model: Annotated[str | None, Field(frozen=True)] = None + + def get_dict(self) -> dict: + """Return the extra parameters as a dictionary.""" + return { + key: value for key, value in self.model_dump().items() if value is not None + } + + class CCLFactory(Updatable, BaseModel): """Factory class for creating instances of the `pyccl.Cosmology` class.""" @@ -111,6 +185,12 @@ class CCLFactory(Updatable, BaseModel): BeforeValidator(_validate_neutrino_mass_splits), Field(frozen=True), ] = NeutrinoMassSplits.NORMAL + creation_mode: Annotated[ + CCLCreationMode, + BeforeValidator(_validate_ccl_creation_mode), + Field(frozen=True), + ] = CCLCreationMode.DEFAULT + camb_extra_params: Annotated[CAMBExtraParams | None, Field(frozen=True)] = None def __init__(self, **data): """Initialize the CCLFactory object.""" @@ -149,6 +229,11 @@ def __init__(self, **data): default_value=ccl_cosmo["sigma8"] ) + self.mu_sigma_model: None | MuSigmaModel = None + match self.creation_mode: + case CCLCreationMode.MU_SIGMA_ISITGR: + self.mu_sigma_model = MuSigmaModel() + @model_serializer(mode="wrap") def serialize_model(self, nxt: SerializerFunctionWrapHandler, _: SerializationInfo): """Serialize the CCLFactory object.""" @@ -171,6 +256,12 @@ def serialize_mass_split(cls, value: NeutrinoMassSplits) -> str: """Serialize the mass split parameter.""" return value.name + @field_serializer("creation_mode") + @classmethod + def serialize_creation_mode(cls, value: CCLCreationMode) -> str: + """Serialize the creation mode parameter.""" + return value.name + def model_post_init(self, __context) -> None: """Initialize the WeakLensingFactory object.""" @@ -214,11 +305,38 @@ def create( else: ccl_args["nonlinear_model"] = None - return pyccl.CosmologyCalculator(**ccl_args) + if (self.creation_mode != CCLCreationMode.DEFAULT) or ( + self.camb_extra_params is not None + ): + raise ValueError( + "Calculator Mode can only be used with the DEFAULT creation " + "mode and no CAMB extra parameters." + ) + + self._ccl_cosmo = pyccl.CosmologyCalculator(**ccl_args) + return self._ccl_cosmo if self.require_nonlinear_pk: ccl_args["matter_power_spectrum"] = "halofit" + if self.camb_extra_params is not None: + ccl_args["extra_parameters"] = {"camb": self.camb_extra_params.get_dict()} + + match self.creation_mode: + case CCLCreationMode.DEFAULT: + pass + case CCLCreationMode.MU_SIGMA_ISITGR: + assert self.mu_sigma_model is not None + ccl_args.update( + mg_parametrization=self.mu_sigma_model.create(), + matter_power_spectrum="linear", + transfer_function="boltzmann_isitgr", + ) + case CCLCreationMode.PURE_CCL_MODE: + pass + case _: + raise ValueError(f"Invalid creation mode: {self.creation_mode}") + self._ccl_cosmo = pyccl.Cosmology(**ccl_args) return self._ccl_cosmo diff --git a/firecrown/connector/cobaya/likelihood.py b/firecrown/connector/cobaya/likelihood.py index 54dcc3042..3607f8435 100644 --- a/firecrown/connector/cobaya/likelihood.py +++ b/firecrown/connector/cobaya/likelihood.py @@ -11,14 +11,15 @@ from cobaya.likelihood import Likelihood from firecrown.likelihood.likelihood import load_likelihood, NamedParameters +from firecrown.likelihood.likelihood import Likelihood as FirecrownLikelihood from firecrown.parameters import ParamsMap -from firecrown.ccl_factory import PoweSpecAmplitudeParameter +from firecrown.ccl_factory import PoweSpecAmplitudeParameter, CCLCreationMode class LikelihoodConnector(Likelihood): """A class implementing cobaya.likelihood.Likelihood.""" - likelihood: Likelihood + likelihood: FirecrownLikelihood firecrownIni: str derived_parameters: list[str] = [] build_parameters: NamedParameters @@ -87,24 +88,27 @@ def get_requirements( Required by Cobaya. :return: a dictionary """ - likelihood_requires: dict[ - str, None | dict[str, npt.NDArray[np.float64]] | dict[str, object] - ] = {"pyccl_args": None, "pyccl_params": None} required_params = ( self.likelihood.required_parameters() + self.tools.required_parameters() ) - # Cosmological parameters differ from Cobaya's, so we need to remove them. - required_params -= self.tools.ccl_factory.required_parameters() + + likelihood_requires: dict[ + str, None | dict[str, npt.NDArray[np.float64]] | dict[str, object] + ] = {} + if self.tools.ccl_factory.creation_mode == CCLCreationMode.DEFAULT: + likelihood_requires.update(pyccl_args=None, pyccl_params=None) + # Cosmological parameters differ from Cobaya's boltzmann interface, so we + # need to remove them when using Calculator mode. + required_params -= self.tools.ccl_factory.required_parameters() + if ( + self.tools.ccl_factory.amplitude_parameter + == PoweSpecAmplitudeParameter.SIGMA8 + ): + likelihood_requires["sigma8"] = None for param_name in required_params.get_params_names(): likelihood_requires[param_name] = None - if ( - self.tools.ccl_factory.amplitude_parameter - == PoweSpecAmplitudeParameter.SIGMA8 - ): - likelihood_requires["sigma8"] = None - return likelihood_requires def must_provide(self, **requirements) -> None: @@ -119,14 +123,20 @@ def logp(self, **params_values) -> float: Required by Cobaya. :params values: The values of the parameters to use. """ - pyccl_args = self.provider.get_pyccl_args() - pyccl_params = self.provider.get_pyccl_params() - - derived = params_values.pop("_derived", {}) - params = ParamsMap(params_values | pyccl_params) - self.likelihood.update(params) - self.tools.update(params) - self.tools.prepare(calculator_args=pyccl_args) + if self.tools.ccl_factory.creation_mode == CCLCreationMode.DEFAULT: + pyccl_args = self.provider.get_pyccl_args() + pyccl_params = self.provider.get_pyccl_params() + derived = params_values.pop("_derived", {}) + params = ParamsMap(params_values | pyccl_params) + self.likelihood.update(params) + self.tools.update(params) + self.tools.prepare(calculator_args=pyccl_args) + else: + derived = params_values.pop("_derived", {}) + params = ParamsMap(params_values) + self.likelihood.update(params) + self.tools.update(params) + self.tools.prepare() loglike = self.likelihood.compute_loglike(self.tools) diff --git a/firecrown/connector/cosmosis/likelihood.py b/firecrown/connector/cosmosis/likelihood.py index 9163ceafa..916f1fb8b 100644 --- a/firecrown/connector/cosmosis/likelihood.py +++ b/firecrown/connector/cosmosis/likelihood.py @@ -16,6 +16,7 @@ from firecrown.likelihood.gaussfamily import GaussFamily from firecrown.likelihood.two_point import TwoPoint from firecrown.likelihood.likelihood import load_likelihood, Likelihood, NamedParameters +from firecrown.ccl_factory import CCLCreationMode from firecrown.parameters import ParamsMap from firecrown.updatable import MissingSamplerParameterError @@ -109,7 +110,6 @@ def execute(self, sample: cosmosis.datablock) -> int: sample, "cosmological_parameters" ) self.map.set_params_from_cosmosis(cosmological_params) - ccl_args = self.map.calculate_ccl_args(sample) # TODO: Future development will need to capture elements that get put into the # datablock. This probably will be in a different "physics module" and not in @@ -121,7 +121,10 @@ def execute(self, sample: cosmosis.datablock) -> int: firecrown_params.use_lower_case_keys(True) self.update_likelihood_and_tools(firecrown_params) - self.tools.prepare(calculator_args=ccl_args) + if self.tools.ccl_factory.creation_mode == CCLCreationMode.DEFAULT: + self.tools.prepare(calculator_args=self.map.calculate_ccl_args(sample)) + else: + self.tools.prepare() loglike = self.likelihood.compute_loglike(self.tools) derived_params_collection = self.likelihood.get_derived_parameters() diff --git a/firecrown/likelihood/factories.py b/firecrown/likelihood/factories.py index a00510a9a..d96349fbd 100644 --- a/firecrown/likelihood/factories.py +++ b/firecrown/likelihood/factories.py @@ -35,6 +35,7 @@ check_two_point_consistence_harmonic, ) from firecrown.modeling_tools import ModelingTools +from firecrown.ccl_factory import CCLFactory from firecrown.utils import YAMLSerializable @@ -101,6 +102,7 @@ class TwoPointExperiment(BaseModel): two_point_factory: TwoPointFactory data_source: DataSourceSacc + ccl_factory: CCLFactory | None = None def model_post_init(self, __context) -> None: """Initialize the TwoPointExperiment object.""" @@ -131,7 +133,7 @@ def build_two_point_likelihood( raise ValueError("No likelihood config found.") exp = TwoPointExperiment.model_validate(likelihood_config, strict=True) - modeling_tools = ModelingTools() + modeling_tools = ModelingTools(ccl_factory=exp.ccl_factory) # Load the SACC file sacc_data = exp.data_source.get_sacc_data() From 78129222cf9daa7125451f7a1695174c06772868 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 10:38:30 -0300 Subject: [PATCH 03/25] Reorganized cobaya example in subdiretorie. --- .../evaluate.yaml} | 4 +- .../evaluate_PT.yaml} | 4 +- .../cobaya/evaluate_pure_ccl.yaml | 162 +++++++++++++++++ examples/des_y1_3x2pt/cobaya/mcmc.yaml | 168 ++++++++++++++++++ examples/des_y1_3x2pt/cobaya_mcmc.yaml | 40 ----- 5 files changed, 334 insertions(+), 44 deletions(-) rename examples/des_y1_3x2pt/{cobaya_evaluate.yaml => cobaya/evaluate.yaml} (98%) rename examples/des_y1_3x2pt/{cobaya_evaluate_PT.yaml => cobaya/evaluate_PT.yaml} (95%) create mode 100644 examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml create mode 100644 examples/des_y1_3x2pt/cobaya/mcmc.yaml delete mode 100644 examples/des_y1_3x2pt/cobaya_mcmc.yaml diff --git a/examples/des_y1_3x2pt/cobaya_evaluate.yaml b/examples/des_y1_3x2pt/cobaya/evaluate.yaml similarity index 98% rename from examples/des_y1_3x2pt/cobaya_evaluate.yaml rename to examples/des_y1_3x2pt/cobaya/evaluate.yaml index d4dbaa58b..1a7cab405 100644 --- a/examples/des_y1_3x2pt/cobaya_evaluate.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: des_y1_3x2pt.py + firecrownIni: ../des_y1_3x2pt.py derived_parameters: - TwoPoint__NumberCountsScale_lens0 - TwoPoint__NumberCountsScale_lens1 @@ -182,7 +182,7 @@ params: sampler: evaluate: null stop_at_error: true -output: cobaya_evaluate_output +output: output packages_path: null test: false debug: false diff --git a/examples/des_y1_3x2pt/cobaya_evaluate_PT.yaml b/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml similarity index 95% rename from examples/des_y1_3x2pt/cobaya_evaluate_PT.yaml rename to examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml index 05335bf89..ab4e763d9 100644 --- a/examples/des_y1_3x2pt/cobaya_evaluate_PT.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: des_y1_3x2pt_PT.py + firecrownIni: ../des_y1_3x2pt_PT.py params: As: prior: @@ -96,7 +96,7 @@ params: sampler: evaluate: null stop_at_error: true -output: cobaya_evaluate_output_PT +output: output_PT packages_path: null test: false debug: false diff --git a/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml b/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml new file mode 100644 index 000000000..4fd5789ac --- /dev/null +++ b/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml @@ -0,0 +1,162 @@ +likelihood: + des_y1_3x2pt: + external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' + firecrownIni: firecrown.likelihood.factories.build_two_point_likelihood + build_parameters: + likelihood_config: ../des_y1_3x2pt_pure_ccl_experiment.yaml +params: + sigma8: + prior: + min: 0.6 + max: 1.0 + ref: 0.8 + proposal: 0.01 + Omega_b: 0.05 + Omega_c: + prior: + min: 0.05 + max: 0.35 + ref: 0.25 + proposal: 0.01 + Omega_k: 0.0 + h: 0.682 + Neff: 3.046 + m_nu: 0.06 + n_s: 0.971 + w0: -1.0 + wa: 0.0 + T_CMB: 2.7255 + +# Likelihood params +# - IA model + ia_bias: + ref: 0.5 + prior: + min: -5.0 + max: +5.0 + alphaz: + ref: 0.0 + prior: + min: -5.0 + max: +5.0 + +# - these parameters are fixed + z_piv: 0.62 + +# - linear bias for lenses + lens0_bias: + ref: 1.4 + prior: + min: 0.8 + max: 3.0 + lens1_bias: + ref: 1.6 + prior: + min: 0.8 + max: 3.0 + lens2_bias: + ref: 1.6 + prior: + min: 0.8 + max: 3.0 + lens3_bias: + ref: 1.9 + prior: + min: 0.8 + max: 3.0 + lens4_bias: + ref: 2.0 + prior: + min: 0.8 + max: 3.0 + +# - photoz shifts for the lensing sources + src0_delta_z: + ref: -0.001 + prior: + dist: norm + loc: -0.001 + scale: 0.016 + src1_delta_z: + ref: -0.019 + prior: + dist: norm + loc: -0.019 + scale: 0.013 + src2_delta_z: + ref: +0.009 + prior: + dist: norm + loc: 0.009 + scale: 0.011 + src3_delta_z: + ref: -0.018 + prior: + dist: norm + loc: -0.018 + scale: 0.022 + +# - photoz shifts for the lenses + lens0_delta_z: + ref: 0.001 + prior: + dist: norm + loc: 0.001 + scale: 0.008 + lens1_delta_z: + ref: 0.002 + prior: + dist: norm + loc: 0.002 + scale: 0.007 + lens2_delta_z: + ref: 0.001 + prior: + dist: norm + loc: 0.001 + scale: 0.007 + lens3_delta_z: + ref: 0.003 + prior: + dist: norm + loc: 0.003 + scale: 0.01 + lens4_delta_z: + ref: 0.0 + prior: + dist: norm + loc: 0.0 + scale: 0.01 + +# - shear errors + src0_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src1_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src2_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src3_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 +sampler: + evaluate: null +stop_at_error: true +output: output_pure_ccl +packages_path: null +test: false +debug: false diff --git a/examples/des_y1_3x2pt/cobaya/mcmc.yaml b/examples/des_y1_3x2pt/cobaya/mcmc.yaml new file mode 100644 index 000000000..41f45042a --- /dev/null +++ b/examples/des_y1_3x2pt/cobaya/mcmc.yaml @@ -0,0 +1,168 @@ +theory: + camb: + stop_at_error: true + extra_args: + num_massive_neutrinos: 1 + halofit_version: mead + fcc_ccl: + external: !!python/name:firecrown.connector.cobaya.ccl.CCLConnector '' + input_style: CAMB +likelihood: + des_y1_3x2pt: + external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' + firecrownIni: ../des_y1_3x2pt.py +params: + As: + prior: + min: 5.0e-10 + max: 8.0e-09 + ref: 2.0e-09 + proposal: 1.0e-10 + ombh2: 0.02242 + omch2: + prior: + min: 0.05 + max: 0.2 + ref: 0.11933 + proposal: 0.01 + H0: 67.66 + tau: 0.0561 + mnu: 0.06 + nnu: 3.046 + ns: 0.9665 + YHe: 0.2454 + +# Likelihood params +# - IA model + ia_bias: + ref: 0.5 + prior: + min: -5.0 + max: +5.0 + alphaz: + ref: 0.0 + prior: + min: -5.0 + max: +5.0 + +# - these parameters are fixed + z_piv: 0.62 + +# - linear bias for lenses + lens0_bias: + ref: 1.4 + prior: + min: 0.8 + max: 3.0 + lens1_bias: + ref: 1.6 + prior: + min: 0.8 + max: 3.0 + lens2_bias: + ref: 1.6 + prior: + min: 0.8 + max: 3.0 + lens3_bias: + ref: 1.9 + prior: + min: 0.8 + max: 3.0 + lens4_bias: + ref: 2.0 + prior: + min: 0.8 + max: 3.0 + +# - photoz shifts for the lensing sources + src0_delta_z: + ref: -0.001 + prior: + dist: norm + loc: -0.001 + scale: 0.016 + src1_delta_z: + ref: -0.019 + prior: + dist: norm + loc: -0.019 + scale: 0.013 + src2_delta_z: + ref: +0.009 + prior: + dist: norm + loc: 0.009 + scale: 0.011 + src3_delta_z: + ref: -0.018 + prior: + dist: norm + loc: -0.018 + scale: 0.022 + +# - photoz shifts for the lenses + lens0_delta_z: + ref: 0.001 + prior: + dist: norm + loc: 0.001 + scale: 0.008 + lens1_delta_z: + ref: 0.002 + prior: + dist: norm + loc: 0.002 + scale: 0.007 + lens2_delta_z: + ref: 0.001 + prior: + dist: norm + loc: 0.001 + scale: 0.007 + lens3_delta_z: + ref: 0.003 + prior: + dist: norm + loc: 0.003 + scale: 0.01 + lens4_delta_z: + ref: 0.0 + prior: + dist: norm + loc: 0.0 + scale: 0.01 + +# - shear errors + src0_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src1_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src2_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src3_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + +sampler: + mcmc: + Rminus1_stop: 0.01 + max_tries: 1000 +stop_at_error: true +output: output_mcmc +resume: True diff --git a/examples/des_y1_3x2pt/cobaya_mcmc.yaml b/examples/des_y1_3x2pt/cobaya_mcmc.yaml deleted file mode 100644 index f5c9f56dc..000000000 --- a/examples/des_y1_3x2pt/cobaya_mcmc.yaml +++ /dev/null @@ -1,40 +0,0 @@ -theory: - camb: - stop_at_error: true - extra_args: - num_massive_neutrinos: 1 - halofit_version: mead - fcc_ccl: - external: !!python/name:firecrown.connector.cobaya.ccl.CCLConnector '' - input_style: CAMB -likelihood: - des_y1_3x2pt: - external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: des_y1_3x2pt.yaml -params: - As: - prior: - min: 5.0e-10 - max: 8.0e-09 - ref: 2.0e-09 - proposal: 1.0e-10 - ombh2: 0.02242 - omch2: - prior: - min: 0.05 - max: 0.2 - ref: 0.11933 - proposal: 0.01 - H0: 67.66 - tau: 0.0561 - mnu: 0.06 - nnu: 3.046 - ns: 0.9665 - YHe: 0.2454 -sampler: - mcmc: - Rminus1_stop: 0.01 - max_tries: 1000 -stop_at_error: true -output: cobaya_mcmc_output_mpi2 -resume: True From cfe4eeb32bff1dbf41ea70805947c807436efa90 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 10:39:20 -0300 Subject: [PATCH 04/25] Handling experiment loading directories. --- firecrown/likelihood/factories.py | 48 ++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/firecrown/likelihood/factories.py b/firecrown/likelihood/factories.py index d96349fbd..6523eb174 100644 --- a/firecrown/likelihood/factories.py +++ b/firecrown/likelihood/factories.py @@ -85,16 +85,28 @@ class DataSourceSacc(BaseModel): """Model for the data source in a likelihood configuration.""" sacc_data_file: str + _path: Path | None = None - def model_post_init(self, __context) -> None: - """Initialize the DataSourceSacc object.""" - sacc_data_file = Path(self.sacc_data_file) - if not sacc_data_file.exists(): - raise FileNotFoundError(f"File {sacc_data_file} does not exist") + def set_path(self, path: Path) -> None: + """Set the path for the data source.""" + self._path = path def get_sacc_data(self) -> sacc.Sacc: """Load the SACC data file.""" - return sacc.Sacc.load_fits(self.sacc_data_file) + sacc_data_path = Path(self.sacc_data_file) + # If sacc_data_file is absolute, use it directly + if sacc_data_path.is_absolute(): + return sacc.Sacc.load_fits(self.sacc_data_file) + # If path is set, use it to find the file + if self._path is not None: + full_sacc_data_path = self._path / sacc_data_path + if full_sacc_data_path.exists(): + return sacc.Sacc.load_fits(full_sacc_data_path) + # If path is not set, use the current directory + if sacc_data_path.exists(): + return sacc.Sacc.load_fits(sacc_data_path) + # If the file does not exist, raise an error + raise FileNotFoundError(f"File {sacc_data_path} does not exist") class TwoPointExperiment(BaseModel): @@ -107,6 +119,21 @@ class TwoPointExperiment(BaseModel): def model_post_init(self, __context) -> None: """Initialize the TwoPointExperiment object.""" + @classmethod + def load_from_yaml(cls, file: str | Path) -> "TwoPointExperiment": + """Load a TwoPointExperiment object from a YAML file.""" + if isinstance(file, str): + file = Path(file) + + # Determine file directory + file_dir = file.parent + with open(file, "r", encoding="utf-8") as f: + config = yaml.safe_load(f) + tpe = cls.model_validate(config, strict=True) + + tpe.data_source.set_path(file_dir) + return tpe + def build_two_point_likelihood( build_parameters: NamedParameters, @@ -125,14 +152,7 @@ def build_two_point_likelihood( - statistic_factories: A YAML file containing the statistic factories to use. """ likelihood_config_file = build_parameters.get_string("likelihood_config") - - with open(likelihood_config_file, "r", encoding="utf-8") as f: - likelihood_config = yaml.safe_load(f) - - if likelihood_config is None: - raise ValueError("No likelihood config found.") - - exp = TwoPointExperiment.model_validate(likelihood_config, strict=True) + exp = TwoPointExperiment.load_from_yaml(likelihood_config_file) modeling_tools = ModelingTools(ccl_factory=exp.ccl_factory) # Load the SACC file From 8a38848768ab3491800023a0e29b74f8c04932a5 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 10:52:35 -0300 Subject: [PATCH 05/25] Reorganized cosmosis example subdir. --- .../default_factory.ini} | 4 +- .../factory.ini} | 4 +- .../factory_PT.ini} | 4 +- .../cosmosis/pure_ccl_default_factory.ini | 46 +++++++++++++++++++ .../values.ini} | 0 .../values_PT.ini} | 0 .../des_y1_3x2pt_pure_ccl_experiment.yaml | 32 +++++++++++++ 7 files changed, 84 insertions(+), 6 deletions(-) rename examples/des_y1_3x2pt/{des_y1_3x2pt_default_factory.ini => cosmosis/default_factory.ini} (92%) rename examples/des_y1_3x2pt/{des_y1_3x2pt.ini => cosmosis/factory.ini} (92%) rename examples/des_y1_3x2pt/{des_y1_3x2pt_PT.ini => cosmosis/factory_PT.ini} (91%) create mode 100644 examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini rename examples/des_y1_3x2pt/{des_y1_3x2pt_values.ini => cosmosis/values.ini} (100%) rename examples/des_y1_3x2pt/{des_y1_3x2pt_PT_values.ini => cosmosis/values_PT.ini} (100%) create mode 100644 examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_default_factory.ini b/examples/des_y1_3x2pt/cosmosis/default_factory.ini similarity index 92% rename from examples/des_y1_3x2pt/des_y1_3x2pt_default_factory.ini rename to examples/des_y1_3x2pt/cosmosis/default_factory.ini index 8f6a211c1..a1eb169e7 100644 --- a/examples/des_y1_3x2pt/des_y1_3x2pt_default_factory.ini +++ b/examples/des_y1_3x2pt/cosmosis/default_factory.ini @@ -12,7 +12,7 @@ verbosity = 0 [pipeline] modules = consistency camb firecrown_likelihood -values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_values.ini +values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/cosmosis/values.ini likelihoods = firecrown quiet = T debug = T @@ -48,7 +48,7 @@ sampling_parameters_sections = firecrown_two_point [test] fatal_errors = T -save_dir = des_y1_3x2pt_output +save_dir = output_default_factory [metropolis] samples = 1000 diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt.ini b/examples/des_y1_3x2pt/cosmosis/factory.ini similarity index 92% rename from examples/des_y1_3x2pt/des_y1_3x2pt.ini rename to examples/des_y1_3x2pt/cosmosis/factory.ini index ea10dca2c..6c57aac24 100644 --- a/examples/des_y1_3x2pt/des_y1_3x2pt.ini +++ b/examples/des_y1_3x2pt/cosmosis/factory.ini @@ -12,7 +12,7 @@ verbosity = 0 [pipeline] modules = consistency camb firecrown_likelihood -values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_values.ini +values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/cosmosis/values.ini likelihoods = firecrown quiet = T debug = T @@ -46,7 +46,7 @@ sampling_parameters_sections = firecrown_two_point [test] fatal_errors = T -save_dir = des_y1_3x2pt_output +save_dir = output_factory [metropolis] samples = 1000 diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_PT.ini b/examples/des_y1_3x2pt/cosmosis/factory_PT.ini similarity index 91% rename from examples/des_y1_3x2pt/des_y1_3x2pt_PT.ini rename to examples/des_y1_3x2pt/cosmosis/factory_PT.ini index aa786bc36..5b75b3233 100644 --- a/examples/des_y1_3x2pt/des_y1_3x2pt_PT.ini +++ b/examples/des_y1_3x2pt/cosmosis/factory_PT.ini @@ -12,7 +12,7 @@ verbosity = 0 [pipeline] modules = consistency camb firecrown_likelihood -values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_PT_values.ini +values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/cosmosis/values_PT.ini likelihoods = firecrown quiet = T debug = T @@ -45,7 +45,7 @@ sampling_parameters_sections = firecrown_two_point [test] fatal_errors = T -save_dir = des_y1_3x2pt_output +save_dir = output_factory_PT [metropolis] samples = 1000 diff --git a/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini b/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini new file mode 100644 index 000000000..f7bc10867 --- /dev/null +++ b/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini @@ -0,0 +1,46 @@ +[runtime] +sampler = test +root = ${PWD} + +[DEFAULT] +fatal_errors = T + +[output] +filename = output/des_y1_3x2pt_samples.txt +format = text +verbosity = 0 + +[pipeline] +modules = consistency firecrown_likelihood +values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/cosmosis/values.ini +likelihoods = firecrown +quiet = T +debug = T +timing = T + +[consistency] +file = ${CSL_DIR}/utility/consistency/consistency_interface.py + +[firecrown_likelihood] +;; Fix this to use an environment variable to find the files. +;; Set FIRECROWN_DIR to the base of the firecrown installation (or build, if you haven't +;; installed it) +file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py +likelihood_source = firecrown.likelihood.factories.build_two_point_likelihood +likelihood_config = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml +;; Connector settings +require_nonlinear_pk = True +sampling_parameters_sections = firecrown_two_point + +[test] +fatal_errors = T +save_dir = output_pure_ccl + +[metropolis] +samples = 1000 +nsteps = 1 + +[emcee] +walkers = 64 +samples = 400 +nsteps = 10 diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_values.ini b/examples/des_y1_3x2pt/cosmosis/values.ini similarity index 100% rename from examples/des_y1_3x2pt/des_y1_3x2pt_values.ini rename to examples/des_y1_3x2pt/cosmosis/values.ini diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_PT_values.ini b/examples/des_y1_3x2pt/cosmosis/values_PT.ini similarity index 100% rename from examples/des_y1_3x2pt/des_y1_3x2pt_PT_values.ini rename to examples/des_y1_3x2pt/cosmosis/values_PT.ini diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml b/examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml new file mode 100644 index 000000000..b5a580856 --- /dev/null +++ b/examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml @@ -0,0 +1,32 @@ +--- + +# This file contains all information about the DES Y1 3x2pt experiment. + +# The 'data_source:' field points to the SACC file containing the DES Y1 3x2pt data vector +# and covariance matrix, which is used for analysis. + +# The 'two_point_factory:' field points to the factory that will create the TwoPoint +# objects. These objects represent the chosen theoretical models for each data point +# found in the SACC file. + +data_source: + sacc_data_file: des_y1_3x2pt_sacc_data.fits + +# The two point statistics are defined by the TwoPoint objects. The TwoPoint statistics +# are created using the factories defined in this file. +two_point_factory: + correlation_space: real + number_counts_factory: + global_systematics: [] + per_bin_systematics: + - type: PhotoZShiftFactory + weak_lensing_factory: + global_systematics: + - alphag: 1 + type: LinearAlignmentSystematicFactory + per_bin_systematics: + - type: MultiplicativeShearBiasFactory + - type: PhotoZShiftFactory + +ccl_factory: + creation_mode: 'pure_ccl_mode' From c1a9f90c5649704c7645f5a330d18cdb5a173c08 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 11:04:39 -0300 Subject: [PATCH 06/25] Renaming example files. --- examples/des_y1_3x2pt/cobaya/evaluate.yaml | 2 +- examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml | 2 +- examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml | 2 +- examples/des_y1_3x2pt/cobaya/mcmc.yaml | 2 +- examples/des_y1_3x2pt/cosmosis/default_factory.ini | 2 +- examples/des_y1_3x2pt/cosmosis/factory.ini | 2 +- examples/des_y1_3x2pt/cosmosis/factory_PT.ini | 2 +- examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini | 2 +- .../{des_y1_3x2pt_experiment.yaml => experiment.yaml} | 2 +- examples/des_y1_3x2pt/{des_y1_3x2pt.py => factory.py} | 4 +--- examples/des_y1_3x2pt/{des_y1_3x2pt_PT.py => factory_PT.py} | 4 +--- ...x2pt_pure_ccl_experiment.yaml => pure_ccl_experiment.yaml} | 2 +- .../{des_y1_3x2pt_sacc_data.fits => sacc_data.fits} | 0 13 files changed, 12 insertions(+), 16 deletions(-) rename examples/des_y1_3x2pt/{des_y1_3x2pt_experiment.yaml => experiment.yaml} (95%) rename examples/des_y1_3x2pt/{des_y1_3x2pt.py => factory.py} (97%) mode change 100755 => 100644 rename examples/des_y1_3x2pt/{des_y1_3x2pt_PT.py => factory_PT.py} (99%) mode change 100755 => 100644 rename examples/des_y1_3x2pt/{des_y1_3x2pt_pure_ccl_experiment.yaml => pure_ccl_experiment.yaml} (95%) rename examples/des_y1_3x2pt/{des_y1_3x2pt_sacc_data.fits => sacc_data.fits} (100%) diff --git a/examples/des_y1_3x2pt/cobaya/evaluate.yaml b/examples/des_y1_3x2pt/cobaya/evaluate.yaml index 1a7cab405..58e68c1fe 100644 --- a/examples/des_y1_3x2pt/cobaya/evaluate.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: ../des_y1_3x2pt.py + firecrownIni: ../factory.py derived_parameters: - TwoPoint__NumberCountsScale_lens0 - TwoPoint__NumberCountsScale_lens1 diff --git a/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml b/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml index ab4e763d9..b19f92334 100644 --- a/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: ../des_y1_3x2pt_PT.py + firecrownIni: ../factory_PT.py params: As: prior: diff --git a/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml b/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml index 4fd5789ac..1eb3bf9e0 100644 --- a/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml @@ -3,7 +3,7 @@ likelihood: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' firecrownIni: firecrown.likelihood.factories.build_two_point_likelihood build_parameters: - likelihood_config: ../des_y1_3x2pt_pure_ccl_experiment.yaml + likelihood_config: ../pure_ccl_experiment.yaml params: sigma8: prior: diff --git a/examples/des_y1_3x2pt/cobaya/mcmc.yaml b/examples/des_y1_3x2pt/cobaya/mcmc.yaml index 41f45042a..3a064dab5 100644 --- a/examples/des_y1_3x2pt/cobaya/mcmc.yaml +++ b/examples/des_y1_3x2pt/cobaya/mcmc.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: ../des_y1_3x2pt.py + firecrownIni: ../factory.py params: As: prior: diff --git a/examples/des_y1_3x2pt/cosmosis/default_factory.ini b/examples/des_y1_3x2pt/cosmosis/default_factory.ini index a1eb169e7..055beccd7 100644 --- a/examples/des_y1_3x2pt/cosmosis/default_factory.ini +++ b/examples/des_y1_3x2pt/cosmosis/default_factory.ini @@ -41,7 +41,7 @@ nk = 1000 ;; installed it) file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py likelihood_source = firecrown.likelihood.factories.build_two_point_likelihood -likelihood_config = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_experiment.yaml +likelihood_config = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/experiment.yaml ;; Connector settings require_nonlinear_pk = True sampling_parameters_sections = firecrown_two_point diff --git a/examples/des_y1_3x2pt/cosmosis/factory.ini b/examples/des_y1_3x2pt/cosmosis/factory.ini index 6c57aac24..271cb35aa 100644 --- a/examples/des_y1_3x2pt/cosmosis/factory.ini +++ b/examples/des_y1_3x2pt/cosmosis/factory.ini @@ -40,7 +40,7 @@ nk = 1000 ;; Set FIRECROWN_DIR to the base of the firecrown installation (or build, if you haven't ;; installed it) file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py -likelihood_source = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt.py +likelihood_source = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/factory.py require_nonlinear_pk = True sampling_parameters_sections = firecrown_two_point diff --git a/examples/des_y1_3x2pt/cosmosis/factory_PT.ini b/examples/des_y1_3x2pt/cosmosis/factory_PT.ini index 5b75b3233..6dde09546 100644 --- a/examples/des_y1_3x2pt/cosmosis/factory_PT.ini +++ b/examples/des_y1_3x2pt/cosmosis/factory_PT.ini @@ -39,7 +39,7 @@ nk = 1000 ;; Set FIRECROWN_DIR to the base of the firecrown installation (or build, if you haven't ;; installed it) file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py -likelihood_source = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_PT.py +likelihood_source = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/factory_PT.py require_nonlinear_pk = True sampling_parameters_sections = firecrown_two_point diff --git a/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini b/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini index f7bc10867..1f5e3c64f 100644 --- a/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini +++ b/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini @@ -27,7 +27,7 @@ file = ${CSL_DIR}/utility/consistency/consistency_interface.py ;; installed it) file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py likelihood_source = firecrown.likelihood.factories.build_two_point_likelihood -likelihood_config = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml +likelihood_config = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/pure_ccl_experiment.yaml ;; Connector settings require_nonlinear_pk = True sampling_parameters_sections = firecrown_two_point diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_experiment.yaml b/examples/des_y1_3x2pt/experiment.yaml similarity index 95% rename from examples/des_y1_3x2pt/des_y1_3x2pt_experiment.yaml rename to examples/des_y1_3x2pt/experiment.yaml index 298317a39..218b9b239 100644 --- a/examples/des_y1_3x2pt/des_y1_3x2pt_experiment.yaml +++ b/examples/des_y1_3x2pt/experiment.yaml @@ -10,7 +10,7 @@ # found in the SACC file. data_source: - sacc_data_file: des_y1_3x2pt_sacc_data.fits + sacc_data_file: sacc_data.fits # The two point statistics are defined by the TwoPoint objects. The TwoPoint statistics # are created using the factories defined in this file. diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt.py b/examples/des_y1_3x2pt/factory.py old mode 100755 new mode 100644 similarity index 97% rename from examples/des_y1_3x2pt/des_y1_3x2pt.py rename to examples/des_y1_3x2pt/factory.py index fb10ffa43..5e2428546 --- a/examples/des_y1_3x2pt/des_y1_3x2pt.py +++ b/examples/des_y1_3x2pt/factory.py @@ -102,9 +102,7 @@ def build_likelihood(_): # We load the correct SACC file. saccfile = os.path.expanduser( - os.path.expandvars( - "${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits" - ) + os.path.expandvars("${FIRECROWN_DIR}/examples/des_y1_3x2pt/sacc_data.fits") ) sacc_data = sacc.Sacc.load_fits(saccfile) diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_PT.py b/examples/des_y1_3x2pt/factory_PT.py old mode 100755 new mode 100644 similarity index 99% rename from examples/des_y1_3x2pt/des_y1_3x2pt_PT.py rename to examples/des_y1_3x2pt/factory_PT.py index 9431b9886..32890f0eb --- a/examples/des_y1_3x2pt/des_y1_3x2pt_PT.py +++ b/examples/des_y1_3x2pt/factory_PT.py @@ -25,9 +25,7 @@ saccfile = os.path.expanduser( - os.path.expandvars( - "${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits" - ) + os.path.expandvars("${FIRECROWN_DIR}/examples/des_y1_3x2pt/sacc_data.fits") ) diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml b/examples/des_y1_3x2pt/pure_ccl_experiment.yaml similarity index 95% rename from examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml rename to examples/des_y1_3x2pt/pure_ccl_experiment.yaml index b5a580856..af4bd33d8 100644 --- a/examples/des_y1_3x2pt/des_y1_3x2pt_pure_ccl_experiment.yaml +++ b/examples/des_y1_3x2pt/pure_ccl_experiment.yaml @@ -10,7 +10,7 @@ # found in the SACC file. data_source: - sacc_data_file: des_y1_3x2pt_sacc_data.fits + sacc_data_file: sacc_data.fits # The two point statistics are defined by the TwoPoint objects. The TwoPoint statistics # are created using the factories defined in this file. diff --git a/examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits b/examples/des_y1_3x2pt/sacc_data.fits similarity index 100% rename from examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits rename to examples/des_y1_3x2pt/sacc_data.fits From 7f8180779c5cf0870462db2d1ae9c81b46ccdf7d Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 11:15:26 -0300 Subject: [PATCH 07/25] Better path specification for cobaya. --- examples/des_y1_3x2pt/cobaya/evaluate.yaml | 2 +- examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml | 2 +- examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml | 2 +- examples/des_y1_3x2pt/cobaya/mcmc.yaml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/des_y1_3x2pt/cobaya/evaluate.yaml b/examples/des_y1_3x2pt/cobaya/evaluate.yaml index 58e68c1fe..52c4fcf20 100644 --- a/examples/des_y1_3x2pt/cobaya/evaluate.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: ../factory.py + firecrownIni: ${FIRECROWN_DIR}/examples/des_y1_3x2pt/factory.py derived_parameters: - TwoPoint__NumberCountsScale_lens0 - TwoPoint__NumberCountsScale_lens1 diff --git a/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml b/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml index b19f92334..da48e5d23 100644 --- a/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate_PT.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: ../factory_PT.py + firecrownIni: ${FIRECROWN_DIR}/examples/des_y1_3x2pt/factory_PT.py params: As: prior: diff --git a/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml b/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml index 1eb3bf9e0..3ab2867fd 100644 --- a/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml +++ b/examples/des_y1_3x2pt/cobaya/evaluate_pure_ccl.yaml @@ -3,7 +3,7 @@ likelihood: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' firecrownIni: firecrown.likelihood.factories.build_two_point_likelihood build_parameters: - likelihood_config: ../pure_ccl_experiment.yaml + likelihood_config: ${FIRECROWN_DIR}/examples/des_y1_3x2pt/pure_ccl_experiment.yaml params: sigma8: prior: diff --git a/examples/des_y1_3x2pt/cobaya/mcmc.yaml b/examples/des_y1_3x2pt/cobaya/mcmc.yaml index 3a064dab5..1f374f988 100644 --- a/examples/des_y1_3x2pt/cobaya/mcmc.yaml +++ b/examples/des_y1_3x2pt/cobaya/mcmc.yaml @@ -10,7 +10,7 @@ theory: likelihood: des_y1_3x2pt: external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' - firecrownIni: ../factory.py + firecrownIni: ${FIRECROWN_DIR}/examples/des_y1_3x2pt/factory.py params: As: prior: From 90a2a43e9044a0cf48ab1b045779d2756495a213 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 11:24:44 -0300 Subject: [PATCH 08/25] Adding support for pure_ccl in numcosmo connector. --- firecrown/connector/numcosmo/numcosmo.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/firecrown/connector/numcosmo/numcosmo.py b/firecrown/connector/numcosmo/numcosmo.py index 0e39f934e..ebdd087e4 100644 --- a/firecrown/connector/numcosmo/numcosmo.py +++ b/firecrown/connector/numcosmo/numcosmo.py @@ -22,6 +22,7 @@ PowerSpec, CCLFactory, PoweSpecAmplitudeParameter, + CCLCreationMode, ) @@ -568,12 +569,16 @@ def do_prepare( # pylint: disable-msg=arguments-differ self.tools.reset() self._nc_mapping.set_params_from_numcosmo(mset, self.tools.ccl_factory) - ccl_args = self._nc_mapping.calculate_ccl_args(mset) params_map = self._nc_mapping.create_params_map(self.model_list, mset) self.likelihood.update(params_map) self.tools.update(params_map) - self.tools.prepare(calculator_args=ccl_args) + if self.tools.ccl_factory.creation_mode == CCLCreationMode.DEFAULT: + self.tools.prepare( + calculator_args=self._nc_mapping.calculate_ccl_args(mset) + ) + else: + self.tools.prepare() def do_m2lnL_val(self, _) -> float: # pylint: disable-msg=arguments-differ """Implements the virtual method `m2lnL`. @@ -840,12 +845,16 @@ def do_prepare( # pylint: disable-msg=arguments-differ self.tools.reset() self._nc_mapping.set_params_from_numcosmo(mset, self.tools.ccl_factory) - ccl_args = self._nc_mapping.calculate_ccl_args(mset) params_map = self._nc_mapping.create_params_map(self._model_list, mset) self.likelihood.update(params_map) self.tools.update(params_map) - self.tools.prepare(calculator_args=ccl_args) + if self.tools.ccl_factory.creation_mode == CCLCreationMode.DEFAULT: + self.tools.prepare( + calculator_args=self._nc_mapping.calculate_ccl_args(mset) + ) + else: + self.tools.prepare() # pylint: disable-next=arguments-differ def do_mean_func(self, _, vp) -> None: From f2cc227fc5a620b020cabfe31829ae7f34316723 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 11:42:31 -0300 Subject: [PATCH 09/25] Updated tests. --- tests/integration/test_des_y1_3x2pt.py | 36 +++++++++++++++++++------- tests/likelihood/test_factories.py | 9 ++++--- tests/test_pt_systematics.py | 2 +- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/tests/integration/test_des_y1_3x2pt.py b/tests/integration/test_des_y1_3x2pt.py index 99fee1ab3..1d45cfd7a 100644 --- a/tests/integration/test_des_y1_3x2pt.py +++ b/tests/integration/test_des_y1_3x2pt.py @@ -13,9 +13,10 @@ def test_des_y1_3x2pt_cosmosis(): """ set -e cd examples/des_y1_3x2pt - cosmosis des_y1_3x2pt.ini - cosmosis des_y1_3x2pt_PT.ini - cosmosis des_y1_3x2pt_default_factory.ini + cosmosis cosmosis/factory.ini + cosmosis cosmosis/factory_PT.ini + cosmosis cosmosis/default_factory.ini + cosmosis cosmosis/pure_ccl_default_factory.ini """, ], capture_output=True, @@ -36,12 +37,28 @@ def test_des_y1_3x2pt_numcosmo(): """ set -e cd examples/des_y1_3x2pt - numcosmo from-cosmosis des_y1_3x2pt.ini --matter-ps eisenstein_hu\\ + mkdir -p numcosmo + cd numcosmo + + numcosmo from-cosmosis ../cosmosis/factory.ini \\ + --matter-ps eisenstein_hu \\ + --nonlin-matter-ps halofit + numcosmo run test factory.yaml + + numcosmo from-cosmosis ../cosmosis/factory_PT.ini \\ + --matter-ps eisenstein_hu \\ --nonlin-matter-ps halofit - numcosmo run test des_y1_3x2pt.yaml - numcosmo from-cosmosis des_y1_3x2pt_PT.ini --matter-ps eisenstein_hu\\ + numcosmo run test factory_PT.yaml + + numcosmo from-cosmosis ../cosmosis/default_factory.ini \\ + --matter-ps eisenstein_hu \\ + --nonlin-matter-ps halofit + numcosmo run test default_factory.yaml + + numcosmo from-cosmosis ../cosmosis/pure_ccl_default_factory.ini \\ + --matter-ps eisenstein_hu \\ --nonlin-matter-ps halofit - numcosmo run test des_y1_3x2pt_PT.yaml + numcosmo run test pure_ccl_default_factory.yaml """, ], capture_output=True, @@ -62,8 +79,9 @@ def test_des_y1_3x2pt_cobaya(): """ set -e cd examples/des_y1_3x2pt - cobaya-run cobaya_evaluate.yaml - cobaya-run cobaya_evaluate_PT.yaml + cobaya-run cobaya/evaluate.yaml + cobaya-run cobaya/evaluate_PT.yaml + cobaya-run cobaya/evaluate_pure_ccl.yaml """, ], capture_output=True, diff --git a/tests/likelihood/test_factories.py b/tests/likelihood/test_factories.py index 45c15da1c..df71df960 100644 --- a/tests/likelihood/test_factories.py +++ b/tests/likelihood/test_factories.py @@ -99,10 +99,11 @@ def test_data_source_sacc_direct() -> None: def test_data_source_sacc_invalid_file() -> None: data_source_sacc_dict = {"sacc_data_file": "tests/file_not_found.sacc.gz"} + data_source = DataSourceSacc.model_validate(data_source_sacc_dict) with pytest.raises( FileNotFoundError, match=".*File tests/file_not_found.sacc.gz does not exist.*" ): - DataSourceSacc.model_validate(data_source_sacc_dict) + _ = data_source.get_sacc_data() def test_data_source_sacc_get_sacc_data() -> None: @@ -209,7 +210,7 @@ def test_build_two_point_likelihood_real(tmp_path: Path) -> None: per_bin_systematics: [] global_systematics: [] data_source: - sacc_data_file: examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits + sacc_data_file: examples/des_y1_3x2pt/sacc_data.fits """ ) @@ -282,7 +283,7 @@ def test_build_two_point_likelihood_harmonic_no_harmonic_data(tmp_path: Path) -> per_bin_systematics: [] global_systematics: [] data_source: - sacc_data_file: examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits + sacc_data_file: examples/des_y1_3x2pt/sacc_data.fits """ ) @@ -302,7 +303,7 @@ def test_build_two_point_likelihood_empty_likelihood_config(tmp_path: Path) -> N tmp_experiment_file.write_text("") build_parameters = NamedParameters({"likelihood_config": str(tmp_experiment_file)}) - with pytest.raises(ValueError, match="No likelihood config found."): + with pytest.raises(ValueError, match="validation error for TwoPointExperiment"): _ = build_two_point_likelihood(build_parameters) diff --git a/tests/test_pt_systematics.py b/tests/test_pt_systematics.py index 11a4d1fe5..4a9d90dad 100644 --- a/tests/test_pt_systematics.py +++ b/tests/test_pt_systematics.py @@ -48,7 +48,7 @@ def fixture_sacc_data(): # This shouldn't be necessary, since we only use the n(z) from the sacc file saccfile = os.path.join( os.path.split(__file__)[0], - "../examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits", + "../examples/des_y1_3x2pt/sacc_data.fits", ) return sacc.Sacc.load_fits(saccfile) From fba1ef7cd8245c7676eeb8fefc84bb0cf4479f63 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 18:46:46 -0300 Subject: [PATCH 10/25] Updated file names. --- environment.yml | 1 + examples/des_y1_3x2pt/README.md | 40 ++++++++++++++++++- ...e_ccl_default_factory.ini => pure_ccl.ini} | 0 .../des_y1_3x2pt/des_y1_cosmic_shear_TATT.py | 4 +- .../des_y1_cosmic_shear_pk_modifier.py | 4 +- examples/des_y1_3x2pt/generate_des_data.py | 2 +- examples/des_y1_3x2pt/plot_des_chain.ipynb | 2 +- .../des_y1_3x2pt/plot_des_data_model.ipynb | 2 +- firecrown/ccl_factory.py | 9 +++-- tests/integration/test_des_y1_3x2pt.py | 13 ++++-- tutorial/two_point_factories.qmd | 2 +- 11 files changed, 60 insertions(+), 19 deletions(-) rename examples/des_y1_3x2pt/cosmosis/{pure_ccl_default_factory.ini => pure_ccl.ini} (100%) diff --git a/environment.yml b/environment.yml index 697a25c9b..b497bd969 100644 --- a/environment.yml +++ b/environment.yml @@ -18,6 +18,7 @@ dependencies: - getdist - glib - idna + - isitgr - matplotlib-base - more-itertools - mypy diff --git a/examples/des_y1_3x2pt/README.md b/examples/des_y1_3x2pt/README.md index ae6f39950..bc33bbaaf 100644 --- a/examples/des_y1_3x2pt/README.md +++ b/examples/des_y1_3x2pt/README.md @@ -1,6 +1,42 @@ # DES Y1 3x2pt Analysis -The code here will run a test sample for a Flat LCDM cosmology using either Cobaya or CosmoSIS. +The DES Y1 3x2pt analysis is a joint analysis of the DES Y1 cosmic shear, galaxy-galaxy +lensing, and galaxy clustering data. In this example, we show how to use the DES Y1 +3x2pt data products to calculate the likelihood using `Cobaya`, `CosmoSIS`, and +`NumCosmo`. + +## Likelihood factories + +Firecrown likelihood can be constructed through user defined factories. In this example: + + - `factory.py`: The FireCrown factory file for the DES Y1 3x2pt analysis. + - `factory_PT.py`: The FireCrown factory file for the DES Y1 3x2pt analysis with + perturbation theory. + +Firecrown default factories can also be used, in this case they are configure through the +experiment configuration file: + + - `experiment.yaml`: The FireCrown experiment configuration file for the DES Y1 3x2pt + analysis. + - `pure_ccl_experiment.yaml`: The FireCrown experiment configuration file for the DES Y1 + 3x2pt analysis using the `pure_ccl` mode. + - `mu_sigma_experiment.yaml`: The FireCrown experiment configuration file for the DES Y1 + 3x2pt analysis using the `mu_sigma_isitgr` mode. + + +## Running cosmosis + +For each of the likelihood factories, we provide a CosmoSIS pipeline configuration file: + + - `cosmosis/factory.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt + analysis. + - `cosmosis/factory_PT.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt + analysis with perturbation theory. + - `cosmosis/pure_ccl.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt + analysis using the `pure_ccl` mode. + - `cosmosis/mu_sigma.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt + + ## Running Cobaya @@ -29,7 +65,7 @@ It also creates the directory `des_y1_3x2pt_output` which is populated with the ## Generating the `firecrown` Inputs Note, this code below only has to be run if you want to generate the firecrown input from the DES Y1 3x2pt data products. -This file is stored at `examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits`. +This file is stored at `examples/des_y1_3x2pt/sacc_data.fits`. You do not have to make it yourself. To generate the `firecrown` inputs, first download the DES Y1 3x2pt data products [here](http://desdr-server.ncsa.illinois.edu/despublic/y1a1_files/chains/2pt_NG_mcal_1110.fits). diff --git a/examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini b/examples/des_y1_3x2pt/cosmosis/pure_ccl.ini similarity index 100% rename from examples/des_y1_3x2pt/cosmosis/pure_ccl_default_factory.ini rename to examples/des_y1_3x2pt/cosmosis/pure_ccl.ini diff --git a/examples/des_y1_3x2pt/des_y1_cosmic_shear_TATT.py b/examples/des_y1_3x2pt/des_y1_cosmic_shear_TATT.py index 647268bdc..0b410b684 100644 --- a/examples/des_y1_3x2pt/des_y1_cosmic_shear_TATT.py +++ b/examples/des_y1_3x2pt/des_y1_cosmic_shear_TATT.py @@ -16,9 +16,7 @@ from firecrown.metadata_types import TracerNames, TRACER_NAMES_TOTAL SACCFILE = os.path.expanduser( - os.path.expandvars( - "${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits" - ) + os.path.expandvars("${FIRECROWN_DIR}/examples/des_y1_3x2pt/sacc_data.fits") ) diff --git a/examples/des_y1_3x2pt/des_y1_cosmic_shear_pk_modifier.py b/examples/des_y1_3x2pt/des_y1_cosmic_shear_pk_modifier.py index 40620eb6e..80ba10353 100644 --- a/examples/des_y1_3x2pt/des_y1_cosmic_shear_pk_modifier.py +++ b/examples/des_y1_3x2pt/des_y1_cosmic_shear_pk_modifier.py @@ -20,9 +20,7 @@ from firecrown.metadata_types import TracerNames SACCFILE = os.path.expanduser( - os.path.expandvars( - "${FIRECROWN_DIR}/examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits" - ) + os.path.expandvars("${FIRECROWN_DIR}/examples/des_y1_3x2pt/sacc_data.fits") ) diff --git a/examples/des_y1_3x2pt/generate_des_data.py b/examples/des_y1_3x2pt/generate_des_data.py index 74be2086a..0f7ff53fd 100644 --- a/examples/des_y1_3x2pt/generate_des_data.py +++ b/examples/des_y1_3x2pt/generate_des_data.py @@ -225,4 +225,4 @@ sacc_data.add_covariance(new_cov) -sacc_data.save_fits("des_y1_3x2pt_sacc_data.fits", overwrite=True) +sacc_data.save_fits("sacc_data.fits", overwrite=True) diff --git a/examples/des_y1_3x2pt/plot_des_chain.ipynb b/examples/des_y1_3x2pt/plot_des_chain.ipynb index af7550f0d..55cf7687d 100644 --- a/examples/des_y1_3x2pt/plot_des_chain.ipynb +++ b/examples/des_y1_3x2pt/plot_des_chain.ipynb @@ -56,7 +56,7 @@ "metadata": {}, "outputs": [], "source": [ - "des_data = sacc.Sacc.load_fits(\"des_y1_3x2pt_sacc_data.fits\")\n", + "des_data = sacc.Sacc.load_fits(\"sacc_data.fits\")\n", "fc_data = sacc.Sacc.load_fits(\n", " \"output_%s/statistics/two_point/sacc_predicted.fits\" % ANALYSIS_ID\n", ")" diff --git a/examples/des_y1_3x2pt/plot_des_data_model.ipynb b/examples/des_y1_3x2pt/plot_des_data_model.ipynb index 45b1c4e3c..266365595 100644 --- a/examples/des_y1_3x2pt/plot_des_data_model.ipynb +++ b/examples/des_y1_3x2pt/plot_des_data_model.ipynb @@ -54,7 +54,7 @@ "metadata": {}, "outputs": [], "source": [ - "des_data = sacc.Sacc.load_fits(\"des_y1_3x2pt_sacc_data.fits\")\n", + "des_data = sacc.Sacc.load_fits(\"sacc_data.fits\")\n", "fc_meas_data = sacc.Sacc.load_fits(\n", " \"output_%s/statistics/two_point/sacc_measured.fits\" % ANALYSIS_ID\n", ")\n", diff --git a/firecrown/ccl_factory.py b/firecrown/ccl_factory.py index 9f59de1be..adc351625 100644 --- a/firecrown/ccl_factory.py +++ b/firecrown/ccl_factory.py @@ -134,20 +134,21 @@ class MuSigmaModel(Updatable): def __init__(self): """Initialize the MuSigmaModel object.""" - super().__init__(parameter_prefix="mu_sigma") + super().__init__(parameter_prefix="mg_musigma") self.mu = register_new_updatable_parameter(default_value=1.0) - self.sigma = register_new_updatable_parameter(default_value=0.0) + self.sigma = register_new_updatable_parameter(default_value=1.0) self.c1 = register_new_updatable_parameter(default_value=1.0) self.c2 = register_new_updatable_parameter(default_value=1.0) - self.lambda_mg = register_new_updatable_parameter(default_value=0.0) + # We cannot clash with the lambda keyword + self.lambda0 = register_new_updatable_parameter(default_value=1.0) def create(self) -> MuSigmaMG: """Create a `pyccl.modified_gravity.MuSigmaMG` object.""" if not self.is_updated(): raise ValueError("Parameters have not been updated yet.") - return MuSigmaMG(self.mu, self.sigma, self.c1, self.c2, self.lambda_mg) + return MuSigmaMG(self.mu, self.sigma, self.c1, self.c2, self.lambda0) class CAMBExtraParams(BaseModel): diff --git a/tests/integration/test_des_y1_3x2pt.py b/tests/integration/test_des_y1_3x2pt.py index 1d45cfd7a..328696683 100644 --- a/tests/integration/test_des_y1_3x2pt.py +++ b/tests/integration/test_des_y1_3x2pt.py @@ -16,7 +16,8 @@ def test_des_y1_3x2pt_cosmosis(): cosmosis cosmosis/factory.ini cosmosis cosmosis/factory_PT.ini cosmosis cosmosis/default_factory.ini - cosmosis cosmosis/pure_ccl_default_factory.ini + cosmosis cosmosis/pure_ccl.ini + cosmosis cosmosis/mu_sigma.ini """, ], capture_output=True, @@ -55,10 +56,15 @@ def test_des_y1_3x2pt_numcosmo(): --nonlin-matter-ps halofit numcosmo run test default_factory.yaml - numcosmo from-cosmosis ../cosmosis/pure_ccl_default_factory.ini \\ + numcosmo from-cosmosis ../cosmosis/pure_ccl.ini \\ --matter-ps eisenstein_hu \\ --nonlin-matter-ps halofit - numcosmo run test pure_ccl_default_factory.yaml + numcosmo run test pure_ccl.yaml + + numcosmo from-cosmosis ../cosmosis/mu_sigma.ini \\ + --matter-ps eisenstein_hu \\ + --nonlin-matter-ps halofit + numcosmo run test mu_sigma.yaml """, ], capture_output=True, @@ -82,6 +88,7 @@ def test_des_y1_3x2pt_cobaya(): cobaya-run cobaya/evaluate.yaml cobaya-run cobaya/evaluate_PT.yaml cobaya-run cobaya/evaluate_pure_ccl.yaml + cobaya-run cobaya/evaluate_mu_sigma.yaml """, ], capture_output=True, diff --git a/tutorial/two_point_factories.qmd b/tutorial/two_point_factories.qmd index ef3b6c90a..fb1fa7009 100644 --- a/tutorial/two_point_factories.qmd +++ b/tutorial/two_point_factories.qmd @@ -32,7 +32,7 @@ In the code below we extract the metadata indices from the `examples/des_y1_3x2p from firecrown.metadata_functions import extract_all_real_metadata_indices import sacc -sacc_data = sacc.Sacc.load_fits("../examples/des_y1_3x2pt/des_y1_3x2pt_sacc_data.fits") +sacc_data = sacc.Sacc.load_fits("../examples/des_y1_3x2pt/sacc_data.fits") all_meta = extract_all_real_metadata_indices(sacc_data) ``` From 181e2074ef95612414e5a45de6d8f6eca92c2566 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 18:47:13 -0300 Subject: [PATCH 11/25] Adding support for mu_sigma experiments. --- .../cobaya/evaluate_mu_sigma.yaml | 176 ++++++++++++++++++ examples/des_y1_3x2pt/cosmosis/mu_sigma.ini | 46 +++++ .../des_y1_3x2pt/cosmosis/mu_sigma_values.ini | 51 +++++ .../des_y1_3x2pt/mu_sigma_experiment.yaml | 32 ++++ 4 files changed, 305 insertions(+) create mode 100644 examples/des_y1_3x2pt/cobaya/evaluate_mu_sigma.yaml create mode 100644 examples/des_y1_3x2pt/cosmosis/mu_sigma.ini create mode 100644 examples/des_y1_3x2pt/cosmosis/mu_sigma_values.ini create mode 100644 examples/des_y1_3x2pt/mu_sigma_experiment.yaml diff --git a/examples/des_y1_3x2pt/cobaya/evaluate_mu_sigma.yaml b/examples/des_y1_3x2pt/cobaya/evaluate_mu_sigma.yaml new file mode 100644 index 000000000..d05309515 --- /dev/null +++ b/examples/des_y1_3x2pt/cobaya/evaluate_mu_sigma.yaml @@ -0,0 +1,176 @@ +likelihood: + des_y1_3x2pt: + external: !!python/name:firecrown.connector.cobaya.likelihood.LikelihoodConnector '' + firecrownIni: firecrown.likelihood.factories.build_two_point_likelihood + build_parameters: + likelihood_config: ${FIRECROWN_DIR}/examples/des_y1_3x2pt/mu_sigma_experiment.yaml +params: + sigma8: + prior: + min: 0.6 + max: 1.0 + ref: 0.8 + proposal: 0.01 + Omega_b: 0.05 + Omega_c: + prior: + min: 0.05 + max: 0.35 + ref: 0.25 + proposal: 0.01 + Omega_k: 0.0 + h: 0.682 + Neff: 3.046 + m_nu: 0.06 + n_s: 0.971 + w0: -1.0 + wa: 0.0 + T_CMB: 2.7255 + +# - MG Model + mg_musigma_mu: + ref: 1.0 + prior: + min: 0.8 + max: 1.2 + mg_musigma_sigma: + ref: 1.0 + prior: + min: 0.8 + max: 1.2 + mg_musigma_c1: 1.0 + mg_musigma_c2: 1.0 + mg_musigma_lambda0: 1.0 +# Likelihood params +# - IA model + ia_bias: + ref: 0.5 + prior: + min: -5.0 + max: +5.0 + alphaz: + ref: 0.0 + prior: + min: -5.0 + max: +5.0 + +# - these parameters are fixed + z_piv: 0.62 + +# - linear bias for lenses + lens0_bias: + ref: 1.4 + prior: + min: 0.8 + max: 3.0 + lens1_bias: + ref: 1.6 + prior: + min: 0.8 + max: 3.0 + lens2_bias: + ref: 1.6 + prior: + min: 0.8 + max: 3.0 + lens3_bias: + ref: 1.9 + prior: + min: 0.8 + max: 3.0 + lens4_bias: + ref: 2.0 + prior: + min: 0.8 + max: 3.0 + +# - photoz shifts for the lensing sources + src0_delta_z: + ref: -0.001 + prior: + dist: norm + loc: -0.001 + scale: 0.016 + src1_delta_z: + ref: -0.019 + prior: + dist: norm + loc: -0.019 + scale: 0.013 + src2_delta_z: + ref: +0.009 + prior: + dist: norm + loc: 0.009 + scale: 0.011 + src3_delta_z: + ref: -0.018 + prior: + dist: norm + loc: -0.018 + scale: 0.022 + +# - photoz shifts for the lenses + lens0_delta_z: + ref: 0.001 + prior: + dist: norm + loc: 0.001 + scale: 0.008 + lens1_delta_z: + ref: 0.002 + prior: + dist: norm + loc: 0.002 + scale: 0.007 + lens2_delta_z: + ref: 0.001 + prior: + dist: norm + loc: 0.001 + scale: 0.007 + lens3_delta_z: + ref: 0.003 + prior: + dist: norm + loc: 0.003 + scale: 0.01 + lens4_delta_z: + ref: 0.0 + prior: + dist: norm + loc: 0.0 + scale: 0.01 + +# - shear errors + src0_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src1_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src2_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 + src3_mult_bias: + ref: 0.0 + prior: + dist: norm + loc: 0.012 + scale: 0.023 +sampler: + evaluate: null +stop_at_error: true +output: output_mu_sigma +packages_path: null +test: false +debug: false diff --git a/examples/des_y1_3x2pt/cosmosis/mu_sigma.ini b/examples/des_y1_3x2pt/cosmosis/mu_sigma.ini new file mode 100644 index 000000000..c0edc1dcd --- /dev/null +++ b/examples/des_y1_3x2pt/cosmosis/mu_sigma.ini @@ -0,0 +1,46 @@ +[runtime] +sampler = test +root = ${PWD} + +[DEFAULT] +fatal_errors = T + +[output] +filename = output/des_y1_3x2pt_samples.txt +format = text +verbosity = 0 + +[pipeline] +modules = consistency firecrown_likelihood +values = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/cosmosis/mu_sigma_values.ini +likelihoods = firecrown +quiet = T +debug = T +timing = T + +[consistency] +file = ${CSL_DIR}/utility/consistency/consistency_interface.py + +[firecrown_likelihood] +;; Fix this to use an environment variable to find the files. +;; Set FIRECROWN_DIR to the base of the firecrown installation (or build, if you haven't +;; installed it) +file = ${FIRECROWN_DIR}/firecrown/connector/cosmosis/likelihood.py +likelihood_source = firecrown.likelihood.factories.build_two_point_likelihood +likelihood_config = ${FIRECROWN_DIR}/examples/des_y1_3x2pt/mu_sigma_experiment.yaml +;; Connector settings +require_nonlinear_pk = True +sampling_parameters_sections = firecrown_two_point + +[test] +fatal_errors = T +save_dir = output_pure_ccl + +[metropolis] +samples = 1000 +nsteps = 1 + +[emcee] +walkers = 64 +samples = 400 +nsteps = 10 diff --git a/examples/des_y1_3x2pt/cosmosis/mu_sigma_values.ini b/examples/des_y1_3x2pt/cosmosis/mu_sigma_values.ini new file mode 100644 index 000000000..1d242f644 --- /dev/null +++ b/examples/des_y1_3x2pt/cosmosis/mu_sigma_values.ini @@ -0,0 +1,51 @@ +; Parameters and data in CosmoSIS are organized into sections +; so we can easily see what they mean. +; There is only one section in this case, called cosmological_parameters +[cosmological_parameters] + +; These are the only parameters being varied. +omega_c = 0.06 0.26 0.46 +omega_b = 0.03 0.04 0.07 + +; The following parameters are set, but not varied. +omega_k = 0.0 +tau = 0.08 +n_s = 0.971 +sigma_8 = 0.801 +h0 = 0.682 +w = -1.0 +wa = 0.0 + + +[firecrown_two_point] + +mg_musigma_mu = 0.8 1.0 1.2 +mg_musigma_sigma = 0.8 1.0 1.2 +mg_musigma_c1 = 1.0 +mg_musigma_c2 = 1.0 +mg_musigma_lambda0 = 1.0 + +ia_bias = -5.0 0.5 +5.0 +alphaz = -5.0 0.0 +5.0 +z_piv = 0.62 +lens0_bias = 0.8 1.4 3.0 +lens1_bias = 0.8 1.6 3.0 +lens2_bias = 0.8 1.6 3.0 +lens3_bias = 0.8 1.9 3.0 +lens4_bias = 0.8 2.0 3.0 + +src0_delta_z = -0.05 -0.001 0.05 +src1_delta_z = -0.05 -0.019 0.05 +src2_delta_z = -0.05 0.009 0.05 +src3_delta_z = -0.05 -0.018 0.05 + +lens0_delta_z = -0.05 0.001 0.05 +lens1_delta_z = -0.05 0.002 0.05 +lens2_delta_z = -0.05 0.001 0.05 +lens3_delta_z = -0.05 0.003 0.05 +lens4_delta_z = -0.05 0.000 0.05 + +src0_mult_bias = 0.001 0.012 0.1 +src1_mult_bias = 0.001 0.012 0.1 +src2_mult_bias = 0.001 0.012 0.1 +src3_mult_bias = 0.001 0.012 0.1 diff --git a/examples/des_y1_3x2pt/mu_sigma_experiment.yaml b/examples/des_y1_3x2pt/mu_sigma_experiment.yaml new file mode 100644 index 000000000..b1bac85df --- /dev/null +++ b/examples/des_y1_3x2pt/mu_sigma_experiment.yaml @@ -0,0 +1,32 @@ +--- + +# This file contains all information about the DES Y1 3x2pt experiment. + +# The 'data_source:' field points to the SACC file containing the DES Y1 3x2pt data vector +# and covariance matrix, which is used for analysis. + +# The 'two_point_factory:' field points to the factory that will create the TwoPoint +# objects. These objects represent the chosen theoretical models for each data point +# found in the SACC file. + +data_source: + sacc_data_file: sacc_data.fits + +# The two point statistics are defined by the TwoPoint objects. The TwoPoint statistics +# are created using the factories defined in this file. +two_point_factory: + correlation_space: real + number_counts_factory: + global_systematics: [] + per_bin_systematics: + - type: PhotoZShiftFactory + weak_lensing_factory: + global_systematics: + - alphag: 1 + type: LinearAlignmentSystematicFactory + per_bin_systematics: + - type: MultiplicativeShearBiasFactory + - type: PhotoZShiftFactory + +ccl_factory: + creation_mode: 'mu_sigma_isitgr' From 1ba6fea326ed1442e294eae86eac2939cab290fc Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 20:53:18 -0300 Subject: [PATCH 12/25] Updated README.md --- examples/des_y1_3x2pt/README.md | 84 ++++++++++++++++----------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/examples/des_y1_3x2pt/README.md b/examples/des_y1_3x2pt/README.md index bc33bbaaf..9e14cf2a6 100644 --- a/examples/des_y1_3x2pt/README.md +++ b/examples/des_y1_3x2pt/README.md @@ -1,66 +1,64 @@ # DES Y1 3x2pt Analysis -The DES Y1 3x2pt analysis is a joint analysis of the DES Y1 cosmic shear, galaxy-galaxy -lensing, and galaxy clustering data. In this example, we show how to use the DES Y1 -3x2pt data products to calculate the likelihood using `Cobaya`, `CosmoSIS`, and -`NumCosmo`. +The DES Y1 3x2pt analysis of the Dark Energy Survey (DES) Year 1 (Y1) data, focusing on +**cosmic shear**, **galaxy-galaxy lensing**, and **galaxy clustering**. This repository +demonstrates how to use the DES Y1 3x2pt data products to compute the likelihood with +the following tools: -## Likelihood factories +- **Cobaya** +- **CosmoSIS** +- **NumCosmo** -Firecrown likelihood can be constructed through user defined factories. In this example: +## Likelihood Factories - - `factory.py`: The FireCrown factory file for the DES Y1 3x2pt analysis. - - `factory_PT.py`: The FireCrown factory file for the DES Y1 3x2pt analysis with - perturbation theory. +The **Firecrown** likelihoods can be customized through user-defined factories. In this +example, the following factory files are provided: -Firecrown default factories can also be used, in this case they are configure through the -experiment configuration file: +- `factory.py`: Factory for the DES Y1 3x2pt analysis. +- `factory_PT.py`: Factory for the DES Y1 3x2pt analysis using perturbation theory. - - `experiment.yaml`: The FireCrown experiment configuration file for the DES Y1 3x2pt - analysis. - - `pure_ccl_experiment.yaml`: The FireCrown experiment configuration file for the DES Y1 - 3x2pt analysis using the `pure_ccl` mode. - - `mu_sigma_experiment.yaml`: The FireCrown experiment configuration file for the DES Y1 - 3x2pt analysis using the `mu_sigma_isitgr` mode. +Alternatively, Firecrown’s **default factories** can be configured using experiment +configuration files: +- `experiment.yaml`: Standard configuration for the DES Y1 3x2pt analysis. +- `pure_ccl_experiment.yaml`: Configuration for the DES Y1 3x2pt analysis in + **pure_ccl** mode. +- `mu_sigma_experiment.yaml`: Configuration for **mu_sigma_isitgr** mode. -## Running cosmosis - -For each of the likelihood factories, we provide a CosmoSIS pipeline configuration file: +## Running CosmoSIS - - `cosmosis/factory.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt - analysis. - - `cosmosis/factory_PT.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt - analysis with perturbation theory. - - `cosmosis/pure_ccl.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt - analysis using the `pure_ccl` mode. - - `cosmosis/mu_sigma.ini`: The CosmoSIS pipeline configuration file for the DES Y1 3x2pt +For each likelihood factory, a corresponding **CosmoSIS** pipeline configuration file is +provided: +- `cosmosis/factory.ini`: Pipeline for the standard DES Y1 3x2pt analysis. +- `cosmosis/factory_PT.ini`: Pipeline for the analysis with perturbation theory. +- `cosmosis/pure_ccl.ini`: Pipeline for the analysis in **pure CCL** mode. +- `cosmosis/mu_sigma.ini`: Pipeline for the analysis in **mu_sigma_isitgr** mode. +### Example Command -## Running Cobaya +To run the CosmoSIS pipeline, use the following command: -The Cobaya file `cobaya_evaluate.yaml` configures Cobaya to use the `des_y1_3x2pt.py` likelihood. -Run it using: - - cobaya-run cobaya_evaluate.yaml +```bash +cosmosis cosmosis/factory.ini +``` -This will produce the output files: +## Running Cobaya - cobaya_evaluate_output.input.yaml - cobaya_evaluate_output.updated.yaml - cobaya_evaluate_output.updated.dill_pickle - cobaya_evaluate_output.1.txt +For Cobaya, the pipeline configuration files are: -## Running CosmoSIS +- `cobaya/evaluate.yaml`: Pipeline for the standard DES Y1 3x2pt analysis. +- `cobaya/evaluate_PT.yaml`: Pipeline for the analysis with perturbation theory. +- `cobaya/evaluate_pure_ccl.yaml`: Pipeline for the analysis in **pure CCL** mode. +- `cobaya/evaluate_mu_sigma.yaml`: Pipeline for the analysis in **mu_sigma_isitgr** mode. -The pipeline configuration file `des_y1_3x2pt.ini` and the related `des_y1_3x2pt_values.ini` configure CosmoSIS to use the `des_y1_3x2pt.py` likelihood. -Run this using: +### Example Command - cosmosis des_y1_3x2pt.ini +To run the Cobaya pipeline, use the following command: -This will produce the output to the screen, showing the calculated likelihood. -It also creates the directory `des_y1_3x2pt_output` which is populated with the CosmoSIS datablock contents for the generated sample. +```bash +cobaya-run cobaya/factory.yaml +``` ## Generating the `firecrown` Inputs From 1c0b139c52eeba2016ce8364c7c5235830e06a89 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 20:55:06 -0300 Subject: [PATCH 13/25] Updated README.md --- examples/des_y1_3x2pt/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/des_y1_3x2pt/README.md b/examples/des_y1_3x2pt/README.md index 9e14cf2a6..e7becbfac 100644 --- a/examples/des_y1_3x2pt/README.md +++ b/examples/des_y1_3x2pt/README.md @@ -62,11 +62,11 @@ cobaya-run cobaya/factory.yaml ## Generating the `firecrown` Inputs -Note, this code below only has to be run if you want to generate the firecrown input from the DES Y1 3x2pt data products. -This file is stored at `examples/des_y1_3x2pt/sacc_data.fits`. -You do not have to make it yourself. - -To generate the `firecrown` inputs, first download the DES Y1 3x2pt data products [here](http://desdr-server.ncsa.illinois.edu/despublic/y1a1_files/chains/2pt_NG_mcal_1110.fits). +There is already a `sacc_data.fits` file in this directory that contains the DES Y1 +3x2pt data products. If you want to regenerate the `firecrown` inputs, you can run +`generate_des_data.py`. To generate the `firecrown` inputs, first download the DES Y1 +3x2pt data products +[here](http://desdr-server.ncsa.illinois.edu/despublic/y1a1_files/chains/2pt_NG_mcal_1110.fits). Then run the script From 65b53c9d937b0ac756fd01fec19452c8c3b9a412 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 22:55:45 -0300 Subject: [PATCH 14/25] Improving integration tests. Making mu_sigma tests run only if isitgr is available. Updating documentation. --- .github/workflows/ci.yml | 4 +- environment.yml | 1 - examples/des_y1_3x2pt/README.md | 4 ++ tests/integration/test_des_y1_3x2pt.py | 86 +++++++++++++++----------- 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index adb1a716e..760d293f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,7 +100,9 @@ jobs: run: python -m pytest -vv --runslow --cov firecrown --cov-report xml - name: Running integration tests shell: bash -l {0} - run: python -m pytest -vv -s --integration tests/integration + run: | + pip install isitgr + python -m pytest -vv -s --integration tests/integration - name: Upload coverage reports to Codecov if: ${{ (matrix.os == 'ubuntu') && (matrix.python-version == '3.11') }} uses: codecov/codecov-action@v4 diff --git a/environment.yml b/environment.yml index b497bd969..697a25c9b 100644 --- a/environment.yml +++ b/environment.yml @@ -18,7 +18,6 @@ dependencies: - getdist - glib - idna - - isitgr - matplotlib-base - more-itertools - mypy diff --git a/examples/des_y1_3x2pt/README.md b/examples/des_y1_3x2pt/README.md index e7becbfac..aa8b3012a 100644 --- a/examples/des_y1_3x2pt/README.md +++ b/examples/des_y1_3x2pt/README.md @@ -25,6 +25,10 @@ configuration files: **pure_ccl** mode. - `mu_sigma_experiment.yaml`: Configuration for **mu_sigma_isitgr** mode. +Note that `mu_sigma_experiment.yaml` requires the +[`isitgr`](https://github.com/mishakb/ISiTGR) package to be installed. This package is +not installed by default in the Firecrown environment. + ## Running CosmoSIS For each likelihood factory, a corresponding **CosmoSIS** pipeline configuration file is diff --git a/tests/integration/test_des_y1_3x2pt.py b/tests/integration/test_des_y1_3x2pt.py index 328696683..716cdcd43 100644 --- a/tests/integration/test_des_y1_3x2pt.py +++ b/tests/integration/test_des_y1_3x2pt.py @@ -4,20 +4,55 @@ import pytest +HAS_ISITGR = False +try: + import isitgr + + if isitgr is not None: + HAS_ISITGR = True +except ImportError: + HAS_ISITGR = False + +INI_FILES = [ + "factory.ini", + "factory_PT.ini", + "default_factory.ini", + "pure_ccl.ini", +] +if HAS_ISITGR: + INI_FILES.append("mu_sigma.ini") + +COBAYA_YAML_FILES = [ + "evaluate.yaml", + "evaluate_PT.yaml", + "evaluate_pure_ccl.yaml", +] +if HAS_ISITGR: + COBAYA_YAML_FILES.append("evaluate_mu_sigma.yaml") + + +@pytest.fixture(name="ini_file", params=INI_FILES) +def fixture_ini_file(request) -> str: + """Fixture to provide the ini files for the DES Y1 3x2pt analysis.""" + return request.param + + +@pytest.fixture(name="cobaya_yaml_file", params=COBAYA_YAML_FILES) +def fixture_cobaya_yaml_file(request) -> str: + """Fixture to provide the cobaya yaml files for the DES Y1 3x2pt analysis.""" + return request.param + + @pytest.mark.integration -def test_des_y1_3x2pt_cosmosis(): +def test_des_y1_3x2pt_cosmosis(ini_file: str): result = subprocess.run( [ "bash", "-c", - """ + f""" set -e cd examples/des_y1_3x2pt - cosmosis cosmosis/factory.ini - cosmosis cosmosis/factory_PT.ini - cosmosis cosmosis/default_factory.ini - cosmosis cosmosis/pure_ccl.ini - cosmosis cosmosis/mu_sigma.ini + cosmosis cosmosis/{ini_file} """, ], capture_output=True, @@ -30,41 +65,21 @@ def test_des_y1_3x2pt_cosmosis(): @pytest.mark.integration -def test_des_y1_3x2pt_numcosmo(): +def test_des_y1_3x2pt_numcosmo(ini_file: str): result = subprocess.run( [ "bash", "-c", - """ + f""" set -e cd examples/des_y1_3x2pt mkdir -p numcosmo cd numcosmo - numcosmo from-cosmosis ../cosmosis/factory.ini \\ - --matter-ps eisenstein_hu \\ - --nonlin-matter-ps halofit - numcosmo run test factory.yaml - - numcosmo from-cosmosis ../cosmosis/factory_PT.ini \\ - --matter-ps eisenstein_hu \\ - --nonlin-matter-ps halofit - numcosmo run test factory_PT.yaml - - numcosmo from-cosmosis ../cosmosis/default_factory.ini \\ - --matter-ps eisenstein_hu \\ - --nonlin-matter-ps halofit - numcosmo run test default_factory.yaml - - numcosmo from-cosmosis ../cosmosis/pure_ccl.ini \\ - --matter-ps eisenstein_hu \\ - --nonlin-matter-ps halofit - numcosmo run test pure_ccl.yaml - - numcosmo from-cosmosis ../cosmosis/mu_sigma.ini \\ + numcosmo from-cosmosis ../cosmosis/{ini_file} \\ --matter-ps eisenstein_hu \\ --nonlin-matter-ps halofit - numcosmo run test mu_sigma.yaml + numcosmo run test {ini_file.replace('.ini', '.yaml')} """, ], capture_output=True, @@ -77,18 +92,15 @@ def test_des_y1_3x2pt_numcosmo(): @pytest.mark.integration -def test_des_y1_3x2pt_cobaya(): +def test_des_y1_3x2pt_cobaya(cobaya_yaml_file: str): result = subprocess.run( [ "bash", "-c", - """ + f""" set -e cd examples/des_y1_3x2pt - cobaya-run cobaya/evaluate.yaml - cobaya-run cobaya/evaluate_PT.yaml - cobaya-run cobaya/evaluate_pure_ccl.yaml - cobaya-run cobaya/evaluate_mu_sigma.yaml + cobaya-run cobaya/{cobaya_yaml_file} """, ], capture_output=True, From 6adedff11ae56eeab3954ac3fd971ad3a787aff8 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Mon, 28 Oct 2024 23:12:33 -0300 Subject: [PATCH 15/25] Removing mu_sigma test from NumCosmo list. --- tests/integration/test_des_y1_3x2pt.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/integration/test_des_y1_3x2pt.py b/tests/integration/test_des_y1_3x2pt.py index 716cdcd43..16e1ad864 100644 --- a/tests/integration/test_des_y1_3x2pt.py +++ b/tests/integration/test_des_y1_3x2pt.py @@ -66,6 +66,10 @@ def test_des_y1_3x2pt_cosmosis(ini_file: str): @pytest.mark.integration def test_des_y1_3x2pt_numcosmo(ini_file: str): + # FIXME: New NumCosmo 0.23.0 on will support the mu_sigma.ini. Update the following + # line to remove the skip when the new version is released. + if "mu_sigma" in ini_file: + pytest.skip("numcosmo does not support mu_sigma.ini") result = subprocess.run( [ "bash", From 659360baadaba306a16cc83bdbf759cea97c585f Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 10:43:20 -0300 Subject: [PATCH 16/25] Testing new camb_extra_parameters. --- firecrown/ccl_factory.py | 2 +- tests/test_ccl_factory.py | 114 +++++++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/firecrown/ccl_factory.py b/firecrown/ccl_factory.py index adc351625..c406b1221 100644 --- a/firecrown/ccl_factory.py +++ b/firecrown/ccl_factory.py @@ -322,6 +322,7 @@ def create( if self.camb_extra_params is not None: ccl_args["extra_parameters"] = {"camb": self.camb_extra_params.get_dict()} + ccl_args["matter_power_spectrum"] = "camb" match self.creation_mode: case CCLCreationMode.DEFAULT: @@ -337,7 +338,6 @@ def create( pass case _: raise ValueError(f"Invalid creation mode: {self.creation_mode}") - self._ccl_cosmo = pyccl.Cosmology(**ccl_args) return self._ccl_cosmo diff --git a/tests/test_ccl_factory.py b/tests/test_ccl_factory.py index 3e12d6140..dc833c8bb 100644 --- a/tests/test_ccl_factory.py +++ b/tests/test_ccl_factory.py @@ -3,12 +3,16 @@ import numpy as np import pytest import pyccl +import pyccl.modified_gravity from pyccl.neutrinos import NeutrinoMassSplits from firecrown.ccl_factory import ( + CAMBExtraParams, + CCLCalculatorArgs, + CCLCreationMode, CCLFactory, + MuSigmaModel, PoweSpecAmplitudeParameter, - CCLCalculatorArgs, ) from firecrown.updatable import get_default_params_map from firecrown.parameters import ParamsMap @@ -34,6 +38,38 @@ def fixture_require_nonlinear_pk(request): return request.param +@pytest.fixture( + name="ccl_creation_mode", + params=[ + CCLCreationMode.DEFAULT, + CCLCreationMode.PURE_CCL_MODE, + CCLCreationMode.MU_SIGMA_ISITGR, + ], + ids=["default", "pure_ccl_mode", "mu_sigma_isitgr"], +) +def fixture_ccl_creation_mode(request): + return request.param + + +@pytest.fixture( + name="camb_extra_params", + params=[ + None, + {"halofit_version": "mead"}, + {"halofit_version": "mead", "kmax": 0.1}, + {"halofit_version": "mead", "kmax": 0.1, "lmax": 100}, + {"dark_energy_model": "ppf"}, + ], + ids=["default", "mead", "mead_kmax", "mead_kmax_lmax", "ppf"], +) +def fixture_camb_extra_params(request) -> CAMBExtraParams | None: + return ( + CAMBExtraParams.model_validate(request.param) + if request.param is not None + else None + ) + + Z_ARRAY = np.linspace(0.0, 5.0, 100) A_ARRAY = 1.0 / (1.0 + np.flip(Z_ARRAY)) K_ARRAY = np.geomspace(1.0e-5, 10.0, 100) @@ -79,11 +115,15 @@ def test_ccl_factory_simple( amplitude_parameter: PoweSpecAmplitudeParameter, neutrino_mass_splits: NeutrinoMassSplits, require_nonlinear_pk: bool, + ccl_creation_mode: CCLCreationMode, + camb_extra_params: CAMBExtraParams | None, ) -> None: ccl_factory = CCLFactory( amplitude_parameter=amplitude_parameter, mass_split=neutrino_mass_splits, require_nonlinear_pk=require_nonlinear_pk, + creation_mode=ccl_creation_mode, + camb_extra_params=camb_extra_params, ) assert ccl_factory is not None @@ -194,6 +234,24 @@ def test_ccl_factory_neutrino_mass_splits( assert isinstance(cosmo, pyccl.Cosmology) +def test_ccl_factory_ccl_creation_mode( + ccl_creation_mode: CCLCreationMode, +) -> None: + ccl_factory = CCLFactory(creation_mode=ccl_creation_mode) + + assert ccl_factory is not None + assert ccl_factory.creation_mode == CCLCreationMode(ccl_creation_mode) + + default_params = get_default_params_map(ccl_factory) + + ccl_factory.update(default_params) + + cosmo = ccl_factory.create() + + assert cosmo is not None + assert isinstance(cosmo, pyccl.Cosmology) + + def test_ccl_factory_get() -> None: ccl_factory = CCLFactory() @@ -338,6 +396,14 @@ def test_ccl_factory_invalid_mass_splits() -> None: CCLFactory(mass_split="Im not a valid value") +def test_ccl_factory_invalid_creation_mode() -> None: + with pytest.raises( + ValueError, + match=".*Invalid value for CCLCreationMode: Im not a valid value.*", + ): + CCLFactory(creation_mode="Im not a valid value") + + def test_ccl_factory_from_dict() -> None: ccl_factory_dict = { "amplitude_parameter": PoweSpecAmplitudeParameter.SIGMA8, @@ -365,3 +431,49 @@ def test_ccl_factory_from_dict_wrong_type() -> None: match=".*Input should be.*", ): CCLFactory.model_validate(ccl_factory_dict) + + +def test_ccl_factory_camb_extra_params_invalid() -> None: + with pytest.raises( + ValueError, + match=".*validation error for CCLFactory*", + ): + CCLFactory(camb_extra_params="Im not a valid value") + + +def test_ccl_factory_camb_extra_params_invalid_model() -> None: + ccl_factory = CCLFactory( + camb_extra_params={"dark_energy_model": "Im not a valid value"} + ) + params = get_default_params_map(ccl_factory) + ccl_factory.update(params) + ccl_cosmo = ccl_factory.create() + with pytest.raises( + ValueError, + match="The only dark energy models CCL supports with CAMB are fluid and ppf.", + ): + ccl_cosmo.compute_linear_power() + + +def test_mu_sigma_model() -> None: + mu_sigma_model = MuSigmaModel() + + assert mu_sigma_model is not None + + default_params = get_default_params_map(mu_sigma_model) + + mu_sigma_model.update(default_params) + + musigma = mu_sigma_model.create() + + assert musigma is not None + assert isinstance(musigma, pyccl.modified_gravity.MuSigmaMG) + + +def test_mu_sigma_create_not_updated() -> None: + mu_sigma_model = MuSigmaModel() + + assert mu_sigma_model is not None + + with pytest.raises(ValueError, match="Parameters have not been updated yet."): + mu_sigma_model.create() From 51b1bf277f38cdb39b19ec4f80db1977d3fa34f4 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 11:34:25 -0300 Subject: [PATCH 17/25] Testing Cobaya connector with ccl_pure mode. --- firecrown/ccl_factory.py | 13 +++-- .../connector/cobaya/test_model_likelihood.py | 54 +++++++++++++++++++ tests/test_ccl_factory.py | 12 ++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/firecrown/ccl_factory.py b/firecrown/ccl_factory.py index c406b1221..8632d2ebb 100644 --- a/firecrown/ccl_factory.py +++ b/firecrown/ccl_factory.py @@ -199,6 +199,11 @@ def __init__(self, **data): BaseModel.__init__(self, **data) Updatable.__init__(self, parameter_prefix=parameter_prefix) + if set(data) - set(self.model_fields.keys()): + raise ValueError( + f"Invalid parameters: {set(data) - set(self.model_fields.keys())}" + ) + self._ccl_cosmo: None | pyccl.Cosmology = None ccl_cosmo = pyccl.CosmologyVanillaLCDM() @@ -230,10 +235,10 @@ def __init__(self, **data): default_value=ccl_cosmo["sigma8"] ) - self.mu_sigma_model: None | MuSigmaModel = None + self._mu_sigma_model: None | MuSigmaModel = None match self.creation_mode: case CCLCreationMode.MU_SIGMA_ISITGR: - self.mu_sigma_model = MuSigmaModel() + self._mu_sigma_model = MuSigmaModel() @model_serializer(mode="wrap") def serialize_model(self, nxt: SerializerFunctionWrapHandler, _: SerializationInfo): @@ -328,9 +333,9 @@ def create( case CCLCreationMode.DEFAULT: pass case CCLCreationMode.MU_SIGMA_ISITGR: - assert self.mu_sigma_model is not None + assert self._mu_sigma_model is not None ccl_args.update( - mg_parametrization=self.mu_sigma_model.create(), + mg_parametrization=self._mu_sigma_model.create(), matter_power_spectrum="linear", transfer_function="boltzmann_isitgr", ) diff --git a/tests/connector/cobaya/test_model_likelihood.py b/tests/connector/cobaya/test_model_likelihood.py index 609add67e..c5107e8ec 100644 --- a/tests/connector/cobaya/test_model_likelihood.py +++ b/tests/connector/cobaya/test_model_likelihood.py @@ -1,6 +1,7 @@ """Unit tests for the cobaya Mapping connector.""" import pytest +import numpy as np from cobaya.model import get_model, Model from cobaya.log import LoggedError from firecrown.connector.cobaya.ccl import CCLConnector @@ -271,3 +272,56 @@ def test_derived_parameter_likelihood(fiducial_params): logpost = model_fiducial.logposterior({}) assert logpost.logpost == -3.14 assert logpost.derived[0] == 1.0 + + +def test_default_factory(): + fiducial_params = { + "ia_bias": 1.0, + "sigma8": 0.8, + "alphaz": 1.0, + "h": 0.7, + "lens0_bias": 1.0, + "lens0_delta_z": 0.1, + "lens1_bias": 1.0, + "lens1_delta_z": 0.1, + "lens2_bias": 1.0, + "lens2_delta_z": 0.1, + "lens3_bias": 1.0, + "lens3_delta_z": 0.1, + "lens4_delta_z": 0.1, + "lens4_bias": 1.0, + "m_nu": 0.06, + "n_s": 0.96, + "Neff": 3.046, + "Omega_b": 0.048, + "Omega_c": 0.26, + "Omega_k": 0.0, + "src0_delta_z": 0.1, + "src0_mult_bias": 1.0, + "src1_delta_z": 0.1, + "src1_mult_bias": 1.0, + "src2_delta_z": 0.1, + "src2_mult_bias": 1.0, + "src3_delta_z": 0.1, + "src3_mult_bias": 1.0, + "w0": -1.0, + "wa": 0.0, + "z_piv": 0.3, + "T_CMB": 2.7255, + } + info_fiducial = { + "params": fiducial_params, + "likelihood": { + "lk_connector": { + "external": LikelihoodConnector, + "firecrownIni": "firecrown.likelihood.factories.build_two_point_likelihood", + "build_parameters": { + "likelihood_config": "examples/des_y1_3x2pt/pure_ccl_experiment.yaml" + }, + }, + }, + } + model_fiducial = get_model(info_fiducial) + assert isinstance(model_fiducial, Model) + logpost = model_fiducial.logposterior({}) + assert np.isfinite(logpost.logpost) diff --git a/tests/test_ccl_factory.py b/tests/test_ccl_factory.py index dc833c8bb..c359ec6f6 100644 --- a/tests/test_ccl_factory.py +++ b/tests/test_ccl_factory.py @@ -1,5 +1,6 @@ """Test the CCLFactory object.""" +import re import numpy as np import pytest import pyccl @@ -220,7 +221,7 @@ def test_ccl_factory_amplitude_parameter( def test_ccl_factory_neutrino_mass_splits( neutrino_mass_splits: NeutrinoMassSplits, ) -> None: - ccl_factory = CCLFactory(neutrino_mass_splits=neutrino_mass_splits) + ccl_factory = CCLFactory(mass_split=neutrino_mass_splits) assert ccl_factory is not None @@ -409,6 +410,7 @@ def test_ccl_factory_from_dict() -> None: "amplitude_parameter": PoweSpecAmplitudeParameter.SIGMA8, "mass_split": NeutrinoMassSplits.EQUAL, "require_nonlinear_pk": True, + "creation_mode": CCLCreationMode.DEFAULT, } ccl_factory = CCLFactory.model_validate(ccl_factory_dict) @@ -455,6 +457,14 @@ def test_ccl_factory_camb_extra_params_invalid_model() -> None: ccl_cosmo.compute_linear_power() +def test_ccl_factory_invalid_extra_params() -> None: + with pytest.raises( + ValueError, + match=re.escape("Invalid parameters: {'not_a_valid_param'}"), + ): + CCLFactory(not_a_valid_param="Im not a valid value") + + def test_mu_sigma_model() -> None: mu_sigma_model = MuSigmaModel() From d02513e36498cb45f9b7e016cd781cf15ae89806 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 11:41:58 -0300 Subject: [PATCH 18/25] Fixed long lines. --- tests/connector/cobaya/test_model_likelihood.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/connector/cobaya/test_model_likelihood.py b/tests/connector/cobaya/test_model_likelihood.py index c5107e8ec..3020e49de 100644 --- a/tests/connector/cobaya/test_model_likelihood.py +++ b/tests/connector/cobaya/test_model_likelihood.py @@ -309,15 +309,15 @@ def test_default_factory(): "z_piv": 0.3, "T_CMB": 2.7255, } + default_factory = "firecrown.likelihood.factories.build_two_point_likelihood" + likelihood_config = "examples/des_y1_3x2pt/pure_ccl_experiment.yaml" info_fiducial = { "params": fiducial_params, "likelihood": { "lk_connector": { "external": LikelihoodConnector, - "firecrownIni": "firecrown.likelihood.factories.build_two_point_likelihood", - "build_parameters": { - "likelihood_config": "examples/des_y1_3x2pt/pure_ccl_experiment.yaml" - }, + "firecrownIni": default_factory, + "build_parameters": {"likelihood_config": likelihood_config}, }, }, } From a44a6a08b9858ce9189de701c63c50472fc108a0 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 12:23:37 -0300 Subject: [PATCH 19/25] More testing for CCLFactory. --- firecrown/ccl_factory.py | 18 ++++++------------ tests/test_ccl_factory.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/firecrown/ccl_factory.py b/firecrown/ccl_factory.py index 8632d2ebb..88b0bf0fc 100644 --- a/firecrown/ccl_factory.py +++ b/firecrown/ccl_factory.py @@ -121,12 +121,11 @@ def _generate_next_value_(name, _start, _count, _last_values): def _validate_ccl_creation_mode(value): - if isinstance(value, str): - try: - return CCLCreationMode(value.lower()) # Convert from string to Enum - except ValueError as exc: - raise ValueError(f"Invalid value for CCLCreationMode: {value}") from exc - return value + assert isinstance(value, str) + try: + return CCLCreationMode(value.lower()) # Convert from string to Enum + except ValueError as exc: + raise ValueError(f"Invalid value for CCLCreationMode: {value}") from exc class MuSigmaModel(Updatable): @@ -329,9 +328,8 @@ def create( ccl_args["extra_parameters"] = {"camb": self.camb_extra_params.get_dict()} ccl_args["matter_power_spectrum"] = "camb" + assert self.creation_mode in CCLCreationMode match self.creation_mode: - case CCLCreationMode.DEFAULT: - pass case CCLCreationMode.MU_SIGMA_ISITGR: assert self._mu_sigma_model is not None ccl_args.update( @@ -339,10 +337,6 @@ def create( matter_power_spectrum="linear", transfer_function="boltzmann_isitgr", ) - case CCLCreationMode.PURE_CCL_MODE: - pass - case _: - raise ValueError(f"Invalid creation mode: {self.creation_mode}") self._ccl_cosmo = pyccl.Cosmology(**ccl_args) return self._ccl_cosmo diff --git a/tests/test_ccl_factory.py b/tests/test_ccl_factory.py index c359ec6f6..9a9684b11 100644 --- a/tests/test_ccl_factory.py +++ b/tests/test_ccl_factory.py @@ -465,6 +465,26 @@ def test_ccl_factory_invalid_extra_params() -> None: CCLFactory(not_a_valid_param="Im not a valid value") +def test_validate_creation_mode_incompatible(): + ccl_factory = CCLFactory(creation_mode=CCLCreationMode.PURE_CCL_MODE) + params = get_default_params_map(ccl_factory) + ccl_factory.update(params) + with pytest.raises( + ValueError, + match="Calculator Mode can only be used with the DEFAULT " + "creation mode and no CAMB extra parameters.", + ): + ccl_factory.create( + calculator_args=CCLCalculatorArgs( + background={ + "a": A_ARRAY, + "chi": CHI_ARRAY, + "h_over_h0": H_OVER_H0_ARRAY, + } + ) + ) + + def test_mu_sigma_model() -> None: mu_sigma_model = MuSigmaModel() From 304199e6cd497e398862213c4e8fabd9792d535a Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 16:55:47 -0300 Subject: [PATCH 20/25] Testing NumCosmo connector. --- environment.yml | 1 + firecrown/connector/numcosmo/numcosmo.py | 2 +- .../numcosmo/test_numcosmo_connector.py | 112 +++++++++++++++++- 3 files changed, 112 insertions(+), 3 deletions(-) diff --git a/environment.yml b/environment.yml index 697a25c9b..a0236c9f6 100644 --- a/environment.yml +++ b/environment.yml @@ -27,6 +27,7 @@ dependencies: - pip: - autoclasstoc - cobaya + - isitgr - pygobject-stubs - portalocker - pybobyqa diff --git a/firecrown/connector/numcosmo/numcosmo.py b/firecrown/connector/numcosmo/numcosmo.py index ebdd087e4..71cb88821 100644 --- a/firecrown/connector/numcosmo/numcosmo.py +++ b/firecrown/connector/numcosmo/numcosmo.py @@ -923,7 +923,7 @@ def __init__( build_parameters, ) - def get_data(self) -> Ncm.Data: + def get_data(self) -> NumCosmoGaussCov | NumCosmoData: """This method return the appropriate Ncm.Data class to be used by NumCosmo. :return: the data used by NumCosmo diff --git a/tests/connector/numcosmo/test_numcosmo_connector.py b/tests/connector/numcosmo/test_numcosmo_connector.py index eae4a4e0d..3fea53dc6 100644 --- a/tests/connector/numcosmo/test_numcosmo_connector.py +++ b/tests/connector/numcosmo/test_numcosmo_connector.py @@ -1,7 +1,7 @@ """Unit tests for the numcosmo connector.""" import pytest -from numcosmo_py import Ncm, Nc +from numcosmo_py import Ncm, Nc, GObject from firecrown.connector.numcosmo.numcosmo import ( NumCosmoFactory, @@ -10,7 +10,9 @@ NumCosmoGaussCov, ) -from firecrown.likelihood.likelihood import NamedParameters, Likelihood +from firecrown.likelihood.likelihood import NamedParameters, Likelihood, load_likelihood +from firecrown.likelihood.gaussian import ConstGaussian +from firecrown.updatable import get_default_params Ncm.cfg_init() @@ -260,3 +262,109 @@ def test_empty_gauss_cov_data(): nc_data = NumCosmoGaussCov() assert nc_data.likelihood_source is None assert nc_data.likelihood_build_parameters is None + + +def test_default_factory_const_gauss(): + """Test the NumCosmo connector.""" + map_cosmo = MappingNumCosmo( + dist=Nc.Distance.new(6.0), + p_ml=Nc.PowspecMLTransfer.new(Nc.TransferFuncEH.new()), + ) + build_parameters = NamedParameters( + {"likelihood_config": "examples/des_y1_3x2pt/pure_ccl_experiment.yaml"} + ) + model_name = "firecrown_model_gauss" + + likelihood_source = "firecrown.likelihood.factories.build_two_point_likelihood" + likelihood, tools = load_likelihood(likelihood_source, build_parameters) + assert isinstance(likelihood, ConstGaussian) + data = NumCosmoGaussCov.new_from_likelihood( + likelihood, + [model_name], + tools, + map_cosmo, + likelihood_source, + build_parameters, + ) + + run_likelihood(model_name, data) + + +def test_default_factory_plain(): + """Test the NumCosmo connector.""" + map_cosmo = MappingNumCosmo( + dist=Nc.Distance.new(6.0), + p_ml=Nc.PowspecMLTransfer.new(Nc.TransferFuncEH.new()), + ) + build_parameters = NamedParameters( + {"likelihood_config": "examples/des_y1_3x2pt/pure_ccl_experiment.yaml"} + ) + model_name = "firecrown_model_plain" + + likelihood_source = "firecrown.likelihood.factories.build_two_point_likelihood" + likelihood, tools = load_likelihood(likelihood_source, build_parameters) + assert isinstance(likelihood, ConstGaussian) + data = NumCosmoData.new_from_likelihood( + likelihood, + [model_name], + tools, + map_cosmo, + likelihood_source, + build_parameters, + ) + + run_likelihood(model_name, data) + + +def run_likelihood(model_name, data): + """Run the likelihood.""" + default_parameters = get_default_params(data.likelihood, data.tools) + model_builder = Ncm.ModelBuilder.new( + Ncm.Model, + model_name, + f"Test model {model_name}", + ) + mapping_dict = [ + "h", + "m_nu", + "n_s", + "Neff", + "Omega_b", + "Omega_c", + "Omega_k", + "sigma8", + "T_CMB", + "w0", + "wa", + ] + for param, value in default_parameters.items(): + if param not in mapping_dict: + model_builder.add_sparam( + param, + param, + -1.0e10, + 1.0e10, + 1.0e-2, + 0.0, + value, + Ncm.ParamType.FIXED, + ) + + FirecrownModel = model_builder.create() # pylint: disable=invalid-name + GObject.new(FirecrownModel) + NcmFirecrownModel = FirecrownModel.pytype # pylint: disable=invalid-name + GObject.type_register(NcmFirecrownModel) + + model: Ncm.Model = NcmFirecrownModel() + model.params_set_default_ftype() + + assert data is not None + assert isinstance(data, Ncm.Data) + cosmo = Nc.HICosmoDEXcdm() + cosmo.add_submodel(Nc.HIPrimPowerLaw.new()) + cosmo.add_submodel(Nc.HIReionCamb.new()) + mset = Ncm.MSet() + mset.set(cosmo) + mset.set(model) + mset.prepare_fparam_map() + data.prepare(mset) From 2399c58d6a9f919dd733e5f412cd1138b4a0ce81 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 17:06:42 -0300 Subject: [PATCH 21/25] Removing the conditional testing. --- tests/integration/test_des_y1_3x2pt.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/integration/test_des_y1_3x2pt.py b/tests/integration/test_des_y1_3x2pt.py index 16e1ad864..d181237cc 100644 --- a/tests/integration/test_des_y1_3x2pt.py +++ b/tests/integration/test_des_y1_3x2pt.py @@ -3,32 +3,20 @@ import subprocess import pytest - -HAS_ISITGR = False -try: - import isitgr - - if isitgr is not None: - HAS_ISITGR = True -except ImportError: - HAS_ISITGR = False - INI_FILES = [ "factory.ini", "factory_PT.ini", "default_factory.ini", "pure_ccl.ini", + "mu_sigma.ini", ] -if HAS_ISITGR: - INI_FILES.append("mu_sigma.ini") COBAYA_YAML_FILES = [ "evaluate.yaml", "evaluate_PT.yaml", "evaluate_pure_ccl.yaml", + "evaluate_mu_sigma.yaml", ] -if HAS_ISITGR: - COBAYA_YAML_FILES.append("evaluate_mu_sigma.yaml") @pytest.fixture(name="ini_file", params=INI_FILES) From ce94a1b6171665fe90c24b506c7313388cff50e9 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 17:13:43 -0300 Subject: [PATCH 22/25] Testing absolute path in DataSource. --- tests/likelihood/test_factories.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/likelihood/test_factories.py b/tests/likelihood/test_factories.py index df71df960..9f9fee60c 100644 --- a/tests/likelihood/test_factories.py +++ b/tests/likelihood/test_factories.py @@ -90,11 +90,37 @@ def test_data_source_sacc_dict() -> None: data_source_sacc = DataSourceSacc.model_validate(data_source_sacc_dict) assert isinstance(data_source_sacc, DataSourceSacc) assert data_source_sacc.sacc_data_file == "tests/bug_398.sacc.gz" + sacc_data = data_source_sacc.get_sacc_data() + assert sacc_data is not None + assert isinstance(sacc_data, sacc.Sacc) + + +def test_data_source_sacc_dict_absolute() -> None: + sacc_file = Path("tests/bug_398.sacc.gz").absolute() + data_source_sacc_dict = {"sacc_data_file": sacc_file.as_posix()} + data_source_sacc = DataSourceSacc.model_validate(data_source_sacc_dict) + assert isinstance(data_source_sacc, DataSourceSacc) + assert data_source_sacc.sacc_data_file == sacc_file.as_posix() + sacc_data = data_source_sacc.get_sacc_data() + assert sacc_data is not None + assert isinstance(sacc_data, sacc.Sacc) def test_data_source_sacc_direct() -> None: data_source_sacc = DataSourceSacc(sacc_data_file="tests/bug_398.sacc.gz") assert data_source_sacc.sacc_data_file == "tests/bug_398.sacc.gz" + sacc_data = data_source_sacc.get_sacc_data() + assert sacc_data is not None + assert isinstance(sacc_data, sacc.Sacc) + + +def test_data_source_sacc_direct_absolute() -> None: + sacc_file = Path("tests/bug_398.sacc.gz").absolute() + data_source_sacc = DataSourceSacc(sacc_data_file=sacc_file.as_posix()) + assert data_source_sacc.sacc_data_file == sacc_file.as_posix() + sacc_data = data_source_sacc.get_sacc_data() + assert sacc_data is not None + assert isinstance(sacc_data, sacc.Sacc) def test_data_source_sacc_invalid_file() -> None: From 72002e6a6e86b1715d020a1d6c560674f2167a13 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 17:35:54 -0300 Subject: [PATCH 23/25] Testing cosmosis connector with pure ccl. --- .../cosmosis/test_cosmosis_module.py | 19 ++++-- .../lkscript_two_point_pure_ccl.py | 59 +++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 tests/likelihood/gauss_family/lkscript_two_point_pure_ccl.py diff --git a/tests/connector/cosmosis/test_cosmosis_module.py b/tests/connector/cosmosis/test_cosmosis_module.py index dc4394b05..6a286db86 100644 --- a/tests/connector/cosmosis/test_cosmosis_module.py +++ b/tests/connector/cosmosis/test_cosmosis_module.py @@ -105,15 +105,24 @@ def fixture_config_with_two_point_real() -> DataBlock: return result -@pytest.fixture(name="config_with_two_point_harmonic") -def fixture_config_with_two_point_harmonic() -> DataBlock: +@pytest.fixture(name="config_with_two_point_harmonic", params=["default", "pure_ccl"]) +def fixture_config_with_two_point_harmonic(request) -> DataBlock: result = DataBlock() + likelihood_file = "" + if request.param == "default": + likelihood_file = ( + "${FIRECROWN_DIR}/tests/likelihood/gauss_family/lkscript_two_point.py" + ) + elif request.param == "pure_ccl": + likelihood_file = ( + "${FIRECROWN_DIR}/tests/likelihood/" + "gauss_family/lkscript_two_point_pure_ccl.py" + ) + result.put_string( option_section, "Likelihood_source", - expandvars( - "${FIRECROWN_DIR}/tests/likelihood/gauss_family/lkscript_two_point.py" - ), + expandvars(likelihood_file), ) result.put_string( option_section, diff --git a/tests/likelihood/gauss_family/lkscript_two_point_pure_ccl.py b/tests/likelihood/gauss_family/lkscript_two_point_pure_ccl.py new file mode 100644 index 000000000..64e64a7ac --- /dev/null +++ b/tests/likelihood/gauss_family/lkscript_two_point_pure_ccl.py @@ -0,0 +1,59 @@ +""" +Provides a trivial likelihood factory function for a ConstGaussian +for testing purposes. +""" + +import numpy as np +import sacc + +from firecrown.likelihood.likelihood import NamedParameters +from firecrown.likelihood.gaussian import ConstGaussian +from firecrown.likelihood.two_point import TwoPoint +from firecrown.likelihood.number_counts import ( + NumberCounts, +) +from firecrown.modeling_tools import ModelingTools +from firecrown.ccl_factory import ( + CCLFactory, + PoweSpecAmplitudeParameter, + CCLCreationMode, +) + + +def build_likelihood(params: NamedParameters): + """Return a ConstGaussian (likelihood) object.""" + sacc_data = sacc.Sacc() + + src0 = NumberCounts(sacc_tracer="lens0") + z = np.linspace(0, 0.8, 50) + 0.05 + dndz = np.exp(-0.5 * (z - 0.4) ** 2 / 0.02 / 0.02) + sacc_data.add_tracer("NZ", "lens0", z, dndz) + + if params.get_string("projection") == "harmonic": + two_point = TwoPoint("galaxy_density_cl", source0=src0, source1=src0) + ells = np.unique(np.logspace(1, 3, 10).astype(np.int64)) + Cells = np.random.normal(size=ells.shape[0]) + sacc_data.add_ell_cl("galaxy_density_cl", "lens0", "lens0", ells, Cells) + cov = np.diag(np.ones_like(Cells) * 0.01) + sacc_data.add_covariance(cov) + elif params.get_string("projection") == "real": + two_point = TwoPoint("galaxy_density_xi", source0=src0, source1=src0) + thetas = np.linspace(0.0, np.pi, 10) + xis = np.random.normal(size=thetas.shape[0]) + sacc_data.add_theta_xi("galaxy_density_xi", "lens0", "lens0", thetas, xis) + cov = np.diag(np.ones_like(xis) * 0.01) + sacc_data.add_covariance(cov) + else: + raise ValueError("Invalid projection") + + likelihood = ConstGaussian(statistics=[two_point]) + likelihood.read(sacc_data) + + tools = ModelingTools( + ccl_factory=CCLFactory( + amplitude_parameter=PoweSpecAmplitudeParameter.SIGMA8, + creation_mode=CCLCreationMode.PURE_CCL_MODE, + ) + ) + + return likelihood, tools From ae8e911ebd63cd6f8e1d98fe530a15eb4f8c3c05 Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 18:15:04 -0300 Subject: [PATCH 24/25] Adding missing mass_split test check. --- tests/test_ccl_factory.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_ccl_factory.py b/tests/test_ccl_factory.py index 9a9684b11..fb736bb6e 100644 --- a/tests/test_ccl_factory.py +++ b/tests/test_ccl_factory.py @@ -224,6 +224,7 @@ def test_ccl_factory_neutrino_mass_splits( ccl_factory = CCLFactory(mass_split=neutrino_mass_splits) assert ccl_factory is not None + assert ccl_factory.mass_split == neutrino_mass_splits default_params = get_default_params_map(ccl_factory) From 33213a4f1a79009c55aaf57a23b1dea4148e79de Mon Sep 17 00:00:00 2001 From: Sandro Dias Pinto Vitenti Date: Tue, 29 Oct 2024 18:35:48 -0300 Subject: [PATCH 25/25] Removing unnecessary pip install of isitgr. --- .github/workflows/ci.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 760d293f7..adb1a716e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,9 +100,7 @@ jobs: run: python -m pytest -vv --runslow --cov firecrown --cov-report xml - name: Running integration tests shell: bash -l {0} - run: | - pip install isitgr - python -m pytest -vv -s --integration tests/integration + run: python -m pytest -vv -s --integration tests/integration - name: Upload coverage reports to Codecov if: ${{ (matrix.os == 'ubuntu') && (matrix.python-version == '3.11') }} uses: codecov/codecov-action@v4