HH model error on PC

Description of problem

My code runs okay on my laptop; however, it runs with error on my lab PC.

Minimal code to reproduce problem

from brian2 import *

area = 20000*umetre**2
C = 1*ufarad*cm**-2 * area
g_leak_e = 2.23*nsiemens
g_leak_fbi = 1.12*nsiemens
g_leak_ffi = 3.86*nsiemens
g_bar_Na_e = 59.*msiemens*cm**-2 * area
g_bar_Na_fbi = 28.*msiemens*cm**-2 * area
g_bar_Na_ffi = 58.*msiemens*cm**-2 * area
g_bar_Kd_e = 3.1*msiemens*cm**-2 * area
g_bar_Kd_fbi = 6.*msiemens*cm**-2 * area
g_bar_Kd_ffi = 6.6*msiemens*cm**-2 * area
g_bar_M_e = .16*msiemens*cm**-2 * area
g_bar_M_fbi = .09*msiemens*cm**-2 * area
g_bar_M_ffi = .05*msiemens*cm**-2 * area
tao_max_e = 686.4*ms
tao_max_fbi = 2646.*ms
tao_max_ffi = 1056.*ms
E_Na = 50.*mV
E_K = -90.*mV
E_Ca = 120.*mV
E_leak_e = -56.2*mV
E_leak_ffi = -70.4*mV
E_leak_fbi = -70.4*mV
V_T_e = -58.67*mV
V_T_fbi = -66.54*mV
V_T_ffi = -61.47*mV
V_x = 2*mV

stim_out1 = []
for i1 in range(200):
    stim_out1.append(1-int(isin(i1, [0, 1, 2, 3])))
stim_out1 = np.array(stim_out1)

@check_units(i1=1, result=1)
def is_stim(i1):
    return stim_out1

runtime = 5*second
t_stim = linspace(0, runtime/second, int(runtime/defaultclock.dt))
stimulus = TimedArray(array([where(abs(t_stim-25.)<=20., 1, 0), \
                             zeros(int(runtime/defaultclock.dt))]).T*nA, \

eqs_e = '''
dv/dt = (g_leak_e*(E_leak_e-v) - g_bar_Na_e*(m*m*m)*h*(v-E_Na) - g_bar_Kd_e*(n*n*n*n)*(v-E_K) - g_bar_M_e*p*(v-E_K) + stimulus(t, is_stim(i) )) / C : volt
dm/dt = 0.32*(mV**-1)*(13.*mV-v+V_T_e)/
    (exp((v-V_T_e-40.*mV)/(5.*mV))-1.)/ms*m : 1
dn/dt = 0.032*(mV**-1)*(15.*mV-v+V_T_e)/
    (exp((15.*mV-v+V_T_e)/(5.*mV))-1.)/ms*(1.-n)-.5*exp((10.*mV-v+V_T_e)/(40.*mV))/ms*n : 1
dh/dt = 0.128*exp((17.*mV-v+V_T_e)/(18.*mV))/ms*(1.-h)-4./(1+exp((40.*mV-v+V_T_e)/(5.*mV)))/ms*h : 1
dp/dt = (1./(1.+exp(-(v+35.*mV)/(10.*mV)))-p)/(tao_max_e/(3.3*exp((v+35.*mV)/(20.*mV))+exp(-(v+35.*mV)/(20.*mV)))) : 1

G_e = NeuronGroup(200, eqs_e,
                    threshold='v > 0*mV',
                    refractory='v > 0*mV',

M_e = SpikeMonitor(G_e)
M_v_e = StateMonitor(G_e, 'v', record=True)


What you have aready tried

I tried installing various packages like cython.
I tried changing the IDE to PyCharm (was using spyder).
I tried the latest versions of everything (brian2 library, python, PyCharm)

Expected output (if relevant)

Actual output (if relevant)

Full traceback of error (if relevant)

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\erased\AppData\Local\Temp\brian_debug_8cuqpzn7.log  Additionally, you can also include a copy of the script that was run, available at: C:\Users\erased\AppData\Local\Temp\brian_script_0to5g7fs.py Thanks! [brian2]
Traceback (most recent call last):
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\codegen\codeobject.py", line 418, in create_runner_codeobj
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\core\functions.py", line 349, in __getitem__
    raise KeyError(('No implementation available for target {key}. '
KeyError: 'No implementation available for target cython. Available implementations: '

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

Traceback (most recent call last):
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\core\network.py", line 897, in before_run
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\groups\group.py", line 1143, in before_run
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\groups\group.py", line 1136, in create_code_objects
    code_object = self.create_default_code_object(run_namespace)
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\groups\group.py", line 1118, in create_default_code_object
    self.codeobj = create_runner_codeobj(group=self.group,
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\codegen\codeobject.py", line 425, in create_runner_codeobj
    raise NotImplementedError(('Cannot use function '
NotImplementedError: Cannot use function is_stim: 'No implementation available for target cython. Available implementations: '

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

Traceback (most recent call last):
  File "E:\Research\05 - Brian\HH_network_temp.py", line 68, in <module>
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\units\fundamentalunits.py", line 2434, in new_f
    result = f(*args, **kwds)
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\core\magic.py", line 373, in run
    return magic_network.run(duration, report=report, report_period=report_period,
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\core\magic.py", line 231, in run
    Network.run(self, duration, report=report, report_period=report_period,
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\core\base.py", line 278, in device_override_decorated_function
    return func(*args, **kwds)
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\units\fundamentalunits.py", line 2434, in new_f
    result = f(*args, **kwds)
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\core\network.py", line 1008, in run
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\lib\site-packages\brian2\core\base.py", line 278, in device_override_decorated_function
    return func(*args, **kwds)
  File "C:\Users\erased\AppData\Local\Programs\Python\Python39\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 "neurongroup_stateupdater".
Object was created here (most recent call only, full details in debug log):
  File "E:\Research\05 - Brian\HH_network_temp.py", line 60, in <module>
    G_e = NeuronGroup(200, eqs_e,              

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

Process finished with exit code 1

it seems to me that Brian can’t find cython.
Can you try a couple of things to help clarify what’s happening on your laptop?

first it’s probably worth trying to execute

import cython

from python in the command line or as part of a script in your IDE like PyCharm. If you installed cython with pip, you can also try pip show cython (just in the command line, outside of python)

This should check that your cython installation was successful. You could go through the steps here to triple check cython is working, but that’s probably overkill.

The important thing is to make sure cython is available in whatever environment brian2 is being executed from, so it may also be worth checking if you have any conda environments active, and that the dependencies are installed inside those environments specifically
docs on environments

It looks like you might be working on a windows machine, so definitely follow the steps outlined here closely:

On Windows, the necessary steps to get Runtime code generation (i.e. Cython) to work depend on the Python version you are using (also see the notes in the Python wiki):

  • Install the Microsoft Build Tools for Visual Studio 2017.
  • Make sure that your setuptools package has at least version 34.4.0 (use conda update setuptools when using Anaconda, or pip install --upgrade setuptools when using pip).

from windows install

as a workaround, if you continue to have issues and need to get the code running asap, you could try setting the codegen target to numpy instead of cython. This will likely make the code run slower but it should run regardless of whether you have cython

you’d do this by adding the following to your script:

from brian2 import *
prefs.codegen.target = 'numpy'  # use the Python fallback

I just came back from the summer break and saw your issue. From your code (I took the liberty to edit your question to put ``` marks around your code so that it gets formatted nicely), I think the issue is that you provide a Python implementation for the is_stim function, but your simulation runs with Cython. Brian therefore complains that it cannot find a Cython implementation. I guess on your laptop you do not have a working Cython installation, so it falls back to Python automatically. @adam 's suggested workaround (setting prefs.codegen.target = 'numpy') will make your lab PC fall back to Python as well and should solve the issue.
You could also provide a Cython implementation, but I think a simpler solution to what you are trying to achieve (if I understood correctly), is to remove the is_stim function and instead add a parameter to your model equations

is_stim : integer (constant)

that you assign the content of your stim_out1 array:

G_e.is_stim = stim_out1

Finally, use it as stimulus(t, is_stim) (instead of ...is_stim(i)...).

Hope that helps, best

1 Like

Hi adam. Sorry for the late reply. Thank you very much. I tried the solutions and the part with prefs.codegen.target = 'numpy' worked. Thanks again

Hi mstimberg. Thank you very much. That worked very well.

1 Like