r""" Perform fitting of a single or or multiple spectra. A single spectra is represented as a 1D ndarray. Multiple spectra collected from scan of a line, 2D or 3D image may be represented as multidimensional array containing spectral information along axis ``axis``. The returned fitt
(data, ref_spectra, *, method="nnls", axis=0, maxiter=100, rate=0.2, epsilon=1e-30)
| 78 | |
| 79 | |
| 80 | def fit_spectrum(data, ref_spectra, *, method="nnls", axis=0, maxiter=100, rate=0.2, epsilon=1e-30): |
| 81 | r""" |
| 82 | Perform fitting of a single or or multiple spectra. A single spectra is represented as |
| 83 | a 1D ndarray. Multiple spectra collected from scan of a line, 2D or 3D image may be represented |
| 84 | as multidimensional array containing spectral information along axis ``axis``. The returned |
| 85 | fitting result have the same dimensionality as the input data with fitted coefficients |
| 86 | located along the same axis as the spectrum data. |
| 87 | |
| 88 | Parameters |
| 89 | ---------- |
| 90 | data : ndarray |
| 91 | single or multidimensional spectral data. If fitting done for a single spectrum with K energy points, |
| 92 | ``data`` is a 1D ndarray with K points. If 'data' contains spectra obtained by scanning NxM map, |
| 93 | ``data`` is 3D ndarray with dimensions (K,N,M), (N,K,M) or (N,M,K). The axis containing spectra |
| 94 | is specified by the parameter ``axis``, which should be set to 0 (default), 1 or 2 for the for the |
| 95 | 3D example above. Dimensionality of ``data`` array is not restricted and the function will perform |
| 96 | properly if ``axis`` points to the correct dimension. |
| 97 | |
| 98 | ref_spectra : ndarray (2D) |
| 99 | array with columns representing the reference spectra. If fitting is performed for Q reference |
| 100 | spectra, then ``ref_spectra`` has dimensions (K,Q), where K is the number of energy points. |
| 101 | |
| 102 | method : str |
| 103 | optimization method used for fitting. Currently supported methods are "nnls" and "admm". |
| 104 | |
| 105 | axis : int |
| 106 | the number of the axis in the ``data`` array that hold the spectral information. If ``data`` |
| 107 | array has ``n`` dimensions, then ``axis`` may take values in the range ``-n .. n-1``. The |
| 108 | fitting results will be placed in the output data array along the same axis. |
| 109 | |
| 110 | maxiter : int |
| 111 | maximum number of iterations. Optimization may stop prematurely if convergence criteria are met. |
| 112 | |
| 113 | rate : float |
| 114 | descent rate for optimization algorithm. Currently is used only for ADMM fitting (1/lambda). |
| 115 | |
| 116 | epsilon : float |
| 117 | small value used in stopping criterion of ADMM optimization algorithm. |
| 118 | |
| 119 | Returns |
| 120 | ------- |
| 121 | weights : ndarray |
| 122 | array with the same number of dimensions as ``data``. Weights are placed along the axis ``axis``. |
| 123 | For example, if ``data`` has the shape (N,K,M) and ``axis=1``, then ``weights`` has |
| 124 | the shape (N,Q,M), where Q is the number of spectrum references. |
| 125 | |
| 126 | rfactor : ndarray |
| 127 | array that contains R-factor value for each fitted spectrum. For example, if ``data`` has shape |
| 128 | (N,K,M) and ``axis=1``, then ``rfactor`` has shape (N,M) |
| 129 | |
| 130 | results_dict : dict |
| 131 | dictionary that contains additional information returned by a fitting routine. The contents of |
| 132 | the dictionary depends on the optimization routine. Common information: ``method`` - string |
| 133 | that contains the name of the optimization method (``nnls`` or ``admm``). |
| 134 | |
| 135 | NNLS optimization: |
| 136 | |
| 137 | - ``residual`` - an array that contains values of least-squares difference between the observed |