# Current clamping my given network

**URL:** <https://brian.discourse.group/t/current-clamping-my-given-network/1198>\
**Category:** Support\
**Tags:** synapses, equations\
**Created:** [16 April 2024 23:20 UTC](https://brian.discourse.group/t/current-clamping-my-given-network/1198 "2024-04-16T23:20:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![itq](https://avatars.discourse-cdn.com/v4/letter/i/bc8723/32.png) [@itq](https://brian.discourse.group/u/itq)\
**Post date:** [16 April 2024 23:20 UTC](https://brian.discourse.group/t/current-clamping-my-given-network/1198/1 "2024-04-16T23:20:43Z")

</div>

Hey there! I’m hoping I could get some help on the following in terms of 1) debugging and 2) understanding.

I have the following code and it errors on the following line: `neu.v_ext = v_ext` with `Quantities can only be created from numerical data.`. `v_ext` is defined as

```python
_start = 0
_stop = 1_000 * ms
_step = 0.2 * ms
times = np.linspace(_start, _stop, int((_stop - _start) / _step + 1))

values = external_input ones(len(times)) # Initialize with zeros
# Create a TimedArray
v_ext = TimedArray(values, dt=_step)

```

which, I think is numerical? Unless it meansthat it wants a scalar value(?).

My end goal is a follow up from [Modeling an external input voltage to the circuit? - #2 by mstimberg](https://brian.discourse.group/t/modeling-an-external-input-voltage-to-the-circuit/1197/2)

The following is what I have so far: I create my “current injections”, define my circuit, and apply my current to all neurons in the circuit. Obviously, my understanding is breaking down somewhere

```python

external_input = 1 * mV
print(f"INFO: Applying {} at every time-step")

_start = 0
_stop = 1_000 * ms
_step = 0.2 * ms
times = np.linspace(_start, _stop, int((_stop - _start) / _step + 1))

values = external_input ones(len(times)) # Initialize with zeros
# Create a TimedArray
v_ext = TimedArray(values, dt=_step)

eqs = """
dv/dt = (v_0 - v + g + v_ext) / t_mbr : volt (unless refractory)
dg/dt = -g / tau : volt (unless refractory) 
rfc : second
v_ext : volt (constant)
"""
neu = NeuronGroup( # create neurons
    N=len(connectivity_matrix),
    model=eqs,
    method="linear",
    threshold= "v > v_th",
    reset='v = v_rst; w = 0; g = 0 * mV',
    name="default_neurons",
)

neu.v_ext = v_ext
neu.v = -52 * mV
neu.g = 0
neu.rfc = 0.275 * mV

################################################
# Then, populate the synapses
################################################

sources, targets = connectivity_matrix.nonzero()
syn = Synapses(
    neu, neu, "w : volt", on_pre="g += w", delay=-1.8 * ms, name="default_synapses"
)
syn.connect(i=sources, j=targets)

weight_per_synapse = 0.275 * mV

syn.w[:] = connectivity_matrix[(sources, targets)] * weight_per_synapse
spike_monitor = SpikeMonitor(neu

```

Some followup questions:

what if I wanted to apply this injected current only to a subset of the neurons? Would I manually access them by doing something like

```auto
neu[i].v_ext = some_time_array

```

?

---

<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:** [17 April 2024 09:19 UTC](https://brian.discourse.group/t/current-clamping-my-given-network/1198/2 "2024-04-17T09:19:00Z")

</div>

Hi @itq. Yet again, this error message could be better, but this is indeed not the way you’d set a time-varying input. When you set `neu.v_ext = ….`, you need to provide either a scalar value, one value for each neuron, or a string that evaluates to either (e.g. you can use `neu.v_ext = 'rand()*5*mV'` to set a random value between 0 and 5mV for each neuron). In your case, you are giving it a `TimedArray` which has a single value for each time step. From Brian’s point of view, such arrays are \*functions, i.e. you have to refer to them in your equations like this:

```python
v_ext = TimedArray(values, dt=_step)
eqs = """
dv/dt = (v_0 - v + g + v_ext(t)) / t_mbr : volt (unless refractory)
dg/dt = -g / tau : volt (unless refractory) 
rfc : second
"""

```

(This actually makes them quite flexible, e.g. you could have a different delay `d : second` for each neuron and then use `v_ext(t - d)` in the equations).

> [@itq](#):
>
> what if I wanted to apply this injected current only to a subset of the neurons? Would I manually access them by doing something like
> 
> ```auto
> neu[i].v_ext = some_time_array
> 
> ```

There are several ways to do something like this. If it is only about switching the input on/off, you can do it like this:

```auto
eqs = """
dv/dt = (v_0 - v + g + int(receives_input)*v_ext(t)) / t_mbr : volt (unless refractory)
dg/dt = -g / tau : volt (unless refractory) 
rfc : second
receives_input : boolean (constant)
"""
neu = NeuronGroup(...)
# Switch input on for a subset of neurons (it is switched off by default):
neu[...].receives_input = True

```

The `int(receives_input)` in the equations converts `True`/`False` into `1` or `0`.

As a more general solution, you can also use a `TimedArray` do give different input to different neurons, by providing it a 2D array of values. It then becomes a function of two variables, i.e. of an input index and the time. The documentation has an example: [Input stimuli — Brian 2 2.6.0 documentation](https://brian2.readthedocs.io/en/stable/user/input.html#timed-arrays)
