From 225dceb0f6aa13f2be15144807435c649bb1c30a Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 20 Nov 2026 08:39:58 +0000 Subject: [PATCH 1/3] fix: plot_grid drops highlighted indexes outside the grid Tutorials pick highlight indexes for a full-size grid; under PYAUTO_SMALL_DATASETS the grid is capped and plot_grid raised IndexError. Out-of-range indexes are now dropped (logged once), and a group with none left is skipped. Closes #705. Co-Authored-By: Claude Opus 5.5 --- autoarray/plot/grid.py | 24 ++++++++++- test_autoarray/plot/test_grid.py | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 test_autoarray/plot/test_grid.py diff --git a/autoarray/plot/grid.py b/autoarray/plot/grid.py index 245d06b55..4dd04ceb1 100644 --- a/autoarray/plot/grid.py +++ b/autoarray/plot/grid.py @@ -4,6 +4,7 @@ This replaces the ``MatPlot2D.plot_grid`` / ``MatWrap`` system. """ +import logging from typing import Iterable, List, Optional, Tuple import numpy as np @@ -18,6 +19,8 @@ numpy_lines, ) +logger = logging.getLogger(__name__) + def plot_grid( grid, @@ -62,6 +65,10 @@ def plot_grid( color_array 1D array of scalar values for colouring each point; triggers a colorbar. + indexes + Groups of grid indexes to highlight, one colour per group. Indexes + outside the grid are dropped (logged), e.g. when indexes chosen for + a full-size grid meet one capped by ``PYAUTO_SMALL_DATASETS``. title Figure title. xlabel, ylabel @@ -170,14 +177,27 @@ def plot_grid( if indexes is not None: colors = ["r", "g", "b", "m", "c", "y"] + total_points = grid.shape[0] + dropped = 0 for i, idx_list in enumerate(indexes): + idx_array = np.asarray(idx_list, dtype=int).ravel() + in_range = (idx_array >= -total_points) & (idx_array < total_points) + dropped += int(np.count_nonzero(~in_range)) + idx_array = idx_array[in_range] + if idx_array.size == 0: + continue ax.scatter( - grid[idx_list, 1], - grid[idx_list, 0], + grid[idx_array, 1], + grid[idx_array, 0], s=10, c=colors[i % len(colors)], zorder=5, ) + if dropped: + logger.info( + f"plot_grid: dropped {dropped} highlighted index(es) outside the " + f"{total_points}-point grid." + ) if force_symmetric_extent and extent is not None: x_abs = max(abs(extent[0]), abs(extent[1])) diff --git a/test_autoarray/plot/test_grid.py b/test_autoarray/plot/test_grid.py new file mode 100644 index 000000000..c68612d0f --- /dev/null +++ b/test_autoarray/plot/test_grid.py @@ -0,0 +1,70 @@ +""" +``plot_grid(indexes=...)`` highlights groups of grid points. Indexes outside the +grid are dropped rather than raising ``IndexError`` (PyAutoArray#705): tutorials +pick indexes for a full-size grid, which ``PYAUTO_SMALL_DATASETS`` can cap. +""" + +import logging + +import matplotlib + +matplotlib.use("Agg") + +import matplotlib.pyplot as plt +import numpy as np + +import autoarray as aa +import autoarray.plot as aplt + + +def _grid(): + return aa.Grid2D.uniform(shape_native=(4, 4), pixel_scales=1.0) + + +def _highlight_offsets(ax): + # The first collection is the base scatter of every grid point; the rest are + # one scatter per highlighted index group. + return [np.asarray(c.get_offsets()) for c in ax.collections[1:]] + + +def test__plot_grid__indexes_in_range__each_group_highlighted(): + grid = _grid() + fig, ax = plt.subplots() + + aplt.plot_grid(grid=grid, indexes=[[0, 1], [5]], ax=ax) + + offsets = _highlight_offsets(ax) + assert len(offsets) == 2 + values = np.asarray(grid.array) + assert np.allclose(offsets[0], values[[0, 1]][:, ::-1]) + assert np.allclose(offsets[1], values[[5]][:, ::-1]) + plt.close(fig) + + +def test__plot_grid__indexes_partly_out_of_range__out_of_range_dropped(caplog): + grid = _grid() + fig, ax = plt.subplots() + + with caplog.at_level(logging.INFO, logger="autoarray.plot.grid"): + aplt.plot_grid(grid=grid, indexes=[[2, 5118], [7099]], ax=ax) + + offsets = _highlight_offsets(ax) + assert len(offsets) == 1 + assert np.allclose(offsets[0], np.asarray(grid.array)[[2]][:, ::-1]) + assert "dropped 2 highlighted index(es) outside the 16-point grid" in caplog.text + plt.close(fig) + + +def test__plot_grid__indexes_all_out_of_range__no_highlight_and_no_error(): + grid = _grid() + fig, ax = plt.subplots() + + aplt.plot_grid( + grid=grid, + indexes=[[5118, 5119], [3078]], + ax=ax, + extent=(-5.5, 5.5, -5.5, 5.5), + ) + + assert _highlight_offsets(ax) == [] + plt.close(fig) From 2e60db009a5e843b2b9c66b92766ae473c0e82af Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 20 Nov 2026 08:40:16 +0000 Subject: [PATCH 2/3] refactor: keep plot_grid logger module-private Co-Authored-By: Claude Opus 5.5 --- autoarray/plot/grid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoarray/plot/grid.py b/autoarray/plot/grid.py index 4dd04ceb1..9075186a8 100644 --- a/autoarray/plot/grid.py +++ b/autoarray/plot/grid.py @@ -19,7 +19,7 @@ numpy_lines, ) -logger = logging.getLogger(__name__) +_logger = logging.getLogger(__name__) def plot_grid( @@ -194,7 +194,7 @@ def plot_grid( zorder=5, ) if dropped: - logger.info( + _logger.info( f"plot_grid: dropped {dropped} highlighted index(es) outside the " f"{total_points}-point grid." ) From f575552d47b8ffd06908e66b25f0036c72e38f5f Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 20 Nov 2026 08:41:57 +0000 Subject: [PATCH 3/3] fix: plot_grid keeps boolean-mask index groups as masks Co-Authored-By: Claude Opus 5.5 --- autoarray/plot/grid.py | 4 +++- test_autoarray/plot/test_grid.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/autoarray/plot/grid.py b/autoarray/plot/grid.py index 9075186a8..19010e2e6 100644 --- a/autoarray/plot/grid.py +++ b/autoarray/plot/grid.py @@ -180,7 +180,9 @@ def plot_grid( total_points = grid.shape[0] dropped = 0 for i, idx_list in enumerate(indexes): - idx_array = np.asarray(idx_list, dtype=int).ravel() + idx_array = np.asarray(idx_list).ravel() + if idx_array.dtype == bool: + idx_array = np.flatnonzero(idx_array) in_range = (idx_array >= -total_points) & (idx_array < total_points) dropped += int(np.count_nonzero(~in_range)) idx_array = idx_array[in_range] diff --git a/test_autoarray/plot/test_grid.py b/test_autoarray/plot/test_grid.py index c68612d0f..6d7f9a22f 100644 --- a/test_autoarray/plot/test_grid.py +++ b/test_autoarray/plot/test_grid.py @@ -68,3 +68,17 @@ def test__plot_grid__indexes_all_out_of_range__no_highlight_and_no_error(): assert _highlight_offsets(ax) == [] plt.close(fig) + + +def test__plot_grid__boolean_mask_group__highlights_masked_points(): + grid = _grid() + fig, ax = plt.subplots() + mask = np.zeros(16, dtype=bool) + mask[[7, 9]] = True + + aplt.plot_grid(grid=grid, indexes=[mask], ax=ax) + + offsets = _highlight_offsets(ax) + assert len(offsets) == 1 + assert np.allclose(offsets[0], np.asarray(grid.array)[[7, 9]][:, ::-1]) + plt.close(fig)