erlab.accessors.fit

Classes

ModelFitDataArrayAccessor(xarray_obj)

xarray.DataArray.modelfit accessor for fitting lmfit models.

ModelFitDatasetAccessor(xarray_obj)

xarray.Dataset.modelfit accessor for fitting lmfit models.

ParallelFitDataArrayAccessor(xarray_obj)

xarray.DataArray.parallel_fit accessor for fitting lmfit models in parallel.

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 with xarray.apply_ufunc().

Parameters:
  • coords (Hashable, xarray.DataArray, or Sequence of Hashable or xarray.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, supply coords as a sequence in the same order as arguments in func. 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 of lmfit.Model.

  • reduce_dims (str, Iterable of Hashable or None, optional) – Additional dimension(s) over which to aggregate while fitting. For example, calling ds.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, or xarray.DataArray, optional) – Optional input parameters to the fit. If a lmfit.Parameters object, it will be used for all fits. If a dict-like object, it must look like the keyword arguments to lmfit.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, a lmfit.Parameters object, or a JSON string created with lmfit.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 with lmfit.Model.make_params, or guessed with lmfit.Model.guess if guess is True.

  • guess (bool, default: False) – Whether to guess the initial parameters with lmfit.Model.guess. For composite models, the parameters will be guessed for each component.

  • errors ({"raise", "ignore"}, default: "raise") – If 'raise', any errors from the lmfit.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 backend joblib.Parallel if parallel is True.

  • 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 full lmfit.model.ModelResult object in the output dataset. If True, the result will be stored in a variable named modelfit_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 if output_result is True.

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:

xarray.Dataset

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 with xarray.apply_ufunc().

Parameters:
  • coords (Hashable, xarray.DataArray, or Sequence of Hashable or xarray.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, supply coords as a sequence in the same order as arguments in func. 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 of lmfit.Model.

  • reduce_dims (str, Iterable of Hashable or None, optional) – Additional dimension(s) over which to aggregate while fitting. For example, calling ds.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, or xarray.DataArray, optional) – Optional input parameters to the fit. If a lmfit.Parameters object, it will be used for all fits. If a dict-like object, it must look like the keyword arguments to lmfit.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, a lmfit.Parameters object, or a JSON string created with lmfit.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 with lmfit.Model.make_params, or guessed with lmfit.Model.guess if guess is True.

  • guess (bool, default: False) – Whether to guess the initial parameters with lmfit.Model.guess. For composite models, the parameters will be guessed for each component.

  • errors ({"raise", "ignore"}, default: "raise") – If 'raise', any errors from the lmfit.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 backend joblib.Parallel if parallel is True.

  • 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 full lmfit.model.ModelResult object in the output dataset. If True, 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 if output_result is True.

[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:

xarray.Dataset

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 to xarray.Dataset.modelfit.

Returns:

curvefit_results – The dataset containing the results of the fit. See xarray.DataArray.modelfit for details.

Return type:

xarray.Dataset