CAMB: missing function in CAMBdata class?

Use of Cobaya. camb, CLASS, cosmomc, compilers, etc.
Post Reply
Felipe Maldonado
Posts: 26
Joined: November 10 2016
Affiliation: FSU

CAMB: missing function in CAMBdata class?

Post by Felipe Maldonado » August 12 2018

Hello everyone,

I have been using camb.get_matter_power_interpolator to obtain a 2D interpolator for the matter power spectrum, which I later use for angular galaxy overdensity power spectra.

I need to change my cosmology in order to get derivatives for Fisher matrix forecasts, so this needs to be run twice per parameter and that gets slow, so I was looking into ways to make this faster.

In the Python docs, I found the same function but from the CAMBdata class: CAMBdata.get_matter_power_interpolator, which I figured would be faster due to reusing previous calculations, but I'm getting an error that the class doesn't have that function. I'm using CAMB version 0.1.6.1.

Here is the error message I get:

Code: Select all

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-6db66903546c> in <module>()
     11 results = camb.get_results(pars)
     12 
---> 13 interpolator = results.get_matter_power_interpolator()

AttributeError: 'CAMBdata' object has no attribute 'get_matter_power_interpolator'

CAMB version =  0.1.6.1
Any help will be appreciated.
Felipe Maldonado

Antony Lewis
Posts: 1941
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: CAMB: missing function in CAMBdata class?

Post by Antony Lewis » August 12 2018

The ReadTheDocs docs apply to the latest CAMB version from github.

Felipe Maldonado
Posts: 26
Joined: November 10 2016
Affiliation: FSU

Re: CAMB: missing function in CAMBdata class?

Post by Felipe Maldonado » August 13 2018

Thank you, professor Lewis.

Felipe Maldonado
Posts: 26
Joined: November 10 2016
Affiliation: FSU

Re: CAMB: missing function in CAMBdata class?

Post by Felipe Maldonado » August 16 2018

I have a follow up question, if I may.

I'm testing the difference between the two versions of get_matter_power_interpolator, and the CAMBdata version keeps crashing. The error message I see simply says "Error in halofit".

I would be very grateful if I could get some clarification on why this is happening.

Felipe Maldonado

Antony Lewis
Posts: 1941
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: CAMB: missing function in CAMBdata class?

Post by Antony Lewis » August 17 2018

Check the parameters look sensible and please give an explicit reproducible example if problems persist.

Code: Select all

print(pars)

Felipe Maldonado
Posts: 26
Joined: November 10 2016
Affiliation: FSU

Re: CAMB: missing function in CAMBdata class?

Post by Felipe Maldonado » August 17 2018

Hello,
Thank you for your reply.

I believe I've isolated the problem to CAMBparams.set_matter_power.

When I don't supply an array of redshifts, I get this error from scipy:

Code: Select all

error: (mx>kx) failed for hidden mx: regrid_smth:mx=1
I understand this error to mean that an interpolator doesn't have enough data points.

I then tried calling set_matter_power again, giving an array of redshifts from [math] down to [math] from numpy:

Code: Select all

cambpars.set_matter_power\
( redshifts=np.linspace( 0.0, 1089.90, 100 ), kmax=1000.0, nonlinear=True\
 , accurate_massive_neutrino_transfers=True )
Now I get 'Error in halofit' and the program crashes when calling CAMBdata.get_matter_power_interpolator (I don't even make it to evaluating it).

When I did print(pars), one thing jumps at me as being unusual (I'll spare you the long output, it's just a Planck 2015 cosmology):

Code: Select all

Transfer:
  high_precision = 1
  accurate_massive_neutrinos = 1
  num_redshifts = 1
  kmax = 1000.0
  k_per_logint = 0
  redshifts = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  PK_redshifts = [1089.9, 1078.8909090909092, 1067.8818181818183, 1056.8727272727274, 1045.8636363636365, 1034.8545454545456, 1023.8454545454546]
  NLL_redshifts = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  PK_redshifts_index = [0, 0, 0, 0, 0, 0, 0]
  NLL_redshifts_index = [0, 0, 0, 0, 0, 0, 0]
  PK_num_redshifts = 100
  NLL_num_redshifts = 0
As you can see, PK_redshifts only has 6 entries, when I passed 100.

With all the exact same parameters, except no call to CAMBparams.set_matter_power, camb.get_matter_power_interpolator doesn't fail, so I'm quite puzzled.

Felipe Maldonado

Antony Lewis
Posts: 1941
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: CAMB: missing function in CAMBdata class?

Post by Antony Lewis » August 17 2018

This works for me as an explicit example, with latest github camb installed and built:

Code: Select all

import camb
pars = camb.CAMBparams()
pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122)
pars.InitPower.set_params(ns=0.965, As=2e-9)
pars.set_matter_power(redshifts=np.linspace( 0.0, 1089.90, 100 ), kmax=1000.0, nonlinear=True\
 , accurate_massive_neutrino_transfers=True )
data = camb.get_results(pars)
PK=data.get_matter_power_interpolator()
(the six entries is just because the print function only prints six to keep things readable).

However I doubt you actually want the matter power spectrum up to z=1000...

Felipe Maldonado
Posts: 26
Joined: November 10 2016
Affiliation: FSU

Re: CAMB: missing function in CAMBdata class?

Post by Felipe Maldonado » August 18 2018

I also am running CAMB's latest version from github.

Your example runs without issue on my end. I did manage to make it crash with this minimal addition:

Code: Select all

import camb
import numpy as np

pars = camb.CAMBparams()
pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122)
pars.InitPower.set_params(ns=0.965, As=2e-9)
pars.set_matter_power(redshifts=np.linspace( 0.0, 1089.90, 100 ), kmax=1000.0, nonlinear=True\
 , accurate_massive_neutrino_transfers=True )
data = camb.get_results(pars)
PK=data.get_matter_power_interpolator()

#trying to change 1 parameter and reuse the CAMBdata instance
#pars.set_cosmology( tau=0.066 ) #this is Planck 2015's value
data.set_params( pars )

PK_perturbed=data.get_matter_power_interpolator() #crashes
I tried changing a parameter that doesn't affect the matter power spectrum, in this case [math], and it crashed. I then tried commenting out the line where I change [math] and it still crashes. This time, I don't get the "Error in halofit" message, however.

I'm interested in being able to reuse the CAMBdata instance that I first made, because I would like to get matter power spectra interpolators for slightly different cosmologies fast, so that I may obtain derivatives of power spectra for Fisher forecasts. At the moment, I'm using camb.get_matter_power_interpolator, and calling it every time I change the cosmology, which I imagine is what the new CAMBdata function is intended to speed up.

Antony Lewis
Posts: 1941
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: CAMB: missing function in CAMBdata class?

Post by Antony Lewis » August 19 2018

It won't make any significant difference to speed, unless you also want to use the computed transfer functions for something else other than getting the matter power interpolator object. I would use the wrapper functions (i.e. camb.get_matter_power_interpolator) unless you really want to dig in to how it works or need to optimize things like generating lots of results with the same cosmology but different initial power spectra (or generating both CL and P(k) interpolator objects).

If you set the parameters in CAMBdata, nothing is computed. You need to call calc_transfers() to actually get new data consistent with the new parameters. e.g. this will work

Code: Select all

import camb
pars = camb.CAMBparams()
pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122)
pars.InitPower.set_params(ns=0.965, As=2e-9)
pars.set_matter_power(redshifts=np.linspace( 0.0, 1089.90, 100 ), kmax=10.0, nonlinear=True\
 , accurate_massive_neutrino_transfers=True )
data = camb.get_results(pars)
PK=data.get_matter_power_interpolator()
pars.set_cosmology( tau=0.066 ) #this will change ombh2, H0 etc back to defaults as well...
data.set_params( pars )
data.calc_transfers(pars)
PK_perturbed=data.get_matter_power_interpolator() 
Note that

Code: Select all

pars.set_cosmology( tau=0.066 ) 
will also change lots of other parameters back to their defaults (many unspecified parameters have default values).

Felipe Maldonado
Posts: 26
Joined: November 10 2016
Affiliation: FSU

Re: CAMB: missing function in CAMBdata class?

Post by Felipe Maldonado » August 20 2018

I see.
I tested just now and after calling calc_transfers(), there is pretty much no difference in speed.

At the moment, I don't need the transfer functions, so the additional sophistication would not yield any advantage, so I will continue to use camb.get_matter_power_interpolator().

I appreciate your assistance, professor Lewis.

Post Reply