# For a single neuron the number of synapses activated at the moment

**URL:** https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540
**Category:** Support
**Tags:** synapses
**Created:** [29 November 2021 18:54 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540 "2021-11-29T18:54:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zeroinput](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zeroinput](https://brian.discourse.group/u/zeroinput)
#### Post date: [29 November 2021 18:54 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/1 "2021-11-29T18:54:33Z")

</div>

# Description of problem

Hi, I have a question, if I write N\_incoming in on\_pre, it means the number of synapses received by this neuron, right? If N\_incoming=10, but there are only 3 synapses firing at this moment, and I want to call the number of synapses firing at this moment in on\_pre, is there a function I can use?

That is, I want to determine the input of different magnitudes depending on the total number of synaptic activations that a single neuron receives

---

<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: [30 November 2021 09:45 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/2 "2021-11-30T09:45:37Z")

</div>

Hi. Yes, `N_incoming` is the total number of synapses (which can be useful to normalize inputs in networks with random connectivity), not the number of received spikes during that time step. Each synapse executes the `on_pre` statement independently, so there is no variable that captures the total number of spikes arriving at a neuron during a time step. From a modelling standpoint, I find this also a bit problematic since it depends on the simulation time step – if you are simulating with a finer time step, you will get fewer spikes in each time step.  
But in general it is possible to have and use this information, but you need to put several pieces together. Here’s a rough idea how this could work:

```auto
source = NeuronGroup(...)
target = NeuronGroup(..., '''
                          ...
                          incoming_spikes : integer
                          ''', ...)
synapses = Synapses(source, target, ...,
                    on_pre='incoming_spikes_post += 1')      
target.run_regularly('''v += int(incoming_spikes > 3)*0.1
                        incoming_spikes = 0''', when='after_synapses')

```

The idea is that the only action of the synapses is to update a “number of spikes during this time step” counter in the target cell. The target cell itself then acts based on this information, in the above example it increases its `v` variable by 0.1 if more than 3 spikes arrived, and does nothing otherwise. You’d replace this by whatever you want to do with this information. Finally, we need to make sure that the counter is reset to 0 for the next time step.

Hope that helps, best  
Marcel

---

<div class="post-metadata">

### Author: ![zeroinput](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zeroinput](https://brian.discourse.group/u/zeroinput)
#### Post date: [30 November 2021 20:01 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/3 "2021-11-30T20:01:51Z")

</div>

Hi Marcel, thanks for your reply.

It looks like a great way to do it. But I have one more question, what does **after\_synapses** mean here, it reports an error when running to this code:

```auto
incoming_spikes = 0''', when='after_synapses')

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: [30 November 2021 20:51 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/4 "2021-11-30T20:51:53Z")

</div>

`after_synapses` means that this `run_regularly` operation should be run after the synaptic updates, i.e. after the `on_pre` statements. See [Running a simulation — Brian 2 2.5.0.1 documentation](https://brian2.readthedocs.io/en/stable/user/running.html#scheduling) for more details.  
What error do you get, can you post the full error message?

---

<div class="post-metadata">

### Author: ![zeroinput](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zeroinput](https://brian.discourse.group/u/zeroinput)
#### Post date: [1 December 2021 17:52 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/5 "2021-12-01T17:52:49Z")

</div>

```auto
WARNING Cannot use Cython, a test compilation failed: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ (DistutilsPlatformError) [brian2.codegen.runtime.cython_rt.cython_rt.failed_compile_test]
INFO Cannot use compiled code, falling back to the numpy code generation target. Note that this will likely be slower than using compiled code. Set the code generation to numpy manually to avoid this message:
prefs.codegen.target = "numpy" [brian2.devices.device.codegen_fallback]
ERROR Brian 2 encountered an unexpected error. If you think this is a bug in Brian 2, please report this issue either to the discourse forum at <http://brian.discourse.group/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: C:\Users\ADMINI~1\AppData\Local\Temp\brian_debug_5kbgoqf9.log Additionally, you can also include a copy of the script that was run, available at: C:\Users\ADMINI~1\AppData\Local\Temp\brian_script_4o_qvvt_.py You can also include a copy of the redirected std stream outputs, available at C:\Users\ADMINI~1\AppData\Local\Temp\brian_stdout_tn2txiom.log and C:\Users\ADMINI~1\AppData\Local\Temp\brian_stderr_tt_5xzh5.log Thanks! [brian2]
Traceback (most recent call last):
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\core\network.py", line 897, in before_run
    obj.before_run(run_namespace)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\groups\group.py", line 1143, in before_run
    self.create_code_objects(run_namespace)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\groups\group.py", line 1136, in create_code_objects
    code_object = self.create_default_code_object(run_namespace)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\groups\group.py", line 1129, in create_default_code_object
    codeobj_class=self.codeobj_class
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\codegen\codeobject.py", line 396, in create_runner_codeobj
    check_units_statements(c, variables)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\equations\unitcheck.py", line 99, in check_units_statements
    expr_unit = parse_expression_dimensions(expr, variables)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\parsing\expressions.py", line 375, in parse_expression_dimensions
    raise DimensionMismatchError(error_msg)
brian2.units.fundamentalunits.DimensionMismatchError: Expression "g_ampa + int(incoming_spikes > 3) * 0.1" uses inconsistent units ("g_ampa" has unit S; "int(incoming_spikes > 3) * 0.1" has unit 1)

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

Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/brian2_demo.py", line 91, in <module>
    run(1*second)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\units\fundamentalunits.py", line 2434, in new_f
    result = f(*args, **kwds)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\core\magic.py", line 374, in run
    namespace=namespace, profile=profile, level=2+level)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\core\magic.py", line 232, in run
    namespace=namespace, profile=profile, level=level+1)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\core\base.py", line 278, in device_override_decorated_function
    return func(*args, **kwds)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\units\fundamentalunits.py", line 2434, in new_f
    result = f(*args, **kwds)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\core\network.py", line 1008, in run
    self.before_run(namespace)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\core\base.py", line 278, in device_override_decorated_function
    return func(*args, **kwds)
  File "F:\Anaconda3\envs\mybrain\lib\site-packages\brian2\core\network.py", line 899, in before_run
    raise BrianObjectException("An error occurred when preparing an object.", obj) from ex
brian2.core.base.BrianObjectException: Error encountered with object named "synapses_run_regularly".
Object was created here (most recent call only, full details in debug log):
  File "C:/Users/Administrator/Desktop/brian2_demo.py", line 53, in <module>
    incoming_spikes = 0''', when='after_synapses')

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

Process finished with exit code 1

```

---

<div class="post-metadata">

### Author: ![zeroinput](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zeroinput](https://brian.discourse.group/u/zeroinput)
#### Post date: [1 December 2021 17:58 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/6 "2021-12-01T17:58:27Z")

</div>

It looks like the problem is with the _run()_

---

<div class="post-metadata">

### Author: ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)
#### Post date: [1 December 2021 19:59 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/7 "2021-12-01T19:59:56Z")

</div>

to me it looks like

```python
Expression "g_ampa + int(incoming_spikes > 3) * 0.1" uses inconsistent units ("g_ampa" has unit S; "int(incoming_spikes > 3) * 0.1" has unit 1)

```

might be the root cause.changing to

```auto
g_ampa + int(incoming_spikes > 3) * 0.1 * S

```

might fix it, but make sure that corresponds to the scale you want to use

---

<div class="post-metadata">

### Author: ![zeroinput](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zeroinput](https://brian.discourse.group/u/zeroinput)
#### Post date: [2 December 2021 08:15 UTC](https://brian.discourse.group/t/for-a-single-neuron-the-number-of-synapses-activated-at-the-moment/540/8 "2021-12-02T08:15:24Z")

</div>

yea 😂, I made a stupid mistake… Thanks for all your help! 😃
