# Last modified: Fri Aug 31, 2018 05:17pm # Copyright: Bedartha Goswami <*****@*****.**> import sys import numpy as np import matplotlib.pyplot as pl import datetime as dt import recurrence as rc if __name__ == "__main__": # generate white noise data and get RP Xnoise = np.random.randn(500) Tnoise = np.arange(1, 501) Rnoise = rc.rp(Xnoise, m=1, tau=1, e=0.1, metric="euclidean", threshold_by="frr") # load the Nino 3.4 data, estimate embedding parameters and get RP # load Nino 3.4 index data D = np.loadtxt("../data/enso/nino.txt", delimiter=",", skiprows=5) Y, M = D[:, 0], D[:, 1] Xnino = D[:, -1] # convert time info to datetime array Tnino = [] for y, m in zip(Y, M): Tnino.append(dt.datetime(int(y), int(m), 15)) Tnino = np.array(Tnino) mi, lags = rc.mi(Xnino, maxlag=100) i = rc.first_minimum(mi) tau = lags[i] fnn, dims = rc.fnn(Xnino, tau, maxdim=20, r=0.01)
def _get_data(): """ Estimates Lyapunov, DET, and SPL for Henon map. """ # Henon map time series print("Henon map time series ...") t = np.arange(0, 10000, 1) a = np.linspace(1.0, 1.4, na).reshape(na, 1) b = 0.30 nt = len(t) x, y = [np.zeros((nt, na, ns)) for i in range(2)] x[0, :, :] = 1E-1 * np.random.rand(na, ns) y[0, :, :] = 1E-1 * np.random.rand(na, ns) pb = _progressbar_start(max_value=nt, pbar_on=True) LPV = np.zeros((na, ns)) for i in range(1, nt): x[i, :, :] = 1. - a * x[i - 1, :, :]**2 + y[i - 1, :, :] y[i, :, :] = b * x[i - 1, :, :] if i >= nt / 2: LPV[:, :] += np.log(np.fabs(-2. * a * x[i - 1, :, :])) _progressbar_update(pb, i) _progressbar_finish(pb) xH_eq = x[-neq:, :, :] LPV /= float(nt) # estimate embedding parameters print("embedding parameters ...") tau, m = np.ones(na, dtype="int"), 2 * np.ones(na, dtype="int") # DET print("DET ...") RR = 0.30 DET = np.zeros((na, ns)) pb = _progressbar_start(max_value=ns * na, pbar_on=True) k = 0 for j in range(ns): for i in range(na): R = rc.rp(xH_eq[:, i, j], m=m[i], tau=tau[i], e=RR, norm="euclidean", threshold_by="frr") DET[i, j] = rqa.det(R, lmin=2, hist=None, verb=False) del R _progressbar_update(pb, k) k += 1 _progressbar_finish(pb) # SPL print("SPL ...") SPL = np.zeros((na, ns)) pb = _progressbar_start(max_value=ns * na, pbar_on=True) k = 0 for j in range(ns): for i in range(na): A = rc.rn(xH_eq[:, i, j], m=m[i], tau=tau[i], e=RR, norm="euclidean", threshold_by="frr") G = ig.Graph.Adjacency(A.tolist(), mode=ig.ADJ_UNDIRECTED) pl_hist = G.path_length_hist(directed=False) SPL[i, j] = pl_hist.mean del A, G _progressbar_update(pb, k) k += 1 _progressbar_finish(pb) # save output FN = DATPATH + "det_spl_lpv_na%d_ns%s_neq%d" % (na, ns, neq) np.savez(FN, DET=DET, SPL=SPL, LPV=LPV, t=t, a=a, b=b, x=x, y=y) print("saved to %s.npz" % FN) return None
# fig = pl.figure(figsize=[7.480315, 3.937008]) # 190 mm wide, 100 mm tall wd, ht = 0.375, 0.375 lm1, lm2 = 0.05, 0.55 bm1, bm2 = 0.585, 0.08 ax1 = fig.add_axes([lm1, bm1, wd, ht]) ax2 = fig.add_axes([lm2, bm1, wd, ht]) ax3 = fig.add_axes([lm1, bm2, wd, ht]) ax4 = fig.add_axes([lm2, bm2, wd, ht]) axlabfs, tiklabfs = 12, 11 clr1, clr2, clr3 = "MediumTurquoise", "GoldenRod", "IndianRed" # A. Gaussian white noise RP print("A: Gaussian white noise ...") N = 500 x = np.random.rand(N) R = rc.rp(x, m=1, tau=1, e=0.10, norm="euclidean", threshold_by="distance") ax1.imshow(R, cmap=pl.cm.gray_r, interpolation="none", origin="lower", rasterized=True) del R ax1.set_title(r"$\tau_e = 1, m_e = 1$, \varepsilon = 0.10", fontsize=axlabfs) # B. Superposed harmonics RP print("B. Superposed harmonics RP ...") N = 500 t = np.arange(N) T1, T2, T3 = 10., 50., 75. A1, A2, A3 = 1., 1.5, 2.
e_cpr = 0.20 e_rmd = 0.25 pb = _progressbar_start(max_value=(N * NS), pbar_on=True) k = 0 for j in range(NS): for i in range(N): # pl.clf() # pl.plot(X1[i], X2[i], "k.", alpha=0.5) # # X = rc.embed(X1[i], m=m[i], tau=tau[i]) # # pl.plot(X[:, 0],X[:, 1]) # pl.show() # sys.exit() R1 = rc.rp(X1[i, j, :], m=m[i, j], tau=tau[i, j], e=e_cpr, norm="euclidean", threshold_by="frr", normed=True) R2 = rc.rp(X2[i, j, :], m=m[i, j], tau=tau[i, j], e=e_cpr, norm="euclidean", threshold_by="frr", normed=True) CPR[i, j] = rqa.cpr(R1, R2) del R1, R2 R1 = rc.rp(X1[i, j, :], m=m[i, j], tau=tau[i, j],
def _get_rmd(): """Estimates the RMD between ENSO and PDO""" # load data utils._printmsg("load data ...", args.verbose) t, x_enso, x_pdo = _load_indices() x = { "enso": x_enso, "pdo": x_pdo, } names = ["enso", "pdo"] # recurrence plot parameters EPS = 0.30 thrby = "frr" # embedding parameters utils._printmsg("embedding parameters ...", args.verbose) n = len(t) m, tau = {}, {} R = {} maxlag = 150 maxdim = 20 r_fnn = 0.0010 for name in names: if args.verbose: print("\t for %s" % name.upper()) # get embedding parameters ## get mi mi, mi_lags = rc.mi(x[name], maxlag, pbar_on=False) # mi, mi_lags = rc.acf(x[name], maxlag) mi_filt, _ = utils.boxfilter(mi, filter_width=3, estimate="mean") try: tau[name] = rc.first_minimum(mi_filt) except ValueError: tau[name] = 1 ## FNN fnn, dims = rc.fnn(x[name], tau[name], maxdim=maxdim, r=r_fnn, pbar_on=False) m[name] = dims[rc.first_zero(fnn)] # take the maximum delay and the maximum embedding dimension tau = np.max([tau["enso"], tau["pdo"]]).astype("int") m = np.max([m["enso"], m["pdo"]]).astype("int") # get surrogates utils._printmsg("surrogates ...", args.verbose) ns = args.nsurr SURR = {} params = { "m": m, "tau": tau, "eps": EPS, "norm": "euclidean", "thr_by": thrby, "tol": 2. } for name in names: utils._printmsg("\t for %s" % name.upper(), args.verbose) # SURR[name] = rc.surrogates(x[name], ns, "iaaft", verbose=args.verbose) SURR[name] = rc.surrogates(x[name], ns, "twins", params, verbose=args.verbose) # get RMD for original data utils._printmsg("RMD for original data ...", args.verbose) ws, ss = args.window_size, args.step_size nw = int(np.floor(float(n - ws) / float(ss))) tm = np.empty(nw, dtype="object") for name in names: R[name] = rc.rp( x[name], m=m, tau=tau, e=EPS, norm="euclidean", threshold_by=thrby, ) rmd = np.zeros(nw) pb = _progressbar_start(max_value=nw, pbar_on=args.verbose) for i in range(nw): start = i * ss end = start + ws Rw_enso = R["enso"][start:end, start:end] Rw_pdo = R["pdo"][start:end, start:end] rmd[i] = rqa.rmd(Rw_enso, Rw_pdo) tm[i] = t[start] + (t[end] - t[start]) / 2 _progressbar_update(pb, i) _progressbar_finish(pb) # get RMD for surrogate data utils._printmsg("RMD for surrogates ...", args.verbose) Rs = {} rmdsurr = np.zeros((ns, nw), dtype="float") pb = _progressbar_start(max_value=ns, pbar_on=args.verbose) for k in range(ns): for name in names: xs = SURR[name][k] Rs[name] = rc.rp( xs, m=m, tau=tau, e=EPS, norm="euclidean", threshold_by=thrby, ) for i in range(nw): start = i * ss end = start + ws Rsw_enso = Rs["enso"][start:end, start:end] Rsw_pdo = Rs["pdo"][start:end, start:end] rmdsurr[k, i] = rqa.rmd(Rsw_enso, Rsw_pdo) _progressbar_update(pb, k) _progressbar_finish(pb) # get each individual array out of dict to avoid NumPy import error SURR_enso = SURR["enso"] SURR_pdo = SURR["pdo"] tm = np.array([date.toordinal() for date in tm]) # save output EPS = int(EPS * 100) FN = DATPATH + "rmd_WS%d_SS%d_EPS%dpc_NSURR%d" \ % (ws, ss, EPS, ns) np.savez( FN, rmd=rmd, tm=tm, rmdsurr=rmdsurr, SURR_enso=SURR_enso, SURR_pdo=SURR_pdo, ) if args.verbose: print("output saved to: %s.npz" % FN) return None
def _get_spl(): """ Estimates the average shortest path length SPL for the indices. """ # load data utils._printmsg("load data ...", args.verbose) t, x_enso, x_pdo = _load_indices() x = { "enso": x_enso, "pdo": x_pdo, } names = ["enso", "pdo"] # get surrogates utils._printmsg("iAAFT surrogates ...", args.verbose) ns = args.nsurr SURR = {} for name in names: utils._printmsg("\t for %s" % name.upper(), args.verbose) SURR[name] = rc.surrogates(x[name], ns, "iaaft", verbose=args.verbose) # recurrence plot parameters EPS, LMIN = 0.30, 3 thrby = "frr" # get SPL for original data utils._printmsg("SPL for original data ...", args.verbose) n = len(t) ws, ss = args.window_size, args.step_size nw = int(np.floor(float(n - ws) / float(ss))) tm = np.empty(nw, dtype="object") m, tau = {}, {} A = {} maxlag = 150 maxdim = 20 r_fnn = 0.0010 SPL = {} for name in names: if args.verbose: print("\t for %s" % name.upper()) # get embedding parameters ## get mi mi, mi_lags = rc.mi(x[name], maxlag, pbar_on=False) # mi, mi_lags = rc.acf(x[name], maxlag) mi_filt, _ = utils.boxfilter(mi, filter_width=3, estimate="mean") try: tau[name] = rc.first_minimum(mi_filt) except ValueError: tau[name] = 1 ## FNN fnn, dims = rc.fnn(x[name], tau[name], maxdim=maxdim, r=r_fnn, pbar_on=False) m[name] = dims[rc.first_zero(fnn)] A[name] = rc.rn( x[name], m=m[name], tau=tau[name], e=EPS, norm="euclidean", threshold_by=thrby, ) A_ = A[name] G_ = ig.Graph.Adjacency(A_.tolist(), mode=ig.ADJ_UNDIRECTED) nw = len(tm) spl = np.zeros(nw) pb = _progressbar_start(max_value=nw, pbar_on=args.verbose) for i in range(nw): start = i * ss end = start + ws Gw = G_.subgraph(vertices=G_.vs[start:end]) pl_hist = Gw.path_length_hist(directed=False) spl[i] = pl_hist.mean tm[i] = t[start] + (t[end] - t[start]) / 2 _progressbar_update(pb, i) _progressbar_finish(pb) SPL[name] = spl # get SPL for surrogate data utils._printmsg("SPL for surrogates ...", args.verbose) SPLSURR = {} for name in names: utils._printmsg("\tfor %s" % name.upper(), args.verbose) xs = SURR[name] y = np.diff(xs, axis=0) splsurr = np.zeros((ns, nw), dtype="float") pb = _progressbar_start(max_value=ns, pbar_on=args.verbose) for k in range(ns): As = rc.rp( xs[k], m=m[name], tau=tau[name], e=EPS, norm="euclidean", threshold_by=thrby, ) Gs = ig.Graph.Adjacency(As.tolist(), mode=ig.ADJ_UNDIRECTED) for i in range(nw): start = i * ss end = start + ws Gw = Gs.subgraph(vertices=Gs.vs[start:end]) pl_hist = Gw.path_length_hist(directed=False) splsurr[k, i] = pl_hist.mean _progressbar_update(pb, k) _progressbar_finish(pb) SPLSURR[name] = splsurr # get each individual array out of dict to avoid NumPy import error SPL_enso = SPL["enso"] SPL_pdo = SPL["pdo"] SPLSURR_enso = SPLSURR["enso"] SPLSURR_pdo = SPLSURR["pdo"] SURR_enso = SURR["enso"] SURR_pdo = SURR["pdo"] tm = np.array([date.toordinal() for date in tm]) # save output EPS = int(EPS * 100) FN = DATPATH + "spl_WS%d_SS%d_EPS%dpc_LMIN%d_NSURR%d" \ % (ws, ss, EPS, LMIN, ns) np.savez(FN, SPL_enso=SPL_enso, SPL_pdo=SPL_pdo, SPLSURR_enso=SPLSURR_enso, SPLSURR_pdo=SPLSURR_pdo, SURR_enso=SURR_enso, SURR_pdo=SURR_pdo, tm=tm) if args.verbose: print("output saved to: %s.npz" % FN) return None
def _get_det(): """ Estimates the determinism DET for the indices. """ # load data utils._printmsg("load data ...", args.verbose) t, x_enso, x_pdo = _load_indices() x = { "enso": x_enso, "pdo": x_pdo, } names = ["enso", "pdo"] # get surrogates utils._printmsg("iAAFT surrogates ...", args.verbose) ns = args.nsurr SURR = {} for name in names: utils._printmsg("\t for %s" % name.upper(), args.verbose) SURR[name] = rc.surrogates(x[name], ns, "iaaft", verbose=args.verbose) # recurrence plot parameters EPS, LMIN = 0.30, 3 thrby = "frr" # get DET for original data utils._printmsg("DET for original data ...", args.verbose) n = len(t) ws, ss = args.window_size, args.step_size nw = int(np.floor(float(n - ws) / float(ss))) tm = np.empty(nw, dtype="object") m, tau = {}, {} R = {} maxlag = 150 maxdim = 20 r_fnn = 0.0010 DET = {} for name in names: if args.verbose: print("\t for %s" % name.upper()) # get embedding parameters ## get mi mi, mi_lags = rc.mi(x[name], maxlag, pbar_on=False) # mi, mi_lags = rc.acf(x[name], maxlag) mi_filt, _ = utils.boxfilter(mi, filter_width=3, estimate="mean") try: tau[name] = rc.first_minimum(mi_filt) except ValueError: tau[name] = 1 ## FNN fnn, dims = rc.fnn(x[name], tau[name], maxdim=maxdim, r=r_fnn, pbar_on=False) m[name] = dims[rc.first_zero(fnn)] R[name] = rc.rp( x[name], m=m[name], tau=tau[name], e=EPS, norm="euclidean", threshold_by=thrby, ) R_ = R[name] nw = len(tm) det = np.zeros(nw) pb = _progressbar_start(max_value=nw, pbar_on=args.verbose) for i in range(nw): start = i * ss end = start + ws Rw = R_[start:end, start:end] det[i] = rqa.det(Rw, lmin=LMIN, hist=None, verb=False) tm[i] = t[start] + (t[end] - t[start]) / 2 _progressbar_update(pb, i) _progressbar_finish(pb) DET[name] = det # get DET for surrogate data utils._printmsg("DET for surrogates ...", args.verbose) DETSURR = {} for name in names: utils._printmsg("\tfor %s" % name.upper(), args.verbose) xs = SURR[name] y = np.diff(xs, axis=0) detsurr = np.zeros((ns, nw), dtype="float") pb = _progressbar_start(max_value=ns, pbar_on=args.verbose) for k in range(ns): Rs = rc.rp( xs[k], m=m[name], tau=tau[name], e=EPS, norm="euclidean", threshold_by=thrby, ) for i in range(nw): start = i * ss end = start + ws Rw = Rs[start:end, start:end] detsurr[k, i] = rqa.det(Rw, lmin=LMIN, hist=None, verb=False) _progressbar_update(pb, k) _progressbar_finish(pb) DETSURR[name] = detsurr # get each individual array out of dict to avoid NumPy import error DET_enso = DET["enso"] DET_pdo = DET["pdo"] DETSURR_enso = DETSURR["enso"] DETSURR_pdo = DETSURR["pdo"] SURR_enso = SURR["enso"] SURR_pdo = SURR["pdo"] tm = np.array([date.toordinal() for date in tm]) # save output EPS = int(EPS * 100) FN = DATPATH + "det_WS%d_SS%d_EPS%dpc_LMIN%d_NSURR%d" \ % (ws, ss, EPS, LMIN, ns) np.savez(FN, DET_enso=DET_enso, DET_pdo=DET_pdo, DETSURR_enso=DETSURR_enso, DETSURR_pdo=DETSURR_pdo, SURR_enso=SURR_enso, SURR_pdo=SURR_pdo, tm=tm) if args.verbose: print("output saved to: %s.npz" % FN) return None
def _get_data(): """ Estimates Lyapunov, DET, and SPL for Henon map. """ # Henon map time series print("Henon map time series ...") t = np.arange(0, 10000, 1) a = np.linspace(1.28, 1.32, na).reshape(na, 1) j, k = (1 * na) / 8, ns / 2 print "a = ", a[j] # sys.exit() b = 0.30 nt = len(t) x, y = [np.zeros((nt, na, ns)) for i in range(2)] x[0, :, :] = 1E-2 * np.random.rand(na, ns) y[0, :, :] = 1E-2 * np.random.rand(na, ns) pb = _progressbar_start(max_value=nt, pbar_on=True) LPV = np.zeros((na, ns)) for i in range(1, nt): x[i, :, :] = 1. - a * x[i - 1, :, :]**2 + y[i - 1, :, :] y[i, :, :] = b * x[i - 1, :, :] if i >= nt / 2: LPV[:, :] += np.log(np.fabs(-2. * a * x[i - 1, :, :])) _progressbar_update(pb, i) _progressbar_finish(pb) xH_eq = x[-neq:, :, :] LPV /= float(nt) print("RP ...") RR = 0.30 y = xH_eq[:, j, k].flatten() R = rc.rp(y, m=2, tau=1, e=RR, norm="euclidean", threshold_by="frr") DET = rqa.det(R, lmin=2, hist=None, verb=False) print DET print("plot...") pl.subplot(211) pl.plot(y, alpha=0.5) pl.subplot(212) pl.imshow(R, cmap=pl.cm.gray_r, origin="lower", interpolation="none") pl.show() sys.exit() print("plot data ...") xplot = np.zeros((na, neq * ns)) for i in range(na): xplot[i] = xH_eq[:, i, :].flatten() print("plot ...") fig = pl.figure(figsize=[21., 12.], facecolor="none") ax = fig.add_axes([0.10, 0.10, 0.80, 0.80]) ax.plot(a, xplot, "o", ms=1.00, alpha=0.25, rasterized=True, mfc="k", mec="none") print("prettify ...") ax.tick_params(labelsize=14, size=8) ax.tick_params(size=5, which="minor") # ax.set_xticks(np.arange(1.0, 1.401, 0.05), minor=False) # ax.set_xticks(np.arange(1.0, 1.401, 0.01), minor=True) ax.grid(which="both") # ax.set_xlim(1.0, 1.4) print("save figure ...") FN = "../plots/" + __file__[2:-3] + ".png" fig.savefig(FN, rasterized=True, dpi=100) print("figure saved to: %s" % FN) sys.exit() # estimate embedding parameters print("embedding parameters ...") tau, m = np.ones(na, dtype="int"), 2 * np.ones(na, dtype="int") # DET print("DET ...") RR = 0.25 DET = np.zeros((na, ns)) pb = _progressbar_start(max_value=ns * na, pbar_on=True) k = 0 for j in range(ns): for i in range(na): R = rc.rp(xH_eq[:, i, j], m=m[i], tau=tau[i], e=RR, norm="euclidean", threshold_by="frr") DET[i, j] = rqa.det(R, lmin=2, hist=None, verb=False) del R _progressbar_update(pb, k) k += 1 _progressbar_finish(pb) # SPL print("SPL ...") SPL = np.zeros((na, ns)) pb = _progressbar_start(max_value=ns * na, pbar_on=True) k = 0 for j in range(ns): for i in range(na): A = rc.rn(xH_eq[:, i, j], m=m[i], tau=tau[i], e=RR, norm="euclidean", threshold_by="frr") G = ig.Graph.Adjacency(A.tolist(), mode=ig.ADJ_UNDIRECTED) pl_hist = G.path_length_hist(directed=False) SPL[i, j] = pl_hist.mean del A, G _progressbar_update(pb, k) k += 1 _progressbar_finish(pb) # save output FN = DATPATH + "det_spl_lpv_na%d_ns%s_neq%d" % (na, ns, neq) np.savez(FN, DET=DET, SPL=SPL, LPV=LPV, t=t, a=a, b=b, x=x, y=y) print("saved to %s.npz" % FN) return None