raster_plot

Contents

raster_plot#

class braintools.metric.raster_plot(sp_matrix, times)#

Extract spike times and neuron indices for raster plot visualization.

A raster plot displays the spiking activity of a population of neurons over time, where each row represents a neuron and each dot or line indicates a spike occurrence. This function extracts the necessary data (neuron indices and corresponding spike times) from a spike matrix to create such visualizations.

Parameters:
  • sp_matrix (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Spike matrix with shape (n_time_steps, n_neurons) where non-zero values indicate spike occurrences. Each element sp_matrix[t, i] represents the spike activity of neuron i at time step t.

  • times (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Time points corresponding to each row of the spike matrix with shape (n_time_steps,). These represent the actual time values for each time step in the simulation.

Returns:

  • neuron_indices (numpy.ndarray) – Array of neuron indices where spikes occurred. Each index corresponds to a neuron that fired at the corresponding time in spike_times.

  • spike_times (numpy.ndarray) – Array of spike times corresponding to each spike event. These are the actual time values when spikes occurred, extracted from the times array.

Examples

Create a simple spike matrix and extract raster data:

>>> import numpy as np
>>> import braintools as braintools
>>> # Create sample spike data (3 neurons, 10 time steps)
>>> spikes = np.array([
...     [0, 1, 0],  # t=0: neuron 1 spikes
...     [1, 0, 0],  # t=1: neuron 0 spikes
...     [0, 0, 1],  # t=2: neuron 2 spikes
...     [0, 1, 1],  # t=3: neurons 1,2 spike
...     [0, 0, 0],  # t=4: no spikes
... ])
>>> times = np.array([0.0, 0.1, 0.2, 0.3, 0.4])  # Time in seconds
>>> neuron_ids, spike_times = braintools.metric.raster_plot(spikes, times)
>>> print("Neuron indices:", neuron_ids)
>>> print("Spike times:", spike_times)

Use the results for matplotlib visualization:

>>> import matplotlib.pyplot as plt
>>> neuron_ids, spike_times = braintools.metric.raster_plot(spikes, times)
>>> plt.scatter(spike_times, neuron_ids, marker='|', s=50)
>>> plt.xlabel('Time (s)')
>>> plt.ylabel('Neuron Index')
>>> plt.title('Raster Plot')
>>> plt.show()

Notes

The function uses numpy.where to find non-zero elements in the spike matrix, making it efficient for sparse spike data. The returned arrays have the same length and can be directly used for scatter plots or other visualizations.

See also

braintools.metric.firing_rate

Calculate population firing rates

matplotlib.pyplot.scatter

For creating raster plot visualizations