Skip to content

Fix BDV N5 setup over-allocation#1207

Merged
AdvancedImagingUTSW merged 1 commit into
developfrom
find-error-in-n5-image-writer
May 27, 2026
Merged

Fix BDV N5 setup over-allocation#1207
AdvancedImagingUTSW merged 1 commit into
developfrom
find-error-in-n5-image-writer

Conversation

@AdvancedImagingUTSW

@AdvancedImagingUTSW AdvancedImagingUTSW commented Mar 21, 2026

Copy link
Copy Markdown
Collaborator

Motivation

  • N5 writer was creating an extra, unwritten position worth of setup groups at the end of a complete acquisition because setup expansion was performed after advancing the frame index.
  • Copilot also pointed out that simply moving expansion before the write exposed a second issue for dynamic growth: c * positions + p changes existing setup IDs when positions grows, which can overwrite an existing channel/position setup.

Description

  • Move dynamic N5/HDF5 setup expansion in BigDataViewerDataSource.write() to happen before the write, and only when the current frame actually targets a new position (if pos >= self.positions: ...).
  • Use an append-friendly setup ID mapping, position * channel_count + channel, for HDF5/N5 dataset names and BDV XML metadata.
  • Keep metadata.positions synchronized when dynamic growth occurs so the XML includes positions that were actually written.
  • Make the BDV data source tests deterministic by using explicit z/time dimensions, deterministic image and stage data, and deterministic stop-early behavior instead of random shape choices.
  • Update the BDV metadata test expectation to the append-friendly setup ordering.

Evidence from 2026-05-21 acquisitions

  • Inspected /Users/Dean/Desktop/pr1207/20260521_lung_mv3_488nm_562nm.
  • cell_001 (per_stack) has 4 N5 setups, 240 chunk files, 12 MIP files, and 12 XML view registrations.
  • cell_002 (per_z) has 4 N5 setups, 240 chunk files, 12 MIP files, and 12 XML view registrations.
  • Logs for both acquisitions report 240 total received frames and include the final expected frame C=1, Z=19, T=2, P=1.

Testing

  • uv run --python 3.9 --extra dev python -m pytest -p no:cov -o addopts= test/model/data_sources/test_bdv_data_source.py::test_bdv_getitem -vv
    • Result: 24 passed in 36.89s.
  • uv run --python 3.9 --extra dev python -m pytest -p no:cov -o addopts= test/model/data_sources/test_bdv_data_source.py
    • Result: 122 passed, 196 warnings in 130.29s.
  • uv run --python 3.9 --extra dev python -m pytest -p no:cov -o addopts= test/model/metadata_sources/test_bdv_metadata.py::test_bdv_xml_dict -vv
    • Result: 2 passed in 2.56s.
  • uv run --python 3.9 --extra dev ruff check test/model/data_sources/test_bdv_data_source.py test/model/metadata_sources/test_bdv_metadata.py
    • Result: All checks passed.

Codex Task

Copilot AI review requested due to automatic review settings March 21, 2026 19:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts BDV N5/HDF5 writer behavior to prevent creating an extra (unwritten) “position worth” of setup groups at the end of a completed acquisition by changing when dynamic setup expansion occurs in BigDataViewerDataSource.write().

Changes:

  • Move dynamic setup expansion to occur immediately before a write that targets a new position.
  • Remove the post-write expansion logic that ran after incrementing _current_frame.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/navigate/model/data_sources/bdv_data_source.py
@AdvancedImagingUTSW AdvancedImagingUTSW force-pushed the find-error-in-n5-image-writer branch 2 times, most recently from 8de0d03 to 711f95e Compare May 21, 2026 20:42
@AdvancedImagingUTSW AdvancedImagingUTSW force-pushed the find-error-in-n5-image-writer branch from 711f95e to 597fd18 Compare May 21, 2026 20:49
Move dynamic BDV N5 setup expansion ahead of the frame write so the writer only creates setup groups for positions that are actually receiving image data. The previous post-write check recomputed indices after _current_frame advanced; at the end of a complete acquisition that pointed at the next position and serialized an empty position worth of setup metadata.

Use an append-friendly setup id mapping, position * channel_count + channel, for both HDF5/N5 dataset names and BDV XML metadata. This keeps existing setup ids stable when dynamic acquisitions grow from one position to more positions; otherwise c * positions + p changes when positions grows and can overwrite an existing channel/position setup.

Keep the metadata position count in sync when dynamic growth occurs so the XML describes the extra positions that were actually written.

Make the BDV data source tests deterministic by using explicit z/time dimensions, deterministic image and stage data, and deterministic stop-early behavior instead of random shape choices. Update the BDV metadata test expectation to the append-friendly setup ordering.

Validation:

- Verified fresh 2026-05-21 PR data in /Users/Dean/Desktop/pr1207/20260521_lung_mv3_488nm_562nm for both per-stack cell_001 and per-Z cell_002 acquisitions: each run has 4 N5 setups, 240 chunk files, 12 MIP files, and 12 XML view registrations.

- Confirmed logs for both 2026-05-21 acquisitions received all 240 expected frames and wrote the final C=1, Z=19, T=2, P=1 frame.

- Added a regression test for dynamic growth from one to two positions with two channels; it verifies setup data, N5 chunk creation, XML ViewSetup ids, and XML ViewRegistration setup ids.

- Ran: uv run --python 3.9 --extra dev python -m pytest -p no:cov -o addopts= test/model/data_sources/test_bdv_data_source.py::test_bdv_getitem -vv

- Result: 24 passed in 36.89s.

- Ran: uv run --python 3.9 --extra dev python -m pytest -p no:cov -o addopts= test/model/data_sources/test_bdv_data_source.py

- Result: 122 passed, 196 warnings in 130.29s.

- Ran: uv run --python 3.9 --extra dev python -m pytest -p no:cov -o addopts= test/model/metadata_sources/test_bdv_metadata.py::test_bdv_xml_dict -vv

- Result: 2 passed in 2.56s.

- Ran: uv run --python 3.9 --extra dev ruff check test/model/data_sources/test_bdv_data_source.py test/model/metadata_sources/test_bdv_metadata.py

- Result: All checks passed.
@AdvancedImagingUTSW AdvancedImagingUTSW force-pushed the find-error-in-n5-image-writer branch from 597fd18 to 7eeeda5 Compare May 22, 2026 12:09
@codecov

codecov Bot commented May 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.41%. Comparing base (3999f2c) to head (7eeeda5).
⚠️ Report is 15 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1207      +/-   ##
===========================================
+ Coverage    63.65%   64.41%   +0.75%     
===========================================
  Files          189      190       +1     
  Lines        26029    26279     +250     
===========================================
+ Hits         16569    16927     +358     
+ Misses        9460     9352     -108     
Flag Coverage Δ
unittests 64.41% <100.00%> (+0.75%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AdvancedImagingUTSW AdvancedImagingUTSW merged commit 60cea86 into develop May 27, 2026
1 check passed
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.

3 participants