|
| 1 | +""" |
| 2 | +Overlays drawn by ``plot_array`` must stay registered to the raster whatever the |
| 3 | +``visualize/general.yaml -> general -> imshow_origin`` config is (PyAutoArray#565). |
| 4 | +
|
| 5 | +The checks are origin-agnostic: they read the drawn overlay coordinates back off |
| 6 | +the axes and map them through the rendered image's own extent and origin to the |
| 7 | +pixel they land on, which must be a bright pixel of an off-centre block. |
| 8 | +""" |
| 9 | + |
| 10 | +import matplotlib |
| 11 | + |
| 12 | +matplotlib.use("Agg") |
| 13 | + |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | +import pytest |
| 17 | + |
| 18 | +from autonerves import conf |
| 19 | + |
| 20 | +import autoarray as aa |
| 21 | +import autoarray.plot as aplt |
| 22 | + |
| 23 | + |
| 24 | +def _bright_block_array(): |
| 25 | + values = np.zeros((50, 50)) |
| 26 | + values[10:14, 23:27] = 1.0 |
| 27 | + return aa.Array2D.no_mask(values=values, pixel_scales=0.1) |
| 28 | + |
| 29 | + |
| 30 | +def _native_grid(array): |
| 31 | + return np.asarray(array.mask.derive_grid.all_false.native) |
| 32 | + |
| 33 | + |
| 34 | +def _pixel_value_under(ax, draw_x, draw_y): |
| 35 | + image = ax.images[0] |
| 36 | + data = np.asarray(image.get_array()) |
| 37 | + xmin, xmax, ymin, ymax = image.get_extent() |
| 38 | + ny, nx = data.shape[:2] |
| 39 | + |
| 40 | + col = int((draw_x - xmin) / (xmax - xmin) * nx) |
| 41 | + frac = (draw_y - ymin) / (ymax - ymin) |
| 42 | + row = int((1.0 - frac) * ny) if image.origin == "upper" else int(frac * ny) |
| 43 | + |
| 44 | + return data[row, col] |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture(name="imshow_origin") |
| 48 | +def make_imshow_origin(request): |
| 49 | + general = conf.instance["visualize"]["general"]["general"] |
| 50 | + original = general["imshow_origin"] |
| 51 | + try: |
| 52 | + general["imshow_origin"] = request.param |
| 53 | + yield request.param |
| 54 | + finally: |
| 55 | + general["imshow_origin"] = original |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("imshow_origin", ["upper", "lower"], indirect=True) |
| 59 | +def test__plot_array__positions_marker_lands_on_block(imshow_origin): |
| 60 | + array = _bright_block_array() |
| 61 | + grid = _native_grid(array) |
| 62 | + y, x = float(grid[12, 25, 0]), float(grid[12, 25, 1]) |
| 63 | + |
| 64 | + fig, ax = plt.subplots() |
| 65 | + try: |
| 66 | + aplt.plot_array(array=array, positions=aa.Grid2DIrregular([(y, x)]), ax=ax) |
| 67 | + |
| 68 | + assert ax.images[0].origin == imshow_origin |
| 69 | + |
| 70 | + # The positions scatter is the only s=20, zorder=5 collection (the auto |
| 71 | + # mask-edge scatter is s=1). |
| 72 | + positions = [ |
| 73 | + c |
| 74 | + for c in ax.collections |
| 75 | + if c.get_zorder() == 5 and np.allclose(c.get_sizes(), 20) |
| 76 | + ] |
| 77 | + assert len(positions) == 1 |
| 78 | + draw_x, draw_y = positions[0].get_offsets()[0] |
| 79 | + |
| 80 | + assert _pixel_value_under(ax, float(draw_x), float(draw_y)) == 1.0 |
| 81 | + finally: |
| 82 | + plt.close(fig) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("imshow_origin", ["upper", "lower"], indirect=True) |
| 86 | +def test__plot_array__lines_vertices_land_on_block(imshow_origin): |
| 87 | + array = _bright_block_array() |
| 88 | + grid = _native_grid(array) |
| 89 | + |
| 90 | + # An asymmetric polyline whose every vertex sits on a bright pixel. |
| 91 | + pixels = [(10, 23), (13, 24), (11, 26)] |
| 92 | + line = np.array([[grid[r, c, 0], grid[r, c, 1]] for r, c in pixels]) |
| 93 | + |
| 94 | + fig, ax = plt.subplots() |
| 95 | + try: |
| 96 | + aplt.plot_array(array=array, lines=[line], ax=ax) |
| 97 | + |
| 98 | + assert ax.images[0].origin == imshow_origin |
| 99 | + |
| 100 | + xy = ax.lines[-1].get_xydata() |
| 101 | + assert xy.shape == (3, 2) |
| 102 | + |
| 103 | + for draw_x, draw_y in xy: |
| 104 | + assert _pixel_value_under(ax, float(draw_x), float(draw_y)) == 1.0 |
| 105 | + finally: |
| 106 | + plt.close(fig) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize("imshow_origin", ["upper", "lower"], indirect=True) |
| 110 | +def test__plot_inversion_reconstruction__uniform_mesh_grid_lands_on_pixel( |
| 111 | + imshow_origin, rectangular_mapper_7x7_3x3 |
| 112 | +): |
| 113 | + mapper = rectangular_mapper_7x7_3x3 |
| 114 | + |
| 115 | + # One off-centre bright source pixel (row 0, column 1 of the 3x3 mesh). |
| 116 | + pixel_values = np.zeros(9) |
| 117 | + pixel_values[1] = 1.0 |
| 118 | + |
| 119 | + mesh = aa.Array2D.no_mask( |
| 120 | + values=np.zeros(mapper.mesh_geometry.shape), |
| 121 | + pixel_scales=mapper.mesh_geometry.pixel_scales, |
| 122 | + origin=mapper.mesh_geometry.origin, |
| 123 | + ) |
| 124 | + y, x = _native_grid(mesh)[0, 1] |
| 125 | + |
| 126 | + fig, ax = plt.subplots() |
| 127 | + try: |
| 128 | + aplt.plot_inversion_reconstruction( |
| 129 | + pixel_values=pixel_values, |
| 130 | + mapper=mapper, |
| 131 | + grid=np.array([[y, x]]), |
| 132 | + zoom_to_brightest=False, |
| 133 | + ax=ax, |
| 134 | + ) |
| 135 | + |
| 136 | + assert ax.images[0].origin == imshow_origin |
| 137 | + |
| 138 | + draw_x, draw_y = ax.collections[-1].get_offsets()[0] |
| 139 | + |
| 140 | + assert _pixel_value_under(ax, float(draw_x), float(draw_y)) == 1.0 |
| 141 | + finally: |
| 142 | + plt.close(fig) |
0 commit comments