Python API¶
The functions you call from a script, a builder or an analysis, with their docstrings. Their use, with examples: Running from a script, A new network model, Analysing a run.
On GitHub this page shows only the names; the documentation site shows the docstrings, which are also in the code and in help(...).
Run and read¶
Simulation
¶
A network and its simulation settings, run on a backend.
output_dir = None
instance-attribute
¶
is_main_process = True
instance-attribute
¶
from_yaml(config_dir='configs', network_name='toy', simulation_name=None, backend=None, backend_options=None)
classmethod
¶
Build a simulation from the config files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_dir
|
str | Path
|
The folder holding |
'configs'
|
network_name
|
str
|
The network folder, |
'toy'
|
simulation_name
|
str | None
|
The simulation profile, |
None
|
backend
|
BackendName | None
|
The simulator, |
None
|
backend_options
|
dict[str, Any] | None
|
Extra keyword arguments for the backend, passed through unchanged, e.g.
|
None
|
Returns:
| Type | Description |
|---|---|
Simulation
|
Ready to |
run()
¶
Build the network in the simulator, simulate it and save the outputs.
What is saved (recordings, the config, metadata) is set by the
recording and output sections of the simulation config; see
:func:microcircuit_lab.run_folder.write.save_run_outputs. output_dir points to the
run folder afterwards. With several MPI processes, only the first prints and
saves; the others have is_main_process False.
read_network(folder)
¶
Read a network folder as written and check it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder
|
str | Path
|
The network folder, e.g. |
required |
Returns:
| Type | Description |
|---|---|
NetworkConfig
|
The network as described by its files, e.g. to change it, or to
scale it with :func: |
read_run(run_dir)
¶
Open a finished run folder, without the simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_dir
|
str | Path
|
The run folder, e.g. |
required |
Returns:
| Type | Description |
|---|---|
Run
|
The run's settings, and its spikes and recordings, which are read when asked for. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If the run has no |
Builder tools¶
builder_tools
¶
Everything a builder needs, in one import.
A builder turns a model's data (published tables, pickles, recordings) into a
network folder configs/network/<name>/::
from microcircuit_lab.builder_tools import delay, weight, write_network_directory, zero_bound
write_network_directory(
"configs/network/my_model",
populations=[{"name": "E", "size": 800, "neuron": "lif", ...}],
neurons={"lif": {"model": "iaf_psc_exp", "parameters": {...}}},
connections=[{"source": "E", "target": "E", "n_synapses": 8000,
"weight": weight(87.8, unit="pA", rel_std=0.1, **zero_bound(87.8, "redraw")),
"delay": delay(1.5, rel_std=0.5, lower_bound=0.1, bound_mode="redraw")}],
poisson_background={"E": {"rate": 8.0, "indegree": 1600, "weight": weight(87.8, unit="pA"),
"delay": delay(1.5)}},
)
Modules: entries.py (weight, delay, synapses from a probability),
write.py (network folders), psp.py (PSP to PSC), scaling.py (scale
a network) and constant_current.py / mean_input.py (the currents and
quantities it uses).
- :func:
write_network_directorywrites every file of the folder and checks the result (with :func:microcircuit_lab.network.load.read_network), so a mistake raises with the file and the field named. - :func:
scale_networkreturns a network scaled down (fewer neurons and synapses, stronger weights, the lost mean input as a current), and :func:write_networkwrites such a network as a folder. - :func:
replace_background_by_mean_currentreturns the network with its background Poisson input replaced by a constant current of the same mean, to apply before writing it. - :func:
mean_currentis the constant current (pA) with the mean of a Poisson input, for a builder that adds the input of areas it leaves out. - :func:
weightand :func:delaybuild theweight/delayentries every connection, background input and stimulus needs. - :func:
num_synapses_from_conn_probturns a connection probability into a number of synapses (Potjans & Diesmann 2014). - :func:
psp_to_psc_iaf_psc_expconverts a PSP amplitude (mV) into the current (pA) that produces it, for a model whose weights are given as PSPs (:func:psc_to_pspgoes back). The core never converts a weight itself.
None of these invents model values: a field a builder leaves out is either a default of the config models or an error. Using them is optional; files written any other way are checked the same way when the network is loaded.
write_network_directory(path, *, populations, neurons, connections, poisson_background, scaling=None, reference=None, stimuli=None, synapses=None, synapse_lists=None, compact=True)
¶
Write a whole network folder and check it.
Each argument is the content of one file, without the file's top-level key
(populations:, connections:, ...), which is added here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The network folder to write, e.g. |
required |
populations
|
Iterable[Mapping[str, Any]]
|
One dict per population ( |
required |
neurons
|
Mapping[str, Mapping[str, Any]]
|
|
required |
connections
|
Iterable[Mapping[str, Any]] | ConnectionTable
|
One dict per projection ( |
required |
poisson_background
|
Mapping[str, Mapping[str, Any]]
|
|
required |
scaling
|
Mapping[str, Any] | None
|
The record of a scaled network ( |
None
|
reference
|
Mapping[str, float] | None
|
|
None
|
stimuli
|
Iterable[Mapping[str, Any]] | None
|
The list of stimuli; |
None
|
synapses
|
Mapping[str, Mapping[str, Any]] | None
|
|
None
|
synapse_lists
|
Mapping[str, SynapseList] | None
|
The synapses of each connection with |
None
|
compact
|
bool
|
Connections are always written one per line. |
True
|
Returns:
| Type | Description |
|---|---|
NetworkConfig
|
The network as written, from
:func: |
write_network(network, path, *, csv=False, compact=True)
¶
Write network as a network folder, e.g. one returned by
:func:scale_network.
Populations that share a neuron entry keep sharing it while their neurons
are the same; a population whose neuron differs (e.g. its own compensation
current in I_e) gets an entry of its own, named after the population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
NetworkConfig
|
The network as written (background rates per synapse). |
required |
path
|
str | Path
|
The folder to write, e.g. |
required |
csv
|
bool
|
Write the connections as |
False
|
compact
|
bool
|
As for :func: |
True
|
Returns:
| Type | Description |
|---|---|
NetworkConfig
|
The network read back from the folder. |
weight(mean, *, unit, rel_std=0.0, distribution=None, lower_bound=None, upper_bound=None, bound_mode=None, **arguments)
¶
A weight entry for a connection, background input or stimulus, from
its mean and its relative spread, as papers and reference codes give them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
float
|
The mean weight the simulator gets, in |
required |
unit
|
str
|
|
required |
rel_std
|
float
|
Standard deviation divided by |
0.0
|
distribution
|
str | None
|
|
None
|
**arguments
|
float
|
The arguments of a distribution that is not shared, e.g. |
{}
|
lower_bound
|
float | None
|
Bounds of a drawn weight, and what to do past them ( |
None
|
upper_bound
|
float | None
|
Bounds of a drawn weight, and what to do past them ( |
None
|
bound_mode
|
float | None
|
Bounds of a drawn weight, and what to do past them ( |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The entry as written in the YAML file. It is checked against
:class: |
delay(mean, *, rel_std=0.0, distribution=None, lower_bound=None, upper_bound=None, bound_mode=None, **arguments)
¶
A delay entry for a connection, background input or stimulus, as
:func:weight.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
float
|
Mean delay in ms. |
required |
rel_std
|
float
|
Standard deviation divided by |
0.0
|
distribution
|
str | None
|
As in :func: |
None
|
**arguments
|
str | None
|
As in :func: |
None
|
lower_bound
|
float | None
|
Bounds (ms) of a drawn delay, which needs a |
None
|
upper_bound
|
float | None
|
Bounds (ms) of a drawn delay, which needs a |
None
|
bound_mode
|
float | None
|
Bounds (ms) of a drawn delay, which needs a |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The entry as written in the YAML file, checked against
:class: |
zero_bound(mean, bound_mode)
¶
Bounds that keep a drawn weight's sign: lower_bound: 0 for a weight
with a mean of 0 or more, upper_bound: 0 for a negative one, with
bound_mode "redraw" or "clip". For :func:weight:
weight(w, unit="pA", rel_std=0.1, **zero_bound(w, "redraw")).
num_synapses_from_conn_prob(conn_prob, n_source, n_target)
¶
Compute the total number of synapses from a connection probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn_prob
|
float
|
Connection probability. |
required |
n_source
|
int
|
Number of source neurons. |
required |
n_target
|
int
|
Number of target neurons. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Expected total number of synapses. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a population is empty, or if |
psp_to_psc_iaf_psc_exp(psp, C_m, tau_m, tau_syn)
¶
Convert a PSP amplitude (mV) to the current amplitude (pA) that produces it.
Holds for a current-based LIF neuron with exponential synaptic currents.
It is named after NEST's iaf_psc_exp, where people know it from, but it
only depends on C_m, tau_m and tau_syn (see
:func:psc_over_psp), whatever the model is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psp
|
float
|
The PSP amplitude, in mV. |
required |
C_m
|
float
|
Membrane capacitance, in pF. |
required |
tau_m
|
float
|
Membrane time constant, in ms. |
required |
tau_syn
|
float
|
Synaptic time constant, in ms. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The current amplitude, in pA. |
psc_to_psp(psc, C_m, tau_m, tau_syn)
¶
Convert a current amplitude (pA) to the PSP amplitude (mV) it produces; the
inverse of :func:psp_to_psc_iaf_psc_exp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psc
|
float
|
The current amplitude, in pA. |
required |
C_m
|
float
|
As in :func: |
required |
tau_m
|
float
|
As in :func: |
required |
tau_syn
|
float
|
As in :func: |
required |
Returns:
| Type | Description |
|---|---|
float
|
The PSP amplitude, in mV. |
scale_network(network, *, N_scaling, K_scaling, scaled_from, preserve_mean_input=True, preserve_variance=True, compensate_recurrent_input=True)
¶
Return network scaled down, as the files of the scaled network show it.
- The number of neurons of each population times
N_scaling. - The synapses per neuron times
K_scaling: the number each connection rule takes (see :func:scale_rule_number) and the background indegrees. - The weights divided by
sqrt(K_scaling)(preserve_variance, which keeps the fluctuations of the input) or byK_scaling(which keeps its mean), for connections and background inputs. - With
preserve_varianceandpreserve_mean_input: a constant current added to each neuron'sI_e, the mean input lost in step 3 (van Albada, Helias & Diesmann 2015). The lost background input always; the lost recurrent input too withcompensate_recurrent_input, fromreference.full_mean_rates.
Stimuli are not scaled. A network that was already scaled is refused: its compensation would take the first one's currents as the full input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
NetworkConfig
|
The network as written, at full scale (e.g. from
:func: |
required |
N_scaling
|
float
|
The factors, above 0 (1 keeps that number). |
required |
K_scaling
|
float
|
The factors, above 0 (1 keeps that number). |
required |
scaled_from
|
str
|
The name of the full-scale network, written in the record. |
required |
preserve_mean_input
|
bool
|
As above. |
True
|
preserve_variance
|
bool
|
As above. |
True
|
compensate_recurrent_input
|
bool
|
As above. |
True
|
Returns:
| Type | Description |
|---|---|
NetworkConfig
|
The scaled network; its |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
replace_background_by_mean_current(network)
¶
Replace the background Poisson input by a constant current of the same mean.
The current (pA) is the mean of the background input as the network gives
it (rate per synapse, indegree, weight), added to I_e (see Potjans &
Diesmann 2014, Fig. 5). The returned network has no background input left:
poisson_background is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
NetworkConfig
|
The network, with its background weights in pA on the default receptor port. |
required |
Returns:
| Type | Description |
|---|---|
NetworkConfig
|
A new network without background input. |
mean_current(weight, indegree, rate, tau_syn)
¶
The constant current (pA) with the mean of the input of indegree
synapses of amplitude weight (pA) firing at rate (spikes/s).
One spike gives an exponential current of amplitude weight and time
constant tau_syn (ms), a charge of tau_syn * weight (ms x pA); the
factor 0.001 turns it into pA x s. This assumes current-based synapses
with exponential currents (tau_syn_ex, tau_syn_in).
Analysis¶
analysis
¶
Analysis and visualization utilities.
compute_population_firing_rates(run, begin=0.0, end=None, *, populations=None, area=None)
¶
The mean firing rate of each population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
Returns:
| Type | Description |
|---|---|
dict of str to float
|
|
compute_neuron_firing_rates(run, begin=0.0, end=None, *, populations=None, area=None)
¶
The firing rate of every neuron, per population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
Returns:
| Type | Description |
|---|---|
dict of str to numpy.ndarray
|
|
compute_population_isi_cv(run, begin=0.0, end=None, *, populations=None, area=None, min_spikes=3)
¶
The mean ISI CV of each population, over the neurons where it is defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
min_spikes
|
int
|
The fewest spikes a neuron needs. |
3
|
Returns:
| Type | Description |
|---|---|
dict of str to float
|
|
compute_neuron_isi_cv(run, begin=0.0, end=None, *, populations=None, area=None, min_spikes=3)
¶
The coefficient of variation of the inter-spike intervals (ISI CV) of every neuron, per population.
A neuron needs at least min_spikes spikes in the window for its CV to be
defined; a neuron with fewer gets NaN, which NaN-aware averages skip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
min_spikes
|
int
|
The fewest spikes a neuron needs. |
3
|
Returns:
| Type | Description |
|---|---|
dict of str to numpy.ndarray
|
|
compute_power_spectrum(run, begin=0.0, end=None, *, populations=None, area=None, bin_size=1.0, method='welch', method_kwargs=None)
¶
The power spectrum of each population's rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
bin_size
|
float
|
The bin width of the rate signal, in ms. |
1.0
|
method
|
str
|
The estimator: a name in :data: |
'welch'
|
method_kwargs
|
dict | None
|
Extra arguments for the estimator, e.g. |
None
|
Returns:
| Type | Description |
|---|---|
dict of str to tuple
|
|
compute_reference_deviation(run, begin=0.0, end=None, *, populations=None, area=None)
¶
How far each population's mean rate is from its reference rate.
The reference rates are full_mean_rates in the network's
reference.yaml. This is the number behind
plot_firing_rates(..., show_reference=True), for a check in a script.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
Returns:
| Type | Description |
|---|---|
dict of str to float
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the network has no reference rates, or none for a selected population. |
list_areas(run)
¶
The areas of a multi-area network, in population order.
An area is the <area> in a population named <area>__<population>.
Next to such names, a population without __ (e.g. TC) is an area
of its own; a network where no name has __ has no areas.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
The area names; empty for a single-area network. |
plot_raster(run, begin=0.0, end=None, *, populations=None, area=None, max_neurons_per_population=UNSET, style=None, config=None, save_path=None, show=True)
¶
Plot the spikes of each population (a raster plot).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
max_neurons_per_population
|
int | None
|
Show at most this many neurons per population ( |
UNSET
|
style
|
PlotStyle | None
|
The shared plot style (:class: |
None
|
config
|
RasterPlotConfig | None
|
This plot's settings from the analysis file; its defaults otherwise. |
None
|
save_path
|
str | Path | None
|
Where to save the figure; not saved if |
None
|
show
|
bool
|
Open the figure in a window. |
True
|
Returns:
| Type | Description |
|---|---|
tuple
|
The matplotlib |
Notes
config.begin and config.end, if set, replace begin and end
for this plot only.
plot_firing_rates(run, begin=0.0, end=None, *, populations=None, area=None, show_reference=False, style=None, config=None, save_path=None, show=True, rng_seed=0)
¶
Plot the distribution of single-neuron firing rates of each population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
show_reference
|
bool
|
Overlay the reference rates ( |
False
|
style
|
PlotStyle | None
|
The shared plot style (:class: |
None
|
config
|
FiringRatePlotConfig | None
|
This plot's settings from the analysis file; its defaults otherwise. |
None
|
save_path
|
str | Path | None
|
Where to save the figure; not saved if |
None
|
show
|
bool
|
Open the figure in a window. |
True
|
rng_seed
|
int
|
Seed for choosing which points to show (when there are many) and for their horizontal jitter. |
0
|
Returns:
| Type | Description |
|---|---|
tuple
|
The matplotlib |
plot_isi_cv(run, begin=0.0, end=None, *, populations=None, area=None, min_spikes=UNSET, style=None, config=None, save_path=None, show=True)
¶
Plot the distribution of single-neuron ISI CVs of each population.
A dashed line at CV = 1 marks Poisson-like firing, the "asynchronous irregular" state cortical models are usually compared with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
min_spikes
|
int
|
The fewest spikes a neuron needs; by default as set in |
UNSET
|
style
|
PlotStyle | None
|
The shared plot style (:class: |
None
|
config
|
IsiCvPlotConfig | None
|
This plot's settings from the analysis file; its defaults otherwise. |
None
|
save_path
|
str | Path | None
|
Where to save the figure; not saved if |
None
|
show
|
bool
|
Open the figure in a window. |
True
|
Returns:
| Type | Description |
|---|---|
tuple
|
The matplotlib |
plot_power_spectrum(run, begin=0.0, end=None, *, populations=None, area=None, bin_size=1.0, method=UNSET, method_kwargs=UNSET, xlim=UNSET, style=None, config=None, save_path=None, show=True)
¶
Plot the power spectrum of each population, in a grid: one row per layer,
one column per cell type (L23_E next to L23_I, L4_E below).
The zero-frequency bin is never plotted: after removing the mean it carries no information, and it would break a log frequency axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run
|
A finished run, from :func: |
required | |
begin
|
float
|
The time window in ms; |
0.0
|
end
|
float
|
The time window in ms; |
0.0
|
populations
|
Sequence[str] | None
|
Population names to use; by default all of them (or those of |
None
|
area
|
str | None
|
For a multi-area network, use only the populations of this area. |
None
|
bin_size
|
float
|
The bin width of the rate signal, in ms. |
1.0
|
method
|
str
|
The estimator, as in :func: |
UNSET
|
method_kwargs
|
str
|
The estimator, as in :func: |
UNSET
|
xlim
|
tuple[float, float] | None
|
The frequency range to show, e.g. |
UNSET
|
style
|
PlotStyle | None
|
The shared plot style (:class: |
None
|
config
|
PowerSpectrumPlotConfig | None
|
This plot's settings from the analysis file; its defaults otherwise. |
None
|
save_path
|
str | Path | None
|
Where to save the figure; not saved if |
None
|
show
|
bool
|
Open the figure in a window. |
True
|
Returns:
| Type | Description |
|---|---|
tuple
|
The matplotlib |
register_plot(name, plot_fn, *, config_cls=None, filename=None)
¶
Add a plot to microcircuit-lab run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The plot's name: its |
required |
plot_fn
|
Callable[..., object]
|
The plot function. It takes the run first (from |
required |
config_cls
|
type[FrozenConfig] | None
|
Optional pydantic model for the plot's settings, with an |
None
|
filename
|
str | None
|
The figure's file name; |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the name is already taken, so a project's plot can't replace a
bundled one, or if |