Skip to content

Commit

Permalink
ENH: autoimport allensdk.brain_observatory.ecephys.nwb when encounter…
Browse files Browse the repository at this point in the history
…ing exception with AIBS_ecephys listed missing

Ultimate solution should IMHO be implemented within PyNWB itself.
See NeurodataWithoutBorders/pynwb#1143 feature request.
Also related - dedicated exception (NeurodataWithoutBorders/pynwb#1144)

With this change we should be able to deal with neuropixels files from Allen
  • Loading branch information
yarikoptic committed Jan 17, 2020
1 parent 40e7524 commit 5a42539
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions dandi/pynwb_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ def get_metadata(path):
# First read out possibly available versions of specifications for NWB(:N)
out["nwb_version"] = get_nwb_version(path)

# PyNWB might fail to load because of missing extensions.
# There is a new initiative of establishing registry of such extensions.
# Not yet sure if PyNWB is going to provide "native" support for needed
# functionality: https://github.com/NeurodataWithoutBorders/pynwb/issues/1143
# So meanwhile, hard-coded workaround for data types we care about
ndtypes_registry = {"AIBS_ecephys": "allensdk.brain_observatory.ecephys.nwb"}
while True:
try:
out.update(_get_pynwb_metadata(path))
break
except KeyError as exc: # ATM there is
lgr.debug("Failed to read %s: %s", path, exc)
import re

res = re.match(r"^['\"\\]+(\S+). not a namespace", str(exc))
if not res:
raise
ndtype = res.groups()[0]
if ndtype not in ndtypes_registry:
raise ValueError(
"We do not know which extension provides %s. "
"Original exception was: %s. " % (ndtype, exc)
)
lgr.debug(
"Importing %r which should provide %r", ndtypes_registry[ndtype], ndtype
)
__import__(ndtypes_registry[ndtype])

return out


def _get_pynwb_metadata(path):
out = {}
with NWBHDF5IO(path, "r") as io:
nwb = io.read()
for key in metadata_fields:
Expand Down

0 comments on commit 5a42539

Please sign in to comment.