def plotphase(A,B,C,D,E): def derivs(u,t): y,z = u; return np.array([ z, -A*y**3 + B*y - C*z + D*np.cos(E*t) ]) N=60 u0 = np.array([0.0, 0.0]) t = np.arange(0,300,2*np.pi/N); u = RK2(derivs, u0, t, subdiv = 10) qww = qn.mle(u) return np.amax(qww)
def test_mle_embed(): # Test lyapunov.mle_embed() t = np.linspace(0, 10 * 2 * np.pi, 5000) y = np.array([np.sin(t), np.cos(t)]).T desired = lyapunov.mle(y, maxt=25) dim = [2] tau = 125 x = y[:, 0] assert_allclose(desired, lyapunov.mle_embed(x, dim=dim, tau=tau, maxt=25)[0], atol=1e-1)
def test_mle(): # Test lyapunov.mle() # Particle moving uniformly in 7d: y(t) = a + b*t a = np.random.random(7) b = np.random.random(7) n = 250 window = 15 t = np.arange(n) y = a + b * t[:, np.newaxis] for metric in ('chebyshev', 'cityblock', 'euclidean'): if metric == 'chebyshev': modb = np.max(np.abs(b)) elif metric == 'cityblock': modb = np.sum(np.abs(b)) elif metric == 'euclidean': modb = np.sqrt(np.sum(b**2)) desired = np.log((window + 1) * modb) assert_allclose(lyapunov.mle(y, window=window, metric=metric), desired)
for k in range(len(times)-1): t = times[k] h = (times[k+1]-times[k])/subdiv for j in range(subdiv): k1 = f(u,t)*h k2 = f(u+0.5*k1, t+0.5*h)*h u, t = u+k2, t+h uout[k+1]=u return uout def plotphase(A,B,C,D,E): def derivs(u,t): y,z = u; return np.array([ z, -A*y**3 + B*y - C*z + D*np.cos(E*t) ]) N=60 u0 = np.array([0.0, 0.0]) t = np.arange(0,300,2*np.pi/N); u = RK2(derivs, u0, t, subdiv = 10) plt.plot(u[:-2*N,0],u[:-2*N,1],'.--y', u[-2*N:,0],u[-2*N:,1], '.-b', lw=0.5, ms=2); plt.plot(u[::N,0],u[::N,1],'rs', ms=4); plt.grid(); plt.show() return u l = plotphase(1.0, 5.0, 0.02, 8.0, 0.5) import nolds qr = nolds.lyap_r(l[:,0]) qe = nolds.lyap_e(l[:,0]) import nolitsa.lyapunov as qn #qn = nolitsa.lyapunov qww = qn.mle(l) maximum= np.amax(qww)
def myFeaturesExtractor( X, myM, myV): # X has to be a matrix where each row is a channel N = len(X) # number of channels L = len(X[0]) maxtLyap = min(500, L // 2 + L // 4) lyapLags = np.arange(maxtLyap) / Fs # get number of features nFeatures = nMono * N + N * (N - 1) / 2 # here we initialize the list of features // We will transform it to an array later featList = np.zeros((int(nFeatures))) # deal with monovariate features first for kChan in range(N): kFeat = 0 mySig = X[kChan, :] #========== Stats ======================== myMean = myM[kChan] featList[nMono * kChan + kFeat] = myMean kFeat += 1 myMax = max(mySig) featList[nMono * kChan + kFeat] = myMax kFeat += 1 myMin = min(mySig) featList[nMono * kChan + kFeat] = myMin kFeat += 1 peak = max(abs(np.array([myMin, myMax]))) featList[nMono * kChan + kFeat] = peak kFeat += 1 myVar = myV[kChan] featList[nMono * kChan + kFeat] = myVar kFeat += 1 featList[nMono * kChan + kFeat] = sp.skew(mySig) kFeat += 1 featList[nMono * kChan + kFeat] = sp.kurtosis(mySig) kFeat += 1 myRMS = rms(mySig) featList[nMono * kChan + kFeat] = myRMS kFeat += 1 featList[nMono * kChan + kFeat] = peak / myRMS kFeat += 1 featList[nMono * kChan + kFeat] = totVar(mySig) kFeat += 1 featList[nMono * kChan + kFeat] = pyeeg.dfa(mySig) kFeat += 1 featList[nMono * kChan + kFeat] = pyeeg.hurst(mySig) kFeat += 1 hMob, hComp = pyeeg.hjorth(mySig) featList[nMono * kChan + kFeat] = hMob kFeat += 1 featList[nMono * kChan + kFeat] = hComp kFeat += 1 ## ======== fractal ======================== # Now we need to get the embeding time lag Tau and embeding dmension ac = delay.acorr(mySig, maxtau=maxTauLag, norm=True, detrend=True) Tau = firstTrue(ac < corrThresh) # embeding delay f1 , f2 , f3 = dimension.fnn(mySig, dim=dim, tau=Tau, R=10.0, A=2.0, metric='euclidean',\ window=10,maxnum=None, parallel=True) myEmDim = firstTrue(f3 < fracThresh) # Here we construct the Embeding Matrix Em Em = pyeeg.embed_seq(mySig, Tau, myEmDim) U, s, Vh = linalg.svd(Em) W = s / np.sum(s) # list of singular values in decreasing order FInfo = pyeeg.fisher_info(X, Tau, myEmDim, W=W) featList[nMono * kChan + kFeat] = FInfo kFeat += 1 featList[nMono * kChan + kFeat] = Tau kFeat += 1 featList[nMono * kChan + kFeat] = myEmDim kFeat += 1 #======================================== PFD = pyeeg.pfd(mySig, D=None) hfd6 = pyeeg.hfd(mySig, 6) hfd10 = pyeeg.hfd(mySig, 10) # Now we fit aline and get its slope to have Lyapunov exponent divAvg = lyapunov.mle(Em, maxt=maxtLyap, window=3 * Tau, metric='euclidean', maxnum=None) poly = np.polyfit(lyapLags, divAvg, 1, rcond=None, full=False, w=None, cov=False) LyapExp = poly[0] featList[nMono * kChan + kFeat] = PFD kFeat += 1 featList[nMono * kChan + kFeat] = hfd6 kFeat += 1 featList[nMono * kChan + kFeat] = hfd10 kFeat += 1 featList[nMono * kChan + kFeat] = LyapExp kFeat += 1 ## ======== Entropy ======================== tolerance = 1 / 4 entropyDim = max([myEmDim, PFD]) featList[nMono * kChan + kFeat] = pyeeg.samp_entropy( mySig, entropyDim, tolerance) kFeat += 1 featList[nMono * kChan + kFeat] = pyeeg.svd_entropy(mySig, Tau, myEmDim, W=W) kFeat += 1 # here we compute bin power power, power_Ratio = pyeeg.bin_power(mySig, freqBins, Fs) featList[nMono * kChan + kFeat] = pyeeg.spectral_entropy( mySig, freqBins, Fs, Power_Ratio=power_Ratio) kFeat += 1 ## ======== Spectral ======================== for kBin in range(len(freqBins) - 1): featList[nMono * kChan + kFeat] = power[kBin] kFeat += 1 featList[nMono * kChan + kFeat] = power_Ratio[kBin] kFeat += 1 # deal with multivariate features first #============ connectivity ================== corrList = connectome(X) nConnect = len(corrList) if N * (N - 1) / 2 != nConnect: raise ValueError('incorrect number of correlation coeffs') for kC in range(nConnect): featList[-nConnect + kC] = corrList[kC] return featList