xarray_polyfit

PlasmaCalcs.tools.xarray_tools.xarray_sci.xarray_polyfit(array, coord, degree, *, stddev=False, full=False, cov=False, eval=False, **kw_polyfit)

returns array.polyfit(coord, degree, **kw_polyfit), after swapping coord to be a dimension, if needed.

E.g. for an array with dimension ‘snap’ and associated non-dimension coordinate ‘t’,
xarray_polyfit(array, ‘t’, 1) is equivalent to array.swap_dims(dict(snap=’t’)).polyfit(‘t’, 1).
stddev: bool
whether to also return the standard deviations of each coefficient in the fit.
if True, assign the variable ‘polyfit_stddev’ = diagonal(polyfit_covariance)**0.5,
mapping the diagonal (across ‘cov_i’, ‘cov_j’) to the dimension ‘degree’.
if cov False when stddev True, do not keep_cov in the result.
Not compatible with full=True.
full: bool
passed into polyfit; see below.
cov: bool
passed into polyfit; see below.
Note: if stddev=True when cov=False, still use cov=True during array.polyfit,
however then remove polyfit_covariance & polyfit_residuals from result.
eval: bool
whether to also return the fit, evaluated at array.coords[coord]
if True, assign the variable ‘polyfit_eval’ = sum(coeff * coord**degree, ‘degree’).
if stddev=True too, assign ‘polyfit_eval-stddev’ and ‘polyfit_eval+stddev’ too,
using similar formula but using coeffs +/- stddev.
Docs for xr.DataArray.polyfit copied below:
——————————————-
Least squares polynomial fit.
This replicates the behaviour of numpy.polyfit but differs by skipping
invalid values when skipna = True.
Parameters
———-
dimHashable
Coordinate along which to fit the polynomials.
degint
Degree of the fitting polynomial.
skipnabool or None, optional
If True, removes all invalid values before fitting each 1D slices of the array.
Default is True if data is stored in a dask.array or if there is any
invalid values, False otherwise.
rcondfloat or None, optional
Relative condition number to the fit.
wHashable, array-like or None, optional
Weights to apply to the y-coordinate of the sample points.
Can be an array-like object or the name of a coordinate in the dataset.
fullbool, default: False
Whether to return the residuals, matrix rank and singular values in addition
to the coefficients.
covbool or “unscaled”, default: False
Whether to return to the covariance matrix in addition to the coefficients.
The matrix is not scaled if cov='unscaled'.
Returns
——-
polyfit_resultsDataset
A single dataset which contains:

polyfit_coefficients
The coefficients of the best fit.
polyfit_residuals
The residuals of the least-square computation (only included if full=True).
When the matrix rank is deficient, np.nan is returned.
[dim]_matrix_rank
The effective rank of the scaled Vandermonde coefficient matrix (only included if full=True)
[dim]_singular_value
The singular values of the scaled Vandermonde coefficient matrix (only included if full=True)
polyfit_covariance
The covariance matrix of the polynomial coefficient estimates (only included if full=False and cov=True)
See Also
——–
numpy.polyfit
numpy.polyval
xarray.polyval
DataArray.curvefit