erlab.accessors.fit¶
Classes
|
|
|
|
|
|
- class erlab.accessors.fit.ModelFitDataArrayAccessor(xarray_obj)[source]¶
Bases:
ERLabDataArrayAccessor
xarray.DataArray.modelfit
accessor for fitting lmfit models.- __call__(*args, **kwargs)[source]¶
Curve fitting optimization for arbitrary models.
Wraps
lmfit.Model.fit
withxarray.apply_ufunc()
.- Parameters:
coords (
Hashable
,xarray.DataArray
, orSequence
ofHashable
orxarray.DataArray
) – Independent coordinate(s) over which to perform the curve fitting. Must share at least one dimension with the calling object. When fitting multi-dimensional functions, supplycoords
as a sequence in the same order as arguments infunc
. To fit along existing dimensions of the calling object,coords
can also be specified as a str or sequence of strs.model (
lmfit.Model
) – A model object to fit to the data. The model must be an instance oflmfit.Model
.reduce_dims (
str
,Iterable
ofHashable
orNone
, optional) – Additional dimension(s) over which to aggregate while fitting. For example, callingds.modelfit(coords='time', reduce_dims=['lat', 'lon'], ...)
will aggregate all lat and lon points and fit the specified function along the time dimension.skipna (
bool
, default:True
) – Whether to skip missing values when fitting. Default is True.params (
lmfit.Parameters
,dict-like
, orxarray.DataArray
, optional) – Optional input parameters to the fit. If almfit.Parameters
object, it will be used for all fits. If a dict-like object, it must look like the keyword arguments tolmfit.create_params
. Additionally, each value of the dictionary may also be a DataArray, which will be broadcasted appropriately. If a DataArray, each entry must be a dictionary-like object, almfit.Parameters
object, or a JSON string created withlmfit.Parameters.dumps
. If given a Dataset, the name of the data variables in the Dataset must match the name of the data variables in the calling object, and each data variable will be used as the parameters for the corresponding data variable. If none or only some parameters are passed, the rest will be assigned initial values and bounds withlmfit.Model.make_params
, or guessed withlmfit.Model.guess
ifguess
isTrue
.guess (
bool
, default:False
) – Whether to guess the initial parameters withlmfit.Model.guess
. For composite models, the parameters will be guessed for each component.errors (
{"raise", "ignore"}
, default:"raise"
) – If'raise'
, any errors from thelmfit.Model.fit
optimization will raise an exception. If'ignore'
, the return values for the coordinates where the fitting failed will be NaN.parallel (
bool
, optional) – Whether to parallelize the fits over the data variables. If not provided, parallelization is only applied for non-dask Datasets with more than 200 data variables.parallel_kw (
dict
, optional) – Additional keyword arguments to pass to the parallelization backendjoblib.Parallel
ifparallel
isTrue
.progress (
bool
, default:False
) – Whether to show a progress bar for fitting over data variables. Only useful if there are multiple data variables to fit.output_result (
bool
, default:True
) – Whether to include the fulllmfit.model.ModelResult
object in the output dataset. IfTrue
, the result will be stored in a variable namedmodelfit_results
.**kwargs (optional) – Additional keyword arguments to passed to
lmfit.Model.fit
.
- Returns:
curvefit_results – A single dataset which contains:
- modelfit_results
The full
lmfit.model.ModelResult
object from the fit. Only included ifoutput_result
isTrue
.- modelfit_coefficients
The coefficients of the best fit.
- modelfit_stderr
The standard errors of the coefficients.
- modelfit_covariance
The covariance matrix of the coefficients. Note that elements corresponding to non varying parameters are set to NaN, and the actual size of the covariance matrix may be smaller than the array.
- modelfit_stats
Statistics from the fit. See
lmfit.minimize
.- modelfit_data
Data used for the fit.
- modelfit_best_fit
The best fit data of the fit.
- Return type:
- class erlab.accessors.fit.ModelFitDatasetAccessor(xarray_obj)[source]¶
Bases:
ERLabDatasetAccessor
xarray.Dataset.modelfit
accessor for fitting lmfit models.- __call__(coords, model, reduce_dims=None, skipna=True, params=None, guess=False, errors='raise', parallel=None, parallel_kw=None, progress=False, output_result=True, **kwargs)[source]¶
Curve fitting optimization for arbitrary models.
Wraps
lmfit.Model.fit
withxarray.apply_ufunc()
.- Parameters:
coords (
Hashable
,xarray.DataArray
, orSequence
ofHashable
orxarray.DataArray
) – Independent coordinate(s) over which to perform the curve fitting. Must share at least one dimension with the calling object. When fitting multi-dimensional functions, supplycoords
as a sequence in the same order as arguments infunc
. To fit along existing dimensions of the calling object,coords
can also be specified as a str or sequence of strs.model (
lmfit.Model
) – A model object to fit to the data. The model must be an instance oflmfit.Model
.reduce_dims (
str
,Iterable
ofHashable
orNone
, optional) – Additional dimension(s) over which to aggregate while fitting. For example, callingds.modelfit(coords='time', reduce_dims=['lat', 'lon'], ...)
will aggregate all lat and lon points and fit the specified function along the time dimension.skipna (
bool
, default:True
) – Whether to skip missing values when fitting. Default is True.params (
lmfit.Parameters
,dict-like
, orxarray.DataArray
, optional) – Optional input parameters to the fit. If almfit.Parameters
object, it will be used for all fits. If a dict-like object, it must look like the keyword arguments tolmfit.create_params
. Additionally, each value of the dictionary may also be a DataArray, which will be broadcasted appropriately. If a DataArray, each entry must be a dictionary-like object, almfit.Parameters
object, or a JSON string created withlmfit.Parameters.dumps
. If given a Dataset, the name of the data variables in the Dataset must match the name of the data variables in the calling object, and each data variable will be used as the parameters for the corresponding data variable. If none or only some parameters are passed, the rest will be assigned initial values and bounds withlmfit.Model.make_params
, or guessed withlmfit.Model.guess
ifguess
isTrue
.guess (
bool
, default:False
) – Whether to guess the initial parameters withlmfit.Model.guess
. For composite models, the parameters will be guessed for each component.errors (
{"raise", "ignore"}
, default:"raise"
) – If'raise'
, any errors from thelmfit.Model.fit
optimization will raise an exception. If'ignore'
, the return values for the coordinates where the fitting failed will be NaN.parallel (
bool
, optional) – Whether to parallelize the fits over the data variables. If not provided, parallelization is only applied for non-dask Datasets with more than 200 data variables.parallel_kw (
dict
, optional) – Additional keyword arguments to pass to the parallelization backendjoblib.Parallel
ifparallel
isTrue
.progress (
bool
, default:False
) – Whether to show a progress bar for fitting over data variables. Only useful if there are multiple data variables to fit.output_result (
bool
, default:True
) – Whether to include the fulllmfit.model.ModelResult
object in the output dataset. IfTrue
, the result will be stored in a variable named[var]_modelfit_results
.**kwargs (optional) – Additional keyword arguments to passed to
lmfit.Model.fit
.
- Returns:
curvefit_results – A single dataset which contains:
- [var]_modelfit_results
The full
lmfit.model.ModelResult
object from the fit. Only included ifoutput_result
isTrue
.- [var]_modelfit_coefficients
The coefficients of the best fit.
- [var]_modelfit_stderr
The standard errors of the coefficients.
- [var]_modelfit_covariance
The covariance matrix of the coefficients. Note that elements corresponding to non varying parameters are set to NaN, and the actual size of the covariance matrix may be smaller than the array.
- [var]_modelfit_stats
Statistics from the fit. See
lmfit.minimize
.- [var]_modelfit_data
Data used for the fit.
- [var]_modelfit_best_fit
The best fit data of the fit.
- Return type:
- class erlab.accessors.fit.ParallelFitDataArrayAccessor(xarray_obj)[source]¶
Bases:
ERLabDataArrayAccessor
xarray.DataArray.parallel_fit
accessor for fitting lmfit models in parallel.- __call__(dim, model, **kwargs)[source]¶
Fit the specified model to the data along the given dimension.
- Parameters:
dim (
str
) – The name of the dimension along which to fit the model.model (
lmfit.Model
) – The model to fit.**kwargs (
dict
) – Additional keyword arguments to be passed toxarray.Dataset.modelfit
.
- Returns:
curvefit_results – The dataset containing the results of the fit. See
xarray.DataArray.modelfit
for details.- Return type: