Error in Google Colab

Description of problem

I am trying to use Brian2 on Colab. It seems that it works fine but I keep getting the following error for almost every line of code. It seems this is a problem with the way that Colab internal debugging tools try to automatically retrieve a shape attribute from objects in Brian2.

Minimal code to reproduce problem

tau = 10*ms

eqs = ‘’’

dv/dt = (1-v)/tau : 1

‘’’

What you have already tried

Expected output (if relevant)

Actual output (if relevant)

Full traceback of error (if relevant)

ERROR:root:Unexpected exception finding object shape
Traceback (most recent call last):
File “/usr/local/lib/python3.11/dist-packages/google/colab/_debugpy_repr.py”, line 54, in get_shape
shape = getattr(obj, ‘shape’, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/brian2/core/preferences.py”, line 226, in getattr
return self[name]
~~~~^^^^^^
File “/usr/local/lib/python3.11/dist-packages/brian2/core/preferences.py”, line 173, in getitem
return self.prefs[item]
~~~~~~~~~~^^^^^^
KeyError: ‘shape’

Hi @SaeedF36. Thanks for letting us know! I understand where the error is coming from and it is arguably a bug on our side: if you access prefs.shape, i.e. a non-existing shape attribute of our preferences object, you’ll get a KeyError because we are translating prefs.shape into prefs['shape'], but it should actually raise an AttributeError to work with things like getattr.
On the other hand, I am not able to reproduce this on Google Collab, if I do a %pip install brian2 and then paste some Brian example, everything seems to be running fine without error. Do you have a simple example that I can run (or a Google Collab to share) that leads to this error? Thanks!

Hi Marcel, thank you for your quick reply. Here is an example of what I encounter. In this case, it gives me this error twice. With every run, one of the errors disappears which means after three runs, it is clear and no exception happens. Thanks!

!pip install brian2
!pip install brian2tools

from brian2 import *

tau = 10*ms
eqs = '''
dv/dt = (1-v)/tau : 1
'''
G = NeuronGroup(1, eqs)
run(100*ms)

BTW, I installed it locally and it runs smoothly. Although I prefer using Colab.

Thanks for the update. This was a bit of a head-scratcher, since I could again copy&paste your code and it ran without issues. But I figured out the reason : you either have the “Variable Inspector” open (the \{x\} button on the left-hand side), or have it configured with “Display values in code editor: While executing” (configuration on the bottom of the variable inspector. This then triggers Colab to try checking for the shape argument of every variable in the local namespace which leads to the error you see. As I said previously, this is something we should fix in Brian, but until then deactivating the variable inspector, or replacing the from brian2 import * by a more restricted import should make everything work.

Best
Marcel

1 Like

Thank you so much! :folded_hands: You are absolutely right. When the inspector is open or it is configured to display numbers, it raises this error. Setting the configuration alone to ‘Never’ does not help and the inspector must be closed. Similarly, just closing the inspector while configuration is set to something other than ‘Never’ still gives the error. So to get rid of the error, inspector must be closed and ‘Never’ have to be set for configuration.

1 Like