diff --git a/cosmopower/wrappers/cobaya/cosmopower.py b/cosmopower/wrappers/cobaya/cosmopower.py index 9792b26..645af7a 100644 --- a/cosmopower/wrappers/cobaya/cosmopower.py +++ b/cosmopower/wrappers/cobaya/cosmopower.py @@ -33,6 +33,11 @@ def initialize(self) -> None: self._networks = self.parser.restore_networks() self._networks_to_eval = [] + self._params_required_by_networks = {} + + for nw in self._networks: + self._params_required_by_networks.update({str(p) : None for p in self._networks[nw].parameters}) + def must_provide(self, **requirements: dict) -> dict: super().must_provide(**requirements) @@ -277,6 +282,11 @@ def get_Cl(self, ell_factor: bool = False, return cls + + def get_requirements(self): + + return self._params_required_by_networks + def get_in_dict(self, dct: dict, path: str) -> Any: """ Networks are labeled with paths like "Cl/tt". This function parses diff --git a/demos/camb_highacc.yaml b/demos/camb_highacc.yaml deleted file mode 100644 index 0999e03..0000000 --- a/demos/camb_highacc.yaml +++ /dev/null @@ -1,138 +0,0 @@ -network_name: camb_highacc -path: cosmopower/trained_models/camb_highacc/ - -emulated_code: - name: camb - version: "1.5.0" - extra_args: - lens_potential_accuracy: 8 - kmax: 10.0 - k_per_logint: 130 - lens_margin: 2050 - AccuracyBoost: 1.0 - lAccuracyBoost: 1.2 - lSampleBoost: 1.0 - DoLateRadTruncation: false - min_l_logl_sampling: 6000 - -samples: - Ntraining: 150000 - - parameters: - ombh2: - range: [0.01,0.05] - omch2: - range: [0.05,0.50] - logA: - range: [2.0,4.0] - drop: True - As: - derived: True - value: "lambda logA: 1.e-10 * np.exp(logA)" - tau: - range: [0.02, 0.12] - ns: - range: [0.8, 1.2] - h: - range: [0.4,1.0] - drop: True - H0: - derived: True - value: "lambda h: h * 100.0" - -networks: - - quantity: "Cl/tt" - ell_factor: True - inputs: [ ombh2, omch2, logA, tau, ns, h ] - type: NN - log: True - modes: - label: l - range: [2,10000] - n_traits: - n_hidden: [ 512, 512, 512, 512 ] - training: - validation_split: 0.1 - learning_rates: [ 1.e-2, 1.e-3, 1.e-4, 1.e-5, 1.e-6, 1.e-7 ] - batch_sizes: [ 1000, 2000, 5000, 10000, 20000, 50000 ] - gradient_accumulation_steps: [ 1, 1, 1, 1, 1, 1 ] - patience_values: [ 100, 100, 100, 100, 100, 100 ] - max_epochs: [ 1000, 1000, 1000, 1000, 1000, 1000 ] - - - quantity: "Cl/te" - ell_factor: True - inputs: [ ombh2, omch2, logA, tau, ns, h ] - type: PCAplusNN - log: False - modes: - label: l - range: [2,10000] - p_traits: - n_pcas: 512 - n_batches: 10 - n_traits: - n_hidden: [ 512, 512, 512, 512 ] - training: - validation_split: 0.1 - learning_rates: [ 1.e-2, 1.e-3, 1.e-4, 1.e-5, 1.e-6, 1.e-7 ] - batch_sizes: [ 1000, 2000, 5000, 10000, 20000, 50000 ] - gradient_accumulation_steps: [ 1, 1, 1, 1, 1, 1 ] - patience_values: [ 100, 100, 100, 100, 100, 100 ] - max_epochs: [ 1000, 1000, 1000, 1000, 1000, 1000 ] - - - quantity: "Cl/ee" - ell_factor: True - inputs: [ ombh2, omch2, logA, tau, ns, h ] - type: NN - log: True - modes: - label: l - range: [2,10000] - n_traits: - n_hidden: [ 512, 512, 512, 512 ] - training: - validation_split: 0.1 - learning_rates: [ 1.e-2, 1.e-3, 1.e-4, 1.e-5, 1.e-6, 1.e-7 ] - batch_sizes: [ 1000, 2000, 5000, 10000, 20000, 50000 ] - gradient_accumulation_steps: [ 1, 1, 1, 1, 1, 1 ] - patience_values: [ 100, 100, 100, 100, 100, 100 ] - max_epochs: [ 1000, 1000, 1000, 1000, 1000, 1000 ] - - - quantity: "Cl/bb" - ell_factor: True - inputs: [ ombh2, omch2, logA, tau, ns, h ] - type: NN - log: True - modes: - label: l - range: [2,10000] - n_traits: - n_hidden: [ 512, 512, 512, 512 ] - training: - validation_split: 0.1 - learning_rates: [ 1.e-2, 1.e-3, 1.e-4, 1.e-5, 1.e-6, 1.e-7 ] - batch_sizes: [ 1000, 2000, 5000, 10000, 20000, 50000 ] - gradient_accumulation_steps: [ 1, 1, 1, 1, 1, 1 ] - patience_values: [ 100, 100, 100, 100, 100, 100 ] - max_epochs: [ 1000, 1000, 1000, 1000, 1000, 1000 ] - - - quantity: "Cl/pp" - ell_factor: True - inputs: [ ombh2, omch2, logA, ns, h ] - type: PCAplusNN - log: True - modes: - label: l - range: [2,10000] - p_traits: - n_pcas: 64 - n_batches: 10 - n_traits: - n_hidden: [ 512, 512, 512, 512 ] - training: - validation_split: 0.1 - learning_rates: [ 1.e-2, 1.e-3, 1.e-4, 1.e-5, 1.e-6, 1.e-7 ] - batch_sizes: [ 1000, 2000, 5000, 10000, 20000, 50000 ] - gradient_accumulation_steps: [ 1, 1, 1, 1, 1, 1 ] - patience_values: [ 100, 100, 100, 100, 100, 100 ] - max_epochs: [ 1000, 1000, 1000, 1000, 1000, 1000 ] diff --git a/demos/camb_highacc_pknl.yaml b/demos/camb_highacc_pknl.yaml deleted file mode 100644 index 18f4c50..0000000 --- a/demos/camb_highacc_pknl.yaml +++ /dev/null @@ -1,78 +0,0 @@ -network_name: camb_highacc -path: cosmopower/trained_models/camb_highacc_pknl/ - -emulated_code: - name: camb - version: "1.5.0" - inputs: [ z, ombh2, omch2, As, tau, ns, H0, NonLinearModel.HMCode_A_baryon, NonLinearModel.HMCode_eta_baryon, NonLinearModel.HMCode_logT_AGN ] - extra_args: - lmax: 6000 - lens_potential_accuracy: 8 - kmax: 50.0 - k_per_logint: 130 - lens_margin: 2050 - AccuracyBoost: 1.0 - lAccuracyBoost: 1.2 - lSampleBoost: 1.0 - DoLateRadTruncation: false - min_l_logl_sampling: 6000 - NonLinearModel.Min_kh_nonlinear: 5.e-5 - NonLinearModel.halofit_version: 'mead2020' - -samples: - Ntraining: 150000 - - parameters: - z: [0.0, 5.0] - ombh2: [0.01,0.05] - omch2: [0.05,0.50] - logA: [2.0,4.0] - As: "lambda logA: 1.e-10 * np.exp(logA)" - tau: [0.02, 0.12] - ns: [0.8, 1.2] - h: [0.4,1.0] - H0: "lambda h: h * 100.0" - NonLinearModel.HMCode_A_baryon: [2.0, 4.0] - NonLinearModel.HMCode_eta_baryon: [0.5, 1.0] - NonLinearModel.HMCode_logT_AGN: [7.3, 8.3] - -networks: - - quantity: "Pk/nonlin" - ell_factor: False - inputs: [ ombh2, omch2, logA, tau, ns, h, z, NonLinearModel.HMCode_A_baryon, NonLinearModel.HMCode_eta_baryon, NonLinearModel.HMCode_logT_AGN] - type: NN - log: True - modes: - label: k - range: [5.e-5,50] - spacing: log - steps: 128 - n_traits: - n_hidden: [ 512, 512, 512, 512 ] - training: - validation_split: 0.1 - learning_rates: [ 1.e-2, 1.e-3, 1.e-4, 1.e-5, 1.e-6, 1.e-7 ] - batch_sizes: [ 1000, 2000, 5000, 10000, 20000, 50000 ] - gradient_accumulation_steps: [ 1, 1, 1, 1, 1, 1 ] - patience_values: [ 100, 100, 100, 100, 100, 100 ] - max_epochs: [ 1000, 1000, 1000, 1000, 1000, 1000 ] - - - quantity: "Pk/nlboost" - ell_factor: False - inputs: [ ombh2, omch2, logA, tau, ns, h, z, NonLinearModel.HMCode_A_baryon, NonLinearModel.HMCode_eta_baryon, NonLinearModel.HMCode_logT_AGN] - type: NN - log: False - modes: - label: k - range: [5.e-5,50] - spacing: log - steps: 128 - n_traits: - n_hidden: [ 512, 512, 512, 512 ] - training: - validation_split: 0.1 - learning_rates: [ 1.e-2, 1.e-3, 1.e-4, 1.e-5, 1.e-6, 1.e-7 ] - batch_sizes: [ 1000, 2000, 5000, 10000, 20000, 50000 ] - gradient_accumulation_steps: [ 1, 1, 1, 1, 1, 1 ] - patience_values: [ 100, 100, 100, 100, 100, 100 ] - max_epochs: [ 1000, 1000, 1000, 1000, 1000, 1000 ] diff --git a/demos/demo_cmb_bao.yaml b/demos/demo_cmb_bao.yaml new file mode 100644 index 0000000..986c218 --- /dev/null +++ b/demos/demo_cmb_bao.yaml @@ -0,0 +1,80 @@ +# Set the location for the sampler outputs +# (samples, restart, progress files etc). +output: output/demo_cmb_bao + + +# Set the parameter ranges and priors to sample over +params: + ombh2: + prior: + min: 0.01 + max: 0.05 + ref: 0.0225 + omch2: + prior: + min: 0.01 + max: 0.90 + ref: 0.120 + h: + prior: + min: 0.4 + max: 1.0 + ref: 0.7 + H0: + value: "lambda h: 100.0 * h" + logA: + prior: + min: 2.0 + max: 4.0 + ref: 3.05 + As: + value: "lambda logA: 1e-10 * np.exp(logA)" + ns: + prior: + min: 0.8 + max: 1.2 + ref: 0.97 + tau: + prior: + min: 0.01 + max: 0.8 + ref: 0.054 + + +# Add an extra prior e.g. this one on tau. +priors: + tau_prior: "stats.norm.logpdf(tau, loc = 6.7e-2, scale = 2.3e-2)" + + +# For testing use evaluate (likelihood calculated at one reference point). +# Otherwise mcmc. +sampler: + # mcmc: + evaluate: + +# Specify the Theory components to use. Here we use cosmpower for the Cl observables +# but run normal CAMB in the background-only mode (which is fast) for background observables. +# Note we use the 'provides' key within each theory, to specify which observables come from where +# (in some cases observables can be available from more than one Theory). +theory: + cosmopower: + package_file: ../cosmopower-organization/jense_2024_emulators/jense_2023_cmb_camb_lcdm.yaml + root_dir: ../cosmopower-organization/jense_2024_emulators/ + provides: # Force cobaya to use the cosmopower-calculated versions of these + Cl: + tt: + te: + ee: + pp: + camb: + provides: # Force cobaya to use the camb-calculated versions of these + angular_diameter_distance: + Hubble: + fsigma8: + +# Include a BAO and CMB TTTEEE likelihood +likelihood: + bao.sdss_dr12_consensus_final: + stop_at_error: True + planck_2018_highl_plik.TTTEEE_lite_native: + stop_at_error: True \ No newline at end of file diff --git a/demos/demo_cp_ext_bgd.yaml b/demos/demo_cp_ext_bgd.yaml deleted file mode 100644 index b7a86ef..0000000 --- a/demos/demo_cp_ext_bgd.yaml +++ /dev/null @@ -1,66 +0,0 @@ -output: output/democp - -params: - ombh2: - prior: - min: 0.01 - max: 0.05 - ref: 0.0225 - omch2: - prior: - min: 0.01 - max: 0.90 - ref: 0.120 - h: - prior: - min: 0.4 - max: 1.0 - ref: 0.7 - H0: - value: "lambda h: 100.0 * h" - logA: - prior: - min: 2.0 - max: 4.0 - ref: 3.05 - As: - value: "lambda logA: 1e-10 * np.exp(logA)" - ns: - prior: - min: 0.8 - max: 1.2 - ref: 0.97 - tau: - prior: - min: 0.01 - max: 0.8 - ref: 0.054 - -priors: - tau_prior: "stats.norm.logpdf(tau, loc = 6.7e-2, scale = 2.3e-2)" - -sampler: - mcmc: - -theory: - wrappers.cobaya.CosmoPower: - package_file: demos/camb_highacc.yaml - provides: - Cl: - tt: - te: - ee: - pp: - camb: - extra_args: - lens_potential_accuracy: 8 - provides: - angular_diameter_distance: - Hubble: - fsigma8: - -likelihood: - bao.sdss_dr12_consensus_final: - stop_at_error: True - planck_2018_highl_plik.TTTEEE_lite_native: - stop_at_error: True diff --git a/requirements.txt b/requirements.txt index 49d30cf..25b09ba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ pyDOE h5py tensorflow>=2.18 tensorflow_probability>=0.24 +tf-keras \ No newline at end of file