Skip to content

Commit bc5f7fe

Browse files
committed
renaming function, code style unit test
1 parent 532212c commit bc5f7fe

File tree

2 files changed

+75
-79
lines changed

2 files changed

+75
-79
lines changed

pydmd/varprodmd.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def compute_varprodmd_any( # pylint: disable=unused-variable
424424
return xi / eigenf[None], omegas, eigenf, indices, opt
425425

426426

427-
def optdmd_predict(
427+
def varprodmd_predict(
428428
phi: np.ndarray,
429429
omegas: np.ndarray,
430430
eigenf: np.ndarray,
@@ -575,14 +575,12 @@ def compute_operator(
575575
if self._sorted_eigs == "auto":
576576
eigs_real = self._eigenvalues.real
577577
eigs_imag = self._eigenvalues.imag
578-
__eigs_abs = np.abs(self._eigenvalues)
578+
_eigs_abs = np.abs(self._eigenvalues)
579579
var_real = np.var(eigs_real)
580580
var_imag = np.var(eigs_imag)
581-
var_abs = np.var(__eigs_abs)
582-
__array = np.array([var_real, var_imag, var_abs])
583-
eigs_abs = (eigs_real, eigs_imag, __eigs_abs)[
584-
np.argmax(__array)
585-
]
581+
var_abs = np.var(_eigs_abs)
582+
array = np.array([var_real, var_imag, var_abs])
583+
eigs_abs = (eigs_real, eigs_imag, _eigs_abs)[np.argmax(array)]
586584

587585
elif self._sorted_eigs == "real":
588586
eigs_abs = np.abs(self._eigenvalues.real)
@@ -726,7 +724,7 @@ def forecast(self, time: np.ndarray) -> np.ndarray:
726724
if not self.fitted:
727725
raise ValueError("Nothing fitted yet. Call fit-method first!")
728726

729-
return optdmd_predict(
727+
return varprodmd_predict(
730728
self._Atilde.modes, self._Atilde.eigenvalues, self._b, time
731729
)
732730

tests/test_varprodmd.py

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
_compute_dmd_rho,
1212
_OptimizeHelper,
1313
compute_varprodmd_any,
14-
optdmd_predict,
14+
varprodmd_predict,
1515
select_best_samples_fast,
1616
)
1717

@@ -26,9 +26,9 @@ def signal(x_loc: np.ndarray, time: np.ndarray) -> np.ndarray:
2626
:return: Spatiotemporal signal.
2727
:rtype: np.ndarray
2828
"""
29-
__f_1 = 1.0 / np.cosh(x_loc + 3) * np.exp(1j * 2.3 * time)
30-
__f_2 = 2.0 / np.cosh(x_loc) * np.tanh(x_loc) * np.exp(1j * 2.8 * time)
31-
return __f_1 + __f_2
29+
f_1 = 1.0 / np.cosh(x_loc + 3) * np.exp(1j * 2.3 * time)
30+
f_2 = 2.0 / np.cosh(x_loc) * np.tanh(x_loc) * np.exp(1j * 2.8 * time)
31+
return f_1 + f_2
3232

3333

3434
def test_varprodmd_rho():
@@ -40,23 +40,25 @@ def test_varprodmd_rho():
4040
alphas = np.array([1.0 + 0j, 1.0 + 0j], np.complex128)
4141
alphas_in = np.array([1.0, 1.0, 0.0, 0.0], np.float64)
4242
phi = np.exp(np.outer(time, alphas))
43-
__u, __s, __v_t = np.linalg.svd(phi, hermitian=False, full_matrices=False)
44-
__idx = np.where(__s.real != 0.0)[0]
45-
__s_inv = np.zeros_like(__s)
46-
__s_inv[__idx] = np.reciprocal(__s[__idx])
43+
U_svd, s_svd, V_svd_t = np.linalg.svd(
44+
phi, hermitian=False, full_matrices=False
45+
)
46+
idx = np.where(s_svd.real != 0.0)[0]
47+
s_inv = np.zeros_like(s_svd)
48+
s_inv[idx] = np.reciprocal(s_svd[idx])
4749

48-
res = data - np.linalg.multi_dot([__u, __u.conj().T, data])
50+
res = data - np.linalg.multi_dot([U_svd, U_svd.conj().T, data])
4951
res_flat = np.ravel(res)
5052
res_flat_reals = np.zeros((2 * res_flat.shape[-1]))
5153
res_flat_reals[: res_flat_reals.shape[-1] // 2] = res_flat.real
5254
res_flat_reals[res_flat_reals.shape[-1] // 2 :] = res_flat.imag
5355
opthelper = _OptimizeHelper(2, *data.shape)
5456
rho_flat_out = _compute_dmd_rho(alphas_in, time, data, opthelper)
55-
assert np.array_equal(rho_flat_out, res_flat_reals)
56-
assert np.array_equal(__u, opthelper.u_svd)
57-
assert np.array_equal(__s_inv, opthelper.s_inv)
58-
assert np.array_equal(__v_t.conj().T, opthelper.v_svd)
5957

58+
assert np.array_equal(rho_flat_out, res_flat_reals)
59+
assert np.array_equal(U_svd, opthelper.u_svd)
60+
assert np.array_equal(s_inv, opthelper.s_inv)
61+
assert np.array_equal(V_svd_t.conj().T, opthelper.v_svd)
6062
assert np.array_equal(phi, opthelper.phi)
6163

6264

@@ -74,16 +76,16 @@ def test_varprodmd_jac(): # pylint: disable=too-many-locals,too-many-statements
7476
d_phi_1[:, 0] = time * phi[:, 0]
7577
d_phi_2[:, 1] = time * phi[:, 1]
7678

77-
__u, __s, __v = np.linalg.svd(phi, hermitian=False, full_matrices=False)
78-
__idx = np.where(__s.real != 0.0)[0]
79-
__s_inv = np.zeros_like(__s)
80-
__s_inv[__idx] = np.reciprocal(__s[__idx])
81-
phi_inv = (__v.conj().T * __s_inv.reshape((1, -1))) @ __u.conj().T
79+
U_svd, s_svd, __v = np.linalg.svd(phi, hermitian=False, full_matrices=False)
80+
idx = np.where(s_svd.real != 0.0)[0]
81+
s_inv = np.zeros_like(s_svd)
82+
s_inv[idx] = np.reciprocal(s_svd[idx])
83+
phi_inv = (__v.conj().T * s_inv.reshape((1, -1))) @ U_svd.conj().T
8284

8385
opthelper = _OptimizeHelper(2, *data.shape)
84-
opthelper.u_svd = __u
86+
opthelper.u_svd = U_svd
8587
opthelper.v_svd = __v.conj().T
86-
opthelper.s_inv = __s_inv
88+
opthelper.s_inv = s_inv
8789
opthelper.phi = phi
8890
opthelper.phi_inv = phi_inv
8991
opthelper.b_matrix = phi_inv @ data
@@ -93,11 +95,11 @@ def test_varprodmd_jac(): # pylint: disable=too-many-locals,too-many-statements
9395
rho_real[: rho_flat.shape[0]] = rho_flat.real
9496
rho_real[rho_flat.shape[0] :] = rho_flat.imag
9597
A_1 = d_phi_1 @ opthelper.b_matrix - np.linalg.multi_dot(
96-
[__u, __u.conj().T, d_phi_1, opthelper.b_matrix]
98+
[U_svd, U_svd.conj().T, d_phi_1, opthelper.b_matrix]
9799
)
98100

99101
A_2 = d_phi_2 @ opthelper.b_matrix - np.linalg.multi_dot(
100-
[__u, __u.conj().T, d_phi_2, opthelper.b_matrix]
102+
[U_svd, U_svd.conj().T, d_phi_2, opthelper.b_matrix]
101103
)
102104

103105
G_1 = np.linalg.multi_dot(
@@ -122,34 +124,34 @@ def test_varprodmd_jac(): # pylint: disable=too-many-locals,too-many-statements
122124
JAC_REAL[J_1_flat.shape[-1] :, 2] = J_1_flat.real
123125
JAC_REAL[: J_2_flat.shape[-1], 3] = -J_2_flat.imag
124126
JAC_REAL[J_2_flat.shape[-1] :, 3] = J_2_flat.real
125-
__JAC_OUT_REAL = _compute_dmd_jac(alphas_in, time, data, opthelper)
127+
JAC_OUT_REAL = _compute_dmd_jac(alphas_in, time, data, opthelper)
126128

127129
GRAD_REAL = JAC_REAL.T @ rho_real
128-
__GRAD_REAL = __JAC_OUT_REAL.T @ rho_real
130+
GRAD_OUT_REAL = JAC_OUT_REAL.T @ rho_real
129131
GRAD_IMAG = JAC_IMAG.conj().T @ rho_flat
130132

131-
assert np.linalg.norm(JAC_REAL - __JAC_OUT_REAL) < 1e-12
132-
assert np.linalg.norm(GRAD_REAL - __GRAD_REAL) < 1e-12
133+
assert np.linalg.norm(JAC_REAL - JAC_OUT_REAL) < 1e-12
134+
assert np.linalg.norm(GRAD_REAL - GRAD_OUT_REAL) < 1e-12
133135

134-
__imag2real = np.zeros_like(GRAD_REAL)
135-
__imag2real[: __imag2real.shape[-1] // 2] = GRAD_IMAG.real
136-
__imag2real[__imag2real.shape[-1] // 2 :] = GRAD_IMAG.imag
136+
imag2real = np.zeros_like(GRAD_REAL)
137+
imag2real[: imag2real.shape[-1] // 2] = GRAD_IMAG.real
138+
imag2real[imag2real.shape[-1] // 2 :] = GRAD_IMAG.imag
137139

138-
__rec_grad = np.zeros_like(GRAD_IMAG)
139-
__rec_grad.real = GRAD_REAL[: GRAD_REAL.shape[-1] // 2]
140-
__rec_grad.imag = GRAD_REAL[GRAD_REAL.shape[-1] // 2 :]
140+
rec_grad = np.zeros_like(GRAD_IMAG)
141+
rec_grad.real = GRAD_REAL[: GRAD_REAL.shape[-1] // 2]
142+
rec_grad.imag = GRAD_REAL[GRAD_REAL.shape[-1] // 2 :]
141143

142144
# funny numerical errors leads to
143145
# np.array_equal(GRAD_IMAG, __rec_grad) to fail
144-
assert np.linalg.norm(GRAD_IMAG - __rec_grad) < 1e-9
146+
assert np.linalg.norm(GRAD_IMAG - rec_grad) < 1e-9
145147

146-
__rec_grad = np.zeros_like(GRAD_IMAG)
147-
__rec_grad.real = __GRAD_REAL[: __GRAD_REAL.shape[-1] // 2]
148-
__rec_grad.imag = __GRAD_REAL[__GRAD_REAL.shape[-1] // 2 :]
148+
rec_grad = np.zeros_like(GRAD_IMAG)
149+
rec_grad.real = GRAD_OUT_REAL[: GRAD_OUT_REAL.shape[-1] // 2]
150+
rec_grad.imag = GRAD_OUT_REAL[GRAD_OUT_REAL.shape[-1] // 2 :]
149151

150152
# funny numerical errors leads to
151153
# np.array_equal(GRAD_IMAG, __rec_grad) to fail
152-
assert np.linalg.norm(GRAD_IMAG - __rec_grad) < 1e-9
154+
assert np.linalg.norm(GRAD_IMAG - rec_grad) < 1e-9
153155

154156

155157
def test_varprodmd_any():
@@ -158,45 +160,44 @@ def test_varprodmd_any():
158160
"""
159161
time = np.linspace(0, 4 * np.pi, 100)
160162
x_loc = np.linspace(-10, 10, 1024)
161-
__x, __time = np.meshgrid(x_loc, time)
162163

163-
z = signal(__x, __time).T
164+
z = signal(*np.meshgrid(x_loc, time)).T
164165

165-
__idx = select_best_samples_fast(z, 0.6)
166+
idx = select_best_samples_fast(z, 0.6)
166167

167-
__z_sub = z[:, __idx]
168-
__t_sub = time[__idx]
168+
z_sub = z[:, idx]
169+
t_sub = time[idx]
169170

170171
with pytest.raises(ValueError):
171172
compute_varprodmd_any(
172-
__z_sub[:, 0],
173-
__t_sub,
173+
z_sub[:, 0],
174+
t_sub,
174175
OPT_DEF_ARGS,
175176
rank=0.0,
176177
)
177178

178179
with pytest.raises(ValueError):
179180
compute_varprodmd_any(
180-
__z_sub, __t_sub.reshape((-1, 1)), OPT_DEF_ARGS, rank=0.0
181+
z_sub, t_sub.reshape((-1, 1)), OPT_DEF_ARGS, rank=0.0
181182
)
182183

183184
phi, lambdas, eigenf, _, _ = compute_varprodmd_any(
184-
__z_sub, __t_sub, OPT_DEF_ARGS, rank=0.0
185+
z_sub, t_sub, OPT_DEF_ARGS, rank=0.0
185186
)
186-
__pred = optdmd_predict(phi, lambdas, eigenf, time)
187-
__diff = np.abs(__pred - z)
188-
__mae_0 = np.sum(np.sum(__diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
187+
pred = varprodmd_predict(phi, lambdas, eigenf, time)
188+
diff = np.abs(pred - z)
189+
mae = np.sum(np.sum(diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
189190

190-
assert __mae_0 < 1.0
191+
assert mae < 1.0
191192

192193
phi, lambdas, eigenf, _, _ = compute_varprodmd_any(
193-
__z_sub, __t_sub, OPT_DEF_ARGS, rank=0.0, use_proj=False
194+
z_sub, t_sub, OPT_DEF_ARGS, rank=0.0, use_proj=False
194195
)
195-
__pred = optdmd_predict(phi, lambdas, eigenf, time)
196-
__diff = np.abs(__pred - z)
197-
__mae_0 = np.sum(np.sum(__diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
196+
pred = varprodmd_predict(phi, lambdas, eigenf, time)
197+
diff = np.abs(pred - z)
198+
mae = np.sum(np.sum(diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
198199

199-
assert __mae_0 < 1.0
200+
assert mae < 1.0
200201

201202

202203
def test_varprodmd_class():
@@ -205,22 +206,21 @@ def test_varprodmd_class():
205206
"""
206207
time = np.linspace(0, 4 * np.pi, 100)
207208
x_loc = np.linspace(-10, 10, 1024)
208-
__x, __time = np.meshgrid(x_loc, time)
209209

210-
z = signal(__x, __time).T
210+
z = signal(*np.meshgrid(x_loc, time)).T
211211
dmd = VarProDMD(0, False, False, 0)
212212

213213
with pytest.raises(ValueError):
214-
__ = dmd.forecast(time)
214+
_ = dmd.forecast(time)
215215

216216
with pytest.raises(ValueError):
217-
__ = dmd.ssr
217+
_ = dmd.ssr
218218

219219
with pytest.raises(ValueError):
220-
__ = dmd.selected_samples
220+
_ = dmd.selected_samples
221221

222222
with pytest.raises(ValueError):
223-
__ = dmd.opt_stats
223+
_ = dmd.opt_stats
224224

225225
dmd.fit(z, time)
226226
assert dmd.fitted
@@ -232,12 +232,12 @@ def test_varprodmd_class():
232232
assert dmd.growth_rate.size == dmd.amplitudes.size
233233
assert dmd.eigs.size == dmd.amplitudes.size
234234

235-
__pred = dmd.forecast(time)
235+
pred = dmd.forecast(time)
236236

237-
__diff = np.abs(__pred - z)
238-
__mae = np.sum(np.sum(__diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
237+
diff = np.abs(pred - z)
238+
mae = np.sum(np.sum(diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
239239

240-
assert __mae < 1
240+
assert mae < 1
241241
assert dmd.ssr < 1e-3
242242

243243
dmd = VarProDMD(0, False, "unkown_sort", 0.8)
@@ -250,10 +250,8 @@ def test_varprodmd_class():
250250
for arg in sort_args:
251251
dmd = VarProDMD(0, False, arg, 0.6)
252252
dmd.fit(z, time)
253-
__pred = dmd.forecast(time)
254-
__diff = np.abs(__pred - z)
255-
__mae = (
256-
np.sum(np.sum(__diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
257-
)
253+
pred = dmd.forecast(time)
254+
diff = np.abs(pred - z)
255+
mae = np.sum(np.sum(diff, axis=0), axis=-1) / z.shape[0] / z.shape[-1]
258256
assert dmd.selected_samples.size == int((1 - 0.6) * 100)
259-
assert __mae < 1.0
257+
assert mae < 1.0

0 commit comments

Comments
 (0)