This is a collection of useful tools and functions mainly for use with Brian2. Or other spiking network software or hardware.


Functions in this module convert data from or to Brian2 compatible formats. In particular, there are functions to convert and process data from Dynamic Vision Sensors (DVSs), which are event-based cameras. If you want to learn more about a DVS checkout the original publication


This provides functions that are used by the TeiliNetwork class to allow running the network several times with different parameters without recompiling. This is in particular useful if you have small networks and you would like to do parameter optimization. Have a look at sequence_learning_standalone_tutorial as an example for how to use it.

# net is built once = standaloneParams)
# and can then be run several times with different parameters without recompilation,standaloneParams = standaloneParams)


These functions to compute different distance measures are mainly there to provide an easy to use cpp implementation for Brian2 cpp code generation. They are used by the synaptic kernel functions, but can also be added into any Brian2 string. Make sure to add the functions you use to the namespace of the respective groups as follows:

from import function
group.namespace['functionname_used_in_string'] = function


Functions that convert 1d indices to x, y coordinates and vice versa including cpp implementation. As Brian2 uses 1d indexing for neurons, it is necessary to convert 1d to 2d indices every so often when e.g. generating synapses. Numpy provides a good API for that, which we use here, but we also add a cpp implementation so the functions can be used in standalone mode.


WIP Live plotting and changing of parameters during numpy based simulation could be done like this.


Functions that didn’t fit in any of the other categories so far.


Will be deprecated soon once implemented into the visualizer. Provides 2d plotting functionality (video plot and gif generation)


Will be deprecated soon once implemented into the visualizer?


Probability density functions with cpp implementation.

This is e.g. a plot of a 1d Gaussian:

import matplotlib.pyplot as plt
dx = 0.1
normal1drange = np.arange(-10, 10, dx)
gaussian = [normal1d_density(x, 0, 1, True) for x in normal1drange]
print(np.sum(gaussian) * dx)

plt.plot(normal1drange, gaussian)


This module provides functions to sample from a random distribution, e.g. for random initialization of weights. All functions in should be callable in a similar way as rand() or randn() that are provided by Brian2.

Here is an example how this works in standalone mode:

from import Rand_gamma, Randn_trunc

n_samples = 10000
standaloneDir = os.path.expanduser('~/gamma_standalone')
set_device('cpp_standalone', directory=standaloneDir, build_on_run=True)

ng = NeuronGroup(n_samples, '''
testvar : 1
testvar2 : 1''', name = 'ng_test')

ng.namespace.update({'rand_gamma': Rand_gamma(4.60, -10750.0),
                     'randn_trunc': Randn_trunc(-1.5,1.5)

ng.testvar = 'rand_gamma()'
ng.testvar2 = '5*randn_trunc()'

run(10 * ms)

plt.hist(ng.testvar, 50, histtype='step')

plt.hist(ng.testvar2, 50, histtype='step')


Functions that generate a random walk. E.g. as artificial input.


To understand the structure in spiking activity of a network or more specifically the structure in the spike rasterplots of a neuronal population we need to sort the neuronal indicies. But also if we want to understand the strucuture of a learned weight matrix we need to be able to sort this matrix. This set of tools allows the user to sort a given weight matrix according to some similarity measure, such as euclidean distance. The class returns a list of permutated indices which can be used to sort a spike rasterplot or the weight matrix itself, before it is being displayed. However, the sorting algorithm is completely agnostic to the similarity measure. It connects each node with maximum two edges and constructs a directed graph. This is similar to the travelling salesman problem.


In order to use this class you need to initialize it either without a filename:

from import SortMatrix
import numpy as np
matrix = np.random.randint((49, 49))
obj = SortMatrix(nrows=49, matrix=matrix)

or instead of using a matrix you can also specify a path to a stored matrix:

filename = '/path/to/your/matrix.npy'
obj = SortMatrix(nrows=49, filename=filename)


The idea is to generate inputs based on a function instead of having to use a fixed spikegenerator that is filled before the simulation. This avoids having to read large datafiles and makes generation of input easier.

Use it as follows (also teili groups and network can be used):

import matplotlib.pyplot as plt

from brian2 import SpikeMonitor, Network, prefs, ms

from import StimulusSpikeGenerator
from teili import normal2d_density, Plotter2d = 'numpy'

nrows = 80
ncols = 80

# Create a moving Gaussian with increasing sigma
# the update that happens every dt is given in the trajectory_eq
# the center coordinates move 5 to the right and 2 upwards every dt
# the sigma is increased by 0.1 in both directions every dt
trajectory_eq = '''
                mu_x = (mu_x + 5)%nrows
                mu_y = (mu_y + 2)%nrows
                sigma_x += 0.1
                sigma_y += 0.1

stimgen = StimulusSpikeGenerator(
    nrows, ncols, dt=50 * ms, trajectory_eq=trajectory_eq, amplitude=200,
    spike_generator='poisson', pattern_func=normal2d_density,
    mu_x=40.0, mu_y=40.0, sigma_x=1.0, sigma_y=1.0, rho=0.0, normalized=False)

poissonspmon = SpikeMonitor(stimgen, record=True)

net = Network()
net.add((stimgen, poissonspmon)) * ms)

plt.plot(poissonspmon.t, poissonspmon.i, ".")

plotter2d = Plotter2d(poissonspmon, (nrows, ncols))
imv = plotter2d.plot3d(plot_dt=10 * ms, filtersize=20 * ms)


This module provides functions that can be used for synaptic connectivity kernels (generate weight matrices). E.g. Gaussian, Mexican hat, Gabor with different dimensionality, also using different distance metrics. In order to also use them with C++ code generation, all functions have a cpp implementation given by the @implementation decorator.