xarray_line_fit
- PlasmaCalcs.tools.xarray_tools.xarray_sci.xarray_line_fit(array, dim, *, pnames=UNSET, pbounds=UNSET, **kw_curve_fitter)
returns result of xarray_curve_fit with f a line:
f(x, slope, intercept) = slope * x + intercept.array: xarray.DataArray or Datasetdata to fit.Currently, Dataset allowed only if it has ‘mean’ and ‘std’ data_vars, whenstddev=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, andignoring scipy standard deviation info about params from each individual fit.dim: strdim to fit along.stddev: boolwhether to include data_var ‘stddev’ telling standard deviation of the fit.werr_samples: intnumber of fits to do ifarrayis a Dataset with ‘mean’ and ‘std’ vars, whenstddev=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 errorbarsjust 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.)werr_seed: None or any object, default 0np.random.seed(werr_seed)beforehand, if doing werr_samples (with Datasetarray).Default 0 ensures reproducible results.None –> don’t call np.random.seed beforehand. Will give different results each time.promote_dims_if_needed: boolwhether 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 strnames of params. If provided, ‘param’ coord will be assigned these names.UNSET –> use pnames = [‘slope’, ‘intercept’]pbounds: UNSET or None or list of [None, callable, or 2-tuple of value, None, or callable]bounds for each parameter. Providepboundsorbounds, 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 pbounds = Nonebounds: UNSET or (list of lower bounds, list of upper bounds)bounds, formatted as expected by scipy curve_fit.Providepboundsorbounds, but not both.additional kwargs go to XarrayCurveFitter, then xarray_curve_fit, then scipy.optimize.curve_fit.