Skip to content

Add arithmetic expression support in batch matrix parameters - #107

Open
arthur-flam wants to merge 1 commit into
masterfrom
claude/yaml-arithmetic-parameters-c0vjk2
Open

Add arithmetic expression support in batch matrix parameters#107
arthur-flam wants to merge 1 commit into
masterfrom
claude/yaml-arithmetic-parameters-c0vjk2

Conversation

@arthur-flam

Copy link
Copy Markdown
Member

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:

    • Arithmetic operators: + - * / // % **
    • Function calls: abs, int, float, round, min, max
    • Variable and attribute access: matrix.gain, matrix['key']
    • Prevents execution of arbitrary code by whitelisting allowed operations
  • New evaluate_expressions() function: Recursively processes ${{ }} blocks in configuration values:

    • Preserves type when expression is the entire value (e.g., ${{ 168 * 2 }} returns 336 as int)
    • Formats results back into strings when embedded (e.g., "[${{ 168 * 2 }}]" returns "[336]")
    • Formats integral floats as integers (e.g., 252.0 becomes "252")
    • Raises ValueError on 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

  • Uses Python's ast module for safe parsing instead of eval()
  • Supports Python 3.7+ compatibility (handles deprecated ast.Num, ast.Str, and ast.Index nodes)
  • Expressions that fail to evaluate stop batch processing with clear error messages, preventing silent failures
  • Unlike lenient ${matrix.param} interpolation, unknown variables in ${{ }} expressions raise errors

https://claude.ai/code/session_01KhjddEErh4oYzHY6CnrRrd

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants