CobayaSampler / cobaya

Code for Bayesian Analysis
http://cobaya.readthedocs.io/en/latest/
Other
131 stars 129 forks source link

Sampling reionization redshift (zrei) instead of optical depth (tau) #199

Closed pcampeti closed 3 years ago

pcampeti commented 3 years ago

Hello everyone, I've been trying without success to sample the reionization redshift parameter (zrei) instead of the optical depth (tau) in CAMB, using in the .yaml file the following:

zre:
  prior:
    min: 0.
    max: 20.
  proposal: 7.67
  latex: z_\mathrm{re}

tau:
  derived: True
  latex: \tau_\mathrm{reio}

theory:
  camb:
    path: /cosmo_codes/code/CAMB/
    stop_at_error: False
    extra_args:
      WantScalars: True
      WantTensors: True
      parameterization: 1
    renames:
      zrei: zre

and obtaining the following error message:

[model] *ERROR* Could not find whom to assign output parameters ['tau'].
[exception handler] ---------------------------------------

Traceback (most recent call last):
  File "cobaya-run", line 33, in <module>
    sys.exit(load_entry_point('cobaya', 'console_scripts', 'cobaya-run')())
  File "/cobaya/cobaya/run.py", line 216, in run_script
    run(info, **arguments.__dict__)
  File "/cobaya/cobaya/run.py", line 134, in run
    with Model(updated_info["params"], updated_info["likelihood"],
  File "/cobaya/cobaya/model.py", line 158, in __init__
    self._assign_params(info_likelihood, info_theory, dropped_theory_params)
  File "/cobaya/cobaya/model.py", line 918, in _assign_params
    raise LoggedError(
cobaya.log.LoggedError: Could not find whom to assign output parameters ['tau'].

Thanks a lot in advance for your help! Best

cmbant commented 3 years ago

I think there's currently no python interface for getting tau as a derived parameter. It will probably work if you do not include tau.

pcampeti commented 3 years ago

Thanks for your help Antony! I tried deleting the part relative to tau in the .yaml file above, but I'm still getting this error:

[model] *ERROR* Could not find anything to use input parameter(s) {'zre'}.
[exception handler] ---------------------------------------

Traceback (most recent call last):
  File "/cobaya-run", line 33, in <module>
    sys.exit(load_entry_point('cobaya', 'console_scripts', 'cobaya-run')())
  File "/cobaya/cobaya/run.py", line 216, in run_script
    run(info, **arguments.__dict__)
  File "/cobaya/cobaya/run.py", line 134, in run
    with Model(updated_info["params"], updated_info["likelihood"],
  File "/cobaya/cobaya/model.py", line 159, in __init__
    self._set_dependencies_and_providers(skip_unused_theories=skip_unused_theories)
  File "/cobaya/cobaya/model.py", line 686, in _set_dependencies_and_providers
    raise LoggedError(
cobaya.log.LoggedError: Could not find anything to use input parameter(s) {'zre'}.

If I try not to rename the CAMB parameter 'zrei' as 'zre' as I did before, that is I use:

zrei:
    prior:
      min: 0.
      max: 20.
    proposal: 7.67
    latex: z_\mathrm{re}

I get the following error instead:

[camb] *ERROR* Some of the parameters passed to CAMB were not recognized: Unrecognized parameter: zre
[exception handler] ---------------------------------------

Traceback (most recent call last):
  File "/cobaya/cobaya/theories/camb/camb.py", line 715, in set
    params = self.camb.set_params(**base_args)
  File "/cosmo_codes/code/CAMB/camb/camb.py", line 180, in set_params
    raise CAMBUnknownArgumentError("Unrecognized parameter: %s" % k)
camb.baseconfig.CAMBUnknownArgumentError: Unrecognized parameter: zre

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/cobaya-run", line 33, in <module>
    sys.exit(load_entry_point('cobaya', 'console_scripts', 'cobaya-run')())
  File "/cobaya/cobaya/run.py", line 216, in run_script
    run(info, **arguments.__dict__)
  File "/cobaya/cobaya/run.py", line 143, in run
    sampler = sampler_class(updated_info["sampler"][sampler_name],
  File "/cobaya/cobaya/sampler.py", line 268, in __init__
    self.initialize()
  File "/cobaya/cobaya/samplers/minimize/minimize.py", line 193, in initialize
    if np.isfinite(self.logp(initial_point)):
  File "/cobaya/cobaya/samplers/minimize/minimize.py", line 153, in <lambda>
    self.logp = lambda x: method(x, **kwargs)
  File "/cobaya/cobaya/model.py", line 341, in loglike
    ret_value = self.loglikes(params_values, return_derived=return_derived,
  File "/cobaya/cobaya/model.py", line 305, in loglikes
    return self._loglikes_input_params(input_params, return_derived=return_derived,
  File "/cobaya/cobaya/model.py", line 310, in _loglikes_input_params
    result = self.logps(input_params, return_derived=return_derived, cached=cached,
  File "/cobaya/cobaya/model.py", line 245, in logps
    compute_success = component.check_cache_and_compute(
  File "/cobaya/cobaya/theory.py", line 260, in check_cache_and_compute
    if self.calculate(state, want_derived, **params_values_dict) is False:
  File "/cobaya/cobaya/theories/camb/camb.py", line 926, in calculate
    camb_params = self.cobaya_camb.set(params_values_dict, state)
  File "/cobaya/cobaya/theories/camb/camb.py", line 773, in set
    raise LoggedError(
cobaya.log.LoggedError: Some of the parameters passed to CAMB were not recognized: Unrecognized parameter: zre
cmbant commented 3 years ago

You can just delete the tau lines and use zrei rather than zre?

pcampeti commented 3 years ago

Yes, I tried to directly name the parameter 'zrei' (without using 'renames') and deleting the derived tau lines but I still get this error:

[camb] *ERROR* Some of the parameters passed to CAMB were not recognized: Unrecognized parameter: zre
[exception handler] ---------------------------------------

Traceback (most recent call last):
  File "/cobaya/cobaya/theories/camb/camb.py", line 715, in set
    params = self.camb.set_params(**base_args)
  File "/cosmo_codes/code/CAMB/camb/camb.py", line 180, in set_params
    raise CAMBUnknownArgumentError("Unrecognized parameter: %s" % k)
camb.baseconfig.CAMBUnknownArgumentError: Unrecognized parameter: zre

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/cobaya-run", line 33, in <module>
    sys.exit(load_entry_point('cobaya', 'console_scripts', 'cobaya-run')())
  File "/cobaya/cobaya/run.py", line 216, in run_script
    run(info, **arguments.__dict__)
  File "/cobaya/cobaya/run.py", line 143, in run
    sampler = sampler_class(updated_info["sampler"][sampler_name],
  File "/cobaya/cobaya/sampler.py", line 268, in __init__
    self.initialize()
  File "/cobaya/cobaya/samplers/minimize/minimize.py", line 193, in initialize
    if np.isfinite(self.logp(initial_point)):
  File "/cobaya/cobaya/samplers/minimize/minimize.py", line 153, in <lambda>
    self.logp = lambda x: method(x, **kwargs)
  File "/cobaya/cobaya/model.py", line 341, in loglike
    ret_value = self.loglikes(params_values, return_derived=return_derived,
  File "/cobaya/cobaya/model.py", line 305, in loglikes
    return self._loglikes_input_params(input_params, return_derived=return_derived,
  File "/cobaya/cobaya/model.py", line 310, in _loglikes_input_params
    result = self.logps(input_params, return_derived=return_derived, cached=cached,
  File "/cobaya/cobaya/model.py", line 245, in logps
    compute_success = component.check_cache_and_compute(
  File "/cobaya/cobaya/theory.py", line 260, in check_cache_and_compute
    if self.calculate(state, want_derived, **params_values_dict) is False:
  File "/cobaya/cobaya/theories/camb/camb.py", line 926, in calculate
    camb_params = self.cobaya_camb.set(params_values_dict, state)
  File "/cobaya/cobaya/theories/camb/camb.py", line 773, in set
    raise LoggedError(
cobaya.log.LoggedError: Some of the parameters passed to CAMB were not recognized: Unrecognized parameter: zre

Seems like the code still calls the parameter 'zre' even if in the yaml I call it 'zrei'...

cmbant commented 3 years ago

Delete renames under camb (or put other way round)?

pcampeti commented 3 years ago

Yeah I deleted 'renames' under the 'theory: camb:' part. For clarity the relevant part of the .yaml file now looks like

zrei:
   prior:
      min: 0.
      max: 20.
   proposal: 7.67
   latex: z_\mathrm{re}

theory:
  camb:
    path: /cosmo_codes/code/CAMB/
    stop_at_error: False
    extra_args:
      lens_potential_accuracy: 1
      WantScalars: True
      WantTensors: True
      parameterization: 1
JesusTorrado commented 3 years ago

Looking at CAMB's code, at first sight it looks that the root of the problem is that, when taken as an input, CAMB uses the name zrei (see [1]), but when requested as derived parameter, it uses zre (see [2]). This leaves Cobaya in a tricky situation, since the alias was intended for requesting it as a derived parameter, but it breaks it when given as an input.

@pcampeti As a workaround, clone Cobaya, install with pip install -e . from cobaya's root folder, and comment out line [3].

@cmbant It looks to me that the cleanest solution would be to unify CAMB's I/O nomenclature and create a get_zrei alias for [2], which should be backwards-compatible.

Otherwise, the only solutions I can think of on Cobaya's side would be pretty ugly: (but maybe you can think of a better one?)

[1] https://github.com/cmbant/CAMB/blob/dec68f121844a0940fadb828c54fc995241095f8/camb/model.py#L410 [2] https://github.com/cmbant/CAMB/blob/dec68f121844a0940fadb828c54fc995241095f8/camb/model.py#L613 [3] https://github.com/CobayaSampler/cobaya/blob/a97a6945f63763673abeb28e58ad1f634487314a/cobaya/theories/camb/camb.yaml#L28

pcampeti commented 3 years ago

Thanks a lot @JesusTorrado the workaround works great

cmbant commented 3 years ago

So just adding get_zrei alias function for get_zre to CAMB [2] would be sufficient to remove the default yaml "renames" for zre, so Cobaya then always uses "zrei"? Sounds OK to me.

JesusTorrado commented 3 years ago

Yes.

cmbant commented 3 years ago

Is in CAMB master now

JesusTorrado commented 3 years ago

Tested locally and pushed removal of line. Closing here. Thanks for pointing this one out, @pcampeti !