NWB Helpers#

Collection of Pydantic models and helper functions for configuring dataset IO parameters for different backends.

get_default_backend_configuration(nwbfile: NWBFile, backend: Literal['hdf5', 'zarr']) Union[HDF5BackendConfiguration, ZarrBackendConfiguration][source]#

Fill a default backend configuration to serve as a starting point for further customization.

get_default_dataset_io_configurations(nwbfile: NWBFile, backend: Union[None, Literal['hdf5', 'zarr']] = None) Generator[DatasetIOConfiguration, None, None][source]#

Generate DatasetIOConfiguration objects for wrapping NWB file objects with a specific backend.

This method automatically detects all objects in an NWB file that can be wrapped in a DataIO. If the NWB file is in append mode, it supports auto-detection of the backend. Otherwise, it requires a backend specification.

Parameters
  • nwbfile (pynwb.NWBFile) – An in-memory NWBFile object, either generated from the base class or read from an existing file of any backend.

  • backend (“hdf5” or “zarr”) – Which backend format type you would like to use in configuring each dataset’s compression methods and options.

Yields

DatasetIOConfiguration – A summary of each detected object that can be wrapped in a DataIO.

configure_backend(nwbfile: NWBFile, backend_configuration: Union[HDF5BackendConfiguration, ZarrBackendConfiguration]) None[source]#

Configure all datasets specified in the backend_configuration with their appropriate DataIO and options.

class BackendConfiguration(__pydantic_self__, **data: Any) None[source]#

Bases: BaseModel

A model for matching collections of DatasetConfigurations to a specific backend.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

__str__() str[source]#

Not overriding __repr__ as this is intended to render only when wrapped in print().

class DatasetIOConfiguration(__pydantic_self__, **data: Any) None[source]#

Bases: BaseModel, ABC

A data model for configuring options about an object that will become a HDF5 or Zarr Dataset in the file.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

__str__() str[source]#

Not overriding __repr__ as this is intended to render only when wrapped in print().

Reason being two-fold; a standard repr is intended to be slightly more machine-readable / a more basic representation of the true object state. But then also because an iterable of these objects, such as a List[DatasetConfiguration], would print out the nested representations, which only look good when using the basic repr (that is, this fancy string print-out does not look good when nested in another container).

abstract get_data_io_kwargs() Dict[str, Any][source]#

Fetch the properly structured dictionary of input arguments.

Should be passed directly as dynamic keyword arguments (**kwargs) into a H5DataIO or ZarrDataIO.

add_device_from_metadata(nwbfile: NWBFile, modality: str = 'Ecephys', metadata: Optional[dict] = None)[source]#

Add device information from metadata to NWBFile object.

Will always ensure nwbfile has at least one device, but multiple devices within the metadata list will also be created.

Parameters
  • nwbfile (NWBFile) – nwb file to which the new device information is to be added

  • modality (str) – Type of data recorded by device. Options: - Ecephys (default) - Icephys - Ophys - Behavior

  • metadata (dict) – Metadata info for constructing the NWBFile (optional). Should be of the format

    metadata[modality][‘Device’] = [
    {

    ‘name’: my_name, ‘description’: my_description

    ]

    Missing keys in an element of metadata[‘Ecephys’][‘Device’] will be auto-populated with defaults.

get_default_nwbfile_metadata() DeepDict[source]#

Return structure with defaulted metadata values required for a NWBFile.

These standard defaults are

metadata[“NWBFile”][“session_description”] = “no description” metadata[“NWBFile”][“identifier”] = str(uuid.uuid4())

Proper conversions should override these fields prior to calling NWBConverter.run_conversion()

get_module(nwbfile: NWBFile, name: str, description: Optional[str] = None)[source]#

Check if processing module exists. If not, create it. Then return module.

make_nwbfile_from_metadata(metadata: dict) NWBFile[source]#

Make NWBFile from available metadata.

make_or_load_nwbfile(nwbfile_path: Optional[FilePath] = None, nwbfile: Optional[NWBFile] = None, metadata: Optional[dict] = None, overwrite: bool = False, verbose: bool = True)[source]#

Context for automatically handling decision of write vs. append for writing an NWBFile.

Parameters
  • nwbfile_path (FilePathType) – Path for where to write or load (if overwrite=False) the NWBFile. If specified, the context will always write to this location.

  • nwbfile (NWBFile, optional) – An in-memory NWBFile object to write to the location.

  • metadata (dict, optional) – Metadata dictionary with information used to create the NWBFile when one does not exist or overwrite=True.

  • overwrite (bool, optional) – Whether to overwrite the NWBFile if one exists at the nwbfile_path. The default is False (append mode).

  • verbose (bool, optional) – If ‘nwbfile_path’ is specified, informs user after a successful write operation. The default is True.

class DatasetInfo(**values)[source]#

Bases: BaseModel

A data model to represent immutable aspects of an object that will become a HDF5 or Zarr dataset on write.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

__hash__()[source]#

To allow instances of this class to be used as keys in dictionaries.

__str__() str[source]#

Not overriding __repr__ as this is intended to render only when wrapped in print().

Reason being two-fold; a standard repr is intended to be slightly more machine-readable / a more basic representation of the true object state. But then also because an iterable of these objects, such as a List[DataSetInfo], would print out the nested representations, which only look good when using the basic repr (that is, this fancy string print-out does not look good when nested in another container).

class HDF5BackendConfiguration(__pydantic_self__, **data: Any) None[source]#

Bases: BackendConfiguration

A model for matching collections of DatasetConfigurations specific to the HDF5 backend.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

class HDF5DatasetIOConfiguration(__pydantic_self__, **data: Any) None[source]#

Bases: DatasetIOConfiguration

A data model for configuring options about an object that will become a HDF5 Dataset in the file.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

get_data_io_kwargs() Dict[str, Any][source]#

Fetch the properly structured dictionary of input arguments.

Should be passed directly as dynamic keyword arguments (**kwargs) into a H5DataIO or ZarrDataIO.

class ZarrBackendConfiguration(__pydantic_self__, **data: Any) None[source]#

Bases: BackendConfiguration

A model for matching collections of DatasetConfigurations specific to the Zarr backend.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

class ZarrDatasetIOConfiguration(__pydantic_self__, **data: Any) None[source]#

Bases: DatasetIOConfiguration

A data model for configuring options about an object that will become a Zarr Dataset in the file.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

get_data_io_kwargs() Dict[str, Any][source]#

Fetch the properly structured dictionary of input arguments.

Should be passed directly as dynamic keyword arguments (**kwargs) into a H5DataIO or ZarrDataIO.

_NWBZarrIO#

alias of NWBZarrIO