Pwl2FlatendFitter

class PlasmaCalcs.addons.instability_tools.instability_data_tools.Pwl2FlatendFitter(array, dim, *, promote_dims_if_needed=True, pnames=UNSET, pbounds=UNSET, bounds=UNSET, **kw_curve_fit)

Bases: XarrayCurveFitter

CurveFitter with f = pwl2_flatend, for fitting data to piecewise linear with 2 pieces.

When fitting, use bounds:
m0 > 0.
1 <= end0 <= len(xdata)-2
pwl2_flatend docs copied below, for convenience:
————————————————
evaluate xx at piecewise linear function with 2 pieces, with final piece slope=0.

xx: 1D array. Assumed to be monotonically increasing. b0: y-intercept of piece 0 m0: slope of piece 0 end0: “index” of end of piece 0

if end0 is not an int, does weighted averaging of xx[int(end0)] and xx[int(end0)+1].
E.g. end0 = 10.25 –> extend piece 0 to xx[10] + 0.25 * (xx[11] - xx[10])
b1 is computed based on the other inputs.
This is a decent approx. for ln(val) with linear growth then saturation.
xx <–> time
b0 <–> pre-growth noise level
m0 <–> growth rate
end0 <–> “saturation index” when linear growth stops.
x0 <–> “saturation time” when linear growth stops, where:
x0 = xx[i0] + (end0 - i0) * (xx[i0+1] - xx[i0], where i0 = int(end0).
y0 <–> “saturation level”; value when saturated, where:
y0 = m0 * x0 + b0.

Methods

eval([xdata, params, stddev])

evaluate curve fit result (params) at these xdata.

f(xx, b0, m0, end0)

evaluate xx at piecewise linear function with 2 pieces, with final piece slope=0.

fit(*[, stddev])

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

get_x0()

return x0, the x value at the end of piece 0.

get_y0()

return y0, the y value at the end of piece 0.

Attributes

fitted

result of latest call to self.fit().

get_xsat

x value at start of saturation.

get_ysat

saturation level (y-value).

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’, …))
static f(xx, b0, m0, end0)

evaluate xx at piecewise linear function with 2 pieces, with final piece slope=0.

xx: 1D array. Assumed to be monotonically increasing. b0: y-intercept of piece 0 m0: slope of piece 0 end0: “index” of end of piece 0

if end0 is not an int, does weighted averaging of xx[int(end0)] and xx[int(end0)+1].
E.g. end0 = 10.25 –> extend piece 0 to xx[10] + 0.25 * (xx[11] - xx[10])
b1 is computed based on the other inputs.
This is a decent approx. for ln(val) with linear growth then saturation.
xx <–> time
b0 <–> pre-growth noise level
m0 <–> growth rate
end0 <–> “saturation index” when linear growth stops.
x0 <–> “saturation time” when linear growth stops, where:
x0 = xx[i0] + (end0 - i0) * (xx[i0+1] - xx[i0], where i0 = int(end0).
y0 <–> “saturation level”; value when saturated, where:
y0 = m0 * x0 + b0.
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().
get_x0()

return x0, the x value at the end of piece 0.

(Crashes if run before self.fit())
x0 = xx[i0] + (end0 - i0) * (xx[i0+1] - xx[i0], where i0 = int(end0).
[TODO] option to get result +-1 stddev error bounds.
property get_xsat

x value at start of saturation. alias to self.get_x0.

get_y0()

return y0, the y value at the end of piece 0.

(Crashes if run before self.fit())
y0 = m0 * x0 + b0.
[TODO] option to get result +-1 stddev error bounds.
property get_ysat

saturation level (y-value). alias to self.get_y0.

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]