Skip to main content

Convert between different style of formulae

Project description

Build Status Coverage Status PyPI Scikit-HEP Project

Easy conversions between different styles of expressions. Formulate currently supports converting between ROOT and numexpr style expressions.

Installation

Install formulate like any other Python package:

pip install --user formulate

or similar (use `sudo, `virtualenv, or `conda` if you wish).

Usage

Command line usage

$ python -m formulate --from-root '(A && B) || TMath::Sqrt(A)' --to-numexpr
(A & B) | sqrt(A)

$ python -m formulate --from-numexpr '(A & B) | sqrt(A)' --to-root
(A && B) || TMath::Sqrt(A)

$ python -m formulate --from-root '(A && B) || TMath::Sqrt(1.23) * e**1.2 + 5*pi' --variables
A
B

$ python -m formulate --from-root '(A && B) || TMath::Sqrt(1.23) * e**1.2 + 5*pi' --named-constants
E
PI

$ python -m formulate --from-root '(A && B) || TMath::Sqrt(1.23) * e**1.2 + 5*pi' --unnamed-constants
1.2
1.23
5

API

The most basic usage involves calling from_$BACKEND and then to_$BACKEND, for example when starting with a ROOT style expression:

>>> import formulate
>>> momentum = formulate.from_root('TMath::Sqrt(X_PX**2 + X_PY**2 + X_PZ**2)')
>>> momentum
Expression<SQRT>(Expression<ADD>(Expression<POW>(Variable(X_PX), UnnamedConstant(2)), Expression<POW>(Variable(X_PY), UnnamedConstant(2)), Expression<POW>(Variable(X_PZ), UnnamedConstant(2))))
>>> momentum.to_numexpr()
'sqrt(((X_PX ** 2) + (X_PY ** 2) + (X_PZ ** 2)))'
>>> momentum.to_root()
'TMath::Sqrt(((X_PX ** 2) + (X_PY ** 2) + (X_PZ ** 2)))'

Similarly, when starting with a numexpr style expression:

>>> my_selection = formulate.from_numexpr('X_PT > 5 & (Mu_NHits > 3 | Mu_PT > 10)')
>>> my_selection.to_root()
'(X_PT > 5) && ((Mu_NHits > 3) || (Mu_PT > 10))'
>>> my_selection.to_numexpr()
'(X_PT > 5) & ((Mu_NHits > 3) | (Mu_PT > 10))'

If the the type of expression isn’t known in advance formulate can also auto detect it:

>>> my_sum = formulate.from_auto('True + False')
>>> my_sum.to_root()
'true + false'
>>> my_sum.to_numexpr()
'True + False'

The Expression Object

When calling from_* the returned object is derived from formulate.ExpressionComponent. From this object you can inspect the expression to find it’s dependencies:

>>> my_check = formulate.from_auto('(X_THETA*TMath::DegToRad() > pi/4) && D_PE > 9.2')
>>> my_check.variables
{'D_PE', 'X_THETA'}
>>> my_check.named_constants
{'DEG2RAD', 'PI'}
>>> my_check.unnamed_constants
{'4', '9.2'}

Additionally ExpressionComponent s can be combined using both operators and numpy functions:

>>> new_selection = (momentum > 100) and (my_check or (numpy.sqrt(my_sum) < 1))
>>> new_selection.to_numexpr()
'((X_THETA * 0.017453292519943295) > (3.141592653589793 / 4)) & (D_PE > 9.2)'

As the == operator returns a new expression, it can’t be used to check for equality. Instead the .equivalent method should be used:

TODO: Implement this using expression.equivalent !

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

formulate-0.1.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distributions

formulate-0.1.0-py3-none-any.whl (71.9 kB view details)

Uploaded Python 3

formulate-0.1.0-py2.py3-none-any.whl (21.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file formulate-0.1.0.tar.gz.

File metadata

  • Download URL: formulate-0.1.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for formulate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2bc954678b44e5f599ea9cc45d4d3e625f5a2ff836191f327aa9b8a832ca31ba
MD5 0ec82b91ed74d468c5786e96655d3b79
BLAKE2b-256 0cc2c2694415e4642f5b9844d6eb3e3825cfb04decaf857f66d2e52b7c259fc3

See more details on using hashes here.

File details

Details for the file formulate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: formulate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 71.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for formulate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 259f38870cf1e73245ecb170897ed1053a38490100529c70209c6fb0cb82c7fc
MD5 800f9bdf9696f0b7f25c9c9c121efa61
BLAKE2b-256 279e687cdd189ec1ba81008ca51917ec5511ded74ddc9bb195607cdb945f2d2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for formulate-0.1.0-py3-none-any.whl:

Publisher: cd.yml on scikit-hep/formulate

Attestations:

File details

Details for the file formulate-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: formulate-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for formulate-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fa5a9b61c8c8249f8e90a2f83de9cd9e5705be634416c2766ca076b1073c2e13
MD5 745281aee8a3f43d30267f8c8fed57c2
BLAKE2b-256 c93e2874edb9643015388b351cc856d4b58c6d32bfbd841305799d0efcba30ef

See more details on using hashes here.

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