-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnwbv2-read-test.py
executable file
·103 lines (75 loc) · 2.97 KB
/
nwbv2-read-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
from pynwb import NWBHDF5IO
import h5py
import sys
import os
from subprocess import run, PIPE, STDOUT
from argparse import ArgumentParser
vers = sys.version_info
if vers < (3, 7):
print("Unsupported python version: {}".format(vers), file=sys.stderr)
sys.exit(1)
def to_str(s):
if isinstance(s, bytes):
return s.decode('utf-8')
else:
return s
def checkFile(path):
if not os.path.isfile(path):
print(f"The file {path} does not exist.", file=sys.stderr)
return 1
# 1.) pynwb Validation
comp = run(["pynwb-validate", path],
stdout=PIPE, stderr=STDOUT, universal_newlines=True, timeout=120)
if comp.returncode != 0:
print(f"pynwb validation output: {comp.stdout}", file=sys.stderr)
return 1
print(f"pynwb validation output: {comp.stdout}", file=sys.stdout)
# 2.) dandi Validation
comp = run(["dandi", "validate", "--ignore", "(NWBI|DANDI)", path],
stdout=PIPE, stderr=STDOUT, universal_newlines=True, timeout=120)
if comp.returncode != 0:
print(f"dandi validation output: {comp.stdout}", file=sys.stderr)
return 1
print(f"dandi validation output: {comp.stdout}", file=sys.stdout)
# 3.) Read test
with NWBHDF5IO(path, mode='r', load_namespaces=True) as io:
nwbfile = io.read()
print(f"nwbfile: {nwbfile}")
print(f"icephys_electrodes: {nwbfile.icephys_electrodes}")
print(f"sweep_table: {nwbfile.sweep_table}")
print(f"lab_meta_data: {nwbfile.lab_meta_data}")
print(f"acquisition: {nwbfile.acquisition}")
print(f"stimulus: {nwbfile.stimulus}")
print(f"epochs: {nwbfile.epochs}")
object_ids = nwbfile.objects.keys()
print(f"object_ids: {object_ids}")
if nwbfile.epochs and len(nwbfile.epochs) > 0:
print(f"epochs.start_time: {nwbfile.epochs[:, 'start_time']}")
print(f"epochs.stop_time: {nwbfile.epochs[:, 'stop_time']}")
print(f"epochs.tags: {nwbfile.epochs[:, 'tags']}")
print(f"epochs.treelevel: {nwbfile.epochs[:, 'treelevel']}")
print(f"epochs.timeseries: {nwbfile.epochs[:, 'timeseries']}")
# 4. Check that pynwb/hdmf can read our object IDs
with h5py.File(path, 'r') as f:
root_object_id_hdf5 = to_str(f["/"].attrs["object_id"])
if root_object_id_hdf5 not in object_ids:
print(f"object IDs don't match as {root_object_id_hdf5} could not be found.", file=sys.stderr)
return 1
return 0
def main():
parser = ArgumentParser(description="Validate and read an NWB file")
parser.add_argument("paths", type=str, nargs='+', help="NWB file paths")
args = parser.parse_args()
ret = 0
for path in args.paths:
ret = ret or checkFile(path)
if ret == 0:
print("Success!")
return ret
if __name__ == '__main__':
try:
sys.exit(main())
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)