MCPcopy Create free account
hub / github.com/NSLS2/PyXRF / test_fitting_nnls

Function test_fitting_nnls

pyxrf/core/tests/test_fitting.py:219–246  ·  view source on GitHub ↗
(dataset_params)

Source from the content-addressed store, hash-verified

217])
218# fmt: on
219def 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

Callers

nothing calls this directly

Calls 3

_fitting_nnlsFunction · 0.90
DataForFittingTestClass · 0.85

Tested by

no test coverage detected