lesgourg / class_public

Public repository of the Cosmic Linear Anisotropy Solving System (master for the most recent version of the standard code; GW_CLASS to include Cosmic Gravitational Wave Background anisotropies; classnet branch for acceleration with neutral networks; ExoCLASS branch for exotic energy injection; class_matter branch for FFTlog)
235 stars 291 forks source link

Python wrapper #291

Open ghost opened 5 years ago

ghost commented 5 years ago

Hello everyone,

I was able to modify CLASS in order to include a new model that only adds a new parameter and everything worked fine. Now I am trying to use MontePython and I think I am missing something while changing the Python wrapper of CLASS. I did the following:

-I did every change in CLASS to add the new parameter. I run it and It worked with no problem and calculated everything.

-In CLASS Python classy.pxd file I added a line in the background struct as double newparamiter. I did everything of make clean, make and python build and install following the instructions.

-In MontePython I added the new parameter in the .param file.

I think I am missing something in doing the Python Wrapper of CLASS. Any idea of what I am missing?

Thanks!!!

carlosggarcia commented 5 years ago

Hi, Did you add it in classy.pyx? MontePython calls get_current_derived_parameters to get the value of the parameters in data.parameters. So you would need to add your parameter in get_current_derived_parameters if you haven't. Cheers, Carlos

ghost commented 5 years ago

Hi, thanks for the reply!

You mean I have to do in the classy.pyxfile something like the following?

def get_current_derived_parameters(self, names):

   if type(names) != type([]):
      raise TypeError("Deprecated")

    derived = {}
    for name in names:
        if name == 'h':
            value = self.ba.h
        elif name == 'H0':
            value = self.ba.h*100
        elif name == 'newparamiter':
           value = self.ba.newparamiter

*newparamiter is declared in the background modeules and used in the background and perturbations modeules.

carlosggarcia commented 5 years ago

Yes, that's it.

ghost commented 5 years ago

I wrote it as it is there and I get the same error of segmentation fault. I also defined in the classy.pxd before as I wrote it in the first post, so is it needed in both places or just in the .pyx file?

carlosggarcia commented 5 years ago

It's needed in both places. I need more info to understand where the segmentation fault comes from. BTW, the value = self.ba.newparamiter has the indentation wrong but I don't think that is causing the segmentation fault. You may try fixing it.

ghost commented 5 years ago

The indentation was just a copy/past issue, in the code is fine.

The changes I made are the following:

+CLASS:

I run class and it works perfectly, then I usemake clean; make;, cd python/, python setup.py buildand python setup.py install --user

+Montepython:

carlosggarcia commented 5 years ago

Just to double check. Did you run the problematic model in CLASS and worked fine? Does the segmentation fault give you some output? (if so, it might be helpful to see it) It might be a double free or something like that.

ghost commented 5 years ago

Yes, I run it in CLASS using a .ini file with the newparamiter (changing the values of different Omega and using use_ppf= no...) and everything works fine and I obtained plots that I previously expected.

What happens when I try to run MontePython is the following:

+CLASS

$cd python/
$python setup.py build
running build
running build_ext
skipping '/mnt/Datos/usuario/Escritorio/code/class/python/../python/classy.c' Cython extension (up-to-date)
$python setup.py install --user
running install
running build
running build_ext
skipping '/mnt/Datos/usuario/Escritorio/code/class/python/../python/classy.c' Cython extension (up-to-date)
running install_lib
running install_egg_info
Removing /home/usuario/.local/lib/python2.7/site-packages/classy-2.7.2.egg-info
Writing /home/usuario/.local/lib/python2.7/site-packages/classy-2.7.2.egg-info

+In MontePython

$ python montepython/MontePython.py run -o test -p example.param
Running Monte Python v3.2.0

with CLASS v2.7.2
 /!\ Running CLASS from a non version-controlled repository
 /!\ Detecting empty folder, logging the parameter file

Testing likelihoods for:
 -> fake_planck_bluebook

Initialised likelihood_mock_cmb with following options:
  unlensed_clTTTEEE is False
  Bmodes is False
  delensing is False
  LensingExtraction is False
  neglect_TD is True
  ExcludeTTTEEE is False
  OnlyTT is False

Creating test/2019-10-03_10__1.txt

Deduced starting covariance matrix:
['omega_b', 'omega_cdm', 'n_s', 'A_s', 'h', 'tau_reio', 'newparamiter']
[[2.56e-04 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
 [0.00e+00 2.56e-06 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
 [0.00e+00 0.00e+00 1.60e-05 0.00e+00 0.00e+00 0.00e+00 0.00e+00]
 [0.00e+00 0.00e+00 0.00e+00 1.44e-03 0.00e+00 0.00e+00 0.00e+00]
 [0.00e+00 0.00e+00 0.00e+00 0.00e+00 4.22e-05 0.00e+00 0.00e+00]
 [0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 1.94e-05 0.00e+00]
 [0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 0.00e+00 1.00e-02]]
Update routine is enabled with value 50 (recommended: 50)
This number is rescaled by cycle length 7 (N_slow + f_fast * N_fast) to 350
Segmentation fault (core dumped)
carlosggarcia commented 5 years ago

Ok. I'd do two checks:

  1. It seems that classy.c is not being generated so I'll try removing the previous classy.c to force a new compilation.
  2. I'd take the values of the parameters that you're setting in MP and compute the cosmology with classy instead of ./class. It might give you more information.
ghost commented 5 years ago

Hi

  1. I deleted the classy.c file and redone all make clean, make, cd python... and:
$cd python
$ python setup.py build
running build
running build_ext
skipping '/mnt/Datos/usuario/Escritorio/code/class/python/../python/classy.c' Cython extension (up-to-date)
$ python setup.py install --user
running install
running build
running build_ext
skipping '/mnt/Datos/usuario/Escritorio/code/class/python/../python/classy.c' Cython extension (up-to-date)
running install_lib
running install_egg_info
Removing /home/usuario/.local/lib/python2.7/site-packages/classy-2.7.2.egg-info
Writing /home/usuario/.local/lib/python2.7/site-packages/classy-2.7.2.egg-info

A new classy.c appeared in /python. And again when using Montepython the same problem.

The different possible values of the new parameter has been previously run in class using the .ini file and range is not problematic. Sorry I am just a beginner in python, what do you mean by running with classy?

carlosggarcia commented 5 years ago

Look at the wiki to see how to use classy: https://github.com/lesgourg/class_public/wiki/Python-wrapper

ghost commented 5 years ago

Hi,

Thanks for the help! Well this appeared when I tried that:

Error in Class: Class did not read input parameter(s): newparamiter

The newparamiter is used only in the modules where:

if (pba->has_fld == _TRUE_) {

      if (pba->use_ppf == _FALSE_){...

This is known to MontePython in any way?

carlosggarcia commented 5 years ago

It seems that you didn't add your new parameter in input.c to be read. ./class ignore the undefined parameters, but classy doesn't. You have to add newparmiter to input.c to be read.

ghost commented 5 years ago

But I have defined in this way inside input.c of CLASS:

In input.c inside int input_read_parameters I wrote class_read_double("newparamiter",pba->newparamiter); and inside int input_default_paramsI have pba->newparamiter=0.;

And in fact CLASS read it because using different values in the .ini file I obtain different output for P(k) or C_l

Is there something more to add?

carlosggarcia commented 5 years ago

I've run out of ideas. I'd need to see the code. You have to manage to make your model work with classy before using MP, as it calls classy to compute the cosmology.

ghost commented 5 years ago

I have look in another issues for some problem with the Python part while running class works with ./class. One of them suggest this test and i found the same error that the one in MP is happening while executing it

$python -c "from classy import Class; cosmo=Class(); cosmo.set({'output':'tCl', 'Omega_Lambda':0.0,'w0_fld':-0.9,'newparamiter':1.0});cosmo.compute()"
Segmentation fault (core dumped)

Does this give any idea of what I am doing wrongly?

carlosggarcia commented 5 years ago

The command seems ok, it must be something about the implementation.

ghost commented 5 years ago

I will try to modify the implementation in a way it works. Thanks for the help!!

carlosggarcia commented 5 years ago

No problem, just share the solution here when you find it so that other people can learn from it.

janinarenk commented 5 years ago

Hi -- maybe try to just add a new parameter to the background structure without modifying the physics and see if that works for you. A patch to add my_new_param is attached (based on commit 3dc78882860a23a4952c5e49d4b20bc51dd000d0).

With this the command python -c "from classy import Class; cosmo=Class(); cosmo.set({'output':'tCl', 'my_new_param':2}); cosmo.compute()" should run fine after running make clean; make all.

The SegFault you see comes from trying to access unmapped memory. So double check if you declared the new parameter everywhere and that there is not type mismatch.

Cheers, Janina

patch_new-bg-param.txt

JorgeVenzor commented 4 years ago

I'm having the same issue with CLASS 2.9 and python 3.7 (via anaconda) and Mac Os Catalina, with both gcc 9.2 (via homebrew) or clang (the system default C compilers)

So I decided to test the same modification on Linux, and it worked!

So, if you are working on macos I recomend you to do the same