Best Python code snippet using grail_python
ExtendedKalmanFilter.py
Source:ExtendedKalmanFilter.py  
...36        elif self.x[2,:] > np.pi:37            self.x[2,:] = self.x[2,:] - 2*np.pi38        else:39            pass40    def external_step(self, u):41        self.internal_step(u)42        z = np.matmul(self.C, self.x) + np.reshape(self.v_sig * np.random.randn(2), (2,1))43        return z44class ExtendedKalmanFilter(object):45    def __init__(self, T=0.05):46        self.T = T47        self.X = np.array([0,0,0], dtype=float).reshape([-1,1])48        self.P = np.diag([2,2,2])49        self.Q = self.T * np.diag([0,0,0.04])50        self.R = np.diag([0.01,0.01])/self.T51        self.H = np.array([52                            [1,0,0],53                            [0,1,0],54        ], dtype=float)55    def prediction(self,u):56        dX_pre = np.zeros((3,1), dtype=float)57        dX_pre[0,:] = u[0] * np.cos(self.X[2,:])58        dX_pre[1,:] = u[0] * np.sin(self.X[2,:])59        dX_pre[2,:] = u[1]60        self.X += (self.T * dX_pre)61        if self.X[2,:] < -np.pi:62            self.X[2,:] = self.x[2,:] + 2*np.pi63        elif self.X[2,:] > np.pi:64            self.X[2,:] = self.x[2,:] - 2*np.pi65        else:66            pass67            68        self.calc_F(self.X,u)69        self.P = self.F.dot(self.P).dot(self.F.T) + self.Q70    def correction(self, Z):71        K = self.P.dot(self.H.T).dot(np.linalg.inv(self.H.dot(self.P).dot(self.H.T) + self.R))72        Y = self.H.dot(self.X)73        self.X = self.X + K.dot(Z-Y)74        self.P = self.P - K.dot(self.H).dot(self.P)75    def calc_F(self, x, u):76        self.F = np.zeros_like(self.Q, dtype=float)77        self.F[0,2] = -u[0]*np.sin(x[2,:])78        self.F[1,2] = u[0] *np.cos(x[2,:])79        self.F = np.identity(x.shape[0]) + self.T * self.F80x_hat_hist, P_hist, z_hist, p_actual = [], [], [], []81env = UNICAR()82env.init_state()83ekf_estimator = ExtendedKalmanFilter()84u = 0.5 * np.ones((100,2))85for i in range(100):86    z = env.external_step(u[i,:])87    ekf_estimator.prediction(u[i,:])88    ekf_estimator.correction(z)89    print(ekf_estimator.X)90    x_hat_hist.append(ekf_estimator.X)91    P_hist.append(ekf_estimator.P)92    z_hist.append(z)93    p_actual.append(env.x[:2,:])94pos_hat = np.matmul(ekf_estimator.H, np.hstack(x_hat_hist))95z_np = np.hstack(z_hist)96p_np = np.hstack(p_actual)97plt.figure(figsize=(15,15))98plt.plot(pos_hat[0,:], pos_hat[1,:],"r-")99plt.plot(z_np[0,:], z_np[1,:], "k-")100plt.plot(p_np[0,:], p_np[1,:],"b-")...KalmanFilter.py
Source:KalmanFilter.py  
...15        self.w_sig = w_sig16        self.v_sig = v_sig17    def internal_step(self, u):18        self.x = np.matmul(self.Ad, self.x) + self.Bd * u + self.w_sig * np.random.randn(1)19    def external_step(self,u):20        self.internal_step(u)21        z = np.matmul(self.C, self.x) + self.v_sig * np.random.randn(1)22        return z23class KALMANFilter(object):24    def __init__(self, T=0.1, Q=np.diag([0,0.25]),R=1):25        self.T = T26        self.A = np.array([[0,1],[0,0]])27        self.B = np.array([[0],[1]])28        self.Ad = np.identity(2) + self.T * self.A29        self.Bd = self.T * self.B30        self.C = np.array([[1,0]])31        self.x_hat = np.array([[0],[0]])32        self.P = np.diag([10,10])33        self.Q = Q34        self.R = R/self.T35    def prediction(self, u):36        self.x_hat = np.matmul(self.Ad, self.x_hat) + self.Bd * u37        self.P = np.matmul(np.matmul(self.Ad, self.P), self.Ad.T) + self.Q38    def measurement(self, z):39        S = np.matmul(self.C, np.matmul(self.P, self.C.T))+ self.R40        K = np.matmul(np.matmul(self.P,self.C.T),np.linalg.inv(S))41        self.x_hat = self.x_hat + np.matmul(K, (z - np.matmul(self.C, self.x_hat)))42        self.P = np.matmul(np.identity(2) - np.matmul(K, self.C), self.P)43x_hat_hist = []44p_hist = []45z_hist = []46v_actual =[]47env = one_dim_env()48estimator = KALMANFilter()49u = np.concatenate([np.ones((30,)), np.zeros((40,)), -0.5*np.ones((30,))])50for i in range(100):51    z = env.external_step(u[i])52    estimator.prediction(u[i])53    estimator.measurement(z)54    x_hat_hist.append(estimator.x_hat)55    p_hist.append(estimator.P)56    z_hist.append(z)57    v_actual.append(env.x[1,:])58pos_hat = np.matmul(estimator.C, np.hstack(x_hat_hist)).squeeze(0)59vel_hat = np.hstack(x_hat_hist)[1,:]60sig_p = np.sqrt(np.vstack(p_hist).reshape([-1,2,2])[:,0,0])61sig_v = np.sqrt(np.vstack(p_hist).reshape([-1,2,2])[:,1,1])62z_np = np.hstack(z_hist).squeeze()63v_np = np.hstack(v_actual).squeeze()64con=365plt.figure(figsize=(20, 20))...7_treat_nested_steps_as_methods.py
Source:7_treat_nested_steps_as_methods.py  
1from grail import BaseTest, step2class DoItInVerySpecialCases(BaseTest):3    def test_its_not_recommended_to_do_this(self):4        self.external_step()5    @step(treat_nested_steps_as_methods=True)6    def external_step(self):7        self.this_is_not_a_step_anymore()8    @step9    def this_is_not_a_step_anymore(self):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
