Add arithmetic expression support in batch matrix parameters - #107
Open
arthur-flam wants to merge 1 commit into
Open
Add arithmetic expression support in batch matrix parameters#107arthur-flam wants to merge 1 commit into
arthur-flam wants to merge 1 commit into
Conversation
…ions
Matrix interpolation only substituted values, so correlated parameters
(e.g. thresholds scaling with gain) had to be maintained as parallel
pre-computed lists. Batch configurations can now embed arithmetic:
matrix:
gain: [1, 2]
configs:
- SMAP.bp_th: "[${{ 168 * matrix.gain }}, 200]"
Expressions are evaluated by a whitelist AST walker: literals, matrix
lookups, + - * / // % ** and abs/int/float/round/min/max only. A string
that is exactly one expression keeps the result's type; embedded results
are formatted back (integral floats as ints). Unlike ${matrix.x}
interpolation, evaluation errors raise instead of passing silently.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KhjddEErh4oYzHY6CnrRrd
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.
Summary
This PR adds support for arithmetic expressions in batch matrix parameters, allowing users to define correlated parameters without maintaining parallel lists of pre-computed values. Expressions use the
${{ <expression> }}syntax and can reference matrix variables.Key Changes
New
safe_eval()function: Safely evaluates arithmetic expressions using AST parsing, supporting:+ - * / // % **abs,int,float,round,min,maxmatrix.gain,matrix['key']New
evaluate_expressions()function: Recursively processes${{ }}blocks in configuration values:${{ 168 * 2 }}returns336as int)"[${{ 168 * 2 }}]"returns"[336]")252.0becomes"252")ValueErroron evaluation errors (intentional failures, not silent)Integration with batch matrix processing: Expressions are evaluated after all matrix parameters are collected, allowing cross-parameter references
Comprehensive test coverage: Added tests for expression evaluation, type preservation, error handling, and integration with batch matrix expansion
Notable Implementation Details
astmodule for safe parsing instead ofeval()ast.Num,ast.Str, andast.Indexnodes)${matrix.param}interpolation, unknown variables in${{ }}expressions raise errorshttps://claude.ai/code/session_01KhjddEErh4oYzHY6CnrRrd