# Current clamping my given network

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

``````_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

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

``````
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

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

?

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:

``````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).

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:

``````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
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