![Page 1: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/1.jpg)
classy - The Python wrapper
Thomas Tram
Institute of Gravitation and Cosmology
October 27, 2014
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 1 / 15
![Page 2: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/2.jpg)
Compiled and interpreted languages
Compiled languagesThe code must be compiled before it can be executed.It will often be faster...but less flexible since some decisions can not be madeat runtime.C, C++, Fortran, ...
Interpreted languagesThe code is interpreted during execution.It can be slower but is very flexible.MATLAB, Octave, IDL, Python, ...
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 2 / 15
![Page 3: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/3.jpg)
Interfacing C with Python
CythonCython is a compiled language.It understands most Python syntax.It can directly call external C libraries, such aslibclass.a.It produces a Python module.
classy, the class wrapperWritten in Cython.Automatically compiled and installed when you typemake.Needed for Monte Python and when using class
from Python.
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 3 / 15
![Page 4: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/4.jpg)
The classy module
classy, the class wrapperAll the functionality of classy is found in the Python
class called Class.In Python import Class by: from classy import Class
Running class from Pythonfrom classy import Class
import numpy as np
import matplotlib . pyplot as plt
cosmo = Class ()
cosmo .set ({ ’output ’:’tCl ,pCl ,lCl ’,’lensing ’:’yes ’,’
modes ’:’s,t’,’r’:’0.2 ’})
cosmo . compute ()
cosmo . cleanup ()
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 4 / 15
![Page 5: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/5.jpg)
Exercise 1b in Python 1/2
Solving exercise 1b in Pythonfrom classy import Class
import numpy as np
import matplotlib . pyplot as plt
cosmo = Class ()
cosmo .set ({ ’output ’:’tCl ,pCl ,lCl ’,’lensing ’:’yes ’,’
modes ’:’s,t’,’r’:’0.2 ’})
cosmo . compute ()
cosmo . cleanup ()
l = np. array ( range (2 ,2501))
factor = l*(l+1) /(2* np.pi)
raw_cl = cosmo . raw_cl (2500)
lensed_cl = cosmo . lensed_cl (2500)
raw_cl . viewkeys ()
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 5 / 15
![Page 6: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/6.jpg)
Exercise 1b in Python 2/2
Solving exercise 1b in Pythonplt. loglog (l, factor * raw_cl [’bb ’][2:] ,l, factor *
lensed_cl [’bb ’][2:])
plt. xlabel (r"$\ell$")
plt. ylabel (r"$\ell (\ ell +1) /(2\ pi) C_l ^{ BB}$")
plt. tight_layout ()
plt. savefig (" solution1b .pdf")
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 6 / 15
![Page 7: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/7.jpg)
Interactive mode
IPython NotebookIPython Notebook is a Mathematica style (cell) interface toIPython.
Has Tab-completion of variables and function names.Nicely presents the documentation of each function.Easy way to get started on Python.
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 7 / 15
![Page 8: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/8.jpg)
Interactive mode
Launching IPython NotebookWrite the following command to launch the notebook:ipython notebook --pylab=inline
--InlineBackend.figure_format=svg
You probably want to alias this command to e.g. inote.You can open an existing notebook byinote MyFirstCLASSNotebook.ipynb
AliasingTo make an alias, open your shell {bash, zsh, ...} startupscript: {~\.bashrc, ~\.zshrc, ...}At the bottom of the file, add the linealias inote="some command"
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 8 / 15
![Page 9: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/9.jpg)
The notebook
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 9 / 15
![Page 10: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/10.jpg)
Tab: Available class methods
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 10 / 15
![Page 11: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/11.jpg)
Shift+Tab: Help on method
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 11 / 15
![Page 12: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/12.jpg)
Shift+Tab: More help
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 12 / 15
![Page 13: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/13.jpg)
What can it do?
What is available in the wrapper?get_background() returns the information normallyfound in _background.dat.get_thermodynamics() returns the information of_thermodynamics.dat.get_primordial() corresponds to _primordial_Pk.dat.get_perturbations() returns everything found in_perturbations*.dat
get_transfer(z,format) returns the density and velocitytransfer functions at anyú redshift z. (Format can beeither ’camb’ or ’class’).
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 13 / 15
![Page 14: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/14.jpg)
And even more...
What is available in the wrapper?raw_cl() returns unlensed C¸.lensed_cl() returns lensed C¸.density_cl() returns density C¸.pk(k, z) returns the P(k) at redshift z.Many other small functions.
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 14 / 15
![Page 15: classy - The Python wrapper · Interfacing C with Python Cython Cython is a compiled language. It understands most Python syntax. It can directly call external Clibraries,suchas libclass.a](https://reader034.vdocuments.net/reader034/viewer/2022042803/5f4c056a4118014c5b7cc093/html5/thumbnails/15.jpg)
Exercise!
IPython Notebook exerciseTry to solve some or all of exercise 1a-1d from yesterdayusing the IPython Notebook.
Example notebooksPlay around with some of the example notebooks found inIPythonNotebooks folder on Dropbox.
T. Tram (ICG) Lecture 7 : Wrapper October 27, 2014 15 / 15