Skip to content

Instantly share code, notes, and snippets.

@bendichter
Last active March 23, 2022 00:54
Show Gist options
  • Save bendichter/8f605d6d3e5a7b11a00255278aa2d5f9 to your computer and use it in GitHub Desktop.
Save bendichter/8f605d6d3e5a7b11a00255278aa2d5f9 to your computer and use it in GitHub Desktop.
draw tree structure of h5py File
import h5py
def print_group(group: h5py.Group):
name = group.name.split('/')[-1]
return f"{name} ({len(group.keys())} objects)/"
def print_dataset(dataset: h5py.Dataset):
name = dataset.name.split('/')[-1]
return f"{name} {dataset.shape}, {dataset.dtype}"
def print_line(elem, last, op):
if op:
op[0] = False
line = ""
for o in op:
if o:
line += "| "
else:
line += " "
if last:
line += "└── "
else:
line += "├── "
if isinstance(elem, h5py.Group):
line += print_group(elem)
elif isinstance(elem, h5py.Dataset):
line += print_dataset(elem)
return line
def _show_group(group, last=True, op = []):
out = []
out.append(print_line(group, last, op))
for i, key in enumerate(group.keys()):
elem = group[key]
this_last = i == (len(group.keys()) - 1)
if isinstance(elem, h5py.Group):
out.extend(_show_group(elem, this_last, op + [True]))
if isinstance(elem, h5py.Dataset):
out.append(print_line(elem, this_last, op + [not last]))
return out
def file_tree(groups):
return '\n'.join(_show_group(file))
@bendichter
Copy link
Author

bendichter commented Mar 23, 2022

example output:

└──  (13 objects)/
    ├── acquisition (0 objects)/
    ├── analysis (0 objects)/
    ├── file_create_date (1,), object
    ├── general (8 objects)/
    |   ├── devices (3 objects)/
    |   |   ├── Cambridge prob (1 x 64) (0 objects)/
    |   |   ├── Neuronexus probe (4 x 1) (0 objects)/
    |   |   └── Neuronexus probe (5 x 12) (0 objects)/
    |   ├── experimenter (1,), object
    |   ├── extracellular_ephys (8 objects)/
    |   |   ├── Group1 (1 objects)/
    |   |   |   └── device (0 objects)/
    |   |   ├── Group2 (1 objects)/
    |   |   |   └── device (0 objects)/
    |   |   ├── Group3 (1 objects)/
    |   |   |   └── device (0 objects)/
    |   |   ├── Group4 (1 objects)/
    |   |   |   └── device (0 objects)/
    |   |   ├── Group5 (1 objects)/
    |   |   |   └── device (0 objects)/
    |   |   ├── Group6 (1 objects)/
    |   |   |   └── device (0 objects)/
    |   |   ├── Group7 (1 objects)/
    |   |   |   └── device (0 objects)/
    |   |   └── electrodes (10 objects)/
    |   |       ├── filtering (128,), object
    |   |       ├── group (128,), object
    |   |       ├── group_name (128,), object
    |   |       ├── id (128,), int64
    |   |       ├── imp (128,), float64
    |   |       ├── location (128,), object
    |   |       ├── shank_electrode_number (128,), float64
    |   |       ├── x (128,), float64
    |   |       ├── y (128,), float64
    |   |       └── z (128,), float64
    |   ├── institution (), object
    |   ├── lab (), object
    |   ├── related_publications (1,), object
    |   ├── session_id (), object
    |   └── subject (2 objects)/
    |       ├── species (), object
    |       └── subject_id (), object
    ├── identifier (), object
    ├── intervals (1 objects)/
    |   └── trials (5 objects)/
    |       ├── direction (239,), object
    |       ├── id (239,), int64
    |       ├── start_time (239,), float64
    |       ├── stop_time (239,), float64
    |       └── trial_type (239,), object
    ├── processing (2 objects)/
    |   ├── behavior (2 objects)/
    |   |   ├── allocentric_frame_tracking (1 objects)/
    |   |   |   └── orientation (3 objects)/
    |   |   |       ├── data (335022, 4), float64
    |   |   |       ├── reference_frame (), object
    |   |   |       └── timestamps (335022,), float64
    |   |   └── wheel_alternation_task (2 objects)/
    |   |   |   ├── error_per_marker (3 objects)/
    |   |   |   |   ├── data (335022,), float64
    |   |   |   |   ├── reference_frame (), object
    |   |   |   |   └── timestamps (335022,), float64
    |   |   |   └── position (3 objects)/
    |   |   |       ├── data (335022, 3), float64
    |   |   |       ├── reference_frame (), object
    |   |   |       └── timestamps (335022,), float64
    |   └── ecephys (2 objects)/
    |   |   ├── LFP (1 objects)/
    |   |   |   └── ElectricalSeries_lfp (3 objects)/
    |   |   |       ├── data (100, 128), float32
    |   |   |       ├── electrodes (128,), int64
    |   |   |       └── starting_time (), float64
    |   |   └── sleep_states (4 objects)/
    |   |       ├── id (142,), int64
    |   |       ├── label (142,), object
    |   |       ├── start_time (142,), float64
    |   |       └── stop_time (142,), float64
    ├── session_description (), object
    ├── session_start_time (), object
    ├── specifications (3 objects)/
    |   ├── core (1 objects)/
    |   |   └── 2.4.0 (13 objects)/
    |   |       ├── namespace (), object
    |   |       ├── nwb.base (), object
    |   |       ├── nwb.behavior (), object
    |   |       ├── nwb.device (), object
    |   |       ├── nwb.ecephys (), object
    |   |       ├── nwb.epoch (), object
    |   |       ├── nwb.file (), object
    |   |       ├── nwb.icephys (), object
    |   |       ├── nwb.image (), object
    |   |       ├── nwb.misc (), object
    |   |       ├── nwb.ogen (), object
    |   |       ├── nwb.ophys (), object
    |   |       └── nwb.retinotopy (), object
    |   ├── hdmf-common (1 objects)/
    |   |   └── 1.5.0 (4 objects)/
    |   |       ├── base (), object
    |   |       ├── namespace (), object
    |   |       ├── sparse (), object
    |   |       └── table (), object
    |   └── hdmf-experimental (1 objects)/
    |   |   └── 0.1.0 (3 objects)/
    |   |       ├── experimental (), object
    |   |       ├── namespace (), object
    |   |       └── resources (), object
    ├── stimulus (2 objects)/
    |   ├── presentation (0 objects)/
    |   └── templates (0 objects)/
    ├── timestamps_reference_time (), object
    └── units (6 objects)/
        ├── clu_id (81,), int64
        ├── group_id (81,), object
        ├── id (81,), int64
        ├── location (81,), object
        ├── spike_times (1197241,), float64
        └── spike_times_index (81,), uint32

@bendichter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment