|
|
@ -479,8 +479,17 @@ lmfit_models = {'Constant': ConstantModel, |
|
|
|
|
|
|
|
|
|
|
|
class FitAnalyser(): |
|
|
|
"""This is a class integrated all the functions to do a fit. |
|
|
|
""" |
|
|
|
|
|
|
|
def __init__(self, fitModel, fitDim=1, **kwargs) -> None: |
|
|
|
"""Initialize function |
|
|
|
|
|
|
|
:param fitModel: The fitting model of fit function |
|
|
|
:type fitModel: lmfit Model |
|
|
|
:param fitDim: The dimension of the fit, defaults to 1 |
|
|
|
:type fitDim: int, optional |
|
|
|
""" |
|
|
|
|
|
|
|
if isinstance(fitModel, str): |
|
|
|
self.fitModel = lmfit_models[fitModel](**kwargs) |
|
|
@ -490,6 +499,11 @@ class FitAnalyser(): |
|
|
|
self.fitDim = fitDim |
|
|
|
|
|
|
|
def print_params_set_template(self, params=None): |
|
|
|
"""Print a template to manually set the initial parameters of the fit |
|
|
|
|
|
|
|
:param params: An object of Parameters class to print, defaults to None |
|
|
|
:type params: lmfit Paraemters, optional |
|
|
|
""" |
|
|
|
|
|
|
|
if params is None: |
|
|
|
params = self.fitModel.make_params() |
|
|
@ -513,13 +527,56 @@ class FitAnalyser(): |
|
|
|
print(res) |
|
|
|
|
|
|
|
def _guess_1D(self, data, x, **kwargs): |
|
|
|
"""Call the guess function of the 1D fit model to guess the initial value. |
|
|
|
|
|
|
|
:param data: The data to fit |
|
|
|
:type data: 1D numpy array |
|
|
|
:param x: The data of x axis |
|
|
|
:type x: 1D numpy array |
|
|
|
:return: The guessed initial parameters for the fit |
|
|
|
:rtype: lmfit Parameters |
|
|
|
""" |
|
|
|
return self.fitModel.guess(data=data, x=x, **kwargs) |
|
|
|
|
|
|
|
def _guess_2D(self, data, x, y, **kwargs): |
|
|
|
"""Call the guess function of the 2D fit model to guess the initial value. |
|
|
|
|
|
|
|
:param data: The flattened data to fit |
|
|
|
:type data: 1D numpy array |
|
|
|
:param x: The flattened data of x axis |
|
|
|
:type x: 1D numpy array |
|
|
|
:param y: The flattened data of y axis |
|
|
|
:type y: 1D numpy array |
|
|
|
:return: The guessed initial parameters for the fit |
|
|
|
:rtype: lmfit Parameters |
|
|
|
""" |
|
|
|
data = data.flatten(order='F') |
|
|
|
return self.fitModel.guess(data=data, x=x, y=y, **kwargs) |
|
|
|
|
|
|
|
def guess(self, dataArray, x=None, y=None, guess_kwargs={}, input_core_dims=None, dask='parallelized', vectorize=True, keep_attrs=True, daskKwargs=None, **kwargs): |
|
|
|
"""Call the guess function of the 1D fit model to guess the initial value. |
|
|
|
|
|
|
|
:param dataArray: The data for the fit |
|
|
|
:type dataArray: xarray DataArray |
|
|
|
:param x: The name of x axis in data or the data of x axis, defaults to None |
|
|
|
:type x: str or numpy array, optional |
|
|
|
:param y: The name of y axis in data or the data of y axis, defaults to None |
|
|
|
:type y: str or numpy array, optional |
|
|
|
:param guess_kwargs: the keyworded arguments to send to the guess function, defaults to {} |
|
|
|
:type guess_kwargs: dict, optional |
|
|
|
:param input_core_dims: over write of the same argument in xarray.apply_ufunc, defaults to None |
|
|
|
:type input_core_dims: _type_, optional |
|
|
|
:param dask: over write of the same argument in xarray.apply_ufunc,, defaults to 'parallelized' |
|
|
|
:type dask: str, optional |
|
|
|
:param vectorize: over write of the same argument in xarray.apply_ufunc,, defaults to True |
|
|
|
:type vectorize: bool, optional |
|
|
|
:param keep_attrs: over write of the same argument in xarray.apply_ufunc,, defaults to True |
|
|
|
:type keep_attrs: bool, optional |
|
|
|
:param daskKwargs: over write of the same argument in xarray.apply_ufunc,, defaults to None |
|
|
|
:type daskKwargs: dict, optional |
|
|
|
:return: _description_ |
|
|
|
:rtype: _type_ |
|
|
|
""" |
|
|
|
|
|
|
|
kwargs.update( |
|
|
|
{ |
|
|
|