예제 #1
0
def schedule(blocknum, blocksize, totalsize):

    if totalsize == 0:
        percent = 0
    else:
        percent = blocknum * blocksize / totalsize
    if percent > 1.0:
        percent = 1.0
    percent = percent * 100
    show_progressbar("Downloading", percent)
예제 #2
0
def ctrdm_cal(data, time_win=10, time_step=5):
    """
    a function to calculate CTRDMs for a single channel & a single subject

    Parameters
    ----------
    data : array
        EEG/MEG data from a time-window.
        The shape of data should be [n_conditions, n_ts]. n_conditions, n_ts represent the number of conditions and the
        number of time-points respectively.
    time_win : int. Default is 10.
        Set a time-window for calculating the CTRDM for different time-points.
        If time_win=10, that means each calculation process based on 10 time-points.
    time_step : int. Default is 5.
        The time step size for each time of calculating.

    Returns
    -------
    CTRDMs : array [int((n_ts-time_win)/time_step)+1, int((n_ts-time_win)/time_step)+1, n_conditions, n_conditions]
        Cross-Temporal RDMs.
    """

    n_cons, n_ts = np.shape(data)

    nts = int((n_ts - time_win) / time_step) + 1

    data_for_cal = np.zeros([n_cons, nts, time_win], dtype=np.float)

    for con in range(n_cons):
        for t in range(nts):
            data_for_cal[con, t] = data[con,
                                        t * time_step:t * time_step + time_win]

    ctrdms = np.zeros([nts, nts, n_cons, n_cons], dtype=np.float)

    total = nts * nts

    for t1 in range(nts):
        for t2 in range(nts):

            percent = (t1 * nts + t2) / total * 100
            show_progressbar("Calculating", percent)

            for con1 in range(n_cons):
                for con2 in range(n_cons):

                    if con1 != con2:
                        r = pearsonr(data_for_cal[con1, t1],
                                     data_for_cal[con2, t2])[0]
                        ctrdms[t1, t2, con1, con2] = 1 - r
                    if con1 == con2:
                        ctrdms[t1, t2, con1, con2] = 0

    return ctrdms
예제 #3
0
파일: multi_cal.py 프로젝트: seapsy/PyCTRSA
def ctrdms_cal(data, sub_opt=1, chl_opt=1, time_win=10, time_step=5):
    """
    a function to calculate CTRDMs for multi-channels

    Parameters
    ----------
    data : array
        EEG/MEG data from a time-window.
        The shape of data should be [n_conditions, n_subs, n_channels, n_ts]. n_conditions, n_subs, n_channels, n_ts
        represent the number of conditions, the number of subjects, the number of channels and the number of time-points
        respectively.
    sub_opt : int 0 or 1. Default is 1.
        Caculate the CTRDMs for each subject or not.
        If sub_opt=1, return the CTRDMs for each subjects.
        If sub_opt=0, return the avg CTRDMs among all subjects.
    chl_opt : int 0 or 1. Default is 1.
        Caculate the CTRDMs for each channel or not.
        If chl_opt=1, calculate the CTRDMs for each channel.
        If chl_opt=0, calculate the CTRDMs after averaging the channels.
    time_win : int. Default is 10.
        Set a time-window for calculating the CTRDM for different time-points.
        If time_win=10, that means each calculation process based on 10 time-points.
    time_step : int. Default is 5.
        The time step size for each time of calculating.

    Returns
    -------
    CTRDMs : array [int((n_ts-time_win)/time_step)+1, int((n_ts-time_win)/time_step)+1, n_conditions, n_conditions]
        Cross-Temporal RDMs.
    """

    n_cons, n_subs, n_chls, n_ts = np.shape(data)

    nts = int((n_ts - time_win) / time_step) + 1

    # chl_opt=0
    if chl_opt == 0:

        data_for_cal = np.zeros([n_cons, n_subs, nts, n_chls, time_win],
                                dtype=np.float)

        for con in range(n_cons):
            for sub in range(n_subs):
                for t in range(nts):
                    for chl in range(n_chls):
                        data_for_cal[con, sub, t,
                                     chl] = data[con, sub, chl,
                                                 t * time_step:t * time_step +
                                                 time_win]

        data_for_cal = np.reshape(data_for_cal,
                                  [n_cons, n_subs, nts, n_chls * time_win])

        ctrdms = np.zeros([n_subs, nts, nts, n_cons, n_cons], dtype=np.float)

        total = n_subs * nts * nts

        for sub in range(n_subs):
            for t1 in range(nts):
                for t2 in range(nts):
                    percent = (sub * nts * nts + t1 * nts + t2) / total * 100
                    show_progressbar("Calculating", percent)

                    for con1 in range(n_cons):
                        for con2 in range(n_cons):

                            if con1 != con2:
                                r = pearsonr(data_for_cal[con1, sub, t1],
                                             data_for_cal[con2, sub, t2])[0]
                                ctrdms[sub, t1, t2, con1, con2] = 1 - r
                            if con1 == con2:
                                ctrdms[sub, t1, t2, con1, con2] = 0

        # chl_opt=0 & sub_opt=0
        if sub_opt == 0:

            return np.average(ctrdms, axis=0)

        # chl_opt=0 & sub_opt=1
        else:

            return ctrdms

    # chl_opt=1
    else:

        ctrdms = np.zeros([n_subs, n_chls, nts, nts, n_cons, n_cons],
                          dtype=np.float)

        for sub in range(n_subs):
            for chl in range(n_chls):

                print("\nSubject " + str(sub + 1) + " Channel " +
                      str(chl + 1) + ":")

                ctrdms[sub, chl] = single_cal.ctrdm_cal(data[:, sub, chl],
                                                        time_win=time_win,
                                                        time_step=time_step)

        # chl_opt=1 & sub_opt=0
        if sub_opt == 0:

            return np.average(ctrdms, axis=0)

        # chl_opt=1 & sub_opt=1
        else:

            return ctrdms


# test
#data = np.random.rand(16, 40, 5, 50)
#from pyctrsa.plotting import ctrdm
#ctrdms = ctrdms_cal(data, sub_opt=1, chl_opt=0)
#ctrdms = np.average(ctrdms, axis=0)
#ctrdm.ctrdm_plot(ctrdms[1, 3])
예제 #4
0
def pre_data(subs, data_dir):

    newdata_dir = data_dir + 'data_for_CTRSA/'

    if os.path.exists(newdata_dir) == False:
        os.makedirs(newdata_dir)
    if os.path.exists(newdata_dir + 'ERP/') == False:
        os.makedirs(newdata_dir + 'ERP/')
    if os.path.exists(newdata_dir + 'Alpha/') == False:
        os.makedirs(newdata_dir + 'Alpha/')

    n = len(subs)
    subindex = 0

    for sub in subs:
        data = sio.loadmat(data_dir + "data/ERP" + sub +
                           ".mat")["filtData"][:, :, 250:]
        # data.shape: n_trials, n_channels, n_times

        ori_label = np.loadtxt(data_dir + "labels/ori_" + sub + ".txt")[:, 1]
        pos_label = np.loadtxt(data_dir + "labels/pos_" + sub + ".txt")[:, 1]

        ori_subdata = np.zeros([16, 40, 27, 500], dtype=np.float)
        pos_subdata = np.zeros([16, 40, 27, 500], dtype=np.float)

        ori_labelindex = np.zeros([16], dtype=np.int)
        pos_labelindex = np.zeros([16], dtype=np.int)

        for i in range(640):
            label = int(ori_label[i])
            ori_subdata[label, ori_labelindex[label]] = data[i]
            ori_labelindex[label] = ori_labelindex[label] + 1
            label = int(pos_label[i])
            pos_subdata[label, pos_labelindex[label]] = data[i]
            pos_labelindex[label] = pos_labelindex[label] + 1

        f = h5py.File(newdata_dir + "ERP/" + sub + ".h5", "w")
        f.create_dataset("ori", data=ori_subdata)
        f.create_dataset("pos", data=pos_subdata)
        f.close()

        data = sio.loadmat(data_dir + "data/Alpha" + sub +
                           ".mat")["filtData"][:, :, 250:]
        # data.shape: n_trials, n_channels, n_times

        ori_label = np.loadtxt(data_dir + "labels/ori_" + sub + ".txt")[:, 1]
        pos_label = np.loadtxt(data_dir + "labels/pos_" + sub + ".txt")[:, 1]

        ori_subdata = np.zeros([16, 40, 27, 500], dtype=np.float)
        pos_subdata = np.zeros([16, 40, 27, 500], dtype=np.float)

        ori_labelindex = np.zeros([16], dtype=np.int)
        pos_labelindex = np.zeros([16], dtype=np.int)

        for i in range(640):
            label = int(ori_label[i])
            ori_subdata[label, ori_labelindex[label]] = data[i]
            ori_labelindex[label] = ori_labelindex[label] + 1
            label = int(pos_label[i])
            pos_subdata[label, pos_labelindex[label]] = data[i]
            pos_labelindex[label] = pos_labelindex[label] + 1

        subindex = subindex + 1
        percent = subindex / n * 100
        show_progressbar("Preprocessing", percent)

        f = h5py.File(newdata_dir + "Alpha/" + sub + ".h5", "w")
        f.create_dataset("ori", data=ori_subdata)
        f.create_dataset("pos", data=pos_subdata)
        f.close()
예제 #5
0
def ctsimilarities_cal(CTRDMs, Model_RDM, method='spearman'):

    """
    Calculate the Cross-Temporal Similarities between CTRDMs and a Coding Model RDM

    Parameters
    ----------
    CTRDMs : array
        The Cross-Temporal Representational Dissimilarity Matrices.
        The shape could be [n_ts, n_ts, n_conditions, n_conditions] or [n_subs, n_ts, n_ts, n_conditions, n_conditions]
        or [n_channels, n_ts, n_ts, n_conditions, n_conditionss] or [n_subs, n_channels, n_ts, n_ts, n_conditions,
        n_conditions]. n_ts, n_conditions, n_subs, n_channels represent the number of time-points, the number of
        conditions, the number of subjects and the number of channels, respectively.
    Model_RDM : array [n_conditions, n_conditions].
        The Coding Model RDM.
    method : string 'spearman' or 'pearson' or 'kendall' or 'similarity' or 'distance'. Default is 'spearman'.
        The method to calculate the similarities.
        If method='spearman', calculate the Spearman Correlations. If method='pearson', calculate the Pearson
        Correlations. If methd='kendall', calculate the Kendall tau Correlations. If method='similarity', calculate the
        Cosine Similarities. If method='distance', calculate the Euclidean Distances.

    Returns
    -------
    CTSimilarities : array
        Cross-temporal similarities.
        If method='spearman' or 'pearson' or 'kendall':
            If the shape of CTRDMs is [n_ts, n_ts, n_conditions, n_conditions], the shape of CTSimilarities will be
            [n_ts, n_ts, 2].
            If the shape of CTRDMs is [n_subs, n_ts, n_ts, n_conditions, n_conditions], the shape of CTSimilarities will
            be [n_subs, n_ts, n_ts, 2].
            If the shape of CTRDMs is [n_channels, n_ts, n_ts, n_conditions, n_conditions], the shape of CTSimilarities
            will be [n_channels, n_ts, n_ts, 2].
            If the shape of CTRDMs is [n_subs, n_channels, n_ts, n_ts, n_conditions, n_conditions], the shape of
            CTSimilarities will be [n_subs, n_channels, n_ts, n_ts, 2].
        If method='similarity' or 'distance':
            If the shape of CTRDMs is [n_ts, n_ts, n_conditions, n_conditions], the shape of CTSimilarities will be
            [n_ts, n_ts].
            If the shape of CTRDMs is [n_subs, n_ts, n_ts, n_conditions, n_conditions], the shape of CTSimilarities will
            be [n_subs, n_ts, n_ts].
            If the shape of CTRDMs is [n_channels, n_ts, n_ts, n_conditions, n_conditions], the shape of CTSimilarities
            will be [n_channels, n_ts, n_ts].
            If the shape of CTRDMs is [n_subs, n_channels, n_ts, n_ts, n_conditions, n_conditions], the shape of
            CTSimilarities will be [n_subs, n_channels, n_ts, n_ts].

    Notes
    -----
    Users can calculate CTRDMs by pyctrsa.ctrdm.single_cal module and pyctrsa.ctrdm.nulti_cal module
    (zitonglu1996.github.io/pyctrsa/)
    """

    n = len(np.shape(CTRDMs))

    if n == 4:

        n_ts, n_cons = np.shape(CTRDMs)[1:3]

        CTSimilarities = np.zeros([n_ts, n_ts, 2], dtype=np.float)

        total = n_ts * n_ts

        for t1 in range(n_ts):
            for t2 in range(n_ts):

                percent = (t1 * n_ts + t2) / total * 100
                show_progressbar("Calculating", percent)

                if method == 'spearman':
                    CTSimilarities[t1, t2] = spearmanrp.spearmanrp_cal(CTRDMs[t1, t2], Model_RDM)
                if method == 'pearson':
                    CTSimilarities[t1, t2] = pearsonrp.pearsonrp_cal(CTRDMs[t1, t2], Model_RDM)
                if method == 'kendall':
                    CTSimilarities[t1, t2] = kendallrp.kendallrp_cal(CTRDMs[t1, t2], Model_RDM)
                if method == 'similarity':
                    CTSimilarities[t1, t2, 0] = cosinesimilarity.cosinesimilarity_cal(CTRDMs[t1, t2], Model_RDM)
                if method == 'distance':
                    CTSimilarities[t1, t2, 0] = euclideandistance.euclideandistance_cal(CTRDMs[t1, t2], Model_RDM)

        if method == 'spearman' or method == 'pearson' or method == 'kendall':

            return CTSimilarities

        if method == 'similarity' or method == 'distance':

            return CTSimilarities[:, :, 0]

    if n == 5:

        n1 = np.shape(CTRDMs)[0]
        n_ts, n_cons = np.shape(CTRDMs)[2:4]

        CTSimilarities = np.zeros([n1, n_ts, n_ts, 2], dtype=np.float)

        total = n1 * n_ts * n_ts

        for i in range(n1):
            for t1 in range(n_ts):
                for t2 in range(n_ts):

                    percent = (i * n_ts * n_ts + t1 * n_ts + t2) / total * 100
                    show_progressbar("Calculating", percent)

                    if method == 'spearman':
                        CTSimilarities[i, t1, t2] = spearmanrp.spearmanrp_cal(CTRDMs[i, t1, t2], Model_RDM)
                        #print(CTSimilarities[i, t1, t2])
                    if method == 'pearson':
                        CTSimilarities[i, t1, t2] = pearsonrp.pearsonrp_cal(CTRDMs[i, t1, t2], Model_RDM)
                    if method == 'kendall':
                        CTSimilarities[i, t1, t2] = kendallrp.kendallrp_cal(CTRDMs[i, t1, t2], Model_RDM)
                    if method == 'similarity':
                        CTSimilarities[i, t1, t2, 0] = cosinesimilarity.cosinesimilarity_cal(CTRDMs[i, t1, t2], Model_RDM)
                    if method == 'distance':
                        CTSimilarities[i, t1, t2, 0] = euclideandistance.euclideandistance_cal(CTRDMs[i, t1, t2], Model_RDM)

        if method == 'spearman' or method == 'pearson' or method == 'kendall':
            return CTSimilarities

        if method == 'similarity' or method == 'distance':
            return CTSimilarities[:, :, :, 0]

    if n == 6:

        n1, n2 = np.shape(CTRDMs)[:2]
        n_ts, n_cons = np.shape(CTRDMs)[3:5]

        CTSimilarities = np.zeros([n1, n2, n_ts, n_ts, 2], dtype=np.float)

        total = n1 * n2 * n_ts * n_ts

        for i in range(n1):
            for j in range(n2):
                for t1 in range(n_ts):
                    for t2 in range(n_ts):

                        percent = (i * n2 * n_ts * n_ts + j * n_ts * n_ts + t1 * n_ts + t2) / total * 100
                        show_progressbar("Calculating", percent)

                        if method == 'spearman':
                            CTSimilarities[i, j, t1, t2] = spearmanrp.spearmanrp_cal(CTRDMs[i, j, t1, t2], Model_RDM)
                        if method == 'pearson':
                            CTSimilarities[i, j, t1, t2] = pearsonrp.pearsonrp_cal(CTRDMs[i, j, t1, t2], Model_RDM)
                        if method == 'kendall':
                            CTSimilarities[i, j, t1, t2] = kendallrp.kendallrp_cal(CTRDMs[i, j, t1, t2], Model_RDM)
                        if method == 'similarity':
                            CTSimilarities[i, j, t1, t2, 0] = cosinesimilarity.cosinesimilarity_cal(CTRDMs[i, j, t1, t2], Model_RDM)
                        if method == 'distance':
                            CTSimilarities[i, j, t1, t2, 0] = euclideandistance.euclideandistance_cal(CTRDMs[i, j, t1, t2], Model_RDM)

        if method == 'spearman' or method == 'pearson' or method == 'kendall':
            return CTSimilarities

        if method == 'similarity' or method == 'distance':
            return CTSimilarities[:, :, :, :, 0]

# test codes
#a = np.random.rand(100, 100, 6, 6)
#c = np.random.rand(6, 6)
#b = ctsimilarities_cal(a, c)
#print(b)