From 75f545a5e1386b066b86887edecf3272357afc90 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Thu, 17 Aug 2023 13:39:46 -0700 Subject: [PATCH 1/2] propogate ruff (#33) --- examples/kinematic_kf.py | 2 +- examples/live_kf.py | 26 +++++++++++++------------- pyproject.toml | 7 ++++--- rednose/helpers/kalmanfilter.py | 14 +++++++------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/examples/kinematic_kf.py b/examples/kinematic_kf.py index 29049a7..7c744c3 100755 --- a/examples/kinematic_kf.py +++ b/examples/kinematic_kf.py @@ -73,7 +73,7 @@ def __init__(self, generated_dir): dim_state_err = self.initial_P_diag.shape[0] # init filter - self.filter = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), dim_state, dim_state_err) + self.filter_func = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), dim_state, dim_state_err) if __name__ == "__main__": diff --git a/examples/live_kf.py b/examples/live_kf.py index dd22b73..c78ec10 100755 --- a/examples/live_kf.py +++ b/examples/live_kf.py @@ -258,22 +258,22 @@ def __init__(self, generated_dir): ObservationKind.ECEF_POS: np.diag([5**2, 5**2, 5**2])} # init filter - self.filter = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), self.dim_state, self.dim_state_err) + self.filter_func = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), self.dim_state, self.dim_state_err) @property def x(self): - return self.filter.state() + return self.filter_func.state() @property def t(self): - return self.filter.filter_time + return self.filter_func.filter_time @property def P(self): - return self.filter.covs() + return self.filter_func.covs() def rts_smooth(self, estimates): - return self.filter.rts_smooth(estimates, norm_quats=True) + return self.filter_func.rts_smooth(estimates, norm_quats=True) def init_state(self, state, covs_diag=None, covs=None, filter_time=None): if covs_diag is not None: @@ -281,8 +281,8 @@ def init_state(self, state, covs_diag=None, covs=None, filter_time=None): elif covs is not None: P = covs else: - P = self.filter.covs() - self.filter.init_state(state, P, filter_time) + P = self.filter_func.covs() + self.filter_func.init_state(state, P, filter_time) def predict_and_observe(self, t, kind, data): if len(data) > 0: @@ -294,16 +294,16 @@ def predict_and_observe(self, t, kind, data): elif kind == ObservationKind.ODOMETRIC_SPEED: r = self.predict_and_update_odo_speed(data, t, kind) else: - r = self.filter.predict_and_update_batch(t, kind, data, self.get_R(kind, len(data))) + r = self.filter_func.predict_and_update_batch(t, kind, data, self.get_R(kind, len(data))) # Normalize quats - quat_norm = np.linalg.norm(self.filter.x[3:7, 0]) + quat_norm = np.linalg.norm(self.filter_func.x[3:7, 0]) # Should not continue if the quats behave this weirdly if not (0.1 < quat_norm < 10): raise KalmanError("Kalman filter quaternions unstable") - self.filter.x[States.ECEF_ORIENTATION, 0] = self.filter.x[States.ECEF_ORIENTATION, 0] / quat_norm + self.filter_func.x[States.ECEF_ORIENTATION, 0] = self.filter_func.x[States.ECEF_ORIENTATION, 0] / quat_norm return r @@ -320,21 +320,21 @@ def predict_and_update_odo_speed(self, speed, t, kind): R = np.zeros((len(speed), 1, 1)) for i, _ in enumerate(z): R[i, :, :] = np.diag([0.2**2]) - return self.filter.predict_and_update_batch(t, kind, z, R) + return self.filter_func.predict_and_update_batch(t, kind, z, R) def predict_and_update_odo_trans(self, trans, t, kind): z = trans[:, :3] R = np.zeros((len(trans), 3, 3)) for i, _ in enumerate(z): R[i, :, :] = np.diag(trans[i, 3:]**2) - return self.filter.predict_and_update_batch(t, kind, z, R) + return self.filter_func.predict_and_update_batch(t, kind, z, R) def predict_and_update_odo_rot(self, rot, t, kind): z = rot[:, :3] R = np.zeros((len(rot), 3, 3)) for i, _ in enumerate(z): R[i, :, :] = np.diag(rot[i, 3:]**2) - return self.filter.predict_and_update_batch(t, kind, z, R) + return self.filter_func.predict_and_update_batch(t, kind, z, R) if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index d08c406..bac2d7f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,7 @@ # https://beta.ruff.rs/docs/configuration/#using-pyprojecttoml [tool.ruff] -select = ["E", "F", "W"] -ignore = ["W292", "E741"] +select = ["E", "F", "W", "PIE", "C4", "ISC", "RUF100", "A"] +ignore = ["W292", "E741", "E402", "C408", "ISC003"] line-length = 160 -target-version="py311" \ No newline at end of file +target-version="py311" +flake8-implicit-str-concat.allow-multiline=false diff --git a/rednose/helpers/kalmanfilter.py b/rednose/helpers/kalmanfilter.py index 0c30e49..39a8ee7 100644 --- a/rednose/helpers/kalmanfilter.py +++ b/rednose/helpers/kalmanfilter.py @@ -10,19 +10,19 @@ class KalmanFilter: Q = np.zeros((0, 0)) obs_noise: Dict[int, Any] = {} - filter = None # Should be initialized when initializating a KalmanFilter implementation + filter_func = None # Should be initialized when initializating a KalmanFilter implementation @property def x(self): - return self.filter.state() + return self.filter_func.state() @property def t(self): - return self.filter.get_filter_time() + return self.filter_func.get_filter_time() @property def P(self): - return self.filter.covs() + return self.filter_func.covs() def init_state(self, state, covs_diag=None, covs=None, filter_time=None): if covs_diag is not None: @@ -30,8 +30,8 @@ def init_state(self, state, covs_diag=None, covs=None, filter_time=None): elif covs is not None: P = covs else: - P = self.filter.covs() - self.filter.init_state(state, P, filter_time) + P = self.filter_func.covs() + self.filter_func.init_state(state, P, filter_time) def get_R(self, kind, n): obs_noise = self.obs_noise[kind] @@ -48,4 +48,4 @@ def predict_and_observe(self, t, kind, data, R=None): if R is None: R = self.get_R(kind, len(data)) - self.filter.predict_and_update_batch(t, kind, data, R) + self.filter_func.predict_and_update_batch(t, kind, data, R) From e32658ed9164d6e8dde882c05c5ece9707acde82 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 29 Aug 2023 15:51:37 -0700 Subject: [PATCH 2/2] remain compatible with other submodules (#35) * Revert "propogate ruff (#33)" This reverts commit 75f545a5e1386b066b86887edecf3272357afc90. * readd config * fix ruff * revert that --- examples/kinematic_kf.py | 2 +- examples/live_kf.py | 26 +++++++++++++------------- rednose/helpers/ekf_sym.py | 2 +- rednose/helpers/kalmanfilter.py | 15 ++++++++------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/examples/kinematic_kf.py b/examples/kinematic_kf.py index 7c744c3..29049a7 100755 --- a/examples/kinematic_kf.py +++ b/examples/kinematic_kf.py @@ -73,7 +73,7 @@ def __init__(self, generated_dir): dim_state_err = self.initial_P_diag.shape[0] # init filter - self.filter_func = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), dim_state, dim_state_err) + self.filter = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), dim_state, dim_state_err) if __name__ == "__main__": diff --git a/examples/live_kf.py b/examples/live_kf.py index c78ec10..dd22b73 100755 --- a/examples/live_kf.py +++ b/examples/live_kf.py @@ -258,22 +258,22 @@ def __init__(self, generated_dir): ObservationKind.ECEF_POS: np.diag([5**2, 5**2, 5**2])} # init filter - self.filter_func = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), self.dim_state, self.dim_state_err) + self.filter = EKF_sym_pyx(generated_dir, self.name, self.Q, self.initial_x, np.diag(self.initial_P_diag), self.dim_state, self.dim_state_err) @property def x(self): - return self.filter_func.state() + return self.filter.state() @property def t(self): - return self.filter_func.filter_time + return self.filter.filter_time @property def P(self): - return self.filter_func.covs() + return self.filter.covs() def rts_smooth(self, estimates): - return self.filter_func.rts_smooth(estimates, norm_quats=True) + return self.filter.rts_smooth(estimates, norm_quats=True) def init_state(self, state, covs_diag=None, covs=None, filter_time=None): if covs_diag is not None: @@ -281,8 +281,8 @@ def init_state(self, state, covs_diag=None, covs=None, filter_time=None): elif covs is not None: P = covs else: - P = self.filter_func.covs() - self.filter_func.init_state(state, P, filter_time) + P = self.filter.covs() + self.filter.init_state(state, P, filter_time) def predict_and_observe(self, t, kind, data): if len(data) > 0: @@ -294,16 +294,16 @@ def predict_and_observe(self, t, kind, data): elif kind == ObservationKind.ODOMETRIC_SPEED: r = self.predict_and_update_odo_speed(data, t, kind) else: - r = self.filter_func.predict_and_update_batch(t, kind, data, self.get_R(kind, len(data))) + r = self.filter.predict_and_update_batch(t, kind, data, self.get_R(kind, len(data))) # Normalize quats - quat_norm = np.linalg.norm(self.filter_func.x[3:7, 0]) + quat_norm = np.linalg.norm(self.filter.x[3:7, 0]) # Should not continue if the quats behave this weirdly if not (0.1 < quat_norm < 10): raise KalmanError("Kalman filter quaternions unstable") - self.filter_func.x[States.ECEF_ORIENTATION, 0] = self.filter_func.x[States.ECEF_ORIENTATION, 0] / quat_norm + self.filter.x[States.ECEF_ORIENTATION, 0] = self.filter.x[States.ECEF_ORIENTATION, 0] / quat_norm return r @@ -320,21 +320,21 @@ def predict_and_update_odo_speed(self, speed, t, kind): R = np.zeros((len(speed), 1, 1)) for i, _ in enumerate(z): R[i, :, :] = np.diag([0.2**2]) - return self.filter_func.predict_and_update_batch(t, kind, z, R) + return self.filter.predict_and_update_batch(t, kind, z, R) def predict_and_update_odo_trans(self, trans, t, kind): z = trans[:, :3] R = np.zeros((len(trans), 3, 3)) for i, _ in enumerate(z): R[i, :, :] = np.diag(trans[i, 3:]**2) - return self.filter_func.predict_and_update_batch(t, kind, z, R) + return self.filter.predict_and_update_batch(t, kind, z, R) def predict_and_update_odo_rot(self, rot, t, kind): z = rot[:, :3] R = np.zeros((len(rot), 3, 3)) for i, _ in enumerate(z): R[i, :, :] = np.diag(rot[i, 3:]**2) - return self.filter_func.predict_and_update_batch(t, kind, z, R) + return self.filter.predict_and_update_batch(t, kind, z, R) if __name__ == "__main__": diff --git a/rednose/helpers/ekf_sym.py b/rednose/helpers/ekf_sym.py index b60c202..de68991 100644 --- a/rednose/helpers/ekf_sym.py +++ b/rednose/helpers/ekf_sym.py @@ -192,7 +192,7 @@ def gen_code(folder, name, f_sym, dt_sym, x_sym, obs_eqs, dim_x, dim_err, eskf_p for group, kinds in func_lists.items(): post_code += f" .{group}s = {{\n" for kind in kinds: - str_kind = f"\"{kind}\"" if type(kind) == str else kind + str_kind = f"\"{kind}\"" if isinstance(kind, str) else kind post_code += f" {{ {str_kind}, {name}_{group}_{kind} }},\n" post_code += " },\n" post_code += " .extra_routines = {\n" diff --git a/rednose/helpers/kalmanfilter.py b/rednose/helpers/kalmanfilter.py index 39a8ee7..d541faa 100644 --- a/rednose/helpers/kalmanfilter.py +++ b/rednose/helpers/kalmanfilter.py @@ -10,19 +10,20 @@ class KalmanFilter: Q = np.zeros((0, 0)) obs_noise: Dict[int, Any] = {} - filter_func = None # Should be initialized when initializating a KalmanFilter implementation + # Should be initialized when initializating a KalmanFilter implementation + filter = None # noqa: A003 @property def x(self): - return self.filter_func.state() + return self.filter.state() @property def t(self): - return self.filter_func.get_filter_time() + return self.filter.get_filter_time() @property def P(self): - return self.filter_func.covs() + return self.filter.covs() def init_state(self, state, covs_diag=None, covs=None, filter_time=None): if covs_diag is not None: @@ -30,8 +31,8 @@ def init_state(self, state, covs_diag=None, covs=None, filter_time=None): elif covs is not None: P = covs else: - P = self.filter_func.covs() - self.filter_func.init_state(state, P, filter_time) + P = self.filter.covs() + self.filter.init_state(state, P, filter_time) def get_R(self, kind, n): obs_noise = self.obs_noise[kind] @@ -48,4 +49,4 @@ def predict_and_observe(self, t, kind, data, R=None): if R is None: R = self.get_R(kind, len(data)) - self.filter_func.predict_and_update_batch(t, kind, data, R) + self.filter.predict_and_update_batch(t, kind, data, R)