|
1 | 1 | from pathlib import Path |
| 2 | +import importlib.util |
| 3 | + |
| 4 | +import pytest |
2 | 5 |
|
3 | 6 | import autofit as af |
4 | 7 | import autolens as al |
|
8 | 11 | directory = Path(__file__).resolve().parent |
9 | 12 |
|
10 | 13 |
|
| 14 | +def _jax_installed() -> bool: |
| 15 | + return importlib.util.find_spec("jax") is not None |
| 16 | + |
| 17 | + |
| 18 | +def test__pyauto_disable_jax_env_downgrades_use_jax__point( |
| 19 | + monkeypatch, point_dataset |
| 20 | +): |
| 21 | + # THE BUG TEST. Before the one-reader fix `AnalysisPoint` had no local |
| 22 | + # env read and `AnalysisLens.__init__` overwrote the base-resolved |
| 23 | + # `self._use_jax` with the raw `use_jax` parameter, so the disable-jax |
| 24 | + # env var was silently a no-op (base set False, AnalysisLens set True). |
| 25 | + # It must now downgrade to False. |
| 26 | + monkeypatch.setenv("PYAUTO_DISABLE_JAX", "1") |
| 27 | + |
| 28 | + solver = al.m.MockPointSolver(model_positions=point_dataset.positions) |
| 29 | + |
| 30 | + analysis = al.AnalysisPoint( |
| 31 | + dataset=point_dataset, solver=solver, use_jax=True |
| 32 | + ) |
| 33 | + |
| 34 | + assert analysis._use_jax is False |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.skipif(not _jax_installed(), reason="jax not installed") |
| 38 | +def test__use_jax_true_env_unset__not_downgraded__point( |
| 39 | + monkeypatch, point_dataset |
| 40 | +): |
| 41 | + # No over-downgrade: with the env var unset and jax installed, |
| 42 | + # `use_jax=True` must survive as `self._use_jax is True`. |
| 43 | + monkeypatch.delenv("PYAUTO_DISABLE_JAX", raising=False) |
| 44 | + |
| 45 | + solver = al.m.MockPointSolver(model_positions=point_dataset.positions) |
| 46 | + |
| 47 | + analysis = al.AnalysisPoint( |
| 48 | + dataset=point_dataset, solver=solver, use_jax=True |
| 49 | + ) |
| 50 | + |
| 51 | + assert analysis._use_jax is True |
| 52 | + |
| 53 | + |
11 | 54 | def _test__make_result__result_imaging_is_returned(point_dataset): |
12 | 55 | model = af.Collection( |
13 | 56 | galaxies=af.Collection( |
|
0 commit comments