Skip to content

Comments

Implement substitution functionality for Statements class#1768

Open
nishantraghuvanshi wants to merge 5 commits intobrian-team:masterfrom
nishantraghuvanshi:feat/statements-substitution
Open

Implement substitution functionality for Statements class#1768
nishantraghuvanshi wants to merge 5 commits intobrian-team:masterfrom
nishantraghuvanshi:feat/statements-substitution

Conversation

@nishantraghuvanshi
Copy link

  • Add init method with **substitutions parameter to Statements class
  • Implement _substitute static method for identifier replacement
  • Support both name substitution (string -> string) and value substitution (identifier -> value)
  • Use regex with word boundaries to respect identifier boundaries
  • Add comprehensive test suite with 22 tests covering:
    • Basic value and name substitution
    • Multiple simultaneous substitutions
    • Word boundary handling
    • Brian2 units support
    • Multi-line statements
    • Integration with Synapses and NeuronGroup
    • Backward compatibility
  • All existing tests pass (no regression)

This enables users to create reusable statement templates for on_pre, on_post, threshold, and reset parameters, similar to how Equations supports substitution for differential equations.

Fixes #1219

- Add __init__ method with **substitutions parameter to Statements class
- Implement _substitute static method for identifier replacement
- Support both name substitution (string -> string) and value substitution (identifier -> value)
- Use regex with word boundaries to respect identifier boundaries
- Add comprehensive test suite with 22 tests covering:
  - Basic value and name substitution
  - Multiple simultaneous substitutions
  - Word boundary handling
  - Brian2 units support
  - Multi-line statements
  - Integration with Synapses and NeuronGroup
  - Backward compatibility
- All existing tests pass (no regression)

This enables users to create reusable statement templates for on_pre,
on_post, threshold, and reset parameters, similar to how Equations
supports substitution for differential equations.

Fixes brian-team#1219
@mstimberg
Copy link
Member

@nishantraghuvanshi Thank you for the PR! I did not yet have time to look at it in detail, but I will so next week.

Change single quotes to double quotes in all test strings
to comply with ruff formatting checks.
  Apply ruff formatting changes:
  - Use double quotes consistently
  - Fix line wrapping for long parameter lists
  - Fix spacing in multiline statements
  - Add spaces around multiplication operators (e.g., 1*mV → 1 * mV)
  - Reformat parametrize decorators with proper indentation

  Fixes CI pre-commit hook failures
  Fix repr test to match actual output format.
  The repr() returns double quotes around single-quoted string.
  The implementation wraps value substitutions in parentheses for clarity,
  e.g., Statements('g += k*w', k=0.3) produces 'g += (0.3)*w'.
  Update docstring examples to match actual behavior.
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.

Handling of += expressions by Equations()

2 participants