Skip to content

Commit 0eefe0c

Browse files
Jammy2211claude
authored andcommitted
improve docstrings for autoarray/inversion package and update fit log_evidence
inversion package: - abstract.py: fix wrong Parameters section in AbstractInversion.__init__ (was listing data/noise_map; now correctly lists dataset/linear_obj_list/settings/xp); remove spurious dict_values param from has() method - imaging/abstract.py: fix same wrong Parameters section (data/noise_map → dataset/...) - interferometer/abstract.py: fix wrong Parameters section (noise_map/transformer → dataset/...); add docstring to fast_chi_squared property - dataset_interface.py: remove spurious over_sampler/border_relocator params; fix "imaigng" typo - inversion_interferometer_util.py: fix wrong param name image → visibilities - inversion_util.py: add missing params add_to_curvature_diag/no_regularization_index_list/ settings/xp to curvature_matrix_via_mapping_matrix_from; fix "transfomed" typo - linear_obj.py: fix "piels" → "pixels" typo; fix "regularizaiton" typo - mappers/abstract.py: fix wrong Parameters in Mapper.__init__ (was listing source_plane_data_grid/source_plane_mesh_grid/adapt_data/mesh_weight_map; now lists actual params interpolator/regularization/settings/image_plane_mesh_grid/xp); fix "unfamliar"/"pxelization"/"annd" typos - imaging/inversion_imaging_util.py: fix "PSf" → "PSF" typo fit package (retrospective improvements using inversion context): - fit_util.py: expand log_evidence_from docstring with full mathematical formula (χ² + s^T H s + ln det(F+H) - ln det(H) + Σ ln 2πσ²) and paper references - fit_dataset.py: expand log_evidence property with same full formula - fit_interferometer.py: expand log_evidence with full formula; mention fast_chi_squared Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 21a1744 commit 0eefe0c

File tree

12 files changed

+95
-64
lines changed

12 files changed

+95
-64
lines changed

autoarray/fit/fit_dataset.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,20 @@ def log_likelihood_with_regularization(self) -> float:
325325
@property
326326
def log_evidence(self) -> float:
327327
"""
328-
Returns the log evidence of the inversion's fit to a dataset, where the log evidence includes a number of terms
329-
which quantify the complexity of an inversion's reconstruction (see the `Inversion` module):
328+
Returns the log Bayesian evidence of the inversion's fit to a dataset, which extends the log likelihood by
329+
including penalty terms that quantify the complexity of the inversion's reconstruction:
330330
331-
Log Evidence = -0.5*[Chi_Squared_Term + Regularization_Term + Log(Covariance_Regularization_Term) -
332-
Log(Regularization_Matrix_Term) + Noise_Term]
331+
Log Evidence = -0.5 * [χ² + s^T H s + ln(det(F + H)) - ln(det(H)) + Σ ln(2π σ²)]
332+
333+
where:
334+
- χ² is the chi-squared goodness-of-fit term
335+
- s^T H s is the regularization term (smoothness penalty on the reconstructed source pixels)
336+
- ln(det(F + H)) penalizes overly complex reconstructions (log determinant of the curvature + regularization matrix)
337+
- ln(det(H)) normalizes the regularization matrix complexity (log determinant of the regularization matrix)
338+
- Σ ln(2π σ²) is the noise normalization term
339+
340+
This is described in Warren & Dye 2003 (https://arxiv.org/pdf/astro-ph/0302587.pdf) and
341+
Nightingale & Dye 2015 (https://arxiv.org/abs/1708.07377).
333342
334343
Returns `None` if no inversion is present, in which case `log_likelihood` is used as the figure of merit.
335344
"""

autoarray/fit/fit_interferometer.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,20 @@ def noise_normalization(self) -> float:
136136
@property
137137
def log_evidence(self) -> float:
138138
"""
139-
Returns the log evidence of the inversion's fit to a dataset, where the log evidence includes a number of terms
140-
which quantify the complexity of an inversion's reconstruction (see the `Inversion` module):
139+
Returns the log Bayesian evidence of the inversion's fit to a dataset, which extends the log likelihood by
140+
including penalty terms that quantify the complexity of the inversion's reconstruction:
141141
142-
Log Evidence = -0.5*[Chi_Squared_Term + Regularization_Term + Log(Covariance_Regularization_Term) -
143-
Log(Regularization_Matrix_Term) + Noise_Term]
142+
Log Evidence = -0.5 * [χ² + s^T H s + ln(det(F + H)) - ln(det(H)) + Σ ln(2π σ²)]
144143
145-
For interferometer fits the chi-squared uses the fast inversion chi-squared (`inversion.fast_chi_squared`).
144+
where:
145+
- χ² is the chi-squared goodness-of-fit term
146+
- s^T H s is the regularization term (smoothness penalty on the reconstructed source pixels)
147+
- ln(det(F + H)) penalizes overly complex reconstructions (log determinant of the curvature + regularization matrix)
148+
- ln(det(H)) normalizes the regularization matrix complexity (log determinant of the regularization matrix)
149+
- Σ ln(2π σ²) is the noise normalization term
150+
151+
For interferometer fits the chi-squared uses `inversion.fast_chi_squared`, which avoids computing the full
152+
residual visibilities by evaluating χ² directly from the reconstruction and inversion matrices.
146153
147154
Returns `None` if no inversion is present, in which case `log_likelihood` is used as the figure of merit.
148155
"""

autoarray/fit/fit_util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,12 @@ def log_evidence_from(
404404
noise_normalization: float,
405405
) -> float:
406406
"""
407-
Returns the log evidence of an inversion's fit to a dataset, where the log evidence includes a number of terms which
408-
quantify the complexity of an inversion's reconstruction (see the `Inversion` module):
407+
Returns the log Bayesian evidence of an inversion's fit to a dataset, which extends the log likelihood by
408+
including penalty terms that quantify the complexity of the inversion's reconstruction:
409409
410-
Log Evidence = -0.5*[Chi_Squared_Term + Regularization_Term + Log(Covariance_Regularization_Term) -
411-
Log(Regularization_Matrix_Term) + Noise_Term]
410+
Log Evidence = -0.5 * [χ² + s^T H s + ln(det(F + H)) - ln(det(H)) + Σ ln(2π σ²)]
411+
412+
This is described in Warren & Dye 2003 (https://arxiv.org/pdf/astro-ph/0302587.pdf).
412413
413414
Parameters
414415
----------

autoarray/inversion/inversion/abstract.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ def __init__(
5555
5656
Parameters
5757
----------
58-
data
59-
The data of the dataset (e.g. the `image` of `Imaging` data) which may have been changed.
60-
noise_map
61-
The noise_map of the noise_mapset (e.g. the `noise_map` of `Imaging` noise_map) which may have been changed.
58+
dataset
59+
The dataset being reconstructed (e.g. an `Imaging` or `Interferometer` dataset, or a `DatasetInterface`
60+
whose attributes like `data` and `noise_map` may have been modified before being passed in).
6261
linear_obj_list
6362
The list of linear objects (e.g. analytic functions, a mapper with a pixelized grid) which reconstruct the
6463
input dataset's data and whose values are solved for via the inversion.
6564
settings
66-
Settings controlling how an inversion is fitted for example which linear algebra formalism is used.
65+
Settings controlling how an inversion is fitted, for example which linear algebra formalism is used.
66+
xp
67+
The array module to use (`numpy` by default; pass `jax.numpy` for JAX support).
6768
"""
6869

6970
self.dataset = dataset
@@ -96,11 +97,8 @@ def has(self, cls: Type) -> bool:
9697
9798
Parameters
9899
----------
99-
dict_values
100-
A class dictionary of values which instances of the input `cls` are checked to see if it has an instance
101-
of that class.
102100
cls
103-
The type of class that is checked if the object has an instance of.
101+
The type of class whose presence is checked among the linear objects and regularizations in this inversion.
104102
"""
105103
return misc_util.has(
106104
values=self.linear_obj_list + self.regularization_list, cls=cls

autoarray/inversion/inversion/dataset_interface.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,10 @@ def __init__(
3636
An array describing the RMS standard deviation error in each pixel used for computing quantities like the
3737
chi-squared in a fit (in PyAutoGalaxy and PyAutoLens the recommended units are electrons per second).
3838
grids
39-
The grids of (y,x) Cartesian coordinates that the image data is paired with, which are used for evaluting
39+
The grids of (y,x) Cartesian coordinates that the image data is paired with, which are used for evaluating
4040
light profiles and calculations associated with a pixelization.
41-
over_sampler
42-
Performs over-sampling whereby the masked image pixels are split into sub-pixels, which are all
43-
mapped via the mapper with sub-fractional values of flux.
44-
border_relocator
45-
The border relocator, which relocates coordinates outside the border of the source-plane data grid to its
46-
edge.
4741
psf
48-
Perform 2D convolution of the imaigng data's PSF when computing the operated mapping matrix.
42+
Perform 2D convolution of the imaging data's PSF when computing the operated mapping matrix.
4943
transformer
5044
Performs a Fourier transform of the image-data from real-space to visibilities when computing the
5145
operated mapping matrix.

autoarray/inversion/inversion/imaging/abstract.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ def __init__(
5050
5151
Parameters
5252
----------
53-
data
54-
The data of the dataset (e.g. the `image` of `Imaging` data) which may have been changed.
55-
noise_map
56-
The noise_map of the noise_mapset (e.g. the `noise_map` of `Imaging` noise_map) which may have been changed.
53+
dataset
54+
The imaging dataset being reconstructed (e.g. an `Imaging` dataset or a `DatasetInterface` whose
55+
attributes like `data`, `noise_map`, and `psf` may have been modified before being passed in).
5756
linear_obj_list
5857
The list of linear objects (e.g. analytic functions, a mapper with a pixelized grid) which reconstruct the
5958
input dataset's data and whose values are solved for via the inversion.
6059
settings
61-
Settings controlling how an inversion is fitted for example which linear algebra formalism is used.
60+
Settings controlling how an inversion is fitted, for example which linear algebra formalism is used.
61+
xp
62+
The array module to use (`numpy` by default; pass `jax.numpy` for JAX support).
6263
"""
6364

6465
super().__init__(

autoarray/inversion/inversion/imaging/inversion_imaging_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def data_linear_func_matrix_from(
154154
The operated values of each linear function divided by the noise-map squared, in a matrix of shape
155155
[data_pixels, total_fixed_linear_functions].
156156
kernel_native
157-
The 2D PSf kernel.
157+
The 2D PSF kernel.
158158
159159
Returns
160160
-------

autoarray/inversion/inversion/interferometer/abstract.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ def __init__(
3131
3232
Parameters
3333
----------
34-
noise_map
35-
The noise-map of the observed interferometer data which values are solved for.
36-
transformer
37-
The transformer which performs a non-uniform fast Fourier transform operations on the mapping matrix
38-
with the interferometer data's transformer.
34+
dataset
35+
The interferometer dataset being reconstructed (e.g. an `Interferometer` dataset or a `DatasetInterface`
36+
whose attributes like `data`, `noise_map`, and `transformer` may have been modified).
3937
linear_obj_list
4038
The linear objects used to reconstruct the data's observed values. If multiple linear objects are passed
4139
the simultaneous linear equations are combined and solved simultaneously.
40+
settings
41+
Settings controlling how an inversion is fitted, for example which linear algebra formalism is used.
42+
xp
43+
The array module to use (`numpy` by default; pass `jax.numpy` for JAX support).
4244
"""
4345

4446
super().__init__(
@@ -124,7 +126,19 @@ def mapped_reconstructed_data_dict(
124126

125127
@property
126128
def fast_chi_squared(self):
129+
"""
130+
Returns the chi-squared of the interferometer inversion without needing to form the full residual visibilities.
131+
132+
This is computed directly from the reconstruction and the matrices of the inversion:
133+
134+
chi_squared = s^T F s - 2 s^T D + sum(d_r^2/sigma_r^2) + sum(d_i^2/sigma_i^2)
127135
136+
where `s` is the reconstruction vector, `F` is the curvature matrix, `D` is the data vector,
137+
and `d_r`/`d_i` are the real/imaginary parts of the observed visibilities.
138+
139+
This avoids computing the full mapped reconstructed visibilities and is faster than computing
140+
`chi_squared` via the residual visibilities when many source pixels are used.
141+
"""
128142
xp = self._xp
129143

130144
chi_squared_term_1 = xp.linalg.multi_dot(

autoarray/inversion/inversion/interferometer/inversion_interferometer_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ def data_vector_via_transformed_mapping_matrix_from(
2020
----------
2121
transformed_mapping_matrix
2222
The matrix representing the transformed mappings between sub-grid pixels and pixelization pixels.
23-
image
24-
Flattened 1D array of the observed image the inversion is fitting.
23+
visibilities
24+
The complex 1D array of observed visibilities the inversion is fitting, with real and imaginary components.
2525
noise_map
26-
Flattened 1D array of the noise-map used by the inversion during the fit.
26+
The complex 1D array of the noise-map used by the inversion during the fit.
2727
"""
2828
# Extract components
2929
vis_real = visibilities.real

autoarray/inversion/inversion/inversion_util.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,20 @@ def curvature_matrix_via_mapping_matrix_from(
9292
Parameters
9393
----------
9494
mapping_matrix
95-
The matrix representing the mappings (these could be blurred or transfomed) between sub-grid pixels and
95+
The matrix representing the mappings (these could be blurred or transformed) between sub-grid pixels and
9696
pixelization pixels.
9797
noise_map
9898
Flattened 1D array of the noise-map used by the inversion during the fit.
99+
add_to_curvature_diag
100+
If `True`, adds a small numerical value to the diagonal entries of the curvature matrix at the indices
101+
specified by `no_regularization_index_list`, to ensure the matrix is positive-definite.
102+
no_regularization_index_list
103+
A list of parameter indices which have no regularization applied. A small value is added to their diagonal
104+
entries if `add_to_curvature_diag` is `True`.
105+
settings
106+
Settings controlling how the inversion is performed, for example whether mixed precision is used.
107+
xp
108+
The array module to use (`numpy` by default; pass `jax.numpy` for JAX support).
99109
"""
100110
# NumPy path: keep it simple + stable
101111
if xp is np:

0 commit comments

Comments
 (0)