# Brian2

> Brian2

- Skill: `hughyau/brian2` (Agent Skill, multi-file: 213 files)
- Install (CLI): `npx skillmds@latest add hughyau/brian2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/hughyau/brian2/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: HughYau (https://skillmd.com/u/hughyau)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/hughyau/brian2

---


# Brian2

Brian2 is a clock-driven simulator for spiking neural networks with equation-defined models, explicit physical units, and multiple execution backends.

## Version

<!-- built against: brian2==2.10.1 -->
Built against: `brian2==2.10.1` [UNVERIFIED: inferred from `docs_sphinx/introduction/release_notes.rst` because install was denied]
Python: `3.13`
> See `assets/version.txt` for environment, install policy, and verification limits.

## Scope

This skill focuses on user-facing modeling workflows: `NeuronGroup`, `Synapses`, inputs/stimuli, recording, device selection, and `SpatialNeuron`.
Coverage profile: `hybrid` (workflow references + dictionary assets).

## Environment Gate

Install policy and environment decision are recorded in `assets/version.txt`.
Current build used `install_permission: no`; execution-dependent claims are tagged where relevant.

## Installation

```bash
# requires explicit install permission
pip install brian2

# optional extras from pyproject.toml
pip install "brian2[test,docs]"
```

---

## Neuron Models and Simulation Loop

```python
# tested against brian2==2.10.1
try:
    from brian2 import NeuronGroup, StateMonitor, ms, mV, run

    neurons = NeuronGroup(
        5,
        "dv/dt = -v/(10*ms) : volt",
        threshold="v > -50*mV",
        reset="v = -70*mV",
    )
    neurons.v = -70 * mV
    mon = StateMonitor(neurons, "v", record=0)
    run(5 * ms)
    print(len(mon.t))
except Exception as e:
    print(f"[UNVERIFIED: install denied in selected environment] {type(e).__name__}: {e}")
```

See `references/modeling-neurons-and-equations.md` for `NeuronGroup`, `Equations`, state variables, and method-selection caveats.

---

## Synapses and Plasticity

```python
# tested against brian2==2.10.1
try:
    from brian2 import NeuronGroup, Synapses, ms, mV, run

    pre = NeuronGroup(2, "dv/dt = -v/(10*ms) : volt", threshold="v > -55*mV", reset="v = -70*mV")
    post = NeuronGroup(2, "dv/dt = -v/(10*ms) : volt")
    syn = Synapses(pre, post, "w : volt", on_pre="v += w")
    syn.connect(i=[0, 1], j=[1, 0])
    syn.w = "0.5*mV"
    run(1 * ms)
    print(int(syn.N))
except Exception as e:
    print(f"[UNVERIFIED: install denied in selected environment] {type(e).__name__}: {e}")
```

See `references/synapses-and-plasticity.md` for `connect(...)` patterns, event-driven equations, and STDP setup.

---

## Inputs and Time-Dependent Stimuli

```python
# tested against brian2==2.10.1
try:
    import numpy as np
    from brian2 import NeuronGroup, PoissonGroup, SpikeMonitor, TimedArray, Hz, mV, ms, run

    stim = TimedArray(np.array([0.0, 1.0, 0.5, 0.0]) * mV, dt=1 * ms)
    target = NeuronGroup(1, "dv/dt = (stim(t) - v)/(5*ms) : volt")
    drive = PoissonGroup(5, rates=20 * Hz)
    mon = SpikeMonitor(drive)
    run(3 * ms)
    print(len(mon.t), float(target.v[0] / mV))
except Exception as e:
    print(f"[UNVERIFIED: install denied in selected environment] {type(e).__name__}: {e}")
```

See `references/inputs-and-stimuli.md` for `PoissonGroup`, `SpikeGeneratorGroup`, `TimedArray`, and `PoissonInput`.

---

## Recording and Rate Analysis

```python
# tested against brian2==2.10.1
try:
    from brian2 import PoissonGroup, PopulationRateMonitor, SpikeMonitor, Hz, ms, run

    group = PoissonGroup(10, rates=50 * Hz)
    spikes = SpikeMonitor(group)
    rates = PopulationRateMonitor(group)
    run(20 * ms)
    print(spikes.num_spikes, len(rates.rate))
except Exception as e:
    print(f"[UNVERIFIED: install denied in selected environment] {type(e).__name__}: {e}")
```

See `references/recording-and-analysis.md` for monitor indexing rules, event-clock sampling, and binned/smoothed rates.

---

## Execution Backends and Devices

```python
# tested against brian2==2.10.1
try:
    from brian2 import get_device, set_device

    set_device("runtime")
    print(type(get_device()).__name__)
except Exception as e:
    print(f"[UNVERIFIED: install denied in selected environment] {type(e).__name__}: {e}")
```

See `references/execution-and-devices.md` for `set_device`, `device.build`, `run_args`, and cache-management patterns.

---

## Morphology and SpatialNeuron

```python
# tested against brian2==2.10.1
try:
    from brian2 import Cylinder, Soma, SpatialNeuron, amp, cm, mV, meter, ohm, uF, um

    morph = Soma(diameter=20 * um)
    morph.axon = Cylinder(length=50 * um, diameter=1 * um, n=5)
    eqs = "Im = 0*amp/meter**2 : amp/meter**2"
    neuron = SpatialNeuron(morphology=morph, model=eqs, Cm=1 * uF / cm**2, Ri=100 * ohm * cm)
    neuron.v = -65 * mV
    print(neuron.flat_morphology.n)
except Exception as e:
    print(f"[UNVERIFIED: install denied in selected environment] {type(e).__name__}: {e}")
```

See `references/morphology-and-spatial-neurons.md` for `Soma`/`Section`/`Cylinder` constructors and `SpatialNeuron` constraints.

---

## Verification (Medium+)

```bash
PYTHONPATH="H:\Agent\OpenSciHub\brian2" python "H:\Agent\OpenSciHub\.opencode\skills\opensci-skill\scripts\verify-snippets.py" --root "H:\Agent\OpenSciHub\.opencode\skills\brian2" --fail-fast
```

## Dictionary Assets (Hybrid)

Use dictionary assets before source traversal for symbol-level lookup:

1. Query `assets/symbol-index.jsonl` for exact symbol names.
2. Open the matching card in `assets/symbol-cards/`.
3. Follow `source` anchors only when implementation details are required.

Primary dictionary entrypoint: `assets/symbol-index.md`.

## Quick Reference

| Function / Class | Purpose |
|-----------------|---------|
| `NeuronGroup(...)` | Define equation-based neuron populations with threshold/reset logic. |
| `Synapses(...)` | Define synaptic dynamics and pre/post event code between groups. |
| `Synapses.connect(...)` | Materialize connectivity via explicit indices, conditions, or probabilities. |
| `PoissonGroup(...)` | Generate stochastic spike trains as an input source. |
| `TimedArray(...)` | Inject time-varying signals into model equations. |
| `SpikeMonitor(...)` | Record spike times and per-neuron spike counts. |
| `StateMonitor(...)` | Record continuous state variables over time or event-clock times. |
| `PopulationRateMonitor(...)` | Record population firing rates with binned/smoothed summaries. |
| `set_device(...)` | Select runtime or `cpp_standalone` backend before creating objects. |
| `SpatialNeuron(...)` | Simulate multicompartment neurons over an explicit morphology tree. |

---

## Module Map

| Submodule | Contents | Notes |
|-----------|----------|-------|
| `brian2.core` | clocks, network scheduler, magic run/store/restore APIs | `core.network` is [LARGE] |
| `brian2.groups` | `NeuronGroup`, subgroup views, group variable handling | `groups.group` and `neurongroup` are [LARGE] |
| `brian2.synapses` | synapse model parsing, connectivity, pathway execution | `synapses.synapses` is [LARGE] |
| `brian2.input` | Poisson, spike-generator, and timed-array input sources | used for feedforward drive |
| `brian2.monitors` | spike/event/state/rate recording interfaces | `spikemonitor` is [LARGE] |
| `brian2.devices` | runtime device and C++ standalone codegen backend | standalone path is [LARGE] |
| `brian2.spatialneuron` | morphology objects and multicompartment simulation | morphology module is [LARGE] |
| `brian2.units` | unit system and dimension checks | `allunits` and `fundamentalunits` are [LARGE] |

Import style: mixed eager import in `brian2.__init__` (`from brian2.only import *`, NumPy/pylab star imports), with top-level API primarily controlled by `brian2.only.__all__`; no lazy `__getattr__` detected.

See `assets/module-map.md` for full inventory and source anchors.

---

## References

- `references/modeling-neurons-and-equations.md` - equation syntax, `NeuronGroup` configuration, and integration-method implications.
- `references/synapses-and-plasticity.md` - `Synapses` signatures, connectivity patterns, and event-driven plasticity.
- `references/inputs-and-stimuli.md` - stochastic and scheduled input APIs (`PoissonGroup`, `SpikeGeneratorGroup`, `TimedArray`, `PoissonInput`).
- `references/recording-and-analysis.md` - monitor APIs and retrieval patterns for spikes, states, and rates.
- `references/execution-and-devices.md` - runtime vs standalone device control, build workflow, and cache behavior.
- `references/morphology-and-spatial-neurons.md` - morphology construction and `SpatialNeuron` modeling details.
- `assets/symbol-index.md` - module-level dictionary navigation.
- `assets/symbol-index.jsonl` - machine-readable symbol registry.
- `assets/symbol-cards/` - per-module symbol cards with signatures and source anchors.

