Skip to content

Commit

Permalink
fixed io namespace issues by creating new io AFTER all the add_to_nwb…
Browse files Browse the repository at this point in the history
…file methods have been run
  • Loading branch information
pauladkisson committed Nov 12, 2024
1 parent 6960872 commit f611dba
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/neuroconv/tools/nwb_helpers/_metadata_and_file_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def make_or_load_nwbfile(
else:
load_kwargs.update(mode="w")

io = backend_io_class(**load_kwargs)
read_io = backend_io_class(**load_kwargs)

read_nwbfile = nwbfile_path_is_provided and append_mode
create_nwbfile = not read_nwbfile and not nwbfile_is_provided
Expand All @@ -249,7 +249,7 @@ def make_or_load_nwbfile(
if nwbfile_is_provided:
nwbfile = nwbfile_in
elif read_nwbfile:
nwbfile = io.read()
nwbfile = read_io.read()
elif create_nwbfile:
if metadata is None:
error_msg = "Metadata is required for creating an nwbfile "
Expand All @@ -266,16 +266,21 @@ def make_or_load_nwbfile(
finally:
if nwbfile_path_is_provided and nwbfile_loaded_succesfully:
try:
io.write(nwbfile)
read_io.close()
if read_nwbfile:
warn("Appending to an existing on-disk NWB file may be slow for large files.")
load_kwargs.update(mode="w")
# read_io CANNOT be used for writing bc it may not have access to all of the namespaces (extensions) added to the nwbfile -- see: https://github.com/rly/ndx-pose/issues/36
with backend_io_class(**load_kwargs) as write_io:
write_io.write(nwbfile)

if verbose:
print(f"NWB file saved at {nwbfile_path_in}!")
except Exception as write_error:
nwbfile_written_succesfully = False
raise write_error
finally:
io.close()
del io
del read_io

if not nwbfile_written_succesfully:
_attempt_cleanup_of_existing_nwbfile(nwbfile_path=nwbfile_path_in)
Expand Down

0 comments on commit f611dba

Please sign in to comment.