Skip to content

Commit

Permalink
add family driver file validation, ignore dandi file validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Sep 7, 2024
1 parent b851b6e commit e0389e5
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import argparse
import glob
import h5py
import inspect
import logging
import os.path
Expand Down Expand Up @@ -152,6 +153,9 @@ def validate_nwbs():
logging.info('running validation tests on NWB files')
examples_nwbs = glob.glob('*.nwb')

# exclude files downloaded from dandi, validation of those files is handled by dandisets-health-status checks
examples_nwbs = [x for x in examples_nwbs if not x.startswith('sub-')]

import pynwb

for nwb in examples_nwbs:
Expand All @@ -161,15 +165,34 @@ def validate_nwbs():
ws = list()
with warnings.catch_warnings(record=True) as tmp:
logging.info("Validating with pynwb.validate method.")
with pynwb.NWBHDF5IO(nwb, mode='r') as io:
errors = pynwb.validate(io)
TOTAL += 1
is_family_nwb_file = False
try:
with pynwb.NWBHDF5IO(nwb, mode='r') as io:
errors = pynwb.validate(io)
except OSError as e:
# if the file was created with the family driver, need to use the family driver to open it
if 'family driver should be used' in str(e):
is_family_nwb_file = True
match = re.search(r'(\d+)', nwb)
filename_pattern = nwb[:match.start()] + '%d' + nwb[match.end():] # infer the filename pattern
memb_size = 1024**2 # note: the memb_size must be the same as the one used to create the file
with h5py.File(filename_pattern, mode='r', driver='family', memb_size=memb_size) as f:
with pynwb.NWBHDF5IO(file=f, manager=None, mode='r') as io:
errors = pynwb.validate(io)
else:
raise e

TOTAL += 1

if errors:
FAILURES += 1
ERRORS += 1
for err in errors:
print("Error: %s" % err)

if errors:
FAILURES += 1
ERRORS += 1
for err in errors:
print("Error: %s" % err)
# if file was created with family driver, skip pynwb.validate CLI because not yet supported
if is_family_nwb_file:
continue

def get_namespaces(nwbfile):
comp = run(["python", "-m", "pynwb.validate",
Expand All @@ -179,7 +202,9 @@ def get_namespaces(nwbfile):
if comp.returncode != 0:
return []

return comp.stdout.split()
output_lines = comp.stdout.split('\n')
filtered_output = [line for line in output_lines if not re.search(r'warning', line, re.IGNORECASE) and line != '']
return filtered_output

namespaces = get_namespaces(nwb)

Expand Down

0 comments on commit e0389e5

Please sign in to comment.