Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions autoarray/dataset/imaging/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions autoarray/dataset/interferometer/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions autoarray/dataset/lazy_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 31 additions & 8 deletions test_autoarray/inversion/inversion/test_lazy_operator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -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,
Expand All @@ -187,15 +194,15 @@ 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
)


def test__imaging__policy_on_via_config_and_pyauto_setup(
masked_imaging_7x7, monkeypatch
masked_imaging_7x7, monkeypatch, caplog
):
pytest.importorskip("numba")

Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading