Skip to content

Commit

Permalink
Return can_read as False if no nwbfile version is found (#1934)
Browse files Browse the repository at this point in the history
* add can_read catch if no nwb_version found

* add tests for can_read method

* fix comment in test description

* fix test name

* update CHANGELOG.md

* add warnings for can_read conditions
  • Loading branch information
stephprince authored Jul 18, 2024
1 parent 570fb3b commit a9f6332
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Documentation and tutorial enhancements
- Added pre-release pull request instructions to release process documentation @stephprince [#1928](https://github.com/NeurodataWithoutBorders/pynwb/pull/1928)

### Bug fixes
- Fixed `can_read` method to return False if no nwbfile version can be found @stephprince [#1934](https://github.com/NeurodataWithoutBorders/pynwb/pull/1934)

## PyNWB 2.8.1 (July 3, 2024)

### Documentation and tutorial enhancements
Expand Down
11 changes: 10 additions & 1 deletion src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path
from pathlib import Path
from copy import deepcopy
from warnings import warn
import h5py

from hdmf.spec import NamespaceCatalog
Expand Down Expand Up @@ -269,7 +270,15 @@ def can_read(path: str):
return False
try:
with h5py.File(path, "r") as file: # path is HDF5 file
return get_nwbfile_version(file)[1][0] >= 2 # Major version of NWB >= 2
version_info = get_nwbfile_version(file)
if version_info[0] is None:
warn("Cannot read because missing NWB version in the HDF5 file. The file is not a valid NWB file.")
return False
elif version_info[1][0] < 2: # Major versions of NWB < 2 not supported
warn("Cannot read because PyNWB supports NWB files version 2 and above.")
return False
else:
return True
except IOError:
return False

Expand Down
38 changes: 38 additions & 0 deletions tests/integration/hdf5/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,41 @@ def test_round_trip_with_pathlib_path(self):
with NWBHDF5IO(pathlib_path, 'r') as io:
read_file = io.read()
self.assertContainerEqual(read_file, self.nwbfile)

def test_can_read_current_nwb_file(self):
with NWBHDF5IO(self.path, 'w') as io:
io.write(self.nwbfile)
self.assertTrue(NWBHDF5IO.can_read(self.path))

def test_can_read_file_does_not_exits(self):
self.assertFalse(NWBHDF5IO.can_read('not_a_file.nwb'))

def test_can_read_file_no_version(self):
# write the example file
with NWBHDF5IO(self.path, 'w') as io:
io.write(self.nwbfile)
# remove the version attribute
with File(self.path, mode='a') as io:
del io.attrs['nwb_version']

# assert can_read returns False and warning raised
warn_msg = "Cannot read because missing NWB version in the HDF5 file. The file is not a valid NWB file."
with self.assertWarnsWith(UserWarning, warn_msg):
self.assertFalse(NWBHDF5IO.can_read(self.path))

def test_can_read_file_old_version(self):
# write the example file
with NWBHDF5IO(self.path, 'w') as io:
io.write(self.nwbfile)
# set the version attribute <2.0
with File(self.path, mode='a') as io:
io.attrs['nwb_version'] = "1.0.5"

# assert can_read returns False and warning raised
warn_msg = "Cannot read because PyNWB supports NWB files version 2 and above."
with self.assertWarnsWith(UserWarning, warn_msg):
self.assertFalse(NWBHDF5IO.can_read(self.path))

def test_can_read_file_invalid_hdf5_file(self):
# current file is not an HDF5 file
self.assertFalse(NWBHDF5IO.can_read(__file__))

0 comments on commit a9f6332

Please sign in to comment.