Skip to content

Commit

Permalink
Merge pull request PyDMD#25 from mathLab/plot_warning
Browse files Browse the repository at this point in the history
improved coverage
  • Loading branch information
mtezzele committed Oct 14, 2017
2 parents 50a38bf + c82a28d commit 4e87e15
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
65 changes: 61 additions & 4 deletions tests/test_dmd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import TestCase
from pydmd.dmd import DMD

import matplotlib.pyplot as plt
import numpy as np
import os

# 15 snapshot with 400 data. The matrix is 400x15 and it contains
# the following data: f1 + f2 where
Expand Down Expand Up @@ -41,6 +42,25 @@ def test_truncation_shape(self):
dmd.fit(X=sample_data)
assert dmd.modes.shape[1] == 3

def test_Atilde_shape(self):
dmd = DMD(svd_rank=3)
dmd.fit(X=sample_data)
assert dmd.atilde.shape == (dmd.svd_rank, dmd.svd_rank)

def test_Atilde_values(self):
dmd = DMD(svd_rank=2)
dmd.fit(X=sample_data)
exact_atilde = np.array([[-0.70558526 + 0.67815084j, 0.22914898 + 0.20020143j],
[0.10459069 + 0.09137814j, -0.57730040 + 0.79022994j]])
np.testing.assert_allclose(exact_atilde, dmd.atilde)

def test_fit_given_Y(self):
dmd = DMD(svd_rank=2)
X = np.copy(sample_data[:, :-1])
Y = np.copy(sample_data[:, 1:])
dmd.fit(X=X, Y=Y)
np.testing.assert_allclose(Y, dmd._Y)

def test_eigs_1(self):
dmd = DMD(svd_rank=-1)
dmd.fit(X=sample_data)
Expand All @@ -60,12 +80,12 @@ def test_reconstructed_data(self):
dmd = DMD()
dmd.fit(X=sample_data)
dmd_data = dmd.reconstructed_data
assert np.allclose(dmd_data, sample_data)
np.testing.assert_allclose(dmd_data, sample_data)

def test_original_timesteps(self):
dmd = DMD()
dmd.fit(X=sample_data)
assert np.array_equal(
np.testing.assert_allclose(
dmd.original_timesteps, np.arange(sample_data.shape[1])
)

Expand All @@ -75,53 +95,90 @@ def test_dmd_time(self):
dmd.dmd_time['t0'] = 10
dmd.dmd_time['tend'] = 14
expected_data = sample_data[:, -5:]
assert np.allclose(dmd.reconstructed_data, expected_data)
np.testing.assert_allclose(dmd.reconstructed_data, expected_data)

def test_plot_eigs_1(self):
dmd = DMD()
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=True, show_unit_circle=True)
plt.close()

def test_plot_eigs_2(self):
dmd = DMD()
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=False, show_unit_circle=False)
plt.close()

def test_plot_modes_1(self):
dmd = DMD()
dmd.fit(X=sample_data)
with self.assertRaises(ValueError):
dmd.plot_modes_2D()
plt.close()

def test_plot_modes_2(self):
dmd = DMD(svd_rank=-1)
dmd.fit(X=sample_data)
dmd.plot_modes_2D((1, 2, 5), x=np.arange(20), y=np.arange(20))
plt.close()

def test_plot_modes_3(self):
dmd = DMD()
snapshots = [snap.reshape(20, 20) for snap in sample_data.T]
dmd.fit(X=snapshots)
dmd.plot_modes_2D()
plt.close()

def test_plot_modes_4(self):
dmd = DMD()
snapshots = [snap.reshape(20, 20) for snap in sample_data.T]
dmd.fit(X=snapshots)
dmd.plot_modes_2D(index_mode=1)
plt.close()

def test_plot_modes_5(self):
dmd = DMD()
snapshots = [snap.reshape(20, 20) for snap in sample_data.T]
dmd.fit(X=snapshots)
dmd.plot_modes_2D(index_mode=1, filename='tmp.png')
self.addCleanup(os.remove, 'tmp.1.png')

def test_plot_snapshots_1(self):
dmd = DMD()
dmd.fit(X=sample_data)
with self.assertRaises(ValueError):
dmd.plot_snapshots_2D()
plt.close()

def test_plot_snapshots_2(self):
dmd = DMD(svd_rank=-1)
dmd.fit(X=sample_data)
dmd.plot_snapshots_2D((1, 2, 5), x=np.arange(20), y=np.arange(20))
plt.close()

def test_plot_snapshots_3(self):
dmd = DMD()
snapshots = [snap.reshape(20, 20) for snap in sample_data.T]
dmd.fit(X=snapshots)
dmd.plot_snapshots_2D()
plt.close()

def test_plot_snapshots_4(self):
dmd = DMD()
snapshots = [snap.reshape(20, 20) for snap in sample_data.T]
dmd.fit(X=snapshots)
dmd.plot_snapshots_2D(index_snap=2)
plt.close()

def test_plot_snapshots_5(self):
dmd = DMD()
snapshots = [snap.reshape(20, 20) for snap in sample_data.T]
dmd.fit(X=snapshots)
dmd.plot_snapshots_2D(index_snap=2, filename='tmp.png')
self.addCleanup(os.remove, 'tmp.2.png')

def test_tdmd_plot(self):
dmd = DMD(tlsq_rank=3)
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=False, show_unit_circle=False)
plt.close()
5 changes: 4 additions & 1 deletion tests/test_dmdbase.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase
from pydmd.dmdbase import DMDBase

import matplotlib.pyplot as plt
import numpy as np

# 15 snapshot with 400 data. The matrix is 400x15 and it contains
Expand Down Expand Up @@ -44,13 +44,16 @@ def test_plot_eigs(self):
dmd = DMDBase()
with self.assertRaises(ValueError):
dmd.plot_eigs(show_axes=True, show_unit_circle=True)
plt.close()

def test_plot_modes_2D(self):
dmd = DMDBase()
with self.assertRaises(ValueError):
dmd.plot_modes_2D()
plt.close()

def test_plot_snaps_2D(self):
dmd = DMDBase()
with self.assertRaises(ValueError):
dmd.plot_snapshots_2D()
plt.close()
3 changes: 3 additions & 0 deletions tests/test_fbdmd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from pydmd.fbdmd import FbDMD
import matplotlib.pyplot as plt
import numpy as np


Expand Down Expand Up @@ -74,8 +75,10 @@ def test_plot_eigs_1(self):
dmd = FbDMD(svd_rank=-1)
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=True, show_unit_circle=True)
plt.close()

def test_plot_eigs_2(self):
dmd = FbDMD(svd_rank=-1)
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=False, show_unit_circle=False)
plt.close()
7 changes: 6 additions & 1 deletion tests/test_mrdmd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase
from pydmd.mrdmd import MrDMD

import matplotlib.pyplot as plt
import numpy as np


Expand Down Expand Up @@ -171,23 +171,28 @@ def test_wrong_plot_eig1(self):
dmd.plot_eigs(
show_axes=True, show_unit_circle=True, figsize=(8, 8), level=7
)
plt.close()

def test_wrong_plot_eig2(self):
dmd = MrDMD(svd_rank=1, max_level=7, max_cycles=1)
with self.assertRaises(ValueError):
dmd.plot_eigs()
plt.close()

def test_plot_eig1(self):
dmd = MrDMD(svd_rank=-1, max_level=7, max_cycles=1)
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=True, show_unit_circle=True, figsize=(8, 8))
plt.close()

def test_plot_eig2(self):
dmd = MrDMD(svd_rank=-1, max_level=7, max_cycles=1)
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=True, show_unit_circle=False, title='Title')
plt.close()

def test_plot_eig3(self):
dmd = MrDMD(svd_rank=-1, max_level=7, max_cycles=1)
dmd.fit(X=sample_data)
dmd.plot_eigs(show_axes=False, show_unit_circle=False, level=1, node=0)
plt.close()

0 comments on commit 4e87e15

Please sign in to comment.