def linear_spectrum_fitting(x, y, params,
                            elemental_lines=None,
                            weights=None):
    if elemental_lines is None:
        elemental_lines = K_LINE + L_LINE + M_LINE

    # Need to use deepcopy here to avoid unexpected change on parameter dict
    fitting_parameters = copy.deepcopy(params)

    total_list, matv, element_area = construct_linear_model(x, params,
                                                            elemental_lines)

    # get background
    bg = snip_method(y, fitting_parameters['e_offset']['value'],
                     fitting_parameters['e_linear']['value'],
                     fitting_parameters['e_quadratic']['value'],
                     width=fitting_parameters['non_fitting_values']['background_width'])
    y = y - bg

    out, res = nnls_fit(y, matv, weights=weights)

    total_y = out * matv
    total_y = np.transpose(total_y)

    x_energy = (params['e_offset']['value'] +
                params['e_linear']['value']*x +
                params['e_quadratic']['value'] * x**2)

    area_dict = OrderedDict()
    result_dict = OrderedDict()

    for name, y, _out in zip(total_list, total_y, out):
        if np.sum(y) == 0:
            continue
        result_dict[name] = y
        area_dict[name] = _out * element_area[name]

    result_dict['background'] = bg
    area_dict['background'] = np.sum(bg)
    return x_energy, result_dict, area_dict
Fit a spectrum to a linear model.
dataset/ETHPy150Open scikit-beam/scikit-beam/skbeam/core/fitting/xrf_model.py/linear_spectrum_fitting