|
| 1 | +""" |
| 2 | +Regression tests for @rhayes777's audit finding in PyAutoArray#332. |
| 3 | +
|
| 4 | +Rectangular meshes do not support split regularization -- their interpolators provide no |
| 5 | +split-cross mappings. Before the fix this surfaced two different ways, both deep inside the |
| 6 | +inversion and neither naming the real cause: |
| 7 | +
|
| 8 | +- ``RectangularUniform`` -> ``AttributeError: 'InterpolatorRectangularUniform' object has no |
| 9 | + attribute '_mappings_sizes_weights_split'`` |
| 10 | +- ``RectangularAdaptDensity`` / ``RectangularAdaptImage`` |
| 11 | + -> ``IndexError: index 4 is out of bounds for axis 0 with size 4`` |
| 12 | + (``InterpolatorRectangular`` returned the plain 4-corner mappings |
| 13 | + from a pass-through that claimed split "reuses the same mappings") |
| 14 | +
|
| 15 | +``Pixelization`` now rejects the combination at construction. These tests assert the *clear |
| 16 | +failure*, not a successful fit -- the capability is deliberately absent. |
| 17 | +""" |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import autoarray as aa |
| 22 | +from autoarray import exc |
| 23 | + |
| 24 | + |
| 25 | +RECTANGULAR_MESHES = [ |
| 26 | + aa.mesh.RectangularUniform, |
| 27 | + aa.mesh.RectangularAdaptDensity, |
| 28 | + aa.mesh.RectangularAdaptImage, |
| 29 | +] |
| 30 | + |
| 31 | +SPLIT_REGULARIZATIONS = [ |
| 32 | + aa.reg.ConstantSplit, |
| 33 | + aa.reg.AdaptSplit, |
| 34 | + aa.reg.AdaptSplitZeroth, |
| 35 | +] |
| 36 | + |
| 37 | +ADAPTIVE_MESHES = [ |
| 38 | + aa.mesh.Delaunay, |
| 39 | + aa.mesh.KNNBarycentric, |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("mesh_cls", RECTANGULAR_MESHES) |
| 44 | +@pytest.mark.parametrize("regularization_cls", SPLIT_REGULARIZATIONS) |
| 45 | +def test__rectangular_mesh_with_split_regularization__raises(mesh_cls, regularization_cls): |
| 46 | + """All 9 rectangular-mesh x split-regularization combinations are rejected.""" |
| 47 | + |
| 48 | + with pytest.raises(exc.PixelizationException) as error: |
| 49 | + aa.Pixelization( |
| 50 | + mesh=mesh_cls(shape=(15, 15)), |
| 51 | + regularization=regularization_cls(), |
| 52 | + ) |
| 53 | + |
| 54 | + message = str(error.value) |
| 55 | + |
| 56 | + # the message must name both sides, so the user can act on it without a traceback |
| 57 | + assert mesh_cls.__name__ in message |
| 58 | + assert regularization_cls.__name__ in message |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize("mesh_cls", RECTANGULAR_MESHES) |
| 62 | +def test__rectangular_mesh_with_non_split_regularization__is_allowed(mesh_cls): |
| 63 | + """The guard is specific to split schemes; `Constant` on the same meshes still builds.""" |
| 64 | + |
| 65 | + pixelization = aa.Pixelization( |
| 66 | + mesh=mesh_cls(shape=(15, 15)), |
| 67 | + regularization=aa.reg.Constant(coefficient=1.0), |
| 68 | + ) |
| 69 | + |
| 70 | + assert isinstance(pixelization.mesh, mesh_cls) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("mesh_cls", ADAPTIVE_MESHES) |
| 74 | +@pytest.mark.parametrize("regularization_cls", SPLIT_REGULARIZATIONS) |
| 75 | +def test__adaptive_mesh_with_split_regularization__is_allowed(mesh_cls, regularization_cls): |
| 76 | + """Split regularization remains supported on the meshes that implement it.""" |
| 77 | + |
| 78 | + pixelization = aa.Pixelization( |
| 79 | + mesh=mesh_cls(pixels=100), |
| 80 | + regularization=regularization_cls(), |
| 81 | + ) |
| 82 | + |
| 83 | + assert isinstance(pixelization.mesh, mesh_cls) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize("mesh_cls", RECTANGULAR_MESHES) |
| 87 | +def test__rectangular_mesh_without_regularization__is_allowed(mesh_cls): |
| 88 | + """`regularization` is optional; a `None` value must not trip the guard.""" |
| 89 | + |
| 90 | + pixelization = aa.Pixelization(mesh=mesh_cls(shape=(15, 15))) |
| 91 | + |
| 92 | + assert pixelization.regularization is None |
| 93 | + |
| 94 | + |
| 95 | +def test__capability_flags(): |
| 96 | + """The flags the guard reads, asserted directly so a future mesh can't silently regress.""" |
| 97 | + |
| 98 | + assert aa.mesh.RectangularUniform(shape=(15, 15)).supports_split_regularization is False |
| 99 | + assert aa.mesh.RectangularAdaptDensity(shape=(15, 15)).supports_split_regularization is False |
| 100 | + assert aa.mesh.RectangularAdaptImage(shape=(15, 15)).supports_split_regularization is False |
| 101 | + assert aa.mesh.Delaunay(pixels=100).supports_split_regularization is True |
| 102 | + assert aa.mesh.KNNBarycentric(pixels=100).supports_split_regularization is True |
| 103 | + |
| 104 | + assert aa.reg.ConstantSplit().is_split_regularization is True |
| 105 | + assert aa.reg.AdaptSplit().is_split_regularization is True |
| 106 | + assert aa.reg.AdaptSplitZeroth().is_split_regularization is True |
| 107 | + assert aa.reg.Constant().is_split_regularization is False |
| 108 | + assert aa.reg.Adapt().is_split_regularization is False |
| 109 | + |
| 110 | + |
| 111 | +def test__interpolator_rectangular_has_no_split_mappings(): |
| 112 | + """ |
| 113 | + The pass-through that produced the `IndexError` is gone and must stay gone -- restoring it |
| 114 | + would reintroduce a silent-looking API that fails one frame later. |
| 115 | + """ |
| 116 | + |
| 117 | + from autoarray.inversion.mesh.interpolator.rectangular import InterpolatorRectangular |
| 118 | + from autoarray.inversion.mesh.interpolator.rectangular_uniform import ( |
| 119 | + InterpolatorRectangularUniform, |
| 120 | + ) |
| 121 | + from autoarray.inversion.mesh.interpolator.delaunay import InterpolatorDelaunay |
| 122 | + |
| 123 | + assert not hasattr(InterpolatorRectangular, "_mappings_sizes_weights_split") |
| 124 | + assert not hasattr(InterpolatorRectangularUniform, "_mappings_sizes_weights_split") |
| 125 | + assert hasattr(InterpolatorDelaunay, "_mappings_sizes_weights_split") |
| 126 | + |
| 127 | + |
| 128 | +def test__pixelization_exception_is_not_a_fit_exception(): |
| 129 | + """ |
| 130 | + An unsupported combination is a configuration error, not a bad model sample. |
| 131 | +
|
| 132 | + `autogalaxy.exc.PixelizationException` subclasses `af.exc.FitException`, which |
| 133 | + `fitness.py` converts into a resample-reject. If the exception raised here were a |
| 134 | + `FitException`, a search would silently reject every sample instead of reporting the |
| 135 | + misconfiguration. `autoarray`'s own `PixelizationException` must stay a plain `Exception`. |
| 136 | + """ |
| 137 | + |
| 138 | + assert issubclass(exc.PixelizationException, Exception) |
| 139 | + |
| 140 | + af = pytest.importorskip("autofit") |
| 141 | + assert not issubclass(exc.PixelizationException, af.exc.FitException) |
0 commit comments