Note
Click here to download the full example code
Calcium Imaging
Working with calcium data.
For the example dataset, we will be working with a recording of a freely-moving mouse imaged with a Miniscope (1-photon imaging). The area recorded for this experiment is the postsubiculum - a region that is known to contain head-direction cells, or cells that fire when the animal's head is pointing in a specific direction.
The NWB file for the example is hosted on OSF. We show below how to stream it.
See the documentation of Pynapple for instructions on installing the package.
This tutorial was made by Sofia Skromne Carrasco and Guillaume Viejo.
Warning
This tutorial uses seaborn and matplotlib for displaying the figure
You can install all with pip install matplotlib seaborn tqdm
mkdocs_gallery_thumbnail_number = 1
Now, import the necessary libraries:
import numpy as pd
import pynapple as nap
import matplotlib.pyplot as plt
import seaborn as sns
import sys, os
import requests, math
import tqdm
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(style="ticks", palette="colorblind", font_scale=1.5, rc=custom_params)
Downloading the data
First things first: Let's find our file
path = "A0670-221213.nwb"
if path not in os.listdir("."):
r = requests.get(f"https://osf.io/sbnaw/download", stream=True)
block_size = 1024*1024
with open(path, 'wb') as f:
for data in tqdm.tqdm(r.iter_content(block_size), unit='MB', unit_scale=True,
total=math.ceil(int(r.headers.get('content-length', 0))//block_size)):
f.write(data)
Parsing the data
Now that we have the file, let's load the data
Out:
A0670-221213
┍━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┑
│ Keys │ Type │
┝━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━┥
│ position_time_support │ IntervalSet │
│ RoiResponseSeries │ TsdFrame │
│ z │ Tsd │
│ y │ Tsd │
│ x │ Tsd │
│ rz │ Tsd │
│ ry │ Tsd │
│ rx │ Tsd │
┕━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┙
Let's save the RoiResponseSeries as a variable called 'transients' and print it
Out:
Time (s) 0 1 2 3 4 ...
---------- ------- ------- ------- ------- ------- -----
3.1187 0.27546 0.79973 0.16383 0.20118 0.02926 ...
3.15225 0.26665 0.86751 0.15879 0.23682 0.02719 ...
3.18585 0.25796 0.89419 0.15352 0.25074 0.03651 ...
3.2194 0.24943 0.89513 0.14812 0.25215 0.05627 ...
...
1203.4902 0.19052 0.16645 0.11096 0.0802 1.8811 ...
1203.52375 0.18449 0.16105 0.10717 0.07542 2.0599 ...
1203.55735 0.17851 0.15494 0.10331 0.07081 2.2176 ...
1203.5909 0.17264 0.14851 0.09942 0.06643 2.311 ...
dtype: float64, shape: (35757, 65)
Plotting the activity of one neuron
Our transients are saved as a (35757, 65) TsdFrame. Looking at the printed object, you can see that we have 35757 data points for each of our 65 regions of interest. We want to see which of these are head-direction cells, so we need to plot a tuning curve of fluorescence vs head-direction of the animal.
plt.figure(figsize=(6, 2))
plt.plot(transients[0:2000,0], linewidth=5)
plt.xlabel("Time (s)")
plt.ylabel("Fluorescence")
plt.show()
Here we extract the head-direction as a variable called angle
Out:
Time (s)
---------- -------
3.0994 2.58326
3.10775 2.5864
3.11605 2.5905
3.1244 2.59191
...
1206.1978 3.6728
1206.20615 3.65452
1206.21445 3.61199
1206.2228 3.5495
dtype: float64, shape: (144382,)
As you can see, we have a longer recording for our tracking of the animal's head than we do for our calcium imaging - something to keep in mind.
Out:
start end
0 3.1187 1203.59
shape: (1, 2), time unit: sec.
start end
0 3.0994 1206.22
shape: (1, 2), time unit: sec.
Calcium tuning curves
Here we compute the tuning curves of all the neurons
Out:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
0.026195 0.395699 0.055843 0.150304 0.159376 0.064966 0.250720 0.105479 0.375018 0.149291 0.179047 0.241102 0.089391 0.276466 0.154857 0.250887 ... 0.287072 0.094793 0.123390 0.105452 0.164385 0.102208 0.127368 0.096528 0.145273 0.153105 0.098130 0.142677 0.086804 0.090393 0.090931
0.078555 0.279695 0.052430 0.153925 0.152457 0.093066 0.282052 0.101772 0.403379 0.165878 0.156748 0.239359 0.134046 0.262837 0.137199 0.273873 ... 0.269038 0.090904 0.111236 0.109685 0.145850 0.100212 0.130258 0.077300 0.119932 0.152965 0.085250 0.129910 0.098154 0.112558 0.101200
0.130915 0.398603 0.044422 0.201113 0.146771 0.074447 0.277748 0.112891 0.509695 0.174218 0.196127 0.269043 0.095671 0.269560 0.142631 0.275985 ... 0.311582 0.083200 0.109123 0.130775 0.161412 0.097349 0.150502 0.073003 0.149255 0.137413 0.104471 0.108882 0.089716 0.092577 0.127856
0.183274 0.379213 0.043964 0.149085 0.185060 0.078182 0.315267 0.107566 0.407264 0.181396 0.191215 0.221558 0.082923 0.224356 0.145352 0.235904 ... 0.281177 0.078241 0.101353 0.144213 0.169794 0.089064 0.150641 0.065277 0.140361 0.161785 0.106143 0.110682 0.087498 0.071661 0.144850
0.235634 0.266577 0.038920 0.175439 0.149006 0.060583 0.280129 0.107965 0.402516 0.163791 0.201132 0.203248 0.082475 0.180643 0.144778 0.242402 ... 0.289859 0.068067 0.080355 0.141221 0.179952 0.080919 0.151985 0.059516 0.104021 0.124016 0.116167 0.092598 0.072857 0.070615 0.177883
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
6.047557 0.390266 0.072893 0.174015 0.189979 0.065083 0.230608 0.089368 0.393424 0.167515 0.100450 0.330360 0.110288 0.305957 0.102294 0.257551 ... 0.290143 0.111767 0.146929 0.090813 0.120939 0.100726 0.118815 0.074665 0.147712 0.134264 0.091795 0.179061 0.115768 0.108395 0.080172
6.099916 0.266773 0.065594 0.118181 0.181366 0.073373 0.235707 0.086450 0.367777 0.183998 0.118367 0.277238 0.101077 0.306432 0.100006 0.227124 ... 0.276895 0.112443 0.146232 0.091413 0.143927 0.102013 0.116333 0.074303 0.117873 0.157095 0.091580 0.183408 0.110677 0.103724 0.081672
6.152276 0.268866 0.060269 0.120475 0.192354 0.056588 0.215175 0.089881 0.395225 0.169153 0.108004 0.291975 0.122327 0.286644 0.087416 0.235598 ... 0.293546 0.112682 0.136103 0.086661 0.126677 0.096451 0.109511 0.077440 0.104101 0.132563 0.089430 0.164008 0.121157 0.099209 0.083993
6.204636 0.281763 0.064460 0.131925 0.163648 0.054197 0.212545 0.080368 0.377480 0.191245 0.141896 0.253220 0.101644 0.295603 0.122067 0.263662 ... 0.315580 0.100423 0.122304 0.091378 0.145588 0.099861 0.127606 0.076488 0.127545 0.134820 0.099216 0.137886 0.099411 0.098601 0.088175
6.256995 0.293497 0.048092 0.117291 0.158287 0.064194 0.235645 0.109280 0.367404 0.166939 0.142197 0.224261 0.104500 0.283471 0.125519 0.244262 ... 0.283688 0.094941 0.114575 0.096160 0.173438 0.096367 0.119857 0.072474 0.111766 0.133008 0.091048 0.134673 0.089862 0.084487 0.100030
[120 rows x 65 columns]
We now have a DataFrame, where our index is the angle of the animal's head in radians, and each column represents the tuning curve of each region of interest. We can plot one neuron.
It looks like this could be a head-direction cell. One important property of head-directions cells however, is that their firing with respect to head-direction is stable. To check for their stability, we can split our recording in two and compute a tuning curve for each half of the recording.
We start by finding the midpoint of the recording, using the function get_intervals_center
. Using this, then create one new IntervalSet with two rows, one for each half of the recording.
center = transients.time_support.get_intervals_center()
halves = nap.IntervalSet(
start = [transients.time_support.start[0], center.t[0]],
end = [center.t[0], transients.time_support.end[0]]
)
Now we can compute the tuning curves for each half of the recording and plot the tuning curves for the fifth region of interest.
half1 = nap.compute_1d_tuning_curves_continuous(transients, angle, nb_bins = 120, ep = halves.loc[[0]])
half2 = nap.compute_1d_tuning_curves_continuous(transients, angle, nb_bins = 120, ep = halves.loc[[1]])
plt.figure(figsize=(12, 5))
plt.subplot(1,2,1)
plt.plot(half1[4])
plt.title("First half")
plt.xlabel("Angle")
plt.ylabel("Fluorescence")
plt.subplot(1,2,2)
plt.plot(half2[4])
plt.title("Second half")
plt.show()
Total running time of the script: ( 0 minutes 1.136 seconds)
Download Python source code: tutorial_calcium_imaging.py