Hi,
I am trying to read NWB files I have downloaded but am getting an error message I do not understand. Here is my code to read the datafile:
import pandas as pd
import numpy as np
import pdb
from pynwb import NWBFile, TimeSeries, NWBHDF5IO
from pynwb.epoch import TimeIntervals
from pynwb.file import Subject
from pynwb.behavior import SpatialSeries, Position
from datetime import datetime
from dateutil import tz
with NWBHDF5IO('session_719161530.nwb', 'r') as io:
read_nwbfile = io.read()
print(read_nwbfile.acquisition["test_timeseries"])
print(read_nwbfile.acquisition["test_timeseries"].data[:])
But I get the following error:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-4-53960b1b40f9> in <module>
1 #with NWBHDF5IO('C:\Users\Pantea\Desktop\AllenDatap\session_715093703\session_715093703.nwb', "r") as io:
2 with NWBHDF5IO('session_719161530.nwb', 'r') as io:
----> 3 read_nwbfile = io.read()
4 print(read_nwbfile.acquisition["test_timeseries"])
5 print(read_nwbfile.acquisition["test_timeseries"].data[:])
~\anaconda3\lib\site-packages\hdmf\backends\hdf5\h5tools.py in read(self, **kwargs)
449 % (self.source, self.__mode))
450 try:
--> 451 return super().read(**kwargs)
452 except UnsupportedOperation as e:
453 if str(e) == 'Cannot build data. There are no values.': # pragma: no cover
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\backends\io.py in read(self, **kwargs)
40 # TODO also check that the keys are appropriate. print a better error message
41 raise UnsupportedOperation('Cannot build data. There are no values.')
---> 42 container = self.__manager.construct(f_builder)
43 return container
44
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\build\manager.py in construct(self, **kwargs)
278 # we are at the top of the hierarchy,
279 # so it must be time to resolve parents
--> 280 result = self.__type_map.construct(builder, self, None)
281 self.__resolve_parents(result)
282 self.prebuilt(result, builder)
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\build\manager.py in construct(self, **kwargs)
787 raise ValueError('No ObjectMapper found for builder of type %s' % dt)
788 else:
--> 789 return obj_mapper.construct(builder, build_manager, parent)
790
791 @docval({"name": "container", "type": AbstractContainer, "doc": "the container to convert to a Builder"},
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in construct(self, **kwargs)
1216 cls = manager.get_cls(builder)
1217 # gather all subspecs
-> 1218 subspecs = self.__get_subspec_values(builder, self.spec, manager)
1219 # get the constructor argument that each specification corresponds to
1220 const_args = dict()
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in __get_subspec_values(self, builder, spec, manager)
1145 ret[subspec] = self.__flatten(sub_builder, subspec, manager)
1146 # now process groups and datasets
-> 1147 self.__get_sub_builders(groups, spec.groups, manager, ret)
1148 self.__get_sub_builders(datasets, spec.datasets, manager, ret)
1149 elif isinstance(spec, DatasetSpec):
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in __get_sub_builders(self, sub_builders, subspecs, manager, ret)
1196 if dt is None:
1197 # recurse
-> 1198 ret.update(self.__get_subspec_values(sub_builder, subspec, manager))
1199 else:
1200 ret[subspec] = manager.construct(sub_builder)
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in __get_subspec_values(self, builder, spec, manager)
1145 ret[subspec] = self.__flatten(sub_builder, subspec, manager)
1146 # now process groups and datasets
-> 1147 self.__get_sub_builders(groups, spec.groups, manager, ret)
1148 self.__get_sub_builders(datasets, spec.datasets, manager, ret)
1149 elif isinstance(spec, DatasetSpec):
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in __get_sub_builders(self, sub_builders, subspecs, manager, ret)
1188 sub_builder = builder_dt.get(dt)
1189 if sub_builder is not None:
-> 1190 sub_builder = self.__flatten(sub_builder, subspec, manager)
1191 ret[subspec] = sub_builder
1192 else:
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in __flatten(self, sub_builder, subspec, manager)
1201
1202 def __flatten(self, sub_builder, subspec, manager):
-> 1203 tmp = [manager.construct(b) for b in sub_builder]
1204 if len(tmp) == 1 and not subspec.is_many():
1205 tmp = tmp[0]
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in <listcomp>(.0)
1201
1202 def __flatten(self, sub_builder, subspec, manager):
-> 1203 tmp = [manager.construct(b) for b in sub_builder]
1204 if len(tmp) == 1 and not subspec.is_many():
1205 tmp = tmp[0]
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\build\manager.py in construct(self, **kwargs)
274 if parent_builder is not None:
275 parent = self._get_proxy_builder(parent_builder)
--> 276 result = self.__type_map.construct(builder, self, parent)
277 else:
278 # we are at the top of the hierarchy,
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\build\manager.py in construct(self, **kwargs)
787 raise ValueError('No ObjectMapper found for builder of type %s' % dt)
788 else:
--> 789 return obj_mapper.construct(builder, build_manager, parent)
790
791 @docval({"name": "container", "type": AbstractContainer, "doc": "the container to convert to a Builder"},
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in construct(self, **kwargs)
1216 cls = manager.get_cls(builder)
1217 # gather all subspecs
-> 1218 subspecs = self.__get_subspec_values(builder, self.spec, manager)
1219 # get the constructor argument that each specification corresponds to
1220 const_args = dict()
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in __get_subspec_values(self, builder, spec, manager)
1145 ret[subspec] = self.__flatten(sub_builder, subspec, manager)
1146 # now process groups and datasets
-> 1147 self.__get_sub_builders(groups, spec.groups, manager, ret)
1148 self.__get_sub_builders(datasets, spec.datasets, manager, ret)
1149 elif isinstance(spec, DatasetSpec):
~\anaconda3\lib\site-packages\hdmf\build\objectmapper.py in __get_sub_builders(self, sub_builders, subspecs, manager, ret)
1174 if dt is None or ns is None:
1175 continue
-> 1176 for parent_dt in manager.namespace_catalog.get_hierarchy(ns, dt):
1177 builder_dt.setdefault(parent_dt, list()).append(g)
1178 for subspec in subspecs:
~\anaconda3\lib\site-packages\hdmf\utils.py in func_call(*args, **kwargs)
643 def func_call(*args, **kwargs):
644 pargs = _check_args(args, kwargs)
--> 645 return func(args[0], **pargs)
646 else:
647 def func_call(*args, **kwargs):
~\anaconda3\lib\site-packages\hdmf\spec\namespace.py in get_hierarchy(self, **kwargs)
326 spec_ns = self.__namespaces.get(namespace)
327 if spec_ns is None:
--> 328 raise KeyError("'%s' not a namespace" % namespace)
329 return spec_ns.get_hierarchy(data_type)
330
KeyError: "'ndx-aibs-ecephys' not a namespace"
I am not sure how to debug this error.
For the sake if completeness, this is the code I used to download the data files:
import os
import shutil
import numpy as np
import pandas as pd
from allensdk.brain_observatory.ecephys.ecephys_project_cache import EcephysProjectCache
data_directory = 'C:/Users/Pantea/Desktop/AllenDatap/' # must be updated to a valid directory in your filesystem
manifest_path = os.path.join(data_directory, "manifest.json")
cache = EcephysProjectCache.from_warehouse(manifest=manifest_path)
sessions = cache.get_session_table()
for session_id, row in sessions.iterrows():
truncated_file = True
directory = os.path.join(data_directory + '/session_' + str(session_id))
while truncated_file:
session = cache.get_session_data(session_id)
try:
print(session.specimen_name)
truncated_file = False
except OSError:
shutil.rmtree(directory)
print(" Truncated spikes file, re-downloading")
for probe_id, probe in session.probes.iterrows():
print(' ' + probe.description)
truncated_lfp = False
while truncated_lfp:
try:
lfp = session.get_lfp(probe_id)
truncated_lfp = False
except OSError:
fname = directory + '/probe_' + str(probe_id) + '_lfp.nwb'
os.remove(fname)
print(" Truncated LFP file, re-downloading")
except ValueError:
print(" LFP file not found.")
truncated_lfp = False