Skip to content

Missing input validation across Array2D, Grid2D, Mask2D, Imaging, regularization #333

Description

@rhayes777

Title: Missing input validation across Array2D, Grid2D, Mask2D, Imaging, regularization

Repo: Jammy2211/PyAutoArray

Tag: @Jammy2211


A handful of inputs that should be rejected at construction are silently
accepted and propagate downstream as confusing NumPy or numba errors. None
of these is performance-critical, but each one converts a clear
ValueError("…") into a head-scratch debugging session for new users.
Found while auditing 2026.5.21.1.

B6. Array2D accepts pixel_scales=0, negative, NaN

import numpy as np, autoarray as aa

aa.Array2D.no_mask(np.ones((4, 4)), pixel_scales=0.0)            # accepted
# first .derive_grid call: ZeroDivisionError: float division by zero

aa.Array2D.no_mask(np.ones((4, 4)), pixel_scales=-0.1)           # accepted; geometry flipped
aa.Array2D.no_mask(np.ones((4, 4)), pixel_scales=float("nan"))   # accepted; NaN propagates

B7. Mask2D.circular_annular(inner > outer) silently returns zero pixels

aa.Mask2D.circular_annular(
    shape_native=(20, 20), pixel_scales=0.1,
    inner_radius=0.8, outer_radius=0.3,
).pixels_in_mask
# 0

Almost always a typo / swapped arguments.

B8. Grid2D.uniform(shape_native=(0, 0)) builds an empty grid

aa.Grid2D.uniform(shape_native=(0, 0), pixel_scales=0.1).shape_slim   # 0
aa.Grid2D.uniform(shape_native=(0, 5), pixel_scales=0.1).shape_slim   # 0

B5 (from autolens). al.Imaging accepts mismatched data / noise_map shapes

import autolens as al
data  = al.Array2D.no_mask(np.ones((10, 10)), pixel_scales=0.1)
noise = al.Array2D.no_mask(np.ones(( 5,  5)) * 0.1, pixel_scales=0.1)
psf   = al.Convolver.from_gaussian(shape_native=(3, 3), sigma=0.1,
                                   pixel_scales=0.1, normalize=True)
ds    = al.Imaging(data=data, noise_map=noise, psf=psf)
ds.shape_native    # (10, 10) — mismatch swallowed

(Filing here rather than in PyAutoLens since Imaging ultimately lives in
autoarray/dataset/imaging/dataset.py.)

B13. Negative regularization coefficient silently accepted

import autolens as al
al.reg.Constant(coefficient=-1.0)   # accepted; produces same log_evidence as +1.0

The coefficient appears to be squared internally, so positive and negative
give identical answers — but the value is unphysical and the user gets no
heads-up.

Suggested fix shape

A small _validate_* helper used in each constructor:

if pixel_scales <= 0 or not np.isfinite(pixel_scales):
    raise ValueError(f"pixel_scales must be a finite positive number; got {pixel_scales!r}")

…and analogous one-liners for the others. Happy to PR if useful.

Environment

  • autoarray == 2026.5.21.1, Python 3.12.8.

Full repro at work/edge_case_tests/01_core_data_structures.py and
work/edge_case_tests/09_more_edges.py in the audit workspace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions