MCPcopy Create free account
hub / github.com/pydata/xarray / _initialize_curvefit_params

Function _initialize_curvefit_params

xarray/computation/fit.py:59–98  ·  view source on GitHub ↗

Set initial guess and bounds for curvefit. Priority: 1) passed args 2) func signature 3) scipy defaults

(params, p0, bounds, func_args)

Source from the content-addressed store, hash-verified

57
58
59def _initialize_curvefit_params(params, p0, bounds, func_args):
60 """Set initial guess and bounds for curvefit.
61 Priority: 1) passed args 2) func signature 3) scipy defaults
62 """
63
64 def _initialize_feasible(lb, ub):
65 # Mimics functionality of scipy.optimize.minpack._initialize_feasible
66 lb_finite = np.isfinite(lb)
67 ub_finite = np.isfinite(ub)
68 p0 = where(
69 lb_finite,
70 where(
71 ub_finite,
72 0.5 * (lb + ub), # both bounds finite
73 lb + 1, # lower bound finite, upper infinite
74 ),
75 where(
76 ub_finite,
77 ub - 1, # lower bound infinite, upper finite
78 0, # both bounds infinite
79 ),
80 )
81 return p0
82
83 param_defaults = dict.fromkeys(params, 1)
84 bounds_defaults = dict.fromkeys(params, (-np.inf, np.inf))
85 for p in params:
86 if p in func_args and func_args[p].default is not func_args[p].empty:
87 param_defaults[p] = func_args[p].default
88 if p in bounds:
89 lb, ub = bounds[p]
90 bounds_defaults[p] = (lb, ub)
91 param_defaults[p] = where(
92 (param_defaults[p] < lb) | (param_defaults[p] > ub),
93 _initialize_feasible(lb, ub),
94 param_defaults[p],
95 )
96 if p in p0:
97 param_defaults[p] = p0[p]
98 return param_defaults, bounds_defaults
99
100
101def polyfit(

Callers 2

test_curvefit_helpersMethod · 0.90
curvefitFunction · 0.85

Calls 2

whereFunction · 0.90
_initialize_feasibleFunction · 0.85

Tested by 1

test_curvefit_helpersMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…