Pwl3FlatendFitter
- class PlasmaCalcs.addons.instability_tools.instability_data_tools.Pwl3FlatendFitter(array, dim, *, promote_dims_if_needed=True, pnames=UNSET, pbounds=UNSET, bounds=UNSET, **kw_curve_fit)
Bases:
XarrayCurveFitterCurveFitter with f = pwl3_flatend, for fitting data to piecewise linear with 3 pieces.
When fitting, use bounds:m0, m1 > 01 <= end0 <= len(xdata)-32 <= end1add <= len(xdata)-2# [TODO] It would be good to force end0 + end1add < len(xdata)-1. But I don’t know how to.pwl3_flatend docs copied below, for convenience:————————————————evaluate xx at piecewise linear function with 3 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[floor(end0)] and xx[ceil(end0)].E.g. end0 = 10.25 –> extend piece 0 to xx[10] + 0.25 * (xx[11] - xx[10])m1: slope of piece 1 end1add: “index” of end of piece 1, minus end0.
end1 = end0 + end1add. (max=len(xx)-1)if end1 is not an int, handle similarly to end0 (see above).b2 is computed based on the other inputs.This is a decent approx. for ln(val) with linear growth, then damped linear growth, then saturation.xx <–> timeb0 <–> pre-growth noise levelm0 <–> growth rate of linear growthend0 <–> “damped index” when linear growth stops.x0 <–> “damped time” when linear growth stops, where:x0 = xx[i0] + (end0 - i0) * (xx[i0+1] - xx[i0], where i0 = int(end0).y0 <–> pre-damped-growth level, where:y0 = m0 * x0 + b0.(–> damped growth piece has y-intercept b1 = y0 - m1 * x0.)m1 <–> growth rate of damped linear growthend1 <–> “saturation index” when damped growth stops, where:end1 = end0 + end1addx1 <–> “saturation time” when damped growth stops, where:x1 = xx[i1] + (end1 - i1) * (xx[i1+1] - xx[i1], where i1 = int(end1).y1 <–> “saturation level”; value when saturated, where:y21= m1 * x1 + b1.Methods
eval([xdata, params, stddev])evaluate curve fit result (params) at these xdata.
f(xx, b0, m0, end0, m1, end1add)evaluate xx at piecewise linear function with 3 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_x1()return x1, the x value at the end of piece 1.
get_y0()return y0, the y value at the end of piece 0.
get_y1()return y1, the y value at the end of piece 1.
Attributes
result of latest call to self.fit().
x value at start of saturation.
saturation level (y-value).
alias to self.fitted['params'], the params from latest call to self.fit.
pboundspnamesalias 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, m1, end1add)
evaluate xx at piecewise linear function with 3 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[floor(end0)] and xx[ceil(end0)].E.g. end0 = 10.25 –> extend piece 0 to xx[10] + 0.25 * (xx[11] - xx[10])m1: slope of piece 1 end1add: “index” of end of piece 1, minus end0.
end1 = end0 + end1add. (max=len(xx)-1)if end1 is not an int, handle similarly to end0 (see above).b2 is computed based on the other inputs.This is a decent approx. for ln(val) with linear growth, then damped linear growth, then saturation.xx <–> timeb0 <–> pre-growth noise levelm0 <–> growth rate of linear growthend0 <–> “damped index” when linear growth stops.x0 <–> “damped time” when linear growth stops, where:x0 = xx[i0] + (end0 - i0) * (xx[i0+1] - xx[i0], where i0 = int(end0).y0 <–> pre-damped-growth level, where:y0 = m0 * x0 + b0.(–> damped growth piece has y-intercept b1 = y0 - m1 * x0.)m1 <–> growth rate of damped linear growthend1 <–> “saturation index” when damped growth stops, where:end1 = end0 + end1addx1 <–> “saturation time” when damped growth stops, where:x1 = xx[i1] + (end1 - i1) * (xx[i1+1] - xx[i1], where i1 = int(end1).y1 <–> “saturation level”; value when saturated, where:y21= m1 * x1 + b1.
- 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.
- get_x1()
return x1, the x value at the end of piece 1.
(Crashes if run before self.fit())x1 = xx[i1] + (end1 - i1) * (xx[i1+1] - xx[i1], where i1 = int(end1),and end1 = end0 + end1add.[TODO] option to get result +-1 stddev error bounds.
- property get_xsat
x value at start of saturation. alias to self.get_x1.
- 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.
- get_y1()
return y1, the y value at the end of piece 1.
(Crashes if run before self.fit())y1 = m1 * x1 + b1.[TODO] option to get result +-1 stddev error bounds.
- property get_ysat
saturation level (y-value). alias to self.get_y1.
- 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]