Skip to content

Commit

Permalink
add NWBHDF5IO.read_nwb() method
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Nov 7, 2024
1 parent 2259bed commit 36df49c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,24 @@ def export(self, **kwargs):
kwargs['container'] = nwbfile
super().export(**kwargs)

@staticmethod
@docval({'name': 'path', 'type': (str, Path), 'doc': 'the path to the HDF5 file', 'default': None},
{'name': 'file', 'type': [h5py.File, 'S3File'], 'doc': 'a pre-existing h5py.File object', 'default': None},
is_method=False)
def read_nwb(**kwargs):
"""
Helper factory method for reading an NWB file and return the NWBFile object
"""
# Retrieve the filepath
path = popargs('path', kwargs)
file = popargs('file', kwargs)

# open the file with NWBZarrIO and rad the file
io = NWBHDF5IO(path=path, file=file, mode="r", load_namespaces=True)
nwbfile = io.read()

# return the NWBFile object
return nwbfile

from . import io as __io # noqa: F401,E402
from .core import NWBContainer, NWBData # noqa: F401,E402
Expand Down
28 changes: 28 additions & 0 deletions tests/integration/hdf5/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,31 @@ 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_read_nwb_method_path(self):

# write the example file
with NWBHDF5IO(self.path, 'w') as io:
io.write(self.nwbfile)

# test that the read_nwb method works
read_nwbfile = NWBHDF5IO.read_nwb(path=self.path)
self.assertContainerEqual(read_nwbfile, self.nwbfile)

read_nwbfile.get_read_io().close()

def test_read_nwb_method_file(self):

# write the example file
with NWBHDF5IO(self.path, 'w') as io:
io.write(self.nwbfile)

import h5py

file = h5py.File(self.path, 'r')

read_nwbfile = NWBHDF5IO.read_nwb(file=file)
self.assertContainerEqual(read_nwbfile, self.nwbfile)

read_nwbfile.get_read_io().close()

0 comments on commit 36df49c

Please sign in to comment.