From 1b95362421324605d0454d3a5adf738d479fe67e Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 25 Nov 2026 10:55:57 +0000 Subject: [PATCH] feat: concise notice when the lazy sparse operator is built outside modeling Part of the concise automatic compute-setup banner (PyAutoLabs/PyAutoGalaxy#754): one header line stating the automatic CPU/GPU choice, aligned Setup / Parallel / Estimate lines printed together, and notices when the setup acts outside modeling. Shares branch feature/auto-setup-banner across PyAutoNerves, PyAutoArray, PyAutoFit, PyAutoGalaxy and PyAutoLens. Co-Authored-By: Claude Opus 5.5 --- autoarray/dataset/imaging/dataset.py | 5 ++- autoarray/dataset/interferometer/dataset.py | 7 ++-- autoarray/dataset/lazy_operator.py | 16 ++++++++ .../inversion/inversion/test_lazy_operator.py | 39 +++++++++++++++---- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index c34627850..e6895d067 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -894,8 +894,9 @@ def sparse_operator_for(self, xp=np, needs_operator: bool = True, settings=None) self._lazy_sparse_operator = self.apply_sparse_operator_cpu().sparse_operator logger.info( - f"INVERSION - built numba sparse operator ({self.mask.pixels_in_mask} pixels) " - f"in {time.perf_counter() - start:.2f} s" + f"Automatic compute setup: built the numba sparse operator for this dataset " + f"({self.mask.pixels_in_mask} image pixels, {time.perf_counter() - start:.2f} s; " + f"cached; {lazy_operator.lazy_sparse_operator_trigger(settings)})" ) return self._lazy_sparse_operator diff --git a/autoarray/dataset/interferometer/dataset.py b/autoarray/dataset/interferometer/dataset.py index 1c38c5087..807645906 100644 --- a/autoarray/dataset/interferometer/dataset.py +++ b/autoarray/dataset/interferometer/dataset.py @@ -1213,9 +1213,10 @@ def sparse_operator_for(self, xp=np, needs_operator: bool = True, settings=None) self._lazy_sparse_operator = dataset.sparse_operator logger.info( - f"INVERSION - built NUFFT w-tilde sparse operator ({n_vis} visibilities, " - f"{self.real_space_mask.pixels_in_mask} pixels" - f"{', chunked' if chunked else ''}) in {time.perf_counter() - start:.2f} s" + f"Automatic compute setup: built the w-tilde sparse operator for this dataset " + f"({n_vis} visibilities{', chunked' if chunked else ''}, " + f"{time.perf_counter() - start:.2f} s; cached; " + f"{lazy_operator.lazy_sparse_operator_trigger(settings)})" ) return self._lazy_sparse_operator diff --git a/autoarray/dataset/lazy_operator.py b/autoarray/dataset/lazy_operator.py index 6564ab9ec..9125a5f8f 100644 --- a/autoarray/dataset/lazy_operator.py +++ b/autoarray/dataset/lazy_operator.py @@ -68,6 +68,22 @@ def lazy_sparse_operator_enabled(settings=None) -> bool: return _pyauto_setup_is_auto() +def lazy_sparse_operator_trigger(settings=None) -> str: + """ + What turned the lazy sparse operator policy on, as the build notice says it: + ``"lazy_sparse_operator: auto"`` (an explicit `Settings` or `general.yaml`), or + ``"PYAUTO_SETUP=auto"`` when only the environment variable did. + """ + from autoarray.settings import Settings + + settings = settings if settings is not None else Settings() + + if settings._lazy_sparse_operator is not None or settings.lazy_sparse_operator == "auto": + return "lazy_sparse_operator: auto" + + return "PYAUTO_SETUP=auto" if _pyauto_setup_is_auto() else "lazy_sparse_operator: auto" + + def _pyauto_setup_is_auto() -> bool: """ Whether `PYAUTO_SETUP` is explicitly set to ``auto``. An invalid value is left for diff --git a/test_autoarray/inversion/inversion/test_lazy_operator.py b/test_autoarray/inversion/inversion/test_lazy_operator.py index cf000f45a..a57d1bba5 100644 --- a/test_autoarray/inversion/inversion/test_lazy_operator.py +++ b/test_autoarray/inversion/inversion/test_lazy_operator.py @@ -1,4 +1,5 @@ import logging +import re import numpy as np import pytest @@ -163,10 +164,16 @@ def test__imaging__policy_on__built_once_cached_reused_and_logged( ) build_lines = [ - r.message for r in caplog.records if "built numba sparse operator" in r.message + r.message + for r in caplog.records + if r.message.startswith("Automatic compute setup: built") ] assert len(build_lines) == 1 - assert "(9 pixels)" in build_lines[0] + assert re.fullmatch( + r"Automatic compute setup: built the numba sparse operator for this dataset " + r"\(9 image pixels, \d+\.\d\d s; cached; lazy_sparse_operator: auto\)", + build_lines[0], + ), build_lines[0] from autoarray.inversion.inversion.imaging_numba.inversion_imaging_numba_util import ( SparseLinAlgImagingNumba, @@ -187,7 +194,7 @@ def test__imaging__policy_on__built_once_cached_reused_and_logged( is operator ) - assert not any("built numba" in r.message for r in caplog.records) + assert not any("Automatic compute setup" in r.message for r in caplog.records) _assert_imaging_operator_equal( operator, masked_imaging_7x7.apply_sparse_operator_cpu().sparse_operator @@ -195,7 +202,7 @@ def test__imaging__policy_on__built_once_cached_reused_and_logged( def test__imaging__policy_on_via_config_and_pyauto_setup( - masked_imaging_7x7, monkeypatch + masked_imaging_7x7, monkeypatch, caplog ): pytest.importorskip("numba") @@ -216,7 +223,15 @@ def test__imaging__policy_on_via_config_and_pyauto_setup( assert dataset.sparse_operator_for(xp=np, needs_operator=True) is None monkeypatch.setenv("PYAUTO_SETUP", "auto") - assert dataset.sparse_operator_for(xp=np, needs_operator=True) is not None + with caplog.at_level(logging.INFO, logger=IMAGING_LOGGER): + assert dataset.sparse_operator_for(xp=np, needs_operator=True) is not None + + # The notice names what turned the lazy build on. + assert any( + r.message.startswith("Automatic compute setup: built the numba sparse operator") + and r.message.endswith("; cached; PYAUTO_SETUP=auto)") + for r in caplog.records + ) def test__imaging__gates__return_none(masked_imaging_7x7, monkeypatch): @@ -349,10 +364,14 @@ def test__interferometer__policy_on__built_once_cached_matches_apply_sparse_oper build_lines = [ r.message for r in caplog.records - if "built NUFFT w-tilde sparse operator" in r.message + if r.message.startswith("Automatic compute setup: built") ] assert len(build_lines) == 1 - assert "(7 visibilities, 9 pixels)" in build_lines[0] + assert re.fullmatch( + r"Automatic compute setup: built the w-tilde sparse operator for this dataset " + r"\(7 visibilities, \d+\.\d\d s; cached; lazy_sparse_operator: auto\)", + build_lines[0], + ), build_lines[0] assert isinstance(operator, aa.InterferometerSparseOperator) assert dataset.sparse_operator is None @@ -389,7 +408,11 @@ def spy(self, chunks, **kwargs): operator = dataset.sparse_operator_for(xp=np, needs_operator=True, settings=ON) assert chunk_lengths == [3, 3, 1] - assert any(", chunked)" in r.message for r in caplog.records) + assert any( + "(7 visibilities, chunked, " in r.message + and r.message.startswith("Automatic compute setup: built the w-tilde") + for r in caplog.records + ) _assert_interferometer_operator_close( operator, dataset.apply_sparse_operator().sparse_operator