|
2 | 2 | import numpy as np |
3 | 3 | from typing import Optional, TYPE_CHECKING |
4 | 4 |
|
| 5 | +from autoarray import validate |
| 6 | + |
5 | 7 | if TYPE_CHECKING: |
6 | 8 | from autoarray.inversion.linear_obj.linear_obj import LinearObj |
7 | 9 |
|
8 | 10 |
|
| 11 | +def validate_coefficient(coefficient, name: str = "coefficient"): |
| 12 | + """ |
| 13 | + Raise if a regularization coefficient is a concrete scalar which is negative or |
| 14 | + non-finite. |
| 15 | +
|
| 16 | + Every regularization scheme calls this from its constructor, so the message for |
| 17 | + this class of mistake is written once here rather than per scheme. |
| 18 | +
|
| 19 | + Zero is permitted: it is a degenerate but meaningful request for no regularization. |
| 20 | + Negative is not, and is **not** inert despite appearances — see the note below. |
| 21 | +
|
| 22 | + Coefficients are free model parameters, so under a JAX-traced fit this receives a |
| 23 | + tracer rather than a number. `autoarray.validate` gates on concreteness before |
| 24 | + comparing, so the guard costs nothing inside a trace. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + coefficient |
| 29 | + The regularization coefficient to validate. |
| 30 | + name |
| 31 | + The parameter's name, used in the error message (schemes with more than one |
| 32 | + coefficient pass their own, e.g. ``inner_coefficient``). |
| 33 | + """ |
| 34 | + validate.validate_non_negative_finite( |
| 35 | + value=coefficient, |
| 36 | + name=name, |
| 37 | + extra=( |
| 38 | + "A regularization coefficient sets the strength of the smoothing applied " |
| 39 | + "to the reconstruction, which cannot be negative. A negative value is not " |
| 40 | + "inert: `regularization_matrix_from` squares it, which hides the sign, but " |
| 41 | + "`regularization_weights_from` returns it unsquared and so leaks negative " |
| 42 | + "regularization weights into every consumer of that method" |
| 43 | + ), |
| 44 | + ) |
| 45 | + |
| 46 | + |
9 | 47 | class AbstractRegularization: |
10 | 48 | is_split_regularization = False |
11 | 49 | """ |
|
0 commit comments