Skip to content

Custom Function

curve_fit

Fit a curve with curve_fit method.

This function returns FitResult see the documentation for more information what is possible with it.

Parameters:

Name Type Description Default
fit_func

Function to fit.

required
x _NDARRAY

x data.

required
data _NDARRAY

data to fit.

required
p0 Optional[List[Any]]

Initial guess for the parameters.

None
bounds Optional[Union[List[Tuple[Any, Any]], Tuple[Any, Any]]]

Bounds for the parameters.

(-inf, inf)
**kwargs

Additional keyword arguments to curve_fit.

{}

Returns:

Name Type Description
FitResult FitResult[DynamicNamedTuple]

Fit result.

Source code in ffit/front.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def curve_fit(
    func: _t.Callable,
    x: _NDARRAY,
    data: _NDARRAY,
    p0: _t.Optional[_t.List[_t.Any]] = None,
    *,
    bounds: _t.Optional[
        _t.Union[_t.List[_t.Tuple[_t.Any, _t.Any]], _t.Tuple[_t.Any, _t.Any]]
    ] = (
        -np.inf,
        np.inf,
    ),
    **kwargs,
) -> FitResult[DynamicNamedTuple]:
    """Fit a curve with curve_fit method.

    This function returns [FitResult][ffit.fit_results.FitResult] see
    the documentation for more information what is possible with it.

    Args:
        fit_func: Function to fit.
        x: x data.
        data: data to fit.
        p0: Initial guess for the parameters.
        bounds: Bounds for the parameters.
        **kwargs: Additional keyword arguments to curve_fit.

    Returns:
        FitResult: Fit result.
    """
    res_all = optimize.curve_fit(func, x, data, p0=p0, bounds=bounds, **kwargs)
    res = create_named_tuple(func, res_all[0])
    return FitResult(res, lambda x: func(x, *res), x=x, data=data)

leastsq

Perform a least squares optimization using the leastsq function from the optimize module.

This function returns FitResult see the documentation for more information what is possible with it.

Parameters:

Name Type Description Default
func Callable

The objective function to minimize.

required
x0 Sequence

The initial guess for the optimization.

required
args tuple

Additional arguments to be passed to the objective function.

()
**kwarg

Additional keyword arguments to be passed to the leastsq function.

{}

Returns:

Type Description
FitResult[tuple]

A FitResult object containing the optimization result and a function to evaluate the optimized parameters.

Source code in ffit/front.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def leastsq(
    func: _t.Callable, x0: _t.Sequence, args: tuple = (), **kwarg
) -> FitResult[tuple]:
    """Perform a least squares optimization using the `leastsq` function from the `optimize` module.

    This function returns [FitResult][ffit.fit_results.FitResult] see
    the documentation for more information what is possible with it.

    Args:
        func: The objective function to minimize.
        x0: The initial guess for the optimization.
        args: Additional arguments to be passed to the objective function.
        **kwarg: Additional keyword arguments to be passed to the `leastsq` function.

    Returns:
        A `FitResult` object containing the optimization result and a function to evaluate the optimized parameters.
    """

    res, cov = optimize.leastsq(func, x0, args=args, **kwarg)
    # print(res)
    return FitResult(res, lambda x: func(res), cov=cov)