From 8032c95a548d518cfd360d4ec1d853f54f2113c2 Mon Sep 17 00:00:00 2001 From: Cody Baker Date: Tue, 17 Sep 2024 11:37:43 -0400 Subject: [PATCH] control when io is returned --- src/nwbinspector/_nwb_inspection.py | 2 +- src/nwbinspector/tools/_read_nwbfile.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/nwbinspector/_nwb_inspection.py b/src/nwbinspector/_nwb_inspection.py index 5220a2562..da5ef265d 100644 --- a/src/nwbinspector/_nwb_inspection.py +++ b/src/nwbinspector/_nwb_inspection.py @@ -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) diff --git a/src/nwbinspector/tools/_read_nwbfile.py b/src/nwbinspector/tools/_read_nwbfile.py index f7a274c92..d3eaada01 100644 --- a/src/nwbinspector/tools/_read_nwbfile.py +++ b/src/nwbinspector/tools/_read_nwbfile.py @@ -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. @@ -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 @@ -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)