Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Reading an NWB File

After downloading an NWB file, you may want to view the data inside. You can do basic reading of the file with PyNWB. This is a package designed to utilize, modify, and process NWB files. The basic read functionality of PyNWB is shown below.

Environment Setup

⚠️Note: If running on a new environment, run this cell once and then restart the kernel⚠️

import warnings
warnings.filterwarnings('ignore')

try:
    from databook_utils.dandi_utils import dandi_stream_open
except:
    !git clone https://github.com/AllenInstitute/openscope_databook.git
    %cd openscope_databook
    %pip install -e .
    %cd ./docs/basics
import pandas as pd

from dandi import dandiapi
from pynwb import NWBHDF5IO

Downloading an NWB File

To read an NWB File, it must first be downloaded. dandiset_id and dandi_filepath may be changed to select a different file off of DANDI. If the file of interest is already downloaded, you don’t need to run the download cell again. When trying to download an embargoed file, refer to the code from the Downloading an NWB File notebook.

dandiset_id = "000021"
dandi_filepath = "sub-723627600/sub-723627600_ses-742951821.nwb"
download_loc = "."
filename = dandi_filepath.split("/")[-1]
filepath = f"{download_loc}/{filename}"

print(filename)
print(filepath)
sub-723627600_ses-742951821.nwb
./sub-723627600_ses-742951821.nwb
client = dandiapi.DandiAPIClient()
my_dandiset = client.get_dandiset(dandiset_id)
file = my_dandiset.get_asset_by_path(dandi_filepath)
# this may take awhile, especially if the file to download is large
file.download(filepath)

print(f"Downloaded file to {filepath}")
A newer version (0.75.1) of dandi/dandi-cli is available. You are using 0.74.3
Downloaded file to ./sub-723627600_ses-742951821.nwb
# change pandas settings to show whole table
pd.set_option('display.max_rows', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

Reading an NWB File

You can read in a NWB file with PyNWB’s NWBHDF5IO method to retrieve an io object. You can use the .read method to actually read it in. From there, you can see the raw data of the NWB file and can print the fields you are interested in.

# takes relative or absolute filepath as first argument
io = NWBHDF5IO(f"{download_loc}/{filename}", mode="r", load_namespaces=True)
nwb = io.read()
nwb
Loading...
### uncomment these to view aspects of the file

### not all of these exist for all NWB files (Key Errors will arise if the fields don't exist for this file)

# nwb.identifier
# nwb.processing
# nwb.acquisition["events"]
# nwb.intervals["trials"]
# nwb.stimulus["StimulusPresentation"]
# nwb.electrodes

OpenScope’s File Schemas

OpenScope works with two main types of brain data files for different experiments. There’s Extracellular Electrophysiology, Ecephys, which uses Neuropixels probes to gather electrical data, and Optical Physiology, Ophys, which uses 2-Photon Calcium imaging to gather fluorescence data. Since the NWB Standard is very flexible, and these files are used for different needs, there are several differences between these data types. These table schemas were made as comprehensive descriptions of what goes in our NWB files. Below, Pandas is used to display a general outline for each file type where every field within the NWB file is described. Note that NWB files or files from other institutions could vary from these significantly. These tables are just meant to be used as a reference, don’t be overwhelmed trying to understand all the fields!

# change pandas settings to show whole table
pd.set_option('display.max_rows', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

Ecephys File

Note that this is the schema for the older OpenScope NWBs, and does not necessarily apply to newer ones

pd.read_csv("../../data/ecephys_schema.csv")
Loading...

Ophys File

Note that this is the schema for the older OpenScope NWBs, and does not necessarily apply to newer ones

pd.read_csv("../../data/ophys_schema.csv")
Loading...