Open In Colab

Installing requirements¶

The cell below installs every Python package needed to run this notebook, at fully pinned versions, using uv for fast resolution. In Colab the cell is collapsed by default — click the ▶ button to run it.

In [1]:
# install cell skipped during CI (deps preinstalled into system Python)

⚠️ Restart runtime after install

The install may upgrade packages already loaded in the kernel. Go to Runtime → Restart session, then Run all cells below (skip this install cell on re-run).

Optogenetics Example Session¶

In [2]:
from stream_nwbfile import stream_nwbfile
import numpy as np
import matplotlib.pyplot as plt

This notebook showcases one example session from the 000971 dataset containing operant behavior and concurrent excitatory optogenetic stimulation.

In [3]:
DANDISET_ID = '000971'
DANDISET_VERSION = "0.261013.1707"
file_path = 'sub-119-416/sub-119-416_ses-Opto-DMS-Excitatory-ChR2-2020-10-20T13-00-57_ogen.nwb'
nwbfile, io = stream_nwbfile(DANDISET_ID, file_path, DANDISET_VERSION)
display(nwbfile)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 4
      1 DANDISET_ID = '000971'
      2 DANDISET_VERSION = "0.261013.1707"
      3 file_path = 'sub-119-416/sub-119-416_ses-Opto-DMS-Excitatory-ChR2-2020-10-20T13-00-57_ogen.nwb'
----> 4 nwbfile, io = stream_nwbfile(DANDISET_ID, file_path, DANDISET_VERSION)
      5 display(nwbfile)

TypeError: stream_nwbfile() takes 2 positional arguments but 3 were given

Retrieve Optogenetic and Behavioral Data

In [4]:
# Optogenetic Stimulation
optogenetic_stimulation_timestamps = nwbfile.stimulus['OptogeneticSeries'].timestamps[:]
optogenetic_stimulation_data = nwbfile.stimulus['OptogeneticSeries'].data[:]
opto_onset_times = optogenetic_stimulation_timestamps[optogenetic_stimulation_data == 0.015]
opto_offset_times = optogenetic_stimulation_timestamps[optogenetic_stimulation_data == 0]

# Behavior
right_nose_poke_times = nwbfile.processing['behavior'].data_interfaces['right_nose_poke_times'].timestamps[:]
right_reward_times = nwbfile.processing['behavior'].data_interfaces['right_reward_times'].timestamps[:]
reward_port_entry_times = nwbfile.processing['behavior'].data_interfaces['reward_port_entry_times'].timestamps[:]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 2
      1 # Optogenetic Stimulation
----> 2 optogenetic_stimulation_timestamps = nwbfile.stimulus['OptogeneticSeries'].timestamps[:]
      3 optogenetic_stimulation_data = nwbfile.stimulus['OptogeneticSeries'].data[:]
      4 opto_onset_times = optogenetic_stimulation_timestamps[optogenetic_stimulation_data == 0.015]
      5 opto_offset_times = optogenetic_stimulation_timestamps[optogenetic_stimulation_data == 0]

NameError: name 'nwbfile' is not defined

Plot

In [5]:
t_start = 1
t_end = 3600
right_nose_poke_mask = np.logical_and(right_nose_poke_times >= t_start, right_nose_poke_times < t_end)
right_reward_mask = np.logical_and(right_reward_times >= t_start, right_reward_times < t_end)
reward_port_entry_mask = np.logical_and(reward_port_entry_times >= t_start, reward_port_entry_times < t_end)
opto_onset_mask = np.logical_and(opto_onset_times >= t_start, opto_onset_times < t_end)
opto_offset_mask = np.logical_and(opto_offset_times >= t_start, opto_offset_times < t_end)
lineoffsets = 1
linelengths = 1
alpha = 0.3
ylim = [-2, 2]
y = np.arange(-1, 0, 0.1)

fix, ax = plt.subplots(figsize=(10, 5))
ax.eventplot(right_nose_poke_times[right_nose_poke_mask], lineoffsets=lineoffsets, linelengths=linelengths, color='r', label='Right Nose Poke')
ax.eventplot(right_reward_times[right_reward_mask], lineoffsets=lineoffsets, linelengths=linelengths, color='g', label='Right Reward')
for i, (onset_time, offset_time) in enumerate(zip(opto_onset_times[opto_onset_mask], opto_offset_times[opto_offset_mask])):
    x1 = onset_time*np.ones(len(y))
    x2 = offset_time*np.ones(len(y))
    if i == 0:
        ax.fill_betweenx(y, x1, x2, color='b', alpha=alpha, label='Optogenetic Stimulation')
    else:
        ax.fill_betweenx(y, x1, x2, color='b', alpha=alpha)
ax.set_ylim(ylim)
ax.set_title('DMS Excitatory ChR2')
ax.yaxis.set_visible(False)
ax.set_xlabel('Time (s)')
_ = ax.legend(bbox_to_anchor=(1.01, 1), loc='upper left')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 3
      1 t_start = 1
      2 t_end = 3600
----> 3 right_nose_poke_mask = np.logical_and(right_nose_poke_times >= t_start, right_nose_poke_times < t_end)
      4 right_reward_mask = np.logical_and(right_reward_times >= t_start, right_reward_times < t_end)
      5 reward_port_entry_mask = np.logical_and(reward_port_entry_times >= t_start, reward_port_entry_times < t_end)
      6 opto_onset_mask = np.logical_and(opto_onset_times >= t_start, opto_onset_times < t_end)

NameError: name 'right_nose_poke_times' is not defined
In [6]:
t_start = 2200
t_end = 2250
right_nose_poke_mask = np.logical_and(right_nose_poke_times >= t_start, right_nose_poke_times < t_end)
right_reward_mask = np.logical_and(right_reward_times >= t_start, right_reward_times < t_end)
reward_port_entry_mask = np.logical_and(reward_port_entry_times >= t_start, reward_port_entry_times < t_end)
opto_onset_mask = np.logical_and(opto_onset_times >= t_start, opto_onset_times < t_end)
opto_offset_mask = np.logical_and(opto_offset_times >= t_start, opto_offset_times < t_end)
lineoffsets = 1
linelengths = 1
alpha = 0.3
ylim = [-2, 2]
y = np.arange(-1, 0, 0.1)

fix, ax = plt.subplots(figsize=(10, 5))
ax.eventplot(right_nose_poke_times[right_nose_poke_mask], lineoffsets=lineoffsets, linelengths=linelengths, color='r', label='Right Nose Poke')
ax.eventplot(right_reward_times[right_reward_mask], lineoffsets=lineoffsets, linelengths=linelengths, color='g', label='Right Reward')
for i, (onset_time, offset_time) in enumerate(zip(opto_onset_times[opto_onset_mask], opto_offset_times[opto_offset_mask])):
    x1 = onset_time*np.ones(len(y))
    x2 = offset_time*np.ones(len(y))
    if i == 0:
        ax.fill_betweenx(y, x1, x2, color='b', alpha=alpha, label='Optogenetic Stimulation')
    else:
        ax.fill_betweenx(y, x1, x2, color='b', alpha=alpha)
ax.set_ylim(ylim)
ax.set_title('DMS Excitatory ChR2')
ax.yaxis.set_visible(False)
ax.set_xlabel('Time (s)')
_ = ax.legend(bbox_to_anchor=(1.01, 1), loc='upper left')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 3
      1 t_start = 2200
      2 t_end = 2250
----> 3 right_nose_poke_mask = np.logical_and(right_nose_poke_times >= t_start, right_nose_poke_times < t_end)
      4 right_reward_mask = np.logical_and(right_reward_times >= t_start, right_reward_times < t_end)
      5 reward_port_entry_mask = np.logical_and(reward_port_entry_times >= t_start, reward_port_entry_times < t_end)
      6 opto_onset_mask = np.logical_and(opto_onset_times >= t_start, opto_onset_times < t_end)

NameError: name 'right_nose_poke_times' is not defined
In [ ]: