Duplicate variable error in Synapses with unique names and cache cleared (Brian2.9.0 / Python 3.13)

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 / (6
second) : 1
dDA_syn/dt = -DA_syn / (2second) : 1
dNE_syn/dt = -NE_syn / (1
second) : 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:

  1. Renamed all variables to completely unique names not used anywhere else
  2. Cleared all Brian2 caches with rm -rf ~/.brian* and rm -rf /tmp/brian_*
  3. Used start_scope() at beginning of every script
  4. Tested in completely fresh terminal sessions
  5. 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!

Hi @Peace . When you define a variable via a differential equation, it becomes a state variable that is stored for each neuron/synapse, and updated according to the equation at every time step (or for each event in case of event-driven variables in synapses). When you define a parameter, it also becomes a state variable, but one that isn’t updated. I don’t quite see why you’d want a variable that is already defined via a differential equation to also be a parameter. My suggestion would be to remove the duplicated parameter lines from the equation to no longer get the error.

1 Like

Hi Marcel,

Thank you so much for the patient explanation! You’re absolutely right, and I really appreciate you taking the time to clarify this.

My Real Background & Why I Made This Error:

I need to be completely honest - I have zero formal background in neuroscience or neural simulators. I’m just someone who thinks about problems like “what if we could build artificial consciousness?” and then… tries to figure out how to actually do it.

This explains my error perfectly: Since I’ve never used Brian2 or similar tools before, I was approaching it like building something from scratch in a general programming sense. In my mind:

  • “I need an emotional_state variable for my consciousness model”
  • “I also need equations to make it change over time”
  • “So I need to declare the variable AND write the equation - two separate things”

I was thinking like: “Let me explicitly declare every component I need, then define how they work.” Because that’s how you’d approach building anything from scratch when you don’t know the system.

But as you explained, Brian2 is way smarter than that - the differential equation demotional_state/dt automatically creates the emotional_state variable. I was essentially trying to build the same component twice because I didn’t understand how elegant Brian2’s design actually is.

What I’m Attempting (And Why):

I’m trying to build what I think might be artificial consciousness:

  • Emotional layer: demotional_state/dt = ...
  • Predictive layer: dprediction_error/dt = ...
  • Executive layer: dnarrative_coherence/dt = ...

My approach is basically: “Why shouldn’t this be possible? Let me try to make 1000 neurons behave like consciousness.” No formal training telling me what’s “impossible” - just curiosity and determination.

The Learning Moment:

Now I understand Brian2’s philosophy: “Write the equation for the behavior you want. Brian2 handles creating and managing the variables.”

That’s actually brilliant design - no need for someone like me to overthink the “component declaration” part.

Thanks for teaching me something fundamental! My 1000-neuron consciousness experiment is now running without errors :brain:

Best regards,
Peace.

1 Like