Interactive NWB Data Exploration

Interactive NWB Data Exploration#

As we saw in the last section, the NWB file contains a ton of information about the experiment, as well as the actual data. We can look through each of the groups in the NWB file to pull out this information, or we can use a handy widget created by the NWB team.

This widget will also let us take a sneak peek at the behavioral and neural data as well. As a first pass, it can be very useful to explore the dataset in this high level way. This allows you to inspect the data for quality, and look for any interesting trends.

First, let’s set up our notebook and read the NWB file that we previously downloaded.

import os
from pynwb import NWBHDF5IO

# Try to import nwbwidgets (may fail due to package incompatibilities)
try:
    from nwbwidgets import nwb2widget
    nwbwidgets_available = True
    print('nwbwidgets successfully imported.')
except (ImportError, AttributeError) as e:
    nwbwidgets_available = False
    print('nwbwidgets could not be imported (package compatibility issue).')
    print('This is expected when building the book. The widget works on Dandihub.')

# Set the filename
filename = '000006/sub-anm369962/sub-anm369962_ses-20170310.nwb'

# Check for alternative file locations
if not os.path.exists(filename):
    alt_filename = '../' + filename
    if os.path.exists(alt_filename):
        filename = alt_filename
    else:
        alt_filename2 = os.path.join('Lesson_1', filename)
        if os.path.exists(alt_filename2):
            filename = alt_filename2

# Read the NWB file
if os.path.exists(filename):
    io = NWBHDF5IO(filename, 'r')
    nwb_file = io.read()
    print('NWB file found and read.')
    print(type(nwb_file))
else:
    print(f"NWB file not found at {filename}")
    print("Download the dataset using the previous notebooks to use this feature.")
nwbwidgets could not be imported (package compatibility issue).
This is expected when building the book. The widget works on Dandihub.
NWB file found and read.
<class 'pynwb.file.NWBFile'>

Below, we’ll take advantage of the nwbwidgets package to explore the data. This package usefully automatically renders information and visualizations of the data, depending on what it is. We’ll use the nwb2widget function to explore the dataset we read above.

To take a peek at the neural data here, you can look under the “Units” header below.

Note: You won’t be able to see the output from the nwb4edu.github.io website. To see the output below, launch this in an interactive environment!

if nwbwidgets_available:
    nwb2widget(nwb_file)
else:
    print("nwbwidgets is not available in this environment.")
    print("To use the interactive widget, run this notebook on Dandihub.")
    print("\nYou can still explore the NWB file programmatically.")
    print("For example, try: nwb_file.units.to_dataframe()")
nwbwidgets is not available in this environment.
To use the interactive widget, run this notebook on Dandihub.

You can still explore the NWB file programmatically.
For example, try: nwb_file.units.to_dataframe()

Questions for consideration

  1. What is the raw data being used to plot the visualizations in “units”? What is this data showing us?

  2. What kinds of visualizations do you see under “units”? Which of these are summary visualizations, versus more raw visualizations?

  3. Do you see any interesting trends in the data?


Lesson 1 Wrap Up#

In this first lesson, we showed you how to explore an NWB file. We discussed common elements in the structure, and how you can use nwbwidgets to explore the data at a high level.

Hopefully now you can:

✔️ Explain the three common features of datasets: metadata, raw data, and processed data

✔️ Identify and implement multiple ways to obtain a NWB dataset via DANDI

✔️ Explore the metadata contained in a typical Dandiset

✔️ Use an interactive widget for a big picture view of a Dandiset

In the following lesson, we’ll demonstrate different kinds of visualizations and analyses that you might do with the data.

Before moving on, try the Lesson #1 problem set to test your understanding and to put some of these new skills to work. 💪

Additional Resources#