Expected string or bytes-like object when calling Network(....).run(...)

Hey there! So my code is the following:

from brian2 import *

if __name__ == '__main__':

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

    # Create a TimedArray
    values = 1 * mV * ones(len(times))
    v_ext_array = 1 * mV * np.repeat([values], len(connectivity_matrix), axis=0)

    # Correct TimedArray setup
    v_ext = TimedArray(v_ext_array, dt=_step)
    tau = 5 * ms
    g = 0 * mV
    v_0 = -52 * mV
    v = v_0
    t_mbr = 20 * ms
    eqs = """
    dv/dt = (v_0 - v + g + v_ext(t, i)) / t_mbr : volt (unless refractory)
    dg/dt = -g / tau               : volt (unless refractory) 
    """
    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",
        refractory=True,
        name="default_neurons",
    )

    ################################################
    # 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)

    net = Network(neu, syn, spike_monitor)

    # run simulation
    print("Started Modeling Process")
    net.run(duration=1_000 * ms, report="stdout", report_period=100 * second)

Iā€™m running into an issue during the net.run(...) step - which makes me think that this is a ā€œrun-timeā€ issue when it tries to run the simulation? Iā€™m not really sure how to get around the issue thoughā€¦ I did some searching and I found Issue with running the model but it doesnā€™t really look relevant, other than that we are facing the same bug

Hi @itq . And yet again, this error message isnā€™t greatā€¦ The error isnā€™t directly related to the run call itself. When you call run, Brian prepares the network, generated the code, etc., so this is often the place where you get errors when something went wrong. This is why Brian gives an additional hint at the end of the error message, in your case:

brian2.core.base.BrianObjectException: Error encountered with object named 'default_neurons_stateupdater'.
Object was created here (most recent call only, full details in debug log):
  File '/home/mstimberg/scratch/run_error.py', line 26, in <module>
    neu = NeuronGroup(  # create neurons

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

This tells you that the actual problem is in the definition of the NeuronGroup. Further up, you get the actual error which states: ā€œTypeError: expected string or bytes-like object, got ā€˜boolā€™ā€. So at some point, Brian expected to find a string but received a boolean value. From your NeuronGroup definition you can see that this is most likely the refractory=True argument, since this is the only boolean you hand over. And indeed, you cannot give a True/False value as the argument for the refractoriness, it needs to be either:

  • a time, that states the refractory time after each spike
  • a string that evaluates to such a time (in this case, it could be different for each cell, random, etc.)
  • a string that evaluates to a boolean value, stating whether the cell should stay refractory (this is mostly used in cells that do not have a reset mechanism to prevent that spikes are counted for every time step where the membrane potential is above the threshold).

See Refractoriness ā€” Brian 2 2.6.0 documentation for more details

I am not quite sure what you wanted to say with refractory=True. Technically, you can make this code run by writing the value as a string instead, i.e. refractory="True". But this would mean that the neuron is always refractory and never spikes, which is quite likely not what you want :wink: Most likely you want to use something like refactory=2*ms or refractory="2*ms" (both are equivalent)?

Ugh, Iā€™m so sorry thank you! In hindsight, I have no idea why I made that change. In any case, everything works now, so thank you! I really appreciate all your help and patience.

1 Like