Skip to main content
v2026.11,986 entries · CC-BY 4.0

Brian2: Python Spiking Neural Network Simulator Explained

Brian2 is a Python simulator for spiking neural networks. Learn how its equations, units and run modes work, how to install and check it, and how to cite it.

Written and maintained by CASRAI Editorial Board

Published

On this page

Brian2 is a free, open-source simulator for spiking neural networks that you drive from Python. You describe each neuron model as differential equations written as text, group neurons and synapses into objects, add monitors to record what happens, and call run(). Brian 2 then turns those short descriptions into efficient low-level code and runs it alongside your own Python code. The reference paper is Stimberg, Brette and Goodman (2019), published in eLife.

This page explains what Brian2 is, how its main ideas fit together, the three ways it can execute a simulation, what to check after installing it, and what a lab or research office should record so that a simulation result can be reproduced. It is written for researchers, lab managers and research-computing staff who need to decide whether Brian2 fits a project, not as a replacement for the official tutorials.

What is Brian2?

Brian2 is the second major generation of the Brian simulator, a tool for building and running models of spiking neural networks, meaning networks in which neurons communicate through discrete spikes. The eLife paper describing it states that it can simulate such models, including models that use novel dynamical equations, models that interact with their environment, and models that implement experimental protocols. The project describes itself on GitHub as a free, open source simulator for spiking neural networks. The package name on PyPI is brian2, and the version number is not the same as the generation number: the generation is Brian 2, while the release current when this page was prepared was 2.11.0 on PyPI (checked 2026-11-20).

The design problem the authors set out to solve is stated in the paper abstract. Other simulators, they write, tend to fall into two groups. Some ask the user for low-level programming, which requires expertise, is error-prone and is bad for reproducibility. Others use model description languages, which cannot describe every aspect of an experiment, for example the logic of a stimulation protocol. Brian addresses both problems with runtime code generation: the user writes concise high-level descriptions, and the simulator transforms them into efficient low-level code that runs interleaved with the user’s own code. The paper illustrates this with a plastic model of the pyloric network, a closed-loop sensorimotor model, programmatic exploration of a neuron model, and an auditory model with real-time input.

Definition and scope

In practice Brian2 is a Python library. You import it, write model equations as strings, and the library compiles or interprets them. Three things follow from that definition, and each matters when deciding whether the tool is the right one.

  • It simulates spiking networks. The documentation, the paper and the repository description all place the tool in that category. It is not a general-purpose numerical package, and it is not a tool for analysing recorded experimental data.
  • It is code, not a graphical application. A model is a Python script or notebook. That makes the experiment itself a text file that can be versioned and re-run, which is the reproducibility argument the authors make in the paper.
  • The experiment is part of the program. Because simulation code runs interleaved with ordinary Python, a stimulation protocol, a parameter sweep or a closed loop is written in the same language as the model, instead of being squeezed into a description format.

Brian2 is one of several neural simulators, and the choice between them depends on the question. If your model is built around detailed multi-compartment neurons, a different tool may be the natural fit; the site covers one such tool in its guide to the NEURON simulator. This page does not compare simulators, because the sources read for it describe Brian2 only.

How Brian2 works in practice

Equations are written as text

A neuron model in Brian2 is a string of equations. The official tutorial’s first example is a single neuron whose variable v relaxes towards 1 with time constant tau. The equation is written as dv/dt = (1-v)/tau : 1. The part after the colon declares the unit of the variable, and the digit 1 means that v is dimensionless.

Groups, monitors and run()

Equations are attached to a NeuronGroup, which holds one or many neurons that share the same model. A StateMonitor records a state variable over time, and a SpikeMonitor records when neurons spike. Calling run(50*ms) advances the simulation by 50 milliseconds. Everything the experiment needs sits in one script.

Worked example: the tutorial’s leaky neuron, line by line

The Brian 2 introductory tutorial for neurons uses the following code. Reading it against the model concepts is the quickest way to see how the pieces map onto each other.

from brian2 import *
tau = 10*ms
eqs = '''dv/dt = (1-v)/tau : 1'''
G = NeuronGroup(1, eqs, threshold='v>0.8', reset='v = 0', method='exact')
statemon = StateMonitor(G, 'v', record=0)
spikemon = SpikeMonitor(G)
run(50*ms)
print(spikemon.t[:])
  • tau = 10*ms sets a time constant of 10 milliseconds. The unit is attached by multiplication, so the number carries its dimension.
  • eqs is the leaky equation: the variable rises towards 1 and the rate of change shrinks as it gets closer.
  • threshold='v>0.8' and reset='v = 0' turn that leaky equation into a spiking neuron. When v passes 0.8 the neuron emits a spike and v is set back to 0. A leaky equation plus a threshold and a reset is the structure of a leaky integrate-and-fire neuron, and this is the simplest instance of it. The tutorial example is not a Hodgkin-Huxley model, and this page does not cover Hodgkin-Huxley specifics.
  • method='exact' chooses how the differential equation is integrated; here the tutorial asks for the exact solution.
  • The two monitors record the voltage-like variable and the spike times. run(50*ms) executes the simulation and the last line prints the spike times.

The tutorial reports spikes at approximately 16, 32.1 and 48.2 ms. You can check that output by hand, which is a useful habit for any simulation. Starting from 0, the equation gives v(t) = 1 − e−t/τ. Setting v = 0.8 gives t = τ ln 5, which is about 16.09 ms for τ = 10 ms. Because the variable resets to 0 at each spike, the interval repeats, so spikes are expected near 16.1, 32.2 and 48.3 ms. The simulated values agree with this closed-form estimate to within about 0.1 ms. That check is the editorial reading of this page’s author, not a statement from the tutorial, but the arithmetic is reproducible by anyone.

Units are an error check

Brian2 attaches physical units to quantities and refuses operations that do not make dimensional sense. According to the units page of the documentation, adding a plain number to a time raises a DimensionMismatchError (the example is tau += 1), and so does an expression such as 3*kgram + 3*amp, which fails with a message that the units do not match and names kilogram and amp. Function arguments are checked as well: passing a plain number where a time step is expected in a NeuronGroup raises an error. Equations declare their units after the colon, as in NeuronGroup(5, 'dv/dt = -v/tau: volt').

When a plain number is needed, for example for plotting, you can strip the unit by dividing by it (tau/ms gives 20.0 for a 20 ms constant), by converting with asarray, or by using the underscore suffix on a variable name, as in G.v_[:]. For a research group the practical point is that a whole class of silent mistakes, such as a time constant entered in seconds where milliseconds were meant, becomes an error at run time instead of a plausible-looking wrong result.

Execution modes: numpy, Cython and C++ standalone

Brian2 can execute the same model in three ways, selected by preference settings, and the choice affects speed, setup effort and what you can do inside a script. The data table below compares them, using only the statements on the computation page of the official documentation. The default is runtime code generation, in which the simulator generates code while your script runs. Two targets exist for that default mode: numpy, which needs no compilation, and cython, which needs Cython and a C++ compiler. The preference codegen.target defaults to 'auto', and when compilation is not possible the simulator falls back to Python. To force the numpy target you set prefs.codegen.target = 'numpy' at the start of the script.

The third way is standalone mode. You call set_device('cpp_standalone') after importing Brian and before the simulation code. Brian then generates a C++ project that is compiled separately. The documentation calls this “in general faster”, and also states that it cannot be used for all kinds of simulations and that not all features of Brian will work. State variables and synaptic indices are not accessible during setup in standalone mode, so a script that inspects them while building the network has to be restructured.

The runtime mode has its own price. The documentation notes a potentially big overhead for each simulated time step, in return for the ability to combine the simulation with arbitrary Python code. It also notes that for short simulations the Cython target can look slower than numpy, and that compiled code is cached on disk. In other words, the first run of a model may include a compile step that later runs avoid, so timing a single short run says little about long-run speed.

Install and check

The installation page of the documentation lists conda and pip as the main routes, with distribution packages also available. Pick one route and record it.

  1. Create a separate environment. The documentation recommends installing into a dedicated virtual or conda environment. The site’s guide to sharing a reproducible conda environment covers how to capture and share one, and the guide to installing Anaconda Python covers the base setup.
  2. Install the package. With conda use conda install -c conda-forge brian2. With pip use python -m pip install brian2. The documentation also lists apt (python3-brian), dnf (python-brian2) and spack (py-brian2) packages.
  3. Check the Python version on the PyPI page. The minimum supported Python version changes between releases, so read the current PyPI project page for the release you plan to install.
  4. Know what compiles. Cython is installed automatically and is needed for C++ code generation, which the documentation strongly recommends for speed. A C++ compiler is required: the conda package installs one on Linux and macOS, while Windows needs the Microsoft Build Tools.
  5. Add optional packages if you need them. The documentation lists matplotlib, pytest, ipython, notebook and brian2tools as optional.
  6. Run the built-in test. In Python run import brian2 and then brian2.test(). A successful run ends with “OK”.

If you want to try the examples without installing anything locally, the project maintains a Binder repository, and the site has a guide to launching a notebook with Binder (mybinder.org).

Where it matters for labs and research offices

Brian2 is software, so the questions a lab has to answer are about reproducibility, compute and support rather than about benches and consumables. The sources support the following points.

Reproducibility

The eLife paper presents reproducibility as one reason for the design: models described at a high level in a text script are easier to share and re-run than models spread across low-level code. To make that real, record the Brian version, the Python version, the code generation target and device, and the environment definition alongside each result. The execution mode belongs in that record because standalone mode does not support every feature, and because the runtime targets differ in how they are set up. The site’s dictionary entry on reproducibility explains the general practice.

Compute and compiler setup

The fastest documented route needs a C++ compiler, and on Windows that means installing the Microsoft Build Tools. Research-computing staff who support a shared workstation or cluster should expect that requirement. If a compiler is not available, the runtime default falls back to Python, which the documentation presents as the slower, no-compilation path.

Citation and software records

The standard reference for the simulator is Stimberg M, Brette R, Goodman DFM. Brian 2, an intuitive and efficient neural simulator. eLife 2019;8:e47314, doi 10.7554/eLife.47314 (PubMed 31429824, PMC6786860). Cite it in methods sections and add the exact version you ran, because the paper describes the tool generally while results depend on the release. The PyPI page and the repository link to the changelog, which is where version-specific changes are listed. For how software is cited and described in machine-readable form, see the dictionary entries on Citation File Format (CITATION.cff) and open source software. The PyPI license field for the package was empty when checked, so read the repository itself before stating licence terms in a data-management plan.

Common misunderstandings

Is Brian2 a graphical program?

No. It is a Python library, and models are written as code. This is the point of the design described in the paper: the experiment is expressed in code so that it can be re-run.

Is Brian2 the same as NEURON?

No. They are separate simulators from separate projects. Brian2’s own materials describe it as a simulator for spiking neural networks, and the official documentation is on brian2.readthedocs.io. Do not mix the two tools’ install instructions or model files.

Is the version number the same as “Brian 2”?

No. “Brian 2” names the generation of the simulator; the package version, such as 2.11.0 on PyPI at the time of checking, identifies the specific release. Record both facts.

Is standalone mode always better because it is faster?

Not always. The documentation says standalone mode is in general faster but cannot be used for every kind of simulation and does not support all features. Check your model against the documentation before choosing it.

Can I pass plain numbers where Brian2 expects a time?

No, and the error is intentional. Quantities carry units, and mixing them with bare numbers raises a DimensionMismatchError. Multiply by a unit such as ms to fix it.

Is the Hodgkin-Huxley model part of the first tutorial?

No. The introductory tutorial’s example is a leaky equation with a threshold and reset. Other models are built by writing different equations in the same way.

Sources

Data: Brian2 execution modes: requirements and documented tradeoffs

Compares the three ways Brian2 can run a simulation (numpy, Cython and C++ standalone): how each is selected, what it needs, and the tradeoff the official Brian 2 documentation states for it. Compiled by CASRAI from the computation and installation pages of the Brian 2 documentation, read 2026-11-20.

Brian2 execution modes: requirements and documented tradeoffs
Execution modeHow it is selectedWhat it needsDocumented tradeoff
Runtime, numpy targetSet prefs.codegen.target = 'numpy' at the start of the scriptNo compilationRuntime mode has a potentially big overhead for each simulated time step, in return for combining the simulation with arbitrary Python code
Runtime, Cython targetDefault 'auto' setting for codegen.target; falls back to Python if compilation is not possibleCython and a C++ compiler (the conda package installs a compiler on Linux and macOS; Windows needs Microsoft Build Tools)Strongly recommended for speed; for short simulations it can appear slower than numpy; compiled code is cached on disk
C++ standalone deviceCall set_device('cpp_standalone') after the import and before the simulation codeA C++ compiler to build the generated C++ project, which is compiled separatelyIn general faster, but cannot be used for all kinds of simulations and not all Brian features work; state variables and synaptic indices are not accessible during setup

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about Brian2: Python Spiking Neural Network Simulator Explained

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →