From 1e723129360b6473e279e4e199d55ccc571cf90b Mon Sep 17 00:00:00 2001 From: Gao Date: Mon, 3 Jul 2023 18:37:44 +0200 Subject: [PATCH] add the commits to the fit analyser --- Analyser/FitAnalyser.py | 180 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 175 insertions(+), 5 deletions(-) diff --git a/Analyser/FitAnalyser.py b/Analyser/FitAnalyser.py index 31ba73b..0115501 100644 --- a/Analyser/FitAnalyser.py +++ b/Analyser/FitAnalyser.py @@ -554,7 +554,7 @@ class FitAnalyser(): 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. + """Call the guess function of the fit model to guess the initial value. :param dataArray: The data for the fit :type dataArray: xarray DataArray @@ -565,7 +565,7 @@ class FitAnalyser(): :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 + :type input_core_dims: list or array like, 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 @@ -574,8 +574,8 @@ class FitAnalyser(): :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_ + :return: The guessed initial parameters for the fit + :rtype: xarray DataArray """ kwargs.update( @@ -667,14 +667,60 @@ class FitAnalyser(): ) def _fit_1D(self, data, params, x): + """Call the fit function of the 1D fit model to do the fit. + + :param data: The data to fit + :type data: 1D numpy array + :param params: The initial paramters of the fit + :type params: lmfit Parameters + :param x: The data of x axis + :type x: 1D numpy array + :return: The result of the fit + :rtype: lmfit FitResult + """ return self.fitModel.fit(data=data, x=x, params=params, nan_policy='omit') def _fit_2D(self, data, params, x, y): + """Call the fit function of the 2D fit model to do the fit. + + :param data: The flattened data to fit + :type data: 1D numpy array + :param params: The flattened initial paramters of the fit + :type params: lmfit Parameters + :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 result of the fit + :rtype: lmfit FitResult + """ data = data.flatten(order='F') return self.fitModel.fit(data=data, x=x, y=y, params=params, nan_policy='omit') def fit(self, dataArray, paramsArray, x=None, y=None, input_core_dims=None, dask='parallelized', vectorize=True, keep_attrs=True, daskKwargs=None, **kwargs): - + """Call the fit function of the fit model to do the fit. + + :param dataArray: The data for the fit + :type dataArray: xarray DataArray + :param paramsArray: The flattened initial paramters of the fit + :type paramsArray: xarray DataArray or lmfit Parameters + :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 input_core_dims: over write of the same argument in xarray.apply_ufunc, defaults to None, defaults to None + :type input_core_dims: list or array like, optional + :param dask: over write of the same argument in xarray.apply_ufunc, defaults to None, defaults to 'parallelized' + :type dask: str, optional + :param vectorize: over write of the same argument in xarray.apply_ufunc, defaults to None, defaults to True + :type vectorize: bool, optional + :param keep_attrs: over write of the same argument in xarray.apply_ufunc, defaults to None, defaults to True + :type keep_attrs: bool, optional + :param daskKwargs: over write of the same argument in xarray.apply_ufunc, defaults to None, defaults to None + :type daskKwargs: dict, optional + :return: The fit result + :rtype: xarray DataArray + """ kwargs.update( { "dask": dask, @@ -816,13 +862,56 @@ class FitAnalyser(): **kwargs) def _eval_1D(self, fitResult, x): + """Call the eval function of the 1D fit model to calculate the curve. + + :param fitResult: The result of fit + :type fitResult: lmfit FitResult + :param x: The data of x axis + :type x: 1D numpy array + :return: The curve based on the fit result + :rtype: 1D numpy array + """ return self.fitModel.eval(x=x, **fitResult.best_values) def _eval_2D(self, fitResult, x, y, shape): + """Call the eval function of the 2D fit model to calculate the curve. + + :param fitResult: The result of fit + :type fitResult: lmfit FitResult + :param x: The data of x axis + :type x: 1D numpy array + :param y: The flattened data of y axis + :type y: 1D numpy array + :param shape: The desired shape for output + :type shape: list, tuple or array like + :return: The curve based on the fit result + :rtype: 2D numpy array + """ res = self.fitModel.eval(x=x, y=y, **fitResult.best_values) return res.reshape(shape, order='F') def eval(self, fitResultArray, x=None, y=None, output_core_dims=None, prefix="", dask='parallelized', vectorize=True, daskKwargs=None, **kwargs): + """_summary_ + + :param fitResultArray: The result of fit + :type fitResultArray: 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 output_core_dims: over write of the same argument in xarray.apply_ufunc, defaults to None + :type output_core_dims: list, optional + :param prefix: prefix str of the fit model, defaults to "" + :type prefix: str, 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 daskKwargs: over write of the same argument in xarray.apply_ufunc, defaults to None + :type daskKwargs: dict, optional + :return: The curve based on the fit result + :rtype: xarray + """ kwargs.update( { @@ -896,9 +985,27 @@ class FitAnalyser(): return res.assign_coords({prefix+'x':np.array(x), prefix+'y':np.array(y)}) def _get_fit_value_single(self, fitResult, key): + """get value of one single parameter from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param key: The name of the parameter + :type key: str + :return: The vaule of the parameter + :rtype: float + """ return fitResult.params[key].value def _get_fit_value(self, fitResult, params): + """get value of parameters from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param params: A list of the name of paramerters to read + :type params: list or array like + :return: The vaule of the parameter + :rtype: 1D numpy array + """ func = np.vectorize(self._get_fit_value_single) res = tuple( func(fitResult, key) @@ -908,6 +1015,15 @@ class FitAnalyser(): return res def get_fit_value(self, fitResult, dask='parallelized', **kwargs): + """get value of parameters from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param dask: over write of the same argument in xarray.apply_ufunc, defaults to 'parallelized' + :type dask: str, optional + :return: The vaule of the parameter + :rtype: xarray DataArray + """ firstIndex = { key: fitResult[key][0] for key in fitResult.dims @@ -938,9 +1054,27 @@ class FitAnalyser(): return value def _get_fit_std_single(self, fitResult, key): + """get standard deviation of one single parameter from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param key: The name of the parameter + :type key: str + :return: The vaule of the parameter + :rtype: float + """ return fitResult.params[key].stderr def _get_fit_std(self, fitResult, params): + """get standard deviation of parameters from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param params: A list of the name of paramerters to read + :type params: list or array like + :return: The vaule of the parameter + :rtype: 1D numpy array + """ func = np.vectorize(self._get_fit_std_single) res = tuple( func(fitResult, key) @@ -950,6 +1084,15 @@ class FitAnalyser(): return res def get_fit_std(self, fitResult, dask='parallelized', **kwargs): + """get standard deviation of parameters from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param dask: over write of the same argument in xarray.apply_ufunc, defaults to 'parallelized' + :type dask: str, optional + :return: The vaule of the parameter + :rtype: xarray DataArray + """ firstIndex = { key: fitResult[key][0] for key in fitResult.dims @@ -980,6 +1123,15 @@ class FitAnalyser(): return value def _get_fit_full_result_single(self, fitResult, key): + """get the full result of one single parameter from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param key: The name of the parameter + :type key: str + :return: The vaule of the parameter + :rtype: float + """ if not fitResult.params[key].value is None: value = fitResult.params[key].value @@ -994,6 +1146,15 @@ class FitAnalyser(): return ufloat(value, std) def _get_fit_full_result(self, fitResult, params): + """get the full result of parameters from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param params: A list of the name of paramerters to read + :type params: list or array like + :return: The vaule of the parameter + :rtype: 1D numpy array + """ func = np.vectorize(self._get_fit_full_result_single) res = tuple( func(fitResult, key) @@ -1003,6 +1164,15 @@ class FitAnalyser(): return res def get_fit_full_result(self, fitResult, dask='parallelized', **kwargs): + """get the full result of parameters from fit result + + :param fitResult: The result of the fit + :type fitResult: lmfit FitResult + :param dask: over write of the same argument in xarray.apply_ufunc, defaults to 'parallelized' + :type dask: str, optional + :return: The vaule of the parameter + :rtype: xarray DataArray + """ firstIndex = { key: fitResult[key][0] for key in fitResult.dims