def test_3(): y = np.random.random() * np.random.randint(100) lower = y - np.random.random() * np.random.randint(100) upper = y + np.random.random() * np.random.randint(100) assert_allclose( fminbound_numba(f_fminbound, lower, upper), fminbound(f_fminbound, lower, upper, full_output=True), )
def compute_join_max_curvature(px, py): from scipy.optimize.optimize import fminbound xp = polyder(px, 1) xpp = polyder(px, 2) yp = polyder(py, 1) ypp = polyder(py, 2) pn = polyadd(polymul(xp, ypp), polymul(yp, xpp)) #numerator pd = polyadd(polymul(xp, xp), polymul(yp, yp)) #denominator kappa = lambda t: -fabs(polyval(pn, t) / (polyval(pd, t)**(0.5))) argmin, res, flag, num = fminbound(kappa, 0, 1, xtol=0.005, full_output=1) return -res[0]
def compute_join_max_curvature( px, py ): from scipy.optimize.optimize import fminbound xp = polyder( px, 1 ) xpp = polyder( px, 2 ) yp = polyder( py, 1 ) ypp = polyder( py, 2 ) pn = polyadd( polymul( xp, ypp ), polymul( yp, xpp )) #numerator pd = polyadd( polymul( xp, xp ) , polymul( yp, yp ) ) #denominator kappa = lambda t: -fabs(polyval( pn, t )/( polyval( pd, t )**(0.5)) ) argmin, res, flag, num = fminbound( kappa, 0, 1, xtol=0.005, full_output=1 ) return -res[0]
def calcBetaEstimate(lSolver, hts, alphaF, mu_sigma, bmin= 0.2, bmax = 5): '''given a set of hitting times hts and known alpha, sigma - calculate the other parameter beta using Max Likelihood''' '''NOT USED:''' # 'F solver:' # dx = .05; # x_min = -1.5; dt = .025; Tf = amax(hts)+2*dt # S = ForwardSolver(mu_sigma, # dx, x_min, # dt, Tf) # alphas_for_F = alphaF( S._ts ); '''f Solver:''' lSolver.setTf(Tf); alphas_for_f = alphaF(lSolver._ts); def beta_nllk(beta): ''' Тhis is the root''' # Fs = squeeze( S.solve(alphas_for_F, # [beta])); # dt = S._ts[1] - S._ts[0]; # gs_dt = -diff(Fs[:, -1]) / dt; gs_dx = lSolver.solve_hittime_distn_per_parameter(1/beta, mu_sigma, alphas_for_f) # # figure(); hold(True) # plot(S._ts[1:], gs_dt, 'b') # plot(lSolver._ts, gs_dx, 'r') # show(); # gs_interp = interp1d( S._ts[1:], gs_dt) gs_interp = interp1d( lSolver._ts, gs_dx) nllk = -sum(log( gs_interp(hts) ) ) sys.stdout.write('%.3f,%.0f '%(beta, nllk)); sys.stdout.flush() return nllk ; beta_est, nllk_val, ierr, numfunc= fminbound(beta_nllk, bmin, bmax, xtol=1e-3, full_output = True); if 1 == ierr : print 'WARNING: fminbound hit max fevals' return beta_est;
def run(self, parConstraints, xtol=5e-5, maxiter=500, tmesh=None, extra_args=(), verbose=False): parsOrig = self.testModel.query(self.parTypeStr)[self.freeParNames[0]] # default time mesh if none supplied (t range split into 20) if tmesh is None: tmesh = self.get_tmesh() full_output = 1 rout.start() rerr.start() results = optimize.fminbound(self._residual_fn, parConstraints[0], parConstraints[1], (tmesh, ) + extra_args, xtol, maxiter, full_output, int(verbose)) out = rout.stop() err = rerr.stop() # build return information success = results[2] == 0 pestReturn = { 'success': success, 'pars_fit': { self.freeParNames[0]: results[0] }, 'pars_orig': { self.freeParNames[0]: parsOrig }, 'alg_results': results[3], 'sys_fit': self.testModel } if verbose: if success: print 'Solution of ', self.freeParNames[0], ' = ', results[0] print 'Original value = ', parsOrig print 'Number of mesh points = ', len(tmesh) print 'Number of fn evals = ', results[3], "(# iterations)" print 'Error tolerance = ', xtol else: print 'No convergence of BoundMin' print results return pestReturn
def calcBetaEstimate(hts, alpha, sigma): '''given a set of hitting times hts and known alpha, sigma - calculate the other parameter beta using Max Likelihood''' 'Max Time for the Solver' #Solver details: dx = .05; x_min = -1.5; dt = .025; Tf = amax(hts)+2*dt params = [0, sigma] print ' Tf=%.2f, alpha=%.2f'%( Tf,alpha) #INit Solver: S = ForwardSolver(params, dx, x_min, dt, Tf) alphas = alpha*ones_like(S._ts); def beta_nllk(beta): ''' Тhis is the root''' Fs = squeeze( S.solve(alphas, [beta])); dt = S._ts[1] - S._ts[0]; gs = -diff(Fs[:, -1]) / dt; gs_interp = interp1d( S._ts[1:], gs) nllk = -sum(log( gs_interp(hts) ) ) # print beta, nllk return nllk ; beta_est, nllk_val, ierr, numfunc= fminbound(beta_nllk, .05, 20, full_output = True); # print beta_est, nllk_val if 1 == ierr : print 'fminbound hit max fevals' return beta_est;
def run(self, parConstraints, xtol=5e-5, maxiter=500, tmesh=None, extra_args=(), verbose=False): parsOrig = self.testModel.query(self.parTypeStr)[self.freeParNames[0]] # default time mesh if none supplied (t range split into 20) if tmesh is None: tmesh = self.get_tmesh() full_output = 1 rout.start() rerr.start() results = optimize.fminbound(self._residual_fn, parConstraints[0], parConstraints[1], (tmesh,) + extra_args, xtol, maxiter, full_output, int(verbose)) out = rout.stop() err = rerr.stop() # build return information success = results[2] == 0 pestReturn = {'success': success, 'pars_fit': {self.freeParNames[0]: results[0]}, 'pars_orig': {self.freeParNames[0]: parsOrig}, 'alg_results': results[3], 'sys_fit': self.testModel } if verbose: if success: print 'Solution of ', self.freeParNames[0], ' = ', results[0] print 'Original value = ', parsOrig print 'Number of mesh points = ', len(tmesh) print 'Number of fn evals = ', results[3], "(# iterations)" print 'Error tolerance = ', xtol else: print 'No convergence of BoundMin' print results return pestReturn
def remove_saturated_pixel(ds, threshold=0.1, minimum=None, maximum=None): """ Remove saturated fixes from an array inplace. :param ds: a dataset as ndarray :param float threshold: what is the upper limit? all pixel > max*(1-threshold) are discareded. :param float minimum: minumum valid value (or True for auto-guess) :param float maximum: maximum valid value :return: the input dataset """ shape = ds.shape if ds.dtype == numpy.uint16: maxt = (1.0 - threshold) * 65535.0 elif ds.dtype == numpy.int16: maxt = (1.0 - threshold) * 32767.0 elif ds.dtype == numpy.uint8: maxt = (1.0 - threshold) * 255.0 elif ds.dtype == numpy.int8: maxt = (1.0 - threshold) * 127.0 else: if maximum is None: maxt = (1.0 - threshold) * ds.max() else: maxt = maximum if maximum is not None: maxt = min(maxt, maximum) invalid = (ds > maxt) if minimum: if minimum is True: # automatic guess of the best minimum TODO: use the HWHM to guess the minumum... data_min = ds.min() x, y = numpy.histogram(numpy.log(ds - data_min + 1.0), bins=100) f = interp1d((y[1:] + y[:-1]) / 2.0, -x, bounds_error=False, fill_value=-x.min()) max_low = fmin(f, y[1], disp=0) max_hi = fmin(f, y[-1], disp=0) if max_hi > max_low: f = interp1d((y[1:] + y[:-1]) / 2.0, x, bounds_error=False) min_center = fminbound(f, max_low, max_hi) else: min_center = max_hi minimum = float(numpy.exp(y[( (min_center / y) > 1).sum() - 1])) - 1.0 + data_min logger.debug("removeSaturatedPixel: best minimum guessed is %s", minimum) ds[ds < minimum] = minimum ds -= minimum # - 1.0 if invalid.sum(dtype=int) == 0: logger.debug("No saturated area where found") return ds gi = ndimage.morphology.binary_dilation(invalid) lgi, nc = ndimage.label(gi) if nc > 100: logger.warning( "More than 100 saturated zones were found on this image !!!!") for zone in range(nc + 1): dzone = (lgi == zone) if dzone.sum(dtype=int) > ds.size // 2: continue min0, min1, max0, max1 = bounding_box(dzone) ksize = min(max0 - min0, max1 - min1) subset = ds[max(0, min0 - 4 * ksize):min(shape[0], max0 + 4 * ksize), max(0, min1 - 4 * ksize):min(shape[1], max1 + 4 * ksize)] while subset.max() > maxt: subset = ndimage.median_filter(subset, ksize) ds[max(0, min0 - 4 * ksize):min(shape[0], max0 + 4 * ksize), max(0, min1 - 4 * ksize):min(shape[1], max1 + 4 * ksize)] = subset return ds
def removeSaturatedPixel(ds, threshold=0.1, minimum=None, maximum=None): """ @param ds: a dataset as ndarray @param threshold: what is the upper limit? all pixel > max*(1-threshold) are discareded. @param minimum: minumum valid value (or True for auto-guess) @param maximum: maximum valid value @return: another dataset """ shape = ds.shape if ds.dtype == numpy.uint16: maxt = (1.0 - threshold) * 65535.0 elif ds.dtype == numpy.int16: maxt = (1.0 - threshold) * 32767.0 elif ds.dtype == numpy.uint8: maxt = (1.0 - threshold) * 255.0 elif ds.dtype == numpy.int8: maxt = (1.0 - threshold) * 127.0 else: if maximum is None: maxt = (1.0 - threshold) * ds.max() else: maxt = maximum if maximum is not None: maxt = min(maxt, maximum) invalid = ds > maxt if minimum: if minimum is True: # automatic guess of the best minimum TODO: use the HWHM to guess the minumum... data_min = ds.min() x, y = numpy.histogram(numpy.log(ds - data_min + 1.0), bins=100) f = interp1d((y[1:] + y[:-1]) / 2.0, -x, bounds_error=False, fill_value=-x.min()) max_low = fmin(f, y[1], disp=0) max_hi = fmin(f, y[-1], disp=0) if max_hi > max_low: f = interp1d((y[1:] + y[:-1]) / 2.0, x, bounds_error=False) min_center = fminbound(f, max_low, max_hi) else: min_center = max_hi minimum = float(numpy.exp(y[((min_center / y) > 1).sum() - 1])) - 1.0 + data_min logger.debug("removeSaturatedPixel: best minimum guessed is %s", minimum) ds[ds < minimum] = minimum ds -= minimum # - 1.0 if invalid.sum(dtype=int) == 0: logger.debug("No saturated area where found") return ds gi = ndimage.morphology.binary_dilation(invalid) lgi, nc = ndimage.label(gi) if nc > 100: logger.warning("More than 100 saturated zones were found on this image !!!!") for zone in range(nc + 1): dzone = lgi == zone if dzone.sum(dtype=int) > ds.size // 2: continue min0, min1, max0, max1 = boundingBox(dzone) ksize = min(max0 - min0, max1 - min1) subset = ds[ max(0, min0 - 4 * ksize) : min(shape[0], max0 + 4 * ksize), max(0, min1 - 4 * ksize) : min(shape[1], max1 + 4 * ksize), ] while subset.max() > maxt: subset = ndimage.median_filter(subset, ksize) ds[ max(0, min0 - 4 * ksize) : min(shape[0], max0 + 4 * ksize), max(0, min1 - 4 * ksize) : min(shape[1], max1 + 4 * ksize), ] = subset fabio.edfimage.edfimage(data=ds).write("removeSaturatedPixel.edf") return ds
E1.summary() E2.summary() def E(alpha): #"""Error of a linear combination of measurements.""" alpha = numpy.squeeze(alpha) #return alpha*E1 + (1 - alpha)*E2 """Error of a geometric combination of measurements.""" return exp(log(E1)*alpha + log(E2)*(1-alpha)) print print "Combining measurements for optimal variance" alphaOptVar = fminbound(lambda alpha: E(alpha).var(), 0, 1, xtol = 1e-16) print "alpha for optimal variance = ", alphaOptVar dopt = E(alphaOptVar) dopt.summary() print print "Combining measurements for optimal Median Absolute Deviance" alphaOptMad = fminbound(lambda alpha: E(alpha).medianad(), 0, 1, xtol = 1e-16) print "alpha for optimal MAD = ", alphaOptMad dopt = E(alphaOptMad) dopt.summary() print print "Combining measurements for optimal 95% confidence interval" alphaOptIQrange = fminbound(lambda alpha: E(alpha).iqrange(0.025), 0, 1, xtol = 1e-16) print "alpha for optimal 95% c.i. = ", alphaOptIQrange
# target Dpsi = pi-2*beta, i.e. beta=delta cost = (beta - delta)**2 print "cost =", cost else: print evs print "Found %i TD events, %i LO events" % (numTDs, numLOs) raise RuntimeError, "Not enough events found" return cost Dpsi_tol = 5e-3 print "Finding periodic gait by varying initial y velocity," print "to tolerance in Dpsi of", Dpsi_tol, "\n" # use optimizer with boundary constraints ydot_opt = optimize.fminbound(residual_fn_ydot, 0.5, 0.75, xtol=Dpsi_tol) #ydot_opt = 0.666463229031 print "ydot for periodic gait = ", ydot_opt # ---- Compute periodic trajectory icdict_pdc = copy(ics) icdict_pdc['ydot'] = ydot_opt icdict_pdc['incontact'] = 0 print "Computing trajectory...\n" start = clock() SLIP.compute(trajname='pdc', tdata=[0, 12], ics=icdict_pdc, verboselevel=0) # optional print '... finished in %.3f seconds.\n' % (clock() - start)
# target Dpsi = pi-2*beta, i.e. beta=delta cost = (beta - delta) ** 2 print "cost =", cost else: print evs print "Found %i TD events, %i LO events" % (numTDs, numLOs) raise RuntimeError, "Not enough events found" return cost Dpsi_tol = 5e-3 print "Finding periodic gait by varying initial y velocity," print "to tolerance in Dpsi of", Dpsi_tol, "\n" # use optimizer with boundary constraints ydot_opt = optimize.fminbound(residual_fn_ydot, 0.5, 0.75, xtol=Dpsi_tol) # ydot_opt = 0.666463229031 print "ydot for periodic gait = ", ydot_opt # ---- Compute periodic trajectory icdict_pdc = copy(ics) icdict_pdc["ydot"] = ydot_opt icdict_pdc["incontact"] = 0 print "Computing trajectory...\n" start = clock() SLIP.compute(trajname="pdc", tdata=[0, 12], ics=icdict_pdc, verboselevel=0) # optional print "... finished in %.3f seconds.\n" % (clock() - start)
def calculateOptimalControl(params, Tf, energy_eps, alpha_bounds, J_tol = 1e-2, K_max=100, visualize=True): print 'Brent Optimizer:' xmin = FPSolver.calculate_xmin(alpha_bounds, params, num_std = 1.0) dx = FPSolver.calculate_dx(alpha_bounds, params, xmin) dt = FPSolver.calculate_dt(alpha_bounds, params, dx, xmin, factor = 4.) print 'Solver params: xmin, dx, dt', xmin,dx,dt #Set up solver #TODO: The way you pass params and the whole object-oriented approach is silly. Tf changes for each solve and atb don't, so maybe rething the architecture!!! S = FPSolver(dx, dt, Tf, xmin) ts = S._ts; alpha_min, alpha_max = alpha_bounds[0], alpha_bounds[1] def generateSwitchControl(switch_point): min_es = ones_like(ts); max_es = ones_like(ts) min_es[ts>=switch_point] = .0; max_es[ts<switch_point] = .0 return alpha_min*min_es + alpha_max * max_es def objective(switch_point): #Form the controls: alphas = generateSwitchControl(switch_point) #Find the solution xs, ts, fs, J = S.solve(params, alphas, alpha_bounds[1], energy_eps, visualize=False) #The associated cost return J; #TODO: Remove figure() switch_ts = linspace(-.1, Tf+.1, 12) Js = [objective(t) for t in switch_ts] plot(switch_ts, Js) title('REGIME=%s'%regime_tags[(params[0],params[2])]) vlines(Tf, .0, 2.0) # from scipy.optimize import brent # opt_switch, J_opt, iterations, funcals = brent(objective, # brack=[0, Tf], tol=J_tol, # full_output=True, maxiter=K_max) from scipy.optimize import fminbound opt_switch, J_opt, ierrr, numfuncals = fminbound(objective, 0, Tf, xtol=5e-2, maxfun=K_max, full_output=True) opt_controls = generateSwitchControl(opt_switch) xs, ts, fs, J = S.solve(params, opt_controls, alpha_bounds[1], energy_eps, visualize=False) opt_switch = min(Tf, max(opt_switch,.0)) print 'Opt switch time = %.3f'%opt_switch return xs, ts, fs, opt_controls, J_opt, opt_switch