PyNWB

3.1.3 · active · verified Thu Apr 16

PyNWB is the official Python API for working with Neurodata Without Borders (NWB) files, a standardized data format for neurophysiology data. It facilitates reading, writing, and manipulating NWB files, providing a high-level interface to the underlying HDF5 structure. The current version is 3.1.3, and it maintains an active release cadence with regular minor updates and less frequent major versions supporting new NWB schema versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic NWB file, add a TimeSeries to it, and then write and read the file from disk using `NWBFile` and `NWBHDF5IO`. It covers the fundamental steps for data archival in the NWB format.

from datetime import datetime
from pynwb import NWBFile, TimeSeries, NWBHDF5IO
import numpy as np
import os

# 1. Create a new NWBFile
nwbfile = NWBFile(
    session_description='My first NWB file tutorial',
    identifier='NWB_Tutorial_1',
    session_start_time=datetime.now().astimezone(),
    experimenter='John Doe',
    lab='Neuroscience Lab',
    institution='University of Example',
    experiment_description='Simple data creation for testing PyNWB.'
)

# 2. Add some TimeSeries data
data = np.random.rand(100, 1)
timestamps = np.linspace(0, 9.9, 100)
series = TimeSeries(
    name='my_timeseries',
    data=data,
    timestamps=timestamps,
    unit='volts',
    description='A simple random time series.'
)

nwbfile.add_acquisition(series)

# 3. Write the NWBFile to disk
filename = 'quickstart_example.nwb'
with NWBHDF5IO(filename, 'w') as io:
    io.write(nwbfile)

print(f"NWB file '{filename}' created successfully.")

# 4. Read the NWBFile from disk
with NWBHDF5IO(filename, 'r') as io:
    read_nwbfile = io.read()
    print(f"\nRead NWB file. Session description: {read_nwbfile.session_description}")
    read_series = read_nwbfile.acquisition['my_timeseries']
    print(f"Read TimeSeries data shape: {read_series.data.shape}")
    print(f"Read TimeSeries unit: {read_series.unit}")

# Clean up the created file
# os.remove(filename)

view raw JSON →