Example #1
0
 def foreach(G, data, ax):
     idx = 0
     rp = RecurrencePlot(data[:, idx],
                         threshold=0.3,
                         metric='supremum',
                         normalize=True)
     mat = rp.recurrence_matrix()
     ax.imshow(mat, origin='lower')
Example #2
0
def decimate_and_recurrence(signal, decimate_q=4, dim=1, tau=1, metric='supremum', normalize=False, threshold=0.2):
    if len(signal):
        decimated_signal = decimate_filtfilt(x=signal, q=int(decimate_q))
        rp = RecurrencePlot(decimated_signal, dim=dim, tau=tau, metric=metric,
                     normalize=normalize, threshold=threshold)
        return rp.laminarity(),rp.determinism()
    else:
        return np.nan
def run_main_prog(params):
    #start = time.clock()
    (m,numSig) = params # m is index for noise level, numSig is idex for realization
    numSurr = 99
    SignalAndSurr = numpy.zeros((numSurr+1,N),dtype='float32')
    noise = NL[m]*numpy.std(SignalX[numSig,:].copy())*numpy.random.normal(0,1,N)
    SignalAndSurr[0,:] = SignalX[numSig,:].copy()+noise
    for j in range(1,numSurr+1,1):
         SignalAndSurr[j,:] = UnivariateSurrogates(SignalAndSurr[0,:].copy(),120)
    
    T = numpy.zeros((numSurr+1,1),dtype='float32')          
    
    for k in range(0,numSurr+1,1):
        ts = SignalAndSurr[k,:]
        ts.shape = (-1,1)
        psv = RecurrencePlot.embed_time_series(ts,dim=3,tau=3)
        randomVertices = random.sample(xrange(psv.shape[0]), int(sys.argv[1]))
        R = RecurrenceNetwork(psv[randomVertices,:],recurrence_rate=float(sys.argv[2]),silence_level=2)
        T[k] = R.transitivity() # compute network measure for hypothesis testing

    if T[0] > max(T[1:]): #non-parametric testing
         H0 = 1 #null-hypothesis rejected
    else:
         H0 = 0
    #elapsed = (time.clock() - start)
    #print elapsed
    return (H0)
Example #4
0
    def rp_timeseries_embedding(ax, data, **kwargs):
        """recurrence plot"""
        emb_del = 1
        emb_dim = 10
        # make data "strictly" one-dimensional
        data = data.reshape((-1, ))
        rp = RecurrencePlot(time_series=data,
                            tau=emb_del,
                            dim=emb_dim,
                            threshold_std=1.5)

        plotdata = rp.recurrence_matrix()
        length = plotdata.shape[0]

        xs = np.linspace(0, length, length)
        ys = np.linspace(0, length, length)
        ax.pcolormesh(xs, ys, plotdata, cmap=plt.get_cmap("Oranges"))
        ax.set_xlabel("$n$")
        ax.set_ylabel("$n$")
def fun_rqa(v,metric):
    # Attempt sparse RQA if metric is euclidean
    metric_sup = (metric is "supremum")
    rp = RecurrencePlot(v, metric=metric, sparse_rqa=metric_sup,
    threshold=1.2, dim=3, tau=6)
    rqa = rp.rqa_summary()
    rqa["Lmax"] = rp.max_diaglength()
    rqa["ENT"] = rp.diag_entropy()
    rqa["TT"] = rp.trapping_time()
    return(rqa)
Example #6
0
#  Create a time series using the logistic map
time_series = logistic_map(x0, r, T)

#  Print the time series
print(time_series)

#  Plot the time series
pylab.plot(time_series, "r")
#  You can include LaTex labels...
pylab.xlabel("$n$")
pylab.ylabel("$x_n$")

#  Generate a recurrence plot object with fixed recurrence threshold EPS
rp = RecurrencePlot(time_series,
                    dim=DIM,
                    tau=TAU,
                    metric=METRIC,
                    normalize=False,
                    threshold=EPS)

#  Show the recurrence plot
pylab.matshow(rp.recurrence_matrix())
pylab.xlabel("$n$")
pylab.ylabel("$n$")
pylab.show()

#  Calculate and print the recurrence rate
print("Recurrence rate:", rp.recurrence_rate())

#  Calculate some standard RQA measures
DET = rp.determinism(l_min=2)
LAM = rp.laminarity(v_min=2)
#

#  Create a time series using the logistic map
time_series = logistic_map(x0, r, T)

#  Print the time series
print time_series

#  Plot the time series
pylab.plot(time_series, "r")
#  You can include LaTex labels...
pylab.xlabel("$n$")
pylab.ylabel("$x_n$")

#  Generate a recurrence plot object with fixed recurrence threshold EPS
rp = RecurrencePlot(time_series, dim=DIM, tau=TAU, metric=METRIC,
                    normalize=False, threshold=EPS)

#  Show the recurrence plot
pylab.matshow(rp.recurrence_matrix())
pylab.xlabel("$n$")
pylab.ylabel("$n$")
pylab.show()

#  Calculate and print the recurrence rate
print "Recurrence rate:", rp.recurrence_rate()

#  Calculate some standard RQA measures
DET = rp.determinism(l_min=2)
LAM = rp.laminarity(v_min=2)

print "Determinism:", DET
      dop = 'betta'
    if (list1[0][46] == 'h'):
      dop = 'alpha'
    if not(os.path.exists(path1 + i[11] + '/' + dop + '/')):
      os.mkdir(path1 + i[11] + '/' + dop + '/')
    if not(os.path.exists(path1 + i[11] + '/' + dop + '/' + i[24:27] + '/')):
      os.mkdir(path1 + i[11] + '/' + dop + '/' + i[24:27] + '/')
    mainpath = path1 + i[11] + '/' + dop + '/' + i[24:27] + '/'
    print('The folder is {}'.format(mainpath))
    df = pd.read_csv(path+i)
    df = df.drop(columns = 'Unnamed: 0' )
    df1 = pd.DataFrame()
    for j in df.columns:
      if (int(j) <= 31):
        print('Column number {}'.format(j))
        rpx = RecurrencePlot(np.array(df[j][0:int(len(df)/6)]),threshold_std=0.03)
        rpx = rpx.recurrence_matrix()
        np.save(mainpath + j +'_RM_' + i[0:len(i)-3] + 'npy' , rpx)

df = pd.read_csv('/content/filters/Participant1_Backgr_int_0.1_type_0.4filter_alpha.csv')

data2=np.load('temp.npy')

list1[5][45]

df = np.load("/content/filters1/3/alpha/0.1/0_RM_Participant3_Backgr_int_0.1_type_0.4filter_alpha.npy")

df1 = df = np.load("/content/filters1/3/alpha/0.1/1_RM_Participant3_Backgr_int_0.1_type_0.4filter_alpha.npy")

/content/filters1/3/alpha/0.1/0_RM_Participant3_Backgr_int_0.1_type_0.4filter_alpha.npy
Example #9
0
def Non_parametric_test(analisys_par,
                        datasets_par,
                        participant,
                        intencity,
                        band1,
                        band2,
                        time_windows=[],
                        time_cut=False,
                        plot=False):
    counter = 0
    output = {}
    proj_path = os.path.dirname(os.path.abspath('./project2.py'))
    dataframes = load_preprocesed_data(analisys_par, datasets_par, participant)
    comb_list = itertools.combinations(list(analisys_par["bands"]), 2)
    N = len(time_windows)

    output = {"time_window": [], "Mean_RMD": []}
    for time_window in time_windows:
        tmp = []
        output[bands[0] + '-' + bands[1]]["time_window"].append(time_window)
        dt_frames_1 = dataframes['Proj2Dataset'][Participant][bands[0]]
        dt_frames_2 = dataframes['Proj2Dataset'][Participant][bands[1]]
        if intencity == 1.0:
            intencity = int(intencity)
        df1 = dt_frames_1[bands[0] + "_int_{}.csv".format(intencity)]
        df2 = dt_frames_2[bands[1] + "_int_{}.csv".format(intencity)]
        for i, colx in enumerate(df1.columns):
            if 31 >= i > 0:
                if os.path.exists(proj_path + "/tmp/i_{}".format(i) + ".npy"):
                    rpx = np.load(proj_path + "/tmp/i_{}".format(i) + ".npy")
                else:
                    if not time_cut:
                        rpx = np.array(df1[colx])
                    else:
                        rpx = np.array(df1[colx][:15 * 250])

                    if time_window > 0:
                        rpx = rpx[:-int(time_window * 250)]

                    if time_window < 0:
                        rpx = rpx[int(time_window * 250):]

                    rpx = RecurrencePlot(rpx, threshold_std=0.03)
                    rpx = rpx.recurrence_matrix()
                    np.save(proj_path + "/tmp/i_{}".format(i), rpx)

            for j, coly in enumerate(df2.columns):
                if 31 >= i > 0 and 31 >= j > 0:
                    counter += 1
                    if os.path.exists(proj_path + "/tmp/j_{}".format(j) +
                                      ".npy"):
                        rpy = np.load(proj_path + "/tmp/j_{}".format(j) +
                                      ".npy")
                    else:
                        if not time_cut:
                            rpy = np.array(df2[coly])
                        else:
                            rpy = np.array(df2[coly][:15 * 250])

                        if time_window > 0:
                            rpy = rpy[int(time_window * 250):]

                        if time_window < 0:
                            rpy = rpy[:-int(time_window * 250)]

                        rpy = RecurrencePlot(rpy, threshold_std=0.03)
                        rpy = rpy.recurrence_matrix()
                        np.save(proj_path + "/tmp/j_{}".format(j), rpy)

                    RMD = np.log2(
                        np.mean(
                            (np.mean(np.multiply(rpx, rpy), axis=0) /
                             (np.mean(rpx, axis=0) * np.mean(rpy, axis=0)))))
                    tmp.append(RMD)
                    print("Reccurence_matrix_computed / Total:\n{}/{}".format(
                        counter, (31 * 31) * N))
                else:
                    continue

        output[bands[0] + '-' + bands[1]]["Mean_RMD"].append(np.mean(tmp))

        for i, colx in enumerate(df1.columns):
            if os.path.exists(proj_path + "/tmp/i_{}".format(i) + ".npy"):
                os.remove(proj_path + "/tmp/i_{}".format(i) + ".npy")
        for j, coly in enumerate(df2.columns):
            if os.path.exists(proj_path + "/tmp/j_{}".format(j) + ".npy"):
                os.remove(proj_path + "/tmp/j_{}".format(j) + ".npy")

    if plot:
        f = plt.figure()
        lgn = []
        comb_list = itertools.combinations(list(analisys_par["bands"]), 2)
        for bands in comb_list:
            plt.plot(output[bands[0] + '-' + bands[1]]["time_window"],
                     output[bands[0] + '-' + bands[1]]["Mean_RMD"])
            lgn.append(bands[0] + '-' + bands[1])
        plt.xlabel('time_window')
        plt.ylabel("Mean_RMD")
        plt.legend(lgn)
        plt.savefig("./Coupling_strength_between_bands/" + participant +
                    '_int_' + str(intencity) + ".png")
        plt.show()
    return output
Example #10
0
def calculate_rp_between_bands(analisys_par,
                               datasets_par,
                               Participant,
                               time_cut=False,
                               plot=False):
    output = {}
    dataframes = load_preprocesed_data(analisys_par, datasets_par, Participant)
    print("Dataframes loaded")
    proj_path = os.path.dirname(os.path.abspath('./project2.py'))

    counter = 0
    comb_list = itertools.combinations(list(analisys_par["bands"]), 2)

    for bands in comb_list:
        output[bands[0] + '-' + bands[1]] = {
            "intencities": [],
            "Mean_RMD": [],
            "Mean_diag_RMD": [],
            "Mean_RMD_mat": {
                "intencity": [],
                "channels": []
            }
        }
        dt_frames_1 = dataframes['Proj2Dataset'][Participant][bands[0]]
        dt_frames_2 = dataframes['Proj2Dataset'][Participant][bands[1]]
        for k, intensity in enumerate(range(1, 11)):
            intensity = round(intensity * 0.1, 1)
            if intensity == 1.0:
                intensity = int(intensity)
            df1 = dt_frames_1[bands[0] + "_int_{}.csv".format(intensity)]
            df2 = dt_frames_2[bands[1] + "_int_{}.csv".format(intensity)]
            tmp_overhaul = []
            tmp_diag = []
            output[bands[0] + '-' + bands[1]]["intencities"].append(intensity)
            output[bands[0] + '-' +
                   bands[1]]["Mean_RMD_mat"]["intencity"].append(intensity)
            output[bands[0] + '-' +
                   bands[1]]["Mean_RMD_mat"]["channels"].append(
                       np.zeros((31, 31)))

            for i, colx in enumerate(df1.columns):
                if 31 >= i > 0:
                    if os.path.exists(proj_path + "/tmp/i_{}".format(i) +
                                      ".npy"):
                        rpx = np.load(proj_path + "/tmp/i_{}".format(i) +
                                      ".npy")
                    else:
                        if not time_cut:
                            rpx = RecurrencePlot(np.array(df1[colx]),
                                                 threshold_std=0.03)
                        else:
                            rpx = RecurrencePlot(np.array(df1[colx][:250 *
                                                                    15]),
                                                 threshold_std=0.03)
                        rpx = rpx.recurrence_matrix()
                        np.save(proj_path + "/tmp/i_{}".format(i), rpx)

                for j, coly in enumerate(df2.columns):
                    if 31 >= i > 0 and 31 >= j > 0:
                        counter += 1
                        if os.path.exists(proj_path + "/tmp/j_{}".format(j) +
                                          ".npy"):
                            rpy = np.load(proj_path + "/tmp/j_{}".format(j) +
                                          ".npy")
                        else:
                            if not time_cut:
                                rpy = RecurrencePlot(np.array(df2[coly]),
                                                     threshold_std=0.03)
                            else:
                                rpy = RecurrencePlot(np.array(df2[coly][:250 *
                                                                        15]),
                                                     threshold_std=0.03)
                            rpy = rpy.recurrence_matrix()
                            np.save(proj_path + "/tmp/j_{}".format(j), rpy)

                        RMD = np.log2(
                            np.mean((np.mean(np.multiply(rpx, rpy), axis=0) /
                                     (np.mean(rpx, axis=0) *
                                      np.mean(rpy, axis=0)))))
                        tmp_overhaul.append(RMD)
                        if i == j:
                            tmp_diag.append(RMD)
                        output[bands[0] + '-' +
                               bands[1]]["Mean_RMD_mat"]["channels"][k][
                                   i - 1, j - 1] = RMD

                        print("Reccurence_matrix_computed / Total:\n{}/{}".
                              format(counter, 31 * 31 * 10))
                    else:
                        continue

            output[bands[0] + '-' + bands[1]]["Mean_RMD"].append(
                np.mean(tmp_overhaul))

            output[bands[0] + '-' + bands[1]]["Mean_diag_RMD"].append(
                np.mean(tmp_diag))

            for i, colx in enumerate(df1.columns):
                if os.path.exists(proj_path + "/tmp/i_{}".format(i) + ".npy"):
                    os.remove(proj_path + "/tmp/i_{}".format(i) + ".npy")
            for j, coly in enumerate(df2.columns):
                if os.path.exists(proj_path + "/tmp/j_{}".format(j) + ".npy"):
                    os.remove(proj_path + "/tmp/j_{}".format(j) + ".npy")

    # for bands in comb_list:
    #     tmp = sorted(zip(output[bands[0]+'-'+bands[1]]["intencities"],
    #                      output[bands[0]+'-'+bands[1]]["Mean_RMD"]))
    #     output[bands[0]+'-'+bands[1]]["intencities"], output[bands[0]+'-'+bands[1]]["Mean_RMD"] = zip(*tmp)

    with open("./Mean_RMD_between_bands/" + Participant + '.pickle',
              'wb') as handle:
        pickle.dump(output, handle, protocol=pickle.HIGHEST_PROTOCOL)

    if plot:
        comb_list = itertools.combinations(list(analisys_par["bands"]), 2)
        fig1 = plt.figure()
        cumul_lgn = []
        for bands in comb_list:
            pair = bands[0] + '-' + bands[1]
            plt.plot(output[pair]["intencities"][:],
                     output[pair]["Mean_RMD"][:])
            cumul_lgn.append(pair)
        plt.xlabel('intencities')
        plt.ylabel("Mean_RMD")
        plt.legend(cumul_lgn)
        plt.savefig("./Mean_RMD_between_bands/" + Participant + ".png")
        plt.show()

        comb_list = itertools.combinations(list(analisys_par["bands"]), 2)
        fig2 = plt.figure()
        cumul_lgn = []
        for bands in comb_list:
            pair = bands[0] + '-' + bands[1]
            plt.plot(output[pair]["intencities"][:],
                     output[pair]["Mean_diag_RMD"][:])
            cumul_lgn.append(pair)
        plt.xlabel('intencities')
        plt.ylabel("Mean_diag_RMD")
        plt.legend(cumul_lgn)
        plt.savefig("./Mean_RMD_between_bands/" + Participant + "_diag.png")
        plt.show()

    return output
Example #11
0
def coupling_RMD_inside_band(analisys_par,
                             datasets_par,
                             participant,
                             name,
                             dataframe,
                             time_windows=[],
                             time_cut=False,
                             plot=False):
    counter = 0
    output = {"time_window": [], "Mean_RMD": []}
    proj_path = os.path.dirname(os.path.abspath('./project2.py'))
    N = len(time_windows)
    for time_window in time_windows:
        tmp = []
        output["time_window"].append(time_window)
        for i, colx in enumerate(dataframe.columns):
            if 31 >= i > 0:
                if os.path.exists(proj_path + "/tmp/i_{}".format(i) + ".npy"):
                    rpx = np.load(proj_path + "/tmp/i_{}".format(i) + ".npy")
                else:
                    if not time_cut:
                        rpx = np.array(dataframe[colx])
                    else:
                        rpx = np.array(dataframe[colx][:15 * 250])

                    if time_window > 0:
                        rpx = rpx[:-int(time_window * 250)]

                    if time_window < 0:
                        rpx = rpx[int(time_window * 250):]

                    rpx = RecurrencePlot(rpx, threshold_std=0.03)
                    rpx = rpx.recurrence_matrix()

            for j, coly in enumerate(dataframe.columns):
                if i < j and 31 >= i > 0 and 31 >= j > 0:
                    if os.path.exists(proj_path + "/tmp/j_{}".format(j) +
                                      ".npy"):
                        rpy = np.load(proj_path + "/tmp/j_{}".format(j) +
                                      ".npy")
                    else:
                        counter += 1
                        if not time_cut:
                            rpy = np.array(dataframe[coly])
                        else:
                            rpy = np.array(dataframe[coly][:15 * 250])

                        if time_window > 0:
                            rpy = rpy[int(time_window * 250):]

                        if time_window < 0:
                            rpy = rpy[:-int(time_window * 250)]

                        rpy = RecurrencePlot(rpy, threshold_std=0.03)
                        rpy = rpy.recurrence_matrix()

                    RMD = np.log2(
                        np.mean(
                            (np.mean(np.multiply(rpx, rpy), axis=0) /
                             (np.mean(rpx, axis=0) * np.mean(rpy, axis=0)))))
                    tmp.append(RMD)
                    print("Reccurence_matrix_computed / Total:\n{}/{}".format(
                        counter, (31 * 31 / 2 - 31) * N))
                else:
                    continue

        output["Mean_RMD"].append(np.mean(tmp))

    tmp = sorted(zip(output["time_window"], output["Mean_RMD"]))
    output["time_window"], output["Mean_RMD"] = zip(*tmp)

    if plot:
        f = plt.figure()
        plt.plot(output["time_window"], output["Mean_RMD"])
        plt.xlabel('time_window')
        plt.ylabel("Mean_RMD")
        bn = re.search(r'(.*)_int_.*\.csv', name)
        plt.legend(bn.group(1))
        plt.savefig("./Coupling_strength_inside_band/" + participant + '_' +
                    name + ".png")
        plt.show()
    return output
Example #12
0
def calculate_rp_inside_band(analisys_par,
                             datasets_par,
                             Participant,
                             time_cut=False,
                             plot=False):
    output = {}
    dataframes = load_preprocesed_data(analisys_par, datasets_par, Participant)
    print("Dataframes loaded")

    counter = 0
    for bn in analisys_par["bands"]:
        output[bn] = {
            "intencities": [],
            "Mean_RMD": [],
            "Mean_RMD": {
                "intencity": "",
                "channels": np.zeros((31, 31))
            }
        }
        for df in dataframes['Proj2Dataset'][Participant][bn]:
            tmp = []
            intensity = re.search(r'int_(.*)\.csv', df)
            output[bn]["intencities"].append(float(intensity.group(1)))
            output[bn]["Mean_RMD"]["intencity"] = intensity.group(1)
            for i, colx in enumerate(
                    dataframes['Proj2Dataset'][Participant][bn][df].columns):
                if 31 >= i > 0:
                    if not time_cut:
                        rpx = RecurrencePlot(
                            np.array(dataframes['Proj2Dataset'][Participant]
                                     [bn][df][colx]),
                            threshold_std=0.03)
                    else:
                        rpx = RecurrencePlot(
                            np.array(dataframes['Proj2Dataset'][Participant]
                                     [bn][df][colx][:250 * 15]),
                            threshold_std=0.03)
                    rpx = rpx.recurrence_matrix()
                for j, coly in enumerate(dataframes['Proj2Dataset']
                                         [Participant][bn][df].columns):
                    if i < j and 31 >= i > 0 and 31 >= j > 0:
                        counter += 1
                        if not time_cut:
                            rpy = RecurrencePlot(
                                np.array(dataframes['Proj2Dataset']
                                         [Participant][bn][df][coly]),
                                threshold_std=0.03)
                        else:
                            rpy = RecurrencePlot(np.array(
                                dataframes['Proj2Dataset'][Participant][bn][df]
                                [coly][:250 * 15]),
                                                 threshold_std=0.03)
                        rpy = rpy.recurrence_matrix()

                        RMD = np.log2(
                            np.mean((np.mean(np.multiply(rpx, rpy), axis=0) /
                                     (np.mean(rpx, axis=0) *
                                      np.mean(rpy, axis=0)))))
                        tmp.append(RMD)
                        output[bn]["Mean_RMD"]["channels"][i - 1, j - 1] = RMD

                        print("Reccurence_matrix_computed / Total:\n{}/{}".
                              format(counter, 31 * 31 * 10))
                    else:
                        continue

            output[bn]["Mean_RMD"].append(np.mean(tmp))

    for bn in analisys_par["bands"]:
        tmp = sorted(zip(output[bn]["intencities"], output[bn]["Mean_RMD"]))
        output[bn]["intencities"], output[bn]["Mean_RMD"] = zip(*tmp)

    if plot:
        f = plt.figure()
        for j in analisys_par["bands"]:
            plt.plot(output[j]["intencities"], output[j]["Mean_RMD"])
        plt.xlabel('intencities')
        plt.ylabel("Mean_RMD")
        plt.legend(analisys_par["bands"])
        plt.savefig("./Mean_RMD_inside_band/" + Participant + ".png")
        plt.show()
    return output