I’m experiencing persistent “Duplicate definition of variable ‘elig_syn’” errors in Brian2 Synapses, even after renaming all variables to unique names, clearing all caches, and restarting in fresh terminals.
System Information:
- Brian2 version: 2.9.0
- Python version: 3.13
- OS: Ubuntu/Linux
Problem Description:
The error occurs when creating Synapses objects, claiming duplicate variable definitions even when each variable appears only once in the code. This persists despite clearing all Brian2 caches and using completely unique variable names.
Minimal Working Example (MWE):
import brian2
from brian2 import *
start_scope()
N = 100
syn_eqs = ‘’’
w_syn : 1
elig_syn : 1
instr_syn : 1
DA_syn : 1
NE_syn : 1
delig_syn/dt = -elig_syn / (3second) : 1 (event-driven)
dinstr_syn/dt = -instr_syn / (6second) : 1
dDA_syn/dt = -DA_syn / (2second) : 1
dNE_syn/dt = -NE_syn / (1second) : 1
‘’’
G = NeuronGroup(N, ‘dv/dt = -v/(10*ms) : 1’, threshold=‘v>0.8’, reset=‘v=0’, method=‘euler’)
S_btsp = Synapses(G, G, model=syn_eqs,
on_pre=‘elig_syn += 1’,
on_post=‘instr_syn = 1 if rand() < 0.2 else 0’
)
S_btsp.connect(p=0.1)
Error Output:
Traceback (most recent call last):
File “code/visceral_full_biological.py”, line 54, in module
S_btsp = Synapses(G, G, model=syn_eqs,
brian2.equations.equations.EquationError: Duplicate definition of variable ‘elig_syn’
Troubleshooting Steps Attempted:
- Renamed all variables to completely unique names not used anywhere else
- Cleared all Brian2 caches with rm -rf ~/.brian* and rm -rf /tmp/brian_*
- Used start_scope() at beginning of every script
- Tested in completely fresh terminal sessions
- Verified no actual duplicate variable declarations in the code
Request:
Is this a known issue with Brian2 2.9.0 and Python 3.13? The error seems to be a parser or caching bug rather than actual duplicate variables. Any workarounds or fixes would be greatly appreciated.
I can provide the debug files if helpful.
UPDATE - SOLUTION FOUND:
After extensive debugging, we successfully identified and fixed this issue!
Root Cause:
Brian2’s equation parser in equations.py was overly strict - it rejected legitimate parameter + differential equation combinations.
Solution Implemented:
Modified the duplicate detection logic (equations.py lines 424-427) to allow:
- Parameter declaration:
elig_syn : 1 - Differential equation:
delig_syn/dt = ... : 1
The fix allows these valid combinations while still catching actual duplicates.
Fix Code:
if identifier in equations:
existing_type = equations[identifier].type
new_type = equation.type
Allow combining parameter with differential equation
if not ((existing_type == PARAMETER and new_type == DIFFERENTIAL_EQUATION) or
(existing_type == DIFFERENTIAL_EQUATION and new_type == PARAMETER)):
raise EquationError(f"Duplicate definition of variable ‘{identifier}’")
equations[identifier] = equation
Testing Results:
• Tested with Brian2 2.9.0 - fix works perfectly
• Tested with Python 3.11 - no issues
• Issue affects multiple Brian2 versions AND multiple Python versions
• Systematic testing confirms this was a deeper parsing bug in Brian2
• Status: FIXED and working!
Would the Brian2 team consider integrating this enhancement into the official codebase?
I can provide debug files if helpful.
Thanks for any assistance!