Skip to content

Fix IndexError when loading image with auto-adjust saturation unchecked#1453

Open
chenyenchung wants to merge 3 commits into
MouseLand:mainfrom
chenyenchung:always-initialize-sat
Open

Fix IndexError when loading image with auto-adjust saturation unchecked#1453
chenyenchung wants to merge 3 commits into
MouseLand:mainfrom
chenyenchung:always-initialize-sat

Conversation

@chenyenchung
Copy link
Copy Markdown

Dear Cellpose team,

Thank you for the great tool! Please find below a bug report and a proposed fix for saturation updates in the GUI:

In Zstack mode (python -m cellpose --Zstack), if an image is loaded when auto-adjust saturation is unchecked, an IndexError: list index out of range is raised:

File ".../cellpose/gui/gui3d.py", line 348, in move_in_Z
    self.update_plot()
File ".../cellpose/gui/gui3d.py", line 568, in update_plot
    super().update_plot()
File ".../cellpose/gui/gui.py", line 1372, in update_plot
    self.img.setLevels(self.saturation[self.color - 1][self.currentZ])
IndexError: list index out of range

This appears to be introduced in a982535 when saturation update when parent.NZ changes was disabled. This causes self.saturation not updating its shape at loading and remaining [] or per the last image loaded when auto-adjust saturation was checked. The first time update_plot() indexes self.saturation[c][self.currentZ] (e.g. via move_in_Z after the user scrolls, or immediately during the initial render at currentZ = NZ // 2), the list is shorter than the new NZ and indexing fails.

To fix this issue, in cellpose/gui/io.py _initialize_images, after parent.NZ is updated to the new image's Z depth, add an else branch to the existing if parent.autobtn.isChecked(): block that re-sizes self.saturation to 3 × NZ. The user's previous per-channel values are preserved by broadcasting each channel's first-Z value across the new Z range; channels with no previous value default to [0, 255].

The structure is the same 3 × NZ × 2 (channel × Z × [lo, hi]) that compute_saturation produces, so update_plot, level_change, and the ortho views can index it without further changes.

A new test asserting self.saturation is 3 × NZ after loading with auto-adjust off is also added.

Please let me know if you need more information. Thank you.

Best,
Yen

@chenyenchung chenyenchung force-pushed the always-initialize-sat branch from 8b06240 to e95f936 Compare May 15, 2026 21:12
@carsen-stringer carsen-stringer requested a review from mrariden May 17, 2026 19:49
Copy link
Copy Markdown
Collaborator

@mrariden mrariden left a comment

Choose a reason for hiding this comment

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

See if the GH tests pass, and remove the tests if they dont

Comment thread cellpose/gui/gui.py
super(MainW, self).__init__()

self.logger = logger
self.logger = logger if logger is not None else logging.getLogger(__name__)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

helpful, thanks

Comment thread tests/test_gui.py
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These tests seem a bit fragile but we can keep them if they pass the GH workflow.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 41.86%. Comparing base (ef481bf) to head (9228e8e).
⚠️ Report is 77 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1453      +/-   ##
==========================================
- Coverage   41.99%   41.86%   -0.13%     
==========================================
  Files          16       16              
  Lines        3777     3810      +33     
==========================================
+ Hits         1586     1595       +9     
- Misses       2191     2215      +24     

☔ 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.

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.

2 participants