From eb21099d7ba213de83f460a4b6ac180b70b8245f Mon Sep 17 00:00:00 2001 From: vsnever Date: Tue, 14 May 2024 12:37:09 +0200 Subject: [PATCH 1/3] Add normalisation to invert_regularised_nnls(). Add **kwargs to pass to scipy.optimize.nnls. --- cherab/tools/inversions/nnls.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cherab/tools/inversions/nnls.py b/cherab/tools/inversions/nnls.py index 76f8149a..f7580b09 100644 --- a/cherab/tools/inversions/nnls.py +++ b/cherab/tools/inversions/nnls.py @@ -21,7 +21,7 @@ import scipy -def invert_regularised_nnls(w_matrix, b_vector, alpha=0.01, tikhonov_matrix=None): +def invert_regularised_nnls(w_matrix, b_vector, alpha=0.01, tikhonov_matrix=None, **kwargs): """ Solves :math:`\mathbf{b} = \mathbf{W} \mathbf{x}` for the vector :math:`\mathbf{x}`, using Tikhonov regulariastion. @@ -36,6 +36,7 @@ def invert_regularised_nnls(w_matrix, b_vector, alpha=0.01, tikhonov_matrix=None the regularisation strength of the tikhonov matrix. :param np.ndarray tikhonov_matrix: The tikhonov regularisation matrix operator, an array with shape :math:`(N_s, N_s)`. If None, the identity matrix is used. + :param **kwargs: Keyword arguments passed to scipy.optimize.nnls. :return: (x, norm), the solution vector and the residual norm. .. code-block:: pycon @@ -60,6 +61,9 @@ def invert_regularised_nnls(w_matrix, b_vector, alpha=0.01, tikhonov_matrix=None d_vector = np.zeros(m+n) d_vector[0:m] = b_vector[:] - x_vector, rnorm = scipy.optimize.nnls(c_matrix, d_vector) + # Normalise c_matrix and d_vector to avoid possible issues with the nnls termination criteria. + vmax = d_vector.max() + + x_vector, rnorm = scipy.optimize.nnls(c_matrix / vmax, d_vector / vmax, **kwargs) return x_vector, rnorm From a8d8cce35334ada24a1c31c8bfa7aa010b8af0e4 Mon Sep 17 00:00:00 2001 From: vsnever Date: Tue, 14 May 2024 15:18:06 +0200 Subject: [PATCH 2/3] Updated invert_regularised_nnls docstring and CHANGELOG.md. --- CHANGELOG.md | 1 + cherab/tools/inversions/nnls.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8156571a..02a1146e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ New: * Replace the coarse numerical constant in the Bremsstrahlung model with an exact expression. (#409) * Add the kind attribute to RayTransferPipelineXD that determines whether the ray transfer matrix is multiplied by sensitivity ('power') or not ('radiance'). (#412) * Improved parsing of metadata from the ADAS ADF15 'bnd' files for H-like ions. Raises a runtime error if the metadata cannot be parsed. (#424) +* Add kwargs to invert_regularised_nnls to pass them to scipy.optimize.nnls. (#438) Bug fixes: * Fix deprecated transforms being cached in LaserMaterial after laser.transform update (#420) diff --git a/cherab/tools/inversions/nnls.py b/cherab/tools/inversions/nnls.py index f7580b09..87e1d09c 100644 --- a/cherab/tools/inversions/nnls.py +++ b/cherab/tools/inversions/nnls.py @@ -22,13 +22,16 @@ def invert_regularised_nnls(w_matrix, b_vector, alpha=0.01, tikhonov_matrix=None, **kwargs): - """ + r""" Solves :math:`\mathbf{b} = \mathbf{W} \mathbf{x}` for the vector :math:`\mathbf{x}`, using Tikhonov regulariastion. This is a thin wrapper around scipy.optimize.nnls, which modifies the arguments to include the supplied Tikhonov regularisation matrix. + The values of w_matrix, b_vector and alpha * tikhonov_matrix are notmalised + by max(b_vector) before passing them to scipy.optimize.nnls(). + :param np.ndarray w_matrix: The sensitivity matrix describing the coupling between the detectors and the voxels. Must be an array with shape :math:`(N_d, N_s)`. :param np.ndarray b_vector: The measured power/radiance vector with shape :math:`(N_d)`. From e96b381601790ef239e551d545ab9b2ea4cdd3c7 Mon Sep 17 00:00:00 2001 From: vsnever Date: Thu, 23 May 2024 01:38:07 +0200 Subject: [PATCH 3/3] Rescale rnorm after parameter normalisation in invert_regularised_nnls. --- cherab/tools/inversions/nnls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cherab/tools/inversions/nnls.py b/cherab/tools/inversions/nnls.py index 87e1d09c..823bb7c2 100644 --- a/cherab/tools/inversions/nnls.py +++ b/cherab/tools/inversions/nnls.py @@ -69,4 +69,4 @@ def invert_regularised_nnls(w_matrix, b_vector, alpha=0.01, tikhonov_matrix=None x_vector, rnorm = scipy.optimize.nnls(c_matrix / vmax, d_vector / vmax, **kwargs) - return x_vector, rnorm + return x_vector, rnorm * vmax