Skip to content

Commit 5be0495

Browse files
Jammy2211claude
authored andcommitted
Cache expensive @Property on Fit classes as @cached_property
FitDataset, FitImaging, and FitInterferometer properties like model_data, residual_map, chi_squared_map, and log_likelihood were recomputing on every access despite depending only on immutable constructor args. For Delaunay inversions model_data alone cost 5-20s — downstream properties accessed it redundantly, creating a cascade of recomputation. Changed @Property to @functools.cached_property. Safe because Fit objects are constructed once and never mutated, and none of these classes use __getstate__ or JAX pytree registration on the Fit object itself. Closes PyAutoArray#340 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent cbdbb69 commit 5be0495

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

autolens/imaging/fit_imaging.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
The ``TracerToInversion`` helper is used to assemble the linear system in step 4.
2020
"""
2121
import copy
22+
import functools
2223
import numpy as np
2324
from typing import Dict, List, Optional
2425

@@ -94,7 +95,7 @@ def __init__(
9495
self.adapt_images = adapt_images
9596
self.settings = settings or aa.Settings()
9697

97-
@property
98+
@functools.cached_property
9899
def blurred_image(self) -> aa.Array2D:
99100
"""
100101
Returns the image of all light profiles in the fit's tracer convolved with the imaging dataset's PSF.
@@ -115,7 +116,7 @@ def blurred_image(self) -> aa.Array2D:
115116
xp=self._xp,
116117
)
117118

118-
@property
119+
@functools.cached_property
119120
def profile_subtracted_image(self) -> aa.Array2D:
120121
"""
121122
Returns the dataset's image with all blurred light profile images in the fit's tracer subtracted.
@@ -155,7 +156,7 @@ def inversion(self) -> Optional[aa.AbstractInversion]:
155156

156157
return self.tracer_to_inversion.inversion
157158

158-
@property
159+
@functools.cached_property
159160
def model_data(self) -> aa.Array2D:
160161
"""
161162
Returns the model-image that is used to fit the data.
@@ -226,7 +227,7 @@ def galaxy_model_image_dict(self) -> Dict[ag.Galaxy, np.ndarray]:
226227

227228
return {**galaxy_blurred_image_2d_dict, **galaxy_linear_obj_image_dict}
228229

229-
@property
230+
@functools.cached_property
230231
def subtracted_images_of_galaxies_dict(self) -> Dict[ag.Galaxy, np.ndarray]:
231232
"""
232233
A dictionary which associates every galaxy in the tracer with its `subtracted image`.
@@ -249,7 +250,7 @@ def subtracted_images_of_galaxies_dict(self) -> Dict[ag.Galaxy, np.ndarray]:
249250

250251
return subtracted_images_of_galaxies_dict
251252

252-
@property
253+
@functools.cached_property
253254
def subtracted_signal_to_noise_maps_of_galaxies_dict(self) -> Dict[ag.Galaxy, np.ndarray]:
254255
"""
255256
A dictionary which associates every galaxy in the tracer with its `subtracted image`.
@@ -299,7 +300,7 @@ def model_images_of_planes_list(self) -> List[aa.Array2D]:
299300

300301
return model_images_of_planes_list
301302

302-
@property
303+
@functools.cached_property
303304
def subtracted_images_of_planes_list(self) -> List[aa.Array2D]:
304305
"""
305306
A list of the subtracted image of every plane.

0 commit comments

Comments
 (0)