Closed etiennedemontalivet closed 6 months ago
Which version of HDMF and PyNWB are you using? Based on the error I suspect the issue is that the file is closed, i.e., NWBHDF5IO
is being created in the context and then display(read_nwbfile)
is being called outside of the context, at which point NWBHDF5IO
no longer exists. I.e., I suspect if you move the display(read_nwbfile)
inside of the context, i.e., if you replace:
with NWBHDF5IO("basics_tutorial.nwb", "r") as io: read_nwbfile = io.read() display(read_nwbfile)
with
with NWBHDF5IO("basics_tutorial.nwb", "r") as io:
read_nwbfile = io.read()
display(read_nwbfile)
that the error will probably go away. It would be great if you could test if that fixes the problem.
I believe this issue was addressed in https://github.com/hdmf-dev/hdmf/pull/882, which is why I was asking about which versions of PyNWB and HDMF you have installed.
Thanks! That fixes the problem 👍 PS: I'm using
Glad that fixed the issue.
Thanks for adding the package versions. Looking at the release notes, I think this issue should already be fixed in those versions. @stephprince could you run the above example in a Jupyter notebook to see if you see the same issue, just to make sure this is really fixed.
I tested in a notebook and see the same issue.
Everything works ok when I make a function that returns io.read() like described in the hdmf issue you linked:
def read_nwb(fname):
io = NWBHDF5IO(fname, mode='r')
return io.read()
nwbfile = read_nwb("basics_tutorial.nwb")
display(nwbfile) # looks normal
But I get the same ValueError: Dset_id is not a dataset id (dset_id is not a dataset ID)
as above when I run the code below. However, I'm still able to access all the fields except for the electrodes table data.
with NWBHDF5IO("basics_tutorial.nwb", "r") as io:
nwbfile = io.read()
display(nwbfile) # error
This same issue came up with someone else I was working with. Before the new awesome expandable html representation of an HDMF container, printing an HDMF container (e.g., NWB file) in a Jupyter notebook after the IO object has been closed did not cause an error. Now it does. Printing a container in such a situation is not recommended because the file is closed and datasets are not accessible. However, it is a common mistake to try to print the container outside of a context manager. I suggest we improve the messaging around this.
Option 1:
We get rid of the error if possible. I'm not sure why a dataset is being accessed such that we get a ValueError
. I think it has to do with printing DynamicTable
objects but I have not looked into this.
Option 2:
If the IO object is closed, we change the print behavior so that it simply says <Closed [container name]>
, e.g., <Closed NWBFile>
, just like h5py prints <Closed HDF5 file>
or <Closed HDF5 group>
if the file or group has already been closed. This might catch all issues with closed IO objects, but at the same time, the current behavior where users can still access the already-read non-dataset metadata in the container after the IO object is closed is kind of nice.
after the IO object has been closed did not cause an error.
We should check what the context manager is doing. https://github.com/hdmf-dev/hdmf/pull/882 added the functionality to have the io object stored on the Container so that it can remain open. This change was to allow us to return the Container from a function without having to also return the io object explicitly. However, I think this same approach should also work here, as long as we have a way to make sure that NWBHDF5IO
does not call close
when leaving the context. In particular in read mode this should should be save since the file will be closed when the last Container gets deleted.
What happened?
When I save electrodes information in NWB file (using Ecephys tutorial) and that I then write/read the NWB file using basic IO (writing, reading), the NWB file is displayed correctly with the in-RAM NWB file (before saving) but crashes with the loaded NWB file (after writing/reading back).
I would expect the NWB file that has been read to display like this:
Steps to Reproduce
Traceback
Operating System
Windows
Python Executable
Python
Python Version
3.9
Package Versions
pyproject_issue.txt
Code of Conduct