Skip to content

Commit

Permalink
Add extensions.py to showcase issue hdmf-dev/hdmf#354
Browse files Browse the repository at this point in the history
  • Loading branch information
t-b committed May 20, 2020
1 parent 2f4bbbd commit 9df5848
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pynwb
from pynwb import NWBHDF5IO, NWBFile, ProcessingModule
from pynwb import register_class, load_namespaces
from pynwb.ecephys import ElectricalSeries
from pynwb import NWBData
from hdmf.utils import docval, call_docval_func, getargs, get_docval

ns_path = "spec/ndx-mies.namespace.yaml"
ext_source = "spec/ndx-mies.extensions.yaml"

from pynwb import get_class, load_namespaces

load_namespaces(ns_path)

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())

@register_class('GeneratedBy', 'ndx-mies')
class GeneratedBy(NWBData):

__nwbfields__ = ('data',)

@docval(*get_docval(NWBData.__init__))
def __init__(self, **kwargs):
call_docval_func(super(NWBData, self).__init__, kwargs)
self.data = getargs('data', kwargs)


nwbfile = NWBFile('demonstrate caching', 'NWB456', start_time,
file_create_date=create_date, lab_meta_data=[GeneratedBy(name="GeneratedBy", data=[1, 2, [1, 2]])])

device = nwbfile.create_device(name='device_ITC18USB_Dev_0')

io = NWBHDF5IO('cache_spec_example.nwb', mode='w')
io.write(nwbfile)
io.close()

####################
# 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.

with NWBHDF5IO('cache_spec_example.nwb', mode='r+', load_namespaces=True) as nwb_io:
nwb = nwb_io.read()

spike_module = ProcessingModule(name='spikes', description='detected spikes')
nwb.add_processing_module(spike_module)

# This is the container which is causing the problem
dev = nwb.devices["device_ITC18USB_Dev_0"]
print(type(dev))
print(dev)
print(type(dev.parent))
print(dev.parent)

nwb_io.write(nwb)

0 comments on commit 9df5848

Please sign in to comment.