Reading and Exploring an NWB File

This tutorial will demonstrate how to read, explore, and do basic visualizations with an NWB File using different tools.

An NWBFile represents a single session of an experiment. It contains all the data of that session and the metadata required to understand the data.

We will demonstrate how to use the DANDI neurophysiology data archive to access the data in two different ways: (1) by downloading it to your computer and (2) streaming it.

We will briefly show tools for exploring NWB Files interactively and refer the reader to the NWB Overview documentation for more details about the available tools.

See also

You can learn more about the NWBFile format in the NWB File Basics tutorial.

The following examples will reference variables that may not be defined within the block they are used in. For clarity, we define them here:

import matplotlib.pyplot as plt
import numpy as np

We will access NWB data on the DANDI Archive, and demonstrate reading one session of an experiment by Chandravadia et al. (2020). In this study, the authors recorded single neuron activity from the medial temporal lobes of human subjects while they performed a recognition memory task.

Download the data

First, we will demonstrate how to download an NWB data file from DANDI to your machine.

Download using the DANDI Web UI

You can download files directly from the DANDI website.

  1. Go to the DANDI archive and open this dataset

  2. List the files in this dataset by clicking the “Files” button in Dandiset Actions (top right column of the page).

view files on dandi
  1. Choose the folder “sub-P11MHM” by clicking on its name.

selecting a folder on dandi

4. Download the NWB data file “sub-P11HMH_ses-20061101_ecephys+image.nwb” to your computer by clicking on the download symbol.

selecting a folder on dandi

Downloading data programmatically

Alternatively, you can download data using the dandi Python module.

from dandi.download import download

download("https://api.dandiarchive.org/api/assets/0f57f0b0-f021-42bb-8eaa-56cd482e2a29/download/", ".")
PATH                                      SIZE    DONE    DONE% CHECKSUM STATUS          MESSAGE
sub-P11HMH_ses-20061101_ecephys+image.nwb 72.6 MB 72.6 MB  100%    ok    done
Summary:                                  72.6 MB 72.6 MB                1 done
                                                  100.00%

See also

Learn about all the different ways you can download data from the DANDI Archive here

See also

Streaming data

Instead of downloading data, another approach is to stream data directly from an archive. Streaming data allows you to download only the data you want from a file, so it can be a much better approach when the desired data files contain a lot of data you don’t care about. There are several approaches to streaming NWB files, outlined in Streaming NWB files.

Opening an NWB file with NWBHDF5IO

Reading and writing NWB data is carried out using the NWBHDF5IO class. NWBHDF5IO reads NWB data that is in the HDF5 storage format, a popular, hierarchical format for storing large-scale scientific data.

The first argument to the constructor of NWBHDF5IO is the file_path. Use the read method to read the data into a NWBFile object.

filepath = "sub-P11HMH_ses-20061101_ecephys+image.nwb"
from pynwb import read_nwb

nwbfile = read_nwb(filepath)
nwbfile

root (NWBFile)

session_description: New/Old recognition task for ID: 9.
identifier: H11_9
session_start_time2006-11-01 00:00:00-07:00
timestamps_reference_time2006-11-01 00:00:00-07:00
file_create_date
02020-05-26 19:08:31.224415-07:00
related_publications('Faraut et al. 2018, Scientific Data; Rutishauser et al. 2015, Nat Neurosci;',)
acquisition
events (AnnotationSeries)
resolution: -1.0
comments: no comments
description: The events coorespond to the TTL markers for each trial. For the learning trials, the TTL markers are the following: 55 = start of the experiment, 1 = stimulus ON, 2 = stimulus OFF, 3 = Question Screen Onset [“Is this an animal?”], 20 = Yes (21 = NO) during learning, 6 = End of Delay after Response, 66 = End of Experiment. For the recognition trials, the TTL markers are the following: 55 = start of experiment, 1 = stimulus ON, 2 = stimulus OFF, 3 = Question Screen Onset [“Have you seen this image before?”], 31:36 = Confidence (Yes vs. No) response [31 (new, confident), 32 (new, probably), 33 (new, guess), 34 (old, guess), 35 (old, probably), 36 (old, confident)], 66 = End of Experiment
conversion: 1.0
offset: 0.0
unit: n/a
data
HDF5 dataset
Data typeobject
Shape(1004,)
Array size7.84 KiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)8032
Compressed size (bytes)16064
Compression ratio0.5
timestamps
HDF5 dataset
Data typefloat64
Shape(1004,)
Array size7.84 KiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)8032
Compressed size (bytes)8032
Compression ratio1.0
timestamps_unit: seconds
interval: 1
experiment_ids (TimeSeries)
resolution: -1.0
comments: no comments
description: The experiment_ids coorespond to the encoding (i.e., learning) or recogniton trials. The learning trials are demarcated by: 80. The recognition trials are demarcated by: 81.
conversion: 1.0
offset: 0.0
unit: NA
data
HDF5 dataset
Data typefloat64
Shape(1004,)
Array size7.84 KiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)8032
Compressed size (bytes)8032
Compression ratio1.0
timestamps
HDF5 dataset
Data typefloat64
Shape(1004,)
Array size7.84 KiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)8032
Compressed size (bytes)8032
Compression ratio1.0
timestamps_unit: seconds
interval: 1
stimulus
StimulusPresentation (OpticalSeries)
resolution: -1.0
comments: no comments
description: no description
conversion: 1.0
offset: 0.0
unit: meters
data
HDF5 dataset
Data typeuint8
Shape(200, 400, 300, 3)
Array size68.66 MiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)72000000
Compressed size (bytes)72000000
Compression ratio1.0
timestamps
HDF5 dataset
Data typefloat64
Shape(200,)
Array size1.56 KiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)1600
Compressed size (bytes)1600
Compression ratio1.0
timestamps_unit: seconds
interval: 1
dimension
HDF5 dataset
Data typeint32
Shape(3,)
Array size12.00 bytes
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)12
Compressed size (bytes)12
Compression ratio1.0

[300 400 3]
format: raw
distance: 0.7
field_of_view
HDF5 dataset
Data typefloat64
Shape(3,)
Array size24.00 bytes
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)24
Compressed size (bytes)24
Compression ratio1.0

[0.2 0.3 0.7]
orientation: lower left
keywords
HDF5 dataset
Data typeobject
Shape(7,)
Array size56.00 bytes
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)56
Compressed size (bytes)112
Compression ratio0.5

['Intracranial Recordings' 'Intractable Epilepsy' 'Single-Unit Recordings' 'Cognitive Neuroscience' 'Learning' 'Memory' 'Neurosurgery']
electrodes (ElectrodesTable)
description: metadata about extracellular electrodes
columns
x
the x coordinate of the channel location
y
the y coordinate of the channel location
z
the z coordinate of the channel location
imp
the impedance of the channel
location
the location of channel within the subject e.g. brain region
filtering
description of hardware filtering
group
a reference to the ElectrodeGroup this electrode is a part of
group_name
the name of the ElectrodeGroup this electrode is a part of
origChannel
The original channel ID for the channel
table
x y z imp location filtering group group_name origChannel
id
0 -15.0 -11.2 -16.7 NaN Left Hippocampus 300-3000Hz Neuralynx-cheetah-microwires-2 pynwb.ecephys.ElectrodeGroup at 0x123590735526544\nFields:\n description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)\n device: Neuralynx-cheetah pynwb.device.Device at 0x123590735391568\n location: Left Hippocampus\n Neuralynx-cheetah-microwires-2 2
1 -15.0 -11.2 -16.7 NaN Left Hippocampus 300-3000Hz Neuralynx-cheetah-microwires-3 pynwb.ecephys.ElectrodeGroup at 0x123590735527184\nFields:\n description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)\n device: Neuralynx-cheetah pynwb.device.Device at 0x123590735391568\n location: Left Hippocampus\n Neuralynx-cheetah-microwires-3 3
2 -15.0 -11.2 -16.7 NaN Left Hippocampus 300-3000Hz Neuralynx-cheetah-microwires-4 pynwb.ecephys.ElectrodeGroup at 0x123590735520016\nFields:\n description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)\n device: Neuralynx-cheetah pynwb.device.Device at 0x123590735391568\n location: Left Hippocampus\n Neuralynx-cheetah-microwires-4 4
3 -15.0 -11.2 -16.7 NaN Left Hippocampus 300-3000Hz Neuralynx-cheetah-microwires-6 pynwb.ecephys.ElectrodeGroup at 0x123590735525840\nFields:\n description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)\n device: Neuralynx-cheetah pynwb.device.Device at 0x123590735391568\n location: Left Hippocampus\n Neuralynx-cheetah-microwires-6 6

... and 5 more row(s).

electrode_groups
Neuralynx-cheetah-microwires-19 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Right Amygdala
device (Device)
Neuralynx-cheetah-microwires-2 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Left Hippocampus
device (Device)
Neuralynx-cheetah-microwires-21 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Right Amygdala
device (Device)
Neuralynx-cheetah-microwires-22 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Right Amygdala
device (Device)
Neuralynx-cheetah-microwires-3 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Left Hippocampus
device (Device)
Neuralynx-cheetah-microwires-4 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Left Hippocampus
device (Device)
Neuralynx-cheetah-microwires-6 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Left Hippocampus
device (Device)
Neuralynx-cheetah-microwires-7 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Left Hippocampus
device (Device)
Neuralynx-cheetah-microwires-8 (ElectrodeGroup)
description: Behnke Fried/Micro Inner Wire Bundle (Behnke-Fried BF08R-SP05X-000 and WB09R-SP00X-0B6; Ad-Tech Medical)
location: Left Hippocampus
device (Device)
devices
Neuralynx-cheetah (Device)
intervals
trials (TimeIntervals)
description: experimental trials
columns
start_time
Start time of epoch, in seconds
stop_time
Stop time of epoch, in seconds
stim_on_time
The Time when the Stimulus is Shown
stim_off_time
The Time when the Stimulus is Off
delay1_time
The Time when Delay1 is Off
delay2_time
The Time when Delay2 is Off
stim_phase
Learning/Recognition Phase During the Trial
stimCategory
The Category ID of the Stimulus
category_name
The Category Name of the Stimulus
external_image_file
The File Path to the Stimulus
new_old_labels_recog
The Ground truth Labels for New or Old Stimulus. 0 == Old Stimuli (presented during the learning phase), 1 = New Stimuli (not seen )'during learning phase
response_value
The Response for Each Stimulus
response_time
The Response Time for each Stimulus
table
start_time stop_time stim_on_time stim_off_time delay1_time delay2_time stim_phase stimCategory category_name external_image_file new_old_labels_recog response_value response_time
id
0 6821.208244 6826.937364 6821.208244 6822.210644 6822.711364 6826.937364 learn 5 smallAnimal newolddelay\smallAnimal\7.jpg NA 0.0 6825.934244
1 6827.084244 6830.582604 6827.084244 6828.079084 6828.579804 6830.582604 learn 4 phones newolddelay\phones\34.jpg NA 1.0 6829.579044
2 6830.675884 6833.647084 6830.675884 6831.674324 6832.175044 6833.647084 learn 2 landscapes newolddelay\landscapes\31.jpg NA 1.0 6832.644884
3 6833.779164 6837.162204 6833.779164 6834.778844 6835.279524 6837.162204 learn 3 mobility newolddelay\mobility\30.jpg NA 1.0 6836.166244

... and 196 more row(s).

subject (Subject)
age: 16
description: Right Lateral Frontal
sex: M
species: Human
subject_id: P11HMH
trials (TimeIntervals)
description: experimental trials
columns
start_time
Start time of epoch, in seconds
stop_time
Stop time of epoch, in seconds
stim_on_time
The Time when the Stimulus is Shown
stim_off_time
The Time when the Stimulus is Off
delay1_time
The Time when Delay1 is Off
delay2_time
The Time when Delay2 is Off
stim_phase
Learning/Recognition Phase During the Trial
stimCategory
The Category ID of the Stimulus
category_name
The Category Name of the Stimulus
external_image_file
The File Path to the Stimulus
new_old_labels_recog
The Ground truth Labels for New or Old Stimulus. 0 == Old Stimuli (presented during the learning phase), 1 = New Stimuli (not seen )'during learning phase
response_value
The Response for Each Stimulus
response_time
The Response Time for each Stimulus
table
start_time stop_time stim_on_time stim_off_time delay1_time delay2_time stim_phase stimCategory category_name external_image_file new_old_labels_recog response_value response_time
id
0 6821.208244 6826.937364 6821.208244 6822.210644 6822.711364 6826.937364 learn 5 smallAnimal newolddelay\smallAnimal\7.jpg NA 0.0 6825.934244
1 6827.084244 6830.582604 6827.084244 6828.079084 6828.579804 6830.582604 learn 4 phones newolddelay\phones\34.jpg NA 1.0 6829.579044
2 6830.675884 6833.647084 6830.675884 6831.674324 6832.175044 6833.647084 learn 2 landscapes newolddelay\landscapes\31.jpg NA 1.0 6832.644884
3 6833.779164 6837.162204 6833.779164 6834.778844 6835.279524 6837.162204 learn 3 mobility newolddelay\mobility\30.jpg NA 1.0 6836.166244

... and 196 more row(s).

units (Units)
description: Autogenerated by NWBFile
waveform_unit: volts
columns
origClusterID
The original cluster id
waveform_mean_encoding
The mean waveform for encoding phase.
waveform_mean_recognition
The mean waveform for the recognition phase.
IsolationDist
IsolDist
SNR
SNR
waveform_mean_sampling_rate
The Sampling Rate of Waveform
spike_times
the spike times for each unit
electrodes
the electrodes that each spike unit came from
table
origClusterID waveform_mean_encoding waveform_mean_recognition IsolationDist SNR waveform_mean_sampling_rate spike_times electrodes
id
0 1102 [4.018883149441902, 5.896767433272135, 6.277246736454119, 5.431975982077304, 3.8770681391092783, 2.4546669300628188, 1.7111231685650992, 1.7369680526439102, 2.226307252365844, 2.702073717129048, 2.782513678017329, 2.358633874094165, 1.6097109615873464, 0.8710828531792982, 0.43974141968217334, 0.42460146744738037, 0.7135138632737544, 1.0609621666935902, 1.2369718291311778, 1.1515249402638867, 0.890132515408403, 0.6497702453574656, 0.6189683677297895, 0.8722230399877956, 1.335185199589462, 1.833855098612335, 2.193985270394992, 2.3319642996916565, 2.287621045057279, 2.185177968088822, 2.149433512900832, 2.2272765264583456, 2.3582860218241746, 2.407894396205811, 2.2407563270494646, 1.7908787290002421, 1.0895847095310016, 0.23842665796943105, -0.6541360231339399, -1.5342308910972635, -2.427470835944029, -3.4151959076287284, -4.578240891303991, -5.941960696717651, -7.47000656194889, -9.00487364465439, -10.438943336733699, -11.713673927185924, -12.836505499421309, -13.85961895886385, -14.834586309521322, -15.77022063554369, -16.618054249459124, -17.29315408572588, -17.717535637921085, -17.86107142261333, -17.75721065572435, -17.48602560257726, -17.135874590063533, -16.76648010897854, -16.394233324572387, -16.00678882767047, -15.596868926202996, -15.19458529786143, -14.879268217400707, -14.76425633210283, -14.963741892295099, -15.560512447449279, -16.591984778979597, -18.060633275556324, -19.960687631773705, -22.304090314715282, -25.129999875717495, -28.49238456140248, -32.433174060893975, -36.95649172369192, -42.018284239781856, -47.5363273915551, -53.413856154691956, -59.562787030083506, -65.9136292100723, -72.40765585148043, -78.97749277517697, -85.52878574839475, -91.9344885054869, -98.04559416542993, -103.71258486729157, -108.80608355111572, -113.22629455186652, -116.89781815203715, -119.75497330948171, -121.72785267176911, -122.73822025131814, -122.70803283970467, -122.6061568629708, -121.04066011001208, -118.34414848023327, -114.53809666911533, -109.66443331112981, -103.77369044499052, ...] [8.000045061307883, 10.073790843880522, 10.199355530062219, 8.92208057568135, 6.981416689954849, 5.372255133816067, 4.693249080030427, 4.98446960054179, 5.828975063792222, 6.6405866880574616, 6.981893390542282, 6.759258548814399, 6.217741072085166, 5.7650643173169955, 5.734994428053802, 6.218389919805304, 7.041421341706132, 7.8858943227835825, 8.472953137464785, 8.705939505596046, 8.69994186790075, 8.692908537254166, 8.897362497107483, 9.378733151276133, 10.024613695802762, 10.614405246820066, 10.943105630994422, 10.926838595801486, 10.633238760062266, 10.225520484972582, 9.857706620857003, 9.583031879336046, 9.325998047667651, 8.929699649347922, 8.247205744574908, 7.223081508332884, 5.919989873516446, 4.478868898799423, 3.039177939403373, 1.666577157534888, 0.32841461063908484, -0.9720922901164927, -2.540980727277608, -4.363521421609343, -6.313904676796966, -8.209000483332908, -9.8906218848638, -11.291137914494472, -12.448001692123214, -13.462174384589591, -14.426448901433682, -15.364971743448503, -16.216975590292147, -16.87152335636797, -17.231356858124318, -17.26887656129104, -17.04361252846928, -16.67368163246737, -16.279852355228638, -15.935129481522528, -15.64778134683875, -15.385248538046024, -15.122743702994997, -14.887100724836891, -14.77054651678746, -14.907355915658387, -15.42773036543718, -16.415416995408847, -17.892088249039364, -19.835096136925063, -22.215879902417477, -25.03520812358167, -28.334503960061785, -32.17729077994401, -36.61228290401681, -41.639703608325576, -47.19964928640651, -53.187940873029106, -59.48908660029098, -66.00689772093979, -72.67581642507122, -79.4481079224174, -86.26631846136725, -93.03851930827886, -99.63148865918441, -105.88601702366319, -111.64569179805021, -116.78328845359991, -121.21113025895785, -124.87169274308154, -127.71621258169148, -129.68542171981045, -130.70438411445747, -130.69446914674435, -130.5328291602809, -128.97907964665637, -126.32068025616144, -122.59204913032927, -117.85220873402517, -112.16712625360128, ...] 11.291718 1.440666 [98400.0] [5932.811644, 6081.077044, 6091.982364, 6093.127644, 6095.068204, 6097.438244, 6116.694804, 6129.827604, 6134.825004, 6142.583924, 6148.385364, 6149.993804, 6150.397044, 6155.302324, 6160.684004, 6164.865244, 6165.967804, 6166.810564, 6169.882924, 6173.715884, 6178.882244, 6179.994244, 6190.154284, 6197.473884, 6201.784204, 6204.267124, 6205.795604, 6209.183204, 6214.079844, 6216.054844, 6216.622204, 6220.794284, 6223.041564, 6227.578284, 6241.826004, 6243.708444, 6248.290124, 6249.827244, 6251.844244, 6252.321324, 6255.445964, 6255.450764, 6256.071404, 6262.130524, 6263.449684, 6271.980484, 6273.345364, 6274.503964, 6278.871164, 6282.031884, 6293.636604, 6294.736004, 6298.655764, 6309.551284, 6316.313844, 6317.823484, 6321.783684, 6324.364244, 6326.245564, 6327.291284, 6327.506404, 6343.618684, 6348.224124, 6356.779644, 6811.910324, 6831.062924, 6835.395244, 6837.133324, 6842.687684, 6844.716764, 6852.494204, 6852.676004, 6861.838364, 6867.722964, 6868.506684, 6870.520564, 6870.696084, 6870.992244, 6874.586124, 6875.521284, 6875.526764, 6880.573684, 6884.808964, 6885.198524, 6887.827804, 6891.872644, 6893.842164, 6895.660884, 6905.411844, 6905.945964, 6908.227684, 6909.327524, 6910.216444, 6913.853124, 6920.003524, 6920.500964, 6923.282284, 6923.790284, 6923.815324, 6924.482084, ...] [0]
1 1172 [18.563049885034353, 21.19324494574446, 22.401812794475934, 21.623085347499256, 19.431236109272096, 17.257054741434892, 16.074801716459188, 16.143145899085436, 17.045739633810374, 18.02560183048147, 18.421834904814734, 17.99001544864162, 16.968026153725344, 15.888861901500178, 15.266880969253158, 15.329411234365226, 15.949693216292083, 16.740114774632335, 17.300704714125466, 17.438003333310718, 17.248849591109828, 17.036868848022603, 17.122345160025624, 17.65848953461777, 18.554065443476787, 19.536123369518045, 20.307229439662922, 20.70324985963161, 20.7645954701194, 20.68811932862865, 20.695393310968935, 20.897073621404072, 21.229813261769344, 21.49659863287397, 21.481537279644403, 21.070240711311207, 20.30805210639883, 19.367355767610377, 18.44798148604422, 17.67087532858639, 17.025401137138978, 16.3968017237506, 15.65344661689906, 14.740692279357233, 13.727231951348326, 12.779546325299115, 12.082044446714002, 11.75030626680166, 11.78624165202287, 12.09763016095533, 12.566766596223296, 13.125816028280518, 13.79486327627091, 14.662185269588493, 15.820347574799047, 17.2964131584193, 19.016238832060736, 20.82166254809691, 22.528514894395453, 23.99088880078875, 25.135411136227326, 25.948352302276902, 26.42638958956146, 26.52228883806667, 26.118339521865224, 25.043048903798763, 23.12116540348289, 20.228493965659663, 16.32152011845167, 11.427599883047797, 5.604577772123305, -1.1044197853899353, -8.693986180833445, -17.184516182309256, -26.583605143170917, -36.84424783712168, -47.84436525265354, -59.39910341509034, -71.29831553860784, -83.34786695776805, -95.39253751668893, -107.31019308135642, -118.98407907918666, -130.27240600352712, -140.99500629674205, -150.94598612247626, -159.92581031021803, -167.77530134531068, -174.39368146883103, -179.73268859767663, -183.772705195748, -186.49652964453225, -187.87654723033265, -187.88209395570186, -187.78560578023627, -185.89961672825646, -182.68371544434652, -178.23386613896153, -172.67719796261574, -166.15105747155698, ...] [14.293816754040822, 16.663642031180412, 17.93832140856563, 17.45367927444733, 15.676783504472066, 13.892875397716674, 13.032874826215838, 13.366300094736067, 14.523704466647994, 15.79840702872813, 16.553391306990324, 16.528904536352186, 15.916894186296869, 15.197104700067088, 14.84791617960128, 15.09483351750216, 15.82829314086073, 16.674314258099763, 17.260614598580545, 17.40250604253553, 17.18490580249319, 16.889356349207045, 16.819627848496587, 17.12972149909924, 17.748103371125776, 18.43189377533732, 18.91095104744543, 19.035523941878118, 18.845681958048804, 18.529958365778025, 18.304361440675763, 18.2849835196728, 18.42603710191216, 18.553889716055618, 18.472033202031753, 18.07379059676833, 17.399128615291033, 16.60716551130904, 15.885003106292874, 15.348064858968662, 14.988623674188805, 14.69868623142825, 14.349837419910326, 13.881315470769653, 13.345505219642586, 12.886815071434699, 12.668875978143584, 12.793476106335284, 13.257039865829544, 13.966756543867817, 14.803437723458744, 15.692293008877904, 16.6403167703905, 17.720138253404393, 19.011803532237202, 20.537520396451775, 22.22687127575323, 23.930984948142736, 25.475520372395405, 26.72084545636284, 27.595398164455705, 28.085356566901293, 28.18969949077738, 27.869254878189263, 27.02054530750289, 25.489686872375223, 23.118002636323403, 19.793251895797685, 15.478345318735908, 10.203541241971527, 4.0295522623696645, -2.9948900720512723, -10.855647713963835, -19.563447990245614, -29.118738195211424, -39.47260408607223, -50.50677457261236, -62.04398579243468, -73.88231318569738, -85.83392217022764, -97.74736757187823, -109.50325940945673, -120.9890643867043, -132.07058886611418, -142.57872325512545, -152.32026803675484, -161.10727840842466, -168.78888011435947, -175.26876251558417, -180.5004684072399, -184.46552881648253, -187.1487743223796, -188.52564953176082, -188.5682784628678, -188.4410736337681, -186.61980667731075, -183.50863576678162, -179.19632949239883, -173.80063417353898, -167.4492009481485, ...] 1127.057965 1.752807 [98400.0] [5932.499684, 5932.996044, 5934.152524, 5934.792404, 5936.041364, 5936.599724, 5937.441724, 5937.480604, 6076.087244, 6077.651164, 6077.734964, 6078.820004, 6078.915524, 6079.084164, 6080.952764, 6081.337964, 6081.626524, 6082.158684, 6082.313164, 6082.974684, 6083.134124, 6083.548724, 6083.891364, 6085.843484, 6086.309524, 6086.382524, 6087.530284, 6087.583564, 6088.048324, 6088.334404, 6090.132124, 6090.214764, 6090.904324, 6090.928124, 6091.185524, 6092.920284, 6093.578524, 6094.081524, 6095.008964, 6095.291924, 6096.409884, 6096.744564, 6096.784924, 6096.895124, 6097.047044, 6098.644804, 6098.852724, 6098.883164, 6098.968764, 6099.201764, 6099.303004, 6101.402204, 6101.736484, 6101.845564, 6101.913524, 6101.925364, 6105.165324, 6105.937444, 6108.245884, 6108.273844, 6108.621604, 6112.968244, 6113.097444, 6113.840364, 6114.854764, 6116.422444, 6116.456404, 6117.170244, 6117.241644, 6122.098604, 6122.459604, 6122.507964, 6124.270644, 6124.363644, 6125.045244, 6125.416524, 6126.749324, 6127.446884, 6128.330564, 6128.761164, 6129.264644, 6129.467924, 6129.705484, 6130.600764, 6130.752844, 6131.145164, 6134.384284, 6136.312604, 6136.908324, 6137.267084, 6137.515084, 6138.210124, 6139.085044, 6139.107524, 6139.378524, 6139.417244, 6139.474964, 6139.808564, 6141.286084, 6141.316004, ...] [0]
2 1192 [-4.13895469721813, -5.240674392157537, -5.465173420682089, -4.506389221495265, -2.9875254540437446, -1.6425600657929138, -0.9550435486225758, -1.0130495008598295, -1.5544284162327573, -2.159074969626442, -2.4813850212728275, -2.4088249502390666, -2.0807109347816772, -1.7767863889357802, -1.7483846422188674, -2.0854528071092044, -2.6840204694001, -3.319880443695133, -3.7782469596838912, -3.964746742232856, -3.9402496928449446, -3.8681188279942766, -3.910557724600574, -4.135013210552806, -4.4809448781981835, -4.800204802140057, -4.9429117240717355, -4.837891270912167, -4.5235914820696586, -4.115995483714717, -3.7359355147601376, -3.4392082567337168, -3.18838409399864, -2.879363970696192, -2.4042963742425716, -1.7132848135584298, -0.8403372654102429, 0.11880325460412527, 1.0615193110423797, 1.932964915139275, 2.749643493064013, 3.5803854611369164, 4.4979370354796275, 5.530291257090213, 6.639481628086941, 7.738543817668003, 8.735178936855789, 9.57614108653087, 10.267344331788456, 10.859793440846977, 11.411396446417104, 11.947939750116433, 12.44581850819152, 12.845594009007716, 13.08741749383757, 13.147301822077216, 13.053718315507497, 12.876160001664392, 12.693650542340755, 12.56217156107919, 12.499609878220408, 12.495841105585056, 12.54074501378025, 12.652944606620759, 12.892371585412693, 13.34972834677469, 14.119388852877218, 15.271384929499071, 16.837853994105785, 18.820273942493998, 21.211578935388765, 24.01899750283859, 27.273727479978163, 31.021803986344075, 35.301623552853826, 40.12105077238282, 45.44674291863122, 51.21081980398746, 57.3299380285878, 63.72510592506858, 70.33089536632971, 77.08953700001959, 83.93447437444281, 90.77395639869273, 97.48486295604344, 103.92072478211162, 109.92969716590454, 115.37289527751722, 120.13392448713167, 124.11608629295257, 127.23107139399931, 129.3876453687438, 130.48832495952468, 130.4369153808066, 130.31995863736782, 128.58903240402745, 125.56740976658524, 121.2535008335999, 115.67366058309652, 108.87256150537974, ...] [-1.8190292764526959, -2.721747652455219, -3.3792886424360775, -3.003887786826723, -1.9460103141926408, -0.8009399549809061, -0.030726483779379418, 0.19091400795962316, -0.011561276862548093, -0.3378511036297102, -0.49370560820006787, -0.33977384681470907, 0.05908071670433471, 0.49470958485631944, 0.7421264715298299, 0.6791563581225438, 0.33857004049884815, -0.1266734852392431, -0.5372551585718426, -0.7844409375983844, -0.8780936826536387, -0.9247815387312867, -1.0544819643129986, -1.3405021848223915, -1.7570996220817734, -2.1950584406673403, -2.5352916489906927, -2.6802668815833712, -2.6274041379317, -2.4473898576172815, -2.234196069055553, -2.0464809689760632, -1.813972338010055, -1.5405402301219844, -1.133762012580243, -0.47161161802207713, 0.29318055282595706, 1.1242885312095885, 1.9738551454590654, 2.8113366914999243, 3.6395574448986836, 4.611716839324275, 5.539251496655387, 6.456062283608822, 7.372934596500468, 8.266213657126947, 9.087962028086885, 9.789082283037173, 10.340459629215816, 10.740029196626676, 11.003292743712018, 11.14500644768397, 11.16451057729568, 11.044088193991337, 10.761201288945811, 10.306886576595174, 9.699103840073185, 8.983223912381245, 8.219758574886821, 7.466955915248914, 6.768568460397776, 6.1535663128779285, 5.647161256952957, 5.285756482277759, 5.126290271414256, 5.243983003909644, 5.719428461865849, 6.622104485520021, 7.999163318650094, 9.8749320883691, 12.260120365985465, 15.164165230804896, 18.60264859310346, 22.594996036086826, 27.153605441250615, 32.270592089400076, 37.90958692925088, 44.00692502175909, 50.48110059595013, 57.24481301638365, 64.21287690182794, 71.30216056419265, 78.42468275464336, 85.47905632457208, 92.34630722767825, 98.89338785769833, 104.98319084403869, 110.48625753973607, 115.28870726805407, 119.29341626280116, 122.41552352542757, 124.57650022254734, 125.70150770436977, 125.72245936162608, 125.64511652673214, 124.08296805198933, 121.33043646878063, 117.39363324812966, 112.29978524336562, 106.09253766277014, ...] 20.535532 1.431687 [98400.0] [5932.363124, 6079.039444, 6080.817844, 6083.155124, 6083.700204, 6083.923364, 6086.331924, 6086.363404, 6088.494004, 6090.516124, 6093.186924, 6095.069884, 6100.753924, 6106.016484, 6107.617524, 6109.696364, 6109.780084, 6110.109564, 6112.685084, 6117.006204, 6119.500244, 6121.968204, 6122.408844, 6124.480084, 6126.214724, 6129.688084, 6132.446404, 6134.841284, 6136.139004, 6139.459124, 6139.514964, 6142.837204, 6146.818444, 6148.681004, 6149.610764, 6157.610604, 6157.816484, 6159.625244, 6159.683044, 6161.667884, 6162.024884, 6165.502444, 6165.949084, 6172.806524, 6174.315444, 6179.863164, 6185.777724, 6185.856964, 6194.790404, 6196.313284, 6197.342364, 6199.485444, 6201.144684, 6203.418924, 6211.634764, 6212.028284, 6215.273364, 6216.266884, 6216.302484, 6223.056004, 6224.170804, 6225.259284, 6227.753204, 6230.620564, 6230.911164, 6233.041724, 6234.504284, 6241.763044, 6242.360124, 6242.466924, 6243.121324, 6244.564124, 6245.940844, 6247.023484, 6250.209924, 6254.643684, 6263.675084, 6265.897404, 6270.905804, 6276.178804, 6276.493204, 6277.863444, 6279.056844, 6280.063364, 6280.852644, 6281.079244, 6282.334764, 6283.602404, 6286.654244, 6290.825404, 6297.550324, 6299.274964, 6302.223244, 6302.763204, 6304.202364, 6304.326284, 6308.169164, 6308.222724, 6308.299564, 6309.222204, ...] [0]
3 1218 [-18.250031869613768, -20.816355317271896, -21.79601460686142, -20.853303170040874, -18.589481708715283, -16.412147491903198, -15.302134871288217, -15.502891087198556, -16.53676987675002, -17.691793512700098, -18.267630272802762, -18.04144259541648, -17.239615903003816, -16.38516599200144, -15.982779499114738, -16.25353434789565, -17.050626859173434, -17.979529755608635, -18.63677477012689, -18.82776703510932, -18.64887819573544, -18.402548169781394, -18.406902442340957, -18.813106646331693, -19.52987900677398, -20.287911462543857, -20.797673075810195, -20.90632974771672, -20.6671033001183, -20.28917661911674, -20.00476579640526, -19.933462165783812, -20.02000735285124, -20.075598097305384, -19.892944764852256, -19.36585697513372, -18.545863225068285, -17.607823484302617, -16.74932119632655, -16.084407158165174, -15.591891545876903, -15.144105226897588, -14.595076667216091, -13.874584909630416, -13.052139389956798, -12.239959314619536, -11.605369414016717, -11.248712584057783, -11.156961670121854, -11.222459778911425, -11.31203142723111, -11.343974742067225, -11.328329132948342, -11.349171622533273, -11.501931858491496, -11.823794025111871, -12.257356053063738, -12.666797498186613, -12.894943391709948, -12.826908198449168, -12.424027887154551, -11.710659961189368, -10.724352549719416, -9.460444952876424, -7.843943681486811, -5.744388083834843, -3.024080036022284, 0.40859597223891186, 4.570624433260512, 9.414868004535649, 14.870919965514984, 20.89070213160928, 27.471838567601914, 34.64602559277962, 42.44048430999704, 50.835822669481836, 59.74476281310524, 69.02323055000532, 78.50637625563587, 88.04835956680809, 97.54381847143017, 106.92074268214097, 116.11155993344246, 125.02152341028747, 133.5141326595748, 141.4225473669371, 148.58054874928612, 154.85567546484816, 160.16676801089724, 164.47796545026935, 167.7750123780695, 170.0393764180685, 171.23582463947704, 171.32021596494138, 171.19193795056586, 169.6420307502056, 166.9790915911604, 163.27216144560464, 158.61727557841883, 153.11797623301428, ...] [-16.681364481563907, -19.135737449481013, -20.064554230050327, -19.158695218967395, -16.767784905675782, -14.354673148412429, -12.985652691540677, -12.982406565473143, -13.943955940930223, -15.08357178706771, -15.686007420824515, -15.455363488418376, -14.60219204548126, -13.662464899441543, -13.174099569956265, -13.393583593996118, -14.19419855306242, -15.178235956539242, -15.920632626738053, -16.200714068415593, -16.099119218749713, -15.920658259819724, -16.001033426598188, -16.51303575580381, -17.37827307373376, -18.324186838913022, -19.043412669250426, -19.359689420299794, -19.308198266022504, -19.092325790268294, -18.9500764250222, -19.011234470733008, -19.22598348507177, -19.40042686088154, -19.31236754450861, -18.837436899102464, -18.014188991896297, -17.015377593067626, -16.047300621642727, -15.238231236763118, -14.579478276885755, -13.949197622752902, -13.20054576781635, -12.260323133070814, -11.181190620204449, -10.119851641432316, -9.256983089985727, -8.706875512793111, -8.468060540062128, -8.440264915726715, -8.494042392046307, -8.550194929453646, -8.62273261227383, -8.802268136505864, -9.191923610555122, -9.834425912304459, -10.672343223052735, -11.562621875472836, -12.334725613601977, -12.857456998885123, -13.076411893554436, -13.002767024285726, -12.662979976259038, -12.041003725374695, -11.047508825691724, -9.533603259646522, -7.340298192010766, -4.354936632199474, -0.5431583922828134, 4.059581266459927, 9.38908560740443, 15.400054334297625, 22.091894745578035, 29.4983730604522, 37.64840649180155, 46.521728900357424, 56.025204183196216, 66.0027215207557, 76.27196657066597, 86.66652462644863, 97.06001880723296, 107.36067653667314, 117.48245958574657, 127.31217744540506, 136.6934086657376, 145.43736974987488, 153.35486107395474, 160.29157413640326, 166.14796734824785, 170.87466167945476, 174.44868341298235, 176.84635820940684, 178.02941999462053, 177.9520524926929, 177.81467025224907, 175.9951805461872, 172.91681554801036, 168.6553151998711, 163.31894744485592, 157.0281061937494, ...] 804.779154 1.672149 [98400.0] [5932.154284, 5932.205764, 5935.794204, 6075.656764, 6075.862804, 6076.909404, 6077.777724, 6078.612524, 6084.229164, 6084.490004, 6084.870124, 6085.668604, 6087.308964, 6087.476404, 6087.909044, 6089.335964, 6089.815164, 6090.007164, 6090.483404, 6092.014724, 6092.949884, 6093.667004, 6095.325324, 6100.587684, 6101.263204, 6101.948804, 6103.779324, 6103.965084, 6105.010204, 6108.869604, 6109.347964, 6110.091244, 6110.588364, 6111.632804, 6114.695644, 6115.055244, 6115.134124, 6115.227884, 6115.661244, 6115.696764, 6116.928964, 6117.049844, 6118.601964, 6119.299444, 6122.545324, 6128.363444, 6129.314164, 6131.717284, 6133.914004, 6134.767484, 6134.781484, 6134.792524, 6134.805044, 6135.182364, 6135.215924, 6135.226924, 6137.703044, 6138.234804, 6139.897564, 6139.972284, 6141.300084, 6141.698924, 6142.153244, 6143.651044, 6145.200524, 6145.504364, 6147.942604, 6148.516524, 6148.672124, 6149.004364, 6149.108844, 6149.937244, 6150.263764, 6152.077724, 6152.609924, 6153.018204, 6153.346644, 6153.485044, 6153.675004, 6153.870964, 6155.446044, 6157.040164, 6157.620804, 6157.646604, 6157.685124, 6157.696364, 6159.598364, 6162.207044, 6163.202684, 6163.299884, 6164.571964, 6166.221164, 6167.798404, 6168.090764, 6168.646004, 6169.049124, 6172.479004, 6174.095604, 6176.561724, 6176.749804, ...] [0]

... and 21 more row(s).

experiment_description: The data contained within this file describes a new/old recogntion task performed in patients with intractable epilepsy implanted with depth electrodes and Behnke-Fried microwires in the human Medical Temporal Lobe (MTL).
lab: Rutishauser
institution: Hunigton Memorial Hospital
data_collection: learning: 80, recognition: 81


For more advanced use cases, the :py:class:~pynwb.NWBHDF5IO class provides additional functionality. Below, we demonstrate how :py:class:~pynwb.NWBHDF5IO can be used as a context manager to read data from an NWB file in a more controlled manner:

from pynwb import NWBHDF5IO
with NWBHDF5IO(filepath, mode="r") as io2:
    nwbfile2 = io2.read()

    # data accessible here

# data not accessible here

The advantage of using a context manager is that the file is closed automatically when the context finishes successfully or if there is an error. Be aware that if you use this method, closing the context (unindenting the code) will automatically close the NWBHDF5IO object and the corresponding h5py File object. The data not already read from the NWB file will then be inaccessible, so any code that reads data must be placed within the context.

Access stimulus data

Data representing stimuli that were presented to the experimental subject are stored in stimulus within the NWBFile object.

nwbfile.stimulus

stimulus

StimulusPresentation (OpticalSeries)

StimulusPresentation (OpticalSeries)

resolution: -1.0
comments: no comments
description: no description
conversion: 1.0
offset: 0.0
unit: meters
data
HDF5 dataset
Data typeuint8
Shape(200, 400, 300, 3)
Array size68.66 MiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)72000000
Compressed size (bytes)72000000
Compression ratio1.0
timestamps
HDF5 dataset
Data typefloat64
Shape(200,)
Array size1.56 KiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)1600
Compressed size (bytes)1600
Compression ratio1.0
timestamps_unit: seconds
interval: 1
dimension
HDF5 dataset
Data typeint32
Shape(3,)
Array size12.00 bytes
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)12
Compressed size (bytes)12
Compression ratio1.0

[300 400 3]
format: raw
distance: 0.7
field_of_view
HDF5 dataset
Data typefloat64
Shape(3,)
Array size24.00 bytes
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)24
Compressed size (bytes)24
Compression ratio1.0

[0.2 0.3 0.7]
orientation: lower left


NWBFile.stimulus is a dictionary that can contain PyNWB objects representing different types of data, such as images (grayscale, RGB) or time series of images. In this file, NWBFile.stimulus contains a single key “StimulusPresentation” with an OpticalSeries object representing what images were shown to the subject and at what times.

nwbfile.stimulus["StimulusPresentation"]

StimulusPresentation (OpticalSeries)

resolution: -1.0
comments: no comments
description: no description
conversion: 1.0
offset: 0.0
unit: meters
data
HDF5 dataset
Data typeuint8
Shape(200, 400, 300, 3)
Array size68.66 MiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)72000000
Compressed size (bytes)72000000
Compression ratio1.0
timestamps
HDF5 dataset
Data typefloat64
Shape(200,)
Array size1.56 KiB
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)1600
Compressed size (bytes)1600
Compression ratio1.0
timestamps_unit: seconds
interval: 1
dimension
HDF5 dataset
Data typeint32
Shape(3,)
Array size12.00 bytes
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)12
Compressed size (bytes)12
Compression ratio1.0

[300 400 3]
format: raw
distance: 0.7
field_of_view
HDF5 dataset
Data typefloat64
Shape(3,)
Array size24.00 bytes
Chunk shapeNone
CompressionNone
Compression optsNone
Uncompressed size (bytes)24
Compressed size (bytes)24
Compression ratio1.0

[0.2 0.3 0.7]
orientation: lower left


Lazy loading of datasets

Data arrays are read passively from the NWB file. Accessing the data attribute of the OpticalSeries object does not read the data values, but presents an h5py.Dataset object that can be indexed to read data. You can use the [:] operator to read the entire data array into memory.

stimulus_presentation = nwbfile.stimulus["StimulusPresentation"]
all_stimulus_data = stimulus_presentation.data[:]

Images may be 3D or 4D (grayscale or RGB), where the first dimension must be time (frame). The second and third dimensions represent x and y. The fourth dimension represents the RGB value (length of 3) for color images.

stimulus_presentation.data.shape
(200, 400, 300, 3)

This OpticalSeries data contains 200 images of size 400x300 pixels with three channels (red, green, and blue).

Slicing datasets

It is often preferable to read only a portion of the data. To do this, index or slice into the data attribute just like if you were indexing or slicing a numpy array.

frame_index = 31
image = stimulus_presentation.data[frame_index]
# Reverse the last dimension because the data were stored in BGR instead of RGB
image = image[..., ::-1]
plt.imshow(image, aspect="auto")
plot read basics
<matplotlib.image.AxesImage object at 0x7067b5d47410>

Access single unit data

Data and metadata about sorted single units are stored in Units object. It stores metadata about each single unit in a tabular form, where each row represents a unit with spike times and additional metadata.

See also

You can learn more about units in the Extracellular Electrophysiology Data tutorial.

units = nwbfile.units

We can view the single unit data as a DataFrame.

origClusterID waveform_mean_encoding waveform_mean_recognition IsolationDist SNR waveform_mean_sampling_rate spike_times electrodes
id
0 1102 [4.018883149441902, 5.896767433272135, 6.27724... [8.000045061307883, 10.073790843880522, 10.199... 11.291718 1.440666 [98400.0] [5932.811644, 6081.077044, 6091.982364, 6093.1... x y ... group...
1 1172 [18.563049885034353, 21.19324494574446, 22.401... [14.293816754040822, 16.663642031180412, 17.93... 1127.057965 1.752807 [98400.0] [5932.499684, 5932.996044, 5934.152524, 5934.7... x y ... group...
2 1192 [-4.13895469721813, -5.240674392157537, -5.465... [-1.8190292764526959, -2.721747652455219, -3.3... 20.535532 1.431687 [98400.0] [5932.363124, 6079.039444, 6080.817844, 6083.1... x y ... group...
3 1218 [-18.250031869613768, -20.816355317271896, -21... [-16.681364481563907, -19.135737449481013, -20... 804.779154 1.672149 [98400.0] [5932.154284, 5932.205764, 5935.794204, 6075.6... x y ... group...
4 2017 [-7.009145964385704, -8.32920965742021, -8.098... [0.23215033352949355, -1.4792817502708107, -2.... 53.676326 1.401921 [98400.0] [5933.886604, 5933.895124, 6082.117284, 6083.7... x y ... group...


To access the spike times of the first single unit, index units with the column name “spike_times” and then the row index, 0. All times in NWB are stored in seconds relative to the session start time.

units["spike_times"][0]
array([5932.811644, 6081.077044, 6091.982364, 6093.127644, 6095.068204,
       6097.438244, 6116.694804, 6129.827604, 6134.825004, 6142.583924,
       6148.385364, 6149.993804, 6150.397044, 6155.302324, 6160.684004,
       6164.865244, 6165.967804, 6166.810564, 6169.882924, 6173.715884,
       6178.882244, 6179.994244, 6190.154284, 6197.473884, 6201.784204,
       6204.267124, 6205.795604, 6209.183204, 6214.079844, 6216.054844,
       6216.622204, 6220.794284, 6223.041564, 6227.578284, 6241.826004,
       6243.708444, 6248.290124, 6249.827244, 6251.844244, 6252.321324,
       6255.445964, 6255.450764, 6256.071404, 6262.130524, 6263.449684,
       6271.980484, 6273.345364, 6274.503964, 6278.871164, 6282.031884,
       6293.636604, 6294.736004, 6298.655764, 6309.551284, 6316.313844,
       6317.823484, 6321.783684, 6324.364244, 6326.245564, 6327.291284,
       6327.506404, 6343.618684, 6348.224124, 6356.779644, 6811.910324,
       6831.062924, 6835.395244, 6837.133324, 6842.687684, 6844.716764,
       6852.494204, 6852.676004, 6861.838364, 6867.722964, 6868.506684,
       6870.520564, 6870.696084, 6870.992244, 6874.586124, 6875.521284,
       6875.526764, 6880.573684, 6884.808964, 6885.198524, 6887.827804,
       6891.872644, 6893.842164, 6895.660884, 6905.411844, 6905.945964,
       6908.227684, 6909.327524, 6910.216444, 6913.853124, 6920.003524,
       6920.500964, 6923.282284, 6923.790284, 6923.815324, 6924.482084,
       6929.252164, 6933.061564, 6934.280164, 6937.846484, 6940.652804,
       6943.622284, 6950.313084, 6950.363204, 6954.785244, 6954.933924,
       6959.337924, 6962.573244, 6963.663164, 6966.080444, 6966.146044,
       6970.827564, 6970.979524, 6973.283724, 6987.875764, 6992.403004,
       6993.954124, 6996.946764, 6997.249284, 6999.875764, 7004.600924,
       7008.001244, 7009.398204, 7011.068564, 7017.730804, 7019.278004,
       7024.695844, 7027.928084, 7041.186004, 7043.951164, 7044.394164,
       7052.429444, 7053.377284, 7054.072164, 7072.272564, 7072.836364,
       7073.433204, 7073.844604, 7073.901084, 7079.004124, 7080.598844,
       7083.965564, 7084.016084, 7086.730804, 7090.336364, 7101.254244,
       7114.549764, 7116.230604, 7119.653564, 7119.685684, 7122.680484,
       7127.814804, 7129.421884, 7141.764244, 7143.759004, 7147.602804,
       7149.140324, 7151.777524, 7157.066044, 7157.118404, 7158.532644,
       7334.693084, 7335.015444, 7336.416404, 7340.052364, 7345.172164,
       7363.078724, 7365.825364, 7375.025684, 7381.381804, 7382.057444,
       7382.416484, 7382.997684, 7383.204804, 7384.305124, 7386.312724,
       7386.694364, 7389.302964, 7396.554924, 7397.925444, 7402.733404,
       7406.523084, 7407.317684, 7409.281964, 7411.153164, 7412.606124,
       7413.468804, 7420.820204, 7424.900604, 7425.235964, 7425.489244,
       7426.975844, 7431.840764, 7434.457964, 7436.035524, 7436.086484,
       7438.281564, 7444.681404, 7446.768444, 7452.015684, 7455.865644,
       7459.695124, 7469.229324, 7469.447164, 7470.021844, 7475.205524,
       7480.513124, 7485.459604, 7487.198044, 7491.487884, 7491.529244,
       7503.131844, 7509.601444, 7518.232884, 7520.176244, 7522.629044,
       7526.786084, 7527.225084, 7527.668244, 7531.142964, 7539.957964,
       7541.722804, 7543.518964, 7547.003564, 7549.018484, 7549.069804,
       7550.374044, 7551.872924, 7553.427244, 7563.422044, 7566.144044,
       7567.238124, 7568.862244, 7569.073564, 7575.387404, 7583.933364,
       7584.624484, 7594.007284, 7594.054964, 7595.263164, 7596.982804,
       7602.387084, 7605.198924, 7605.552324, 7606.064884, 7606.100604,
       7606.471644, 7608.873244, 7611.354804, 7611.386804, 7615.139004,
       7620.247884, 7621.979604, 7629.935284, 7632.076484, 7635.188044,
       7635.195444, 7650.052684, 7954.638503, 7954.781023, 7955.781503,
       7957.472423, 8737.861503, 8738.114583, 8745.994143, 8751.983023,
       8752.956743, 8759.633223, 8763.513143, 8767.843183, 8769.437743,
       8769.722423, 8770.277983, 8771.478103, 8774.213063, 8775.969463,
       8788.423623, 8788.552143, 8788.986663, 8794.429063, 8796.650303,
       8797.612063, 8799.980783, 8800.796863, 8803.618063, 8803.886663,
       8803.947783, 8806.479783, 8809.613743, 8811.981903, 8821.248783,
       8821.956583, 8823.353583, 8832.448903, 8833.632423, 8835.896503,
       8846.307303, 8853.548143, 8854.569343, 8855.609823, 8858.938063,
       8859.377623, 8861.211783, 8861.520783, 8863.375583, 8874.494623,
       8875.107303, 8877.309463, 8890.033823, 8893.205503, 8893.727743,
       8902.555583, 8908.088743, 8909.480663, 8909.599623, 8917.999063,
       8918.456263, 8922.668823, 8924.168263, 8925.681703, 8934.790023,
       8941.341583, 8941.729463, 8946.041383, 8953.742663, 8955.295183,
       8955.777943, 8958.401863, 8959.338423, 8968.056023, 8968.512463,
       8970.277183, 8971.752023, 8974.239503, 8983.962903, 8985.244303,
       8988.592263, 8991.135303, 8991.409423, 8992.908263, 8995.101423,
       8997.635343, 8999.058143, 9002.087743, 9003.022143, 9003.163143,
       9003.484943, 9003.741023, 9008.767263, 9008.915183, 9010.942143,
       9010.976183, 9013.915423, 9015.557943, 9018.411263, 9023.921183,
       9030.917263, 9043.395623, 9049.802143, 9053.791543, 9065.276343,
       9074.427783, 9077.736063, 9081.744663, 9082.581383, 9088.609223,
       9089.031503, 9089.147943, 9098.463543])

Visualize spiking activity relative to stimulus onset

We can look at when these single units spike relative to when image stimuli were presented to the subject. We will iterate over the first 3 units and get their spike times. Then for each unit, we will iterate over each stimulus onset time and compute the spike times relative to stimulus onset. Finally, we will create a raster plot and histogram of these aligned spike times.

before = 1.0  # in seconds
after = 3.0

# Get the stimulus times for all stimuli
# get_timestamps() works whether the time is stored as an array of timestamps or as
# starting time and sampling rate.
stim_on_times = stimulus_presentation.get_timestamps()

for unit in range(3):
    unit_spike_times = nwbfile.units["spike_times"][unit]
    trial_spikes = []
    for time in stim_on_times:
        # Compute spike times relative to stimulus onset
        aligned_spikes = unit_spike_times - time
        # Keep only spike times in a given time window around the stimulus onset
        aligned_spikes = aligned_spikes[
            (-before < aligned_spikes) & (aligned_spikes < after)
        ]
        trial_spikes.append(aligned_spikes)
    fig, axs = plt.subplots(2, 1, sharex="all")
    plt.xlabel("time (s)")
    axs[0].eventplot(trial_spikes)

    axs[0].set_ylabel("trial")
    axs[0].set_title("unit {}".format(unit))
    axs[0].axvline(0, color=[0.5, 0.5, 0.5])

    axs[1].hist(np.hstack(trial_spikes), 30)
    axs[1].axvline(0, color=[0.5, 0.5, 0.5])
  • unit 0
  • unit 1
  • unit 2

Access Trials

Trials are stored as TimeIntervals object which is a subclass of DynamicTable. DynamicTable objects are used to store metadata about each trial in a tabular form, where each row represents a trial and has a start time, stop time, and additional metadata.

See also

You can learn more about trials in the Annotating Time Intervals tutorial.

Similarly to Units, we can view trials as a pandas.DataFrame.

trials_df = nwbfile.trials.to_dataframe()
trials_df.head()
start_time stop_time stim_on_time stim_off_time delay1_time delay2_time stim_phase stimCategory category_name external_image_file new_old_labels_recog response_value response_time
id
0 6821.208244 6826.937364 6821.208244 6822.210644 6822.711364 6826.937364 learn 5 smallAnimal newolddelay\smallAnimal\7.jpg NA 0.0 6825.934244
1 6827.084244 6830.582604 6827.084244 6828.079084 6828.579804 6830.582604 learn 4 phones newolddelay\phones\34.jpg NA 1.0 6829.579044
2 6830.675884 6833.647084 6830.675884 6831.674324 6832.175044 6833.647084 learn 2 landscapes newolddelay\landscapes\31.jpg NA 1.0 6832.644884
3 6833.779164 6837.162204 6833.779164 6834.778844 6835.279524 6837.162204 learn 3 mobility newolddelay\mobility\30.jpg NA 1.0 6836.166244
4 6837.301244 6840.487044 6837.301244 6838.304044 6838.804724 6840.487044 learn 3 mobility newolddelay\mobility\13.jpg NA 1.0 6839.487884


The stimulus can be mapped one-to-one to each row (trial) of trials based on the stim_on_time column.

assert np.all(stimulus_presentation.timestamps[:] == trials_df.stim_on_time[:])

Visualize the first 3 images that were categorized as landscapes in the session:

stim_on_times_landscapes = trials_df[
    trials_df.category_name == "landscapes"
].stim_on_time
for time in stim_on_times_landscapes.iloc[:3]:
    img = np.squeeze(
        stimulus_presentation.data[
            np.where(stimulus_presentation.timestamps[:] == time)
        ]
    )
    # Reverse the last dimension because the data were stored in BGR instead of RGB
    img = img[..., ::-1]
    plt.figure()
    plt.imshow(img, aspect="auto")
  • plot read basics
  • plot read basics
  • plot read basics

Exploring the NWB file

So far we have explored the NWB file by printing the NWBFile object and accessing its attributes, but it may be useful to explore the data in a more interactive, visual way. See Exploring NWB Files for an updated list of programs for exploring NWB files.

Close the open NWB file

It is good practice, especially on Windows, to close any files that you have opened.

Total running time of the script: (0 minutes 7.886 seconds)

Gallery generated by Sphinx-Gallery