Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix make_or_load_nwbfile #1138

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading