Exemplo n.º 1
0
def financeDataTest():
    pass
    df = pd.read_sql('SELECT * FROM AssetsRets',
                     conn).set_index('Dates', drop=True).iloc[-500:]
    df = df[['Nasdaq', 'DAX', 'USDEUR']]
    X = df.values
    #X = sl.cs(df[['Nasdaq', 'DAX', 'USDEUR']]).values
    print(X)

    dmd = DMD(svd_rank=2)
    dmd.fit(X.T)

    for eig in dmd.eigs:
        print('Eigenvalue {}: distance from unit circle {}'.format(
            eig, np.abs(eig.imag**2 + eig.real**2 - 1)))

    #dmd.plot_eigs(show_axes=True, show_unit_circle=True)

    modeList = []
    for mode in dmd.modes.T:
        modeList.append(mode)
        #plt.plot(x, mode.real)
        #plt.title('Modes')
    pd.DataFrame(modeList).plot()
    plt.show()

    for dynamic in dmd.dynamics:
        print(dynamic)
Exemplo n.º 2
0
    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)
def _compute_dmd(frames, svd_rank):
    if len(frames.shape) == 4:  #if from color image
        vec_frames = np.reshape(frames, (frames.shape[0], frames.shape[1] *
                                         frames.shape[2] * frames.shape[3]))
    else:  #if from greyscale image
        vec_frames = np.reshape(
            frames, (frames.shape[0], frames.shape[1] * frames.shape[2]))
    dmd = DMD(svd_rank=svd_rank)
    #print("input is nan: " + str(np.isnan(vec_frames).any()))
    #print("input is inf: " + str(np.isinf(vec_frames).any()))
    vec_frames /= 255.0
    dmd.fit(np.nan_to_num(vec_frames.T, posinf=255, neginf=0))
    modes = dmd.modes.real
    return modes
Exemplo n.º 4
0
def test():
    def f1(x, t):
        return 1. / np.cosh(x + 3) * np.exp(2.3j * t)

    def f2(x, t):
        return 2. / np.cosh(x) * np.tanh(x) * np.exp(2.8j * t)

    x = np.linspace(-5, 5, 128)
    t = np.linspace(0, 4 * np.pi, 256)

    xgrid, tgrid = np.meshgrid(x, t)

    X1 = f1(xgrid, tgrid)
    X2 = f2(xgrid, tgrid)
    X = X1 + X2
    titles = ['$f_1(x,t)$', '$f_2(x,t)$', '$f$']
    data = [X1, X2, X]

    fig = plt.figure(figsize=(17, 6))
    for n, title, d in zip(range(131, 134), titles, data):
        plt.subplot(n)
        plt.pcolor(xgrid, tgrid, d.real)
        plt.title(title)
    plt.colorbar()
    plt.show()

    dmd = DMD(svd_rank=2)
    dmd.fit(X.T)

    for eig in dmd.eigs:
        print('Eigenvalue {}: distance from unit circle {}'.format(
            eig, np.abs(eig.imag**2 + eig.real**2 - 1)))

    #dmd.plot_eigs(show_axes=True, show_unit_circle=True)

    for mode in dmd.modes.T:
        plt.plot(x, mode.real)
        plt.title('Modes')
    plt.show()

    for dynamic in dmd.dynamics:
        plt.plot(t, dynamic.real)
        plt.title('Dynamics')
    plt.show()
Exemplo n.º 5
0
def main():
    # when on cylon to test on full vid fdir = '../data/vid.tif'
    # when local test use 'short_vid.tif'
    vid = ca_data_utils.load_vid()
    X1 = flatten[:, :-1]
    X2 = flatten[:, 1:]

    print('start dmd')
    dmd = DMD(svd_rank=10)
    dmd.fit(flatten.T)
    print('finished fitting data')

    for eig in dmd.eigs:
        print('Eigenvalue {}: distance from unit circle {}'.format(
            eig, np.abs(eig.imag**2 + eig.real**2 - 1)))
    dmd.plot_eigs(show_axes=True, show_unit_circle=True)

    self_re = np.load('reconstructed.npy')
    diff = dmd.reconstructed_data.T - self_re

    print(len(np.where(np.abs(diff) > 1e-3)[0]))
    print(diff.max())
    print(diff.min())
Exemplo n.º 6
0
def predict(mu, t, params, dataset, dyas=True):
    if dyas:
        params = params[:, [1, 2, 3, 6, 7, 8]]
        mu = mu[[1, 2, 3, 6, 7, 8]]

    dmd = DMD(svd_rank=10)
    dmd.fit(dataset)
    dmd.original_time['t0'] = 12000
    dmd.original_time['tend'] = 20000
    dmd.original_time['dt'] = 1
    dmd.dmd_time['tend'] = t

    future_state = dmd.reconstructed_data.real[:, -1]  # output at t = t s

    kernel = GPy.kern.RBF(input_dim=params.shape[1],
                          variance=1.,
                          lengthscale=1.)
    model = GPy.models.GPRegression(params,
                                    future_state.reshape(-1, 1),
                                    kernel,
                                    normalizer=True)
    model.optimize_restarts(num_restarts=10, verbose=False)

    return model.predict(np.atleast_2d(mu))[0][0][0]
Exemplo n.º 7
0
pts = np.genfromtxt('data/velocity0.20.csv', delimiter=',', skip_header=1)[:, -3:-1]


plt.figure(figsize=(16, 16))
for i, snapshot in enumerate(snapshots[::5], start=1):
    plt.subplot(2, 2, i)
    plt.scatter(pts[:, 0], pts[:, 1], c=snapshot, marker='.')
plt.show()

fbdmd = FbDMD(exact=True)
fbdmd.fit(snapshots)
fbdmd.reconstructed_data.shape


dmd = DMD(exact=True)
dmd.fit(snapshots)

print('[DMD  ] Total distance between eigenvalues and unit circle: {}'.format(
    np.sum(np.abs(dmd.eigs.real**2 + dmd.eigs.imag**2 - 1))
))
print('[FbDMD] Total distance between eigenvalues and unit circle: {}'.format(
    np.sum(np.abs(fbdmd.eigs.real**2 + fbdmd.eigs.imag**2 - 1))
))

dmd.plot_eigs()
fbdmd.plot_eigs()


fbdmd.dmd_time['dt'] *= .5
fbdmd.dmd_time['tend'] += 10
            marker=".",
            linestyle="-",
            label="mode {:.0f}".format(i))

plt.legend()
plt.title("coefficients of the first modes")
ax.set_xlabel("time")
ax.set_ylabel("right singular value")

plt.xlim(0, 1)
plt.ylim(X[:, :r].min(), X[:, :r].max())
plt.savefig(path + "DMD8Modes.png", dpi=250)
plt.close()

dmd = DMD(svd_rank=r)
dmd.fit(X.T)  # expected shape = (65, 129) = (spacial, time)

for eig in dmd.eigs:
    print('Eigenvalue {}: distance from unit circle {}'.format(
        eig, np.abs(np.sqrt(eig.imag**2 + eig.real**2) - 1)))

fig, ax = plt.subplots()
dmd.plot_eigs(show_axes=True, show_unit_circle=True)
plt.savefig(path + "DMDeigs.png", dpi=250)
plt.close()

fig, ax = plt.subplots()
for i, mode in enumerate(dmd.modes.T):
    # print(i)
    if i < 10:
        plt.plot(x, mode.real)
X2 = f2(xgrid, tgrid)
X = X1 + X2

titles = ['$f_1(x,t)$', '$f_2(x,t)$', '$f$']
data = [X1, X2, X]

fig = plt.figure(figsize=(17, 6))
for n, title, d in zip(range(131, 134), titles, data):
    plt.subplot(n)
    plt.pcolor(xgrid, tgrid, d.real)
    plt.title(title)
plt.colorbar()
plt.show()

dmd = DMD(svd_rank=2)
dmd.fit(X.T)

for eig in dmd.eigs:
    print('Eigenvalue {}: distance from unit circle {}'.format(
        eig, np.abs(np.sqrt(eig.imag**2 + eig.real**2) - 1)))

dmd.plot_eigs(show_axes=True, show_unit_circle=True)

for mode in dmd.modes.T:
    plt.plot(x, mode.real)
    plt.title('Modes')
plt.show()

for dynamic in dmd.dynamics:
    plt.plot(t, dynamic.real)
    plt.title('Dynamics')
Exemplo n.º 10
0
    diff_img = (255 * diff.astype('float32') / diff_factor).astype('uint8')
    cv2.imshow("diff", diff_img.astype('uint8'))
    cv2.imshow("background", bg_filt.astype('uint8'))

    # now run dmd on the diff image (already compensated for camera
    # motion)

    gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    small = cv2.resize(gray, (dmd_size, dmd_size),
                       interpolation=cv2.INTER_AREA)
    if not small.any():
        continue
    X.append(np.flipud(small))
    while len(X) > window_size:
        del X[0]
    dmd.fit(np.array(X))
    if len(dmd.eigs):
        #print(dmd.eigs)
        idx = np.argsort(np.abs(dmd.eigs - 1))
        #idx = np.argsort(np.abs(dmd.eigs.imag))
        print(idx)
        print(dmd.eigs)
        print(dmd.eigs[idx[0]])
        print(dmd.reconstructed_data.shape)

        big = 255 * dmd.reconstructed_data[:, -1] / np.max(
            dmd.reconstructed_data[:, -1])  # avoid overflow
        big = cv2.resize(np.flipud(
            big.reshape((dmd_size, dmd_size)).astype('uint8')),
                         (frame_undist.shape[1], frame_undist.shape[0]),
                         interpolation=cv2.INTER_AREA)
Exemplo n.º 11
0
 def test_integral_contribution_reconstruction(self):
     dmd = DMD(svd_rank=10)
     dmd.fit(sample_data)
     exp = dmd.reconstructed_data
     dmd.select_modes(DMDBase.ModesSelectors.integral_contribution(2))
     np.testing.assert_array_almost_equal(exp, dmd.reconstructed_data)
Exemplo n.º 12
0
# reshape x_train, x_test to fit input in LSTM layers
X_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
X_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))

%%time
model.fit(x = X_train, y = y_train,
                    epochs=5,
                    steps_per_epoch=10)

predictions = model.predict(X_test)
predictions = predictions.reshape(test_size,2)
koopman_pred = predictions.T

dmd = DMD(svd_rank=2)
dmd.fit(koopman_pred)
dmd_eigs = dmd.eigs
dmd_modes = dmd.modes
dmd_dynamics = dmd.dynamics

x = dmd_dynamics[0]
y = dmd_dynamics[1]

fig = plt.figure()
ax = fig.gca(projection='3d')

# PLOTTING
print(ax.plot_trisurf(range(3000), x, y))
print(plt.contour(dmd_modes))
print(plt.contour(dmd_dynamics))
import numpy as np
from dmd_utils import getVideoFrames, shape_frames
from pydmd import DMD, MrDMD
shape = (540,960,3)
frames = getVideoFrames('data/test_video.webm', (75,200))
dmd = DMD(svd_rank=6)
dmd.fit(frames.T)
mrdmd = MrDMD(svd_rank=6, max_level=4, max_cycles=1)
mrdmd.fit(frames.T.astype(np.float64))

dmd_frames = dmd.reconstructed_data.real.T.astype(np.uint8)
shape_frames(dmd_frames,"data/dmd_test.avi",shape)

for i in range(4):
    mrdmd_frames = mrdmd.partial_reconstructed_data(i).real.T.astype(np.uint8)
    shape_frames(mrdmd_frames,"data/mrdmd_level%d_test.avi"%i, shape)
mrdmd_frames = mrdmd.reconstructed_data.real.T.astype(np.uint8)
shape_frames(mrdmd_frames,"data/mrdmd_all_test.avi",shape)

Exemplo n.º 14
0
            qValues0 = np.random.rand(2, nActions, nSim)

            for cIter in range(nIter):
                qValues0 = qUpdate(qValues0, payoffs)

                if cIter >= 0:

                    allActions += [
                        stringActions(getActionProbs(qValues0, nSim))
                    ]

                    if cIter == 30:
                        dmd = DMD(svd_rank=-1)

                        dmd.fit(np.array(allActions).T)
                        dmd.plot_eigs()
                        plt.show()

                    if cIter == 100:
                        dmd = DMD(svd_rank=-1)

                        dmd.fit(np.array(allActions).T)
                        dmd.plot_eigs()
                        plt.show()

                    if cIter == 1000:
                        dmd = DMD(svd_rank=-1)

                        dmd.fit(np.array(allActions).T)
                        dmd.plot_eigs()
def getModes(sequence):
    dmd = DMD(svd_rank=6)
    dmd.fit(sequence.T)
    return dmd.modes


    # PLOT Principal Components of time dynamics
    num_pcs = 1

    plt.figure(146)
    for i, neuron in enumerate(neuron_numbers):
        plt.subplot(plot_rows, plot_cols, i+1)
        plt.title('RNN representation (Neuron {})'.format(neuron))
        plt.xlabel('Frame number')
        plt.ylabel('Neuron Activation')
        
#        [u, s, v] = np.linalg.svd(rnn_representation[:, :, neuron].T, full_matrices=False)
        dmd = DMD(svd_rank = num_pcs)
        dmd.fit(rnn_representation[:, :, neuron].T)


        for mode in dmd.modes.T:
#            plt.plot(u[i, :])
            plt.plot(mode.real)
             

        plt.legend(['Mode {}'.format(k+1) for k in range(num_pcs)])
        
    plt.show()


    plt.figure(1445)
    for i, neuron in enumerate(neuron_numbers):
        plt.subplot(plot_rows, plot_cols, i+1)
Exemplo n.º 17
0
    def run_dmd(self):
        def _plot_future_state():
            print("Shape before manipulation: {}".format(
                dmd.reconstructed_data.shape))
            dmd.dmd_time['tend'] *= 40
            print("Shape after manipulation: {}".format(
                dmd.reconstructed_data.shape))
            new_num_frames = dmd.reconstructed_data.shape[1]
            new_time = np.linspace(1, new_num_frames, new_num_frames)

            atom_axis, time_axis = np.meshgrid(new_time, atoms)
            plt.figure(figsize=(7, 8))
            plt.title("Projection with DMD")
            plt.pcolormesh(time_axis, atom_axis, dmd.reconstructed_data.real)
            plt.xlabel("Atom Index")
            plt.ylabel("Frame")
            plt.colorbar()
            plt.show()

        def _plot_data():
            atom_axis, time_axis = np.meshgrid(time, atoms)

            plt.figure(figsize=(7, 8))
            plt.subplot(2, 1, 1)
            plt.title("Original PDB data")
            plt.pcolormesh(time_axis, atom_axis, snapshot_matrix)
            plt.xlabel("Atom Index")
            plt.ylabel("Frame")
            plt.colorbar()
            plt.subplot(2, 1, 2)
            plt.title("Reconstructed with DMD")
            plt.pcolormesh(time_axis, atom_axis, dmd.reconstructed_data.real)
            plt.xlabel("Atom Index")
            plt.ylabel("Frame")
            plt.colorbar()
            plt.show()

        def _plot_modes():
            plt.figure(figsize=(8, 8))
            for mode in dmd.modes.T:
                plt.plot(atoms, mode)
                plt.title('Modes')
            plt.show()

        def _plot_dynamics():
            plt.figure(figsize=(8, 8))
            for dynamic in dmd.dynamics:
                plt.plot(time, dynamic)
                plt.title('Dynamics')
            plt.show()

        def _print_eigs():
            for eig in dmd.eigs:
                dist = np.abs(eig.imag**2 + eig.real**2 - 1)
                print("Eigenvalue:", eig, " Distance from unit circle:", dist)

        def _plot_eigs():
            dmd.plot_eigs(show_axes=True, show_unit_circle=True)

        def _print_error():
            # error = np.linalg.norm((snapshot_matrix - dmd.reconstructed_data))
            error = (np.square((snapshot_matrix -
                                dmd.reconstructed_data).real)).mean(axis=None)
            print("DMD error:", error)

        def _plot_error():
            plt.pcolormesh(
                time, atoms,
                np.divide((snapshot_matrix - dmd.reconstructed_data).real,
                          snapshot_matrix))
            plt.colorbar()
            plt.show()

        self.data = []  # TODO: DANGEROUS PLEASE REMOVE (TESTING ONLY)
        self._get_hilbert()  # updates self.data[]

        snapshot_matrix = np.array(self.data).transpose()

        dmd = DMD(svd_rank=.97, tlsq_rank=2, exact=True,
                  opt=True)  # create instance of DMD object
        dmd.fit(snapshot_matrix)  # populate the matrix with data

        num_atoms = len(self.data[0])
        num_frames = len(snapshot_matrix[0])

        atoms = np.linspace(1, num_atoms, num_atoms)
        time = np.linspace(1, num_frames, num_frames)

        # _plot_future_state()
        _plot_data()
Exemplo n.º 18
0
# -*- coding: utf-8 -*-
"""
Created on Thu Feb  7 16:25:16 2019

@author: Colton Smith
"""

import numpy as np
from pydmd import DMD

vals = np.array([[-2, 6, 1, 1, -1], [-1, 5, 1, 2, -1], [0, 4, 2, 1, -1],
                 [1, 3, 2, 2, -1], [2, 2, 3, 1, -1], [3, 1, 3, 2, -1]])

dmd = DMD(svd_rank=2)
vals_sub = vals[:5, :]
dmd.fit(vals_sub.T)
dmd.dmd_time['tend'] *= (1 + 1 / 6)
recon = dmd.reconstructed_data.real.T

print('Actual :', vals[5, :])
print('Predicted :', recon[5, :])
Exemplo n.º 19
0
# We instantiate the `cDMD` matrix, passing as `compression_matrix` argument the matrix we created. The constructor is very similar to the `DMD` class, except to the compression matrix and the missing of the `exact` argument (in the compressed version, there is only one way to compute the modes). We plot the modes and the dynamics.

cdmd = CDMD(svd_rank=3, compression_matrix=compression_matrix)
cdmd.fit(snapshots_matrix)

plt.figure(figsize=(16, 8))
plt.subplot(1, 2, 1)
plt.plot(cdmd.modes.real)
plt.subplot(1, 2, 2)
plt.plot(cdmd.dynamics.T.real)
plt.show()

# In order to investigate about the reconstruction accuracy, we compare the results obtained with the cDMD and the standard DMD, respectively.

dmd = DMD(svd_rank=3, exact=True)
dmd.fit(snapshots_matrix)

dmd_error = np.linalg.norm(snapshots_matrix - dmd.reconstructed_data)
cdmd_error = np.linalg.norm(snapshots_matrix - cdmd.reconstructed_data)
print("DMD error: {}".format(dmd_error))
print("CDMD error: {}".format(cdmd_error))

plt.figure(figsize=(16, 8))
plt.subplot(1, 3, 1)
plt.title('Original snapshots')
plt.pcolor(xgrid, tgrid, snapshots_matrix.real.T)
plt.subplot(1, 3, 2)
plt.title('Reconstructed with DMD')
plt.pcolor(xgrid, tgrid, dmd.reconstructed_data.real.T)
plt.subplot(1, 3, 3)
plt.title('Reconstructed with CDMD')
Exemplo n.º 20
0
    plt.ylabel('Time')
    plt.show()


# Let us start by creating the dataset and plot the data in order to have a first idea of the problem.

sample_data = create_sample_data()
x = np.linspace(-10, 10, 80)
t = np.linspace(0, 20, 1600)
make_plot(sample_data.T, x=x, y=t)


# First we apply the classical DMD without the svd rank truncation, and then we try to reconstruct the data. You can clearly see that all the transient time events are missing.

first_dmd = DMD(svd_rank=-1)
first_dmd.fit(X=sample_data)
make_plot(first_dmd.reconstructed_data.T, x=x, y=t)


# Now we do the same but using the mrDMD instead. The result is remarkable even with the svd rank truncation (experiment changing the input parameters).

sub_dmd = DMD(svd_rank=-1)
dmd = MrDMD(sub_dmd, max_level=7, max_cycles=1)
dmd.fit(X=sample_data)
make_plot(dmd.reconstructed_data.T, x=x, y=t)


# We now plot the eigenvalues in order to better understand the mrDMD. Without truncation we have 80 eigenvalues.


print('The number of eigenvalues is {}'.format(dmd.eigs.shape[0]))
import scipy
import scipy.integrate

from matplotlib import animation
from IPython.display import HTML
from matplotlib import pyplot as plt
from pydmd import DMD
from pydmd import FbDMD

A = np.loadtxt('dmd.txt', delimiter=",")
B = A.T
C = B[:, 0:5000]

print(C.shape)
dmd = DMD(svd_rank=1, tlsq_rank=2, exact=True, opt=True)
dmd.fit(C)
fbdmd = FbDMD(exact=True)
fbdmd.fit(C)
print(fbdmd.reconstructed_data.shape)
fbdmd.dmd_time['dt'] *= .5
fbdmd.dmd_time['tend'] += 10

plt.plot(fbdmd.dmd_timesteps, fbdmd.dynamics.T.real)

plt.show()

print("Shape before manipulation: {}".format(dmd.reconstructed_data.shape))
dmd.dmd_time['dt'] *= .25
dmd.dmd_time['tend'] *= 3
print("Shape after manipulation: {}".format(dmd.reconstructed_data.shape))