Skip to content

Add prefix() and postfix() methods to Equations class#1789

Closed
nishantraghuvanshi wants to merge 1 commit into
brian-team:masterfrom
nishantraghuvanshi:feat/equations-prefix-postfix
Closed

Add prefix() and postfix() methods to Equations class#1789
nishantraghuvanshi wants to merge 1 commit into
brian-team:masterfrom
nishantraghuvanshi:feat/equations-prefix-postfix

Conversation

@nishantraghuvanshi
Copy link
Copy Markdown

This PR fixes issue #1050, adding prefix() and postfix() methods to the Equations class.
These methods allow users to rename all variable names in a set of equations, which is useful for avoiding name conflicts in multi-population models.

Example

eqs = Equations('''
    dv/dt = -v/tau : volt
    I = g*E : amp
''')

exc_eqs = eqs.prefix('exc_')  # Variables become: exc_v, exc_I
inh_eqs = eqs.postfix('_inh')  # Variables become: v_inh, I_inh

Key Design Decisions

  • Recursive behavior: Methods apply to current variable names, so eqs.prefix('a_').prefix('b_') produces b_a_v (nested)
  • Comprehensive validation: Full Python identifier validation with keyword checks
  • Built-in protection: Variables like t, dt, xi, i, N are never renamed
  • Scope: Only explicitly defined variables are renamed (diff eqs, parameters, subexpressions), not external references

Please suggest any changes that I need to make in the above implementation.
@thesamovar @mstimberg

  - Implement prefix() method to prepend string to all variable names
  - Implement postfix() method to append string to all variable names
  - Comprehensive validation (Python identifier, keyword checks)
  - Protection of built-in variables (t, dt, xi, i, N, etc.)
  - Recursive behavior (nested calls)
  - 15 comprehensive tests covering all functionality
  - All tests passing (30/30)
  - Uses triple double quotes for multi-line strings (Brian2 convention)

  Fixes brian-team#1050
@mstimberg
Copy link
Copy Markdown
Member

Hi @nishantraghuvanshi, thank you for the PR. This issue is a good way of getting familiar with Brian's code and our contribution process, but please be aware that we most likely won't merge it. We should have probably closed #1050, since our discussions since then have converged onto a more general solution (see #1244). So, I'd be happy to continue to work with you on this PR, but I also understand if you want to focus on things that will actually make it into Brian's codebase.

@nishantraghuvanshi
Copy link
Copy Markdown
Author

Thanks for the transparency, @mstimberg
I really appreciate you pointing me toward #1244.

I've started reading through the issue thread (it's quite long, so I'm still digesting everything), but I can already see the templating approach with {...} syntax is much more flexible than the simple prefix/postfix methods.

A few questions as I'm learning about this:

  1. From what I've read so far, it looks like the template approach can handle the prefix/suffix use case (by doing things like {name}_{suffix}). Is that right, or am I understanding it incorrectly?

  2. How far along is Structured equations with equation templates #1244? Is there a specific area where I can help?

I'm definitely interested in contributing to something that's going into Brian2. Whether that means pivoting to help with #1244 or continuing with this PR as a learning exercise to better understand the codebase, I'm excited to contribute wherever makes the most sense. Would love your guidance on the best path forward.

@mstimberg
Copy link
Copy Markdown
Member

Hi @nishantraghuvanshi. I will close this PR, but if you are still interested in contributing to #1244, please make a comment over in that PR. I haven't looked into that branch in quite some time, so I'm not exactly sure how much work it still needs (also, for now, it does not apply cleanly to master).

@mstimberg mstimberg closed this May 19, 2026
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