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

Container source not set for some references #218

Closed
rly opened this issue Dec 10, 2019 · 0 comments · Fixed by #219
Closed

Container source not set for some references #218

rly opened this issue Dec 10, 2019 · 0 comments · Fixed by #219
Labels
category: bug errors in the code or code behavior

Comments

@rly
Copy link
Contributor

rly commented Dec 10, 2019

On write, Container.container_source is set for all built objects not read from a file. This field tracks where a Container object is located on disk (e.g., file path) so that links can be made across files. This also prevents a Container object from being written to multiple files.

Oddly, in some cases, Container.container_source is not set at all. Specifically, when Container A makes a reference to Container B, and Container A gets built first, then Container B does not get a container_source. However, if no container makes a reference to Container B before Container B is built, then Container B does get a container_source.

This occurs in PyNWB when writing an NWBFile with a Device, an ElectrodeGroup, and an ElectricalSeries that references those. The ElectricalSeries gets built first, which results in the Device and ElectrodeGroup being built without a container_source set.

If users are not careful, this can lead to unexpected/inconsistent behavior when interacting with a Container that is missing its container_source. For example, a Container without a container_source can be added to multiple files under different build managers. This creates a broken link in all files after the first. Admittedly, this is a bit of an edge case and is minor.

MWE:

from pynwb import NWBFile, NWBHDF5IO
from pynwb.ecephys import ElectricalSeries
from datetime import datetime
import numpy as np


nwbfile = NWBFile(session_description='ADDME',
                  identifier='ADDME',
                  session_start_time=datetime.now().astimezone())

dev1 = nwbfile.create_device('dev1')
group = nwbfile.create_electrode_group('tetrode1', 'tetrode description', 'tetrode location', dev1)

nwbfile.add_electrode(id=1, x=1.0, y=2.0, z=3.0, imp=-1.0, location='CA1', filtering='none', group=group,
                      group_name='tetrode1')

region = nwbfile.create_electrode_table_region(
    region=(0, ),
    name='electrodes',
    description='desc'
)
ts = ElectricalSeries(
    name='test_data',
    data=np.arange(10),
    timestamps=np.arange(10),
    electrodes=region
)
nwbfile.add_acquisition(ts)

with NWBHDF5IO('nwbfile.nwb', 'w') as io:
    io.write(nwbfile)

# dev1 and group do not have container_source set. all other Containers do.
print(dev1.container_source)
print(group.container_source)

# add dev1 and group to a new file
nwbfile2 = NWBFile(session_description='ADDME',
                   identifier='ADDME',
                   session_start_time=datetime.now().astimezone())
nwbfile2.add_device(dev1)
nwbfile2.add_electrode_group(group)

# using a new build manager, a new file can be written with dev1 and group,
# but the file has broken links for them
with NWBHDF5IO('nwbfile2.nwb', 'w') as io:
    io.write(nwbfile2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: bug errors in the code or code behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant