PlasmaCalcs.tools.xarray_tools.xarray_sci.XarrayCurveFitter

class PlasmaCalcs.tools.xarray_tools.xarray_sci.XarrayCurveFitter(array, dim, *, promote_dims_if_needed=True, pnames=UNSET, pbounds=UNSET, bounds=UNSET, **kw_curve_fit)

Bases: object

class for helping with curve fitting.
Not intended for direct use; subclass must define f:

f: function to fit. callable like f(x, param1, param2, …)

array: xarray.DataArray
array to use for fitting.
dim: str
dim to fit along.
promote_dims_if_needed: bool
whether to promote non-dimension coords to dimensions.
if False, raise DimensionKeyError if any relevant coord is not already a dimension.
pnames: UNSET, None, or list of str
names of params. If provided, ‘param’ coord will be assigned these names.
UNSET –> use cls.pnames (default: None)
None –> no pnames provided
pbounds: UNSET, or list of [None, callable, or 2-tuple of value, None, or callable]
bounds for each parameter. Provide pbounds or bounds, but not both.
UNSET –> use cls.pbounds (default: None)
None –> no pbounds provided
Each bound can be:
callable –> call as bound(array) (after doing array.pc.ensure_dims(dim)).
None –> use (-np.inf, np.inf).
2-tuple –> (lower, upper).
callable –> use lower(array) / upper(array)
None –> use -np.inf / np.inf.
bounds: UNSET or (list of lower bounds, list of upper bounds)
bounds, formatted as expected by scipy curve_fit.
Provide pbounds or bounds, but not both.
additional kwargs go to scipy.optimize.curve_fit.
__init__(array, dim, *, promote_dims_if_needed=True, pnames=UNSET, pbounds=UNSET, bounds=UNSET, **kw_curve_fit)

Methods

__init__(array, dim, *[, ...])

eval([xdata, params])

f(x, *params)

fit(*[, stddev])

Attributes

fitted

params

pbounds

pnames

xdata

eval(xdata=UNSET, params=UNSET)
evaluate curve fit result (params) at these xdata.

Equivalent: xarray_curve_eval(params, self.f, xdata)

xdata: UNSET or 1D xarray.DataArray
x values at which to evaluate the fit.
UNSET –> use self.xdata.
params: UNSET or values of params from a fit.
UNSET –> use self.params
[EFF] note: if self.f is well-vectorized, it is equivalent and faster to do:
self.f(xdata, *params.transpose(‘param’, …))
f(x, *params)
function to fit. callable like f(x, param1, param2, …).
[Not implemented here; subclass should implement]
fit(*, stddev=UNSET)
curve_fit to ydata = self.array, xdata = self.array[self.dim].
Remembers result in self.fitted. Returns self.fitted.
stddev: UNSET or bool
whether to include data_var ‘stddev’ telling standard deviation of the fit.
UNSET –> use value from self.kw_curve_fit, else default of xarray_curve_fit.
property fitted
result of latest call to self.fit().
None if never called self.fit(), or if crashed before finishing self.fit().
property params
alias to self.fitted[‘params’], the params from latest call to self.fit.
crash with helpful message if self.fitted doesn’t exist.
property xdata
alias to self.array[self.dim]