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, …)
(For direct curve fitting from array, with arbitrary function f,
without implementing subclass, consider array.pc.curve_fit(f, dim) instead.)
array: xarray.DataArray or Dataset
data to fit.
Currently, Dataset allowed only if it has ‘mean’ and ‘std’ data_vars, when stddev=True,
in which case will sample the implied gaussians (via np.random.normal),
N=``werr_samples`` times, performing N fits to f,
reporting the mean and stddev of each fit param across all N fits, and
ignoring scipy standard deviation info about params from each individual fit.
dim: str
dim to fit along.
stddev: bool
whether to include data_var ‘stddev’ telling standard deviation of the fit.
(Internally, stored inside self.kw_curve_fit)
werr_samples: int
number of fits to do if array is a Dataset with ‘mean’ and ‘std’ vars, when stddev=True,
in which case result will tell mean and stddev of each fit param across all N fits,
and ignore scipy standard deviation info about params from each individual fit.
(Implemented this because default scipy linear least squares fitting with errorbars
just weights each point’s important by inverse of error bar,
which highly favors points with small errors.
That default does NOT correspond to the results of “repeating the experiment” N times,
where “the experiment” is gathering data then fitting,
and then asking “what is the mean and stddev of fit params across all N experiments?”.
However, using werr_samples DOES correspond to “repeating the experiment” N times.)
(Internally, stored inside self.kw_curve_fit)
werr_seed: None or any object, default 0
np.random.seed(werr_seed) beforehand, if doing werr_samples (with Dataset array).
Default 0 ensures reproducible results.
None –> don’t call np.random.seed beforehand. Will give different results each time.
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 or None or list of str
names of params. If provided, ‘param’ coord will be assigned these names.
UNSET –> use cls.pnames (default: None)
pbounds: UNSET or None or list of [None, callable, or 2-tuple of value, None, or callable]
bounds for each parameter. Provide pbounds or bounds, but not both.
None –> no bounds provided.
Each bound can be:
callable –> call as bound(array, dim) (after doing array.pc.ensure_dims(dim)).
None –> use (-np.inf, np.inf).
2-tuple –> (lower, upper).
callable –> use lower(array, dim) / upper(array, dim)
None –> use -np.inf / np.inf.
UNSET –> use cls.pbounds (default: None)
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 xarray_curve_fit, then scipy.optimize.curve_fit.

Methods

eval([xdata, params, stddev])

evaluate curve fit result (params) at these xdata.

f(x, *params)

function to fit.

fit(*[, stddev])

curve_fit to ydata = self.array, xdata = self.array[self.dim].

Attributes

fitted

result of latest call to self.fit().

params

alias to self.fitted['params'], the params from latest call to self.fit.

pbounds

pnames

xdata

alias to self.array[self.dim]

eval(xdata=UNSET, params=UNSET, stddev=False)

evaluate curve fit result (params) at these xdata.

Equivalent: xarray_curve_eval(params, self.f, xdata)
xdata: UNSET, 1D xarray.DataArray, or other 1D array-like
x values at which to evaluate the fit.
UNSET –> use self.xdata.
non-xarray 1D array-like –> convert to 1D DataArray via xr1d(xdata, self.dim)
params: UNSET or values of params from a fit.
UNSET –> use self.fitted
stddev: bool
whether to instead return Dataset with data_vars ‘eval+std’, ‘eval’, and ‘eval-std’,
telling f(xdata, *(params+std)), f(xdata, *params), and f(xdata, *(params-std)).
Fails if params is not a Dataset with ‘stddev’ data_var.
[EFF] note: if self.f is well-vectorized, it is equivalent (when stddev=False) 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]