How to use the pyxrf.core.fitting.fit_spectrum function in pyxrf

To help you get started, we’ve selected a few pyxrf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NSLS-II / PyXRF / pyxrf / xanes_maps / xanes_maps_api.py View on Github external
# The number of averaged points
                n_averaged_pts = (xd_px_max - xd_px_min + 1) * (yd_px_max - yd_px_min + 1)

                plot_comments = ""
                if n_averaged_pts > 1:
                    plot_comments = f"Area: {n_averaged_pts} px."

                fit_real_time = True
                if plot_single_point and (self.incident_energy_shift_keV ==
                                          self.incident_energy_shift_keV_updated):
                    # fit_real_time = False
                    fit_real_time = True  # For now let's always do fitting on the fly
                if fit_real_time:
                    # Interpolate references (same function as in the main processing routine)
                    refs = _interpolate_references(self.energy, self.ref_energy, self.ref_data)
                    xanes_fit_pt, xanes_fit_rfactor, _ = fit_spectrum(data_selected,
                                                                      refs,
                                                                      method=self.fitting_method,
                                                                      rate=self.fitting_descent_rate)
                else:
                    # Use precomputed data
                    xanes_fit_pt = self.xanes_map_data[:, yd_px_min, xd_px_min]
                    # We still compute R-factor
                    xanes_fit_rfactor = rfactor_compute(data_selected, xanes_fit_pt, self.absorption_refs)

                # Compute fit results represented in counts (for output)
                xanes_fit_pt_counts = xanes_fit_pt.copy()
                for n, v in enumerate(xanes_fit_pt):
                    xanes_fit_pt_counts[n] = v * np.sum(self.absorption_refs[:, n])

                for n, v in enumerate(xanes_fit_pt_counts):
                    plot_comments += f"\n{self.ref_labels[n]}: {xanes_fit_pt_counts[n]:.2f} c."
github NSLS-II / PyXRF / pyxrf / core / map_processing.py View on Github external
if use_snip:
        bg_sel = np.apply_along_axis(snip_method_numba, 2, spec_sel,
                                     snip_param['e_offset'],
                                     snip_param['e_linear'],
                                     snip_param['e_quadratic'],
                                     width=snip_param['b_width'])

        y = spec_sel - bg_sel
        bg_sum = np.sum(bg_sel, axis=2)

    else:
        y = spec_sel
        bg_sum = np.zeros(shape=data.shape[0:2])

    weights, rfactor, _ = fit_spectrum(y, matv, axis=2, method="nnls")

    total_cnt = np.sum(spec, axis=2)
    sel_cnt = np.sum(spec_sel, axis=2)

    # Stack depth-wise (along axis 2)
    data_out = np.dstack((weights, bg_sum, rfactor, sel_cnt, total_cnt))

    return data_out
github NSLS-II / PyXRF / pyxrf / xanes_maps / xanes_maps_api.py View on Github external
# Align the stack of images
    if alignment_enable:
        eline_data_aligned = _align_stacks(eline_data=eline_data,
                                           eline_alignment=eline_alignment,
                                           alignment_starts_from=alignment_starts_from)
        logger.info("Alignment of the image stack: success.")
    else:
        eline_data_aligned = eline_data
        logger.info("Alignment of the image stack: skipped.")

    if seq_generate_xanes_map:
        scan_absorption_refs = _interpolate_references(scan_energies_shifted, ref_energy, ref_data)

        logger.info(f"Fitting XANES specta using '{fitting_method}' method")
        xanes_map_data, xanes_map_rfactor, _ = fit_spectrum(eline_data_aligned[eline_selected],
                                                            scan_absorption_refs,
                                                            method=fitting_method,
                                                            rate=fitting_descent_rate)

        # Scale xanes maps so that the values represent counts
        n_refs, _, _ = xanes_map_data.shape
        xanes_map_data_counts = np.zeros(shape=xanes_map_data.shape)
        for n in range(n_refs):
            xanes_map_data_counts[n, :, :] = xanes_map_data[n, :, :] * np.sum(scan_absorption_refs[:, n])

        logger.info("XANES fitting: success.")
    else:
        scan_absorption_refs = None
        xanes_map_data = None
        xanes_map_data_counts = None
        xanes_map_rfactor = None