Repository navigation
Conversation
Add `formulate.evaluate()` and `AST.evaluate()`, which compute an expression on arrays without requiring NumExpr. The default "numpy" engine walks the AST with the existing iterative fold and applies NumPy functions, so it also works on awkward and pandas inputs; the "numexpr" engine hands `to_numexpr()` output to NumExpr. Semantics follow ROOT's per-entry model, checked against TTreeFormula: reductions act on the last axis, Min$/Max$ of an empty entry are 0, indexing a short jagged entry gives a missing value, logical operators are logical, and ** is computed in floating point. NumPy becomes an optional `formulate[numpy]` extra, loaded through the new `formulate.extras` module, and `_encode_name` moves to identifiers to avoid an import cycle. Assisted-by: Claude Code:claude-opus-5-5
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
|
- min/max of a 1-D awkward array return a 0-d array, and 0 when empty, instead of a 1-element array or a TypeError. - min/max of empty rectangular entries keep the input dtype. - Only `variables=None` counts as "nothing passed"; other wrong types raise their own TypeError instead of a misleading NameError. - The NumPy engine evaluates under np.errstate(divide="ignore", invalid="ignore"), so guarded expressions such as where(x > 0, 1 / x, 0) no longer warn, matching NumExpr and ROOT. - Document and pin against ROOT that TTreeFormula gives 0 for a division by zero, where NumExpr, NumPy and evaluate give inf. - Quote the extras in the install hint and list `~` in the docstring. Assisted-by: Claude Code:claude-opus-5-5
Assisted-by: Antigravity:Gemini 4 Neon
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Adds expression evaluation to formulate, so NumExpr is no longer needed to compute an expression.
API
expressionis a string, parsed according tosyntax("numexpr"or"root"), or an already parsed expression.variablesis anything indexable by name: a dict, a pandasDataFrameor an awkward record array. Lists are converted to arrays.NameErrorlisting every missing name.Engines
"numpy"(default): walks the AST with the existing iterative_traversal.foldand applies a NumPy function at every node, through a new_evaluatormethod on each node type, analogous to_serializer. It works on any array NumPy dispatches to, jagged awkward arrays included."numexpr": passesto_numexpr()output to NumExpr, with names likebranch.leafmapped to their encoded form. It supports fewer expressions, and refuses reductions when an input has more than one dimension, since NumExpr would reduce the whole array rather than each entry.Semantics
These follow ROOT's per-entry model, and were checked against
TTreeFormulaon a variable-length branch:sum/prod/min/max/length(Sum$,Min$,Length$, …) reduce along the last axis. That is per entry on 2-D or jagged arrays, and the whole array on 1-D arrays, as NumExpr does.Min$/Max$of an empty entry are 0.arr[i]picks positioniwithin every entry. On awkward arrays, an entry too short to have it givesNone. Only non-negative integer literal indices are supported.&&/||/!and&/|/^/~are logical operations.**is computed in floating point, son ** -1is a fraction.%is floating-point modulo, as before (see the modulo section ofissues.rst).a:breturns a tuple of arrays.inf/nansilently: the NumPy engine runs undernp.errstate(divide="ignore", invalid="ignore"), becausewherecomputes both branches and a guardedwhere(x > 0, 1 / x, 0)would otherwise warn. Overflow and underflow keep the caller's settings.TTreeFormulainstead returns 0 for a division by zero; this is documented inissues.rstand pinned by a test, like%.Other changes
formulate.extrasmodule (uproot-style loaders) and aformulate[numpy]extra. NumPy is still not a required dependency, and is imported only when evaluating._encode_namemoved fromAST.pytoidentifiers.pyto avoid an import cycle.testanddocsgroups.guide/evaluation.rst), new sections inissues.rst(scalar-branch reductions, how the engines differ), an evaluation subsection inexpressions.rst, and updates to the API pages, installation page, README and CHANGELOG.Deferred
A bug that was already in main is documented here but not fixed:
to_numexpr()turns ROOT's!ninto~n, which NumExpr computes bitwise on integers. The"numexpr"engine inherits this.