Skip to content

Commit

Permalink
Merge branch 'development' into feature/thermal_cx_line
Browse files Browse the repository at this point in the history
  • Loading branch information
vsnever committed Jul 11, 2024
2 parents b0113d9 + d033dd1 commit 941df80
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
numpy-version: ["oldest-supported-numpy", "numpy"]
numpy-version: ["oldest-supported-numpy", "'numpy<2'"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
steps:
- name: Checkout code
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ New:
* 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)
* **Beam dispersion calculation has changed from sigma(z) = sigma + z * tan(alpha) to sigma(z) = sqrt(sigma^2 + (z * tan(alpha))^2) for consistancy with the Gaussian beam model. Attention!!! The results of BES and CX spectroscopy are affected by this change. (#414)**
* Improved beam direction calculation to allow for natural broadening of the BES line shape due to beam divergence. (#414)
* Add kwargs to invert_regularised_nnls to pass them to scipy.optimize.nnls. (#438)
* Add thermal charge-exchange emission model. (#57)
* PECs for C VI spectral lines for n <= 5 are now included in populate(). Rerun populate() after upgrading to 1.5 to update the atomic data repository.

Expand Down
15 changes: 11 additions & 4 deletions cherab/tools/inversions/nnls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@
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):
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)`.
:param float alpha: The regularisation hyperparameter :math:`\alpha` which determines
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
Expand All @@ -60,6 +64,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
return x_vector, rnorm * vmax
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cython>=0.28
numpy>=1.14
numpy>=1.14,<2.0
scipy
matplotlib
raysect==0.8.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=[
"numpy>=1.14",
"numpy>=1.14,<2.0",
"scipy",
"matplotlib",
"raysect==0.8.1",
Expand Down

0 comments on commit 941df80

Please sign in to comment.