fix: guard degenerate ZStackLoop when calculating home index - #306
Merged
Merged
Conversation
A 1-plane ZStackLoop with dZStep=0 and dZLow == dZHigh (written by NIS-Elements for single-plane acquisitions on systems with a piezo Z device) made _calc_zstack_home_index divide by zero, so .experiment, .metadata, .sizes and .asarray() all raised ZeroDivisionError on otherwise valid 2D files. Return 0 for count <= 1 (which also stops the fallback branch returning -1 for count == 0), and 0 when dZLow == dZHigh, where every plane sits at the same z and no index is more "home" than another. Also replace the `(inverted and home_range_i) or home_range_f` idiom with real ternaries: it silently fell back to the non-inverted range whenever the inverted one was exactly 0.0 (i.e. home == high for types 2/3, or home == low for types 6/7). Fixes #305 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #306 +/- ##
==========================================
+ Coverage 93.65% 93.70% +0.05%
==========================================
Files 22 22
Lines 2617 2622 +5
==========================================
+ Hits 2451 2457 +6
+ Misses 166 165 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The ubuntu-latest (3.12) [lowest-direct] job failed at collection with `AttributeError: np.unicode_ was removed in the NumPy 2.0 release`, raised from inside xarray 2023.1.0. Under lowest-direct only *direct* deps are lowered, so xarray drops to its floor while transitive deps stay at their highest: pandas resolves to 3.x, which requires numpy >= 2, and xarray 2023.1.0 still refers to np.unicode_ at import time. pandas 3.x requires python >= 3.11, which is why only the 3.12 job broke and the 3.9/3.10 lowest-direct jobs pass. 2023.9.0 is the first xarray release that imports cleanly under numpy 2, and it still supports python >= 3.9, so the floor can be raised unconditionally. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Fixes #305
The bug
_calc_zstack_home_indexdivides byabs(high_um - low_um)with no guard. NIS-Elements writes a 1-planeZStackLoop(uiCount=1,dZStep=0,dZLow == dZHigh) for single-plane acquisitions on systems with a piezo Z device configured, so.experiment,.metadata,.sizesand.asarray()all raiseZeroDivisionErroron otherwise perfectly valid 2D files.Note that
_parse_z_stack_looponly recomputes a nonzerostepwhencount > 1, so within nd2 thestep_um <= 0branch is essentially only reachable in the degenerate configurations (count <= 1,dZLow == dZHigh, or a negativedZStep) — it is unexercised by all 102 files intests/data, which is why this went unnoticed.Changes
count <= 1returns0up front. Also stops the fallback branch returning-1forcount == 0.step_um <= 0branch returns0— every plane sits at the same z, so no index is more "home" than another.hrange = (inverted and home_range_i) or home_range_fdoesn't do what it looks like. WheninvertedisTruebuthome_range_iis exactly0.0, theandyields falsy0.0and it falls through to the non-inverted range. Replaced with real ternaries (which is what limnd2 uses).Tests
6 new tests in
tests/test_parse.py: the issue's exactuLoopParsthrough_parse_z_stack_loop, thecount > 1zero-range route into the divide, and the inverted-with-zero-range case for both type pairs (2/3 and 6/7 — old code returned4, correct is0).All 6 fail on
mainand pass here. Full suite green, and no goldenhomeIndexintests/samples_metadata.jsonshifted, so the ternary change is a no-op on every real file in the corpus.Caveat for review
The
step_um <= 0branch still has zero coverage from real files, so the values it returns for a non-degenerate loop (negativedZStep) remain unvalidated against the SDK. This PR makes the degenerate cases well-defined; it doesn't confirm the surviving arithmetic. @lanery's offer of a sample file for the test suite is still worth taking up.🤖 Generated with Claude Code