Easy conversions between different styles of expressions
Project description
formulate
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f0e8f4094b6c7ac5901a7d557238ae2838dffc3b62fbbd430119e743d1d1f31
|
|
| MD5 |
929628188c5aa8f42861dc67904b57e3
|
|
| BLAKE2b-256 |
a11fba323b1280d29772af08b332b0d717d9298be82927d5426016fb7feab381
|
Provenance
The following attestation bundles were made for formulate-1.0.1.tar.gz:
Publisher:
cd.yml on scikit-hep/formulate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
formulate-1.0.1.tar.gz -
Subject digest:
1f0e8f4094b6c7ac5901a7d557238ae2838dffc3b62fbbd430119e743d1d1f31 - Sigstore transparency entry: 614126445
- Sigstore integration time:
-
Permalink:
scikit-hep/formulate@f89ea1a58206452e39dead50b1383cf3f03919ad -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/scikit-hep
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@f89ea1a58206452e39dead50b1383cf3f03919ad -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56003379503a29a7cff6fcaa0d34a939b874a5f35312531d7663848c4d6240a9
|
|
| MD5 |
a88fe0f6c6cc6e6855cd889249538f48
|
|
| BLAKE2b-256 |
2d1205530762d1d4ccf6da0b3dc2bdddaf20a1569545f629e5e56c2aeaf6819d
|
Provenance
The following attestation bundles were made for formulate-1.0.1-py3-none-any.whl:
Publisher:
cd.yml on scikit-hep/formulate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
formulate-1.0.1-py3-none-any.whl -
Subject digest:
56003379503a29a7cff6fcaa0d34a939b874a5f35312531d7663848c4d6240a9 - Sigstore transparency entry: 614126458
- Sigstore integration time:
-
Permalink:
scikit-hep/formulate@f89ea1a58206452e39dead50b1383cf3f03919ad -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/scikit-hep
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@f89ea1a58206452e39dead50b1383cf3f03919ad -
Trigger Event:
release
-
Statement type: