Skip to content

Commit

Permalink
strict mypy typechecking for the package
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdi committed Nov 22, 2023
1 parent e0a023d commit 3cd777d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
38 changes: 20 additions & 18 deletions pyssp/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -29,14 +31,15 @@ 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.
Also calculates minimum error achieved.
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.")

Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
1 change: 0 additions & 1 deletion pyssp/optimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np


# pylint: disable=R0914
def kalman(y, A, C, sigmaw, sigmav):
"""Kalman Filter.
Expand Down

0 comments on commit 3cd777d

Please sign in to comment.