r""" Computes R-factor for the fitting results Parameters ---------- spectrum : ndarray spectrum data on which fitting is performed (N elements) fit_results : ndarray results of fitting (coefficients, K elements) ref_spectra : 2D ndarray reference s
(spectrum, fit_results, ref_spectra)
| 3 | |
| 4 | |
| 5 | def rfactor_compute(spectrum, fit_results, ref_spectra): |
| 6 | r""" |
| 7 | Computes R-factor for the fitting results |
| 8 | |
| 9 | Parameters |
| 10 | ---------- |
| 11 | spectrum : ndarray |
| 12 | spectrum data on which fitting is performed (N elements) |
| 13 | |
| 14 | fit_results : ndarray |
| 15 | results of fitting (coefficients, K elements) |
| 16 | |
| 17 | ref_spectra : 2D ndarray |
| 18 | reference spectra used for fitting (NxK element array) |
| 19 | |
| 20 | Returns |
| 21 | ------- |
| 22 | float, the value of R-factor |
| 23 | """ |
| 24 | |
| 25 | # Check if input parameters are valid |
| 26 | assert ( |
| 27 | spectrum.ndim == 1 or spectrum.ndim == 2 |
| 28 | ), "Parameter 'spectrum' must be 1D or 2D array, ({spectrum.ndim})" |
| 29 | assert spectrum.ndim == fit_results.ndim, ( |
| 30 | f"Spectrum data (ndim = {spectrum.ndim}) and fitting results " |
| 31 | f"(ndim = {fit_results.ndim}) must have the same number of dimensions" |
| 32 | ) |
| 33 | assert ref_spectra.ndim == 2, "Parameter 'ref_spectra' must be 2D array, ({ref_spectra.ndim})" |
| 34 | assert spectrum.shape[0] == ref_spectra.shape[0], ( |
| 35 | f"Arrays 'spectrum' ({spectrum.shape}) and 'ref_spectra' ({ref_spectra.shape}) " |
| 36 | "must have the same number of data points" |
| 37 | ) |
| 38 | assert fit_results.shape[0] == ref_spectra.shape[1], ( |
| 39 | f"Arrays 'fit_results' ({fit_results.shape}) and 'ref_spectra' ({ref_spectra.shape}) " |
| 40 | "must have the same number of spectrum points" |
| 41 | ) |
| 42 | if spectrum.ndim == 2: # Only if multiple spectra are processed |
| 43 | assert spectrum.shape[1] == fit_results.shape[1], ( |
| 44 | f"Arrays 'spectrum' {spectrum.shape} and 'fit_results' {fit_results.shape}" |
| 45 | "must have the same number of columns" |
| 46 | ) |
| 47 | |
| 48 | spectrum_fit = np.matmul(ref_spectra, fit_results) |
| 49 | return rfactor(spectrum, spectrum_fit) |
| 50 | |
| 51 | |
| 52 | def rfactor(spectrum_experimental, spectrum_fit): |