Source code for autoarray.inversion.regularization.constant
from __future__ import annotations
import numpy as np
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from autoarray.inversion.linear_obj.linear_obj import LinearObj
from autoarray.inversion.regularization.abstract import AbstractRegularization
from autoarray.inversion.regularization import regularization_util
[docs]class Constant(AbstractRegularization):
def __init__(self, coefficient: float = 1.0):
"""
A constant regularization scheme (regularization is described in the `Regularization` class above) which
uses a single value to apply smoothing on the solution of an `Inversion`.
For this regularization scheme, there is only 1 regularization coefficient that is applied to
all neighboring pixels / parameters. This means that the matrix B only needs to regularize pixels / parameters
in one direction (e.g. pixel 0 regularizes pixel 1, but NOT visa versa). For example:
B = [-1, 1] [0->1]
[0, -1] 1 does not regularization with 0
A small numerical value of 1.0e-8 is added to all elements in constant regularization matrix, to ensure that
it is positive definite.
Parameters
----------
coefficient
The regularization coefficient which controls the degree of smooth of the inversion reconstruction.
"""
self.coefficient = coefficient
super().__init__()
[docs] def regularization_weights_from(self, linear_obj: LinearObj) -> np.ndarray:
"""
Returns the regularization weights of this regularization scheme.
The regularization weights define the level of regularization applied to each parameter in the linear object
(e.g. the ``pixels`` in a ``Mapper``).
For standard regularization (e.g. ``Constant``) are weights are equal, however for adaptive schemes
(e.g. ``AdaptiveBrightness``) they vary to adapt to the data being reconstructed.
Parameters
----------
linear_obj
The linear object (e.g. a ``Mapper``) which uses these weights when performing regularization.
Returns
-------
The regularization weights.
"""
return self.coefficient * np.ones(linear_obj.params)
[docs] def regularization_matrix_from(self, linear_obj: LinearObj) -> np.ndarray:
"""
Returns the regularization matrix of this regularization scheme.
Parameters
----------
linear_obj
The linear object (e.g. a ``Mapper``) which uses this matrix to perform regularization.
Returns
-------
The regularization matrix.
"""
return regularization_util.constant_regularization_matrix_from(
coefficient=self.coefficient,
neighbors=linear_obj.neighbors,
neighbors_sizes=linear_obj.neighbors.sizes,
)