Efficiently interfacing Brian2 with external code - Bypassing NetworkOperation

Hello, in my application I am interfacing a Brian2 network (built in Cython) with an external simulation that also runs in Cython and is called from Python code.
The two simulations need to exchange data (network states and external input) every timestep.
The only way I found to solve this problem is through the use of a NetworkOperation object that takes care of the communication happening via Python queues among two threads.
While this solution works, it slows down the simulation drammatically.
Could there be a way to provide a callback function directly at the Cython level and by-pass the NetworkOperation?

I think it would be very nice to have a feature such as a Python, Cython or even C++ - level q_in/q_out option

Hi @AlexPazzaglia. Linking a Brian simulation to external code without having to use a network_operation is one of the things we want to make possible in Brian. In many cases, it should be quite straightforward using our “user-defined function” mechanism, which allows you to define a function directly in the target language: Functions — Brian 2 2.5.1 documentation Unfortunately, we currently only have one example showing how to use this mechanism (for C++): Example: opencv_movie — Brian 2 2.5.1 documentation

I think the feature has been considerably less tested for Cython compared to C++, so please let us know if it doesn’t work for you or if anything is unclear for your particular use case.

1 Like