Skip to content

Commit

Permalink
steph suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Jan 15, 2025
1 parent 33847a6 commit 0bb8f1a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
27 changes: 19 additions & 8 deletions src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ def read_nwb(**kwargs):
This function uses the following defaults:
* Always opens in read-only mode
* Automatically loads namespaces
* Detects file format based on extension
* Reads any backend (e.g. HDF5 or Zarr) if there is an IO class available.
Advanced features requiring direct use of IO classes include:
Advanced features requiring direct use of IO classes (e.g. NWBHDF5IO NWBZarrIO) include:
* Streaming data from s3
* Custom namespace extensions
* Parallel I/O with MPI
Expand All @@ -583,16 +583,27 @@ def read_nwb(**kwargs):
"""

path = popargs('path', kwargs)
# HDF5 is always available so we try that first
backend_is_hdf5 = NWBHDF5IO.can_read(path=path)
if backend_is_hdf5:
return NWBHDF5IO.read_nwb(path=path)
else:
from hdmf_zarr import NWBZarrIO
backend_is_zarr = NWBZarrIO.can_read(path=path)
if backend_is_zarr:
return NWBZarrIO.read_nwb(path=path)
else:
raise ValueError(f"Unsupported backend for file: {path}")
# If hdmf5 zarr is available we try that next
try:
from hdmf_zarr import NWBZarrIO
backend_is_zarr = NWBZarrIO.can_read(path=path)
if backend_is_zarr:
return NWBZarrIO.read_nwb(path=path)
else:
raise ValueError(
f"Unable to read file: '{path}'. The file exists but is not recognized as "
"either a valid HDF5 or Zarr NWB file. Please ensure the file contains valid NWB data."
)
except ImportError:
raise ValueError(
f"Unable to read file: '{path}'. The file is not recognized as an HDF5 NWB file. "
"If you are trying to read a Zarr file, please install hdmf-zarr using: pip install hdmf-zarr"
)



Expand Down
44 changes: 35 additions & 9 deletions tests/integration/io/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@


class TestReadNWBMethod(TestCase):
"""
Test that H5DataIO functions correctly on round trip with the HDF5IO backend
"""
"""Test suite for the read_nwb function."""

def setUp(self):
self.nwbfile = mock_NWBFile()


def test_read_nwb_hdf5(self):
"""Test reading a valid HDF5 NWB file."""
from pynwb import NWBHDF5IO

with tempfile.TemporaryDirectory() as temp_dir:
Expand All @@ -35,14 +34,41 @@ def test_read_nwb_hdf5(self):

@unittest.skipIf(not HAVE_NWBZarrIO, "NWBZarrIO library not available")
def test_read_zarr(self):
# from pynwb import NWBZarrIO
from hdmf_zarr import NWBZarrIO

"""Test reading a valid Zarr NWB file."""
with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir) / "test.nwb"
path = Path(temp_dir) / "test.zarr"
with NWBZarrIO(path, 'w') as io:
io.write(self.nwbfile)

read_nwbfile = read_nwb(path=path)
self.assertContainerEqual(read_nwbfile, self.nwbfile)
read_nwbfile.get_read_io().close()
read_nwbfile.get_read_io().close()

def test_read_zarr_without_hdmf_zarr(self):
"""Test attempting to read a Zarr file without hdmf_zarr installed."""
if HAVE_NWBZarrIO:
self.skipTest("hdmf_zarr is installed")

with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir) / "test.zarr"
path.mkdir() # Create empty directory to simulate Zarr store

expected_message = (
f"Unable to read file: '{path}'. The file is not recognized as an HDF5 NWB file. "
"If you are trying to read a Zarr file, please install hdmf-zarr using: pip install hdmf-zarr"
)

with self.assertRaisesWith(ValueError, expected_message):
read_nwb(path=path)

@unittest.skipIf(not HAVE_NWBZarrIO, "NWBZarrIO library not available. Neeed for correct error message.")

Check failure on line 64 in tests/integration/io/test_read.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

Neeed ==> Need, Needed
def test_read_invalid_file(self):
"""Test attempting to read a file that exists but is neither HDF5 nor Zarr."""
with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir) / "test.txt"
path.write_text("Not an NWB file")

with self.assertRaisesWith(ValueError,
f"Unable to read file: '{path}'. The file exists but is not recognized as "
"either a valid HDF5 or Zarr NWB file. Please ensure the file contains valid NWB data."):
read_nwb(path=path)

0 comments on commit 0bb8f1a

Please sign in to comment.