The animal doesn’t care about whether your computer is finished with the calculations or not, it keeps doing its own thing!
Rather than thinking of code as text, Bonsai tries to capture an intuitive way of thinking of code as a picture or diagram

Making research accessible with an intuitive visual programming language

20 October 2022

By Hyewon Kim

Developing an intuitive visual programming language: the birth of Bonsai

Ten years ago, a neuroscience PhD student at the Champalimaud Centre for the Unknown in Lisbon realised that traditional programming languages limited his way of thinking about the experiments he wanted to run.

“I initially developed the Bonsai language to run my experiments. We wanted to work mostly on natural behaviour in animals, specifically in rats. So, we needed a way to create a complex environment for animals and to control the variables in real time so that we could ask questions that require a more precise control over what is happening in the environment as the animal behaves,” shared Sainsbury Wellcome Centre (SWC) alumnus and NeuroGEARS founder Gonçalo Lopes.

Most modern programming languages still constrain us to think of programme structure like a written novel or a play describing a sequence of steps. Nothing in the middle of the code can change while the input of a system gets transformed to an output. This paradigm is not ideal when designing an environment where change is as constant as in the real world that animals navigate. “The animal doesn’t care about whether your computer is finished with the calculations or not, it keeps doing its own thing!” explained Gonçalo.

Goncalo Bonsai demo

Gonçalo demonstrates the concept behind the Bonsai visual programming language, in this example recording pictures, the position of the cursor, and input from the keyboard all at once, while at the same time being able to link them together as desired

The programming language that Gonçalo created, fittingly called Bonsai, embodies the same spirit of the Japanese practice of growing a miniature tree in a confined space. “Just like the art form, the main aesthetics and challenge of our work involve being able to recreate all the complexities of a living landscape in a very confined space,” shared Gonçalo in 2015. Rather than thinking of code as text, Bonsai tries to capture an intuitive way of thinking of code as a picture or diagram. Each element can be running independently and pieces can be interconnected to help design complex systems where many kinds of changes are taking place at the same time. “The challenge is how to make visual programming work well, to elegantly and compactly describe the complex interplay of environments and instruments required for studying behaviour in natural conditions,” he explained.

Bonsai’s unexpected and continuing accessibility

In 2014, a couple years before Gonçalo finished his PhD, he was invited to give a workshop about Bonsai at Cold Spring Harbor. “I wasn’t even aware before then that people outside our research institute were interested in Bonsai! Bonsai was indeed made to be accessible, but I was not advertising it in any way. I made it open source from day one because I wanted this language to be available to anyone. But I was also in the depths of my PhD, so I didn’t have the time to make it pretty and write up the documentation. I was mostly concerned with making it work for my own experiments,” he said. However, by word of mouth, Bonsai started to spread through labs that needed to design new environments for behavioural neuroscience experiments. These labs had been missing good tools which were accessible for biologists who had little experience with programming, while allowing flexibility to design completely new experiments.

Bonsai roadmap

Bonsai development timeline and community adoption. (A) Timeline of milestones and landmark package releases. (B) Example capabilities within Bonsai. (C) Bonsai workflow for tracking the position of objects in a video. (D) Worldwide distribution of visitors to the website. (E) Number of downloads for each Bonsai version. (F) Cumulative number of registered forum users, citations of the main publication, and number of training events, by year (bottom panels), versus release and development cycle (top panel), measured in commits to main repository. Credit: Gonçalo Lopes and Joaquin Rapela

Eventually, Bonsai’s appeal was felt in research institutions on a global scale. After the workshop, Gonçalo published a paper on Bonsai in Frontiers, an open access journal. He found out through the journal’s metrics that thanks to the workshop, Bonsai had reached the east and west coasts of the United States, even though he had made no effort to push it there. From the Cold Spring Harbor workshop that Gonçalo had run, PhD students and postdocs had picked it up to use in their own work. Because Bonsai is free and open source, when these researchers moved on in their careers and relocated, Bonsai went with them, since it does not require obtaining a licence to use it.

This May, Gonçalo, along with researchers at the SWC and Gatsby Computational Neuroscience Unit (GCNU), were awarded over £700,000 from the Biotechnology and Biological Sciences Research Council (BBSRC) to implement machine learning techniques in Bonsai. “You need a community of developers that are putting in the effort to make useable extensions,” Gonçalo explained. “I think now, the future is going to be to increase not just the community of users of Bonsai, but also the community of developers. That will ensure the future of the language.”

Joaquin Goncalo board 2

Joaquin Rapela and Gonçalo Lopes discuss the design of a closed-loop experiment in Joaquin’s office at the GCNU

An experiment in and of itself

From this same spirit of merging machine learning with a powerful visual programming language to empower new research, emerged the very grounds that enabled Bonsai to welcome such machine learning makeovers. When Gonçalo had finished his PhD five years ago and was at the SWC for a postdoc, the future of Bonsai was already on his mind. Back then, it was common to apply for funding to do research on a specific scientific question, but the means to develop a tool that would be widely available to everyone to help answer such questions was much harder to come by. Gonçalo wanted Bonsai, a completely independent, free and open-source tool, to stay that way. “When I discussed these goals with several people, the idea to start a company was suggested – by Adam Kampff, my PhD advisor, and Troy Margrie, Associate Director of the SWC.”

Gonçalo took their advice. NeuroGEARS was founded in 2017 with the vision of reimagining science. What sets NeuroGEARS apart from other companies that may centre their business models on investors in a commercial product like LabVIEW or MATLAB, is their way of staying sustainable without selling Bonsai. “We are like an independent research group, but we help other groups do their research using Bonsai,” Gonçalo said. This is where he comes in as a collaborator on the BBSRC grant. NeuroGEARS works with the SWC as an expert consultant to design, assemble, and operate the experimental systems and instruments using Bonsai. “For a typical commercial product, what companies do is make a projection of sales and how production is going to grow,” he continued.

The way that NeuroGEARS stays sustainable as a company, even when an additional 100,000 Bonsai users doesn’t mean an increase in funding from usage, lies in its unique partnership with research groups. They apply to grants together with partners like the SWC and consult in services. Then, they remain an integral part of the team. “NeuroGEARS is, in a way, an experiment in asking whether we can self-organise in an unconventional way, being close to science, but not through the traditional route, and yet still support what needs to be done to advance the field.”

Learn more about the backstory of the recently awarded BBSRC grant ‘Machine Intelligence for Neuroscience Experimental Control’