postgkyl.fit

postgkyl.fit(data, fit_type, *, guess=None, window=False, min_n=None, print_coeffs=False, inplace=False, tag=None, label=None)

Fit a model to data and return the fitted curve.

Fits the model named (or expressed) by fit_type to each component of data independently and returns the fitted values evaluated on the data’s (cell-centered) grid. Axes collapsed to a single cell (e.g. after integrate or select) are dropped, so 1D and 2D fits are supported.

Parameters:
  • data – the dataset to fit; must contain point values. Its grid provides the independent variable(s) and each component is fit separately.

  • fit_type – the model to fit – a key of numerics.FIT_FUNCTIONS (‘linear’, ‘quadratic’, ‘plane’, ‘quadratic2d’, ‘exp_plateau’, ‘gaussian’, ‘power’, ‘sinusoid’, ‘tanh_transition’, ‘exp2’), or a custom RPN expression string (e.g. 'x a * b +') whose free tokens (not the spatial variables ‘x’/’y’, operators, or numbers) become fit parameters.

  • guess – initial guess for the fit parameters – a comma-separated string (e.g. '1,0,2') or a sequence of floats. None derives a data-driven guess per component via numerics.auto_guess (for the first window, if window=True, or each window for exp_plateau).

  • window – fit only the best-scoring leading window of the data (1D only) instead of the full domain – see numerics.fit_best_window.

  • min_n – minimum window length when window=True; None defaults to one tenth of the number of samples. Ignored otherwise.

  • print_coeffs – print the model equation and coefficient descriptions, followed by named coefficients with estimated 1-sigma uncertainties, R^2, residual sum of squares (RSS), root mean squared error (RMSE), residual standard error, sample count, degrees of freedom, and coordinate ranges for each zero-based component (12 significant digits). Statistics use only the fitted points, including when window=True. R^2 is undefined for constant data; residual standard error requires positive residual degrees of freedom. Custom RPN models show their expression and free parameter names. Full precision remains in ctx['fit_params'], ctx['fit_std'], and ctx['fit_R2'].

  • inplace – mutate and return data instead of a new dataset.

  • tag – optional tag for the returned dataset.

  • label – optional label for the returned dataset.

Returns:

A dataset holding the fitted curve on the original active grid, with ctx['fit_params'], ctx['fit_std'], and ctx['fit_R2'] set. Coefficients and uncertainties use the original coordinate units. Exponential plateau fits normalize x internally; an amplitude at x=0 outside floating-point range does not affect the returned curve. ctx['fit_models'] retains stable results for numerics.fit_evaluate.

Raises:

ValueError – if data is unevaluated modal coefficients, if fit_type is neither a recognized model name nor a valid RPN expression, if the data’s active dimensionality does not match the model’s, or if window=True and the data is not 1D.

CLI: fit