Fix NWBFile.objects going stale after the file is modified - #2243
Open
HugoFara wants to merge 1 commit into
Open
Fix NWBFile.objects going stale after the file is modified#2243HugoFara wants to merge 1 commit into
NWBFile.objects going stale after the file is modified#2243HugoFara wants to merge 1 commit into
Conversation
NWBFile.objects populated its LabelledDict once, lazily, and nothing ever invalidated it, so the first read of the property decided its contents for the life of the NWBFile: containers added afterwards were absent from it and containers removed afterwards were still in it. all_children() also repopulated the cache as a side effect, so whether objects was correct depended on how reads and writes in otherwise unrelated code interleaved. Build the dict on every access instead. all_children() no longer touches any cached state; objects walks it and keys the result by object ID, keeping the warning for children without an object_id in the walk where it was before. Fixes NeurodataWithoutBorders#2242
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #2243 +/- ##
==========================================
- Coverage 96.22% 96.22% -0.01%
==========================================
Files 30 30
Lines 2993 2992 -1
Branches 433 434 +1
==========================================
- Hits 2880 2879 -1
Misses 64 64
Partials 49 49
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Author
|
CI is not passing, I can create a second PR to fix it but I'd rather ask maintainers before stacking up work. Two solutions from what I see:
1 is cleaner, but 2 is easy if you would rather not break that usage. The codecov patch percentage is the two branch partials in the new loop; I can add a case for a child without an |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
NWBFile.objectspopulates its dict once, lazily, and nothing invalidates it, so the first read decides its contents for the life of the file: containers added afterwards are missing from it, containers removed are still in it.all_children()repopulates the same cache as a side effect, so whetherobjectsis correct depends on how reads and writes in unrelated code interleave.Nothing raises when it goes wrong. In #2242 a read-only inspection froze the dict at 42 objects in a file that ended up with 85; NeuroConv's backend configuration then walked
.objectsand wrote the other 43 containers with backend defaults, i.e. uncompressed — visible only by reopening the file and checking its filters.objectsis now rebuilt on every access, andall_children()no longer writes to cached state. I considered keeping the cache and invalidating it on mutation, but HDMF exposes no hook for "a descendant was added or removed", so invalidation could only be best-effort. Rebuilding costs one walk per access, so reading.objectsinside a loop over containers is now O(n²) where it was O(n); happy to trade the other way if you prefer.How to test the behavior?
The snippet in #2242 prints
False / True / True / False / TrueondevandTrue / True / True / True / Falsewith this change. End to end through NeuroConv 0.9.4, the threeTimeSeriesadded after the.objectsread are writtencompression=None, chunks=Noneondevandgzip, chunks=(1000,)here.Three regression tests in
tests/unit/test_file.py—test_objects_includes_container_added_after_first_access,test_objects_includes_nested_container_added_after_first_access,test_objects_excludes_removed_container— fail ondevand pass with the change.pytest tests/unit: 575 passed, 3 skipped.pytest tests/integration/hdf5: 223 passed, 3 skipped.ruff check .andcodespellclean. Python 3.13, hdmf 6.1.0, h5py 3.16.0.Fix #2242
Checklist
ruff check . && codespellfrom the source directory.Pull request created by Claude Opus 5 using the principles of knock-first.