Update gui.py for
fix: GUI flow visualization indexing and gradZ contrast bugs
- Fix IndexError when viewing flows with different array layouts (C,Z,Y,X)
- Add robust Z-axis slicing for flow views with _pick_z_image() helper
- Apply normalize99() to gradZ for contrast stretching
- Correct flow array index mapping for 3D gradZ/cellprob views
- Prevent TypeError from incompatible array shapes in pyqtgraph
Resolves crashes and poor visibility in 3D flow visualizations.
Fix cellpose GUI flow visualization indexing and contrast bugs
Summary
This PR fixes critical bugs in cellpose GUI's flow visualization system that caused crashes and poor contrast in 3D datasets. The fixes address both indexing errors and normalization issues that made gradZ views unusable.
Root Cause Analysis The GUI had multiple interconnected issues with flow visualization in 3D mode: Z-axis indexing bug: Original code assumed Z was always axis 0, causing IndexError when flow arrays came in different formats (e.g., (C, Z, Y, X))
Poor gradZ normalization: gradZ used /10 * 127 + 127 scaling, severely compressing dynamic range
Incorrect flow array mapping: View dropdown indices didn't correctly map to flow array indices in 3D mode
Changes Made:
-
Robust Z-axis slicing (update_plot() lines 1386-1441) Problem: Flow arrays can have different layouts: (Z, Y, X), (C, Z, Y, X), (Y, X, C), causing IndexError when accessing flows[view-1][currentZ] Solution: Added _pick_z_image() helper function that: Finds Z axis by matching nZ dimension size Properly slices along correct Z axis regardless of array layout Ensures channels are moved to last dimension for pyqtgraph compatibility Prevents TypeError: data.shape[2] must be <= 4 by collapsing incompatible dimensions
-
Improved gradZ normalization (compute_segmentation() line 1991-1992) Problem: gradZ was dim due to poor normalization: (flows[1][0] / 10 * 127 + 127) Solution: Applied same normalize99() treatment as cellprob for excellent contrast:
-
Corrected flow array indexing (update_plot() lines 1424-1430) Problem: View dropdown indices didn't map correctly to flow array positions in 3D mode Solution: Added explicit mapping for 3D flow indices:
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 48.83%. Comparing base (4b4ffd7) to head (b57d960).
:warning: Report is 10 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #1295 +/- ##
==========================================
- Coverage 48.89% 48.83% -0.07%
==========================================
Files 16 16
Lines 3767 3774 +7
==========================================
+ Hits 1842 1843 +1
- Misses 1925 1931 +6
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.