def predicition(dmddataset, days): hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=len(dmddataset) / days).fit(dmddataset) hodmd.reconstructed_data.shape hodmd.plot_eigs() hodmd.dmd_time['tend'] = len(dmddataset) / days * ( days + 1) - 1 # since it starts from zero dmd_output = hodmd.reconstructed_data[0].real dmd_prediction = dmd_output[-len(dmddataset) / days:] return dmd_prediction, dmd_output
def _get_modes_from_word_vecs(self, v, concat_avg=True): list_of_modes = [] time_lags = [1, 2] for d in time_lags: dmd = HODMD(svd_rank=2, opt=True, exact=True, d=d) dmd.fit(v.T) fmode = dmd.modes.T list_of_modes.append(np.hstack(np.absolute(fmode))) if concat_avg: mean_vec = self._p_mean_vector([1.0], v) list_of_modes.append(mean_vec) return list_of_modes
def sentence_to_dmd_vec(to_keep, time_lags, concat_avg, vectors): v = numpy.asarray(vectors) list_of_modes = [] for d in time_lags: dmd = HODMD(svd_rank=to_keep, opt=True, exact=True, d=d) dmd.fit(v.T) fmode = dmd.modes.T list_of_modes.append(numpy.hstack(numpy.absolute(fmode))) if concat_avg: mean_vec = p_mean_vector([1.0], v) list_of_modes.append(mean_vec) return numpy.hstack(list_of_modes)
def dmddiffer(emasets, days_to_keep, days_to_use=25, pointsperday=288): #get the multiple dmd result dmds = [] for n in np.arange(days_to_use): dmddataset = [] dmdset = emasets[-days_to_use + n - days_to_keep:-days_to_use + n] for loop in np.arange(len(dmdset)): dmddataset.extend(dmdset[loop]) dmddataset = np.array(dmddataset) hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=288).fit(dmddataset) hodmd.reconstructed_data.shape hodmd.dmd_time['tend'] = (days_to_keep + 1) * pointsperday - 1 dmddataset = hodmd.reconstructed_data[0].real[-pointsperday:] dmds.append(dmddataset) return dmds
def emddmd(dataset, d, draw=0 ): #can we do the dmd again? to see whether there are some miracles emd = EMD() imfs = emd(dataset) # do the EMD nimfs = len(imfs) result = [] sumresult = [] #firstimf = firstone([imfs[0]]) #result.append(firstimf) for n in np.arange(nimfs): dmddataset = imfs[n] hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=d).fit(dmddataset) hodmd.reconstructed_data.shape hodmd.dmd_time['tend'] = len(imfs[n]) + 24 dmdresult = hodmd.reconstructed_data[0].real #uppercap = np.max(dmddataset)*1.1 #lowercap = np.min(dmddataset)*1.1 #if dmdresult[-2] > uppercap: # dmdresult[-2] = uppercap #elif dmdresult[-2] < lowercap: #dmdresult[-2] = lowercap if draw == 1: fig = plt.figure(figsize=(20, 10)) plt.plot(np.arange(len(imfs[n])), imfs[n], '-', label='the practical signal', color='g') plt.plot(np.arange(len(imfs[n]) + 25), dmdresult, '--', label='DMD output', color='r') plt.show() result.append(dmdresult[-2]) sumresult.append(np.sum(result)) return sumresult, result
(loop + 1) + points_per_day] daysdata.append(onedaydata) return daysdata[-days_to_keep:], daysdata #moving average definition is done days = 5 cut = iters madataset = realwindset[:cut] maresult = ma(madataset, days, pointsperday) dmddataset_org = maresult[0] dmddataset = [] for n in np.arange(len(dmddataset_org)): dmddataset.extend(dmddataset_org[n]) dmddataset = np.array(dmddataset) hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=pointsperday).fit(dmddataset) hodmd.reconstructed_data.shape hodmd.dmd_time['tend'] = ( days + 1) * pointsperday - 1 # since it starts from zero #----one pic about the with DMD one #fig = plt.figure(figsize=(20, 10)) #plt.plot(hodmd.original_timesteps+cut-days*pointsperday, dmddataset, '.', label='snapshots') data_practical = windsetoriginal[cut + hodmd.dmd_timesteps - days * pointsperday] data_prediction = hodmd.reconstructed_data[0].real #----some special correction on the decomposition----- zeroindex = np.where(dmddataset_org[-1] <= 0.01)[0] data_prediction[days * pointsperday + zeroindex] = dmddataset_org[-1][zeroindex]
dataGridHalf1 = uGridHalf1 dataGridHalf2 = uGridHalf2 # Joined data tVector = np.hstack((tVectorHalf1, tVectorHalf2)) xGrid, tGrid = np.meshgrid(xVector1, tVector) uGrid = np.vstack((uGridHalf1, uGridHalf2)) vGrid = np.vstack((vGridHalf1, vGridHalf2)) aGrid = np.vstack((aGridHalf1, aGridHalf2)) uexGrid = np.vstack((uexGridHalf1, uexGridHalf2)) dataGrid = np.vstack((dataGridHalf1, dataGridHalf2)) # Compute DMDs d = 5 print('Computing DMD 1...') hodmd1 = HODMD(svd_rank=rank, opt=True, d=d) hodmd1.fit(dataGridHalf1.T) print('Computing DMD 2...') hodmd2 = HODMD(svd_rank=rank, opt=True, d=d) hodmd2.fit(dataGridHalf2.T) print('Computing mixed DMD...') hodmd = HODMD(svd_rank=rank, opt=True, d=d) hodmd.fit(dataGrid.T) # Adjust times for partials hodmd1.original_time['dt'] = hodmd1.dmd_time[ 'dt'] = tVectorHalf1[1] - tVectorHalf1[0] hodmd1.original_time['t0'] = hodmd1.dmd_time['t0'] = tVectorHalf1[0] hodmd1.original_time['tend'] = hodmd1.dmd_time['tend'] = tVectorHalf1[-1]
# # Plot read data # fig = plt.figure(figsize=(17,6)) # plt.pcolor(xGrid, tGrid, uGrid) # plt.title('Displacement') # plt.colorbar() # plt.show() # # print('Done') print('Computing DMD...') # ---------------------------------------------------------------------------------------------------------------------- # Compute DMD on displacement d = 5 dmd = HODMD(svd_rank=rank, opt=True, d=d) # dmd = MrDMD(svd_rank=rank, max_level = 3, max_cycles = 1) dmd.fit(dataGrid.T) # Show eigenvalues 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) # Show modes fig = plt.figure(figsize=(8,3)) fig.subplots_adjust(top=0.8, bottom=0.2) plt.subplot(121) for mode in dmd.modes.T: plt.plot(xVector, mode.real)
import numpy as np import time from pydmd import HODMD x = int(input("predicted timestep-\n")) z = int(input("data cutoff-\n")) b = slice(0, z) A = np.loadtxt('dmd.txt', delimiter=",") B = A.T C = B[:, b] original = C print(C.shape) hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=30).fit(C) print(hodmd.reconstructed_data.shape) hodmd.plot_eigs() hodmd.original_time['dt'] = hodmd.dmd_time['dt'] hodmd.original_time['t0'] = hodmd.dmd_time['t0'] hodmd.original_time['tend'] = hodmd.dmd_time['tend'] plt.plot(hodmd.original_timesteps, C[0, :], '.', label='snapshots') plt.plot(hodmd.original_timesteps, original[0, :], '-', label='original function') plt.plot(hodmd.dmd_timesteps, hodmd.reconstructed_data[0].real, '--', label='DMD output')
composite = np.arange( int(composite[1]) - 1, int(composite[2]), 1) else: composite = np.array(map(int, composite)) newsignal = np.sum(imfs[composite], axis=0) plt.figure() plt.plot(x, newsignal) plt.show() else: print(colored("You have to do at least one EMD first.", 'red')) continue if name == 'dmd': # do a DMD after a composition among IMFs if 'newsignal' in locals(): d = raw_input('what is the d in DMD-d?') x = np.linspace(1, len(newsignal), len(newsignal)) hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=int(d)).fit(newsignal) hodmd.reconstructed_data.shape hodmd.plot_eigs() hodmd.original_time['dt'] = hodmd.dmd_time['dt'] = x[1] - x[0] hodmd.original_time['t0'] = hodmd.dmd_time['t0'] = x[0] hodmd.original_time['tend'] = hodmd.dmd_time['tend'] = x[-1] #draw plt.plot(hodmd.dmd_timesteps, hodmd.reconstructed_data[0].real, '--', label='DMD output') plt.legend() plt.show() farseer = raw_input('want to see a forecast?(y/n)') if farseer == 'y':
# # Plot read data # fig = plt.figure(figsize=(17,6)) # plt.pcolor(xGrid, tGrid, uGrid) # plt.title('Displacement') # plt.colorbar() # plt.show() # # print('Done') print('Computing DMD...') # ---------------------------------------------------------------------------------------------------------------------- # Compute DMD on displacement d = 5 dmd = HODMD(svd_rank=rank, opt=True, d=d) # dmd = MrDMD(svd_rank=rank, max_level=2, max_cycles=2) dmd.fit(dataGrid.T) # Show eigenvalues 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) # Show modes for mode in dmd.modes.T: plt.plot(xVector, mode.real) plt.title('Modes') plt.show()
fucresult = np.cos(x) * np.sin(np.cos(x)) + np.cos( x * .2) * np.sin(x) + x * (25 - x) / 100 return fucresult import plotcheck #just test #path = "/home/bwei/PycharmProjects/data lib/long wind" #windset = rd(path) #name = raw_input('the name of data set?') x = np.linspace(0, 30, 128) realwindset = fuc(x) realwindset.shape = (len(realwindset), ) #realwindset = realwindset + np.random.rand(128) #x = np.linspace(1, len(realwindset), len(realwindset)) hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=58).fit(realwindset) hodmd.reconstructed_data.shape hodmd.plot_eigs() hodmd.original_time['dt'] = hodmd.dmd_time['dt'] = x[1] - x[0] hodmd.original_time['t0'] = hodmd.dmd_time['t0'] = x[0] hodmd.original_time['tend'] = hodmd.dmd_time['tend'] = x[-1] plt.plot(hodmd.original_timesteps, realwindset, '.', label='realwindset') plt.plot(x, realwindset, '-', label='original function') plt.plot(hodmd.dmd_timesteps, hodmd.reconstructed_data[0].real, '--', label='DMD output') plt.legend() plt.show()
from plotcheck import pl import GPy from matplotlib import pyplot as plt from pydmd import HODMD aaa = np.array([1, 2, 3, 4, 5, 6, 8, 19, 30]) bbb = np.array([2, 10, 4, 5, 12, 7]) ccc = np.array([3, 4, 5, 6, 7, 8]) imfs = np.array([]) kkkk = np.array([aaa, bbb, ccc]) Percentile = np.percentile(aaa, [0, 25, 50, 75, 100]) IQR = Percentile[3] - Percentile[1] UpLimit = Percentile[3] + IQR * 1.5 DownLimit = Percentile[1] - IQR * 1.5 aaa[np.where(aaa > UpLimit)] = UpLimit avvv = [{}] * 3 path = "/home/bwei/PycharmProjects/data lib/PVhourly6months.csv" realwindset = readcsv.rd(path, datawashflag=1) pointsperday = 24 days = 2 hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=pointsperday).fit(realwindset[-48:]) hodmd.reconstructed_data.shape hodmd.plot_eigs() hodmd.dmd_time['tend'] = (days + 1) * pointsperday - 1 # since it starts from zero
import time from pydmd import HODMD def myfunc(x): return np.cos(x)*np.sin(np.cos(x)) + np.cos(x*.2) x = np.linspace(0, 10, 64) y = myfunc(x) snapshots = y plt.plot(x, snapshots, '.') plt.show() hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=30).fit(snapshots) hodmd.reconstructed_data.shape hodmd.plot_eigs() hodmd.original_time['dt'] = hodmd.dmd_time['dt'] = x[1] - x[0] hodmd.original_time['t0'] = hodmd.dmd_time['t0'] = x[0] hodmd.original_time['tend'] = hodmd.dmd_time['tend'] = x[-1] plt.plot(hodmd.original_timesteps, snapshots, '.', label='snapshots') plt.plot(hodmd.original_timesteps, y, '-', label='original function') plt.plot(hodmd.dmd_timesteps, hodmd.reconstructed_data[0].real, '--', label='DMD output') plt.legend() plt.show() hodmd.dmd_time['tend'] = 50
x = np.linspace(0, 10, 128) realwindset = fuc(x) # toy end #realwindset = windset[name] rounds = raw_input('from ?') rounds = int(rounds) roundt = raw_input('to?') roundt = int(roundt) drange = np.arange(rounds, roundt + 1, 1) realwindset.shape = (len(realwindset), ) x = np.linspace(1, len(realwindset), len(realwindset)) #new: for loop in drange: hodmd = HODMD(svd_rank=0, exact=True, opt=True, d=int(loop)).fit(realwindset) # key functional sentence #hodmd.reconstructed_data.shape errors = realwindset - hodmd.reconstructed_data[0] sumerror = np.sum(abs(errors)) errorvector.append(sumerror) y = np.arange(1, len(errorvector) + 1, 1) plt.plot(y, errorvector, '--', label='errors') #plt.plot(hodmd.dmd_timesteps, realwindset, '-', label='realwindset') #plt.plot(hodmd.dmd_timesteps, errors, '.', label='errors') plt.legend() plt.show()