(dataset_params)
| 217 | ]) |
| 218 | # fmt: on |
| 219 | def test_fitting_nnls(dataset_params): |
| 220 | fitting_data = DataForFittingTest(**dataset_params) |
| 221 | |
| 222 | spectra = fitting_data.spectra |
| 223 | data_input = fitting_data.data_input |
| 224 | |
| 225 | # -------------- Test regular fitting --------------- |
| 226 | weights_estimated, rfactor, residual = _fitting_nnls(data_input, spectra) |
| 227 | |
| 228 | fitting_data.validate_output_weights(weights_estimated, decimal=10) |
| 229 | |
| 230 | # Validate 'rfactor' and 'residual' (do it for a single point) |
| 231 | data_fitted = np.matmul(weights_estimated[:, 0], np.transpose(spectra)) |
| 232 | res = data_fitted - data_input[:, 0] |
| 233 | |
| 234 | # R-factor |
| 235 | assert ( |
| 236 | rfactor.ndim == 1 and len(rfactor) == weights_estimated.shape[1] |
| 237 | ), f"'rfactor' dimensions are incorrect ({rfactor.shape})" |
| 238 | rf = np.sum(np.abs(res)) / np.sum(np.abs(data_input)) # Desired value |
| 239 | npt.assert_almost_equal(rfactor[0], rf, err_msg="R-factor is computed incorrectly") |
| 240 | |
| 241 | # Residual |
| 242 | assert ( |
| 243 | residual.ndim == 1 and len(residual) == weights_estimated.shape[1] |
| 244 | ), f"'residual' dimensions are incorrect ({residual.shape})" |
| 245 | rs = np.sqrt(np.sum(np.square(res))) # Desired value (this is how 'nnls' computes the residual) |
| 246 | npt.assert_almost_equal(rfactor[0], rs, err_msg="Residual is computed incorrectly") |
| 247 | |
| 248 | |
| 249 | # fmt: off |
nothing calls this directly
no test coverage detected