Skip to content

Instantly share code, notes, and snippets.

@rly
Created July 1, 2022 16:11
Show Gist options
  • Save rly/21593370c51fd7891fd87dcc6e02c26f to your computer and use it in GitHub Desktop.
Save rly/21593370c51fd7891fd87dcc6e02c26f to your computer and use it in GitHub Desktop.
Demonstration of the loading of NWB namespaces in different contexts
import pynwb
from pynwb.spec import NWBDatasetSpec, NWBGroupSpec, NWBNamespace
from hdmf.spec import NamespaceCatalog
from hdmf.build import TypeMap
def print_namespace_versions(type_map):
"""Print the namespace name and version of all namespaces in the given type map."""
for ns_name in type_map.namespace_catalog.namespaces:
print("%s: %s" % (ns_name, type_map.namespace_catalog.get_namespace(ns_name)["version"]))
path = r"C:\Users\Ryan\Documents\NWB_Data\AIBS_ecephys_dandiset21\sub-699733573_ses-715093703_probe-810755797_ecephys.nwb"
# pynwb contains the latest versions of certain namespaces (pynwb, hdmf-common, and hdmf-experimental)
# that are available at the time of release of this version of pynwb.
# these namespaces are automatically loaded into all NWBHDF5IO objects subsequently created
print("Namespaces loaded in global scope:")
print_namespace_versions(pynwb.get_manager().type_map)
print()
# NWB files contain cached namespaces which may have different versions that the
# namespaces with the same name loaded in the global scope
catalog = NamespaceCatalog(NWBGroupSpec, NWBDatasetSpec, NWBNamespace)
pynwb.NWBHDF5IO.load_namespaces(catalog, path)
tm = TypeMap(catalog)
print("Namespaces cached in the NWB file '%s':" % path)
print_namespace_versions(tm)
print()
# the global scope namespaces are loaded into the NWBHDF5IO object when it is created
# when load_namespaces=True, all namespaces cached within the NWB file that are not
# already in the namespaces within the NWBHDF5IO object (ignoring the version of the namespace)
# are then loaded into the NWBHDF5IO object
io = pynwb.NWBHDF5IO(path, "r", load_namespaces=True)
print("Namespaces loaded in the build manager for the NWB file '%s':" % path)
print_namespace_versions(io.manager.type_map)
print()
# loading cached namespaces into an NWBHDF5IO object does not affect the namespaces
# loaded in the global scope
print("Namespaces loaded in global scope:")
print_namespace_versions(pynwb.get_manager().type_map)
print()
# load a namespace into the global scope, which will be loaded into all NWBHDF5IO objects
# subsequently created
namespace_filepath = "ndx-ecog.namespace.yaml"
pynwb.load_namespaces(namespace_filepath)
# note that the new namespace is present in the global scope
print("Namespaces loaded in global scope:")
print_namespace_versions(pynwb.get_manager().type_map)
print()
# note that the new namespace is present in the namespaces for this NWBHDF5IO object
io = pynwb.NWBHDF5IO(path, "r", load_namespaces=True)
print("Namespaces loaded in the build manager for the NWB file '%s':" % path)
print_namespace_versions(io.manager.type_map)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment