How is brainpy different from other frameworks?

How is brainpy different from other frameworks?#

Colab Open in Kaggle

@Chaoming Wang @Xiaoyu Chen

import brainpy as bp
import brainpy.math as bm

bp.math.set_platform('cpu')

bp.__version__

BrainPy vs Brian2/NEST/NEURON …#

Different from traditional brain simulators (most of them employ a descriptive language for programming brain dynamics models), BrainPy aims to provide the full supports for brain dynamics modeling.

Currently, brain dynamics modeling is far beyond simulation. There are many new modeling approaches which take inspiration from the machine learning community. Moreover, it has also inspired the new development of brain-inspired computation.

These new advances cannot be captured by the traditional brain simulators. Therefore, BrainPy aims to provide an ecosystem for brain dynamics modeling, in which users can build various models easily and flexibly, and extend new modeling approach conveniently, etc.

The core idea behind BrainPy is the Just-In-Time (JIT) compilation. JIT compilation enables your Python code to be compiled into machine code “just-in-time” for execution. Subsequently, such transformed code can run at native machine code speed!

Based on this, BrainPy provides an integrated platform for brain dynamics modeling, including

  • universal model building

  • dynamics simulation

  • dynamics training

  • dynamics analysis

Such integrative framework may help users to study brain dynamics comprehensively.

BrainPy vs JAX/Numba#

BrainPy relies on JAX and Numba. But it also has important aspects which are different from them.

JAX and Numba are excellent JIT compilers in Python. However, they are designed to work only on pure Python functions. Most computational neuroscience models have too many parameters and variables to manage using functions only. Therefore, BrainPy provides an object-oriented programming interface for brain dynamics modeling. There object-oriented transformations are implemented in brainpy.math module.

brainpy.math is not intended to be a reimplementation of the API of any other frameworks. All we are trying to do is to make a better brain dynamics programming framework for Python users.

There are important differences between brainpy.math and JAX and JAX related frameworks.

Specifically, brainpy.math provides:

  1. Numpy-like ndarray.

Python users are familiar with NumPy, especially its ndarray. JAX has similar ndarray structures and operations. However, several basic features are fundamentally different from numpy ndarray. For example, JAX ndarray does not support in-place mutating updates, like x[i] += y. To overcome these drawbacks, brainpy.math provides Array that can be used in the same way as numpy ndarray.

b = bm.arange(5)
b
Array([0, 1, 2, 3, 4], dtype=int32)
b[0] += 5
b
Array([5, 1, 2, 3, 4], dtype=int32)
  1. Numpy-like random sampling.

JAX has its own style to make random numbers, which is very different from the original NumPy. To provide a consistent experience, brainpy.math provides brainpy.math.random for random sampling just like the numpy.random module. For example:

# random sampling in "brainpy.math.random"

bm.random.seed(12345)
bm.random.random(5)
Array([0.47887695, 0.5548092 , 0.8850775 , 0.30382073, 0.6007602 ],            dtype=float32)
bm.random.normal(0., 2., 5)
Array([-1.5375282, -0.5970201, -2.272839 ,  3.233081 , -0.2738593],            dtype=float32)

For more details, please see the Arrays tutorial.

  1. JAX transformations on class objects.

OOP is the essence of Python. However, JAX’s excellent tranformations (like JIT compilation) only support pure functions. To make them work on object-oriented coding in brain dynamics programming, brainpy.math extends JAX transformations to Python classes. Details please see BrainPy Concept of Object-oriented Transformation.