xrf_model
Module¶
-
class
skbeam.core.fitting.xrf_model.
ElementModel
(*args, **kwargs)¶ Bases:
lmfit.model.Model
Wrap the element_peak_xrf function for fitting within lmfit framework
This is a function to construct xrf element peak, which is based on gauss profile, but more specific requirements need to be considered. For instance, the standard deviation is replaced by global fitting parameters, and energy calibration on x is taken into account.
- Parameters
- xarray
independent variable, channel number instead of energy
- areafloat
area of gaussian function
- centerfloat
center position
- delta_centerfloat
adjustment to center position
- delta_sigmafloat
adjustment to standard deviation
- ratiofloat
branching ratio
- ratio_adjustfloat
value used to adjust peak height
- fwhm_offsetfloat
global fitting parameter for peak width
- fwhm_fanoprimefloat
global fitting parameter for peak width
- e_offsetfloat
offset of energy calibration
- e_linearfloat
linear coefficient in energy calibration
- e_quadraticfloat
quadratic coefficient in energy calibration
- Returns
- array:
gaussian peak profile
-
class
skbeam.core.fitting.xrf_model.
ModelSpectrum
(params, elemental_lines)¶ Bases:
object
Construct Fluorescence spectrum which includes elastic peak, compton and element peaks.
-
assemble_models
(self)¶ Put all models together to form a spectrum.
-
model_fit
(self, channel_number, spectrum, weights=None, method='leastsq', **kwargs)¶ - Parameters
- channel_numberarray
independent variable
- spectrumarray
intensity
- weightsarray, optional
weight for fitting
- methodstr
default as leastsq
- kwargsdict
fitting criteria, such as max number of iteration
- Returns
- result object from lmfit
-
setup_compton_model
(self)¶ setup parameters related to Compton model
-
setup_elastic_model
(self)¶ setup parameters related to Elastic model
-
setup_element_model
(self, elemental_line, default_area=100000.0)¶ Construct element model. Elemental line that can be add include elemental peak, pileup peak and user peak. User peak is a fake peak, but can be used as a quick replacement for pileup peaks, escape peaks or other unknown peaks due to detector defects or scattering process.
- Parameters
- elemental_linestr
elemental line, such as ‘Fe_K’ pileup peak, such as ‘Si_Ka1-Si_Ka1’ user peak, such as ‘user_peak1’, ‘user_peak2’
- default_areafloat, optional
value for the initial area of a given element default is 1e5, found to be a good value
-
-
class
skbeam.core.fitting.xrf_model.
ParamController
(params, elemental_lines)¶ Bases:
object
Update element peak information in parameter dictionary. This is an important step in dynamical fitting.
-
add_param
(self, kind, element, constraint=None)¶ Create a Parameter controlling peak position, width, branching ratio, or area.
- Parameters
- kind{‘pos’, ‘width’, ‘ratio’, ‘area’}
- elementstr
element name
- constraint{‘lo’, ‘hi’, ‘lohi’, ‘fixed’, ‘none’}, optional
default “bound strategy” (fitting constraints)
- .. note:: Element that can be add include elemental peak, pileup peak and user peak.
- User peak is a fake peak, but can be used as a quick replacement for pileup peaks,
- escape peaks or other unknown peaks due to detector defects or scattering process.
-
set_strategy
(self, strategy_name)¶ Update the default value of bounds.
- Parameters
- strategy_namestr
fitting strategy that this Model will use
-
update_element_prop
(self, element_list, **kwargs)¶ Update element properties, such as pos, width, area and ratio.
- Parameters
- element_listlist
define which element to update
- kwargsdict
define what kind of property to change
- Returns
- dictupdated value
-
-
skbeam.core.fitting.xrf_model.
calculate_area
(e_select, matv, results, param, first_peak_area=False)¶ - Parameters
- e_selectlist
elements
- matv2D array
matrix constains elemental profile as columns
- results3D array
x, y positions, and each element’s weight on third dim
- paramdict
parameters of fitting
- first_peak_areaBool, optional
get overal peak area or only the first peak area, such as Ar_Ka1
- Returns
- dict :
dict of each 2D elemental distribution
-
skbeam.core.fitting.xrf_model.
compute_escape_peak
(spectrum, ratio, params, escape_e=1.73998)¶ Calculate the escape peak for given detector.
- Parameters
- spectrumarray
original, uncorrected spectrum
- ratiofloat
ratio of shadow to full spectrum
- paramdict
fitting parameters
- escape_efloat
Units: keV By default, 1.73998 (Ka1 line of Si)
- Returns
- array:
x after shift, and adjusted y
-
skbeam.core.fitting.xrf_model.
construct_linear_model
(channel_number, params, elemental_lines, default_area=100)¶ Create spectrum with parameters given from params.
- Parameters
- channel_numberarray
N.B. This is the raw independent variable, not energy.
- paramsdict
fitting parameters
- elemental_lineslist
e.g., [‘Na_K’, Mg_K’, ‘Pt_M’] refers to the K lines of Sodium, the K lines of Magnesium, and the M lines of Platinum
- default_areafloat
value for the initial area of a given element
- Returns
- selected_elementslist
selected elements for given energy
- matvarray
matrix for linear fitting
- element_areadict
area of the given elements
-
skbeam.core.fitting.xrf_model.
define_range
(data, low, high, a0, a1)¶ Cut x range based on offset and linear term of a linear function. a0 and a1 should be defined in param_dict.
- Parameters
- data1D array
raw spectrum
- lowfloat
low bound in KeV
- highfloat
high bound in KeV
- a0float
offset term of energy calibration
- a1float
linear term of energy calibration
- Returns
- xarray
trimmed channel number
- yarray
trimmed spectrum according to x
-
skbeam.core.fitting.xrf_model.
element_peak_xrf
(x, area, center, delta_center, delta_sigma, ratio, ratio_adjust, fwhm_offset, fwhm_fanoprime, e_offset, e_linear, e_quadratic, epsilon=2.96)¶ This is a function to construct xrf element peak, which is based on gauss profile, but more specific requirements need to be considered. For instance, the standard deviation is replaced by global fitting parameters, and energy calibration on x is taken into account.
- Parameters
- xarray
independent variable, channel number instead of energy
- areafloat
area of gaussian function
- centerfloat
center position
- delta_centerfloat
adjustment to center position
- delta_sigmafloat
adjustment to standard deviation
- ratiofloat
branching ratio
- ratio_adjustfloat
value used to adjust peak height
- fwhm_offsetfloat
global fitting parameter for peak width
- fwhm_fanoprimefloat
global fitting parameter for peak width
- e_offsetfloat
offset of energy calibration
- e_linearfloat
linear coefficient in energy calibration
- e_quadraticfloat
quadratic coefficient in energy calibration
- Returns
- array:
gaussian peak profile
-
skbeam.core.fitting.xrf_model.
extract_strategy
(param, name)¶ Extract given strategy from param dict.
- Parameters
- paramdict
saving all parameters for data fitting
- namestr
strategy name
- Returns
- dict :
with given strategy as value
-
skbeam.core.fitting.xrf_model.
fit_per_line_nnls
(data, matv, param, use_snip)¶ Fit experiment data for a given row using nnls algorithm.
- Parameters
- dataarray
selected one row of experiment spectrum
- matvarray
matrix for regression analysis
- paramdict
fitting parameters
- use_snipbool
use snip algorithm to remove background or not
- Returns
- array :
fitting values for all the elements at a given row. Background is calculated as a summed value. Also residual is included.
-
skbeam.core.fitting.xrf_model.
fit_pixel_multiprocess_nnls
(exp_data, matv, param, use_snip=False)¶ Multiprocess fit of experiment data.
- Parameters
- exp_dataarray
3D data of experiment spectrum, with x,y positions as the first 2-dim, and energy as the third one.
- matvarray
matrix for regression analysis
- paramdict
fitting parameters
- use_snipbool, optional
use snip algorithm to remove background or not
- Returns
- array
Fitting values for all the elements
-
skbeam.core.fitting.xrf_model.
get_activated_lines
(incident_energy, elemental_lines)¶ - Parameters
- incident_energyfloat
beam energy
- element_lineslist
e.g., [‘Na_K’, ‘Mg_K’, ‘Pt_M’]
- Returns
- list
all activated lines for given elements
-
skbeam.core.fitting.xrf_model.
get_line_energy
(elemental_line)¶ Return the energy of the first line in K, L or M series.
- Parameters
- elemental_linestr
For instance, Eu_L is the format for L lines and Pt_M for M lines. And for K lines, user needs to define lines like ka1, kb1, because for K lines, we consider contributions from either ka1 or kb1, while for L or M lines, we only consider the primary peak.
- Returns
- float :
energy of emission line
-
skbeam.core.fitting.xrf_model.
get_relative_cs_ratio
(elemental_line, energy)¶ At given energy, multiple elemental lines may be activated. This function is used to calculate the ratio of the first line’s cross section (cs) to the summed cross section from all the lines.
- Parameters
- elemental_linestr
e.g., ‘Mg_K’, refers to the K lines of Magnesium
- energyfloat
incident energy in keV
- Returns
- float :
calculated ratio
-
skbeam.core.fitting.xrf_model.
linear_spectrum_fitting
(x, y, params, elemental_lines=None, weights=None)¶ Fit a spectrum to a linear model.
This is a convenience function that wraps up construct_linear_model and nnls_fit.
- Parameters
- xarray
channel array
- yarray
spectrum intensity
- paramdict
fitting parameters
- elemental_lineslist, optional
e.g., [‘Na_K’, Mg_K’, ‘Pt_M’] refers to the K lines of Sodium, the K lines of Magnesium, and the M lines of Platinum. If elemental_lines is set as None, all the possible lines activated at given energy will be used.
- weightsarray, optional
for weighted nnls fitting. Setting weights as None means fitting without weights.
- Returns
- x_energyarray
x axis with unit in energy
- result_dictdict
Fitting results
- area_dictdict
the area of the first main peak, such as Ka1, of a given element
-
skbeam.core.fitting.xrf_model.
nnls_fit
(spectrum, expected_matrix, weights=None)¶ Non-negative least squares fitting.
- Parameters
- spectrumarray
spectrum of experiment data
- expected_matrixarray
2D matrix of activated element spectrum
- weightsarray, optional
for weighted nnls fitting. Setting weights as None means fitting without weights.
- Returns
- resultsarray
weights of different element
- residuefloat
error
Note
nnls is chosen as amplitude of each element should not be negative. ..
-
skbeam.core.fitting.xrf_model.
register_strategy
(key, strategy, overwrite=True)¶ Register new strategy.
- Parameters
- keystr
strategy name
- strategydict
bound for every parameter
-
skbeam.core.fitting.xrf_model.
set_parameter_bound
(param, bound_option, extra_config=None)¶ Update the default value of bounds.
Warning
This function mutates the input values.
- Parameters
- paramdict
saving all the fitting values and their bounds
- bound_optionstr
define bound type
- extra_configdict
strategy-specific configuration
-
skbeam.core.fitting.xrf_model.
sum_area
(elemental_line, result_val)¶ Return the total area for given element.
- Parameters
- elemental_linestr
name of a given element line, such as Na_K
- result_valobj
result obj from lmfit to save all the fitting results
- Returns
- sumvfloat
the total area
-
skbeam.core.fitting.xrf_model.
trim
(x, y, low, high)¶ Mask two arrays applying bounds to the first array.
- Parameters
- xarray
independent variable
- yarray
dependent variable
- lowfloat
low bound
- highfloat
high bound
- Returns
- array :
x with new range
- array :
y with new range
-
skbeam.core.fitting.xrf_model.
update_parameter_dict
(param, fit_results)¶ Update fitting parameters dictionary according to given fitting results, usually obtained from previous run.
Warning
This function mutates param.
- Parameters
- paramdict
saving all the fitting values and their bounds
- fit_resultsobject
ModelFit object from lmfit