GSoC 2021 project idea 16.2: Make BRIAN run in the web browser via Pyodide

BRIAN simulations can currently only run in a web browser if a server runs the code. Due to recent improvements in browser technology such as WebAssembly, browser are however capable of executing computationally complex code themselves, directly on the client machine.

The Pyodide project leverages this technology to run the Python scientific stack in the browser, and we have shown with a prototype project that this can be extended to support BRIAN. The specific aims of this project are to:

  • create a robust Pyodide implementation of BRIAN2 that gets updated automatically with new releases of BRIAN2
  • create convenient methods to display simulation results in the browser
  • investigate integrating this with the current BRIAN documentation and/or on a dedicated “showcase” website

Skills: Python programming and basic web development (HTML, JavaScript), experience with WebAssembly helpful

Mentors: Marcel Stimberg @mstimberg , Dan Goodman @d.goodman

Tags: BRIAN, Python, HTML, JavaScript

Hello, I’m Zhihao and I’m interested in this project.

About me

Now I’m an M1 student at The University of Tokyo. In the past, I developed a science data toolkit AMinerOpen / prediction_api for AMiner using python. What’s more, when doing an internship in Kuaishou, I did some data visualization websites for both front-end and back-end. I think I’m relatively familiar with Python, HTML, and JavaScript.

Some problems

I have no experience with Pyodide. So I did some tries but met some problems. According to the description, you have shown a prototype project brian2_pyodide. But when I ran this project, I got an error in the browser console: RecursionError: maximum recursion depth exceeded while calling a Python object. And I found this error exists when I tried to import brain2. Is there something wrong when I ran this project or a bug?

@mstimberg @d.goodman

Hi Zhihao, happy to hear you are interested in the project! The prototype project is terribly underdocumented (sorry about that!), so possibly you did not run it in the right way. What works for me (in Firefox and Chrome under Linux at least):

  • clone the repository
  • run a webserver in the project directory with python -m http.server
  • Open the displayed address (probably http://0.0.0.0:8000/)

It should start loading the libraries (which will take a while), and then you can run the code.

Let us know if this does not work for you!

Dear Mr. Stimberg,

I tried to follow your instruction but got the same error in the browser console:

Traceback (most recent call last):
  File "/lib/python3.7/site-packages/pyodide.py", line 43, in eval_code
    exec(compile(mod, '<exec>', mode='exec'), ns, ns)
  File "<exec>", line 1, in <module>
  File "/lib/python3.7/site-packages/brian2/__init__.py", line 37, in <module>
    from pylab import *
  File "/lib/python3.7/site-packages/pylab.py", line 1, in <module>
    from matplotlib.pylab import *
  File "/lib/python3.7/site-packages/matplotlib/pylab.py", line 252, in <module>
    from matplotlib import cbook, mlab, pyplot as plt
  File "/lib/python3.7/site-packages/matplotlib/pyplot.py", line 31, in <module>
    import matplotlib.colorbar
  File "/lib/python3.7/site-packages/matplotlib/colorbar.py", line 44, in <module>
    import matplotlib._constrained_layout as constrained_layout
  File "/lib/python3.7/site-packages/matplotlib/_constrained_layout.py", line 55, in <module>
    from matplotlib.legend import Legend
  File "/lib/python3.7/site-packages/matplotlib/legend.py", line 47, in <module>
    from matplotlib.offsetbox import HPacker, VPacker, TextArea, DrawingArea
  File "/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 37, in <module>
    from matplotlib.image import BboxImage
  File "/lib/python3.7/site-packages/matplotlib/image.py", line 11, in <module>
    from six.moves.urllib.request import urlopen
  File "/lib/python3.7/site-packages/six.py", line 92, in __get__
    result = self._resolve()
  File "/lib/python3.7/site-packages/six.py", line 160, in _resolve
    module = _import_module(self.mod)
  File "/lib/python3.7/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/lib/python3.7/urllib/request.py", line 88, in <module>
    import http.client
  File "/lib/python3.7/http/client.py", line 71, in <module>
    import email.parser
  File "/lib/python3.7/email/parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "/lib/python3.7/email/feedparser.py", line 27, in <module>
    from email._policybase import compat32
  File "/lib/python3.7/email/_policybase.py", line 7, in <module>
    from email import header
  File "/lib/python3.7/email/header.py", line 20, in <module>
    from email import charset as _charset
  File "/lib/python3.7/email/charset.py", line 18, in <module>
    from email.encoders import encode_7or8bit
  File "/lib/python3.7/email/encoders.py", line 16, in <module>
    from quopri import encodestring as _encodestring
  File "<frozen importlib._bootstrap>", line 980, in _find_and_load
  File "<frozen importlib._bootstrap>", line 148, in __enter__
  File "<frozen importlib._bootstrap>", line 174, in _get_module_lock
  File "<frozen importlib._bootstrap>", line 59, in __init__
RecursionError: maximum recursion depth exceeded while calling a Python object

My environment: Windows 10, Chrome

I’m not sure whether it is because of compatibility between Linux and windows. I tried the same one on Windows Subsystem for Linux, still got that error. @mstimberg

Dear Mr. Stimberg,

I tried to increase the maximum recursion depth to 1e5 and solved this problem. This looks strange that there are so many recursions. I’m not familiar with brain2 so I cannot point out where recursions are.

import sys
sys.setrecursionlimit(10**5)

I got some output from browser console:

Starting simulation at t=0. s for a duration of 1. s
0.3431 s (34%) simulated in 10s, estimated 19s remaining.
0.6945 s (69%) simulated in 20s, estimated 9s remaining.
1. s (100%) simulated in 28s

And a new error:

pyodideWorker error:  Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope': ArrayBuffer is not detachable and could not be cloned.

@mstimberg

Dear Mr. Mstimberg,

And then I tried to solve the worker error by directly transferring String rather than ArrayBuffer. It seems to work on my computer. I fork a repository brian2_pyodide and would you mind having a try to see whether it works?

What’s more, I still don’t know why there are such bugs in my environment. And the way I solve them is a little tricky. Do you have some ideas on them?

Hi @Zhihao , I am not sure what is going on, but maybe you can investigate this in more detail and include your analysis in the application document? I don’t think it is a general Windows/Linux issue, since @d.goodman was able to run my code on Windows, IIRC. Just to confirm: your version works on my machine as well.

Some general words about the application process:

  • when you start the application on the GSoC website, you will get a template for the general structure, so I’d recommend to wait for the official start of the application period before compiling the document.
  • don’t hesitate to share your draft application with me so that I can give you feedback, but please don’t wait until the very last day if you want to incorporate my feedback into the final version :slight_smile:
  • in the application, the detailed timeline does not matter that much (these things are always hard to predict); the important thing is to show that you 1) understand the project and its deliverables and 2) that you have the knowledge/skills to successfully finish this project.
  • for the second point, point to concrete proof of your experience, e.g. if you published code anywhere (e.g. for project work as part of your studies), please include a link to it.
  • this year, GSoC adds a bit of flexibility to the schedule: the official guideline is that over the 10 weeks of the project, “students are expected to spend on average 18 hours a week on the program”. Please include in your application how you’d like to organize time over the project, e.g. whether you prefer to do this “part-time work” over the full 10 weeks, or rather have fewer weeks with more hours, but include time off for vacation, etc. If several options work for you, you can of course write this as well. Finally, please mention any external constraints (e.g. exams) and how they fit into the schedule.

Finally, a personal remark: I am currently on paternity leave, so please don’t worry if I don’t reply right away, I might be busy with other things :baby_bottle: :wink:

Hiii…!!

I’m Vedarutvija and I have keen interest in this project, it will be great if a mentor could guide me understand well about this and help me proceed making a proposal.

About me:
I’m a Data science and Artificial intelligence student at Icfai University. I read about open source recently(in this month) and did a lot of research and finally landed over here. I would love to contribute to this organization and participate in GSoc 2021, I’m here for learning and exploring so I wish to contribute as much as I can despite getting selected.

I have worked on few AI based projects in an internship. The tech stack used by this organization is what I actually have (Python, Java, JavaScript, C, Html, CSS ) and to contribute to this project I’m familiar with python and basic Web Development, need to work on WebAssembly.

Problems
This is my first contribution. I have recently learnt git commands and how they work.

@mstimberg @d.goodman

**

Hi @vedarutvija, happy to hear you are interested in the project. For some general guidelines, see my post above. The most important point of the application is to show that you understood the projects and the technologies involved, so the more you can go into details about what already exists and what needs to be done the better. It is also important to point to examples of your own work relevant to this project (similar technologies, etc.). If you have any specific questions about the project (or Brian in general), don’t hesitate to ask here or via a private message.

1 Like

Thank you so much, I will work on it and let you know if i come up with questions on it