Python libraries of interest to particle physicists. This is meant for educational purposes.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
AUTHORS
CONTRIBUTING.md
LICENSE
README.md

README.md

Python Libraries of Interest to Particle Physics

Join the chat at https://gitter.im/HSF/PyHEP DOI

Python libraries of interest to particle physicists. This is meant to be a living document. Therefore, if you have suggestions, click the edit button then make a pull request with your proposed change(s).

You are more than welcome to join the HSF/PyHEP Gitter channel and contribute to the informal discussions there.

New to Python

If you are new to Python, the following contains general information on using Python in Science.

Name Use
Software Carpentry Python Lesson Lesson aimed at people who have never used Python before.
Scipy tutorials You'll want the beginner courses, the intermediate/advanced courses are actually quite advanced. Setup instructions are linked on the page, videos are here
Dive into Python 3 Very useful for learning python, though it's a bit old and doesn't cover any of the scientific python stuff you really need.
Code academy
Many EdX and Coursera courses Often introductory CS courses which can teach other useful skills (algorithms and datastructures)
The python docs

Otherwise, just google python + description of problem, usually answer is on stackoverflow.

Youtube channels with talks / tutorials:

Pycon, e.g.:

Some more advanced talks of interested:

Getting Python

Name Use
Anaconda Package Manager Anaconda packages most scientific Python libraries while also living purely in user space. Therefore, you don't need special permissions to setup.
pipenv Official Python way.
ripa ripa solves the packaging issue by letting you install packages (or requirements.txt) where priority given to conda channels, otherwise fetches from PyPI. (Works but rough)

Scientific Python Stack

The packages that are used in Physics and/or data science within Python grew somewhat organically before forming the current ecosystem. Detailed information on what exists can be found here, but we will summarize here.

Name Use
jupyter notebook Main one way of doing interactive and/or exploratory analysis.
numpy Array and matrix operations (including math operations) at C speeds.
pandas A very elegant way to work with tabular data (i.e. ntuples) with in memory calculations. Especially good at time series.
xarray Extension of pandas to N-Dim structures.
h5py Simple numpy to HDF5 bindings (backend for Keras saved models).
scipy Various scientific routines like minimization.
matplotlib Main Python plotting library. Start from matplotlib gallery then adapt to your application.
scikit-learn Very easy to use machine learning routines with great examples.

Visualisation:

Name Use
matplotlib Main Python plotting library. Start from matplotlib gallery then adapt to your application.
seaborn Easier to use plotting library with some statistical routines. Builds on matplotlib, but annoying to customize.
vegascope View Vega/Vega-Lite plots in your web browser from local or remote Python processes.

Notable mentions:

Name Use
tensorflow For deep neural networks.
pytorch deep learning framework for fast, flexible experimentation with dynamic computational graphs.
keras Higher level neural network interfaces.

General information through talks tthat maybe useful on PyData (various conferences each year):

Particle Physics packages

Name Use
numpythia Interface between FastJet and NumPy.
pyjet Interface between PYTHIA and NumPy.

Tutorials

See tutorials here and other resources collected by IML HEP-ML Resources.

ROOT and interoperability with ROOT

For many particle physics experiments, a lot of data is stored within ROOT files. This means at very least one must have the ability to read ROOT files. ROOT also serves as a tool suite designed to solve many computational problems encountered in HEP, which means that one may want to access some of this tool suite. The following packages below are worth knowing for these situations:

Package name Use Pro Con Further information
ostap User-friendly & more intuitive interface to(Py)ROOT Many decorations to ROOT classes Requires C++ code compilation
uproot Native Python ROOT I/O Easy to install, fast, no dependence on C++ ROOT Although can read all ROOT files, can only write ROOT files with specific objects.
root_numpy, root_pandas ROOT to/from Numpy and Pandas, like uproot full ROOT functionality, like TFormula slower than uproot, binary incompatibilities with different versions of ROOT
ROOT conda Using ROOT within Anaconda Easy to get ROOT installed with PyROOT support Not all features of ROOT and getting dated (6.04 Py2.7/3.4 since XENON1T uses that) Recipes
PyROOT Official ROOT Python bindings Good support and many examples Raw C++ wrapping results in weird Python code
rootpy Pythonic ROOT access More logical for people who know Python Smaller user base than PyROOT, abandoned? Repository
alphatwirl Summerizing ROOT data into categorical data as Pandas' data frames Small output size. Easy one-function interface with qtwirl Not for data type conversion Repository
pyhf statistical analysis / fitting pure python implementation of HistFactory specification with auto-diff enabled backends in tensorflow, pytorch, and MXNet not yet interoperable with ROOT-based RooFit models pyhf

Jupyter extensions

Jupyter has a wide ecosystem of extensions that can be used to extend the functionality. Some useful extensions for HEP data analysis are summarised here.

Name Use
nbdime Simplifies diffing and merging of jupyter notebooks that are stored in version control.
jupytext Splits notebooks into a .ipynb and .py file for easier version control and to allow them to be run as scripts idependently of jupyter.

Speeding up code

Often, it is not needed anymore to write C++/C routines that get wrapped since there are other ways to speed up your Python code. Namely:

Name Use
numba Tight loops are often the slow part of Python code, where this compiles them!
Pythran whole scripts
numpy Expressing your code as array options means you get native-C speeds.
NumExpr single pass "mapper" operations (one input → one output).

Binding C/C++ to Python

Before you read this, realize that this is for existing C++ code. If you want to write new C/C++ code for speed, see section above.

Python entered into the particle physics ecosystem since it was useful as a 'glue lanaguage'. This means that you can get multiple softwares in different languages to work with one another. Given the large ecosystem of Python packages in the last decade, this is less common now. However, the situation still does arise that you want to call some existing C/C++ code from Python.

Please be aware that wrapping C code is signficantly easier than wrapping C++ code due to details of how function names get garbled in libraries within C++.

At present, the best summary of how to bind code in HEP applications comes from Henry Schreiner in a 2018 PyHEP talk

(TODO: improve table. Most non-pybind11 have given me hundreds of hours of pain so I can't comment objectively)

Package name Use Pro Con Further information
pybind11 Wrapping existing C++ codes Small elegant package Young but quickly widely used. Henry's slides
Cython Wrapping C++ code Widely used, freely mixing Python and C++. Weird syntax, incomplete coverage of C++
swig Wrapping C++ code Widely used. Have to write wrapper file and feels dated.
Boost Wrapping C++ code Widely used. Giant dependency since Boost does many other things.

Experimental codes

Stealing code from other physicists is its own sign of flattery. Codes that are abandoned more than two years will get struck through.:

Name Collaboration Use Further information Date added to list