Skip to content

Contributing

First ask whether the change belongs in the core. Behaviour that only your project needs goes in your project's builders/ or analyses/, which the core finds without edits. Change the core when the fix or feature would help any project.

  1. Fork the repository on GitHub and clone your fork.
  2. Install it editable: pip install -e ".[dev]". Tests that simulate need NEST (Installation); the Brian2 tests need .[brian2] and are skipped without it.
  3. Create a branch, make the change and add a test for it.
  4. Run ruff check . and pytest. CI runs both.
  5. Push the branch to your fork and open a pull request into main.

  6. The version number is set when you install. In a clone, microcircuit_lab_version in a run's metadata.json stays at that number until you install again; core_git always has the commit that ran.

  7. A project made with init --with-core has a copy of the core with the backends of the simulators installed at the time; init <project> --with-core --backends <name> adds another.

To pull later changes into your fork, add this repository as a remote: git remote add upstream git@github.com:shimoura/microcircuit-lab.git.

Changing the core

Package layout

microcircuit_lab/
├── config/            what each YAML file may contain (one module per file), and reading and writing YAML
├── network/           reading, checking and preparing a network
├── simulation.py      Simulation: load and run
├── backends/          the simulators (NEST, Brian2), and the readers of their files
├── analysis/          measures and plots
├── cli/               the microcircuit-lab command
├── builder_tools/     what a builder needs, in one import (also scaling and the constant-current steps)
├── run_folder/        write.py makes a run folder and its metadata, read.py reads it back (read_run)
└── project_code.py    finds the builders/, analyses/ and extensions/ of a project

Only this folder is installed by pip. In a clone, examples/ holds one folder per model, each laid out like a project (builders/, configs/, extensions/, ...) and run from its own folder. The tests run from examples/toy/ (tests/conftest.py).

Follow the name

A concept keeps its name from the YAML file to the test. To change something, start from its YAML file and follow the name:

%%{init: {"flowchart": {"wrappingWidth": 400}}}%%
flowchart TD
  Y["examples/toy/configs/network/toy_stimulated/stimuli.yaml"] --> C["config/stimuli.py<br/>what the file may contain"]
  C --> B["backends/nest/simulator.py<br/>create_stimuli: builds it in NEST"]
  B --> T["tests/test_stimuli.py"]

The files hold the numbers that are simulated; the simulator only turns each background rate per synapse into the rate of its generator (rate × indegree, generator_rate in config/poisson_background.py). Scaling and constant currents are builder steps (builder_tools/). The code behind each step from a builder to the plots: docs/workflow.md.

Who checks what

Every mistake is reported once, by whoever knows the name it is about, and as early as that one can:

What Checked by When
The file format; the values the core computes with (sizes, indegrees, rates, a weight's mean); what each shared name means (p is a probability, a draw has a spread, a delay lasts at least one time step) the core: Field(...) limits and validators in config/ when the files are read; a check that needs the simulation file too (a delay against the time step), in the backend's check() through a core function
What its simulator cannot do with a shared name and would not say itself (it would run something else, or stop without a message); the names it looks up or the core writes (a project's distribution, a receptor port's name, an I_e added by a builder) the backend in its check(), before anything is built
Everything under a simulator's own name (neuron and synapse parameters, the arguments of its own rules and distributions), and its own stricter limits the simulator (for Brian2, its models in extensions/brian2/) when it builds; the core passes them on unchecked

So the core leaves a simulator's names to the simulator: {rule: symmetric_pairwise_bernoulli, p: 2.5} is refused by NEST. And a shared name means the same on every simulator: {distribution: normal, mean: 1, std: 0} is refused by the core (a value without spread is written as value), not left to run on one simulator and fail on the other.

Does your idea fit what already exists?

flowchart TD
  Q{"One more of something<br/>the files already have?<br/>(a stimulus type, a plot, ...)"}
  Q -->|yes| T["Find its recipe below:<br/>2–3 files, a test, docs,<br/>then a pull request"]
  Q -->|"no, a new concept<br/>(spatial structure, plasticity)"| D["Open an issue with<br/>a short design note first"]
  D --> S["Small pull requests,<br/>one step each, with<br/>a small example network"]

A new concept touches every step and raises scientific questions to answer before the code. For spatial structure, for example: does N_scaling keep the density or the size of the sheet, and what does K_scaling do to a connection kernel? The design note settles those, so each pull request can be reviewed on its own.

Recipes

Click the kind of change. Each recipe ends with a test and a line in the docs (docs/). A change in config/ also needs python docs/write_config_files.py, which rewrites docs/config_files.md; a test fails until you do.

A stimulus type 1. `config/stimuli.py`: add a class next to the others, and add it to `StimulusConfig` at the end of the file. 2. `backends/nest/simulator.py`, `create_stimuli`: create its NEST device. Change `connect_stimuli` only if it connects differently. `backends/brian2/inputs.py`: its rate or current over time in `_stimulus_curve` (or its own Brian2 objects in `create_stimuli`). 3. Test in `tests/test_stimuli.py`. The [worked example](#an-example-a-single-spike-stimulus) below does exactly this.
A weight or delay distribution A distribution only one project needs doesn't go in the core: with NEST it is a function in the project's `extensions/nest/`, with Brian2 a `Distribution` in `extensions/brian2/` (see [Other distributions](docs/weights-and-delays.md#other-distributions-optional) and [Distributions of your own](docs/brian2.md#distributions-of-your-own-optional)). A shared one, which every simulator draws: 1. `config/distributions.py`: add its name and the fields it takes to `SHARED_DISTRIBUTIONS` (e.g. `"gamma": ("mean", "std")`, the mean and standard deviation of the drawn values). 2. `backends/nest/distributions.py`: draw it in `drawn_parameter`. `backends/brian2/distributions.py`: its Brian2 expression in `DRAWS` and its two numbers in `_numbers`, and its chance of crossing a bound in `_chance_past`. 3. Test in `tests/test_bound_mode_and_tau_syn.py`.
A field on a population 1. `config/populations.py`: add it to `PopulationEntry` (the file) and `PopulationConfig` (the network). 2. `network/load.py`, `assemble_network`: copy it from one to the other. 3. `backends/nest/simulator.py`: use it in `create_populations` or `initialize_population`; `backends/brian2/simulator.py`: in `create_populations`. 4. Test in `tests/test_config_validation.py`.
A field on a connection, or a connection rule A rule only one project needs doesn't go in the core: with Brian2 it is a `ConnectionRule` in the project's `extensions/brian2/` (see [Connection rules of your own](docs/brian2.md#connection-rules-of-your-own-optional)). A shared rule, one that every simulator runs: 1. `config/connectivity.py`: add it to `ConnectionConfig` (a field), or to `SHARED_RULES` (a rule; to `RULE_NUMBER` too if it takes a number). 2. `config/connection_table.py`: add a field's column to `COLUMNS` (the core holds all connections as one table; it refuses to load until the new field has a column). 3. `backends/nest/simulator.py`: use it in `create_connections`, which reads the table column by column; `backends/brian2/simulator.py` too, and a rule in `backends/brian2/connections.py`: its counts in `_counts` if it takes a number, its `connect` call in `_single`. 4. If scaling or the indegrees depend on it: `builder_tools/mean_input.py` (`expected_synapses`) and `builder_tools/scaling.py` (`_check_connections_can_scale` for a rule that can't follow the scaling), on whole columns. 5. Test in `tests/test_connection_rules.py`.
A scaling or compensation option 1. `builder_tools/scaling.py`: the option, as an argument of `scale_network`, and its formula. 2. The same option in the `parameters` of the record it writes (`ScalingRecord`, `config/scaling.py`, is `scaling.yaml`). 3. `builder_tools/constant_current.py`, for a compensation current. 4. Test in `tests/test_scaling.py` or `tests/test_scaling_compensation.py`, and in `tests/test_heterogeneous_areas.py` if it depends on cell types.
Something to record 1. `config/simulation.py`: add it to `RecordingConfig`. 2. `backends/nest/simulator.py`: a `create_*`/`connect_*` pair, and reading it back; `backends/brian2/recording.py`: a Brian2 monitor in a `create_*` function, and writing it in `save_recordings`. 3. `run_folder/write.py`, if it is saved in the run folder, and `run_folder/read.py` if `read_run` should give it. 4. Test in `tests/test_run_folder.py`.
A bundled plot 1. `analysis/.py`: the plot function `plot_(run, ...)`, whose first parameter is the `Run` that `read_run` gives, a settings class, and `register_plot(...)` at the end. 2. `analysis/__init__.py`: import the module, so the plot is registered when the package loads. 3. Test in `tests/test_.py`. The call at the end of step 1, with a settings class:
register_plot("spectrogram", plot_spectrogram,
              config_cls=SpectrogramPlotConfig, filename="spectrogram.png")
A simulator 1. Implement `SimulationBackend` (`microcircuit_lab/backends/interface.py`) in a package `microcircuit_lab/backends//`, like `nest/` or `brian2/`. `prepare_output` and `save_recordings` write the recordings in the simulator's own way; its file reader (below) reads them back, so nothing else needs to know. `get_population` and `get_spike_events` give `metadata.json` the node IDs and spike counts. `version()` gives the simulator's version for `metadata.json`. 2. Name the class `Backend` too, at the end of `/simulator.py` (`Backend = MyBackend`), and write its file reader as `/spike_files.py` with `read_saved_spikes` and `read_saved_variable` (like `nest/spike_files.py`, which does not import the simulator). `microcircuit_lab/backends/__init__.py` finds both by these names; saved runs can then be re-plotted without the simulator. 3. Add its name to `BackendName` (`microcircuit_lab/config/simulation.py`). Select it with `backend: {name: , options: {...}}` in the simulation file, or `Simulation.from_yaml(..., backend="", backend_options={...})`; the options go to its constructor unchanged.

No change to the core is needed for a neuron model (name it in neurons.yaml, NESTML models too; for Brian2, define it in extensions/brian2/, see Models of your own), for a connection rule, distribution or synapse model only one project needs (for Brian2, in extensions/brian2/; see Names the simulator looks up), or for a new network model (a network folder or a builder; see A new network model).

When a stimulus type is not known, the error message points to this section.

An example: a single-spike stimulus

  1. Write the config you want, e.g. in stimuli.yaml:
    stimuli:
      - type: spike
        targets: [L4_E]
        spike_times: [500.0]
        weight: {value: 87.8, unit: pA}
        delay: {value: 1.5}
    
  2. Running it fails with stimuli.yaml: stimuli.0: Input tag 'spike' ... does not match any of the expected tags and points here.
  3. In config/stimuli.py, add the class next to the others and to StimulusConfig:
    class SpikeStimulusConfig(StimulusBase):
        """Spikes at fixed times, e.g. a single-spike perturbation."""
    
        type: Literal["spike"] = "spike"
        spike_times: list[float] = Field(min_length=1, description="ms")
        weight: WeightDistribution
        delay: DelayDistribution
        synapse: str | None = Field(
            default=None,
            examples=["depressing"],
            description="name of an entry in synapses.yaml; omit for the default synapse",
        )
    
  4. In NestBackend.create_stimuli, create its device:
    elif isinstance(stimulus, SpikeStimulusConfig):
        model = "spike_generator"
        params["spike_times"] = stimulus.spike_times
    
    Connecting it needs nothing new: every stimulus except dc connects with its own weight, delay and synapse, like poisson.
  5. A test in tests/test_stimuli.py, and the new type in the list of stimuli in Network files.

Testing

Terminal
pytest

The tests cover the core: reading and checking configs, preparing a network, the simulator backends and the analysis. A builder is tested through what it writes: every network in configs/network/ is loaded and checked. Tests that simulate need NEST; the Brian2 tests are skipped without Brian2.

Code style

  • Use plain names close to the papers and reference codes (N_scaling, K_ext, tau_syn_ex), no extra layers, no logic written twice.
  • Use pydantic models for anything read from a file, so a mistake is reported with the file and the field. A field's limits go in Field(...), not in separate checks, together with a description (with its unit) and, for a required field, examples=[...]: docs/config_files.md is written from them.
  • Docstrings explain what the code does and why. Functions meant to be called from outside the core (the top-level API, builder_tools, Simulation, analysis, config.yaml_files) use the NumPy docstring style (Parameters, Returns, Raises); internal ones can stay in prose. Comments are full lines above the code, not at the end of a line.
  • Core tests check the core and how it reads a builder's output. They must not depend on a specific builder.
  • Keep the dependencies few (pyproject.toml), and add one only when it is clearly worth it. pandas is there for one reason: reading and writing large CSV files fast (the connection table, the spike files). A connection table of 1M rows takes 0.1 s with pandas and about 4 s with Python's csv module, and a large multi-area network has millions. Everywhere else, the code holds numpy arrays and plain structures, not DataFrames.
  • Keep the field names of the config models stable (populations, size, indegree, ...): every run saves its network in config.yaml with those names, and renaming one breaks re-plotting old runs. A new field needs a default for the same reason. Rename a field only when its old name is wrong or misleading. The old name then fails with a message giving the new one (no conversion is kept), and the rename is listed in the release notes. Command-line flags, backend options and folder names follow the same rule but matter less, because they don't end up in a run folder.