grapa.parse.curve_subclasses_utils.FitHandler
- class grapa.parse.curve_subclasses_utils.FitHandler(filename)
Bases:
objectThis class should work as a minimum version, although it has no fit function implemented - see below shild class FitHandlerBasicFunc to see how to do that.
The configuration file is a grapa-style text file, with following attributes (lines of parameters). See example grapa/datatypes/XRF_fitparameters.txt. Each column is a fit “preset”, intended to be ready to be used. Required attributes:
label: used for referencing the preset
roi: list [valuemin, valuemax], range of interest, in units of the data
function: str, name of the fit function, that must be impolemented in a child class. Function names must start with func_
p0: a list, default initial guess for fit parameters. Presumably numeric. str are also possible. For example ‘1/350’, see function p0_interpret_value; or “Background” that will return a interp1d interpolation function over the data stored in preset curve with label “Background”.
fixed: a list of booleans, True if parameter is fixed by default
showingui if the preset is shown in the gui, by default
sum_amp_gaussian: not needed, specific to CurveMCA.
Then, create a subclass containing the fit functions:
fit functions must be named starting with func_
also: overrides needed for additional behaviors (e.g. amplitude in CurveMCA)
also: overrides to add e.g. interpretation of text into numerical values
other needed behaviors
For implementation into Curve subclass, take care of notably:
class variable - FitHandler, or rather subclass of if containing the fit functions of interest
FITHANDLER = FitHandler(os.path.join(_PATHXRD, FITPARAMETERS_FILE))
__init__:
# in __init__, as class itself not defined at instantiation of class variable self.FITHANDLER.set_typecurvefit(CurveXRD) # to adapt with own Curve type
funcListGUI
# fit-related elements out += self.FITHANDLER.funcListGUI(self, **kwargs)
updateFitParam, to provide fit function to Curve.updateFitParam, and in case textual keywords are used in fit parameters for e.g. “Background”.
def updateFitParam(self, *param): # override default behavior, additional things to do param, revert, func = self.FITHANDLER.updateFitParam_before(self, *param) # call base function, including parameter func super().updateFitParam(*param, func=func) # revert callable parameter to its initial string value self.FITHANDLER.updateFitParam_after(self, revert) return True
fit_explicit, so curve action can point curve.fit_explicit and not more complex dependency, and to host the docstring to report in printHelp()
def fit_explicit(self, roi, *args, funcname="", preset_label="", p0_raw=None, **kwargs): # docstring to write here between triple double-quotes return self.FITHANDLER.fit_explicit(self, roi, *args, funcname=funcname, preset_label=preset_label, p0_raw=p0_raw, **kwargs)
printHelp
- __init__(filename)
Methods
__init__(filename)fit(curve, func, roi, p0, fixed)fit_explicit(curve, roi, *_args[, funcname, ...])Fit fit_explicit: fit data, for example a peak of a CurveMCA data.
fit_preset(curve, preset_label[, roi, p0, fixed])Fit fit_preset: fit a Curve, for example a peak of a CurveMCA data.
fit_resamplex(deltax, curve)Change the base of datapoints a fit result is displayed on
funcListGUI(curve, **kwargs)List of options for GUI
funcListGUI_ifnewline(counter, _lbl, _func)by dfault, new line every 4 parameters
funcListGUI_isafit(curve, **_kwargs)If is a fit: updateFitParam, resamplex
funcListGUI_notafit_choicepresets(curve, ...)if not a fit, show fit options
funcListGUI_notafit_preset(curve, preset, ...)Fit using preset settings
funcListGUI_notafit_whichones(curve, **_kwargs)Chooses which preset to show
get_fitfunction(funcname)fit functions must have names starting with func_, and be implemented in a child class
get_preset(label)Returns first preset which label matches label
get_presets(label[, startswith, fitdefined])tolerant input, useful for GUI Curve Actions Returns a list of (idx, preset Curve)
p0_interpret(p0[, curve, interp])Returns a list of parameters, after interpreting textual values e.g. contaiing +, * or /, or background curves.
set_guishowfitfunc(which, fitimmediately[, ...])To make GUI show specific fit functions
set_typecurvefit(typecurvefit)set the type of Curve to be fitted
updateFitParam_after(curve, revert)Small bit of code performed after updateFitParam
updateFitParam_before(curve, *popt)Small bit of code performed before updateFitParam
- fit_explicit(curve, roi, *_args, funcname='', preset_label='', p0_raw=None, **kwargs)
Fit fit_explicit: fit data, for example a peak of a CurveMCA data. Pre-set fit parameters are configured in file indicated at instantiation.
- Parameters:
curve – a Curve object containing the data to fit.
roi – range of interest, in unit of channel.
funcname – name of the fit function. Mandatory.
preset_label – label of the fit preset in the configuration file, if relevant.
p0_raw – p0 values of the fit preset in configuration file, if relevant.
kwargs –
to provide custom initial guess p0. Weird syntax for grapa GUI.
Syntax 1: keywords “p0” and “fixed” are provided. Instances of list.
Syntax 2: to meet limitations of grapa GUI. “0p0”: 1.0, “0fi”: True (i.e. is fixed fit parameter), “1p0”: 2.1, etc. These are later compiled into lists: p0 and fixed
- Returns:
a CurveMCA fit curve
- fit_preset(curve, preset_label, roi='auto', p0='auto', fixed='auto')
Fit fit_preset: fit a Curve, for example a peak of a CurveMCA data. Pre-set fit parameters are configured in file XRF_fitparameters.txt.
- Parameters:
curve – a Curve object contaiing the data to fit
preset_label – see row “label” in config file. e.g. “Cu Ka1,2”, “Ag Ka1,2”
roi – range of interest, in unit of channel. Overwrite preset.
p0 – list, to provide custom initial guess p0. Overwrite preset.
fixed – list of boolean, to provide custom initial guess p0. Overwrite reset.
- Returns:
a CurveMCA fit curve
- fit_resamplex(deltax, curve)
Change the base of datapoints a fit result is displayed on
- funcListGUI(curve, **kwargs)
List of options for GUI
- funcListGUI_ifnewline(counter, _lbl, _func)
by dfault, new line every 4 parameters
- funcListGUI_isafit(curve, **_kwargs)
If is a fit: updateFitParam, resamplex
- funcListGUI_notafit_choicepresets(curve, **_kwargs)
if not a fit, show fit options
- funcListGUI_notafit_preset(curve, preset, **_kwargs)
Fit using preset settings
- funcListGUI_notafit_whichones(curve, **_kwargs)
Chooses which preset to show
- get_fitfunction(funcname)
fit functions must have names starting with func_, and be implemented in a child class
- get_preset(label)
Returns first preset which label matches label
- get_presets(label, startswith=True, fitdefined=True)
tolerant input, useful for GUI Curve Actions Returns a list of (idx, preset Curve)
- p0_interpret(p0, curve=None, interp=True)
Returns a list of parameters, after interpreting textual values e.g. contaiing +, * or /, or background curves
- set_guishowfitfunc(which, fitimmediately, curve: Curve = None)
To make GUI show specific fit functions
- Parameters:
which – label of the function to show
fitimmediately – if True, fit immediately
curve – must be provided
- set_typecurvefit(typecurvefit)
set the type of Curve to be fitted
- updateFitParam_after(curve, revert)
Small bit of code performed after updateFitParam
- updateFitParam_before(curve, *popt)
Small bit of code performed before updateFitParam