Skip to content

Commit

Permalink
add read example to extensions tutorial (#587)
Browse files Browse the repository at this point in the history
add read example to extensions tutorial
  • Loading branch information
bendichter authored Aug 7, 2018
1 parent 98f6ed4 commit a4fc53d
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions docs/gallery/general/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ def __init__(self, **kwargs):
# For more information on writing NWB files, see :ref:`basic_writing`.

####################
# By default, PyNWB does not use the namespaces cached in a file--you must explicitly specify this.
# This behavior is enabled by the *load_namespaces* argument to the :py:class:`~pynwb.NWBHDF5IO`
# constructor.
# By default, PyNWB does not use the namespaces cached in a file--you must
# explicitly specify this. This behavior is enabled by the *load_namespaces*
# argument to the :py:class:`~pynwb.NWBHDF5IO` constructor.

io = NWBHDF5IO('cache_spec_example.nwb', mode='r', load_namespaces=True)
nwbfile = io.read()
Expand All @@ -238,9 +238,11 @@ def __init__(self, **kwargs):
# .. _MultiContainerInterface:
# Creating and using a custom MultiContainerInterface
# -----------------------------------------------------
# It is sometimes the case that we need a group to hold zero-or-more or one-or-more of the same object.
# Here we show how to create an extension that defines a group (`PotatoSack`) that holds multiple objects (`Pototo`es)
# and then how to use the new data types. First, we use `pynwb` to define the new data types.
# It is sometimes the case that we need a group to hold zero-or-more or
# one-or-more of the same object. Here we show how to create an extension that
# defines a group (`PotatoSack`) that holds multiple objects (`Pototo` es) and
# then how to use the new data types. First, we use `pynwb` to define the new
# data types.

from pynwb.spec import NWBNamespaceBuilder, NWBGroupSpec, NWBAttributeSpec

Expand Down Expand Up @@ -283,7 +285,8 @@ def __init__(self, **kwargs):
ns_builder.export(ns_path)

####################
# Then create Container classes registered to the new data types (this is generally done in a different file)
# Then create Container classes registered to the new data types (this is
# generally done in a different file)

from pynwb import register_class, load_namespaces
from pynwb.file import MultiContainerInterface, NWBContainer
Expand Down Expand Up @@ -337,3 +340,14 @@ class PotatoSack(MultiContainerInterface):

with NWBHDF5IO('test_multicontainerinterface.nwb', 'w') as io:
io.write(nwbfile)

####################
# This is how you read the NWB file (again, this would often be done in a
# different file).

load_namespaces(ns_path)
# from xxx import PotatoSack, Potato
io = NWBHDF5IO('test_multicontainerinterface.nwb', 'r')
nwb = io.read()
print(nwb.get_processing_module()['potato_sack'].get_potato().weight)
io.close()

0 comments on commit a4fc53d

Please sign in to comment.