### Setup: Create an Example NWB File Source: https://pynwb.readthedocs.io/en/stable/_sources/tutorials/general/plot_timeintervals.rst.txt Initializes an NWBFile object with essential session information and adds example TimeSeries objects. Ensure all required fields like session_description, identifier, and session_start_time are provided. ```Python from datetime import datetime from uuid import uuid4 import numpy as np from dateutil.tz import tzlocal from pynwb import NWBFile, TimeSeries # create the NWBFile nwbfile = NWBFile( session_description="my first synthetic recording", # required identifier=str(uuid4()), # required session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal()), # required experimenter="Baggins, Bilbo", # optional lab="Bag End Laboratory", # optional institution="University of Middle Earth at the Shire", # optional experiment_description="I went on an adventure with thirteen dwarves to reclaim vast treasures.", # optional session_id="LONELYMTN", # optional ) # create some example TimeSeries test_ts = TimeSeries( name="series1", data=np.arange(1000), unit="m", timestamps=np.linspace(0.5, 601, 1000), ) rate_ts = TimeSeries(name="series2", data=np.arange(600), unit="V", starting_time=0.0, rate=1.0) # Add the TimeSeries to the file nwbfile.add_acquisition(test_ts) nwbfile.add_acquisition(rate_ts) ``` -------------------------------- ### Test installation Source: https://pynwb.readthedocs.io/en/stable/make_a_release.html Installs the package and verifies the version. ```bash $ pip install pynwb && \ python -c "import pynwb; print(pynwb.__version__)" ``` -------------------------------- ### Install PyNWB from Git Repository Source: https://pynwb.readthedocs.io/en/stable/_sources/install_developers.rst.txt Clones the PyNWB repository, navigates into it, installs requirements, and then installs PyNWB in editable mode. Ensure your virtual environment is activated. ```bash git clone --recurse-submodules https://github.com/NeurodataWithoutBorders/pynwb.git cd pynwb pip install -r requirements.txt -r requirements-dev.txt pip install -e . ``` -------------------------------- ### Create virtual environment Source: https://pynwb.readthedocs.io/en/stable/make_a_release.html Sets up a clean virtual environment for testing the installation. ```bash $ python -m venv pynwb-${release}-install-test && \ source pynwb-${release}-install-test/bin/activate ``` -------------------------------- ### Test Pynwb Installation Source: https://pynwb.readthedocs.io/en/stable/_sources/make_a_release.rst.txt Install pynwb using pip and verify the installed version. This confirms the release was published correctly. ```bash $ pip install pynwb && \ python -c "import pynwb; print(pynwb.__version__)" ``` -------------------------------- ### Full ICEphys Example Source: https://pynwb.readthedocs.io/en/stable/tutorials/domain/plot_icephys.html This comprehensive example demonstrates the creation and population of an NWB file with intracellular electrophysiology data, including stimulus, response, and various recording tables. It concludes by writing the file and optionally visualizing its hierarchy. ```python ex_nwbfile = NWBFile( session_description="my first synthetic recording", identifier=str(uuid4()), session_start_time=datetime.now(tzlocal()), experimenter="Baggins, Bilbo", lab="Bag End Laboratory", institution="University of Middle Earth at the Shire", experiment_description="I went on an adventure with thirteen dwarves " "to reclaim vast treasures.", session_id="LONELYMTN", ) # Add a device ex_device = ex_nwbfile.create_device(name="Heka ITC-1600") # Add an intracellular electrode ex_electrode = ex_nwbfile.create_icephys_electrode( name="elec0", description="a mock intracellular electrode", device=ex_device ) # Create an ic-ephys stimulus ex_stimulus = VoltageClampStimulusSeries( name="stimulus", data=[1, 2, 3, 4, 5], starting_time=123.6, rate=10e3, electrode=ex_electrode, gain=0.02, ) # Create an ic-response ex_response = VoltageClampSeries( name="response", data=[0.1, 0.2, 0.3, 0.4, 0.5], conversion=1e-12, resolution=np.nan, starting_time=123.6, rate=20e3, electrode=ex_electrode, gain=0.02, capacitance_slow=100e-12, resistance_comp_correction=70.0, ) # (A) Add an intracellular recording to the file # NOTE: We can optionally define time-ranges for the stimulus/response via # the corresponding optional _start_index and _index_count parameters. # NOTE: It is allowed to add a recording with just a stimulus or a response # NOTE: We can add custom columns to any of our tables in steps (A)-(E) ex_ir_index = ex_nwbfile.add_intracellular_recording( electrode=ex_electrode, stimulus=ex_stimulus, response=ex_response ) # (B) Add a list of sweeps to the simultaneous recordings table ex_sweep_index = ex_nwbfile.add_icephys_simultaneous_recording( recordings=[ ex_ir_index, ] ) # (C) Add a list of simultaneous recordings table indices as a sequential recording ex_sequence_index = ex_nwbfile.add_icephys_sequential_recording( simultaneous_recordings=[ ex_sweep_index, ], stimulus_type="square", ) # (D) Add a list of sequential recordings table indices as a repetition run_index = ex_nwbfile.add_icephys_repetition( sequential_recordings=[ ex_sequence_index, ] ) # (E) Add a list of repetition table indices as a experimental condition ex_nwbfile.add_icephys_experimental_condition( repetitions=[ run_index, ] ) # Write our test file ex_testpath = "ex_test_icephys_file.nwb" with NWBHDF5IO(ex_testpath, "w") as io: io.write(ex_nwbfile) # Read the data back in with NWBHDF5IO(ex_testpath, "r") as io: infile = io.read() # Optionally plot the organization of our example NWB file try: import matplotlib.pyplot as plt from hdmf_docutils.doctools.render import ( HierarchyDescription, NXGraphHierarchyDescription, ) ex_file_hierarchy = HierarchyDescription.from_hdf5(ex_testpath) ex_file_graph = NXGraphHierarchyDescription(ex_file_hierarchy) ex_fig = ex_file_graph.draw( show_plot=False, figsize=(12, 16), label_offset=(0.0, 0.0065), label_font_size=10, ) plt.show() except ImportError: # ignore in case hdmf_docutils is not installed pass ``` -------------------------------- ### Install fsspec and dependencies Source: https://pynwb.readthedocs.io/en/stable/tutorials/advanced_io/streaming.html Install the fsspec library and its dependencies for HTTP access. ```bash pip install fsspec requests aiohttp ``` -------------------------------- ### Install HDF5 Filter Plugins Source: https://pynwb.readthedocs.io/en/stable/_downloads/fc427b0f711a91f63a34bd461bce8dab/h5dataio.ipynb Command to install the hdf5plugin library for accessing additional compression filters. ```bash pip install hdf5plugin ``` -------------------------------- ### Create and Write Intracellular Electrophysiology Data Source: https://pynwb.readthedocs.io/en/stable/_downloads/5df2e9d78ab96c19ddefb60483bbda7e/plot_icephys.ipynb This example demonstrates the creation of an NWBFile for intracellular electrophysiology data, including adding devices, electrodes, stimulus, and response series. It then writes the data to an NWB file and reads it back. This is a comprehensive example for setting up and saving electrophysiology recordings. ```python # Create an ICEphysFile ex_nwbfile = NWBFile( session_description="my first synthetic recording", identifier=str(uuid4()), session_start_time=datetime.now(tzlocal()), experimenter="Baggins, Bilbo", lab="Bag End Laboratory", institution="University of Middle Earth at the Shire", experiment_description="I went on an adventure with thirteen dwarves " "to reclaim vast treasures.", session_id="LONELYMTN", ) # Add a device ex_device = ex_nwbfile.create_device(name="Heka ITC-1600") # Add an intracellular electrode ex_electrode = ex_nwbfile.create_icephys_electrode( name="elec0", description="a mock intracellular electrode", device=ex_device ) # Create an ic-ephys stimulus ex_stimulus = VoltageClampStimulusSeries( name="stimulus", data=[1, 2, 3, 4, 5], starting_time=123.6, rate=10e3, electrode=ex_electrode, gain=0.02, ) # Create an ic-response ex_response = VoltageClampSeries( name="response", data=[0.1, 0.2, 0.3, 0.4, 0.5], conversion=1e-12, resolution=np.nan, starting_time=123.6, rate=20e3, electrode=ex_electrode, gain=0.02, capacitance_slow=100e-12, resistance_comp_correction=70.0, ) # (A) Add an intracellular recording to the file # NOTE: We can optionally define time-ranges for the stimulus/response via # the corresponding optional _start_index and _index_count parameters. # NOTE: It is allowed to add a recording with just a stimulus or a response # NOTE: We can add custom columns to any of our tables in steps (A)-(E) ex_ir_index = ex_nwbfile.add_intracellular_recording( electrode=ex_electrode, stimulus=ex_stimulus, response=ex_response ) # (B) Add a list of sweeps to the simultaneous recordings table ex_sweep_index = ex_nwbfile.add_icephys_simultaneous_recording( recordings=[ ex_ir_index, ] ) # (C) Add a list of simultaneous recordings table indices as a sequential recording ex_sequence_index = ex_nwbfile.add_icephys_sequential_recording( simultaneous_recordings=[ ex_sweep_index, ], stimulus_type="square", ) # (D) Add a list of sequential recordings table indices as a repetition run_index = ex_nwbfile.add_icephys_repetition( sequential_recordings=[ ex_sequence_index, ] ) # (E) Add a list of repetition table indices as a experimental condition ex_nwbfile.add_icephys_experimental_condition( repetitions=[ run_index, ] ) # Write our test file ex_testpath = "ex_test_icephys_file.nwb" with NWBHDF5IO(ex_testpath, "w") as io: io.write(ex_nwbfile) # Read the data back in with NWBHDF5IO(ex_testpath, "r") as io: infile = io.read() # Optionally plot the organization of our example NWB file try: import matplotlib.pyplot as plt from hdmf_docutils.doctools.render import ( HierarchyDescription, NXGraphHierarchyDescription, ) ex_file_hierarchy = HierarchyDescription.from_hdf5(ex_testpath) ex_file_graph = NXGraphHierarchyDescription(ex_file_hierarchy) ex_fig = ex_file_graph.draw( show_plot=False, figsize=(12, 16), label_offset=(0.0, 0.0065), label_font_size=10, ) plt.show() exce ``` -------------------------------- ### NWBFile Print Output Source: https://pynwb.readthedocs.io/en/stable/tutorials/advanced_io/plot_linking_data.html Example output showing the structure of an NWBFile read from a file. ```text root pynwb.file.NWBFile at 0x135339594174352 Fields: acquisition: { example_timeseries } file_create_date: [datetime.datetime(2025, 12, 9, 23, 41, 27, 300876, tzinfo=tzlocal())] identifier: 7090b8db-ed8d-40f6-88aa-93f19a5f586f session_description: example file family session_start_time: 2025-12-09 23:41:27.300665+00:00 timestamps_reference_time: 2025-12-09 23:41:27.300665+00:00 ``` -------------------------------- ### Terminal command example Source: https://pynwb.readthedocs.io/en/stable/make_a_release.html Demonstrates the convention for indicating terminal commands. ```bash $ echo "Hello" Hello ``` -------------------------------- ### TimeSeriesReferenceVectorData._data Property Source: https://pynwb.readthedocs.io/en/stable/pynwb.base.html Gets the selected data values from the TimeSeriesReferenceVectorData. This is a convenience function to slice data from the referenced TimeSeries based on the start index and count. ```APIDOC ## GET /timeseries/reference/data ### Description Get the selected data values. This is a convenience function to slice data from the `timeseries` based on the given `idx_start` and `count`. ### Method GET ### Endpoint /timeseries/reference/data ### Parameters #### Query Parameters - **idx_start** (int) - The starting index for slicing. - **count** (int) - The number of elements to slice. ``` -------------------------------- ### Initialize NWB Tutorial Environment Source: https://pynwb.readthedocs.io/en/stable/_downloads/97f01f679548c378585d3bb5f80d5b4e/images.ipynb Imports necessary libraries and defines file paths for the tutorial. ```python from datetime import datetime import os from uuid import uuid4 import numpy as np from dateutil import tz from PIL import Image from pynwb import NWBHDF5IO, NWBFile from pynwb.base import Images from pynwb.image import GrayscaleImage, ImageSeries, OpticalSeries, RGBAImage, RGBImage from pynwb.misc import AbstractFeatureSeries # Define file paths used in the tutorial nwbfile_path = os.path.abspath("images_tutorial.nwb") moviefiles_path = [ os.path.abspath("image/file_1.tiff"), os.path.abspath("image/file_2.tiff"), os.path.abspath("image/file_3.tiff"), ] ``` -------------------------------- ### Get TimeSeries Timestamps Source: https://pynwb.readthedocs.io/en/stable/_modules/pynwb/base.html Retrieves the timestamps corresponding to the selected data segment. Timestamps are either loaded directly or calculated from the starting time and sampling rate. ```python @property def timestamps(self): """ Get the floating point timestamp offsets in seconds from the timeseries that correspond to the array. These are either loaded directly from the :py:meth:`~pynwb.base.TimeSeriesReference.timeseries` timestamps or calculated from the starting time and sampling rate. :raises IndexError: If the combination of :py:meth:`~pynwb.base.TimeSeriesReference.idx_start` and :py:meth:`~pynwb.base.TimeSeriesReference.count` are not valid for the given timeseries. :raises TypeError: If one of the fields does not match the expected type :returns: Array with the timestamps. """ # isvalid will be False only if both idx_start and count are negative. Otherwise well get errors or be True. if not self.isvalid(): return None # load the data from the timestamps elif self.timeseries.timestamps is not None: return self.timeseries.timestamps[self.idx_start: (self.idx_start + self.count)] # construct the timestamps from the starting_time and rate else: start_time = self.timeseries.rate * self.idx_start + self.timeseries.starting_time return np.arange(0, self.count) * self.timeseries.rate + start_time ``` -------------------------------- ### Create NWBFile with Custom Extensions Source: https://pynwb.readthedocs.io/en/stable/_downloads/100e961199add479f48fdec3829db901/extensions.ipynb Demonstrates setting up an NWBFile, defining electrode groups, and adding a custom TetrodeSeries extension to the acquisition. ```python from datetime import datetime from dateutil.tz import tzlocal from pynwb import NWBFile start_time = datetime(2017, 4, 3, 11, tzinfo=tzlocal()) create_date = datetime(2017, 4, 15, 12, tzinfo=tzlocal()) nwbfile = NWBFile( "demonstrate caching", "NWB456", start_time, file_create_date=create_date ) device = nwbfile.create_device(name="trodes_rig123") electrode_name = "tetrode1" description = "an example tetrode" location = "somewhere in the hippocampus" electrode_group = nwbfile.create_electrode_group( electrode_name, description=description, location=location, device=device ) for idx in [1, 2, 3, 4]: nwbfile.add_electrode( id=idx, x=1.0, y=2.0, z=3.0, imp=float(-idx), location="CA1", filtering="none", group=electrode_group, ) electrode_table_region = nwbfile.create_electrode_table_region( [0, 2], "the first and third electrodes" ) import numpy as np rate = 10.0 np.random.seed(1234) data_len = 1000 data = np.random.rand(data_len * 2).reshape((data_len, 2)) timestamps = np.arange(data_len) / rate ts = TetrodeSeries( "test_ephys_data", data, electrode_table_region, timestamps=timestamps, trode_id=1, # Alternatively, could specify starting_time and rate as follows # starting_time=ephys_timestamps[0], # rate=rate, resolution=0.001, comments="This data was randomly generated with numpy, using 1234 as the seed", description="Random numbers generated with numpy.random.rand", ) nwbfile.add_acquisition(ts) ``` -------------------------------- ### Build Documentation Source: https://pynwb.readthedocs.io/en/stable/_sources/make_a_tutorial.rst.txt Commands to navigate to the documentation directory and generate the HTML output. ```bash cd docs make html ``` -------------------------------- ### Install PyNWB from PyPI using pip Source: https://pynwb.readthedocs.io/en/stable/_sources/install_users.rst.txt Use this command to install or update the PyNWB distribution from the Python Package Index (PyPI). It automatically installs required dependencies. ```bash $ pip install -U pynwb ``` -------------------------------- ### Create a mock Device Source: https://pynwb.readthedocs.io/en/stable/_modules/pynwb/testing/mock/device.html Instantiates a Device object with optional parameters and adds it to an NWBFile if provided. ```python def mock_Device( name: Optional[str] = None, description: str = "description", manufacturer: Optional[str] = None, nwbfile: Optional[NWBFile] = None, ) -> Device: device = Device( name=name or name_generator("Device"), description=description, manufacturer=manufacturer, ) if nwbfile is not None: nwbfile.add_device(device) return device ``` -------------------------------- ### Initialize NWBFile and Subject Source: https://pynwb.readthedocs.io/en/stable/_downloads/2761b4ab54eebc844f8375ea67ea4946/plot_configurator.ipynb Sets up the NWBFile with required metadata and associates a Subject object. ```python try: dir_path = os.path.dirname(os.path.abspath(__file__)) # when running as a .py except NameError: dir_path = os.path.dirname(os.path.abspath("__file__")) # when running as a script or notebook yaml_file = os.path.join(dir_path, 'nwb_gallery_config.yaml') load_type_config(config_path=yaml_file) session_start_time = datetime(2018, 4, 25, hour=2, minute=30, second=3, tzinfo=tz.gettz("US/Pacific")) nwbfile = NWBFile( session_description="Mouse exploring an open field", # required identifier=str(uuid4()), # required session_start_time=session_start_time, # required session_id="session_1234", # optional experimenter=[ "Bilbo Baggins", ], # optional lab="Bag End Laboratory", # optional institution="University of My Institution", # optional experiment_description="I went on an adventure to reclaim vast treasures.", # optional related_publications="DOI:10.1016/j.neuron.2016.12.011", # optional ) subject = Subject( subject_id="001", age="P90D", description="mouse 5", species="Mus musculus", sex="M", ) nwbfile.subject = subject ``` -------------------------------- ### Install dandi package Source: https://pynwb.readthedocs.io/en/stable/_downloads/e565a512f7954270d1b0cc38def66513/streaming.ipynb Install the latest release of the dandi package using pip. ```bash pip install dandi ``` -------------------------------- ### Write and read NWBFile Source: https://pynwb.readthedocs.io/en/stable/_downloads/5df2e9d78ab96c19ddefb60483bbda7e/plot_icephys.ipynb Demonstrates writing an NWBFile to disk using NWBHDF5IO and reading it back. ```python # Write our test file testpath = "test_icephys_file.nwb" with NWBHDF5IO(testpath, "w") as io: io.write(nwbfile) # Read the data back in with NWBHDF5IO(testpath, "r") as io: infile = io.read() ``` -------------------------------- ### Install fsspec dependencies Source: https://pynwb.readthedocs.io/en/stable/_downloads/e565a512f7954270d1b0cc38def66513/streaming.ipynb Install the necessary packages to enable fsspec and its HTTP filesystem implementation. ```bash pip install fsspec requests aiohttp ``` -------------------------------- ### NWBH5IOMixin Setup and Teardown Source: https://pynwb.readthedocs.io/en/stable/_modules/pynwb/testing/testh5io.html Sets up and tears down resources for NWB file roundtrip testing. It initializes container objects, file names, and readers, and ensures cleanup of created files. ```python from datetime import datetime from dateutil.tz import tzlocal, tzutc import os from abc import ABCMeta, abstractmethod import warnings from pynwb import NWBFile, NWBHDF5IO, get_manager, validate as pynwb_validate from .utils import remove_test_file from hdmf.backends.warnings import BrokenLinkWarning from hdmf.build.warnings import MissingRequiredBuildWarning [docs] class NWBH5IOMixin(metaclass=ABCMeta): """ Mixin class for methods to run a roundtrip test writing an NWB file with an Container and reading the Container from the NWB file. The setUp, test_roundtrip, and tearDown methods will be run by unittest. The abstract methods setUpContainer, addContainer, and getContainer needs to be implemented by classes that include this mixin. Example:: class TestMyContainerIO(NWBH5IOMixin, TestCase): def setUpContainer(self): # return a test Container to read/write def addContainer(self, nwbfile): # add the test Container to an NWB file def getContainer(self, nwbfile): # return the test Container from an NWB file This code is adapted from hdmf.testing.H5RoundTripMixin. """ [docs] def setUp(self): self.container = self.setUpContainer() self.start_time = datetime(1971, 1, 1, 12, tzinfo=tzutc()) self.create_date = datetime(2018, 4, 15, 12, tzinfo=tzlocal()) self.container_type = self.container.__class__.__name__ self.filename = 'test_%s.nwb' % self.container_type self.export_filename = 'test_export_%s.nwb' % self.container_type self.reader = None self.export_reader = None [docs] def tearDown(self): if self.reader is not None: self.reader.close() if self.export_reader is not None: self.export_reader.close() remove_test_file(self.filename) remove_test_file(self.export_filename) ``` -------------------------------- ### Initialize NWBFile Source: https://pynwb.readthedocs.io/en/stable/_downloads/6fed62c9fc13b548dd8f87a03f01ec6d/plot_behavior.ipynb Creates an NWBFile object with required metadata fields such as session description, identifier, and start time. ```python nwbfile = NWBFile( session_description="my first synthetic recording", identifier=str(uuid4()), session_start_time=datetime.now(tzlocal()), experimenter=[ "Baggins, Bilbo", ], lab="Bag End Laboratory", institution="University of Middle Earth at the Shire", experiment_description="I went on an adventure to reclaim vast treasures.", session_id="LONELYMTN001", ) nwbfile ``` -------------------------------- ### Install dandi package Source: https://pynwb.readthedocs.io/en/stable/tutorials/advanced_io/streaming.html Install the dandi package to interact with the DANDI Archive. This is a prerequisite for fetching file locations. ```bash pip install dandi ``` -------------------------------- ### Install virtualenv Tool Source: https://pynwb.readthedocs.io/en/stable/_sources/install_developers.rst.txt Installs the virtualenv tool for creating isolated Python environments. Ensure you are in your project directory before running. ```bash pip install -U virtualenv virtualenv venv ``` -------------------------------- ### Initialize NWBHDF5IO for Writing Source: https://pynwb.readthedocs.io/en/stable/pynwb.html Instantiate NWBHDF5IO to create a new NWB file or overwrite an existing one in write mode. Specify the desired file path. ```python with NWBHDF5IO(self.export_path, mode='w') as export_io: ``` -------------------------------- ### Get Data Interface from ProcessingModule Source: https://pynwb.readthedocs.io/en/stable/_modules/pynwb/base.html Retrieves an NWBDataInterface or DynamicTable from a ProcessingModule by its name. The `get` method is the preferred way to retrieve containers. ```python from pynwb.base import ProcessingModule # Assume module is an instance of ProcessingModule # Assume 'interface_name' is the name of the data interface # interface = module.get('interface_name') ``` -------------------------------- ### Install PyNWB from Conda-forge using conda Source: https://pynwb.readthedocs.io/en/stable/_sources/install_users.rst.txt Use this command to install or update the PyNWB distribution from conda-forge using the conda package manager. ```bash $ conda install -c conda-forge pynwb ``` -------------------------------- ### Initialize NWBFile Source: https://pynwb.readthedocs.io/en/stable/_downloads/fc427b0f711a91f63a34bd461bce8dab/h5dataio.ipynb Create an NWBFile instance to serve as a container for testing data. ```python from datetime import datetime from dateutil.tz import tzlocal from pynwb import NWBFile start_time = datetime(2017, 4, 3, 11, tzinfo=tzlocal()) nwbfile = NWBFile( session_description="demonstrate advanced HDF5 I/O features", identifier="NWB123", session_start_time=start_time, ) ``` -------------------------------- ### Create a mock DeviceModel Source: https://pynwb.readthedocs.io/en/stable/_modules/pynwb/testing/mock/device.html Instantiates a DeviceModel object with optional parameters and adds it to an NWBFile if provided. ```python def mock_DeviceModel( name: Optional[str] = None, manufacturer: str = None, model_number: Optional[str] = None, description: str = "description", nwbfile: Optional[NWBFile] = None, ) -> DeviceModel: device = DeviceModel( name=name or name_generator("DeviceModel"), manufacturer=manufacturer, model_number=model_number, description=description, ) if nwbfile is not None: nwbfile.add_device_model(device) return device ``` -------------------------------- ### Get S3 URL of NWB file on DANDI Source: https://pynwb.readthedocs.io/en/stable/_downloads/e565a512f7954270d1b0cc38def66513/streaming.ipynb Use DandiAPIClient to get the S3 URL of an NWB file using its dandiset ID and filepath. ```python from dandi.dandiapi import DandiAPIClient dandiset_id = '000006' # ephys dataset from the Svoboda Lab filepath = 'sub-anm372795/sub-anm372795_ses-20170718.nwb' # 450 kB file with DandiAPIClient() as client: asset = client.get_dandiset(dandiset_id, 'draft').get_asset_by_path(filepath) s3_url = asset.get_content_url(follow_redirects=1, strip_query=True) ``` -------------------------------- ### Configure HDF5 Dataset Properties Source: https://pynwb.readthedocs.io/en/stable/_downloads/fc427b0f711a91f63a34bd461bce8dab/h5dataio.ipynb Example output showing various configurations for chunking, compression, and maxshape settings in HDF5 datasets. ```python name=test_regular_timeseries, chunks=None, compression=None, maxshape=(10,) name=test_compressed_timeseries, chunks=(10,), compression=gzip, maxshape=(10,) name=test_chunked_timeseries, chunks=(250, 5), compression=None, maxshape=(None, 10) name=test_gzipped_timeseries, chunks=(250, 5), compression=gzip, maxshape=(1000, 10) ``` -------------------------------- ### Install h5py with S3 support via Conda Source: https://pynwb.readthedocs.io/en/stable/_downloads/e565a512f7954270d1b0cc38def66513/streaming.ipynb Use these commands to remove the standard PyPI version and install the Conda version that includes S3 support. ```bash pip uninstall h5py conda install h5py ``` -------------------------------- ### Install remfile package Source: https://pynwb.readthedocs.io/en/stable/_downloads/e565a512f7954270d1b0cc38def66513/streaming.ipynb Install the remfile library using pip. This library is used for indexing and streaming files in s3, optimized for reading HDF5 files. ```bash pip install remfile ``` -------------------------------- ### Create and Activate Virtual Environment for Testing Source: https://pynwb.readthedocs.io/en/stable/_sources/make_a_release.rst.txt Create a new virtual environment to test the installation of the new release. This ensures a clean environment for verification. ```bash $ python -m venv pynwb-${release}-install-test && \ source pynwb-${release}-install-test/bin/activate ``` -------------------------------- ### Create and Write NWBFile with PotatoSack Source: https://pynwb.readthedocs.io/en/stable/tutorials/general/extensions.html Demonstrates how to create an NWBFile, add a custom data structure (PotatoSack) to it, and write it to disk. Requires `PotatoSack` and `Potato` classes to be defined. ```python potato_sack = PotatoSack(potatos=Potato(name="potato1", age=2.3, weight=3.0)) potato_sack.add_potato(Potato("potato2", 3.0, 4.0)) potato_sack.create_potato("big_potato", 10.0, 20.0) nwbfile = NWBFile( "a file with metadata", "NB123A", datetime(2018, 6, 1, tzinfo=tzlocal()) ) pmod = nwbfile.create_processing_module("module_name", "desc") pmod.add_container(potato_sack) with NWBHDF5IO("test_multicontainerinterface.nwb", "w") as io: io.write(nwbfile) ``` -------------------------------- ### Generate Citations with duecredit (CLI) Source: https://pynwb.readthedocs.io/en/stable/_sources/overview_citing.rst.txt Use the duecredit command-line tool to generate citations for your Python script. Ensure duecredit is installed via 'pip install duecredit'. ```bash cd /path/to/your/module python -m duecredit yourscript.py ``` -------------------------------- ### Install h5py with conda for S3 support Source: https://pynwb.readthedocs.io/en/stable/tutorials/advanced_io/streaming.html Install or reinstall h5py using conda to ensure ROS3 (S3) support is enabled, as PyPI packages may not include this feature. ```bash pip uninstall h5py conda install h5py ``` -------------------------------- ### Initialize NWBHDF5IO for Reading Source: https://pynwb.readthedocs.io/en/stable/pynwb.html Instantiate NWBHDF5IO to open an existing NWB file in read mode. Ensure the file path is correct. ```python with NWBHDF5IO(self.read_path, mode='r') as read_io: ``` -------------------------------- ### Import required modules Source: https://pynwb.readthedocs.io/en/stable/_downloads/c1b4d2ad435866f3b2898961e5464969/plot_icephys_pandas.ipynb Initial imports for the tutorial. ```python import os ``` -------------------------------- ### Create and Activate Conda Environment Source: https://pynwb.readthedocs.io/en/stable/_sources/install_developers.rst.txt Creates a new conda environment named 'venv' with Python 3.9 installed and then activates it. Use 'conda install' for packages within this environment. ```bash conda create --name venv python=3.9 conda activate venv ``` -------------------------------- ### Validate NWB File Against Installed Core Specification Source: https://pynwb.readthedocs.io/en/stable/_sources/validation.rst.txt Use the --no-cached-namespace flag to validate against the core NWB specification version installed with PyNWB, useful for checking against newer or older versions. ```bash pynwb-validate --no-cached-namespace test.nwb ``` -------------------------------- ### Initialize NWBFile and TimeSeries Source: https://pynwb.readthedocs.io/en/stable/_downloads/601cd348078a6ff30e9a0d90f70c8b55/plot_timeintervals.ipynb Creates a basic NWBFile instance and adds two sample TimeSeries objects to the acquisition group. ```python from datetime import datetime from uuid import uuid4 import numpy as np from dateutil.tz import tzlocal from pynwb import NWBFile, TimeSeries # create the NWBFile nwbfile = NWBFile( session_description="my first synthetic recording", # required identifier=str(uuid4()), # required session_start_time=datetime(2017, 4, 3, 11, tzinfo=tzlocal()), # required experimenter="Baggins, Bilbo", # optional lab="Bag End Laboratory", # optional institution="University of Middle Earth at the Shire", # optional experiment_description="I went on an adventure with thirteen dwarves to reclaim vast treasures.", # optional session_id="LONELYMTN", # optional ) # create some example TimeSeries test_ts = TimeSeries( name="series1", data=np.arange(1000), unit="m", timestamps=np.linspace(0.5, 601, 1000), ) rate_ts = TimeSeries( name="series2", data=np.arange(600), unit="V", starting_time=0.0, rate=1.0 ) # Add the TimeSeries to the file nwbfile.add_acquisition(test_ts) nwbfile.add_acquisition(rate_ts) ``` -------------------------------- ### Create OptogeneticSeries Source: https://pynwb.readthedocs.io/en/stable/tutorials/domain/ogen.html Create an OptogeneticSeries to store the laser power data over time. Specify the name, generated data, associated stimulus site, and sampling rate. The series starts at the session start time by default. ```python import numpy as np from pynwb.ogen import OptogeneticSeries ogen_series = OptogeneticSeries( name="OptogeneticSeries", data=np.random.randn(20), # watts site=ogen_site, rate=30.0, # Hz ) nwbfile.add_stimulus(ogen_series) ``` -------------------------------- ### Create Device Object Source: https://pynwb.readthedocs.io/en/stable/_downloads/5df2e9d78ab96c19ddefb60483bbda7e/plot_icephys.ipynb Create a Device object using the NWBFile instance method. This represents hardware used in the experiment. ```python device = nwbfile.create_device(name="Heka ITC-1600") ``` -------------------------------- ### Create Images and IndexSeries Source: https://pynwb.readthedocs.io/en/stable/_downloads/97f01f679548c378585d3bb5f80d5b4e/images.ipynb Initializes an Images collection and an IndexSeries to reference images by index. ```python from pynwb.base import ImageReferences from pynwb.image import GrayscaleImage, Images, IndexSeries, RGBImage images = Images( name="raccoons", images=[rgb_logo, gs_logo], description="A collection of raccoons.", order_of_images=ImageReferences("order_of_images", [rgb_logo, gs_logo]), ) idx_series = IndexSeries( name="stimuli", data=[0, 1, 0, 1], indexed_images=images, unit="N/A", timestamps=[0.1, 0.2, 0.3, 0.4], ) ``` -------------------------------- ### Install Latest PyNWB Pre-release Source: https://pynwb.readthedocs.io/en/stable/_sources/install_users.rst.txt Install the latest pre-release version of PyNWB, useful for testing new features or setting up CI against the latest development version. This command uses pip and specifies a link to the latest release on GitHub. ```bash $ pip install -U pynwb --find-links https://github.com/NeurodataWithoutBorders/pynwb/releases/tag/latest --no-index ``` -------------------------------- ### Create a mock ImagingPlane Source: https://pynwb.readthedocs.io/en/stable/_modules/pynwb/testing/mock/ophys.html Generates an ImagingPlane instance. If an NWBFile is provided, it adds the plane to the file and defaults missing dependencies like OpticalChannel or Device. ```python def mock_ImagingPlane( name: Optional[str] = None, optical_channel: Optional[OpticalChannel] = None, description: str = "description", device: Optional[Device] = None, excitation_lambda: float = 500.0, indicator: str = "indicator", location: str = "unknown", imaging_rate: float = 30.0, manifold=None, conversion: float = 1.0, unit: str = "meters", reference_frame=None, origin_coords=None, origin_coords_unit: str = "meters", grid_spacing=None, grid_spacing_unit: str = "meters", nwbfile: Optional[NWBFile] = None, ) -> ImagingPlane: imaging_plane = ImagingPlane( name=name or name_generator("ImagingPlane"), optical_channel=optical_channel or mock_OpticalChannel(nwbfile=nwbfile), description=description, device=device or mock_Device(nwbfile=nwbfile), excitation_lambda=excitation_lambda, indicator=indicator, location=location, imaging_rate=imaging_rate, manifold=manifold, conversion=conversion, unit=unit, reference_frame=reference_frame, origin_coords=origin_coords, origin_coords_unit=origin_coords_unit, grid_spacing=grid_spacing, grid_spacing_unit=grid_spacing_unit, ) if nwbfile is not None: nwbfile.add_imaging_plane(imaging_plane) return imaging_plane ``` -------------------------------- ### GET /nwbfile/processing_module Source: https://pynwb.readthedocs.io/en/stable/pynwb.file.html Retrieves a specific ProcessingModule from the NWBFile by name. ```APIDOC ## GET /nwbfile/processing_module ### Description Get a ProcessingModule from this NWBFile. ### Method GET ### Parameters #### Query Parameters - **name** (str) - Optional - The name of the ProcessingModule ### Response #### Success Response (200) - **result** (ProcessingModule) - The ProcessingModule with the given name ``` -------------------------------- ### Initialize NWBFile Source: https://pynwb.readthedocs.io/en/stable/_downloads/8e42a92a26282b214be4121c3ce91670/ophys.ipynb Creates the base NWBFile object with required session metadata and experimenter information. ```python nwbfile = NWBFile( session_description="my first synthetic recording", identifier=str(uuid4()), session_start_time=datetime.now(tzlocal()), experimenter=[ "Baggins, Bilbo", ], lab="Bag End Laboratory", institution="University of Middle Earth at the Shire", experiment_description="I went on an adventure to reclaim vast treasures.", keywords=["ecephys", "exploration", "wanderlust"], related_publications="doi:10.1016/j.neuron.2016.12.011", ) ``` -------------------------------- ### Define Gallery Section Source: https://pynwb.readthedocs.io/en/stable/_sources/make_a_tutorial.rst.txt Format for the README.txt file required to initialize a new tutorial section in the gallery. ```rst .. _my-new-tutorials: My new tutorial section ----------------------- ``` -------------------------------- ### GET /nwbfile/device Source: https://pynwb.readthedocs.io/en/stable/pynwb.file.html Retrieves a specific Device from the NWBFile by name. ```APIDOC ## GET /nwbfile/device ### Description Get a Device from this NWBFile. ### Method GET ### Parameters #### Query Parameters - **name** (str) - Optional - The name of the Device ### Response #### Success Response (200) - **result** (Device) - The Device with the given name ``` -------------------------------- ### Import necessary libraries Source: https://pynwb.readthedocs.io/en/stable/tutorials/domain/plot_icephys_pandas.html Imports required for the tutorial, including os, dataframe_image, numpy, and pandas. Sets up paths and rendering options for tables. ```python import os ``` ```python import dataframe_image # Standard Python imports import numpy as np import pandas # Get the path to the this tutorial try: tutorial_path = os.path.abspath(__file__) # when running as a .py except NameError: tutorial_path = os.path.abspath("__file__") # when running as a script or notebook # directory to save rendered dataframe images for display df_basedir = os.path.abspath( os.path.join( os.path.dirname(tutorial_path), "../../source/tutorials/domain/images/" ) ) # Create the image directory. This is necessary only for gallery tests on GitHub # but not for normal doc builds the output path already exists os.makedirs(df_basedir, exist_ok=True) # Set rendering options for tables pandas.set_option("display.max_colwidth", 30) pandas.set_option("display.max_rows", 10) pandas.set_option("display.max_columns", 6) pandas.set_option("display.colheader_justify", "right") dfi_fontsize = 7 # Fontsize to use when rendering with dataframe_image ``` -------------------------------- ### GET /get_scratch Source: https://pynwb.readthedocs.io/en/stable/_modules/pynwb/file.html Retrieves data from the scratch space by name. ```APIDOC ## GET /get_scratch ### Description Retrieves data from the scratch space using the specified name. ### Parameters #### Query Parameters - **name** (str) - Required - The name of the object to get. - **convert** (bool) - Optional - Return the original data instead of the NWB object. Defaults to True. ``` -------------------------------- ### Parallel I/O with MPI in pynwb Source: https://pynwb.readthedocs.io/en/stable/tutorials/advanced_io/parallelio.html This code snippet demonstrates parallel read and write operations to an NWB file using MPI. It requires that HDF5 is installed with MPI support and that mpi4py is installed. Each MPI rank writes and reads a specific timestamp's data. ```python from mpi4py import MPI import numpy as np from dateutil import tz from pynwb import NWBHDF5IO, NWBFile, TimeSeries from datetime import datetime from hdmf.backends.hdf5.h5_utils import H5DataIO start_time = datetime(2018, 4, 25, 2, 30, 3, tzinfo=tz.gettz("US/Pacific")) fname = "test_parallel_pynwb.nwb" rank = MPI.COMM_WORLD.rank # The process ID (integer 0-3 for 4-process run) # Create file on one rank. Here we only instantiate the dataset we want to # write in parallel but we do not write any data if rank == 0: nwbfile = NWBFile("aa", "aa", start_time) data = H5DataIO(shape=(4,), maxshape=(4,), dtype=np.dtype("int")) nwbfile.add_acquisition( TimeSeries(name="ts_name", description="desc", data=data, rate=100.0, unit="m") ) with NWBHDF5IO(fname, "w") as io: io.write(nwbfile) # write to dataset in parallel with NWBHDF5IO(fname, "a", comm=MPI.COMM_WORLD) as io: nwbfile = io.read() print(rank) nwbfile.acquisition["ts_name"].data[rank] = rank # read from dataset in parallel with NWBHDF5IO(fname, "r", comm=MPI.COMM_WORLD) as io: print(io.read().acquisition["ts_name"].data[rank]) ``` -------------------------------- ### Run PyNWB Tests Source: https://pynwb.readthedocs.io/en/stable/_sources/install_developers.rst.txt Installs development requirements and PyNWB, then runs tests using tox. This requires the PyNWB repository to be cloned and the virtual environment activated. ```bash git clone --recurse-submodules https://github.com/NeurodataWithoutBorders/pynwb.git cd pynwb pip install -r requirements.txt -r requirements-dev.txt pip install -e . tox ``` -------------------------------- ### Get Plane Segmentation Source: https://pynwb.readthedocs.io/en/stable/pynwb.ophys.html Retrieves a PlaneSegmentation object from this ImageSegmentation by name. ```APIDOC ## GET /get_plane_segmentation ### Description Get a PlaneSegmentation from this ImageSegmentation. ### Method GET ### Endpoint /get_plane_segmentation ### Parameters #### Query Parameters - **name** (str) - the name of the PlaneSegmentation ### Returns - **PlaneSegmentation** - the PlaneSegmentation with the given name ``` -------------------------------- ### Create NWBFile Source: https://pynwb.readthedocs.io/en/stable/tutorials/domain/plot_icephys.html Initialize an NWBFile object with essential session metadata. This is the first step in organizing electrophysiology data. ```python nwbfile = NWBFile( session_description="my first synthetic recording", identifier=str(uuid4()), session_start_time=datetime.now(tzlocal()), experimenter=[ "Baggins, Bilbo", ], lab="Bag End Laboratory", institution="University of Middle Earth at the Shire", experiment_description="I went on an adventure to reclaim vast treasures.", session_id="LONELYMTN001", ) ```