Skip to content

Commit ba61fd1

Browse files
committed
Improving code style, including typehints, updating docu.
1 parent 0f0d14b commit ba61fd1

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

pydmd/utils.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Utilities module."""
22

33
import warnings
4-
from typing import Union
4+
from numbers import Number
5+
from typing import Tuple
56

67
import numpy as np
78
from numpy.lib.stride_tricks import sliding_window_view
@@ -43,10 +44,11 @@ def _svht(sigma_svd: np.ndarray, rows: int, cols: int) -> int:
4344

4445

4546
def _compute_rank(
46-
sigma_svd: np.ndarray, rows: int, cols: int, svd_rank: Union[float, int]
47+
sigma_svd: np.ndarray, rows: int, cols: int, svd_rank: Number
4748
) -> int:
4849
"""
4950
Rank computation for the truncated Singular Value Decomposition.
51+
5052
:param sigma_svd: 1D singular values of SVD.
5153
:type sigma_svd: np.ndarray
5254
:param rows: Number of rows of original matrix.
@@ -62,6 +64,7 @@ def _compute_rank(
6264
:type svd_rank: int or float
6365
:return: the computed rank truncation.
6466
:rtype: int
67+
6568
References:
6669
Gavish, Matan, and David L. Donoho, The optimal hard threshold for
6770
singular values is, IEEE Transactions on Information Theory 60.8
@@ -80,10 +83,12 @@ def _compute_rank(
8083
return rank
8184

8285

83-
def compute_rank(X: np.ndarray, svd_rank=0):
86+
def compute_rank(X: np.ndarray, svd_rank: Number = 0) -> int:
8487
"""
8588
Rank computation for the truncated Singular Value Decomposition.
86-
:param numpy.ndarray X: the matrix to decompose.
89+
90+
:param X: the matrix to decompose.
91+
:type X: np.ndarray
8792
:param svd_rank: the rank for the truncation; If 0, the method computes
8893
the optimal rank and uses it for truncation; if positive interger,
8994
the method uses the argument for the truncation; if float between 0
@@ -93,6 +98,7 @@ def compute_rank(X: np.ndarray, svd_rank=0):
9398
:type svd_rank: int or float
9499
:return: the computed rank truncation.
95100
:rtype: int
101+
96102
References:
97103
Gavish, Matan, and David L. Donoho, The optimal hard threshold for
98104
singular values is, IEEE Transactions on Information Theory 60.8
@@ -102,18 +108,23 @@ def compute_rank(X: np.ndarray, svd_rank=0):
102108
return _compute_rank(s, X.shape[0], X.shape[1], svd_rank)
103109

104110

105-
def compute_tlsq(X, Y, tlsq_rank):
111+
def compute_tlsq(
112+
X: np.ndarray, Y: np.ndarray, tlsq_rank: int
113+
) -> Tuple[np.ndarray, np.ndarray]:
106114
"""
107115
Compute Total Least Square.
108116
109-
:param numpy.ndarray X: the first matrix;
110-
:param numpy.ndarray Y: the second matrix;
111-
:param int tlsq_rank: the rank for the truncation; If 0, the method
117+
:param X: the first matrix;
118+
:type X: np.ndarray
119+
:param Y: the second matrix;
120+
:type Y: np.ndarray
121+
:param tlsq_rank: the rank for the truncation; If 0, the method
112122
does not compute any noise reduction; if positive number, the
113123
method uses the argument for the SVD truncation used in the TLSQ
114124
method.
125+
:type tlsq_rank: int
115126
:return: the denoised matrix X, the denoised matrix Y
116-
:rtype: numpy.ndarray, numpy.ndarray
127+
:rtype: Tuple[np.ndarray, np.ndarray]
117128
118129
References:
119130
https://arxiv.org/pdf/1703.11004.pdf
@@ -130,11 +141,14 @@ def compute_tlsq(X, Y, tlsq_rank):
130141
return X.dot(VV), Y.dot(VV)
131142

132143

133-
def compute_svd(X, svd_rank=0):
144+
def compute_svd(
145+
X: np.ndarray, svd_rank: Number = 0
146+
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
134147
"""
135148
Truncated Singular Value Decomposition.
136149
137-
:param numpy.ndarray X: the matrix to decompose.
150+
:param X: the matrix to decompose.
151+
:type X: np.ndarray
138152
:param svd_rank: the rank for the truncation; If 0, the method computes
139153
the optimal rank and uses it for truncation; if positive interger,
140154
the method uses the argument for the truncation; if float between 0
@@ -144,7 +158,7 @@ def compute_svd(X, svd_rank=0):
144158
:type svd_rank: int or float
145159
:return: the truncated left-singular vectors matrix, the truncated
146160
singular values array, the truncated right-singular vectors matrix.
147-
:rtype: numpy.ndarray, numpy.ndarray, numpy.ndarray
161+
:rtype: Tuple[np.ndarray, np.ndarray, np.ndarray]
148162
149163
References:
150164
Gavish, Matan, and David L. Donoho, The optimal hard threshold for
@@ -162,7 +176,7 @@ def compute_svd(X, svd_rank=0):
162176
return U, s, V
163177

164178

165-
def pseudo_hankel_matrix(X: np.ndarray, d: int):
179+
def pseudo_hankel_matrix(X: np.ndarray, d: int) -> np.ndarray:
166180
"""
167181
Arrange the snapshot in the matrix `X` into the (pseudo) Hankel
168182
matrix. The attribute `d` controls the number of snapshot from `X` in

0 commit comments

Comments
 (0)