Note
Click here to download the full example code
Fit Grid Cells population
import math
import os
from typing import Optional
import jax
import matplotlib.pyplot as plt
import nemos as nmo
import numpy as np
import pynapple as nap
import nemos as nmo
from scipy.ndimage import gaussian_filter
import workshop_utils
jax.config.update("jax_enable_x64", True)
DATA STREAMING
- Stream the data
io = workshop_utils.data.download_dandi_data("000582", "sub-11265/sub-11265_ses-07020602_behavior+ecephys.nwb",
)
PYNAPPLE
- Load the data with pynapple
data = nap.NWBFile(io.read())
- Print the data
print(data)
- extract the spike times and the position of the animal frome the
data
object
spikes = data["units"] # Get spike timings
position = data["SpatialSeriesLED1"] # Get the tracked orientation of the animal
- compute the head-direction of the animal from
SpatialSeriesLED1
andSpatialSeriesLED1
diff = data['SpatialSeriesLED1'].values-data['SpatialSeriesLED2'].values
head_dir = (np.arctan2(*diff.T) + (2*np.pi))%(2*np.pi)
head_dir = nap.Tsd(data['SpatialSeriesLED1'].index, head_dir).dropna()
- compute the head-direction and position tuning curves
hd_tuning = nap.compute_1d_tuning_curves(
group=spikes,
feature=head_dir,
nb_bins=61,
minmax=(0, 2 * np.pi)
)
pos_tuning, binsxy = nap.compute_2d_tuning_curves(
group=spikes,
features=position,
nb_bins=12)
- plot the tuning curves for each neurons
fig = plt.figure(figsize = (12, 4))
gs = plt.GridSpec(2, len(spikes))
for i in range(len(spikes)):
ax = plt.subplot(gs[0,i], projection='polar')
ax.plot(hd_tuning.loc[:,i])
ax = plt.subplot(gs[1,i])
ax.imshow(gaussian_filter(pos_tuning[i], sigma=1))
plt.tight_layout()
NEMOS
- bin spike trains in 10 ms bin size
- interpolate the position to the timestamps of counts using
interpolate
function of pynapple
- define a basis in 2D using nemos
RaisedCosineBasisLinear
- evaluate the basis on a 100x100 grid using
evaluate_on_grid
- plot the evaluated basis
- rescale the position between 0 and 1 to match the basis functions
- evaluate the basis for each position of the animal
- instantiate a GLM model with Ridge regularization.
- set
regularizer_strength=1.0
- fit the model only to neuron 7 for faster computation
- predict the rate and compute a tuning curves using
compute_2d_tuning_curves_continuous
from pynapple
- compare the tuning curves
- find the best
regularizer_strength
usingsklearn.model_selection.GriSearchCV
- instantiate the best model from scikit-learn
- predict the rate of the best model and compute a 2d tuning curves
- compare the 2d tuning curves
Total running time of the script: ( 0 minutes 0.000 seconds)
Download Python source code: 03_grid_cells_users.py