From 3cd777d9894f78a408c6c7b3c8391a02d2c05196 Mon Sep 17 00:00:00 2001 From: mahdi Date: Tue, 21 Nov 2023 23:21:24 -0500 Subject: [PATCH] strict mypy typechecking for the package --- pyproject.toml | 4 ++++ pyssp/modeling.py | 38 ++++++++++++++++++++------------------ pyssp/optimal.py | 1 - 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 26a046a..2960bd9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,4 +35,8 @@ convention = "google" [tool.mypy] mypy_path = "pyssp" ignore_missing_imports = true +disallow_incomplete_defs = true +disallow_untyped_calls = true +disallow_untyped_defs = true +disallow_untyped_decorators = true plugins = "numpy.typing.mypy_plugin" diff --git a/pyssp/modeling.py b/pyssp/modeling.py index b71206b..32cd301 100644 --- a/pyssp/modeling.py +++ b/pyssp/modeling.py @@ -2,21 +2,23 @@ import numpy as np +import numpy.typing as npt import scipy as sp from .state import convm -def pade(x, p, q): +def pade(x: npt.ArrayLike, p: int, q: int): """Reference Page 138, Table 4.1. The Pade approximation models a signal as the unis sample response of linear shift invariant system have p poles and q zeros. """ - if p + q > len(x): + _x = np.array(x) + if p + q > len(_x): raise ValueError(f"Model order {p + q} is too large.") - X = convm(x, p + 1) + X = convm(_x, p + 1) # Linear difference matrix spanning the number of zeros Xq = X[q + 1:q + p + 1, 1:p + 1].copy() @@ -29,7 +31,7 @@ def pade(x, p, q): return (a, b) -def prony(x, p, q): +def prony(x: npt.ArrayLike, p: int, q: int): """Least square minimization of poles to get denominator coefficients. Solves directly (Pade method) to get numerator coefficients. @@ -37,6 +39,7 @@ def prony(x, p, q): Condition to energy_match is on page 575 """ + x = np.array(x) if p + q > len(x): raise ValueError(f"Model order {p + q} is too large.") @@ -74,8 +77,9 @@ def prony(x, p, q): return a, b, err -def shanks(x, p, q): +def shanks(x: npt.ArrayLike, p: int, q: int): """Shank's method.""" + x = np.array(x) N = len(x) if p + q >= N: raise ValueError(f"Model order {p + q} is too large.") @@ -103,9 +107,9 @@ def shanks(x, p, q): return a, b, err -def spike(g, n0, n): +def spike(g: npt.ArrayLike, n0: int, n: int): """Leaset Squares Inverse Filter.""" - g = g.reshape(-1, 1) + g = np.array(g).reshape(-1, 1) m = len(g) if m + n - 1 <= n0: @@ -124,16 +128,14 @@ def spike(g, n0, n): return h -def ipf(): - """Iterative Pre-Filtering. - - Arguements: (x, p, q, n=10, a=None) - """ +def ipf(x: npt.ArrayLike, p: int, q: int, n: int = 10, a = None): + """Iterative Pre-Filtering.""" + raise NotImplementedError() -def acm(x, p) -> tuple[np.ndarray, np.ndarray]: +def acm(x: npt.ArrayLike, p: int) -> tuple[np.ndarray, np.ndarray]: """The auto-correlation method.""" - x0 = x.copy().ravel().reshape(-1, 1) + x0 = np.array(x).ravel().reshape(-1, 1) N = len(x0) if p >= len(x0): raise ValueError("p (all-pole model) too large") @@ -150,9 +152,9 @@ def acm(x, p) -> tuple[np.ndarray, np.ndarray]: return a, err -def covm(x, p): +def covm(x: npt.ArrayLike, p: int): """Solve the complete Prony normal equations.""" - x0 = x.copy().ravel().reshape(-1, 1) + x0 = np.array(x).ravel().reshape(-1, 1) N = len(x0) if p >= len(x0): raise ValueError(f"{p=} all-pole model too large") @@ -169,9 +171,9 @@ def covm(x, p): return a, err -def durbin(x, p, q): +def durbin(x: npt.ArrayLike, p: int, q: int): """The durbin method.""" - x0 = x.copy().ravel().reshape(-1, 1) + x0 = np.array(x).ravel().reshape(-1, 1) # N = len(x0) if p >= len(x0): raise ValueError("p (all-pole model) too large") diff --git a/pyssp/optimal.py b/pyssp/optimal.py index 1176c29..3ea4ed7 100644 --- a/pyssp/optimal.py +++ b/pyssp/optimal.py @@ -4,7 +4,6 @@ import numpy as np -# pylint: disable=R0914 def kalman(y, A, C, sigmaw, sigmav): """Kalman Filter.