def __call__(self): "Return the locations of the ticks" b = self._transform.base vmin, vmax = self.axis.get_view_interval() vmin, vmax = self._transform.transform_point((vmin, vmax)) if vmax < vmin: vmin, vmax = vmax, vmin numdec = math.floor(vmax) - math.ceil(vmin) if self._subs is None: if numdec > 10: subs = np.array([1.0]) elif numdec > 6: subs = np.arange(2.0, b, 2.0) else: subs = np.arange(2.0, b) else: subs = np.asarray(self._subs) stride = 1 while numdec / stride + 1 > self.numticks: stride += 1 decades = np.arange(math.floor(vmin), math.ceil(vmax) + stride, stride) if len(subs) > 1 or subs[0] != 1.0: ticklocs = [] for decade in decades: ticklocs.extend(subs * (np.sign(decade) * b ** np.abs(decade))) else: ticklocs = np.sign(decades) * b ** np.abs(decades) return np.array(ticklocs)
def FWHM(X,Y): half_max = (max(Y)+min(Y)) / 2 d = np.sign(half_max - np.array(Y[0:-1])) - np.sign(half_max - np.array(Y[1:])) #find the left and right most indexes left_idx = find(d > 0)[0] right_idx = find(d < 0)[-1] return X[right_idx], X[left_idx], half_max #return the difference (full width)
def __bisect(f, a, b, args): tol = 4.4408920985006262e-16 fa = f(*(a,)+args) fb = f(*(b,)+args) if np.sign(fa) == np.sign(fb): raise RuntimeError('f(a) and f(b) must have different signs') while abs(fa) > tol and abs(fb) > tol: ab = (a + b) / 2. fab = f(*(ab,)+args) if cmp(fa, 0) != cmp(fab, 0): b = ab fb = fab elif cmp(fb, 0) != cmp(fab, 0): a = ab fa = fab else: raise RuntimeError('Something fishy happened during bisection') if abs(fa) < tol and abs(fb) < tol: return a if min(abs(fa), abs(fb)) == abs(fa) else b elif abs(fa) < tol: return a elif abs(fb) < tol: return b else: raise RuntimeError('Something fishy happened during bisection')
def get_N_mtx(self,r_pnt, node_ls_values, r_ls_value): ''' Returns the matrix of the shape functions used for the field approximation containing zero entries. The number of rows corresponds to the number of nodal dofs. The matrix is evaluated for the specified local coordinate r. ''' p_N_mtx = self.parent_fets.get_N_mtx(r_pnt) n_nodes = self.parent_fets.n_e_dofs/self.parent_fets.n_nodal_dofs p_nodal_dofs = self.parent_fets.n_e_dofs/self.parent_fets.n_nodal_dofs p_value = sign(r_ls_value) N_e_list = [p_N_mtx[:,i*4:i*4+2]* \ (p_value-sign(node_ls_values[i]))\ for i in range(0,n_nodes)] N_e_mtx = hstack(N_e_list) N_enr_mtx = zeros((2,self.parent_fets.n_e_dofs*6), dtype = 'float_') for i in range(0,p_nodal_dofs): N_enr_mtx[0,i] = p_N_mtx[0,i]#s N_enr_mtx[1,i] = p_N_mtx[0,i] N_enr_mtx[0,i+2] = p_N_mtx[0,i]#m N_enr_mtx[1,i+4] = p_N_mtx[0,i]#f N_enr_mtx[0,i+6] = N_e_mtx[0,i]#sx N_enr_mtx[1,i+6] = N_e_mtx[0,i]#sx N_enr_mtx[0,i+8] = N_e_mtx[0,i]#mx N_enr_mtx[1,i+10] = N_e_mtx[0,i]#fx return N_enr_mtx
def get_ry0_distance(self, mesh): """ :param mesh: :class:`~openquake.hazardlib.geo.mesh.Mesh` of points to calculate Ry0-distance to. :returns: Numpy array of distances in km. See also :meth:`superclass method <.base.BaseSurface.get_ry0_distance>` for spec of input and result values. This is version specific to the planar surface doesn't make use of the mesh """ dst1 = geodetic.distance_to_arc(self.top_left.longitude, self.top_left.latitude, (self.strike + 90.) % 360, mesh.lons, mesh.lats) dst2 = geodetic.distance_to_arc(self.top_right.longitude, self.top_right.latitude, (self.strike + 90.) % 360, mesh.lons, mesh.lats) # Find the points on the rupture # Get the shortest distance from the two lines idx = numpy.sign(dst1) == numpy.sign(dst2) dst = numpy.zeros_like(dst1) dst[idx] = numpy.fmin(numpy.abs(dst1[idx]), numpy.abs(dst2[idx])) return dst
def addFace(self, du, dv, d, ru=0.5, rv=0.5): """ Creates a set of rectangular surfaces, their IDs, and face dims. nu,nv: number of surfaces in the u and v directions du,dv: {1,2,3} maps to {x,y,z}; negative sign means reverse order d: position of the surfaces in the remaining coordinate axis ru,rv: surfaces span -ru to +ru in u dir. and -rv to +rv in v dir. Adds to self.Ps and self.Ks """ self.faces.append([du,dv]) nP = 10 ni = self.ms[abs(du)-1].shape[0] nj = self.ms[abs(dv)-1].shape[0] verts = numpy.zeros((2,2,3),order='F') verts[:,:,:] = d verts[0,:,abs(du)-1] = -ru*numpy.sign(du) verts[1,:,abs(du)-1] = ru*numpy.sign(du) verts[:,0,abs(dv)-1] = -rv*numpy.sign(dv) verts[:,1,abs(dv)-1] = rv*numpy.sign(dv) for j in range(nj): for i in range(ni): self.Ps.append(PGMlib.bilinearinterp(nP, ni, nj, i+1, j+1, verts)) if len(self.Ks) > 0: counter = numpy.max(self.Ks[-1]) + 1 else: counter = 0 K = numpy.zeros((ni,nj),int) for j in range(nj): for i in range(ni): K[i,j] = counter counter += 1 self.Ks.append(K) self.outers.append(numpy.ones((ni,nj),bool))
def forward(self, state, action, Reward, state_dash, episode_end): num_of_batch = state.shape[0] s = Variable(state) s_dash = Variable(state_dash) Q = self.Q_func(s) # Get Q-value # Generate Target Signals tmp2 = self.Q_func(s_dash) tmp2 = list(map(np.argmax, tmp2.data.get())) # argmaxQ(s',a) tmp = self.Q_func_target(s_dash) # Q'(s',*) tmp = list(tmp.data.get()) # select Q'(s',*) due to argmaxQ(s',a) res1 = [] for i in range(num_of_batch): res1.append(tmp[i][tmp2[i]]) #max_Q_dash = np.asanyarray(tmp, dtype=np.float32) max_Q_dash = np.asanyarray(res1, dtype=np.float32) target = np.asanyarray(Q.data.get(), dtype=np.float32) for i in xrange(num_of_batch): if not episode_end[i][0]: tmp_ = np.sign(Reward[i]) + self.gamma * max_Q_dash[i] else: tmp_ = np.sign(Reward[i]) action_index = self.action_to_index(action[i]) target[i, action_index] = tmp_ # TD-error clipping td = Variable(cuda.to_gpu(target)) - Q # TD error td_tmp = td.data + 1000.0 * (abs(td.data) <= 1) # Avoid zero division td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1) zero_val = Variable(cuda.to_gpu(np.zeros((self.replay_size, self.num_of_actions), dtype=np.float32))) loss = F.mean_squared_error(td_clip, zero_val) return loss, Q
def _sym_ortho(a, b): """ Stable implementation of Givens rotation. Notes ----- The routine 'SymOrtho' was added for numerical stability. This is recommended by S.-C. Choi in [1]_. It removes the unpleasant potential of ``1/eps`` in some important places (see, for example text following "Compute the next plane rotation Qk" in minres.py). References ---------- .. [1] S.-C. Choi, "Iterative Methods for Singular Linear Equations and Least-Squares Problems", Dissertation, http://www.stanford.edu/group/SOL/dissertations/sou-cheng-choi-thesis.pdf """ if b == 0: return np.sign(a), 0, abs(a) elif a == 0: return 0, np.sign(b), abs(b) elif abs(b) > abs(a): tau = a / b s = np.sign(b) / sqrt(1 + tau * tau) c = s * tau r = b / s else: tau = b / a c = np.sign(a) / sqrt(1+tau*tau) s = c * tau r = a / c return c, s, r
def start(self, f, a, b, args=()): r"""Prepare for the iterations.""" self.function_calls = 0 self.iterations = 0 self.f = f self.args = args self.ab[:] = [a, b] if not np.isfinite(a) or np.imag(a) != 0: raise ValueError("Invalid x value: %s " % (a)) if not np.isfinite(b) or np.imag(b) != 0: raise ValueError("Invalid x value: %s " % (b)) fa = self._callf(a) if not np.isfinite(fa) or np.imag(fa) != 0: raise ValueError("Invalid function value: f(%f) -> %s " % (a, fa)) if fa == 0: return _ECONVERGED, a fb = self._callf(b) if not np.isfinite(fb) or np.imag(fb) != 0: raise ValueError("Invalid function value: f(%f) -> %s " % (b, fb)) if fb == 0: return _ECONVERGED, b if np.sign(fb) * np.sign(fa) > 0: raise ValueError("a, b must bracket a root f(%e)=%e, f(%e)=%e " % (a, fa, b, fb)) self.fab[:] = [fa, fb] return _EINPROGRESS, sum(self.ab) / 2.0
def _newton_quadratic(ab, fab, d, fd, k): """Apply Newton-Raphson like steps, using divided differences to approximate f' ab is a real interval [a, b] containing a root, fab holds the real values of f(a), f(b) d is a real number outside [ab, b] k is the number of steps to apply """ a, b = ab fa, fb = fab _, B, A = _compute_divided_differences([a, b, d], [fa, fb, fd], forward=True, full=False) # _P is the quadratic polynomial through the 3 points def _P(x): # Horner evaluation of fa + B * (x - a) + A * (x - a) * (x - b) return (A * (x - b) + B) * (x - a) + fa if A == 0: r = a - fa / B else: r = (a if np.sign(A) * np.sign(fa) > 0 else b) # Apply k Newton-Raphson steps to _P(x), starting from x=r for i in range(k): r1 = r - _P(r) / (B + A * (2 * r - a - b)) if not (ab[0] < r1 < ab[1]): if (ab[0] < r < ab[1]): return r r = sum(ab) / 2.0 break r = r1 return r
def increase(self, qty, px, fees=0, **kwargs): if not self.is_open(): raise Exception('increase position failed: no position currently open') if np.sign(self._live_qty) != np.sign(qty): msg = 'increase position failed: trade quantity {0} is different sign as live quantity {1}' raise Exception(msg.format(qty, self._live_qty)) self._order(qty, px, fees, **kwargs)
def adaboost_clf(Y_train, X_train, Y_test, X_test, M, clf): n_train, n_test = len(X_train), len(X_test) # Initialize weights w = np.ones(n_train) / n_train pred_train, pred_test = [np.zeros(n_train), np.zeros(n_test)] for i in range(M): # Fit a classifier with the specific weights clf.fit(X_train, Y_train, sample_weight = w) pred_train_i = clf.predict(X_train) pred_test_i = clf.predict(X_test) # Indicator function miss = [int(x) for x in (pred_train_i != Y_train)] # Equivalent with 1/-1 to update weights miss2 = [x if x==1 else -1 for x in miss] # Error err_m = np.dot(w,miss) / sum(w) # Alpha alpha_m = 0.5 * np.log( (1 - err_m) / float(err_m)) # New weights w = np.multiply(w, np.exp([float(x) * alpha_m for x in miss2])) # Add to prediction pred_train = [sum(x) for x in zip(pred_train, [x * alpha_m for x in pred_train_i])] pred_test = [sum(x) for x in zip(pred_test, [x * alpha_m for x in pred_test_i])] pred_train, pred_test = np.sign(pred_train), np.sign(pred_test) # Return error rate in train and test set return get_error_rate(pred_train, Y_train), \ get_error_rate(pred_test, Y_test)
def slow_set_freq(self,freq,step): current_f=float(self.get_freq())/1000 print(current_f) detun_f=freq-current_f ns=np.floor(detun_f/step) print('ns',ns) f_set=current_f for i in range(int(abs(ns))): f_set=f_set+np.sign(detun_f)*step print(f_set) self.set_freq(f_set) print('Start moving cavity slowly...') current_f=float(self.get_freq())/1000 detun_f=freq-current_f f_set=current_f print('fine moves') for i in range(int(abs(detun_f))): f_set=f_set+np.sign(detun_f) print(f_set) self.set_freq(f_set) print('Finish moving') print('Final freq',self.get_freq())
def turnStep(self, deg, blocking, anly): self.setStep(deg) if blocking: # If nudge steps = deg / (eleMaxStep / self.microsteps) self.wait = diode_wait / self.microsteps # If nudging would put elevation too high or low if -steps + self.cnt > ele_steps: # If nudging would put elevation too high steps = -ele_steps + self.cnt elif -steps + self.cnt < 0: # If nudging would put elevation too low steps = self.cnt else: if anly: steps = (np.sign(deg) * self.microsteps * deg_tol / eleMaxStep) / 2 else: steps = np.sign(deg) * self.microsteps * deg_tol / eleMaxStep if steps: if anly: self.wait = (diode_wait / abs(steps)) / 4 else: self.wait = (diode_wait / abs(steps)) if abs(deg) >= deg_tol: if self.cnt >= 0: if self.cnt < ele_steps: self.move(steps) elif self.cnt >= ele_steps and steps > 0: self.move(steps) else: time.sleep(diode_wait / 2) elif self.cnt < 0 and steps < 0: self.move(steps) else: time.sleep(diode_wait / 2) else: time.sleep(diode_wait / 2)
def waypoints_to_commands(coords): #cmd = [[vx,az,time],etc] #Convert waypoints to value in stage lin_vel = 0.2 ang_vel = math.radians(45) #45 deg/s in rad/s init_ang = 0; move_ang = [0] move_dist = [0] for i in range(len(coords)-1): p1 = coords[i] p2 = coords[i+1] move_ang.append(math.atan2(p2[1]-p1[1],p2[0]-p1[0])) move_dist.append(math.sqrt((p2[1]-p1[1])**2+(p2[0]-p1[0])**2)) print np.degrees(move_ang) print len(move_dist) move_cmd = [] for i in range(len(move_ang)-1): ang_cmd = (move_ang[i+1]-move_ang[i]) ang_time = ang_cmd/ang_vel dist_cmd =move_dist[i+1]-move_dist[i] dist_time = dist_cmd/lin_vel move_cmd.append([0,np.sign(ang_cmd),math.fabs(ang_time)]) move_cmd.append([np.sign(dist_cmd),0,math.fabs(dist_time)]) print move_cmd print len(move_cmd) return move_cmd
def prob_flat(m1, m2, s1z, s2z, **kwargs): ''' Return probability density for uniform in component mass Parameters ---------- m1: array Component masses 1 m2: array Component masses 2 s1z: array Aligned spin 1 (not in use currently) s2z: Aligned spin 2 (not in use currently) **kwargs: string Keyword arguments as model parameters Returns ------- p_m1_m2: array the probability density for m1, m2 pair ''' min_mass = kwargs.get('min_mass', 1.) max_mass = kwargs.get('max_mass', 2.) bound = np.sign(m1 - m2) bound += np.sign(max_mass - m1) * np.sign(m2 - min_mass) idx = np.where(bound != 2) p_m1_m2 = 2. / (max_mass - min_mass)**2 p_m1_m2[idx] = 0 return p_m1_m2
def example_classification(method='pa', classification_loss='hingeloss', initial_learning_rate=1e-4, tol=0.01): noise = 0.1 rng = np.random.RandomState(0) U_true = rng.randn(50, 2) V_true = rng.randn(40, 2) X = np.sign(np.dot(U_true, V_true.T) + noise * rng.randn(50, 40)) print(X[:5, :5]) # Put aside 1500 entries for testing row_mask = rng.randint(0, 50, size=1500) col_mask = rng.randint(0, 40, size=1500) # Get a mask for the remaining (training) observations mask = np.zeros((50, 40), dtype=np.bool) mask[row_mask, col_mask] = 1 fit_mask = (~mask).nonzero() MF = MatrixCompletion(method=method, is_classification=True, classification_loss = classification_loss, initial_learning_rate=initial_learning_rate, n_components=2,random_state=0, alpha=0.1, verbose=10, shuffle=True, tol=tol) MF.fit(X, mask=fit_mask) U, V = MF.U_, MF.V_ X_pred = np.dot(U, V.T) test_acc = np.mean(np.sign(X_pred[row_mask, col_mask]) == X[row_mask, col_mask]) print("Test accuracy:", test_acc) print(np.dot(U, V.T)[:5, :5])
def _prepare_input_epr_mur(w,epb,mub,ange,angm,sigmadc,tau): epr = _np.zeros((len(epb),len(w)),dtype=complex) mur = _np.zeros((len(epb),len(w)),dtype=complex) for j in range(len(epb)): epr[j,:] = epb[j]*(1-1j*_np.sign(w)*_np.tan(ange[j])) + sigmadc[j]/(1+1j*w*tau[j])/(1j*w*_ep0) mur[j,:] = mub[j]*(1-1j*_np.sign(w)*_np.tan(angm[j])) return epr, mur
def __init__(self, direction, hp, **kwargs): """Initialize a Critter `direction`: direction in radians that the critter is facing initially `hp`: Health of the Critter """ Widget.__init__(self, **kwargs) # Max health self.hp = hp # Direction we're facing currently self.direction = direction # Speed in tiles per second self.speed = CRITTER_SPEED # Damage done (accessed through .damage) self._damage = 0 # Are we dead yet? self.dead = False # x-component of velocity self.xdir = int(numpy.sign(round(numpy.cos(direction)))) # y-component of velocity self.ydir = int(numpy.sign(round(numpy.sin(direction)))) # Initial velocity self.initial_dir = self.xdir, self.ydir self.draw() Clock.schedule_once(self.go) Clock.schedule_once(self.tick)
def convertDec(deg, min, sec): if np.size(deg) == 1: sign = +1 if deg==0 else np.sign(deg) else: sign = np.sign(deg) sign[deg==0] = +1 return sign * (np.abs(deg) + (min + sec/60.)/60.)
def find_correct_sign(z1,z2,z_approx): ''' Create new vector from z1, z2 choosing elements with sign matching z_approx This is used when you have to make a root choice on a complex number. and you know the approximate value of the root. .. math:: z1,z2 = \\pm \\sqrt(z^2) Parameters ------------ z1 : array-like root 1 z2 : array-like root 2 z_approx : array-like approximate answer of z Returns ---------- z3 : npy.array array built from z1 and z2 by z1 where sign(z1) == sign(z_approx), z2 else ''' return npy.where( npy.sign(npy.angle(z1)) == npy.sign(npy.angle(z_approx)),z1, z2)
def deflections(self, xin, yin): import numpy from math import cos, sin, pi from scipy.special import gammainc x, y = self.align_coords(xin, yin) q = self.q if q == 1.: q = 1. - 1e-7 # Avoid divide-by-zero errors b, n, re = self.b, self.n, self.re k = 2. * n - 1. / 3 + 4. / (405. * n) + 46 / (25515. * n**2) amp = (b / re)**2 / gammainc(2 * n, k * (b / re)**(1 / n)) r0 = re / q**0.5 o = numpy.ones(x.size).astype(x.dtype) eval = numpy.array( [abs(x).ravel() / r0, abs(y).ravel() / r0, n * o, q * o]).T xout = numpy.sign(x) * (amp * self.xmod.eval(eval) * r0).reshape( x.shape) yout = numpy.sign(y) * (amp * self.ymod.eval(eval) * r0).reshape( y.shape) theta = -(self.theta - pi / 2.) ctheta = cos(theta) stheta = sin(theta) x = xout * ctheta + yout * stheta y = yout * ctheta - xout * stheta return x, y
def get_corr_pred(self, eps, d_eps, sig, t_n, t_n1, alpha, q, kappa): # g = lambda k: 0.8 - 0.8 * np.exp(-k) # g = lambda k: 1. / (1 + np.exp(-2 * k + 6.)) n_e, n_ip, n_s = eps.shape D = np.zeros((n_e, n_ip, 3, 3)) D[:, :, 0, 0] = self.E_m D[:, :, 2, 2] = self.E_f sig_trial = sig[:, :, 1]/(1-self.g(kappa)) + self.E_b * d_eps[:,:, 1] xi_trial = sig_trial - q f_trial = abs(xi_trial) - (self.sigma_y + self.K_bar * alpha) elas = f_trial <= 1e-8 plas = f_trial > 1e-8 d_sig = np.einsum('...st,...t->...s', D, d_eps) sig += d_sig d_gamma = f_trial / (self.E_b + self.K_bar + self.H_bar) * plas alpha += d_gamma kappa += d_gamma q += d_gamma * self.H_bar * np.sign(xi_trial) w = self.g(kappa) sig_e = sig_trial - d_gamma * self.E_b * np.sign(xi_trial) sig[:, :, 1] = (1-w)*sig_e E_p = -self.E_b / (self.E_b + self.K_bar + self.H_bar) * derivative(self.g, kappa, dx=1e-6) * sig_e \ + (1 - w) * self.E_b * (self.K_bar + self.H_bar) / \ (self.E_b + self.K_bar + self.H_bar) D[:, :, 1, 1] = (1-w)*self.E_b*elas + E_p*plas return sig, D, alpha, q, kappa
def main(): file1 = sys.argv[1] data = numpy.loadtxt(file1) train = data[25:] valid = data[0:25] train_newx = transform(train[:,0:2]) val_newx = transform(valid[:,0:2]) file2 = sys.argv[2] out = numpy.loadtxt(file2) out_newx = transform(out[:,0:2]) for i in range(0, train_newx.shape[1]): weights, E_in = linreg(train_newx[:,0:i+1], train[:,2]) val_y = numpy.sign(numpy.dot(val_newx[:,0:i+1], weights)) val_error = sum(val_y != valid[:,2])/float(len(valid)) print 'Validation error for k =', i, ':', val_error out_y = numpy.sign(numpy.dot(out_newx[:,0:i+1], weights)) out_error = sum(out_y != out[:,2])/float(len(out)) print 'Out of sample error for k =', i, ':', out_error
def findspikes(t,x,dxdt): """Searching for spikes in a TODL LTC2442 data series. The algorithm searches for the given threshold. If it is found and the subsequent data exceeds the threshold as well with a negative sign its defined as a spike Args: t: time x: data dxdt: Threshold for rejection, a working rejection for FP07 is 0.1 [V/s] """ #print('Despiking') dt = np.diff(t) dx = np.diff(x) spikes = np.zeros(np.shape(t)) for i in range(1,len(dt)-1): dxdt1 = dx[i]/dt[i] dxdt2 = dx[i+1]/dt[i+1] if(abs(dxdt1) > dxdt): if(abs(dxdt1) > dxdt): if(np.sign(dxdt1) == -np.sign(dxdt2)): spikes[i+1] = 1 #print('Done despiking') return spikes
def run( self, X, y, callback): # through the epochs for e in xrange(self.epochs): # compute gradients self.compute_dEdW(X, y, self.weights) # through the layers for i in xrange(self.nn_size-1): # select weights sel_pos = (self.dEdW_pre[i] * self.dEdW[i])>0.0 sel_neg = (self.dEdW_pre[i] * self.dEdW[i])<0.0 sel_equ = (self.dEdW_pre[i] * self.dEdW[i])==0.0 # if dEdW[i]*dEdW_pre[i] > 0 self.Delta[i][sel_pos] = np.minimum(self.Delta_pre[i][sel_pos]*self.eta_plus, self.Delta_max) self.Delta_w[i][sel_pos] = -np.sign(self.dEdW[i][sel_pos]) * self.Delta[i][sel_pos] self.W[i][sel_pos] = self.W[i][sel_pos] + self.Delta_w[i][sel_pos] # if dEdW[i]*dEdW_pre[i] < 0 self.Delta[i][sel_neg] = np.maximum(self.Delta_pre[i][sel_neg]*self.eta_minus, self.Delta_min) if self.E>self.E_pre: self.W[i][sel_neg] = self.W[i][sel_neg] - self.Delta_w_pre[i][sel_neg] self.dEdW[i][sel_neg] = 0.0 # if dEdW[i]*dEdW_pre[i] == 0 self.Delta_w[i][sel_equ] = -np.sign(self.dEdW[i][sel_equ]) * self.Delta[i][sel_equ] self.W[i][sel_equ] = self.W[i][sel_equ] + self.Delta_w[i][sel_equ] # update self.dEdW_pre[i] = self.dEdW[i] self.Delta_pre[i] = self.Delta[i] self.Delta_w_pre[i] = self.Delta_w[i] self.E_pre = self.E self.E = callback(e)
def testDigits(kTup=('rbf', 10)): data, labels = loadImages('trainingDigits') b, alphas = smo(data, labels, 200, 0.0001, 10000, kTup) dataMat = np.mat(data) labelMat = np.mat(labels).transpose() svInd = np.nonzero(alphas.A > 0)[0] sVs = dataMat[svInd] labelSV = labelMat[svInd] print "There are %d Support Vectors" % np.shape(sVs)[0] m, n = np.shape(dataMat) errorCount = 0 for i in xrange(m): kernelEval = kernelTransform(sVs, dataMat[i, :], kTup) predict = kernelEval.T * np.multiply(labelSV, alphas[svInd]) + b if np.sign(predict) != np.sign(labels[i]): errorCount += 1 print "The training error rate is %f " % (float(errorCount) / m) data, labels = loadImages('testDigits') dataMat = np.mat(data) labelMat = np.mat(labels).transpose() m, n = np.shape(dataMat) errorCount = 0 for i in xrange(m): kernelEval = kernelTransform(sVs, dataMat[i, :], kTup) predict = kernelEval.T * np.multiply(labelSV, alphas[svInd]) + b if np.sign(predict) != np.sign(labels[i]): errorCount += 1 print "The test error rate is %f " % (float(errorCount) / m)
def get_loss(self, state, action, reward, state_prime, episode_end): s = Variable(cuda.to_gpu(state)) s_dash = Variable(cuda.to_gpu(state_prime)) q = self.model.q_function(s) # Get Q-value # Generate Target Signals tmp = self.model_target.q_function(s_dash) # Q(s',*) tmp = list(map(np.max, tmp.data)) # max_a Q(s',a) max_q_prime = np.asanyarray(tmp, dtype=np.float32) target = np.asanyarray(copy.deepcopy(q.data.get()), dtype=np.float32) for i in range(self.replay_size): if episode_end[i][0] is True: tmp_ = np.sign(reward[i]) else: # The sign of reward is used as the reward of DQN! tmp_ = np.sign(reward[i]) + self.gamma * max_q_prime[i] target[i, action[i]] = tmp_ # TD-error clipping td = Variable(cuda.to_gpu(target)) - q # TD error td_tmp = td.data + 1000.0 * (abs(td.data) <= 1) # Avoid zero division td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1) zero_val = Variable(cuda.to_gpu(np.zeros((self.replay_size, self.n_act), dtype=np.float32))) loss = F.mean_squared_error(td_clip, zero_val) return loss, q
def __init__( self, sides, size, color, position = (0.0, 0.0, 0.0) ): self.sides = sides self.color = color self.size = size self.position = np.array(position) height = LEGO_BIG_HEIGHT if self.size else LEGO_SMALL_HEIGHT length = self.bottom - self.top combine = lambda _points, _vertices, _weights: _points self.__tess = GLU.gluNewTess() GLU.gluTessCallback(self.__tess, GLU.GLU_TESS_BEGIN, GL.glBegin) GLU.gluTessCallback(self.__tess,GLU.GLU_TESS_VERTEX,GL.glVertex3fv) GLU.gluTessCallback(self.__tess,GLU.GLU_TESS_COMBINE,combine) GLU.gluTessCallback(self.__tess, GLU.GLU_TESS_END, GL.glEnd) GLU.gluTessProperty(self.__tess, GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_ODD) self.__gllist = GL.glGenLists(1) GL.glNewList(self.__gllist,GL.GL_COMPILE) GL.glColor3fv(self.color) GL.glBindTexture(GL.GL_TEXTURE_2D,0) GLU.gluTessNormal(self.__tess, 0.0, 1.0, 0.0) GL.glNormal3f(0.0, 1.0, 0.0) GLU.gluTessBeginPolygon(self.__tess,None) GLU.gluTessBeginContour(self.__tess) for i in range(0,len(self.coords)): vertex = (self.coords[i][0]*LEGO_GRID, height, self.coords[i-1][1]*LEGO_GRID) GLU.gluTessVertex(self.__tess, vertex, vertex) vertex = (self.coords[i][0]*LEGO_GRID, height, self.coords[i][1]*LEGO_GRID) GLU.gluTessVertex(self.__tess, vertex, vertex) GLU.gluTessEndContour(self.__tess) GLU.gluTessEndPolygon(self.__tess) for i in range(0,len(self.coords)): GL.glBegin(GL.GL_QUADS) sign = float(np.sign(self.sides[2*i-1])) GL.glNormal3f( sign, 0.0, 0.0 ) GL.glVertex3f( self.coords[i][0] * LEGO_GRID, height, self.coords[i-1][1] * LEGO_GRID) GL.glVertex3f( self.coords[i][0] * LEGO_GRID, height, self.coords[i] [1] * LEGO_GRID) GL.glVertex3f( self.coords[i][0] * LEGO_GRID, 0.0, self.coords[i] [1] * LEGO_GRID) GL.glVertex3f( self.coords[i][0] * LEGO_GRID, 0.0, self.coords[i-1][1] * LEGO_GRID) sign = float(np.sign(self.sides[2*i-2])) GL.glNormal3f( 0.0, 0.0, -sign ) GL.glVertex3f( self.coords[i-1][0] * LEGO_GRID, height, self.coords[i-1][1] * LEGO_GRID ) GL.glVertex3f( self.coords[i] [0] * LEGO_GRID, height, self.coords[i-1][1] * LEGO_GRID ) GL.glVertex3f( self.coords[i] [0] * LEGO_GRID, 0.0, self.coords[i-1][1] * LEGO_GRID ) GL.glVertex3f( self.coords[i-1][0] * LEGO_GRID, 0.0, self.coords[i-1][1] * LEGO_GRID ) GL.glEnd() GL.glTranslatef( self.left*LEGO_GRID + LEGO_GRID/2.0, (LEGO_BUMP_HEIGHT+height)/2.0 , self.bottom*LEGO_GRID - LEGO_GRID/2.0 ) for i in range( self.left, self.right ): for j in range( self.bottom, self.top ): GL.glTranslatef( 0.0, 0.0, LEGO_GRID ) if self.is_hit( (i+0.5,j+0.5) ): _caped_cylinder( LEGO_BUMP_RADIUS, height+LEGO_BUMP_HEIGHT, 32 ) GL.glTranslatef( 0.0, 0.0, length*LEGO_GRID ) GL.glTranslatef( LEGO_GRID, 0.0, 0.0 ) GL.glEndList()
def intercept_poly(self, r, p, k): S = r.__class__ u = self.curvature*np.sign(self.offset[2]) if u == 0.: r, f, fr, g = Element.intercept_poly(self, r, p, k) else: p1 = p.copy().shift(1) a = (-u*k).shift(1) a -= (a*a - p1*r*u**2)**.5 a = a*p1**-1 # (44) f = a/u r = a*(-a).shift(2) # (45) g = (-a).shift(1) # (47) fr = .5*u*g**-1. # (46) if self.aspherics: # FIXME: not curve/conic u = self.aspherics r0 = r for i in range(len(u)): # (28) df = S() for uj in reversed(u): df = df.shift(uj*np.sign(self.offset[2]))*r # FIXME: real Newton Raphson r = r0 + df*(2*k + df*p) dfr = S() for i in reversed(range(len(u))): dfr = (dfr*r).shift((i + 1)*u[i]*np.sign(self.offset[2])) # FIXME f += df fr += dfr g = (4*r*dfr*dfr).shift(1)**-.5 return r, f, fr, g
def wheelEvent(self, event): self.screen.data.offsetCurrentFrame(numpy.sign(event.delta()) * int(self.screen.parameterDialog.getScrollSpeed()))
def alpha019(self): return ((-1 * sign((self.close - delay(self.close, 7)) + delta(self.close, 7))) * (1 + rank(1 + ts_sum(self.returns, 250))))
return np.dot(map_func(x1), map_func(x2).T) # fit SVM with custom non-linear kernel model = svm.SVC(kernel=my_kernel, C=1000, tol=1e-3) model.fit(X,Y) # coef : dual coefficients alpha_i*y_i per support vector in decision function coef = model.dual_coef_[0] # intercept in decision function b = model.intercept_ # bug in sklearn versions before 0.16: https://github.com/scikit-learn/scikit-learn/issues/4262 # The first class to appear in the dataset, i.e. Y[0], is mapped to the class +1, even if it is labeled -1 version_check = re.match(r"0\.(\d+).", sklearn.__version__).group(1) if np.sign(Y[0]) == -1 and int(version_check) < 16: coef = -coef def calc_plane_norm(sv, coef): """ Calculate the normal to the hyperplane (in mapped space) sv : matrix, contains mapped points, shape = [n_supportvectors, n_mappedfeatures] coef : array of floats, shape = [n_supportvectors, 1] """ components = coef[:, np.newaxis]*sv return np.sum(components, axis = 0) def calc_z_plane(x): """ Calculate z-coordinates of the decision plane
def generate_2D_edge_meshes(path, closed=False, limit=3, bevel=False): """Determines the triangulation of a path in 2D. The resulting `offsets` can be multiplied by a `width` scalar and be added to the resulting `centers` to generate the vertices of the triangles for the triangulation, i.e. `vertices = centers + width*offsets`. Using the `centers` and `offsets` representation thus allows for the computed triangulation to be independent of the line width. Parameters ---------- path : np.ndarray Nx2 or Nx3 array of central coordinates of path to be triangulated closed : bool Bool which determines if the path is closed or not limit : float Miter limit which determines when to switch from a miter join to a bevel join bevel : bool Bool which if True causes a bevel join to always be used. If False a bevel join will only be used when the miter limit is exceeded Returns ------- centers : np.ndarray Mx2 or Mx3 array central coordinates of path trinagles. offsets : np.ndarray Mx2 or Mx3 array of the offsets to the central coordinates that need to be scaled by the line width and then added to the centers to generate the actual vertices of the triangulation triangles : np.ndarray Px3 array of the indices of the vertices that will form the triangles of the triangulation """ clean_path = np.array(path).astype(float) if closed: if np.all(clean_path[0] == clean_path[-1]) and len(clean_path) > 2: clean_path = clean_path[:-1] full_path = np.concatenate( ([clean_path[-1]], clean_path, [clean_path[0]]), axis=0) normals = [ segment_normal(full_path[i], full_path[i + 1]) for i in range(len(clean_path)) ] normals = np.array(normals) full_path = np.concatenate((clean_path, [clean_path[0]]), axis=0) full_normals = np.concatenate((normals, [normals[0]]), axis=0) else: full_path = np.concatenate((clean_path, [clean_path[-2]]), axis=0) normals = [ segment_normal(full_path[i], full_path[i + 1]) for i in range(len(clean_path)) ] normals[-1] = -normals[-1] normals = np.array(normals) full_path = clean_path full_normals = np.concatenate(([normals[0]], normals), axis=0) miters = np.array( [full_normals[i:i + 2].mean(axis=0) for i in range(len(full_path))]) miters = np.array([ miters[i] / np.dot(miters[i], full_normals[i]) if np.dot(miters[i], full_normals[i]) != 0 else full_normals[i] for i in range(len(full_path)) ]) miter_lengths = np.linalg.norm(miters, axis=1) miters = 0.5 * miters vertex_offsets = [] central_path = [] triangles = [] m = 0 for i in range(len(full_path)): if i == 0: if (bevel or miter_lengths[i] > limit) and closed: offset = np.array([miters[i, 1], -miters[i, 0]]) offset = 0.5 * offset / np.linalg.norm(offset) flip = np.sign(np.dot(offset, full_normals[i])) vertex_offsets.append(offset) vertex_offsets.append(-flip * miters[i] / miter_lengths[i] * limit) vertex_offsets.append(-offset) central_path.append(full_path[i]) central_path.append(full_path[i]) central_path.append(full_path[i]) triangles.append([0, 1, 2]) m = m + 1 else: vertex_offsets.append(-miters[i]) vertex_offsets.append(miters[i]) central_path.append(full_path[i]) central_path.append(full_path[i]) elif i == len(full_path) - 1: if closed: a = vertex_offsets[m + 1] b = vertex_offsets[1] ray = full_path[i] - full_path[i - 1] if np.cross(a, ray) * np.cross(b, ray) > 0: triangles.append([m, m + 1, 1]) triangles.append([m, 0, 1]) else: triangles.append([m, m + 1, 1]) triangles.append([m + 1, 0, 1]) else: vertex_offsets.append(-miters[i]) vertex_offsets.append(miters[i]) central_path.append(full_path[i]) central_path.append(full_path[i]) a = vertex_offsets[m + 1] b = vertex_offsets[m + 3] ray = full_path[i] - full_path[i - 1] if np.cross(a, ray) * np.cross(b, ray) > 0: triangles.append([m, m + 1, m + 3]) triangles.append([m, m + 2, m + 3]) else: triangles.append([m, m + 1, m + 3]) triangles.append([m + 1, m + 2, m + 3]) elif bevel or miter_lengths[i] > limit: offset = np.array([miters[i, 1], -miters[i, 0]]) offset = 0.5 * offset / np.linalg.norm(offset) flip = np.sign(np.dot(offset, full_normals[i])) vertex_offsets.append(offset) vertex_offsets.append(-flip * miters[i] / miter_lengths[i] * limit) vertex_offsets.append(-offset) central_path.append(full_path[i]) central_path.append(full_path[i]) central_path.append(full_path[i]) a = vertex_offsets[m + 1] b = vertex_offsets[m + 3] ray = full_path[i] - full_path[i - 1] if np.cross(a, ray) * np.cross(b, ray) > 0: triangles.append([m, m + 1, m + 3]) triangles.append([m, m + 2, m + 3]) else: triangles.append([m, m + 1, m + 3]) triangles.append([m + 1, m + 2, m + 3]) triangles.append([m + 2, m + 3, m + 4]) m = m + 3 else: vertex_offsets.append(-miters[i]) vertex_offsets.append(miters[i]) central_path.append(full_path[i]) central_path.append(full_path[i]) a = vertex_offsets[m + 1] b = vertex_offsets[m + 3] ray = full_path[i] - full_path[i - 1] if np.cross(a, ray) * np.cross(b, ray) > 0: triangles.append([m, m + 1, m + 3]) triangles.append([m, m + 2, m + 3]) else: triangles.append([m, m + 1, m + 3]) triangles.append([m + 1, m + 2, m + 3]) m = m + 2 centers = np.array(central_path) offsets = np.array(vertex_offsets) triangles = np.array(triangles) return centers, offsets, triangles
def reward(self, reward): """Bin reward to {+1, 0, -1} by its sign.""" return np.sign(reward)
def predict(self, features): # sign(x.w + b) classification = np.sign(np.dot(np.array(features), self.w) + self.b) return classification
segment_prod = utils.copy_docstring( 'tf.math.segment_prod', _segment_prod) segment_sum = utils.copy_docstring( 'tf.math.segment_sum', _segment_sum) sigmoid = utils.copy_docstring( 'tf.math.sigmoid', lambda x, name=None: scipy_special.expit(x)) sign = utils.copy_docstring( 'tf.math.sign', lambda x, name=None: np.sign(x)) sin = utils.copy_docstring( 'tf.math.sin', lambda x, name=None: np.sin(x)) sinh = utils.copy_docstring( 'tf.math.sinh', lambda x, name=None: np.sinh(x)) softmax = utils.copy_docstring( 'tf.math.softmax', _softmax) def _softplus(x, name=None): # pylint: disable=unused-argument
def alpha030(self): delta_close = delta(self.close, 1) inner = sign(delta_close) + sign(delay(delta_close, 1)) + sign(delay(delta_close, 2)) return ((1.0 - rank(inner)) * ts_sum(self.volume, 5)) / ts_sum(self.volume, 20)
def alpha007(self): adv20 = sma(self.volume, 20) alpha = -1 * ts_rank(abs(delta(self.close, 7)), 60) * sign(delta(self.close, 7)) alpha[adv20 >= self.volume] = -1 return alpha
def alpha012(self): return sign(delta(self.volume, 1)) * (-1 * delta(self.close, 1))
def projSO3(M): # return a rotation matrix close to M Q,R = np.linalg.qr(M) return Q@diag(diag(sign(R)))
def __updateParameters(self, x, y): """ w_k = w_{k-1} - lr*dw b_k = b_{k-1} - lr*db """ yt = self.__fit_predict(x) residuals = yt - y dW = 2/self.m * (x.T).dot(residuals) + self.lambda_ * (self.W != 0).astype(int)*np.sign(self.W) self.dws.append(dW) self.W = self.W - self.lr*dW if self.withBias: dB = 2/self.m * np.sum( residuals ) self.B = self.B - self.lr*dB
theta_train = np.zeros(35, dtype=float) converged = False train_accuracy = 0 train_iter = 1000 eta = 0.05 error_train = [] error_test = [] while (converged != True and train_iter > 0): converged = True train_accuracy = len(data_train) sum = np.zeros(35, dtype=float) for i in range(len(data_train)): inner_product = np.inner(data_train[i], theta_train) result = int(np.sign(inner_product)) if (result < 0 and label_train[i] == 1) or (result > 0 and label_train[i] == 0) or result == 0: train_accuracy -= 1 converged = False error_train.append(1 - train_accuracy / float(len(data_train))) test_accuracy = len(data_test) for i in range(len(data_test)): inner_product = np.inner(data_test[i], theta_train) result = int(np.sign(inner_product)) if (result < 0 and label_test[i] == 1) or (result > 0 and label_test[i] == 0) or result == 0: test_accuracy -= 1 error_test.append(1 - test_accuracy / float(len(data_test)))
def test_sanity_check_pls_canonical_random(): # Sanity check for PLSCanonical on random data # The results were checked against the R-package plspm n = 500 p_noise = 10 q_noise = 5 # 2 latents vars: rng = check_random_state(11) l1 = rng.normal(size=n) l2 = rng.normal(size=n) latents = np.array([l1, l1, l2, l2]).T X = latents + rng.normal(size=4 * n).reshape((n, 4)) Y = latents + rng.normal(size=4 * n).reshape((n, 4)) X = np.concatenate((X, rng.normal(size=p_noise * n).reshape(n, p_noise)), axis=1) Y = np.concatenate((Y, rng.normal(size=q_noise * n).reshape(n, q_noise)), axis=1) pls = PLSCanonical(n_components=3) pls.fit(X, Y) expected_x_weights = np.array([ [0.65803719, 0.19197924, 0.21769083], [0.7009113, 0.13303969, -0.15376699], [0.13528197, -0.68636408, 0.13856546], [0.16854574, -0.66788088, -0.12485304], [-0.03232333, -0.04189855, 0.40690153], [0.1148816, -0.09643158, 0.1613305], [0.04792138, -0.02384992, 0.17175319], [-0.06781, -0.01666137, -0.18556747], [-0.00266945, -0.00160224, 0.11893098], [-0.00849528, -0.07706095, 0.1570547], [-0.00949471, -0.02964127, 0.34657036], [-0.03572177, 0.0945091, 0.3414855], [0.05584937, -0.02028961, -0.57682568], [0.05744254, -0.01482333, -0.17431274], ]) expected_x_loadings = np.array([ [0.65649254, 0.1847647, 0.15270699], [0.67554234, 0.15237508, -0.09182247], [0.19219925, -0.67750975, 0.08673128], [0.2133631, -0.67034809, -0.08835483], [-0.03178912, -0.06668336, 0.43395268], [0.15684588, -0.13350241, 0.20578984], [0.03337736, -0.03807306, 0.09871553], [-0.06199844, 0.01559854, -0.1881785], [0.00406146, -0.00587025, 0.16413253], [-0.00374239, -0.05848466, 0.19140336], [0.00139214, -0.01033161, 0.32239136], [-0.05292828, 0.0953533, 0.31916881], [0.04031924, -0.01961045, -0.65174036], [0.06172484, -0.06597366, -0.1244497], ]) expected_y_weights = np.array([ [0.66101097, 0.18672553, 0.22826092], [0.69347861, 0.18463471, -0.23995597], [0.14462724, -0.66504085, 0.17082434], [0.22247955, -0.6932605, -0.09832993], [0.07035859, 0.00714283, 0.67810124], [0.07765351, -0.0105204, -0.44108074], [-0.00917056, 0.04322147, 0.10062478], [-0.01909512, 0.06182718, 0.28830475], [0.01756709, 0.04797666, 0.32225745], ]) expected_y_loadings = np.array([ [0.68568625, 0.1674376, 0.0969508], [0.68782064, 0.20375837, -0.1164448], [0.11712173, -0.68046903, 0.12001505], [0.17860457, -0.6798319, -0.05089681], [0.06265739, -0.0277703, 0.74729584], [0.0914178, 0.00403751, -0.5135078], [-0.02196918, -0.01377169, 0.09564505], [-0.03288952, 0.09039729, 0.31858973], [0.04287624, 0.05254676, 0.27836841], ]) assert_array_almost_equal(np.abs(pls.x_loadings_), np.abs(expected_x_loadings)) assert_array_almost_equal(np.abs(pls.x_weights_), np.abs(expected_x_weights)) assert_array_almost_equal(np.abs(pls.y_loadings_), np.abs(expected_y_loadings)) assert_array_almost_equal(np.abs(pls.y_weights_), np.abs(expected_y_weights)) x_loadings_sign_flip = np.sign(pls.x_loadings_ / expected_x_loadings) x_weights_sign_flip = np.sign(pls.x_weights_ / expected_x_weights) y_weights_sign_flip = np.sign(pls.y_weights_ / expected_y_weights) y_loadings_sign_flip = np.sign(pls.y_loadings_ / expected_y_loadings) assert_array_almost_equal(x_loadings_sign_flip, x_weights_sign_flip) assert_array_almost_equal(y_loadings_sign_flip, y_weights_sign_flip) assert_matrix_orthogonal(pls.x_weights_) assert_matrix_orthogonal(pls.y_weights_) assert_matrix_orthogonal(pls._x_scores) assert_matrix_orthogonal(pls._y_scores)
X_2 = np.array([[-1, 0, 1.5, -0.5]]).T X_3 = np.array([[-1, -1, 1, 0.5]]).T X = np.append(X_1, X_2, axis=1) X = np.append(X, X_3, axis=1) #X是4*3的矩阵 Y = np.array([[-1, -1, 1]]) #Y是1*3的矩阵 W = (np.random.random([4, 1]) - 0.5) * 2 #初始化权值,范围是[-1,1] #W是4*1的矩阵 lr = 0.11 #设置学习率为0.11 output = np.array([[0, 0, 0]]) #神经网络的输出(output)初始化为0 def iteration(): #迭代函数 global X, Y, W, lr output = np.sign(np.dot(W.T, X)) delta = lr * (np.dot(X, (Y - output).T)) W = W + delta #对权值矩阵进行调整 for i in range(100): print("第 %s 次迭代" % i) print(W) iteration() output = np.sign(np.dot(W.T, X)) #计算当前的输出 if (output == Y).all(): print("Finished") break
def iteration(): #迭代函数 global X, Y, W, lr output = np.sign(np.dot(W.T, X)) delta = lr * (np.dot(X, (Y - output).T)) W = W + delta #对权值矩阵进行调整
# This block should reset the PID if there is nothing on the screen for more than 5 frames. if mX == 0: count += 1 else: count = 0 if count > 5: p = PID(3.0, 0.4, 1.2) p.setPoint(300.0) # Note: need to put in something so it doesn't send PID controls when mX is 0 pid = p.update(mX) if abs(pid) > 255: pid = 255 * (np.sign(pid)) if abs(pid) < 100: pid = 100 * (np.sign(pid)) val = str(int(abs(pid))) if pid > 0: motControl = '-' + val + '-' + val + '+' + val + '+' + val + '>' else: motControl = '+' + val + '+' + val + '-' + val + '-' + val + '>' if mX != 0: ser.write(motControl) cv2.putText(frame, str(pid),
# %% # Resta np.subtract(arr_2, arr_1) # %% # Raiz cuadrada np.sqrt(arr_1) # %% # Potencia np.power(arr_1, 2) # %% # Signo np.sign(arr_1) # %% # Seno np.sin(arr_1) # %% # CoSeno np.cos(arr_1) # %% # Tangente np.tan(arr_1) # %% # Grados a radianes
def _step(self, action): #if action[0]==action[1]==action[2]==action[3]==0.0: # self.hull.ApplyForceToCenter((0, 10.0*(5.0*(20/SCALE)**2)), True) # self.legs[0].ApplyForceToCenter((0, 10.0 * 1.0 * LEG_H*LEG_W), True) # self.legs[1].ApplyForceToCenter((0, 10.0 * 1.0 * LEG_H * LEG_W), True) # self.legs[2].ApplyForceToCenter((0, 10.0 * 1.0 * LEG_H * LEG_W), True) # self.legs[3].ApplyForceToCenter((0, 10.0 * 1.0 * LEG_H * LEG_W), True) #self.hull.ApplyForceToCenter((0, 20), True) #-- Uncomment this to receive a bit of stability help control_speed = False if control_speed: self.joints[0].motorSpeed = float(SPEED_HIP * np.clip(action[0], -1, 1)) self.joints[1].motorSpeed = float(SPEED_KNEE * np.clip(action[1], -1, 1)) self.joints[2].motorSpeed = float(SPEED_HIP * np.clip(action[2], -1, 1)) self.joints[3].motorSpeed = float(SPEED_KNEE * np.clip(action[3], -1, 1)) else: self.joints[0].maxMotorTorque = float(np.abs(action[0])) self.joints[0].motorSpeed = float(SPEED_HIP * np.sign(action[0])) self.joints[1].maxMotorTorque = float(np.abs(action[1])) self.joints[1].motorSpeed = float(SPEED_KNEE * np.sign(action[1])) self.joints[2].maxMotorTorque = float(np.abs(action[2])) self.joints[2].motorSpeed = float(SPEED_HIP * np.sign(action[2])) self.joints[3].maxMotorTorque = float(np.abs(action[3])) self.joints[3].motorSpeed = float(SPEED_KNEE * np.sign(action[3])) self.world.Step(1.0/FPS, 6*30, 2*30) pos = self.hull.position vel = self.hull.linearVelocity for i in range(10): self.lidar[i].fraction = 1.0 self.lidar[i].p1 = pos self.lidar[i].p2 = ( pos[0] + math.sin(1.5*i/10.0)*LIDAR_RANGE, pos[1] - math.cos(1.5*i/10.0)*LIDAR_RANGE) self.world.RayCast(self.lidar[i], self.lidar[i].p1, self.lidar[i].p2) state = [ self.hull.angle, #s[0] self.hull.angularVelocity, #s[1] vel.x, #s[2] global dxcom vel.y, #s[3] self.joints[0].angle, #s[4] self.joints[0].speed, #s[5] self.joints[1].angle, #s[6] self.joints[1].speed, #s[7] 1.0 if self.legs[1].ground_contact else 0.0, #s[8] self.joints[2].angle, #s[9] self.joints[2].speed, #s[10] self.joints[3].angle, #s[11] self.joints[3].speed, #s[12] 1.0 if self.legs[3].ground_contact else 0.0, #s[13] pos.y - TERRAIN_HEIGHT, #s[14] pos.x - self.legs[1].position.x, # s[15] pos.x - self.legs[3].position.x, # s[16] pos.x - init_x, # global xcom s[17] ] state += [l.fraction for l in self.lidar] assert len(state)==28 self.scroll = pos.x - VIEWPORT_W/SCALE/5 shaping = 130*pos[0]/SCALE # moving forward is a way to receive reward (normalized to get 300 on completion) shaping -= 5.0*abs(state[0]) # keep head straight, other than that and falling, any behavior is unpunished reward = 0 if self.prev_shaping is not None: reward = shaping - self.prev_shaping self.prev_shaping = shaping for a in action: reward -= 0.00035 * MOTORS_TORQUE * np.clip(np.abs(a), 0, 1) # normalized to about -50.0 using heuristic, more optimal agent should spend less done = False if self.game_over or pos[0] < 0: reward = -100 done = True if pos[0] > (TERRAIN_LENGTH-TERRAIN_GRASS)*TERRAIN_STEP: done = True return np.array(state), reward, done, {}
def _SMC_acc_Cartesian(self,sigma_max,sigma_d_max,sigma_min,sigma_d_min,phi_max,phi_min,J,dsigma_dp,Ka,U_plus,pd_ref,pdd_ref,pose,p_ref,Kp,Kpd,Kqd,Kq,Fx_max,Fy_max,U_plus_map,mode_eq,mode_ineq,temp_int,nContact,sm_ant,ASG_opt,U_plus_map2,auto,U_plus2,U_plus2_map,U_plus2_map2,phi2_max,Sensor2R,Kp_L3,Kpd_L3,threshold,error,Tp): p = np.zeros((6,1)) joint_acc_4 = np.zeros((7,1)) dsigma_dp2 = np.zeros((6,6)) phi_tmp_L2 = np.sign(phi2_max) XYZ = pose['position'] RPY = Quat2rpy(pose['orientation']) Qd = self._limb.joint_velocities() Q = self._limb.joint_angles() q = np.zeros((7,1)) qo = np.zeros((7,1)) qd = np.zeros((7,1)) for i in range(0,6,1): if i < 3: p[i] = XYZ[i] else: p[i] = RPY[i-3] if p[3] < 0: p[3] = p[3] + 2.0*math.pi for i,joint in enumerate(self._limb.joint_names()): qo[i] = self._start_angles[joint] q[i] = (self._limb.joint_angles())[joint] qd[i] = Qd[joint] pd = J.dot(qd) #Saturation of the feedforward error for i in range(0,6): error[i] = p_ref[i] - p[i] #Error saturation by direction m_e = np.sqrt(error[0]*error[0] + error[1]*error[1] + error[2]*error[2]) if m_e > 0.025: for i in range(0,3): error[i] = error[i]/m_e*0.025 #Error saturation by module #for i in range(0,3): # if np.abs(p_ref[i] - p[i]) > 0.02: # error[i] = np.sign(p_ref[i] - p[i])*0.02 # else: # error[i] = p_ref[i] - p[i] phi_tmp = np.sign(phi_max) #if phi_max[1] > 0: # phi_tmp[1] = 1.0 # dsigma_dp[1][1] = 4.0 #elif phi_min[1] > 0: # phi_tmp[1] = -1.0 # dsigma_dp[1][1] = 4.0 #else: # dsigma_dp[1][1] = 0.0 # phi_tmp[1] = 0.0 #if phi_max[0] > 0: # phi_tmp[0] = 1.0 # dsigma_dp[0][0] = 4.0 #elif phi_min[0] > 0: # phi_tmp[0] = -1.0 # dsigma_dp[0][0] = 4.0 #else: # dsigma_dp[0][0] = 0.0 # phi_tmp[0] = 0.0 #1st Level SMC: joint_acc = -linalg.pinv(Ka*(dsigma_dp.T).dot(J),rcond = 0.01).dot(U_plus_map2.dot(phi_tmp)) #2nd Level trajectory tracking: # Automatic or Collaborative mode if auto == 1 or nContact == True: N2 = np.eye(7) - linalg.pinv((dsigma_dp.T).dot(J),rcond = 0.01).dot((dsigma_dp.T).dot(J)) joint_acc_2 = linalg.pinv(J.dot(N2),rcond = 0.01).dot(pdd_ref + Kpd*(pd_ref - pd) + Kp*error - J.dot(joint_acc)) #3rf Level self motion: N3 = N2.dot(np.eye(7) - np.linalg.pinv(J.dot(N2)).dot(J.dot(N2))) joint_acc_3 = linalg.pinv(N3,rcond = 0.01).dot(-Kqd*qd + Kq*(qo - q) - (joint_acc + joint_acc_2)) self._joint_vel = self._joint_vel + (joint_acc + joint_acc_2 + joint_acc_3)/self._rate elif auto == 0 or nContact == False: # Level 2: X, Y, Z inequalities activation if phi2_max[0] > 0: phi_tmp_L2[0] = 1.0 dsigma_dp2[0][0] = (2.0*Sensor2R[0])/(2.0*np.sqrt(Sensor2R[0]*Sensor2R[0] + Sensor2R[1]*Sensor2R[1])) dsigma_dp2[1][0] = (2.0*Sensor2R[1])/(2.0*np.sqrt(Sensor2R[0]*Sensor2R[0] + Sensor2R[1]*Sensor2R[1])) else: phi_tmp_L2[0] = 0.0 dsigma_dp2[0][0] = 0.0 dsigma_dp2[1][0] = 0.0 #print "phi2_max[0]: ",np.sign(phi2_max[0]) # Yaw control in Level 2 for Exp 1 #if phi2_max[5] > 0: # phi_tmp_L2[5] = 1.0 # dsigma_dp2[5][5] = np.sign(Sensor2R[5]) #else: # phi_tmp_L2[5] = 0.0 # dsigma_dp2[5][5] = 0.0 U_plus2_map2[0][0] = U_plus2_map[0][0]*U_plus2 #U_plus_map2[5][5] = U_plus_map[5][5]*U_plus N2 = np.eye(7) - linalg.pinv((dsigma_dp.T).dot(J),rcond = 0.01).dot((dsigma_dp.T).dot(J)) joint_acc_2 = -linalg.pinv(((dsigma_dp2.T).dot(J)).dot(N2),rcond = 0.01).dot((np.sign(phi_tmp_L2.T).dot(U_plus2_map2)).T + J.dot(joint_acc)) #3rd Level cartesian self motion: #N3 = N2.dot(np.eye(7) - np.linalg.pinv(((dsigma_dp2.T).dot(J)).dot(N2)).dot(((dsigma_dp2.T).dot(J)).dot(N2))) #joint_acc_3 = linalg.pinv(J.dot(N3),rcond = 0.01).dot(-np.sign(pd)*Kpd_L3 - Kp_L3*pd - J.dot(joint_acc + joint_acc_2)) N3 = N2.dot(np.eye(7) - np.linalg.pinv(((dsigma_dp2.T).dot(J)).dot(N2)).dot(((dsigma_dp2.T).dot(J)).dot(N2))) joint_acc_3 = linalg.pinv(J.dot(N3),rcond = 0.01).dot(pdd_ref + Kpd*(pd_ref - pd) + Kp*error + np.sign(pd_ref - pd + Kp/Kpd*error)*Kpd_L3 - J.dot(joint_acc + joint_acc_2)) #if np.sqrt(Sensor2R[0]*Sensor2R[0] + Sensor2R[1]*Sensor2R[1]) > threshold: # joint_acc_3 = np.zeros((7,1)) #4th Level home configuration: N4 = N3.dot(np.eye(7) - np.linalg.pinv(J.dot(N3)).dot(J.dot(N3))) joint_acc_4 = linalg.pinv(N4,rcond = 0.01).dot(-Kqd*qd + Kq*(qo - q) - (joint_acc + joint_acc_2 + joint_acc_3)) self._joint_vel = self._joint_vel + (joint_acc + joint_acc_2 + joint_acc_3 + joint_acc_4)/self._rate cmd = make_cmd(self._limb.joint_names(), self._joint_vel) # command new joint torques self._limb.set_joint_velocities(cmd) b = "" b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(p.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(pd.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(error.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(qo.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(q.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(qd.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(joint_acc.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(joint_acc_2.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(joint_acc_3.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(joint_acc_4.T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray((joint_acc + joint_acc_2 + joint_acc_3 + joint_acc_4).T)),decimals = 4))) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(self._joint_vel.T)),decimals = 4))) b += " " + str(np.round(dsigma_dp[0][0],decimals = 4)) + " " + str(np.round(dsigma_dp[1][1],decimals = 4)) b += " " + str(np.round(dsigma_dp[2][2],decimals = 4)) + " " + str(np.round(dsigma_dp[3][3],decimals = 4)) b += " " + str(np.round(dsigma_dp[4][4],decimals = 4)) + " " + str(np.round(dsigma_dp[5][5],decimals = 4)) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(phi_tmp.T)),decimals = 4))) b += " " + str(np.round(U_plus_map2[2][2],decimals = 4)) + " " + str(np.round(U_plus_map2[3][3],decimals = 4)) + " " + str(np.round(U_plus_map2[4][4],decimals = 4)) b += " " + str(np.round(dsigma_dp2[0][0],decimals = 4)) + " " + str(np.round(dsigma_dp2[1][1],decimals = 4)) b += " " + str(np.round(dsigma_dp2[2][2],decimals = 4)) + " " + str(np.round(dsigma_dp2[3][3],decimals = 4)) b += " " + str(np.round(dsigma_dp2[4][4],decimals = 4)) + " " + str(np.round(dsigma_dp2[5][5],decimals = 4)) b += " " + ' '.join(map(str,np.around(np.squeeze(np.asarray(phi_tmp_L2.T)),decimals = 4))) b += " " + str(np.round(U_plus2_map2[0][0],decimals = 4)) return b
def _copysign(x1, x2): """Slow replacement for np.copysign, which was introduced in numpy 1.4""" return np.abs(x1) * np.sign(x2)
def _generate_terrain(self, hardcore): GRASS, STUMP, STAIRS, PIT, _STATES_ = range(5) state = GRASS velocity = 0.0 y = TERRAIN_HEIGHT counter = TERRAIN_STARTPAD oneshot = False self.terrain = [] self.terrain_x = [] self.terrain_y = [] for i in range(TERRAIN_LENGTH): x = i*TERRAIN_STEP self.terrain_x.append(x) if state==GRASS and not oneshot: velocity = 0.8*velocity + 0.01*np.sign(TERRAIN_HEIGHT - y) if i > TERRAIN_STARTPAD: velocity=0 #+= self.np_random.uniform(-1, 1)/SCALE #1 y += velocity #y = TERRAIN_HEIGHT elif state==PIT and oneshot: counter = self.np_random.randint(3, 5) poly = [ (x, y), (x+TERRAIN_STEP, y), (x+TERRAIN_STEP, y-4*TERRAIN_STEP), (x, y-4*TERRAIN_STEP), ] t = self.world.CreateStaticBody( fixtures = fixtureDef( shape=polygonShape(vertices=poly), friction = FRICTION )) t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6) self.terrain.append(t) t = self.world.CreateStaticBody( fixtures = fixtureDef( shape=polygonShape(vertices=[(p[0]+TERRAIN_STEP*counter,p[1]) for p in poly]), friction = FRICTION )) t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6) self.terrain.append(t) counter += 2 original_y = y elif state==PIT and not oneshot: y = original_y if counter > 1: y -= 4*TERRAIN_STEP elif state==STUMP and oneshot: counter = self.np_random.randint(1, 3) poly = [ (x, y), (x+counter*TERRAIN_STEP, y), (x+counter*TERRAIN_STEP, y+counter*TERRAIN_STEP), (x, y+counter*TERRAIN_STEP), ] t = self.world.CreateStaticBody( fixtures = fixtureDef( shape=polygonShape(vertices=poly), friction = FRICTION )) t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6) self.terrain.append(t) elif state==STAIRS and oneshot: stair_height = +1 if self.np_random.rand() > 0.5 else -1 stair_width = self.np_random.randint(4, 5) stair_steps = self.np_random.randint(3, 5) original_y = y for s in range(stair_steps): poly = [ (x+( s*stair_width)*TERRAIN_STEP, y+( s*stair_height)*TERRAIN_STEP), (x+((1+s)*stair_width)*TERRAIN_STEP, y+( s*stair_height)*TERRAIN_STEP), (x+((1+s)*stair_width)*TERRAIN_STEP, y+(-1+s*stair_height)*TERRAIN_STEP), (x+( s*stair_width)*TERRAIN_STEP, y+(-1+s*stair_height)*TERRAIN_STEP), ] t = self.world.CreateStaticBody( fixtures = fixtureDef( shape=polygonShape(vertices=poly), friction = FRICTION )) t.color1, t.color2 = (1,1,1), (0.6,0.6,0.6) self.terrain.append(t) counter = stair_steps*stair_width elif state==STAIRS and not oneshot: s = stair_steps*stair_width - counter - stair_height n = s/stair_width y = original_y + (n*stair_height)*TERRAIN_STEP oneshot = False self.terrain_y.append(y) counter -= 1 if counter==0: counter = self.np_random.randint(TERRAIN_GRASS/2, TERRAIN_GRASS) if state==GRASS and hardcore: state = self.np_random.randint(1, _STATES_) oneshot = True else: state = GRASS oneshot = True self.terrain_poly = [] for i in range(TERRAIN_LENGTH-1): poly = [ (self.terrain_x[i], self.terrain_y[i]), (self.terrain_x[i+1], self.terrain_y[i+1]) ] t = self.world.CreateStaticBody( fixtures = fixtureDef( shape=edgeShape(vertices=poly), friction = FRICTION, categoryBits=0x0001, )) color = (0.3, 1.0 if i%2==0 else 0.8, 0.3) t.color1 = color t.color2 = color self.terrain.append(t) color = (0.4, 0.6, 0.3) poly += [ (poly[1][0], 0), (poly[0][0], 0) ] self.terrain_poly.append( (poly, color) ) self.terrain.reverse()
def sign(z): return np.sign(z)
def step(self, action): if self.continuous: action = np.clip(action, -1, +1).astype(np.float32) else: assert self.action_space.contains(action), "%r (%s) invalid " % ( action, type(action), ) # Engines tip = (math.sin(self.lander.angle), math.cos(self.lander.angle)) side = (-tip[1], tip[0]) dispersion = [0 for _ in range(2)] m_power = 0.0 if (self.continuous and action[0] > 0.0) or ( not self.continuous and action == 2 ): # Main engine if self.continuous: m_power = (np.clip(action[0], 0.0, 1.0) + 1.0) * 0.5 # 0.5..1.0 assert m_power >= 0.5 and m_power <= 1.0 else: m_power = 1.0 ox = ( tip[0] * (4 / SCALE + 2 * dispersion[0]) + side[0] * dispersion[1] ) # 4 is move a bit downwards, +-2 for randomness oy = -tip[1] * (4 / SCALE + 2 * dispersion[0]) - side[1] * dispersion[1] impulse_pos = (self.lander.position[0] + ox, self.lander.position[1] + oy) p = self._create_particle( 3.5, # 3.5 is here to make particle speed adequate impulse_pos[0], impulse_pos[1], m_power, ) # particles are just a decoration p.ApplyLinearImpulse( (ox * MAIN_ENGINE_POWER * m_power, oy * MAIN_ENGINE_POWER * m_power), impulse_pos, True, ) self.lander.ApplyLinearImpulse( (-ox * MAIN_ENGINE_POWER * m_power, -oy * MAIN_ENGINE_POWER * m_power), impulse_pos, True, ) s_power = 0.0 if (self.continuous and np.abs(action[1]) > 0.5) or ( not self.continuous and action in [1, 3] ): # Orientation engines if self.continuous: direction = np.sign(action[1]) s_power = np.clip(np.abs(action[1]), 0.5, 1.0) assert s_power >= 0.5 and s_power <= 1.0 else: direction = action - 2 s_power = 1.0 ox = tip[0] * dispersion[0] + side[0] * ( 3 * dispersion[1] + direction * SIDE_ENGINE_AWAY / SCALE ) oy = -tip[1] * dispersion[0] - side[1] * ( 3 * dispersion[1] + direction * SIDE_ENGINE_AWAY / SCALE ) impulse_pos = ( self.lander.position[0] + ox - tip[0] * 17 / SCALE, self.lander.position[1] + oy + tip[1] * SIDE_ENGINE_HEIGHT / SCALE, ) p = self._create_particle(0.7, impulse_pos[0], impulse_pos[1], s_power) p.ApplyLinearImpulse( (ox * SIDE_ENGINE_POWER * s_power, oy * SIDE_ENGINE_POWER * s_power), impulse_pos, True, ) self.lander.ApplyLinearImpulse( (-ox * SIDE_ENGINE_POWER * s_power, -oy * SIDE_ENGINE_POWER * s_power), impulse_pos, True, ) self.world.Step(1.0 / FPS, 6 * 30, 2 * 30) pos = self.lander.position vel = self.lander.linearVelocity state = [ (pos.x - VIEWPORT_W / SCALE / 2) / (VIEWPORT_W / SCALE / 2), (pos.y - (self.helipad_y + LEG_DOWN / SCALE)) / (VIEWPORT_H / SCALE / 2), vel.x * (VIEWPORT_W / SCALE / 2) / FPS, vel.y * (VIEWPORT_H / SCALE / 2) / FPS, self.lander.angle, 20.0 * self.lander.angularVelocity / FPS, 1.0 if self.legs[0].ground_contact else 0.0, 1.0 if self.legs[1].ground_contact else 0.0, ] assert len(state) == 8 reward = 0 shaping = ( -100 * np.sqrt(state[0] * state[0] + state[1] * state[1]) - 100 * np.sqrt(state[2] * state[2] + state[3] * state[3]) - 100 * abs(state[4]) + 10 * state[6] + 10 * state[7] ) # And ten points for legs contact, the idea is if you # lose contact again after landing, you get negative reward if self.prev_shaping is not None: reward = shaping - self.prev_shaping self.prev_shaping = shaping reward -= ( m_power * 0.30 ) # less fuel spent is better, about -30 for heuristic landing reward -= s_power * 0.03 done = False if self.game_over or abs(state[0]) >= 1.0: done = True reward = -100 if not self.lander.awake: done = True reward = +100 return np.array(state, dtype=np.float32), reward, done, {}
def backward(self, x): return 0.5 * (np.sign(x) + 1)
def scores(key, paths, config): values = [mapreduce.OutputCollector(p) for p in paths] try: values = [item.load() for item in values] except Exception as e: print(e) return None y_true_splits = [item["y_true"].ravel() for item in values] y_pred_splits = [item["y_pred"].ravel() for item in values] y_true = np.concatenate(y_true_splits) y_pred = np.concatenate(y_pred_splits) prob_pred_splits = [item["prob_pred"].ravel() for item in values] prob_pred = np.concatenate(prob_pred_splits) # Prediction performances p, r, f, s = precision_recall_fscore_support(y_true, y_pred, average=None) auc = roc_auc_score(y_true, prob_pred) # balanced accuracy (recall_mean) bacc_splits = [recall_score(y_true_splits[f], y_pred_splits[f], average=None).mean() for f in range(len(y_true_splits))] auc_splits = [roc_auc_score(y_true_splits[f], prob_pred_splits[f]) for f in range(len(y_true_splits))] print("bacc all - mean(bacc) %.3f" % (r.mean() - np.mean(bacc_splits))) # P-values success = r * s success = success.astype('int') prob_class1 = np.count_nonzero(y_true) / float(len(y_true)) pvalue_recall0_true_prob = binom_test(success[0], s[0], 1 - prob_class1,alternative = 'greater') pvalue_recall1_true_prob = binom_test(success[1], s[1], prob_class1,alternative = 'greater') pvalue_recall0_unknwon_prob = binom_test(success[0], s[0], 0.5,alternative = 'greater') pvalue_recall1_unknown_prob = binom_test(success[1], s[1], 0.5,alternative = 'greater') pvalue_bacc = binom_test(success[0]+success[1], s[0] + s[1], p=0.5,alternative = 'greater') # Beta's measures of similarity betas = np.hstack([item["beta"][:, penalty_start:].T for item in values]).T # Correlation R = np.corrcoef(betas) R = R[np.triu_indices_from(R, 1)] # Fisher z-transformation / average z_bar = np.mean(1. / 2. * np.log((1 + R) / (1 - R))) # bracktransform r_bar = (np.exp(2 * z_bar) - 1) / (np.exp(2 * z_bar) + 1) # threshold betas to compute fleiss_kappa and DICE try: betas_t = np.vstack([ array_utils.arr_threshold_from_norm2_ratio(betas[i, :], .99)[0] for i in range(betas.shape[0])]) # Compute fleiss kappa statistics beta_signed = np.sign(betas_t) table = np.zeros((beta_signed.shape[1], 3)) table[:, 0] = np.sum(beta_signed == 0, 0) table[:, 1] = np.sum(beta_signed == 1, 0) table[:, 2] = np.sum(beta_signed == -1, 0) fleiss_kappa_stat = fleiss_kappa(table) # Paire-wise Dice coeficient ij = [[i, j] for i in range(betas.shape[0]) for j in range(i+1, betas.shape[0])] dices = list() for idx in ij: A, B = beta_signed[idx[0], :], beta_signed[idx[1], :] dices.append(float(np.sum((A == B)[(A != 0) & (B != 0)])) / (np.sum(A != 0) + np.sum(B != 0))) dice_bar = np.mean(dices) except: dice_bar = fleiss_kappa_stat = 0 # Proportion of selection within the support accross the CV support_count = (betas_t != 0).sum(axis=0) support_count = support_count[support_count > 0] support_prop = support_count / betas_t.shape[0] scores = OrderedDict() scores['key'] = key scores['recall_0'] = r[0] scores['recall_1'] = r[1] scores['bacc'] = r.mean() scores['bacc_se'] = np.std(bacc_splits) / np.sqrt(len(bacc_splits)) scores["auc"] = auc scores['auc_se'] = np.std(auc_splits) / np.sqrt(len(auc_splits)) scores['pvalue_recall0_true_prob_one_sided'] = pvalue_recall0_true_prob scores['pvalue_recall1_true_prob_one_sided'] = pvalue_recall1_true_prob scores['pvalue_recall0_unknwon_prob_one_sided'] = pvalue_recall0_unknwon_prob scores['pvalue_recall1_unknown_prob_one_sided'] = pvalue_recall1_unknown_prob scores['pvalue_bacc_mean'] = pvalue_bacc scores['prop_non_zeros_mean'] = float(np.count_nonzero(betas_t)) / \ float(np.prod(betas.shape)) scores['beta_r_bar'] = r_bar scores['beta_fleiss_kappa'] = fleiss_kappa_stat scores['beta_dice_bar'] = dice_bar scores['beta_dice'] = str(dices) scores['beta_r'] = str(R) scores['beta_support_prop_select_mean'] = support_prop.mean() scores['beta_support_prop_select_sd'] = support_prop.std() return scores
def start_PID(self, input=None): try: for sig in self.move_done_signals: sig.connect(self.move_done) for sig in self.grab_done_signals: sig.connect(self.det_done) self.current_time = time.perf_counter() self.status_sig.emit(["Update_Status", 'PID loop starting', 'log']) while self.running: #print('input: {}'.format(self.input)) ## GRAB DATA FIRST AND WAIT ALL DETECTORS RETURNED self.det_done_flag = False self.det_done_datas = OrderedDict() for ind_det, cmd in enumerate(self.detector_modules_commands): cmd.emit( ThreadCommand("single", [self.det_averaging[ind_det], ''])) QtWidgets.QApplication.processEvents() self.wait_for_det_done() self.input = self.model_class.convert_input( self.det_done_datas) ## EXECUTE THE PID output = self.pid(self.input) ## PROCESS THE OUTPUT IF NEEDED if output is not None and self.output is not None: if self.filter['enable']: if np.abs(output - self.output) <= self.filter['value']: self.output = output else: self.output += np.abs( self.filter['value']) * np.sign(output - self.output) self.pid._last_output = self.output else: self.output = output else: self.output = output ## APPLY THE PID OUTPUT TO THE ACTUATORS #print('output: {}'.format(self.output)) if self.output is None: self.output = self.pid.setpoint dt = time.perf_counter() - self.current_time self.output_to_actuator = self.model_class.convert_output( self.output, dt, stab=True) if not self.paused: self.move_done_positions = OrderedDict() for ind_mov, cmd in enumerate(self.move_modules_commands): cmd.emit( ThreadCommand('move_Abs', [self.output_to_actuator[ind_mov]])) QtWidgets.QApplication.processEvents() self.wait_for_move_done() self.current_time = time.perf_counter() QtWidgets.QApplication.processEvents() QThread.msleep(int(self.pid.sample_time * 1000)) self.status_sig.emit(["Update_Status", 'PID loop exiting', 'log']) for sig in self.move_done_signals: sig.disconnect(self.move_done) for sig in self.grab_done_signals: sig.disconnect(self.det_done) except Exception as e: self.status_sig.emit(["Update_Status", str(e), 'log'])
def _HypergraphPredictor__eval_i(args): g_i = args[0] b = args[1] phi = args[2] ratio = args[3] step = args[4] last_idx = len(g_i.vertex_name) - 1 res = nibble(g_i, last_idx, b, phi) res = res[res != last_idx] if (len(res) != 0): res_label = 1 - g_i.get_labels(res) res_multi = g_i.get_multi_item_labels(res) res_h = g_i.h_mat[res, :].sign() res_r = g_i.r_mat[res, :].sign() #pred = (res_label.sum()*0.5 + # (g_i.r_mat[res,:].dot(g_i.h_mat[last_idx, :].T) > 0).sum()*0.5)/res.shape[0] a = res_label.dot(1 - res_multi) / len(res) c = res_label.dot(res_multi) / len(res) pred = ((1 - a) * ratio + (1 - c) - np.sqrt((1 - a)**2 * ratio**2 + (1 - c)**2 + 2 * (a * c + (a + c) - 1) * ratio)) / (2 * ratio) # TODO: this is to try a different prediction method # pred_prod = (res_h.T.dot(1-res_label))/(res_h.sum(0).A1) with np.errstate(divide='ignore', invalid='ignore'): pred_prod = (res_r.T.dot(1 - res_label)) / (res_h.T.dot(1 - res_label)) idx = np.isnan(pred_prod) pred_prod[idx] = g_i.wgt[idx] else: pred = np.nan pred_prod = g_i.h_mat[:last_idx, :].sum(1) if g_i.mult_item_label[last_idx]: pred = pred * ratio lbl = g_i.get_label(last_idx) prd_order = (g_i.h_mat[last_idx, :] > 0).todense().A1 lbl_prod = np.sign(g_i.r_mat[last_idx, :].todense().A1) lbl_prod = lbl_prod[prd_order] mul_raise_rate = 1.0 num_known = False if step == 1: pred = pred * 0 + 1. res_h1 = (g_i.h_mat[last_idx, :] > 1).todense().A1 * mul_raise_rate + 1 pred_prod = pred_prod * res_h1 pred_prod = pred_prod[prd_order] * pred # print("Number assumption: %r; Raise multi item return rate by %f times" % # (num_known, mul_raise_rate)) if num_known: num_ret = sum(lbl_prod) if num_ret > 0: ind = np.argpartition(pred_prod, -num_ret)[-num_ret:] pred_prod = pred_prod * 0 pred_prod[ind] = 1 else: pred_prod = pred_prod * 0 return pred, lbl, pred_prod, lbl_prod
def path_metric_approx(last_pm, llr, ui): if ui == int(0.5 * (1 - np.sign(llr))): return last_pm return last_pm + np.abs(llr)
def _sign_flip(A): """ Flip the signs of A so that largest absolute value is positive. """ signs = np.sign(A[np.argmax(np.abs(A), axis=0), list(range(A.shape[1]))]) return signs * A