Skip to content

Commit

Permalink
control when io is returned
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyCBakerPhD committed Sep 17, 2024
1 parent e4be201 commit 8032c95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/nwbinspector/_nwb_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def inspect_nwbfile(
filterwarnings(action="ignore", message="Ignoring cached namespace .*")

try:
in_memory_nwbfile, io = read_nwbfile(nwbfile_path=nwbfile_path)
in_memory_nwbfile, io = read_nwbfile(nwbfile_path=nwbfile_path, return_io=True)

if not skip_validate:
validation_errors = pynwb.validate(io=io)
Expand Down
13 changes: 10 additions & 3 deletions src/nwbinspector/tools/_read_nwbfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def read_nwbfile(
nwbfile_path: Union[str, Path],
method: Optional[Literal["local", "fsspec", "ros3"]] = None,
backend: Optional[Literal["hdf5", "zarr"]] = None,
) -> tuple[NWBFile, HDMFIO]:
return_io: bool = False,
) -> Union[NWBFile, tuple[NWBFile, HDMFIO]]:
"""
Read an NWB file using the specified (or auto-detected) method and specified (or auto-detected) backend.
Expand All @@ -86,12 +87,15 @@ def read_nwbfile(
backend : "hdf5", "zarr", or None (default)
Type of backend used to write the file.
The default auto-detects the type of the file.
return_io : bool, default: False
Whether to return the HDMFIO object used to open the file.
Returns
-------
nwbfile : pynwb.NWBFile
The in-memory NWBFile object.
io : hdmf.backends.io.HDMFIO
io : hdmf.backends.io.HDMFIO, optional
Only passed if `return_io` is True.
The initialized HDMFIO object used to read the file.
"""
nwbfile_path = str(nwbfile_path) # If pathlib.Path, cast to str; if already str, no harm done
Expand Down Expand Up @@ -134,4 +138,7 @@ def read_nwbfile(
io = BACKEND_IO_CLASSES[backend](**io_kwargs)
nwbfile = io.read()

return (nwbfile, io)
if return_io:
return (nwbfile, io)
else: # Note: do not be concerned about io object closing due to garbage collection here
return nwbfile # (it is attached as an attribute to the NWBFile object)

0 comments on commit 8032c95

Please sign in to comment.