Skip to main content

A generic correction library

Project description

correctionlib

Actions Status Documentation Status Code style: black

PyPI version PyPI platforms

GitHub Discussion

Introduction

The purpose of this library is to provide a well-structured JSON data format for a wide variety of ad-hoc correction factors encountered in a typical HEP analysis and a companion evaluation tool suitable for use in C++ and python programs. Here we restrict our definition of correction factors to a class of functions with scalar inputs that produce a scalar output.

In python, the function signature is:

from typing import Union

def f(*args: Union[str,int,float]) -> float:
    return ...

In C++, the evaluator implements this currently as:

double Correction::evaluate(const std::vector<std::variant<int, double, std::string>>& values) const;

The supported function classes may include:

  • multi-dimensional binned lookups;
  • binned lookups pointing to multi-argument formulas with a restricted math function set (exp, sqrt, etc.);
  • categorical (string or integer enumeration) maps; and
  • compositions of the above.

Each function type is represented by a "node" in a call graph and holds all of its parameters in a JSON structure, described by the JSON schema. Possible future extension nodes might include weigted sums (which, when composed with the others, could represent a BDT) and perhaps simple MLPs.

The tool should provide:

  • standardized, versioned JSON schemas;
  • forward-porting tools (to migrate data written in older schema versions); and
  • a well-optimized C++ evaluator and python bindings (with numpy vectorization support).

This tool will definitely not provide:

  • support for TLorentzVector or other object-type inputs (such tools should be written as a higher-level tool depending on this library as a low-level tool)

Formula support is currently planned via linking to ROOT libraries and using TFormula, however if possible we would like to avoid this external dependency. One alternative could be using the boost.spirit parser with some reasonable grammar-- this is the approach used for CMSSW's expression parser. There are also various C++ formula parsers such as ExprTk, and the python bindings may be able to call into numexpr, though, due to the tree-like structure of the corrections, it may prove difficult to exploit vectorization at levels other than the entrypoint.

Installation

The build process is Makefile-based for the C++ evaluator and via setuptools for the python bindings. Builds have been tested in Windows, OS X, and Linux, and python bindings can be compiled against both python2 and python3, as well as from within a CMSSW environment. The python bindings are distributed as a pip-installable package.

If you use python 3, you can simply pip install correctionlib (possibly with --user, or in a virtualenv, etc.)

To build the C++ evaluator in most environments:

git clone --recursive git@github.com:nsmith-/correctionlib.git
cd correctionlib
make
# demo C++ binding, main function at src/demo.cc
./demo data/examples.json

To compile with python2 support, consider using python 3 :) If you considered that and still want to us python2, follow the C++ build instructions and then call make PYTHON=python2 correctionlib to compile. Inside CMSSW you should use make PYTHON=python correctionlib assuming python is the name of the scram tool you intend to link against. This will output a correctionlib directory that acts as a python package, and can be moved where needed. This package will only provide the correctionlib._core evaluator module, as the schema tools and high-level bindings are python3-only.

Creating new corrections

The correctionlib python package provides a helpful framework for defining correction objects. Nodes can be type-checked as they are constructed using the parse_obj class method. Some examples can be found in convert.ipynb.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

correctionlib-1.1.0.tar.gz (305.8 kB view hashes)

Uploaded Source

Built Distributions

correctionlib-1.1.0-pp37-pypy37_pp73-win32.whl (181.1 kB view hashes)

Uploaded PyPy Windows x86

correctionlib-1.1.0-pp37-pypy37_pp73-manylinux2010_x86_64.whl (297.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

correctionlib-1.1.0-pp37-pypy37_pp73-macosx_10_14_x86_64.whl (255.8 kB view hashes)

Uploaded PyPy macOS 10.14+ x86-64

correctionlib-1.1.0-pp36-pypy36_pp73-win32.whl (181.2 kB view hashes)

Uploaded PyPy Windows x86

correctionlib-1.1.0-pp36-pypy36_pp73-manylinux2010_x86_64.whl (297.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

correctionlib-1.1.0-pp36-pypy36_pp73-macosx_10_14_x86_64.whl (255.8 kB view hashes)

Uploaded PyPy macOS 10.14+ x86-64

correctionlib-1.1.0-cp39-cp39-win_amd64.whl (202.2 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

correctionlib-1.1.0-cp39-cp39-win32.whl (181.4 kB view hashes)

Uploaded CPython 3.9 Windows x86

correctionlib-1.1.0-cp39-cp39-manylinux2010_x86_64.whl (297.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

correctionlib-1.1.0-cp39-cp39-manylinux2010_i686.whl (311.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

correctionlib-1.1.0-cp39-cp39-macosx_10_14_x86_64.whl (259.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.14+ x86-64

correctionlib-1.1.0-cp38-cp38-win_amd64.whl (204.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

correctionlib-1.1.0-cp38-cp38-win32.whl (181.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

correctionlib-1.1.0-cp38-cp38-manylinux2010_x86_64.whl (297.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

correctionlib-1.1.0-cp38-cp38-manylinux2010_i686.whl (313.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

correctionlib-1.1.0-cp38-cp38-macosx_10_14_x86_64.whl (257.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.14+ x86-64

correctionlib-1.1.0-cp37-cp37m-win_amd64.whl (204.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

correctionlib-1.1.0-cp37-cp37m-win32.whl (182.8 kB view hashes)

Uploaded CPython 3.7m Windows x86

correctionlib-1.1.0-cp37-cp37m-manylinux2010_x86_64.whl (300.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

correctionlib-1.1.0-cp37-cp37m-manylinux2010_i686.whl (315.7 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

correctionlib-1.1.0-cp37-cp37m-macosx_10_14_x86_64.whl (256.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.14+ x86-64

correctionlib-1.1.0-cp36-cp36m-win_amd64.whl (204.2 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

correctionlib-1.1.0-cp36-cp36m-win32.whl (182.8 kB view hashes)

Uploaded CPython 3.6m Windows x86

correctionlib-1.1.0-cp36-cp36m-manylinux2010_x86_64.whl (300.4 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

correctionlib-1.1.0-cp36-cp36m-manylinux2010_i686.whl (315.7 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

correctionlib-1.1.0-cp36-cp36m-macosx_10_14_x86_64.whl (256.8 kB view hashes)

Uploaded CPython 3.6m macOS 10.14+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page