MCPcopy Index your code
hub / github.com/pydata/xarray / curvefit

Function curvefit

xarray/computation/fit.py:311–537  ·  view source on GitHub ↗

Curve fitting optimization for arbitrary functions. Wraps `scipy.optimize.curve_fit` with `apply_ufunc`. Parameters ---------- obj : Dataset or DataArray Object to perform the curvefit on coords : hashable, DataArray, or sequence of hashable or DataArray In

(
    obj,
    coords: str | DataArray | Iterable[str | DataArray],
    func: Callable[..., Any],
    reduce_dims: Dims = None,
    skipna: bool = True,
    p0: Mapping[str, float | DataArray] | None = None,
    bounds: Mapping[str, tuple[float | DataArray, float | DataArray]] | None = None,
    param_names: Sequence[str] | None = None,
    errors: ErrorOptions = "raise",
    kwargs: dict[str, Any] | None = None,
)

Source from the content-addressed store, hash-verified

309
310
311def curvefit(
312 obj,
313 coords: str | DataArray | Iterable[str | DataArray],
314 func: Callable[..., Any],
315 reduce_dims: Dims = None,
316 skipna: bool = True,
317 p0: Mapping[str, float | DataArray] | None = None,
318 bounds: Mapping[str, tuple[float | DataArray, float | DataArray]] | None = None,
319 param_names: Sequence[str] | None = None,
320 errors: ErrorOptions = "raise",
321 kwargs: dict[str, Any] | None = None,
322):
323 """
324 Curve fitting optimization for arbitrary functions.
325
326 Wraps `scipy.optimize.curve_fit` with `apply_ufunc`.
327
328 Parameters
329 ----------
330 obj : Dataset or DataArray
331 Object to perform the curvefit on
332 coords : hashable, DataArray, or sequence of hashable or DataArray
333 Independent coordinate(s) over which to perform the curve fitting. Must share
334 at least one dimension with the calling object. When fitting multi-dimensional
335 functions, supply `coords` as a sequence in the same order as arguments in
336 `func`. To fit along existing dimensions of the calling object, `coords` can
337 also be specified as a str or sequence of strs.
338 func : callable
339 User specified function in the form `f(x, *params)` which returns a numpy
340 array of length `len(x)`. `params` are the fittable parameters which are optimized
341 by scipy curve_fit. `x` can also be specified as a sequence containing multiple
342 coordinates, e.g. `f((x0, x1), *params)`.
343 reduce_dims : str, Iterable of Hashable or None, optional
344 Additional dimension(s) over which to aggregate while fitting. For example,
345 calling `ds.curvefit(coords='time', reduce_dims=['lat', 'lon'], ...)` will
346 aggregate all lat and lon points and fit the specified function along the
347 time dimension.
348 skipna : bool, default: True
349 Whether to skip missing values when fitting. Default is True.
350 p0 : dict-like, optional
351 Optional dictionary of parameter names to initial guesses passed to the
352 `curve_fit` `p0` arg. If the values are DataArrays, they will be appropriately
353 broadcast to the coordinates of the array. If none or only some parameters are
354 passed, the rest will be assigned initial values following the default scipy
355 behavior.
356 bounds : dict-like, optional
357 Optional dictionary of parameter names to tuples of bounding values passed to the
358 `curve_fit` `bounds` arg. If any of the bounds are DataArrays, they will be
359 appropriately broadcast to the coordinates of the array. If none or only some
360 parameters are passed, the rest will be unbounded following the default scipy
361 behavior.
362 param_names : sequence of hashable, optional
363 Sequence of names for the fittable parameters of `func`. If not supplied,
364 this will be automatically determined by arguments of `func`. `param_names`
365 should be manually supplied when fitting a function that takes a variable
366 number of parameters.
367 errors : {"raise", "ignore"}, default: "raise"
368 If 'raise', any errors from the `scipy.optimize_curve_fit` optimization will

Callers

nothing calls this directly

Calls 11

broadcastFunction · 0.90
apply_ufuncFunction · 0.90
typeFunction · 0.85
itemsMethod · 0.80
_get_func_argsFunction · 0.70
equalsMethod · 0.45
broadcast_likeMethod · 0.45
valuesMethod · 0.45
assign_coordsMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…