Spectrum fitting for a block of XRF dataset. The function is intended to be called using `map_blocks` function for parallel processing using Dask distributed package. Parameters ---------- data : ndarray block of an XRF dataset. Shape=(ny, nx, ne). data_sel_indi
(data, data_sel_indices, matv, snip_param, use_snip)
| 660 | |
| 661 | |
| 662 | def _fit_xrf_block(data, data_sel_indices, matv, snip_param, use_snip): |
| 663 | """ |
| 664 | Spectrum fitting for a block of XRF dataset. The function is intended to be |
| 665 | called using `map_blocks` function for parallel processing using Dask distributed |
| 666 | package. |
| 667 | |
| 668 | Parameters |
| 669 | ---------- |
| 670 | data : ndarray |
| 671 | block of an XRF dataset. Shape=(ny, nx, ne). |
| 672 | data_sel_indices: tuple |
| 673 | tuple `(n_start, n_end)` which defines the indices along axis 2 of `data` array |
| 674 | that are used for fitting. Note that `ne` (in `data`) and `ne_model` (in `matv`) |
| 675 | are not equal. But `n_end - n_start` MUST be equal to `ne_model`! Indexes |
| 676 | `n_start .. n_end - 1` will be selected from each pixel. |
| 677 | matv: ndarray |
| 678 | Matrix of spectra of the selected elements (emission lines). Shape=(ne_model, n_lines) |
| 679 | snip_param: dict |
| 680 | Dictionary of parameters forwarded to 'snip' method for background removal. |
| 681 | Keys: `e_offset`, `e_linear`, `e_quadratic` (parameters of the energy axis approximation), |
| 682 | `b_width` (width of the window that defines resolution of the snip algorithm). |
| 683 | use_snip: bool, optional |
| 684 | enable/disable background removal using snip algorithm |
| 685 | |
| 686 | Returns |
| 687 | ------- |
| 688 | data_out: ndarray |
| 689 | array with fitting results. Shape: `(ny, nx, ne_model + 4)`. For each pixel |
| 690 | the output data contains: `ne_model` values that represent area under the emission |
| 691 | line spectra; background area (only in the selected energy range), error (R-factor), |
| 692 | total count in the selected energy range, total count of the full experimental spectrum. |
| 693 | """ |
| 694 | spec = data |
| 695 | spec_sel = spec[:, :, data_sel_indices[0] : data_sel_indices[1]] |
| 696 | |
| 697 | if use_snip: |
| 698 | bg_sel = np.apply_along_axis( |
| 699 | snip_method_numba, |
| 700 | 2, |
| 701 | spec_sel, |
| 702 | snip_param["e_offset"], |
| 703 | snip_param["e_linear"], |
| 704 | snip_param["e_quadratic"], |
| 705 | width=snip_param["b_width"], |
| 706 | ) |
| 707 | |
| 708 | y = spec_sel - bg_sel |
| 709 | bg_sum = np.sum(bg_sel, axis=2) |
| 710 | |
| 711 | else: |
| 712 | y = spec_sel |
| 713 | bg_sum = np.zeros(shape=data.shape[0:2]) |
| 714 | |
| 715 | weights, rfactor, _ = fit_spectrum(y, matv, axis=2, method="nnls") |
| 716 | |
| 717 | total_cnt = np.sum(spec, axis=2) |
| 718 | sel_cnt = np.sum(spec_sel, axis=2) |
| 719 |