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

Function test_fit_spectrum

pyxrf/core/tests/test_fitting.py:449–509  ·  view source on GitHub ↗
(dataset_params, process_params)

Source from the content-addressed store, hash-verified

447])
448# fmt: on
449def test_fit_spectrum(dataset_params, process_params):
450 fitting_data = DataForFittingTest(**dataset_params)
451
452 params = process_params.copy() # We don't want to create a reference, since we change 'params'
453
454 if "axis" in dataset_params:
455 params["axis"] = dataset_params["axis"]
456
457 spectra = fitting_data.spectra
458 data_input = fitting_data.data_input
459
460 # -------------- Test regular fitting ---------------
461 weights_estimated, rfactor, results_dict = fit_spectrum(data_input, spectra, **params)
462
463 fitting_data.validate_output_weights(weights_estimated, decimal=10)
464
465 # We don't verify the values of 'rfactor' and 'residual', since they are verified for each
466 # optimization method separately. We verify only the dimensions of the arrays
467 data_dim = dataset_params["n_data_dimensions"]
468 assert (
469 rfactor.shape == data_dim
470 ), f"The shape of 'rfactor' array {rfactor.shape} does not match the shape of data {data_dim}"
471
472 if "method" not in params:
473 params["method"] = "nnls" # This is supposed to be the default value
474
475 # The rest of the checks are individual for optimization method
476 if params["method"] == "admm":
477 # Check for existance and dimensions of 'convergence' and 'feasibility' arrays
478 assert (
479 results_dict["method"] == "admm"
480 ), f"Incorrect method '{results_dict['method']}' is reported by ADMM optimization function"
481 assert (
482 "convergence" in results_dict
483 ), "Array 'convergence' is not in the dictionary of results for ADMM optimization method"
484 assert (
485 "feasibility" in results_dict
486 ), "Array 'feasibility' is not in the dictionary of results for ADMM optimization method"
487 assert (
488 results_dict["convergence"].ndim == 1
489 ), "The returned 'convergence' must be 1D array (ADMM optimization method)"
490 assert (
491 results_dict["feasibility"].ndim == 1
492 ), "The returned 'feasibility' must be 1D array (ADMM optimization method)"
493 assert results_dict["convergence"].shape == results_dict["feasibility"].shape, (
494 "The returned 'convergence' and 'feasibility' arrays must have the same size "
495 "(ADMM optimization method)"
496 )
497 elif params["method"] == "nnls":
498 assert (
499 results_dict["method"] == "nnls"
500 ), f"Incorrect method '{results_dict['method']}' is reported by NNLS optimization function"
501 assert (
502 "residual" in results_dict
503 ), "Array 'residual' is not in the dictionary of results for NNLS optimization method"
504 assert results_dict["residual"].shape == data_dim, (
505 f"The shape of 'residual' array {results_dict['residual'].shape} does not match "
506 f"the shape of data {data_dim}"

Callers

nothing calls this directly

Calls 3

fit_spectrumFunction · 0.90
DataForFittingTestClass · 0.85

Tested by

no test coverage detected