Skip to content

Fix render_images(norm=LogNorm()) crash on zero/non-positive data#672

Merged
timtreis merged 3 commits into
mainfrom
fix/issue-604-lognorm-zeros
May 20, 2026
Merged

Fix render_images(norm=LogNorm()) crash on zero/non-positive data#672
timtreis merged 3 commits into
mainfrom
fix/issue-604-lognorm-zeros

Conversation

@timtreis
Copy link
Copy Markdown
Member

@timtreis timtreis commented May 20, 2026

Summary

  • render_images(norm=LogNorm()) crashed with an opaque matplotlib ValueError: Invalid vmin or vmax whenever the autoscaled vmin/vmax ended up non-positive (e.g. all-zero data, all-negative data, user-supplied LogNorm(vmin=0,…)). The default colorbar="auto" made this hit the standard call path.
  • Add a narrow guard at the top of _draw_colorbar (basic.py): if the mappable's norm is LogNorm and its bounds are invalid, emit a UserWarning pointing to colorbar=False / data clipping and skip the colorbar. The image artist itself still draws.
  • Guard is class-specific — SymLogNorm (which legitimately handles zero/negative data via its linear region) is unaffected.

Closes #604

timtreis added 3 commits May 20, 2026 15:06
render_images(norm=LogNorm()) crashed with an opaque matplotlib
"Invalid vmin or vmax" when the image data was all zeros (or otherwise
left LogNorm's vmin/vmax non-positive after autoscale). The default
colorbar="auto" hit the crash without unusual args.

Add a narrow guard at the start of _draw_colorbar: when the mappable
norm is LogNorm and its (already autoscaled) bounds are invalid, emit
a UserWarning pointing to colorbar=False / data clipping and skip the
colorbar. The image artist itself still renders.

Guard is class-specific so SymLogNorm (which legitimately supports
zero/negative values) is unaffected.

Closes #604
vmax <= 0 is implied by vmin <= 0 combined with vmin >= vmax
(if vmin > 0 and vmin < vmax, then vmax > 0).
Drop the mixed-positives and SymLogNorm tests — they guarded against
hypothetical future regressions rather than the actual bug. Keep just
the zeros-only LogNorm regression test.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 77.64%. Comparing base (59da170) to head (8b177df).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/spatialdata_plot/pl/basic.py 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #672      +/-   ##
==========================================
+ Coverage   77.63%   77.64%   +0.01%     
==========================================
  Files          11       11              
  Lines        3612     3619       +7     
  Branches      850      853       +3     
==========================================
+ Hits         2804     2810       +6     
  Misses        485      485              
- Partials      323      324       +1     
Files with missing lines Coverage Δ
src/spatialdata_plot/pl/basic.py 86.54% <85.71%> (-0.06%) ⬇️

... and 1 file with indirect coverage changes

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

@timtreis timtreis merged commit 69560ef into main May 20, 2026
7 of 8 checks passed
@timtreis timtreis deleted the fix/issue-604-lognorm-zeros branch May 20, 2026 13:27
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.

render_images(norm=LogNorm()) crashes when image contains zeros

2 participants