From 43fc2c2f39ebeedde5d9e5f70d0ec84bdd49a879 Mon Sep 17 00:00:00 2001 From: Neil Vaytet Date: Thu, 2 Jul 2026 11:22:56 +0200 Subject: [PATCH 1/2] convert numpy uint dtypes to int64 before storing into a Scipp variable --- src/plopp/plotting/common.py | 2 ++ tests/plotting/plot_1d_test.py | 5 +++++ tests/plotting/plot_2d_test.py | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/src/plopp/plotting/common.py b/src/plopp/plotting/common.py index fb7eb4317..0abd64e64 100644 --- a/src/plopp/plotting/common.py +++ b/src/plopp/plotting/common.py @@ -56,6 +56,8 @@ def _maybe_to_variable(obj: Plottable | list) -> Plottable: out = np.array(out) if isinstance(out, np.ndarray): dims = [f"axis-{i}" for i in range(len(out.shape))] + if np.issubdtype(out.dtype, np.unsignedinteger): + out = out.astype('int64') out = sc.Variable(dims=dims, values=out) return out diff --git a/tests/plotting/plot_1d_test.py b/tests/plotting/plot_1d_test.py index a3ccf4100..464a53c68 100644 --- a/tests/plotting/plot_1d_test.py +++ b/tests/plotting/plot_1d_test.py @@ -34,6 +34,11 @@ def test_plot_variable(): pp.plot(sc.arange('x', 50.0)) +@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64]) +def test_plot_ndarray_uint(dtype): + pp.plot(np.arange(50, dtype=dtype)) + + def test_plot_data_array(): pp.plot(data_array(ndim=1)) diff --git a/tests/plotting/plot_2d_test.py b/tests/plotting/plot_2d_test.py index 72ebee44b..9384dd8b9 100644 --- a/tests/plotting/plot_2d_test.py +++ b/tests/plotting/plot_2d_test.py @@ -22,6 +22,11 @@ def test_plot_ndarray_int(): pp.plot(np.arange(50).reshape(5, 10)) +@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64]) +def test_plot_ndarray_uint(dtype): + pp.plot(np.arange(50, dtype=dtype).reshape(5, 10)) + + def test_plot_variable(): pp.plot(variable(ndim=2)) From 88faf662b25e4ba025c8e3823bf3a8a00c6847e7 Mon Sep 17 00:00:00 2001 From: Neil Vaytet Date: Thu, 2 Jul 2026 15:06:20 +0200 Subject: [PATCH 2/2] add comment --- src/plopp/plotting/common.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/plopp/plotting/common.py b/src/plopp/plotting/common.py index 0abd64e64..aab49e7d6 100644 --- a/src/plopp/plotting/common.py +++ b/src/plopp/plotting/common.py @@ -57,6 +57,9 @@ def _maybe_to_variable(obj: Plottable | list) -> Plottable: if isinstance(out, np.ndarray): dims = [f"axis-{i}" for i in range(len(out.shape))] if np.issubdtype(out.dtype, np.unsignedinteger): + # This conversion is not completely without loss in the case of very large + # unsigned integers, but it is probably fine in most cases, and better than + # not plotting the data at all. out = out.astype('int64') out = sc.Variable(dims=dims, values=out) return out