Spike Time Analysis with Pandas and Matplotlib
Authors
In this notebook, we will be focusing on the spike timing of neurons from the Steinmetz et al, 2019 paper. With the rich dataset that they collected during their experiments many analyses are possible that can link the timing of spikes to behavioral or experimental events.
Analysis Goals
We will visualize spiking events, examine their relationship with key experimental variables, and compare activity patterns across distinct brain regions.
Learning Goals
We will be using the Xarray Python package to load the data which is stored in netCDF format (i.e. .nc extension), as well as the Pandas and matplotlib libraries for analysis and visualization.
Setup
Import Libraries
import xarray as xr
import pandas as pd
import matplotlib.pyplot as plt
import owncloud
from pathlib import PathDownload the dataset
Path('data').mkdir(exist_ok=True, parents=True)
owncloud.Client.from_public_link('https://uni-bonn.sciebo.de/s/gBtjQ4TaXGEPyao', folder_password="ibots").get_file('/', f'data/steinmetz_2017-01-08_Muller.nc')TrueSection 1: Spike Timing Data as Pandas DataFrame
In this section we’ll load the spike timing data as a Pandas DataFrame and explore some of the properties of the data to get a better understanding of it. As you will see later, each spike is characterised by 4 pieces of information:
spike_id: The identifier for that spikespike_time: The time at which the spike occurredspike_cell: The cell to which the spike belongsspike_trial: The trial in which the spike occurred
| Code | Description |
|---|---|
dset.to_dataframe() |
Convert an Xarray dataset to a Pandas DataFrame. |
df.reset_index() |
Reset the index of a DataFrame. |
df[df['column_B'] == 42] |
Filter a DataFrame where the values of column_B are 42. |
df['column_A'].nunique() |
Count the number of unique values in column_A of a DataFrame. |
df.groupby('column_B')['column_A'].nunique() |
Group a DataFrame according to values of column_B and count the number of unique values in column_A. |
df.max() |
Find the maximum value of a DataFrame. |
df.min() |
Find the minimum value of a DataFrame. |
df.idxmax() |
Find the index where the maximum value occurs in a DataFrame. |
df.idxmin() |
Find the index where the minimum value occurs in a DataFrame. |
plt.hist(data_values) |
Plot a histogram of data_values using Matplotlib. |
Run the cell below to load and display the dataset using Xarray.
Exercises
dset = xr.load_dataset('data/steinmetz_2017-01-08_Muller.nc')
dset<xarray.Dataset> Size: 124MB
Dimensions: (trial: 261, time: 250, cell: 1268,
waveform_component: 3, sample: 82, probe: 384,
brain_area_lfp: 5, spike_id: 1836009)
Coordinates:
* trial (trial) int32 1kB 1 2 3 4 5 6 ... 257 258 259 260 261
* time (time) float64 2kB 0.01 0.02 0.03 0.04 ... 2.48 2.49 2.5
* cell (cell) int32 5kB 1 2 3 4 5 ... 1264 1265 1266 1267 1268
* waveform_component (waveform_component) int32 12B 1 2 3
* probe (probe) int32 2kB 1 2 3 4 5 6 ... 380 381 382 383 384
* brain_area_lfp (brain_area_lfp) <U5 100B 'CA1' 'DG' 'LP' 'PO' 'VISam'
* spike_id (spike_id) int32 7MB 1 2 3 4 ... 1836007 1836008 1836009
Dimensions without coordinates: sample
Data variables: (12/31)
contrast_left (trial) int8 261B 50 0 100 0 50 0 ... 100 0 100 0 100 0
contrast_right (trial) int8 261B 0 50 25 100 50 50 ... 100 50 100 25 25
gocue (trial) float64 2kB 0.9828 0.902 1.114 ... nan nan nan
stim_onset (trial) float64 2kB 0.5 0.5 0.5 0.5 ... 0.5 0.5 0.5 0.5
feedback_type (trial) float64 2kB 1.0 1.0 1.0 1.0 ... nan nan nan nan
feedback_time (trial) float64 2kB 1.272 1.104 1.402 ... nan nan nan
... ...
waveform_w (cell, sample, waveform_component) float32 1MB 0.0 .....
waveform_u (cell, waveform_component, probe) float32 6MB 0.0 ......
lfp (brain_area_lfp, trial, time) float64 3MB -27.6 ... 0...
spike_time (spike_id) float32 7MB 2.363 2.385 ... 1.651 0.5142
spike_cell (spike_id) uint32 7MB 1 1 1 1 1 ... 1268 1268 1268 1268
spike_trial (spike_id) uint32 7MB 1 1 2 2 2 ... 205 205 205 213 252
Attributes:
session_date: 2017-01-08
mouse: Muller
stim_onset: 0.5
bin_size: 0.01- trial: 261
- time: 250
- cell: 1268
- waveform_component: 3
- sample: 82
- probe: 384
- brain_area_lfp: 5
- spike_id: 1836009
- trial(trial)int321 2 3 4 5 6 ... 257 258 259 260 261
array([ 1, 2, 3, ..., 259, 260, 261], dtype=int32)
- time(time)float640.01 0.02 0.03 ... 2.48 2.49 2.5
array([0.01, 0.02, 0.03, ..., 2.48, 2.49, 2.5 ])
- cell(cell)int321 2 3 4 5 ... 1265 1266 1267 1268
array([ 1, 2, 3, ..., 1266, 1267, 1268], dtype=int32)
- waveform_component(waveform_component)int321 2 3
array([1, 2, 3], dtype=int32)
- probe(probe)int321 2 3 4 5 6 ... 380 381 382 383 384
array([ 1, 2, 3, ..., 382, 383, 384], dtype=int32)
- brain_area_lfp(brain_area_lfp)<U5'CA1' 'DG' 'LP' 'PO' 'VISam'
array(['CA1', 'DG', 'LP', 'PO', 'VISam'], dtype='<U5')
- spike_id(spike_id)int321 2 3 4 ... 1836007 1836008 1836009
array([ 1, 2, 3, ..., 1836007, 1836008, 1836009], dtype=int32)
- contrast_left(trial)int850 0 100 0 50 0 ... 0 100 0 100 0
array([ 50, 0, 100, 0, 50, 0, 0, 100, 0, 100, 50, 25, 25, 25, 100, 100, 0, 50, 0, 100, 50, 0, 0, 100, 50, 100, 0, 50, 50, 100, 0, 0, 0, 25, 100, 50, 0, 0, 25, 50, 50, 0, 100, 0, 100, 0, 50, 0, 0, 0, 50, 100, 100, 25, 100, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 100, 100, 0, 0, 50, 50, 0, 100, 100, 0, 0, 0, 100, 0, 100, 25, 0, 25, 100, 0, 0, 0, 100, 100, 0, 100, 0, 25, 25, 25, 50, 50, 0, 0, 25, 0, 0, 25, 100, 50, 0, 50, 0, 100, 25, 50, 25, 25, 50, 50, 0, 0, 0, 100, 0, 0, 100, 100, 100, 100, 100, 50, 0, 0, 0, 0, 50, 0, 0, 50, 0, 100, 100, 100, 100, 100, 100, 100, 100, 0, 0, 0, 100, 0, 100, 100, 100, 0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 100, 0, 0, 0, 0, 0, 0, 100, 100, 0, 0, 100, 100, 0, 0, 0, 0, 0, 0, 0, 100, 100, 0, 100, 100, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 0, 0, 100, 0, 0, 0, 100, 0, 0, 0, 100, 0, 0, 0, 100, 100, 100, 0, 0, 100, 100, 100, 100, 0, 100, 0, 0, 100, 0, 0, 0, 0, 100, 0, 100, 0, 100, 100, 100, 0, 0, 0, 0, 0, 0, 0, 0, 100, 0, 100, 0, 100, 0], dtype=int8) - contrast_right(trial)int80 50 25 100 50 ... 100 50 100 25 25
array([ 0, 50, 25, 100, 50, 50, 50, 0, 0, 25, 25, 100, 100, 100, 0, 50, 0, 50, 100, 0, 0, 0, 0, 25, 0, 0, 100, 0, 100, 50, 0, 50, 100, 25, 0, 0, 0, 0, 25, 0, 0, 50, 50, 0, 0, 50, 100, 0, 0, 0, 25, 0, 25, 50, 50, 25, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 25, 0, 0, 50, 100, 100, 0, 50, 0, 50, 50, 0, 100, 100, 25, 0, 50, 0, 50, 0, 0, 25, 0, 25, 50, 50, 100, 100, 100, 50, 100, 0, 0, 0, 50, 0, 100, 50, 50, 0, 0, 100, 0, 50, 50, 100, 50, 0, 0, 50, 0, 100, 50, 100, 50, 0, 0, 0, 0, 0, 50, 50, 0, 0, 50, 100, 25, 50, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 100, 25, 0, 50, 25, 100, 50, 100, 50, 100, 0, 100, 25, 100, 100, 50, 50, 25, 100, 25, 100, 50, 50, 100, 100, 25, 25, 100, 100, 25, 100, 25, 100, 25, 100, 100, 100, 100, 100, 100, 50, 50, 25, 25, 25, 25, 100, 100, 100, 0, 100, 100, 100, 50, 50, 100, 100, 100, 100, 100, 100, 100, 0, 100, 100, 50, 100, 100, 100, 50, 0, 25, 25, 100, 100, 50, 0, 0, 50, 100, 0, 100, 100, 50, 100, 100, 100, 100, 100, 100, 0, 100, 25, 100, 0, 50, 100, 100, 100, 100, 100, 100, 25, 50, 100, 50, 100, 25, 25], dtype=int8) - gocue(trial)float640.9828 0.902 1.114 ... nan nan nan
array([0.98281551, 0.90201422, 1.11361759, 0.68881083, 1.01761606, 0.61880972, 1.02881624, 1.01241598, 1.11201757, 0.68121071, 0.87641382, 0.64841019, 1.13801798, 0.6804107 , 0.79521252, 0.58040911, 0.9824155 , 0.91681446, 0.52040815, 1.05841671, 0.97761543, 1.13041786, 0.48320756, 0.825213 , 0.84241328, 0.8500134 , 0.60680953, 0.91081436, 1.08001706, 0.64681016, 0.99681573, 0.86521364, 0.67241057, 0.86241359, 1.11801766, 1.17161851, 0.76081198, 0.81041277, 0.80161263, 0.8564135 , 0.57040895, 0.91481443, 0.8940141 , 0.77081214, 0.73201152, 0.91881449, 1.04561651, 0.70041102, 0.83481315, 0.55840876, 0.74601174, 0.81841289, 0.79281249, 1.14281806, 0.99081564, 0.71681128, 0.80481268, 0.72681144, 1.00681589, 0.84001324, 0.79081245, 1.09041722, 0.53360836, 1.03521634, 0.68641079, 0.59200929, 0.7808123 , 0.57240898, 0.50400789, 1.11321758, 0.87281376, 0.71241121, 0.50080784, 1.01281599, 0.55960878, 0.95001499, 0.8064127 , 0.92001451, 1.16201836, 1.17601858, 0.72681144, 0.98481554, 0.91761447, 0.9004142 , 0.74841178, 0.89841417, 0.79201247, 1.11041754, 1.10201741, 0.91881449, 1.18281869, 0.61080959, 0.99401569, 1.15561826, 0.92001451, 0.80441267, 0.83001308, 0.62560983, 0.71121119, 0.76161199, ... nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - stim_onset(trial)float640.5 0.5 0.5 0.5 ... 0.5 0.5 0.5 0.5
array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]) - feedback_type(trial)float641.0 1.0 1.0 1.0 ... nan nan nan nan
array([ 1., 1., 1., 1., -1., 1., 1., 1., 1., 1., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -1., 1., -1., -1., 1., -1., 1., 1., -1., 1., 1., 1., 1., 1., 1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -1., 1., 1., 1., 1., 1., -1., 1., 1., -1., 1., -1., 1., 1., 1., 1., -1., 1., 1., -1., 1., 1., -1., 1., 1., -1., 1., 1., 1., -1., 1., -1., 1., -1., 1., 1., 1., 1., -1., 1., -1., -1., -1., 1., 1., -1., 1., 1., 1., -1., 1., 1., 1., -1., 1., 1., 1., 1., -1., 1., 1., 1., 1., 1., 1., 1., 1., -1., 1., -1., -1., -1., -1., -1., 1., -1., -1., 1., 1., -1., 1., -1., -1., -1., 1., -1., -1., -1., -1., -1., -1., -1., -1., nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - feedback_time(trial)float641.272 1.104 1.402 ... nan nan nan
array([1.2716201 , 1.10361743, 1.40242219, 1.05281662, 1.19361886, 0.78601238, 1.22001928, 1.18601874, 2.65084205, 1.23561953, 2.38923789, 2.16163427, 2.64284192, 1.48682353, 0.95281503, 0.76961212, 2.52003997, 1.07161692, 1.03761638, 1.202419 , 1.25401982, 2.66804232, 2.02203204, 0.98681557, 1.02321615, 1.10241741, 0.90321424, 1.2208193 , 1.20361902, 1.05441665, 2.53524021, 2.02003201, 0.80521268, 1.09641732, 1.48802355, 2.68484259, 1.4724233 , 2.37003758, 2.26323588, 1.45322299, 0.98761559, 2.43923868, 1.05201661, 2.31203666, 0.92121453, 1.6544262 , 1.32042088, 2.2392355 , 0.98041547, 2.09723324, 1.13641795, 2.13643386, 1.05321663, 1.48642352, 1.13681796, 1.80362857, 1.00241582, 0.85281344, 1.50482382, 2.38003774, 2.32843692, 2.62484163, 2.07043281, 2.57324081, 0.88481395, 2.13203379, 0.93601477, 1.51122392, 2.0444324 , 1.39562208, 2.41283826, 0.87001371, 0.62000974, 1.13681796, 0.9756154 , 2.50443972, 1.17001849, 1.57122487, 1.45322299, 1.40522223, 2.23763547, 2.52404003, 1.35482143, 1.705227 , 0.87001371, 1.45402301, 1.0200161 , 2.63764184, 2.6412419 , 2.42963853, 1.95443097, 2.13403383, 2.53804025, 2.70244287, 1.37122169, 1.18801877, 2.33563703, 0.97201534, 2.22243523, 2.26723595, ... nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - response_type(trial)float641.0 -1.0 1.0 -1.0 ... nan nan nan
array([ 1., -1., 1., -1., 1., -1., -1., 1., 0., 1., 0., 0., 0., -1., 1., 1., 0., 1., -1., 1., 1., 0., 0., 1., 1., 1., -1., 1., -1., 1., 0., -1., -1., 1., 1., 0., -1., 0., -1., 1., 1., 0., 1., 0., 1., -1., -1., 0., 1., 0., 1., 1., 1., -1., 1., -1., 1., -1., 1., 0., 0., 0., 0., 0., -1., 0., -1., 1., 0., 1., 0., 1., 1., 1., 1., 0., 1., 1., -1., 1., 0., 0., -1., 1., 1., -1., 1., 0., 0., 0., 1., 0., 0., 0., 1., 1., 0., 1., 0., 0., 0., -1., 1., 1., 0., 0., 1., 0., 0., -1., 1., 0., 0., 1., -1., 1., 1., 1., -1., -1., 1., 1., -1., 0., -1., 0., -1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - response_time(trial)float641.233 1.067 1.367 ... nan nan nan
array([1.23325695, 1.06664772, 1.36663277, 1.01682411, 1.18343498, 0.75081889, 1.18407822, 1.1504518 , 2.6205886 , 1.2002919 , 2.38187835, 2.15428869, 2.64432937, 1.45050085, 0.9171474 , 0.73342794, 2.48924666, 1.03432402, 1.00080032, 1.16689261, 1.21754183, 2.63825895, 1.99153645, 0.95081153, 0.98443178, 1.06679184, 0.86693035, 1.18351134, 1.16814301, 1.01797613, 2.50457237, 1.98396289, 0.76711468, 1.08381117, 1.45042011, 2.68324477, 1.46748739, 2.33258373, 2.25125259, 1.41746282, 0.95149875, 2.42123834, 1.01708905, 2.28154807, 0.88504477, 1.61791026, 1.28498864, 2.20732461, 0.96840371, 2.06502604, 1.10073198, 2.10071714, 1.01842349, 1.45169833, 1.10111159, 1.76781636, 0.96733613, 0.81781407, 1.50111591, 2.34857407, 2.29739687, 2.59399567, 2.03915574, 2.54225929, 0.88527423, 2.10096715, 0.90137733, 1.50170189, 2.01170273, 1.3853986 , 2.38123109, 0.83565853, 0.58540426, 1.10212412, 0.96864898, 2.46722514, 1.13463195, 1.56758403, 1.41791287, 1.36844536, 2.23588244, 2.49250167, 1.31865578, 1.70095733, 0.83549404, 1.41880362, 0.9855619 , 2.63317287, 2.6088659 , 2.42406801, 1.91796962, 2.11991166, 2.50061523, 2.66700876, 1.33555777, 1.15132906, 2.33538814, 0.93516847, 2.21560372, 2.2658931 , ... nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - reaction_type(trial)float641.0 -1.0 1.0 -1.0 ... nan nan nan
array([ 1., -1., 1., -1., 1., -1., -1., 1., 0., 1., 1., 0., -1., -1., 1., 1., 0., 1., -1., 1., 1., 0., 0., 1., 1., 1., -1., 1., -1., 1., 0., -1., -1., 1., 1., 0., 1., 0., 1., 1., 1., 0., 1., 1., 1., -1., -1., 0., 1., 0., 1., 0., 1., 1., 1., -1., 1., -1., 1., 0., 0., 1., 0., 0., -1., 0., -1., 1., 0., 1., 0., 1., 1., 1., 1., 1., 1., 1., -1., 1., 0., 0., 1., 1., 1., -1., 1., 0., 0., 0., 1., 0., 0., 0., 1., 1., 0., 1., 0., 0., 0., -1., 1., 1., 0., 0., 1., -1., 0., -1., 1., 0., 0., 1., 1., 1., 1., 1., -1., -1., 1., 1., -1., 0., -1., 0., -1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - reaction_time(trial)float64280.0 200.0 190.0 ... nan nan nan
array([ 280., 200., 190., 250., 190., 220., 170., 210., inf, 1080., 200., inf, 160., 790., 200., 160., inf, 220., 360., 210., 200., inf, inf, 180., 400., 190., 330., 190., 170., 370., inf, 1860., 340., 290., 200., inf, 1080., inf, 260., 180., 420., inf, 210., 1340., 170., 1510., 290., inf, 850., inf, 290., inf, 300., 630., 360., 1600., 330., 610., 1340., inf, inf, 680., inf, inf, 760., inf, 260., 1380., inf, 1270., inf, 290., 240., 210., 830., 1370., 360., 1460., 250., 420., inf, inf, 1050., 1580., 290., 1300., 240., inf, inf, inf, 230., inf, inf, inf, 1200., 200., inf, 820., inf, inf, inf, 850., 1080., 1450., inf, inf, 610., 500., inf, 440., 340., inf, inf, 580., 1240., 280., 750., 1900., 1030., 1190., 1300., 1280., 1660., inf, 1650., inf, 200., inf, inf, inf, inf, inf, 1610., inf, inf, inf, inf, inf, 1250., inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - prev_reward(trial)float64-10.0 -2.683 -4.467 ... nan nan nan
array([-10. , -2.68320616, -4.46664111, -2.28302609, -3.54964934, -2.61542626, -2.44923282, -2.38237932, -3.25001901, -5.94554849, -2.29936458, -2.30139696, -3.76100622, -6.68861991, -2.44996201, -2.63290988, -3.24943622, -2.9264403 , -2.58213431, -3.61647394, -2.49876651, -2.44971731, -2.29382032, -2.2917325 , -2.63204627, -2.43282339, -2.51646596, -2.6495264 , -2.33214539, -2.3151132 , -2.38247877, -2.31190506, -2.29890605, -2.38293624, -2.61584849, -2.51604379, -3.03244697, -2.48297626, -2.61629581, -2.3992222 , -2.39819868, -2.39935536, -2.44483988, -2.51616796, -2.36732669, -2.34820746, -2.66535869, -2.7654766 , -2.60835281, -2.39805064, -2.60224901, -2.33212343, -2.3649547 , -2.43163219, -2.61516717, -2.39814488, -2.49945233, -2.51552008, -2.43223844, -2.38094665, -2.33550125, -2.61828214, -2.62288812, -3.29412991, -2.65662422, -2.46437986, -2.66550948, -2.28187411, -2.63196467, -2.65397229, -2.31506107, -2.38444553, -2.58079662, -2.397844 , -2.58113527, -2.4810067 , -5.71650586, -2.58182798, -2.66528411, -2.64815261, ... nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]) - active_trials(trial)boolTrue True True ... False False
array([ True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False]) - wheel(trial, time)int81 0 0 1 0 0 0 ... -1 -2 -1 -2 -1 -1
array([[ 1, 0, 0, ..., 1, 1, 1], [ 0, 0, 0, ..., 0, 0, 1], [ 0, 0, 0, ..., -1, 0, -1], ..., [ 0, 0, 0, ..., 0, 0, 0], [ 0, 0, 0, ..., 0, 0, 0], [ 0, 0, 0, ..., -2, -1, -1]], dtype=int8) - licks(trial, time)int80 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 1, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], dtype=int8) - pupil_x(trial, time)float64-0.7537 -0.7744 ... 0.8159 0.8239
array([[-0.7536845 , -0.77438827, -0.74893892, ..., 0.41276153, 0.53200778, 0.6059435 ], [-0.1872646 , -0.13526671, -0.15770239, ..., -0.3104277 , -0.22372295, -0.24695587], [ 0.7839589 , 0.79603282, 0.78007488, ..., 0.15773712, 0.05718518, 0.02599552], ..., [-1.00967831, -1.01527981, -1.05219656, ..., -0.95835791, -0.92747762, -0.92222346], [ 0.65306241, 0.67567605, 0.7031893 , ..., 0.46709787, 0.48264617, 0.47615427], [ 0.46901131, 0.49572037, 0.47792664, ..., 0.86579173, 0.81592479, 0.82394872]]) - pupil_y(trial, time)float641.717 1.565 ... -0.4639 -0.4615
array([[ 1.71674839, 1.5651633 , 1.60562544, ..., -0.07542674, 0.11406018, 0.05512887], [ 0.35805268, 0.30677451, 0.30322713, ..., 0.17039691, 0.34066004, 0.22467543], [-0.76404043, -0.90662802, -0.73351687, ..., -0.42070803, -0.50293623, -0.54970812], ..., [ 0.02356526, -0.03004184, 0.03595452, ..., -0.00841479, 0.08427229, 0.11527266], [ 0.21059782, 0.30219291, 0.25346585, ..., 0.49355713, 0.45671945, 0.46818451], [ 0.41731699, 0.46450419, 0.47029483, ..., -0.52897732, -0.46387761, -0.46153118]]) - pupil_area(trial, time)float642.636 2.622 ... 0.07115 0.06507
array([[ 2.63552359, 2.62229554, 2.62128879, ..., 0.13787576, 0.1320759 , 0.13358063], [ 0.10406219, 0.10456148, 0.11775878, ..., 0.14647881, 0.14620532, 0.15638102], [ 0.18677008, 0.19012276, 0.18462697, ..., 0.1440697 , 0.13889101, 0.15955417], ..., [ 0.03934839, 0.04297402, 0.0293903 , ..., 0.01318427, 0.01130864, 0.01268708], [ 0.00732134, 0.01235198, 0.014924 , ..., -0.01273482, -0.00921613, -0.01199399], [-0.00924377, -0.00442913, -0.00842242, ..., 0.07160288, 0.07115257, 0.06506964]]) - face(trial, time)float640.7588 0.7588 ... 1.336 1.498
array([[0.75884192, 0.75884192, 0.91051149, ..., 2.23058024, 2.23058024, 2.23058024], [1.96034162, 1.96034162, 1.87092079, ..., 2.46426008, 2.46426008, 2.46426008], [1.12739405, 1.12739405, 1.29487283, ..., 1.78891053, 1.78891053, 1.95638932], ..., [0.53701899, 0.53701899, 0.53701899, ..., 0.04594551, 0.02420785, 0.02420785], [0.51725748, 0.51725748, 0.51725748, ..., 0.22379908, 0.19069855, 0.19069855], [0.19613297, 0.03260649, 0.03260649, ..., 1.336372 , 1.336372 , 1.49841636]]) - spike_rate(cell, trial, time)int80 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0
array([[[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], [[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 1, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [1, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], [[0, 0, 0, ..., 0, 1, 0], [0, 0, 0, ..., 0, 1, 0], [0, 0, 0, ..., 0, 0, 0], ..., ... ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], [[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], [[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]]], dtype=int8) - trough_to_peak(cell)int815 14 14 17 4 19 ... 13 25 13 24 24
array([15, 14, 14, ..., 13, 24, 24], dtype=int8)
- ccf_ap(cell)float647.172e+03 7.063e+03 ... nan nan
array([7171.5, 7062.6, 7208.6, ..., nan, nan, nan])
- ccf_dv(cell)float643.336e+03 3.639e+03 ... nan nan
array([3336.3, 3638.8, 3185. , ..., nan, nan, nan])
- ccf_lr(cell)float644.226e+03 4.233e+03 ... nan nan
array([4225.5, 4233.2, 4176.9, ..., nan, nan, nan])
- brain_area(cell)<U5'PO' 'PO' 'PO' ... 'root' 'root'
array(['PO', 'PO', 'PO', ..., 'root', 'root', 'root'], dtype='<U5')
- brain_groups(cell)<U13'thalamus' 'thalamus' ... 'root'
array(['thalamus', 'thalamus', 'thalamus', ..., 'root', 'root', 'root'], dtype='<U13') - waveform_w(cell, sample, waveform_component)float320.0 0.0 0.0 ... 0.04712 -0.01475
array([[[ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0. , 0. , 0. ], ..., [-0.1809352 , 0.01451338, -0.05218167], [-0.1448934 , 0.01549385, -0.04766656], [-0.11991666, 0.00905554, -0.04877429]], [[ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0. , 0. , 0. ], ..., [-0.0396658 , 0.0158161 , -0.04555661], [-0.0164031 , 0.01162716, -0.03673343], [ 0.009633 , 0.01056049, -0.03005127]], [[ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0. , 0. , 0. ], ..., ... ..., [-0.12877208, 0.09644453, 0.01988335], [-0.08726224, 0.08256274, 0.00930735], [-0.0709131 , 0.08002122, -0.01226503]], [[ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0. , 0. , 0. ], ..., [-0.15919374, 0.05030607, -0.01431272], [-0.1401487 , 0.04486557, -0.01248888], [-0.1122384 , 0.0471202 , -0.01475146]], [[ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0. , 0. , 0. ], ..., [-0.15919374, 0.05030607, -0.01431272], [-0.1401487 , 0.04486557, -0.01248888], [-0.1122384 , 0.0471202 , -0.01475146]]], dtype=float32) - waveform_u(cell, waveform_component, probe)float320.0 0.0 -0.002719 ... 0.0 0.0 0.0
array([[[ 0. , 0. , -0.00271877, ..., 0. , 0. , 0. ], [ 0. , 0. , -0.0087151 , ..., 0. , 0. , 0. ], [ 0. , 0. , -0.03010223, ..., 0. , 0. , 0. ]], [[-0.0016242 , -0.00687359, -0.00866863, ..., 0. , 0. , 0. ], [-0.01843663, -0.02093229, 0.01714569, ..., 0. , 0. , 0. ], [-0.0624942 , -0.0517174 , -0.19163765, ..., 0. , 0. , 0. ]], [[ 0. , 0.00628563, 0.00517994, ..., 0. , 0. , 0. ], [ 0. , 0.00886862, -0.02077066, ..., 0. , 0. , 0. ], [ 0. , 0.00623638, -0.02751336, ..., 0. , 0. , 0. ]], ... [[ 0. , 0. , 0. , ..., 0. , 0. , 0. ], [ 0. , 0. , 0. , ..., 0. , 0. , 0. ], [ 0. , 0. , 0. , ..., 0. , 0. , 0. ]], [[ 0. , 0. , 0. , ..., 0. , 0. , 0. ], [ 0. , 0. , 0. , ..., 0. , 0. , 0. ], [ 0. , 0. , 0. , ..., 0. , 0. , 0. ]], [[ 0. , 0. , 0. , ..., 0. , 0. , 0. ], [ 0. , 0. , 0. , ..., 0. , 0. , 0. ], [ 0. , 0. , 0. , ..., 0. , 0. , 0. ]]], dtype=float32) - lfp(brain_area_lfp, trial, time)float64-27.6 -9.929 -13.3 ... 0.707 0.1221
array([[[-2.75952381e+01, -9.92857143e+00, -1.33035714e+01, ..., 3.65476190e+00, 1.82142857e+00, -2.80357143e+00], [-1.47448980e+00, 2.90051020e+00, -1.68282313e+00, ..., -9.34948980e+00, -1.85994898e+01, -1.72661565e+01], [-1.69047619e+00, -1.90476190e-01, -5.98214286e+00, ..., -5.64880952e+00, -1.95238095e+01, -2.18571429e+01], ..., [ 1.20391156e+01, 7.91411565e+00, 1.59557823e+01, ..., -3.37755102e+00, -7.66921769e+00, -1.22108844e+01], [-9.38520408e+00, 1.09064626e+01, 7.15646259e+00, ..., -1.29268707e+01, -1.35935374e+01, -9.63520408e+00], [-7.27380952e+00, -1.34404762e+01, -4.64880952e+00, ..., -1.03154762e+01, -3.69047619e+00, -1.15238095e+01]], [[ 6.53915344e+01, 3.12063492e+01, 3.32619048e+01, ..., -9.49735450e+00, 1.11137566e+01, -6.82539683e-01], [-1.26734694e+01, 1.91912320e+00, 2.24006047e+01, ..., 2.19690098e+00, 3.99561602e+01, 3.38080121e+01], [ 1.58688587e+01, -1.07978080e+01, -2.50151172e+00, ..., 9.98488284e-01, 3.88318216e+01, 6.93503401e+01], ... [ 1.73119534e+01, 1.22940962e+01, 5.88338192e+00, ..., -5.95590379e+00, -5.36661808e+00, -3.00947522e+00], [ 1.48199708e+01, 8.99854227e+00, 3.12354227e+00, ..., 5.85568513e+00, 1.25699708e+01, 8.03425656e+00], [ 9.53607872e+00, 3.75036443e+00, -5.71064140e-01, ..., 8.01822157e+00, 8.16107872e+00, 9.28607872e+00]], [[-7.77608779e+00, -1.78552176e+00, -2.16288025e+00, ..., 5.35598768e+00, -1.06276473e-01, 6.57874471e-01], [-5.17327686e+00, -5.03465537e-01, -2.90912591e+00, ..., 1.33615710e+00, -3.21101271e+00, -4.56950327e+00], [-3.19445514e+00, -1.70388910e+00, 5.03658067e-01, ..., 1.22063920e+00, -3.13785137e+00, -4.84539854e+00], ..., [ 1.35656527e+01, 8.97131305e+00, 1.07071621e+01, ..., -4.77396997e+00, -5.74566808e+00, -1.22456681e+01], [-2.63111282e+00, -3.10358106e-01, -3.49903735e+00, ..., -4.56507509e+00, -7.64054678e+00, -7.73488641e+00], [-2.31189834e+00, -5.41567193e+00, -4.46284174e+00, ..., -1.72699268e+00, 7.06969580e-01, 1.22063920e-01]]]) - spike_time(spike_id)float322.363 2.385 1.451 ... 1.651 0.5142
array([2.3628814, 2.3849149, 1.4507183, ..., 2.401162 , 1.6506066, 0.5142453], dtype=float32) - spike_cell(spike_id)uint321 1 1 1 1 ... 1268 1268 1268 1268
array([ 1, 1, 1, ..., 1268, 1268, 1268], dtype=uint32)
- spike_trial(spike_id)uint321 1 2 2 2 2 ... 205 205 205 213 252
array([ 1, 1, 2, ..., 205, 213, 252], dtype=uint32)
- trialPandasIndex
PandasIndex(Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... 252, 253, 254, 255, 256, 257, 258, 259, 260, 261], dtype='int32', name='trial', length=261)) - timePandasIndex
PandasIndex(Index([0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, ... 2.41, 2.42, 2.43, 2.44, 2.45, 2.46, 2.47, 2.48, 2.49, 2.5], dtype='float64', name='time', length=250)) - cellPandasIndex
PandasIndex(Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268], dtype='int32', name='cell', length=1268)) - waveform_componentPandasIndex
PandasIndex(Index([1, 2, 3], dtype='int32', name='waveform_component'))
- probePandasIndex
PandasIndex(Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... 375, 376, 377, 378, 379, 380, 381, 382, 383, 384], dtype='int32', name='probe', length=384)) - brain_area_lfpPandasIndex
PandasIndex(Index(['CA1', 'DG', 'LP', 'PO', 'VISam'], dtype='object', name='brain_area_lfp'))
- spike_idPandasIndex
PandasIndex(Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... 1836000, 1836001, 1836002, 1836003, 1836004, 1836005, 1836006, 1836007, 1836008, 1836009], dtype='int32', name='spike_id', length=1836009))
- session_date :
- 2017-01-08
- mouse :
- Muller
- stim_onset :
- 0.5
- bin_size :
- 0.01
Run the cell below to make a DataFrame with spiking data ("spike_time", "spike_cell", "spike_trial")
spike_cols = ['spike_time', 'spike_cell', 'spike_trial']
df = dset[spike_cols].to_dataframe().reset_index()
df.head()| spike_id | spike_time | spike_cell | spike_trial | |
|---|---|---|---|---|
| 0 | 1 | 2.362881 | 1 | 1 |
| 1 | 2 | 2.384915 | 1 | 1 |
| 2 | 3 | 1.450718 | 1 | 2 |
| 3 | 4 | 1.459585 | 1 | 2 |
| 4 | 5 | 1.963452 | 1 | 2 |
Example: How many cells spiked during trial 1?
df[df['spike_trial'] == 1]['spike_cell'].nunique()860Exercise: How many cells spiked during trial 24?
Solution
df[df['spike_trial']==24]['spike_cell'].nunique()830Exercise: How many cells spiked during the last trial?
Solution
df[df['spike_trial'] == df['spike_trial'].max()]['spike_cell'].nunique()963Exercise: How many cells spiked in each trial. Hint - use df.groupby('spike_trial').
Solution
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
n_spiking_cellsspike_trial
1 860
2 839
3 809
4 879
5 836
...
257 889
258 914
259 890
260 954
261 963
Name: spike_cell, Length: 261, dtype: int64Exercise: What was the maximum number cells that spiked in a single trial?
Solution
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
n_spiking_cells.max()1001Exercise: Which trial had the most spiking cells?
Solution
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
n_spiking_cells.idxmax()153Exercise: Find the trial number and the spike count where the fewest number of cells spiked
Solution
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
n_spiking_cells.idxmin(), n_spiking_cells.min()(71, 758)Example: Use the plt.hist() function to plot a histogram of the number of cells that spiked, across all trials.
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
plt.hist(n_spiking_cells);Exercise: Use the plt.hist() function to plot a histogram of the number of cells that spiked, across all trials. Make the histogram with the following features:
- color: black
Solution
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
plt.hist(n_spiking_cells, color="k");Exercise: Use the plt.hist() function to plot a histogram of the number of cells that spiked, across all trials. Make the histogram with the following features:
- color: black
- histtype: “step”
Solution
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
plt.hist(n_spiking_cells, color="k", histtype='step');Exercise: Use the plt.hist() function to plot a histogram of the number of cells that spiked, across all trials. Make the histogram with the following features:
- color: black
- histtype: “step”
- linewidth: 3
Solution
n_spiking_cells = df.groupby(by='spike_trial')['spike_cell'].nunique()
plt.hist(n_spiking_cells, color="k", histtype='step', linewidth=3);Section 2: Visualize the spiking activity using spike times
Now we’ll focus on visualizing the spike timing data, and some of their properties, using matplotlib:
| Code | Description |
|---|---|
plt.scatter(x, y) |
Create a scatter plot of x vs y. x and y are both 1D arrays. |
| `plt.scatter(x, y, marker=' | ‘)` |
plt.scatter(x, y, s=5) |
Create a scatter plot with a specific marker size. |
plt.scatter(x, y, c=values) |
Create a scatter plot with colors based on values. |
plt.scatter(x, y, color='k') |
Create a scatter plot with a specific color. |
plt.scatter(x, y, linewidths=0.5) |
Create a scatter plot with a specific line width for markers. |
plt.scatter(x, y, label='text') |
Create a scatter plot with a label for the legend. |
plt.legend() |
Display a legend on the plot. |
plt.xlabel("your_text") |
Label the x-axis of the plot with specified text. |
plt.ylabel("your_text") |
Label the y-axis of the plot with specified text. |
plt.title("your_text") |
Add a title to the plot with the specified text. |
Exercises
Example: Create a scatter plot to visualize spikes for a single neuron across multiple trials.
spike_cols = ['spike_time', 'spike_cell', 'spike_trial']
df = dset[spike_cols].to_dataframe().reset_index()dd = df[df['spike_cell'] == 12]
plt.scatter(dd['spike_time'], dd['spike_trial'])
plt.xlabel('time/s')
plt.ylabel('trial number');Exercise: Create a scatter plot to visualize spikes for a neuron number 11 across multiple trials.
Solution
dd = df[df['spike_cell'] == 11]
plt.scatter(dd['spike_time'], dd['spike_trial'])
plt.xlabel('time/s')
plt.ylabel('trial number');Exercise: Wow, that plot looks crowded!
To help with this, instead of a dot let’s use | as marker. This can be done by simply setting the marker argument of the plt.scatter() function.
Solution
dd = df[df.spike_cell == 11]
plt.scatter(dd['spike_time'], dd['spike_trial'], marker='|')
plt.xlabel('time/s')
plt.ylabel('trial number');Exercise: This might still look a bit crowded. Let’s improve it further by setting the size of the marker to a smaller value. You can use s which is an argument of the plt.scatter() to specify the size.
Solution
dd = df[df.spike_cell == 11]
plt.scatter(dd['spike_time'], dd['spike_trial'], marker='|', s=5)
plt.xlabel('time/s')
plt.ylabel('trial number');Exercise: Create a scatter plot to visualize spikes for all neurons in trial 83.
Solution
dd = df[df.spike_trial == 83]
plt.scatter(dd['spike_time'], dd['spike_cell'], marker='|', s=5);
plt.xlabel('time/s')
plt.ylabel('cell');Exercise: Visualize spikes for all neurons in trial 83 using a scatter plot with the following features:
- color: “black”
Color can be specified using the c argument.
Solution
dd = df[df['spike_trial'] == 83]
plt.scatter(dd['spike_time'], dd['spike_cell'], marker='|', s=5, color="k");
plt.xlabel('time/s')
plt.ylabel('cell');Exercise: Visualize spikes for all neurons in trial 83 using a scatter plot with the following features:
- color: depends on the value of
spike_time.
hint: c=dd['spike_time']
Solution
dd = df[df['spike_trial'] == 83]
plt.scatter(dd['spike_time'], dd['spike_cell'], marker='|', s=5, c=dd['spike_time']);
plt.xlabel('time/s')
plt.ylabel('cell');Exercise: Visualize spikes for all neurons in trial 83 using a scatter plot with the following features:
- color: depends on the value of
spike_cell.
Solution
dd = df[df['spike_trial'] == 83]
plt.scatter(dd['spike_time'], dd['spike_cell'], marker='|', s=5, c=dd['spike_cell']);
plt.xlabel('time/s')
plt.ylabel('cell');Section 3: Demo: Raster plot with spikes from multiple brain areas
Below are two examples of how you can make a raster plot with spikes from different brain areas.
Exercises
spike_cols = ['spike_time', 'spike_cell', 'spike_trial']
df = dset[spike_cols].to_dataframe().reset_index()
brain_region_df = dset["brain_area"].to_dataframe().reset_index()
spike_region_df = pd.merge(df, brain_region_df, left_on='spike_cell', right_on='cell')
spike_region_df| spike_id | spike_time | spike_cell | spike_trial | cell | brain_area | |
|---|---|---|---|---|---|---|
| 0 | 1 | 2.362881 | 1 | 1 | 1 | PO |
| 1 | 2 | 2.384915 | 1 | 1 | 1 | PO |
| 2 | 3 | 1.450718 | 1 | 2 | 1 | PO |
| 3 | 4 | 1.459585 | 1 | 2 | 1 | PO |
| 4 | 5 | 1.963452 | 1 | 2 | 1 | PO |
| ... | ... | ... | ... | ... | ... | ... |
| 1836004 | 1836005 | 2.240762 | 1268 | 205 | 1268 | root |
| 1836005 | 1836006 | 2.268195 | 1268 | 205 | 1268 | root |
| 1836006 | 1836007 | 2.401162 | 1268 | 205 | 1268 | root |
| 1836007 | 1836008 | 1.650607 | 1268 | 213 | 1268 | root |
| 1836008 | 1836009 | 0.514245 | 1268 | 252 | 1268 | root |
1836009 rows × 6 columns
dd = spike_region_df[spike_region_df["spike_trial"] == 187]
dd = dd.sort_values(by="brain_area")
dd["ordered_index"] = dd.spike_cell.factorize()[0]
area = "CA1"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', label=area)
area = "DG"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', label=area)
area = "LP"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', label=area)
plt.legend();dd = spike_region_df[spike_region_df["spike_trial"] == 187]
dd = dd.sort_values(by="brain_area")
dd["ordered_index"] = dd.spike_cell.factorize()[0]
area = "CA1"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', linewidths=0.5, label=area)
area = "DG"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', linewidths=0.5, label=area)
area = "LP"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index , marker='|', linewidths=0.5, label=area)
area = "PO"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', linewidths=0.5, label=area)
area = "VISam"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', linewidths=0.5, label=area)
area = "root"
dd_area = dd[dd["brain_area"] == area]
plt.scatter(dd_area.spike_time, dd_area.ordered_index, marker='|', linewidths=0.5, label=area)
plt.legend();Visualize the spiking activity with firing rate plots
Raster plots are a great way to visualize spiking activity of many cells together or single cells over many trials. But often it’s also a good idea to show the average activity of a whole population of cells and/or the average activity across trials. To do that, we calculate and plot the firing rate. Below, you will use the groupby function in Pandas and the plotting functions in Matplotlib to display the firing rate and adapt the plots.
| Code | Description |
|---|---|
series.keys() |
Get the index (keys) of a Pandas Series. |
series.values |
Get the values of a Pandas Series as a NumPy array. |
df.mean() |
Find the mean value of a DataFrame. |
df['column_name'].mean() |
Find the mean value of the `‘column_name’ column of a DataFrame. |
df.groupby('columnA')['columnB'].mean() |
Group a DataFrame according to values of column_B and find the mean value of the data in column_A given this grouping. |
plt.plot(x, y) |
Create a line plot of x against y. x and y are both 1D arrays. |
plt.plot(x, y, label='text') |
Create a line plot with a label for the legend. |
plt.plot(x, y, color='red') |
Create a line plot with a specific color. |
plt.legend() |
Display a legend on the plot. |
str(value) |
Convert a value to a string. |
Run the cell below to make a DataFrame with the spike rate data.
spike_cols = ['spike_rate']
df = dset[spike_cols].to_dataframe().reset_index()
df| cell | trial | time | spike_rate | |
|---|---|---|---|---|
| 0 | 1 | 1 | 0.01 | 0 |
| 1 | 1 | 1 | 0.02 | 0 |
| 2 | 1 | 1 | 0.03 | 0 |
| 3 | 1 | 1 | 0.04 | 0 |
| 4 | 1 | 1 | 0.05 | 0 |
| ... | ... | ... | ... | ... |
| 82736995 | 1268 | 261 | 2.46 | 0 |
| 82736996 | 1268 | 261 | 2.47 | 0 |
| 82736997 | 1268 | 261 | 2.48 | 0 |
| 82736998 | 1268 | 261 | 2.49 | 0 |
| 82736999 | 1268 | 261 | 2.50 | 0 |
82737000 rows × 4 columns
Example: Get firing rate over time in trial number 187 averaged across all cells and plot the firing rate.
dd = df[df['trial'] == 187]
rate_series = dd.groupby('time')['spike_rate'].mean()
rate_seriestime
0.01 0.027603
0.02 0.018927
0.03 0.031546
0.04 0.021293
0.05 0.018927
...
2.46 0.019716
2.47 0.026025
2.48 0.032334
2.49 0.029180
2.50 0.020505
Name: spike_rate, Length: 250, dtype: float64time = rate_series.keys()
rate = rate_series.values
plt.plot(time, rate);Exercise: Get average firing rate across trials in for cell number 4 and plot the firing rate
Solution
dd = df[df['cell'] == 4]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values
plt.plot(time, rate);Exercise: Now let’s compare the firing rates of two different cells. Make a figure that shows the firing rate over time averaged across trials for cell number 4 and cell number 10 together in the same plot.
Solution
dd = df[df['cell'] == 4]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values
plt.plot(time, rate)
dd = df[df['cell'] == 10]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values
plt.plot(time, rate);It’s best if the person viewing the plot can immediately see which firing rate belongs to which cell without having to read the code. To show that, we add the parameter label to the plot function and add the line plt.legend() in the code to tell Python to display the labels.
Example: Plot the firing rate for cell number 4 and make a legend where the cell number is labeled.
cell_nr = 11
dd = df[df['cell'] == cell_nr]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values
plt.plot(time, rate, label = f'Cell Nr. {cell_nr}')
plt.legend();Exercise: Make the plot showing firing rates over time for both cell nr. 4 and cell nr. 10 again, but this time display labels for both cells. (Tip: you only need to use the plt.legend() function once to make the legend, even though you have multiple labels.)
Solution
cell_nr = 4
dd = df[df['cell'] == cell_nr]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values
plt.plot(time, rate, label = f'Cell Nr. {cell_nr}')
cell_nr = 10
dd = df[df['cell'] == cell_nr]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values
plt.plot(time, rate, label = 'Cell Nr. {cell_nr}')
plt.legend();Exercise: Make the plot with the firing rate over time for cell number 4 and 10 again, but this time put labels on the x- and y-axis and add a title to the plot. Remember to include the unit in the label. Hint: The rate is typically given in Hz or spikes per second, but since the time bins are 0.01 seconds, it’s currently given in spikes per 1/100th of a second. To get it in spikes/second, simply multiply the rate with 100 before plotting (it can also be done inside the plot function if you don’t want to change the scale of the variable).
Solution
cell_nr = 4
dd = df[df['cell'] == cell_nr]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values*1E2
plt.plot(time, rate, label = f'Cell Nr. {cell_nr}')
cell_nr = 10
dd = df[df['cell'] == cell_nr]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values*1E2
plt.plot(time, rate, label = f'Cell Nr. {cell_nr}')
plt.xlabel('Time (s)')
plt.ylabel('Spikes/s')
plt.title('Trial averaged firing rate')
plt.legend();Exercise: Make the same plot with trial averaged firing rates over time for cell number 4 and 10, but change the color of the line for each cell to one of your choosing.
Solution
cell_nr = 4
dd = df[df['cell'] == cell_nr]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values*1E2
plt.plot(time, rate, label = f'Cell Nr. {cell_nr}', color = 'red')
cell_nr = 10
dd = df[df['cell'] == cell_nr]
rate_series = dd.groupby('time')['spike_rate'].mean()
time = rate_series.keys()
rate = rate_series.values*1E2
plt.plot(time, rate, label = f'Cell Nr. {cell_nr}', color = 'black')
plt.xlabel('Time (s)')
plt.ylabel('Spikes/s')
plt.title('Trial averaged firing rate')
plt.legend();