# About the Custom Events

**URL:** <https://brian.discourse.group/t/about-the-custom-events/574>\
**Category:** Support\
**Created:** [6 January 2022 10:30 UTC](https://brian.discourse.group/t/about-the-custom-events/574 "2022-01-06T10:30:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SEU\_diff](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/seu_diff/32/245_2.png) [@SEU\_diff](https://brian.discourse.group/u/SEU_diff)\
**Post date:** [6 January 2022 10:30 UTC](https://brian.discourse.group/t/about-the-custom-events/574/1 "2022-01-06T10:30:34Z")

</div>

# Description of problem

I’m doing some work with the Tempotron rule.

 ![image](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/94eef4fd38481885f32abf62998e85858102aafc.png)

And I use the Custom events to do this.  
But there’re Errors occured.

# Minimal code to reproduce problem

```auto
# parameters
v_th = 1 # -54 * mV
v_reset = 0 # -65 * mV
tau_m = 350 * ms
N_spike = 2*34*34
N_Tempotron = 10
duration = 350 * ms
t_refractory = 5 * ms
defaultclock.dt = 0.001*ms
lr = 0.001

# eqs
lif_model = '''
            dv/dt = (v_reset - v)/tau_m : 1
            dtrace/dt = -trace/tau_m : 1
            fired_status : boolean
            label : 1 
            '''
T_pre_eq = '''
            v += w
            trace += 1
            '''
T_post_eq = '''
            fired_status = True
            '''

Tempotron_ftf_eq = '''
                w += lr * trace
                '''

Tempotron_fw_eq = '''
                w -= lr * trace
                trace = 0
                fired_status = False
                '''

# load data
train_index, train_timestamp, test_index, test_timestamp = load_data()
train_label, test_label = load_label()

# input group
input_group = SpikeGeneratorGroup(N_spike, train_index[0],train_timestamp[0]*ms, sorted=True)

# Tempotron Group
Tempotron_group = NeuronGroup(N_Tempotron, model=lif_model,threshold='v > v_th',reset='v=v_reset',
                              refractory=t_refractory, method='exact',
                              events={'fail_to_fire': '(not fired_status) and (label == i) and (t%duration==0) ',
                                      'fire_wrongly': 'fired_status and (label != i)'})
Tempotron_group.v = v_reset
Tempotron_group.fired_status = False

# synapse
i2T_synapes = Synapses(input_group, Tempotron_group, model='w : 1', on_pre=T_pre_eq,
                       on_post={'post': T_post_eq,
                                'Tempotron_ftf': Tempotron_ftf_eq,
                                'Tempotron_fw': Tempotron_fw_eq},
                       on_event={'Tempotron_ftf': 'fail_to_fire',
                                 'Tempotron_fw': 'fire_wrongly'})
i2T_synapes.connect()
i2T_synapes.w = 'rand() * 0.001'

# monitor

T_mon = SpikeMonitor(Tempotron_group)

# run
for item in tqdm(range(len(train_index))):

    Tempotron_group.label = train_label[item]

    if item == 0:
        run(0*ms)
        run(duration)
    else:
        input_group.set_spikes(indices=train_index[item], times=train_timestamp[item]*ms+item*duration, sorted=True)
        run(duration)

    Tempotron_group.fired_status = False

```

# What you have aready tried

I’ve read the Custom events document, but didn’t found the reason. Is there anything I miss?

# Full traceback of error (if relevant)

Traceback (most recent call last):  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\core\network.py”, line 892, in before\_run  
obj.before\_run(run\_namespace)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\groups\group.py”, line 1135, in before\_run  
self.create\_code\_objects(run\_namespace)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\groups\group.py”, line 1128, in create\_code\_objects  
code\_object = self.create\_default\_code\_object(run\_namespace)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\groups\group.py”, line 1100, in create\_default\_code\_object  
self.update\_abstract\_code(run\_namespace=run\_namespace)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\groups\neurongroup.py”, line 249, in update\_abstract\_code  
stateupdate\_output = StateUpdateMethod.apply\_stateupdater(self.group.equations,  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\utils\caching.py”, line 101, in cached\_func  
func.\_cache[cache\_key] = func(\*args, \*\*kwds)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\stateupdaters\base.py”, line 222, in apply\_stateupdater  
code = stateupdater(equations, variables, method\_options)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\stateupdaters\exact.py”, line 226, in **call**  
rhs = rhs.subs(\_S[row\_idx, 0], varname)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\basic.py”, line 948, in subs  
rv = rv.\_subs(old, new, \*\*kwargs)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\cache.py”, line 72, in wrapper  
retval = cfunc(\*args, \*\*kwargs)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\basic.py”, line 1062, in \_subs  
rv = fallback(self, old, new)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\basic.py”, line 1039, in fallback  
rv = self.func(\*args)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\cache.py”, line 72, in wrapper  
retval = cfunc(\*args, \*\*kwargs)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\operations.py”, line 53, in **new**  
args = list(map(_sympify_, args))  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\sympify.py”, line 513, in \_sympify  
return sympify(a, strict=True)  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\sympy\core\sympify.py”, line 434, in sympify  
raise SympifyError(a)  
sympy.core.sympify.SympifyError: SympifyError: \<function trace at 0x0000029538D59430\>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):  
File “C:\ProgramData\Anaconda3\envs\python38\lib\site-packages\brian2\core\network.py”, line 894, in before\_run  
raise BrianObjectException(“An error occurred when preparing an object.”, obj) from ex  
brian2.core.base.BrianObjectException: Error encountered with object named ‘neurongroup\_stateupdater’.  
Object was created here (most recent call only, full details in debug log):  
File ‘C:/Dev/Python/LogicFunction/Application/clf\_N-MNIST/clf\_NMNIST\_test.py’, line 68, in   
Tempotron\_group = NeuronGroup(N\_Tempotron, model=lif\_model,threshold=‘v \> v\_th’,reset=‘v=v\_reset’,

An error occurred when preparing an object. (See above for original error message and traceback.)

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [6 January 2022 16:11 UTC](https://brian.discourse.group/t/about-the-custom-events/574/2 "2022-01-06T16:11:22Z")

</div>

Hi @SEU_diff . It seems you came across a bug… We are using [sympy](https://sympy.org) for symbolic processing of the equations, and this fails here because it confuses your variable `trace` with a function of the same name (the trace of a matrix, I guess). You could work around this specific problem by renaming your `trace` variable to something else.

---

<div class="post-metadata">

**Author:** ![SEU\_diff](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/seu_diff/32/245_2.png) [@SEU\_diff](https://brian.discourse.group/u/SEU_diff)\
**Post date:** [10 January 2022 16:53 UTC](https://brian.discourse.group/t/about-the-custom-events/574/3 "2022-01-10T16:53:10Z")

</div>

Thank you very much! I fix it by changing the variable name.😊
