Skip to main content

Easy conversions between different styles of expressions

Project description

formulate

Actions Status Documentation Status

PyPI version Conda-Forge PyPI platforms

GitHub Discussion Scikit-HEP

Formulate

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, ideally inside a virtual environment:

pip install formulate

or using conda:

conda install -c conda-forge formulate

(-c conda-forge is only needed if you don't have the conda-forge channel already configured)

Roadmap and releases

For the roadmap, planned features, breaking changes and versioning please see the roadmap.

Usage

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
Call(function='sqrt', arguments=[BinaryOperator(operator='add', left=BinaryOperator(operator='add', left=BinaryOperator(operator='pow', left=Symbol(name='X_PX'), right=Literal(value=2)), right=BinaryOperator(operator='pow', left=Symbol(name='X_PY'), right=Literal(value=2))), right=BinaryOperator(operator='pow', left=Symbol(name='X_PZ'), right=Literal(value=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)))'

CLI

The package also provides a command-line interface for converting expressions between different styles. To use it, simply run the formulate command followed by the input expression and the desired output.

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

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

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

$ formulate --from-root '(A && B) || TMath::Sqrt(1.23) * e_num**1.2 + 5*pi' --named-constants
exp1
pi

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

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-1.0.1.tar.gz (49.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

formulate-1.0.1-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: formulate-1.0.1.tar.gz
  • Upload date:
  • Size: 49.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for formulate-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1f0e8f4094b6c7ac5901a7d557238ae2838dffc3b62fbbd430119e743d1d1f31
MD5 929628188c5aa8f42861dc67904b57e3
BLAKE2b-256 a11fba323b1280d29772af08b332b0d717d9298be82927d5426016fb7feab381

See more details on using hashes here.

Provenance

The following attestation bundles were made for formulate-1.0.1.tar.gz:

Publisher: cd.yml on scikit-hep/formulate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: formulate-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for formulate-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56003379503a29a7cff6fcaa0d34a939b874a5f35312531d7663848c4d6240a9
MD5 a88fe0f6c6cc6e6855cd889249538f48
BLAKE2b-256 2d1205530762d1d4ccf6da0b3dc2bdddaf20a1569545f629e5e56c2aeaf6819d

See more details on using hashes here.

Provenance

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

Publisher: cd.yml on scikit-hep/formulate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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