Skip to content

Commit

Permalink
Add method for DMD modes selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ndem0 committed Apr 20, 2021
1 parent a0005bd commit 3193929
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pydmd/dmdbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,41 @@ def _compute_amplitudes(self):

return a


def select_modes(self, func):
"""
Select the DMD modes by using the given `func`.
`func` has to be a callable function which takes as input the DMD
object itself and return a numpy.ndarray of boolean where `False`
indicates that the corresponding mode will be discarded.
:param callable func: the function to select the modes
Example:
>>> def stable_modes(dmd_object):
>>> toll = 1e-3
>>> return np.abs(np.abs(dmd.eigs) - 1) < toll
>>> dmd = DMD(svd_rank=10)
>>> dmd.fit(sample_data)
>>> dmd.select_modes(stable_modes)
"""
selected_indeces = func(self)

self.operator._eigenvalues = self.operator._eigenvalues[selected_indeces]
self.operator._Lambda = self.operator._Lambda[selected_indeces]

self.operator._eigenvectors = self.operator._eigenvectors[:, selected_indeces]
self.operator._modes = self.operator._modes[:, selected_indeces]

self.operator._Atilde = np.linalg.multi_dot([
self.operator._eigenvectors,
np.diag(self.operator._eigenvalues),
np.linalg.pinv(self.operator._eigenvectors)])

self._b = self._compute_amplitudes()


def plot_eigs(self,
show_axes=True,
show_unit_circle=True,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dmdbase.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from pydmd.dmdbase import DMDBase
from pydmd import DMD
import matplotlib.pyplot as plt
import numpy as np

Expand Down Expand Up @@ -103,3 +104,13 @@ def test_sorted_eigs_default(self):
def test_sorted_eigs_param(self):
dmd = DMDBase(sorted_eigs='real')
assert dmd.operator._sorted_eigs == 'real'

def test_select_modes(self):
def stable_modes(dmd_object):
toll = 1e-3
return np.abs(np.abs(dmd_object.eigs) - 1) < toll
dmd = DMD(svd_rank=10)
dmd.fit(sample_data)
exp = dmd.reconstructed_data
dmd.select_modes(stable_modes)
np.testing.assert_array_almost_equal(exp, dmd.reconstructed_data)

0 comments on commit 3193929

Please sign in to comment.