Skip to content

Fix px.imshow integer dtype detection for int32/uint32 on Windows - #5917

Open
AsadullahMaliyawala wants to merge 2 commits into
plotly:mainfrom
AsadullahMaliyawala:imshow-int32-windows-fix
Open

AsadullahMaliyawala wants to merge 2 commits into
plotly:mainfrom
AsadullahMaliyawala:imshow-int32-windows-fix

Conversation

@AsadullahMaliyawala

Copy link
Copy Markdown

Link to issue

No existing issue; found by running the test suite on Windows (details below).

Description of change

On Windows with NumPy 2, int32/uint32 arrays have np.long/np.ulong as their scalar type. Those are separate types from np.intc/np.uintc, and np.int_ is int64 there, so neither is in _integer_types in plotly/express/imshow_utils.py. As a result px.imshow treats int32/uint32 images as float data and contrast_rescaling="infer" picks a value-based range instead of the dtype range:

import numpy as np, plotly.express as px
img = np.arange(100, dtype=np.int32).reshape(10, 10)
px.imshow(img, contrast_rescaling="infer").layout.coloraxis.cmax
# Linux: 2147483647
# Windows, NumPy 2.5.5 before this PR: 255

The same mismatch applies to rescale_intensity(..., in_range="dtype"), which looks types up in the same table.

The fix builds _integer_types from the dtype characters "bBhHiIlLqQ", so every integer scalar type NumPy can produce on the current platform is covered (duplicates are dropped, so Linux/macOS get the same set as before).

Testing strategy

  • The existing test_imshow_source_dtype_zmax[infer-int32] fails on Windows before this change and passes after. CI runs on Linux only, which is why it was not caught.
  • Added test_imshow_infer_zmax_all_integer_dtypes, which checks cmax equals the dtype maximum for every integer dtype character (uint8 is left out because infer keeps zmax=None for it). On Windows, the l/L cases fail before the fix and pass after; on Linux all cases pass both ways.
  • pytest tests/test_optional/test_px/test_imshow.py: 99 passed on Windows 11, Python 3.12, NumPy 2.5.5. ruff format --check and ruff check are clean.

Additional information (optional)

I used Claude Code (Claude Opus 5.5) to help find and fix this. I reproduced the bug and checked the change myself.

Guidelines

AsadullahMaliyawala and others added 2 commits November 30, 2026 22:29
With NumPy 2 on Windows, int32 and uint32 arrays have np.long/np.ulong as
their scalar type, which is not one of the named aliases listed in
_integer_types (np.intc/np.uintc are distinct types there and np.int_ is
int64). px.imshow then treated such images as float data, so
contrast_rescaling="infer" picked a value-based zmax (e.g. 255) instead of
the dtype maximum. Build the type table from the dtype characters so every
platform integer type is covered.

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant