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

Method verify_fit_output

pyxrf/core/tests/test_map_processing.py:699–762  ·  view source on GitHub ↗
(self, *, data_out, snip_param=None)

Source from the content-addressed store, hash-verified

697 self.snip_param = {"e_offset": 0.0, "e_linear": 0.1, "e_quadratic": 0.0, "b_width": 1}
698
699 def verify_fit_output(self, *, data_out, snip_param=None):
700 assert data_out.shape == (
701 self.data_tmp.shape[1],
702 self.data_tmp.shape[2],
703 self.n_lines + 4,
704 ), f"The shape of 'data_out' is incorrect: data_out.shape={data_out.shape}"
705
706 weights_estimated = data_out[:, :, 0 : self.n_lines]
707 bg_sum = data_out[:, :, self.n_lines]
708
709 if not self.use_snip:
710 # No background
711 self.fitting_data.validate_output_weights(np.moveaxis(weights_estimated, 2, 0), decimal=10)
712
713 assert (bg_sum == 0.0).all(), (
714 f"Baseline estimate is non-zero when snip method is disabled: \n"
715 f"bg_sum = {bg_sum}\nmin(bg_sum) = {np.min(bg_sum)}\nmax(bg_sum) = {np.max(bg_sum)}"
716 )
717 else:
718 # Background is present in the data. Here we repreat the procedure
719 # of background subtraction and fitting. Unfortunately, the test code
720 # is very similar to the computational code used
721 assert snip_param is not None, "Test parameter `snip_param` must be provided if 'use_snip' is enabled"
722 _data = np.moveaxis(self.data_tmp, 0, 2)
723 bg_sel = np.zeros(shape=_data.shape)
724 for ny in range(bg_sel.shape[0]):
725 for nx in range(bg_sel.shape[1]):
726 bg = snip_method_numba(
727 _data[ny, nx, :],
728 snip_param["e_offset"],
729 snip_param["e_linear"],
730 snip_param["e_quadratic"],
731 width=snip_param["b_width"],
732 )
733 bg_sel[ny, nx, :] = bg
734
735 _data_no_bg = _data - bg_sel
736 weights_expected, rfactor, _ = fit_spectrum(_data_no_bg, self.spectra, axis=2, method="nnls")
737 npt.assert_array_almost_equal(
738 weights_estimated,
739 weights_expected,
740 err_msg="Estimated weights are not equal to expected (use_snip==True)",
741 )
742
743 bg_sum_expected = np.sum(bg_sel, axis=2)
744 npt.assert_array_almost_equal(
745 bg_sum, bg_sum_expected, err_msg="Baseline is estimated incorrectly (use_snip==True)"
746 )
747
748 # Let's trust, that R-factor is correctly inserted into
749 # the 'data_out' array. Computation of R-factor is tested elsewhere,
750
751 # Verify if the total count for the whole spectra and the selected region
752 # was computed correctly
753 sm_total = np.sum(self.data_input, axis=2)
754 sm_sel = np.sum(self.data_tmp, axis=0) # Sum over the original dataset
755 npt.assert_array_almost_equal(
756 data_out[:, :, self.n_lines + 2],

Callers 3

test_fit_xrf_blockFunction · 0.80
test_fit_xrf_map1Method · 0.80
test_fit_xrf_map2Function · 0.80

Calls 3

snip_method_numbaFunction · 0.90
fit_spectrumFunction · 0.90

Tested by

no test coverage detected