NeurodataWithoutBorders / pynwb

A Python API for working with Neurodata stored in the NWB Format
https://pynwb.readthedocs.io
Other
176 stars 85 forks source link

demo code fails on h5 write #298

Closed bendichter closed 6 years ago

bendichter commented 6 years ago

This is a repeat of #285. The issue was closed but as far as I can tell the bug was not resolved.

I'm still seeing this error when I run the demo code on the current dev branch:

import numpy as np
from datetime import datetime
from pynwb import NWBFile
from pynwb.ecephys import ElectrodeTable, ElectrodeTableRegion
from pynwb.ecephys import ElectricalSeries
from pynwb import get_manager
from pynwb.form.backends.hdf5 import HDF5IO

rate = 10.0
np.random.seed(1234)
data_len = 1000
ephys_data = np.random.rand(data_len)
ephys_timestamps = np.arange(data_len) / rate
spatial_timestamps = ephys_timestamps[::10]
spatial_data = np.cumsum(np.random.normal(size=(2, len(spatial_timestamps))), axis=-1).T

f = NWBFile('the PyNWB tutorial', 'my first synthetic recording', 'EXAMPLE_ID', datetime.now(),
            experimenter='Dr. Bilbo Baggins',
            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')

epoch_tags = ('example_epoch',)

ep1 = f.create_epoch(source='an hypothetical source', name='epoch1', start=0.0, stop=1.0,
                     tags=epoch_tags,
                     description="the first test epoch")

device = f.create_device(name='trodes_rig123', source="a source")

electrode_name = 'tetrode1'
source = "an hypothetical source"
description = "an example tetrode"
location = "somewhere in the hippocampus"

electrode_group = f.create_electrode_group(electrode_name,
                                           source=source,
                                           description=description,
                                           location=location,
                                           device=device)

electrode_table = ElectrodeTable('electrodes')
for idx in [1, 2, 3, 4]:
    electrode_table.add_row(idx,
                            x=1.0, y=2.0, z=3.0,
                            imp=float(-idx),
                            location='CA1', filtering='none',
                            description='channel %s' % idx, group=electrode_group)

electrode_table_region = ElectrodeTableRegion(electrode_table, [0, 2], 'the first and third electrodes')

ephys_ts = ElectricalSeries('test_ephys_data',
                            'an hypothetical source',
                            ephys_data,
                            electrode_table_region,
                            timestamps=ephys_timestamps,
                            # 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")
f.add_acquisition(ephys_ts, [ep1])

filename = "example.h5"
io = HDF5IO(filename, manager=get_manager(), mode='w')
io.write(f)
io.close()
Traceback (most recent call last):
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/backends/hdf5/h5tools.py", line 284, in __add_refs
    call()
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/backends/hdf5/h5tools.py", line 494, in _filler
    ref = self.__get_ref(data, builder.region)
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/backends/hdf5/h5tools.py", line 618, in __get_ref
    dset = self.__file[path]
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "/anaconda/envs/preprocessing/lib/python3.6/site-packages/h5py/_hl/group.py", line 167, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'electrodes' doesn't exist)"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/bendichter/Development/ecog_preprocessing/nwb_demo.py", line 71, in <module>
    io.write(f)
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/utils.py", line 306, in func_call
    return func(self, **parsed['args'])
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/backends/hdf5/h5tools.py", line 121, in write
    call_docval_func(super(HDF5IO, self).write, kwargs)
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/utils.py", line 212, in call_docval_func
    return func(*fargs, **fkwargs)
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/utils.py", line 306, in func_call
    return func(self, **parsed['args'])
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/backends/io.py", line 39, in write
    self.write_builder(f_builder)
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/utils.py", line 306, in func_call
    return func(self, **parsed['args'])
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/backends/hdf5/h5tools.py", line 269, in write_builder
    self.__add_refs()
  File "/Users/bendichter/Development/pynwb/src/pynwb/form/backends/hdf5/h5tools.py", line 287, in __add_refs
    raise RuntimeError('Unable to resolve reference')
RuntimeError: Unable to resolve reference

Maybe I have something wrong in my installation. Can someone run this code and let me know if they are getting the same error?

neuromusic commented 6 years ago

closing to re-open #285