Skip to content

Commit

Permalink
ENH: obtain also "nwb_version", ls it as NWB field in the table
Browse files Browse the repository at this point in the history
I went for a shorter name in  ls  command since space is scarse.

Unfortunately I could not immediately figure out how to even populate
that field e.g. using NWBFile.  And PyNWB does not populate it
either ATM: NeurodataWithoutBorders/pynwb#1085

The only example I found quickly with nwb_version field populated was from
///crcns/ssc-7 dataset -- and in particular
data/L4E_whole_cell/Exp_2015-09-05_001_0001-0162.nwb
file there.

I really think we should come up with some good, and not that large collection
of sample .nwb files to test on.  Unfortunately any "real" .nwb file seems to be
quite large
  • Loading branch information
yarikoptic committed Oct 8, 2019
1 parent 87cd5ac commit 1fd3023
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
16 changes: 13 additions & 3 deletions dandi/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

from collections import OrderedDict

from ..pynwb_utils import get_metadata
from ..pynwb_utils import (
get_metadata,
get_nwb_version,
)
from ..support import pyout as pyouts

lgr = get_logger()
Expand Down Expand Up @@ -66,8 +69,14 @@ def get_metadata_pyout(path):
if v:
rec[f] = v
except Exception as exc:
# lgr.error('Failed to get metadata from %s: %s', f, exc)
pass
lgr.debug('Failed to get metadata from %s: %s', path, exc)

if 'nwb_version' not in rec:
# Let's at least get that one
rec['NWB'] = get_nwb_version(path)
else:
# renames for more concise ls
rec['NWB'] = rec.pop('nwb_version', None)
return rec


Expand Down Expand Up @@ -145,6 +154,7 @@ def ls(paths):
out = pyout.Tabular(
columns=[
'path',
'NWB',
'size',
#'experiment_description',
'lab',
Expand Down
29 changes: 29 additions & 0 deletions dandi/pynwb_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import h5py
from pynwb import NWBHDF5IO

from . import get_logger

lgr = get_logger()

# A list of metadata fields which dandi extracts from .nwb files.
# Additional fields (such as `number_of_*`) might be added by the
# get_metadata`
Expand All @@ -18,14 +22,39 @@
)


def get_nwb_version(filepath):
"""Return a version of the NWB standard used by a file
Returns
-------
str or None
None if there is no version detected
"""
with h5py.File(filepath, 'r') as h5file:
try:
return h5file['nwb_version'].value.decode
except KeyError:
lgr.debug('%s has no nwb_version' % filepath)


def get_metadata(filepath):
"""Get selected metadata from a .nwb file
Parameters
----------
filepath: str
query_nwb_version: bool, optional
Either to query/include nwb_version field
Returns
-------
dict
"""
out = dict()

# First read out possibly available versions of specifications for NWB(:N)
out['nwb_version'] = get_nwb_version(filepath)

with NWBHDF5IO(filepath, 'r') as io:
nwb = io.read()
for key in metadata_fields:
Expand Down

0 comments on commit 1fd3023

Please sign in to comment.