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: 5 additions & 0 deletions autofit/non_linear/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ def compute_latent_samples(self, samples: Samples, batch_size: Optional[int] = N
reads which latents to compute (and how) from ``self.Latent`` (see
:class:`Latent`). Kept as a method for backwards compatibility — it is
the entry point called by ``SearchUpdater._compute_latent_samples``.

Raises ``exc.SamplesException`` if ``samples`` is ``None``, which is
what ``result.samples`` yields for a resumed fit whose ``samples.csv``
was never written (samples output disabled). See
:func:`~autofit.non_linear.analysis.latent.latent_samples_from`.
"""
return latent_samples_from(self, samples, batch_size=batch_size)

Expand Down
28 changes: 28 additions & 0 deletions autofit/non_linear/analysis/latent.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import numpy as np

from autofit import exc
from autofit.non_linear.samples.sample import Sample
from autofit.non_linear.samples.samples import Samples
from autofit.non_linear.samples.util import simple_model_for_kwargs
Expand Down Expand Up @@ -91,7 +92,34 @@ def latent_samples_from(

Returns ``None`` when no finite latent samples remain (or no latents are
defined).

Raises
------
exc.SamplesException
If ``samples`` is ``None``. This is the resumed-fit case: a search whose
fit already completed short-circuits to
``NonLinearSearch.result_via_completed_fit``, which loads the samples
back off disk from ``samples.csv``. If that file was never written --
because ``output.yaml``'s ``samples: false`` (or ``general.yaml``'s
``samples_to_csv: false``) disabled it -- ``result.samples`` is ``None``
and there is nothing to compute latents from. Latent computation needs
the full sample list; ``samples_summary.json`` is not a substitute.
"""
if samples is None:
raise exc.SamplesException(
"Cannot compute latent samples: `samples` is None.\n\n"
"This usually means the fit was resumed rather than run: a completed "
"fit reloads its samples from `samples.csv`, and that file was not "
"written because samples output is disabled (`samples: false` in "
"`config/output.yaml`, or `samples_to_csv: false` in "
"`config/general.yaml`).\n\n"
"Latent variables are computed per posterior sample, so the full "
"sample list is required -- `samples_summary.json` is not enough. "
"Either enable samples output and re-run the fit (deleting the "
"existing output so it is not resumed), or pass a `Samples` object "
"explicitly."
)

batch_size = batch_size or 10

latent = analysis.Latent
Expand Down
36 changes: 36 additions & 0 deletions test_autofit/analysis/test_latent.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,39 @@ def test_new_path_single_survivor_summary_does_not_crash():
_ = latent.summary()
instance = latent.median_pdf()
assert instance.fwhm == pytest.approx(FWHM_SIGMA_3)


class NoLatentAnalysis(af.Analysis):
"""An analysis declaring no latent variables at all."""

def log_likelihood_function(self, instance):
return 1.0


def test_none_samples_raises_samples_exception():
"""
A resumed fit whose `samples.csv` was never written (samples output
disabled) hands `result.samples is None` to `compute_latent_samples`.
That must be an intentional, explanatory `SamplesException` -- never an
`AttributeError` raised from inside `latent_samples_from` when it
dereferences `samples.model`.
"""
with pytest.raises(af.exc.SamplesException) as exc_info:
FwhmAnalysis().compute_latent_samples(None)

message = str(exc_info.value)
assert "samples.csv" in message
assert "resumed" in message


def test_none_samples_raises_via_engine_directly_and_before_latent_lookup():
"""
The guard lives in the engine (so the free-function entry point is covered
too) and fires before any latent configuration is consulted -- a `None`
samples object is a broken input regardless of whether latents are defined.
"""
with pytest.raises(af.exc.SamplesException):
latent_samples_from(FwhmAnalysis(), None)

with pytest.raises(af.exc.SamplesException):
latent_samples_from(NoLatentAnalysis(), None)
Loading