Skip to content

Fix reading a Units table whose columns are links - #2274

Open
monsterlag wants to merge 1 commit into
NeurodataWithoutBorders:devfrom
monsterlag:fix/issue-1223-298f
Open

monsterlag wants to merge 1 commit into
NeurodataWithoutBorders:devfrom
monsterlag:fix/issue-1223-298f

Conversation

@monsterlag

Copy link
Copy Markdown

Note

AI-generated, not reviewed by a human. This pull request was written by an AI coding agent and then checked by a separate AI review agent, which re-ran the tests described below. No human has reviewed the code. It is submitted by @monsterlag, who runs these agents. If that does not fit how you want to receive contributions, please feel free to close it.

Motivation

Fix #1223

Reading a file that was written from a shallow copy (NWBFile.copy()) of a file with a Units table fails. In the copy, the Units group is new but its columns (spike_times, waveform_mean, ...) are links back to the original file, so the builders that UnitsMap gets for them are LinkBuilders. UnitsMap.resolution_carg and UnitsMap._get_waveform_stat read .attributes directly from those builders and fail with:

AttributeError: 'LinkBuilder' object has no attribute 'attributes'

The script in #1223 still reproduces this on current dev (with HDMF 6.2.0).

This PR adds a small UnitsMap._get_column_builder helper that follows the link to the target builder when a column is a LinkBuilder, the same way TimeIntervalsMap.columns_carg already handles a linked timeseries column (#1863), and uses it in both places. resolution, waveform_rate, waveform_unit, and waveform_time_before_peak_in_ms are now read from the linked columns.

How to test the behavior?

tests/integration/hdf5/test_file_copy.py::TestFileCopy::test_copy_file_link_units_columns writes a file with a Units table that has resolution, waveform_rate, and waveform_unit set, writes a shallow copy of it, reads the copy, and checks those values and the spike times.

python -m pytest tests/integration/hdf5/test_file_copy.py

Without the fix, the new test fails with AttributeError: 'LinkBuilder' object has no attribute 'attributes' raised from src/pynwb/io/misc.py. With the fix:

  • python -m pytest tests/integration/hdf5/test_file_copy.py tests/integration/hdf5/test_misc.py tests/unit/test_misc.py: 75 passed
  • python -m pytest tests/unit tests/integration: 835 passed, 19 skipped
  • the reproduction script from Cannot read file after nwbfile.copy() with Units #1223 now reads the copy and prints the spike times
  • ruff check . and codespell: clean

Checklist

  • Did you update CHANGELOG.md with your changes?
  • Have you checked our Contributing document?
  • Have you ensured the PR clearly describes the problem and the solution?
  • Is your contribution compliant with our coding style? This can be checked running ruff check . && codespell from the source directory.
  • Have you checked to ensure that there aren't other open Pull Requests for the same change?
  • Have you included the relevant issue number using "Fix #XXX" notation where XXX is the issue number? By including "Fix #XXX" you allow GitHub to close issue #XXX when the PR is merged.

UnitsMap read attributes directly from the column builders, which fails
with AttributeError when a column is a LinkBuilder, e.g., in a file
written from a shallow copy made with NWBFile.copy(). Follow the link to
the target builder first.

Fix NeurodataWithoutBorders#1223

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot read file after nwbfile.copy() with Units

1 participant