From c8511b553b422a2c98d32beef9c410bd92aadddc Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sun, 12 Apr 2026 18:21:55 +0100 Subject: [PATCH] fix: guard interpolator empty instances and grid search None log_evidence Two defensive fixes: - interpolator.__getitem__ now raises a clear IndexError when instances list is empty instead of a cryptic index-out-of-range - grid_search_result.log_evidences() handles None log_evidence values instead of crashing on NoneType - float subtraction Co-Authored-By: Claude Opus 4.6 --- autofit/interpolator/abstract.py | 5 +++++ autofit/non_linear/grid/grid_search/result.py | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/autofit/interpolator/abstract.py b/autofit/interpolator/abstract.py index 51f0c6453..164566b99 100644 --- a/autofit/interpolator/abstract.py +++ b/autofit/interpolator/abstract.py @@ -95,6 +95,11 @@ def __getitem__(self, item: Equality) -> ModelInstance: value_map = self._value_map(item.path) x = sorted(value_map) + if not self.instances: + raise IndexError( + "Cannot interpolate: no instances have been added to the interpolator." + ) + instance = self.instances[0] new_instance = copy.copy(instance) diff --git a/autofit/non_linear/grid/grid_search/result.py b/autofit/non_linear/grid/grid_search/result.py index d85dcab27..39ecc1b99 100644 --- a/autofit/non_linear/grid/grid_search/result.py +++ b/autofit/non_linear/grid/grid_search/result.py @@ -303,7 +303,12 @@ def log_evidences(self, relative_to_value: float = 0.0) -> GridList: The value to subtract from every log likelihood, for example if Bayesian model comparison is performed on the grid search and the subtracted value is the maximum log likelihood of a previous search. """ - return [sample.log_evidence - relative_to_value for sample in self.samples] + return [ + sample.log_evidence - relative_to_value + if sample.log_evidence is not None + else None + for sample in self.samples + ] def figure_of_merits( self, use_log_evidences: bool, relative_to_value: float = 0.0