项目作者: jmittelstaedt

项目描述 :
curve fitting in xarray
高级语言: Python
项目地址: git://github.com/jmittelstaedt/xfit.git
创建时间: 2020-09-08T01:32:48Z
项目社区:https://github.com/jmittelstaedt/xfit

开源协议:MIT License

下载


XFit: Curve Fitting in Xarray

Xarray is a package providing labelled
N-dimensional arrays.
This makes it convenient for storing data in a variety of application domains.
Xfit provides an easy way to fit data contained in an xarray object
to an arbitrary nonlinear function using scipy’s curve fit.

Installation

This package is only on github (for now), so you can clone and install using

  1. cd xfit/
  2. python -m pip install -e .

If the dependencies were not installed automatically, they are:

  • python >= 3.6
  • xarray
  • numpy
  • scipy
  • matplotlib

Usage

We’ll briefly describe the two main fitting methods here.

Fitting DataArray’s

We first import the necessary packages and make some dummy data

  1. import numpy as np
  2. import xarray as xr
  3. from xfit import *
  4. # define slopes, intercepts and independent variable coordinates
  5. ms = xr.DataArray(np.arange(6), coords = {'m_true': np.arange(6)}, dims='m_true')
  6. bs = xr.DataArray(np.linspace(0,10,6), coords={'b_true': np.linspace(0,10,6)}, dims='b_true')
  7. xs = xr.DataArray(np.linspace(0,20,101), coords={'x': np.linspace(0,20,101)}, dims='x')
  8. # create data
  9. data_da = xs*ms + bs
  10. data_da.values += 5*np.random.rand(101, 6, 6) # adding some noise
  11. data_da.name = 'data'

We now have a DataArray with three dimensions: one each for slope
and intercept and one for the independent variable.
For each slope and intercept, the data is a line along x with the
slope and intercept described by the coordinate.
To fit this, we will need three things: the functional form which we
want to fit, a function to generate guesses of the parameters, and
a list of the names of the various parameters:

  1. def linfunc(x, m, b):
  2. return x*m + b
  3. def linfunc_guess(x, y, **kwargs):
  4. m_guess = (y[-1] - y[0])/(x[-1] - x[0])
  5. b_guess = y[0] - m_guess*x[0]
  6. return m_guess, b_guess
  7. param_names = ['m', 'b']

With these defined, we can now perform the actual fitting, which
will be done with the fit_dataArray function:

  1. fit = fit_dataArray(
  2. data_da, # data to fit
  3. linfunc, # function to fit to
  4. linfunc_guess, # function for making guesses
  5. lin_params, # parameter names
  6. 'x' # dimension to fit over
  7. )

fit is now a Dataset, containing the results of fitting our data.
The main results are contained in popt, which has a dim for each parameter
in the fit model as well as the same dims as the data_da we fit over, excluding
the xdim that we fit over.
The error estimates are stored in perr which has the same structure as popt,
and the full covariance matrix is stored in pcov.
The fit Dataset also contains some possibly useful information in its attrs,
such as the function the data was fit to.

Fitting Datasets

The process for fitting Dataset‘s is quite similar.
Assuming we have the same setup as above, we make a Dataset using the DataArray
from before, and fit it:

  1. data_ds = xr.Dataset(data_vars={'lindata': data_da})
  2. fit2 = fit_dataset(
  3. data_ds, # Dataset containing the data to fit
  4. linfunc, # Function to fit
  5. linfunc_guess, # Function generating initial parameter guesses
  6. lin_params, # Parameter names
  7. 'x', # Dimension name
  8. 'lindata' # Data variable name containing the dataArray to fit
  9. )

and now fit2 is the same as fit.

More Examples

There are example notebooks of how to use the features in the doc/ folder of this repo.