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.

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)
  0%|          | 0.00/71.0 [00:00<?, ?MB/s]
  7%|▋         | 5.00/71.0 [00:00<00:01, 48.1MB/s]
 18%|█▊        | 13.0/71.0 [00:00<00:00, 60.5MB/s]
 31%|███       | 22.0/71.0 [00:00<00:00, 72.7MB/s]
 42%|████▏     | 30.0/71.0 [00:00<00:00, 72.0MB/s]
 54%|█████▎    | 38.0/71.0 [00:00<00:00, 66.6MB/s]
 63%|██████▎   | 45.0/71.0 [00:00<00:00, 61.1MB/s]
 76%|███████▌  | 54.0/71.0 [00:00<00:00, 67.8MB/s]
 86%|████████▌ | 61.0/71.0 [00:00<00:00, 66.4MB/s]
 97%|█████████▋| 69.0/71.0 [00:01<00:00, 67.2MB/s]
72.0MB [00:01, 67.0MB/s]                          


Parsing the data#

Now that we have the file, let’s load the data

data = nap.load_file(path)
print(data)
/home/runner/.local/lib/python3.10/site-packages/hdmf/spec/namespace.py:535: UserWarning: Ignoring cached namespace 'hdmf-common' version 1.5.1 because version 1.8.0 is already loaded.
  warn("Ignoring cached namespace '%s' version %s because version %s is already loaded."
/home/runner/.local/lib/python3.10/site-packages/hdmf/spec/namespace.py:535: UserWarning: Ignoring cached namespace 'core' version 2.6.0-alpha because version 2.7.0 is already loaded.
  warn("Ignoring cached namespace '%s' version %s because version %s is already loaded."
/home/runner/.local/lib/python3.10/site-packages/hdmf/spec/namespace.py:535: UserWarning: Ignoring cached namespace 'hdmf-experimental' version 0.2.0 because version 0.5.0 is already loaded.
  warn("Ignoring cached namespace '%s' version %s because version %s is already loaded."
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

transients = data['RoiResponseSeries']
print(transients)
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  ...
3.253       0.24111  0.88023  0.14898  0.24651  0.07095  ...
3.28655     0.233    0.85584  0.14858  0.23706  0.08147  ...
3.32015     0.22513  1.0996   0.14715  0.22572  0.08859  ...
...
1203.38945  0.20815  0.17535  0.12126  0.09446  0.87427  ...
1203.42305  0.20247  0.17243  0.11807  0.08992  1.2578   ...
1203.4566   0.19654  0.17056  0.11461  0.08508  1.62     ...
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()
../_images/6baca68e1a3893b683f9a8cf3ce6f996833c0e0f1f252374b9f4e90a385d40bd.png

Here we extract the head-direction as a variable called angle

angle = data['ry']
print(angle)
Time (s)
----------  -------
3.0994      2.58326
3.10775     2.5864
3.11605     2.5905
3.1244      2.59191
3.13275     2.59263
3.14105     2.59306
3.1494      2.59404
...
1206.1728   3.70308
1206.18115  3.6908
1206.18945  3.69804
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.

print(transients.time_support)
print(angle.time_support)
            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

tcurves = nap.compute_1d_tuning_curves_continuous(transients, angle, nb_bins = 120)

print(tcurves)
                0         1         2         3         4         5   \
0.026195  0.395699  0.055843  0.150304  0.159376  0.064966  0.250720   
0.078555  0.279695  0.052430  0.153925  0.152457  0.093066  0.282052   
0.130915  0.398603  0.044422  0.201113  0.146771  0.074447  0.277748   
0.183274  0.379213  0.043964  0.149085  0.185060  0.078182  0.315267   
0.235634  0.266577  0.038920  0.175439  0.149006  0.060583  0.280129   
...            ...       ...       ...       ...       ...       ...   
6.047557  0.390266  0.072893  0.174015  0.189979  0.065083  0.230608   
6.099916  0.266773  0.065594  0.118181  0.181366  0.073373  0.235707   
6.152276  0.268866  0.060269  0.120475  0.192354  0.056588  0.215175   
6.204636  0.281763  0.064460  0.131925  0.163648  0.054197  0.212545   
6.256995  0.293497  0.048092  0.117291  0.158287  0.064194  0.235645   

                6         7         8         9   ...        55        56  \
0.026195  0.105479  0.375018  0.149291  0.179047  ...  0.102208  0.127368   
0.078555  0.101772  0.403379  0.165878  0.156748  ...  0.100212  0.130258   
0.130915  0.112891  0.509695  0.174218  0.196127  ...  0.097349  0.150502   
0.183274  0.107566  0.407264  0.181396  0.191215  ...  0.089064  0.150641   
0.235634  0.107965  0.402516  0.163791  0.201132  ...  0.080919  0.151985   
...            ...       ...       ...       ...  ...       ...       ...   
6.047557  0.089368  0.393424  0.167515  0.100450  ...  0.100726  0.118815   
6.099916  0.086450  0.367777  0.183998  0.118367  ...  0.102013  0.116333   
6.152276  0.089881  0.395225  0.169153  0.108004  ...  0.096451  0.109511   
6.204636  0.080368  0.377480  0.191245  0.141896  ...  0.099861  0.127606   
6.256995  0.109280  0.367404  0.166939  0.142197  ...  0.096367  0.119857   

                57        58        59        60        61        62  \
0.026195  0.096528  0.145273  0.153105  0.098130  0.142677  0.086804   
0.078555  0.077300  0.119932  0.152965  0.085250  0.129910  0.098154   
0.130915  0.073003  0.149255  0.137413  0.104471  0.108882  0.089716   
0.183274  0.065277  0.140361  0.161785  0.106143  0.110682  0.087498   
0.235634  0.059516  0.104021  0.124016  0.116167  0.092598  0.072857   
...            ...       ...       ...       ...       ...       ...   
6.047557  0.074665  0.147712  0.134264  0.091795  0.179061  0.115768   
6.099916  0.074303  0.117873  0.157095  0.091580  0.183408  0.110677   
6.152276  0.077440  0.104101  0.132563  0.089430  0.164008  0.121157   
6.204636  0.076488  0.127545  0.134820  0.099216  0.137886  0.099411   
6.256995  0.072474  0.111766  0.133008  0.091048  0.134673  0.089862   

                63        64  
0.026195  0.090393  0.090931  
0.078555  0.112558  0.101200  
0.130915  0.092577  0.127856  
0.183274  0.071661  0.144850  
0.235634  0.070615  0.177883  
...            ...       ...  
6.047557  0.108395  0.080172  
6.099916  0.103724  0.081672  
6.152276  0.099209  0.083993  
6.204636  0.098601  0.088175  
6.256995  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.

plt.figure()
plt.plot(tcurves[4])
plt.xlabel("Angle")
plt.ylabel("Fluorescence")
plt.show()
../_images/cd2e7aa480016beae59b29bc50d01c4df2bbf39efeb3a82313473793b56dc513.png

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]]
    )
/tmp/ipykernel_1797/2600373263.py:3: UserWarning: Some starts and ends are equal. Removing 1 microsecond!
  halves = nap.IntervalSet(

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.xlabel("Angle")
plt.show()
../_images/5421284955a037aa70aa15912316830a2ee8f23c08b3b274337bd04e04acd544.png

Authors

Sofia Skromne Carrasco