Set initial guess and bounds for curvefit. Priority: 1) passed args 2) func signature 3) scipy defaults
(params, p0, bounds, func_args)
| 57 | |
| 58 | |
| 59 | def _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 | |
| 101 | def polyfit( |
searching dependent graphs…