Exemple #1
0
    def test_rdm_correlation_spearman(self):

        rdm1 = np.random.rand(8, 8)
        rdm2 = np.random.rand(8, 8)
        rp = rdm_correlation_spearman(rdm1, rdm2, permutation=True)
        self.assertEqual(len(rp), 2)

        rp = rdm_correlation_spearman(rdm1, rdm2, permutation=False)
        self.assertEqual(len(rp), 2)

        rdm1 = np.random.rand(8, 7)
        rp = rdm_correlation_spearman(rdm1, rdm2)
        self.assertEqual(rp, "Invalid input!")
def rdms_corr(demo_rdm,
              eeg_rdms,
              method="spearman",
              fisherz=False,
              rescale=False,
              permutation=False,
              iter=5000):
    """
    Calculate the similarity between RDMs based on RDMs of EEG-like data and a demo RDM

    Parameters
    ----------
    demo_rdm : array [n_cons, n_cons]
        A demo RDM.
    eeg_rdms : array
        The EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDM(s).
        The shape can be [n_cons, n_cons] or [n1, n_cons, n_cons] or [n1, n2, n_cons, n_cons] or
        [n1, n2, n3, n_cons, n_cons]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs.
    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.
    fisherz : bool True or False. Default is False.
        Do the Fisher-Z transform of the RDMs or not.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    corrs : array
        The similarities between EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDMs and a demo RDM
        If the shape of eeg_rdms is [n_cons, n_cons], the shape of corrs will be [2]. If the shape of eeg_rdms is
        [n1, n_cons, n_cons], the shape of corrs will be [n1, 2]. If the shape of eeg_rdms is [n1, n2, n_cons, n_cons],
        the shape of corrs will be [n1, n2, 2]. If the shape of eeg_rdms is [n1, n2, n3, n_cons, n_cons], the shape of
        corrs will be [n1, n2, n3, 2]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs. 2 represents a r-value
        and a p-value.
    """

    if len(np.shape(demo_rdm)) != 2 or len(np.shape(eeg_rdms)) < 2 or len(np.shape(eeg_rdms)) > 5 or \
            np.shape(demo_rdm)[0] != np.shape(demo_rdm)[1] or np.shape(eeg_rdms)[-1] != np.shape(eeg_rdms)[-2]:

        return "Invalid input!"

    if len(eeg_rdms.shape) == 5:

        n1, n2, n3 = eeg_rdms.shape[:3]

        # initialize the corrs
        corrs = np.zeros([n1, n2, n3, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(n1):
            for j in range(n2):
                for k in range(n3):

                    if method == "spearman":
                        corrs[i, j, k] = rdm_correlation_spearman(
                            demo_rdm,
                            eeg_rdms[i, j, k],
                            rescale=rescale,
                            permutation=permutation,
                            iter=iter)
                    elif method == "pearson":
                        corrs[i, j, k] = rdm_correlation_pearson(
                            demo_rdm,
                            eeg_rdms[i, j, k],
                            rescale=rescale,
                            permutation=permutation,
                            iter=iter)
                    elif method == "kendall":
                        corrs[i, j, k] = rdm_correlation_kendall(
                            demo_rdm,
                            eeg_rdms[i, j, k],
                            rescale=rescale,
                            permutation=permutation,
                            iter=iter)
                    elif method == "similarity":
                        corrs[i, j, k, 0] = rdm_similarity(demo_rdm,
                                                           eeg_rdms[i, j, k],
                                                           rescale=rescale)
                    elif method == "distance":
                        corrs[i, j, k, 0] = rdm_distance(demo_rdm,
                                                         eeg_rdms[i, j, k],
                                                         rescale=rescale)

        return corrs

    if len(eeg_rdms.shape) == 4:

        n1, n2 = eeg_rdms.shape[:2]

        # initialize the corrs
        corrs = np.zeros([n1, n2, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(n1):
            for j in range(n2):

                if method == "spearman":
                    corrs[i, j] = rdm_correlation_spearman(
                        demo_rdm,
                        eeg_rdms[i, j],
                        rescale=rescale,
                        permutation=permutation,
                        iter=iter)
                elif method == "pearson":
                    corrs[i,
                          j] = rdm_correlation_pearson(demo_rdm,
                                                       eeg_rdms[i, j],
                                                       rescale=rescale,
                                                       permutation=permutation,
                                                       iter=iter)
                elif method == "kendall":
                    corrs[i,
                          j] = rdm_correlation_kendall(demo_rdm,
                                                       eeg_rdms[i, j],
                                                       rescale=rescale,
                                                       permutation=permutation,
                                                       iter=iter)
                elif method == "similarity":
                    corrs[i, j, 0] = rdm_similarity(demo_rdm,
                                                    eeg_rdms[i, j],
                                                    rescale=rescale)
                elif method == "distance":
                    corrs[i, j, 0] = rdm_distance(demo_rdm,
                                                  eeg_rdms[i, j],
                                                  rescale=rescale)

        return corrs

    if len(eeg_rdms.shape) == 3:

        n1 = eeg_rdms.shape[0]

        # initialize the corrs
        corrs = np.zeros([n1, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(n1):
            if method == "spearman":
                corrs[i] = rdm_correlation_spearman(demo_rdm,
                                                    eeg_rdms[i],
                                                    rescale=rescale,
                                                    permutation=permutation,
                                                    iter=iter)
            elif method == "pearson":
                corrs[i] = rdm_correlation_pearson(demo_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale,
                                                   permutation=permutation,
                                                   iter=iter)
            elif method == "kendall":
                corrs[i] = rdm_correlation_kendall(demo_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale,
                                                   permutation=permutation,
                                                   iter=iter)
            elif method == "similarity":
                corrs[i, 0] = rdm_similarity(demo_rdm,
                                             eeg_rdms[i],
                                             rescale=rescale)
            elif method == "distance":
                corrs[i, 0] = rdm_distance(demo_rdm,
                                           eeg_rdms[i],
                                           rescale=rescale)

        return corrs

    # initialize the corrs
    corr = np.zeros([2], dtype=np.float64)

    # calculate the corrs
    if method == "spearman":
        corr = rdm_correlation_spearman(demo_rdm,
                                        eeg_rdms,
                                        rescale=rescale,
                                        permutation=permutation,
                                        iter=iter)
    elif method == "pearson":
        corr = rdm_correlation_pearson(demo_rdm,
                                       eeg_rdms,
                                       rescale=rescale,
                                       permutation=permutation,
                                       iter=iter)
    elif method == "kendall":
        corr = rdm_correlation_kendall(demo_rdm,
                                       eeg_rdms,
                                       rescale=rescale,
                                       permutation=permutation,
                                       iter=iter)
    elif method == "similarity":
        corr[0] = rdm_similarity(demo_rdm, eeg_rdms, rescale=rescale)
    elif method == "distance":
        corr[0] = rdm_distance(demo_rdm, eeg_rdms, rescale=rescale)

    return corr
Exemple #3
0
def ctsimilarities_cal(RDMs, method='spearman', fisherz=True):
    """
    Calculate the Cross-Temporal Similarities based on normal RDMs

    Parameters
    ----------
    RDMs : array
        The Representational Dissimilarity Matrices in time series.
        The shape could be [n_ts, n_conditions, n_conditions] or [n_subs, n_ts, n_conditions, n_conditions] or
        [n_channels, n_ts, n_conditions, n_conditions] or [n_subs, n_channels, 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.
    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.
    fisherz : bool True or False. Default is False.
        Do the Fisher-Z transform of the CTRDMs or not.
        Only when method='spearman' or 'pearson' or 'kendall', it works.

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

    Notes
    -----
    Users can calculate RDMs by NeuroRA (zitonglu1996.github.io/neurora/)
    """

    n = len(np.shape(RDMs))

    if n == 3:

        n_ts, n_cons = np.shape(RDMs)[:2]

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

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

                if method == 'spearman':
                    CTSimilarities[t1, t2] = rdm_correlation_spearman(
                        RDMs[t1], RDMs[t2], fisherz=fisherz)
                if method == 'pearson':
                    CTSimilarities[t1, t2] = rdm_correlation_pearson(
                        RDMs[t1], RDMs[t2], fisherz=fisherz)
                if method == 'kendall':
                    CTSimilarities[t1, t2] = rdm_correlation_kendall(
                        RDMs[t1], RDMs[t2], fisherz=fisherz)
                if method == 'similarity':
                    CTSimilarities[t1, t2,
                                   0] = rdm_similarity(RDMs[t1], RDMs[t2])
                if method == 'distance':
                    CTSimilarities[t1, t2,
                                   0] = rdm_distance(RDMs[t1], RDMs[t2])

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

            return CTSimilarities

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

            return CTSimilarities[:, :, 0]

    if n == 4:

        n1, n_ts, n_cons = np.shape(RDMs)[:3]

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

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

                    if method == 'spearman':
                        CTSimilarities[i, t1, t2] = rdm_correlation_spearman(
                            RDMs[i, t1], RDMs[i, t2], fisherz=fisherz)
                    if method == 'pearson':
                        CTSimilarities[i, t1, t2] = rdm_correlation_pearson(
                            RDMs[i, t1], RDMs[i, t2], fisherz=fisherz)
                    if method == 'kendall':
                        CTSimilarities[i, t1, t2] = rdm_correlation_kendall(
                            RDMs[i, t1], RDMs[i, t2], fisherz=fisherz)
                    if method == 'similarity':
                        CTSimilarities[i, t1, t2, 0] = rdm_similarity(
                            RDMs[i, t1], RDMs[i, t2])
                    if method == 'distance':
                        CTSimilarities[i, t1, t2, 0] = rdm_distance(
                            RDMs[i, t1], RDMs[i, t2])

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

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

    if n == 5:

        n1, n2, n_ts, n_cons = np.shape(RDMs)[:4]

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

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

                        if method == 'spearman':
                            CTSimilarities[i, j, t1,
                                           t2] = rdm_correlation_spearman(
                                               RDMs[i, j, t1],
                                               RDMs[i, j, t2],
                                               fisherz=fisherz)
                        if method == 'pearson':
                            CTSimilarities[i, j, t1,
                                           t2] = rdm_correlation_pearson(
                                               RDMs[i, j, t1],
                                               RDMs[i, j, t2],
                                               fisherz=fisherz)
                        if method == 'kendall':
                            CTSimilarities[i, j, t1,
                                           t2] = rdm_correlation_kendall(
                                               RDMs[i, j, t1],
                                               RDMs[i, j, t2],
                                               fisherz=fisherz)
                        if method == 'similarity':
                            CTSimilarities[i, j, t1, t2, 0] = rdm_similarity(
                                RDMs[i, j, t1], RDMs[i, j, t2])
                        if method == 'distance':
                            CTSimilarities[i, j, t1, t2, 0] = rdm_distance(
                                RDMs[i, j, t1], RDMs[i, j, t2])

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

        if method == 'similarity' or method == 'distance':
            return CTSimilarities[:, :, :, :, 0]
def fmrirdms_corr(demo_rdm, fmri_rdms, method="spearman", rescale=False, permutation=False, iter=1000):


    """
    Calculate the similarity between fMRI searchlight RDMs and a demo RDM

    Parameters
    ----------
    demo_rdm : array [n_cons, n_cons]
        A demo RDM.
    fmri_rdms : array
        The fMRI-Searchlight RDMs.
        The shape of RDMs is [n_x, n_y, n_z, n_cons, n_cons]. n_x, n_y, n_z represent the number of calculation units
        for searchlight along the x, y, z axis.
    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.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 1000.
        The times for iteration.

    Returns
    -------
    corrs : array
        The similarities between fMRI searchlight RDMs and a demo RDM
        The shape of RDMs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units for searchlight
        along the x, y, z axis and 2 represents a r-value and a p-value.

    Notes
    -----
    The demo RDM could be a behavioral RDM, a hypothesis-based coding model RDM or a computational model RDM.
    """

    if len(np.shape(demo_rdm)) != 2 or np.shape(demo_rdm)[0] != np.shape(demo_rdm)[1]:

        print("\nThe shape of the demo RDM should be [n_cons, n_cons].\n")

        return "Invalid input!"

    if len(np.shape(fmri_rdms)) != 5 or np.shape(fmri_rdms)[3] != np.shape(fmri_rdms)[4]:

        print("\nThe shape of the fMRI RDMs should be [n_x, n_y, n_z, n_cons, n_cons].\n")

        return "Invalid input!"

    # calculate the number of the calculation units in the x, y, z directions
    n_x = np.shape(fmri_rdms)[0]
    n_y = np.shape(fmri_rdms)[1]
    n_z = np.shape(fmri_rdms)[2]

    print("\nComputing similarities")

    # initialize the corrs
    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    total = n_x * n_y * n_z

    # calculate the corrs
    for i in range(n_x):
        for j in range(n_y):
            for k in range(n_z):

                # show the progressbar
                percent = (i * n_y * n_z + j * n_z + k + 1) / total * 100
                show_progressbar("Calculating", percent)

                if method == "spearman":
                    corrs[i, j, k] = rdm_correlation_spearman(demo_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter)
                elif method == "pearson":
                    corrs[i, j, k] = rdm_correlation_pearson(demo_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter)
                elif method == "kendall":
                    corrs[i, j, k] = rdm_correlation_kendall(demo_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter)
                elif method == "similarity":
                    corrs[i, j, k, 0] = rdm_similarity(demo_rdm, fmri_rdms[i, j, k], rescale=rescale)
                elif method == "distance":
                    corrs[i, j, k, 0] = rdm_distance(demo_rdm, fmri_rdms[i, j, k], rescale=rescale)

    print("\nComputing finished!")

    return corrs
def rdms_corr(demo_rdm, eeg_rdms, method="spearman", rescale=False, permutation=False, iter=1000):

    """
    Calculate the similarity between RDMs based on RDMs of EEG-like data and a demo RDM

    Parameters
    ----------
    demo_rdm : array [n_cons, n_cons]
        A demo RDM.
    eeg_rdms : array
        The EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDM(s).
        The shape can be [n_cons, n_cons] or [n1, n_cons, n_cons] or [n1, n2, n_cons, n_cons] or
        [n1, n2, n3, n_cons, n_cons]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs.
    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.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 1000.
        The times for iteration.

    Returns
    -------
    corrs : array
        The similarities between EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDMs and a demo RDM
        If the shape of eeg_rdms is [n_cons, n_cons], the shape of corrs will be [2]. If the shape of eeg_rdms is
        [n1, n_cons, n_cons], the shape of corrs will be [n1, 2]. If the shape of eeg_rdms is [n1, n2, n_cons, n_cons],
        the shape of corrs will be [n1, n2, 2]. If the shape of eeg_rdms is [n1, n2, n3, n_cons, n_cons], the shape of
        corrs will be [n1, n2, n3, 2]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs. 2 represents a r-value
        and a p-value.

    Notes
    -----
    The demo RDM could be a behavioral RDM, a hypothesis-based coding model RDM or a computational model RDM.
    """

    if len(np.shape(demo_rdm)) != 2 or np.shape(demo_rdm)[0] != np.shape(demo_rdm)[1]:

        print("\nThe shape of the demo RDM should be [n_cons, n_cons].\n")

        return "Invalid input!"

    if len(np.shape(eeg_rdms)) < 2 or len(np.shape(eeg_rdms)) > 5 or np.shape(eeg_rdms)[-1] != np.shape(eeg_rdms)[-2]:

        print("\nThe shape of the EEG-like RDMs should be [n_cons, n_cons] or [n1, n_cons, n_cons] or "
              "[n1, n2, n_cons, n_cons] or [n1, n2, n3, n_cons, n_cons].\n")

        return "Invalid input!"

    if len(eeg_rdms.shape) == 5:

        print("\nComputing similarities")

        n1, n2, n3 = eeg_rdms.shape[:3]

        # initialize the corrs
        corrs = np.zeros([n1, n2, n3, 2], dtype=np.float64)

        total = n1 * n2 * n3

        # calculate the corrs
        for i in range(n1):
            for j in range(n2):
                for k in range(n3):

                    # show the progressbar
                    percent = (i * n2 * n2 + j * n3 + k + 1) / total * 100
                    show_progressbar("Calculating", percent)

                    if method == "spearman":
                        corrs[i, j, k] = rdm_correlation_spearman(demo_rdm, eeg_rdms[i, j, k], rescale=rescale,
                                                                  permutation=permutation, iter=iter)
                    elif method == "pearson":
                        corrs[i, j, k] = rdm_correlation_pearson(demo_rdm, eeg_rdms[i, j, k], rescale=rescale,
                                                                 permutation=permutation, iter=iter)
                    elif method == "kendall":
                        corrs[i, j, k] = rdm_correlation_kendall(demo_rdm, eeg_rdms[i, j, k], rescale=rescale,
                                                                 permutation=permutation, iter=iter)
                    elif method == "similarity":
                        corrs[i, j, k, 0] = rdm_similarity(demo_rdm, eeg_rdms[i, j, k], rescale=rescale)
                    elif method == "distance":
                        corrs[i, j, k, 0] = rdm_distance(demo_rdm, eeg_rdms[i, j, k], rescale=rescale)

        print("\nComputing finished!")

        return corrs

    if len(eeg_rdms.shape) == 4:

        print("\nComputing similarities")

        n1, n2 = eeg_rdms.shape[:2]

        # initialize the corrs
        corrs = np.zeros([n1, n2, 2], dtype=np.float64)

        total = n1 * n2

        # calculate the corrs
        for i in range(n1):
            for j in range(n2):

                if method == "spearman":
                    corrs[i, j] = rdm_correlation_spearman(demo_rdm, eeg_rdms[i, j], rescale=rescale,
                                                           permutation=permutation, iter=iter)
                elif method == "pearson":
                    corrs[i, j] = rdm_correlation_pearson(demo_rdm, eeg_rdms[i, j], rescale=rescale,
                                                          permutation=permutation, iter=iter)
                elif method == "kendall":
                    corrs[i, j] = rdm_correlation_kendall(demo_rdm, eeg_rdms[i, j], rescale=rescale,
                                                          permutation=permutation, iter=iter)
                elif method == "similarity":
                    corrs[i, j, 0] = rdm_similarity(demo_rdm, eeg_rdms[i, j], rescale=rescale)
                elif method == "distance":
                    corrs[i, j, 0] = rdm_distance(demo_rdm, eeg_rdms[i, j], rescale=rescale)

        print("\nComputing finished!")

        return corrs

    if len(eeg_rdms.shape) == 3:

        print("\nComputing similarities")

        n1 = eeg_rdms.shape[0]

        # initialize the corrs
        corrs = np.zeros([n1, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(n1):
            if method == "spearman":
                corrs[i] = rdm_correlation_spearman(demo_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation,
                                                    iter=iter)
            elif method == "pearson":
                corrs[i] = rdm_correlation_pearson(demo_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation,
                                                   iter=iter)
            elif method == "kendall":
                corrs[i] = rdm_correlation_kendall(demo_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation,
                                                   iter=iter)
            elif method == "similarity":
                corrs[i, 0] = rdm_similarity(demo_rdm, eeg_rdms[i], rescale=rescale)
            elif method == "distance":
                corrs[i, 0] = rdm_distance(demo_rdm, eeg_rdms[i], rescale=rescale)

        print("\nComputing finished!")

        return corrs

    print("\nComputing the similarity")

    # initialize the corrs
    corr = np.zeros([2], dtype=np.float64)

    # calculate the corrs
    if method == "spearman":
        corr = rdm_correlation_spearman(demo_rdm, eeg_rdms, rescale=rescale, permutation=permutation, iter=iter)
    elif method == "pearson":
        corr = rdm_correlation_pearson(demo_rdm, eeg_rdms, rescale=rescale, permutation=permutation, iter=iter)
    elif method == "kendall":
        corr = rdm_correlation_kendall(demo_rdm, eeg_rdms, rescale=rescale, permutation=permutation, iter=iter)
    elif method == "similarity":
        corr[0] = rdm_similarity(demo_rdm, eeg_rdms, rescale=rescale)
    elif method == "distance":
        corr[0] = rdm_distance(demo_rdm, eeg_rdms, rescale=rescale)

    print("\nComputing finished!")

    return corr
Exemple #6
0
def ctsim_drsa_cal(Model_RDMs, RDMs, method='spearman'):
    """
    Calculate the Cross-Temporal Similarities between temporal model RDMs and temporal neural RDMs (dynamic-RSA)

    Parameters
    ----------
    Model_RDMs : array
        The Coding Model RDMs.
        The shape should be [n_ts, n_cons, n_cons]. n_ts and n_cons represent the number of time-points and the number
        of conditions, respectively.
    RDMs : array
        The Representational Dissimilarity Matrices in time series.
        The shape could be [n_ts, n_cons, n_cons] or [n_subs, n_ts, n_cons, n_cons] or [n_channels, n_ts, n_cons,
        n_cons] or [n_subs, n_channels, n_ts, n_cons, n_cons]. n_ts, n_cons, n_subs, n_channels represent the number of
        time-points, the number of conditions, the number of subjects and the number of channels, respectively.
    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 the shape of RDMs is [n_ts, n_cons, n_cons] and method='spearman' or 'pearson' or 'kendall', the shape of
        CTSimilarities will be [n_ts, n_ts, 2].
        If the shape of RDMs is [n_subs, n_ts, n_cons, n_cons] and method='spearman' or 'pearson' or 'kendall', the
        shape of CTSimilarities will be [n_subs, n_ts, n_ts, 2].
        If the shape of RDMs is [n_chls, n_ts, n_cons, n_cons] and method='spearman' or 'pearson' or 'kendall', the
        shape of CTSimilarities will be [n_chls, n_ts, n_ts, 2].
        If the shape of RDMs is [n_subs, n_chls, n_ts, n_cons, n_cons] and method='spearman' or 'pearson' or 'kendall',
        the shape of CTSimilarities will be [n_subs, n_chls, n_ts, n_ts, 2].
        If the shape of RDMs is [n_ts, n_cons, n_cons] and method='similarity' or 'distance', the shape of
        CTSimilarities will be [n_ts, n_ts].
        If the shape of RDMs is [n_subs, n_ts, n_cons, n_cons] and method='similarity' or 'distance', the shape of
        CTSimilarities will be [n_subs, n_ts, n_ts].
        If the shape of RDMs is [n_chls, n_ts, n_cons, n_cons] and method='similarity' or 'distance', the shape of
        CTSimilarities will be [n_chls, n_ts, n_ts].
        If the shape of RDMs is [n_subs, n_chls, n_ts, n_cons, n_cons] and method='similarity' or 'distance', the shape
        of CTSimilarities will be [n_subs, n_chls, n_ts, n_ts].
    """

    n = len(np.shape(RDMs))

    if n == 3:

        n_ts, n_cons = np.shape(RDMs)[:2]

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

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

                if method == 'spearman':
                    CTSimilarities[t1, t2] = rdm_correlation_spearman(
                        RDMs[t1], Model_RDMs[t2])
                if method == 'pearson':
                    CTSimilarities[t1, t2] = rdm_correlation_pearson(
                        RDMs[t1], Model_RDMs[t2])
                if method == 'kendall':
                    CTSimilarities[t1, t2] = rdm_correlation_kendall(
                        RDMs[t1], Model_RDMs[t2])
                if method == 'similarity':
                    CTSimilarities[t1, t2,
                                   0] = rdm_similarity(RDMs[t1],
                                                       Model_RDMs[t2])
                if method == 'distance':
                    CTSimilarities[t1, t2,
                                   0] = rdm_distance(RDMs[t1], Model_RDMs[t2])

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

            return CTSimilarities

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

            return CTSimilarities[:, :, 0]

    if n == 4:

        n1, n_ts, n_cons = np.shape(RDMs)[:3]

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

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

                    if method == 'spearman':
                        CTSimilarities[i, t1, t2] = rdm_correlation_spearman(
                            RDMs[i, t1], Model_RDMs[i, t2])
                    if method == 'pearson':
                        CTSimilarities[i, t1, t2] = rdm_correlation_pearson(
                            RDMs[i, t1], Model_RDMs[i, t2])
                    if method == 'kendall':
                        CTSimilarities[i, t1, t2] = rdm_correlation_kendall(
                            RDMs[i, t1], Model_RDMs[i, t2])
                    if method == 'similarity':
                        CTSimilarities[i, t1, t2, 0] = rdm_similarity(
                            RDMs[i, t1], Model_RDMs[i, t2])
                    if method == 'distance':
                        CTSimilarities[i, t1, t2, 0] = rdm_distance(
                            RDMs[i, t1], Model_RDMs[i, t2])

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

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

    if n == 5:

        n1, n2, n_ts, n_cons = np.shape(RDMs)[:4]

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

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

                        if method == 'spearman':
                            CTSimilarities[i, j, t1,
                                           t2] = rdm_correlation_spearman(
                                               RDMs[i, j, t1], Model_RDMs[i, j,
                                                                          t2])
                        if method == 'pearson':
                            CTSimilarities[i, j, t1,
                                           t2] = rdm_correlation_pearson(
                                               RDMs[i, j, t1], Model_RDMs[i, j,
                                                                          t2])
                        if method == 'kendall':
                            CTSimilarities[i, j, t1,
                                           t2] = rdm_correlation_kendall(
                                               RDMs[i, j, t1], Model_RDMs[i, j,
                                                                          t2])
                        if method == 'similarity':
                            CTSimilarities[i, j, t1, t2, 0] = rdm_similarity(
                                RDMs[i, j, t1], Model_RDMs[i, j, t2])
                        if method == 'distance':
                            CTSimilarities[i, j, t1, t2, 0] = rdm_distance(
                                RDMs[i, j, t1], Model_RDMs[i, j, t2])

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

        if method == 'similarity' or method == 'distance':
            return CTSimilarities[:, :, :, :, 0]
Exemple #7
0
def eegANDfmri_corr(eeg_data, fmri_data, chl_opt=0, ksize=[3, 3, 3], strides=[1, 1, 1], sub_result=1, method="spearman", fisherz=False, rescale=False, permutation=False, iter=5000):

    """
    Calculate the Similarities between EEG/MEG/fNIRS data and fMRI data for searchligt

    Parameters
    ----------
    eeg_data : array
        The EEG/MEG/fNIRS data.
        The shape of EEGdata must be [n_cons, n_subs, n_trials, n_chls, n_ts].
        n_cons, n_subs, n_trials, n_chls & n_ts represent the number of conidtions, the number of subjects, the number
        of trials, the number of channels & the number of time-points, respectively.
    fmri_data : array
        The fmri data.
        The shape of fmri_data must be [n_cons, n_subs, nx, ny, nz]. n_cons, nx, ny, nz represent the number of
        conditions, the number of subs & the size of fMRI-img, respectively.
    chl_opt : int 0 or 1. Default is 0.
        Calculate the RDM & similarities for each channel or not.
        If chl_opt=0, calculating based on all channels' data.
        If chl_opt=1, calculating based on each channel's data respectively.
    ksize : array or list [kx, ky, kz]. Default is [3, 3, 3].
        The size of the calculation unit for searchlight.
        kx, ky, kz represent the number of voxels along the x, y, z axis.
    strides : array or list [sx, sy, sz]. Default is [1, 1, 1].
        The strides for calculating along the x, y, z axis.
    sub_result: int 0 or 1. Default is 1.
        Return the subject-result or average-result.
        If sub_result=0, return the average result.
        If sub_result=1, return the results of each subject.
    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.
    fisherz : bool True or False. Default is False.
        Do the Fisher-Z transform of the RDMs or not.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    corrs : array
        The similarities between EEG/MEG/fNIRS data and fMRI data for searchlight.
        If chl_opt=1 & sub_result=1, the shape of corrs is [n_subs, n_chls, n_x, n_y, n_z, 2]. n_subs, n_x, n_y, n_z
        represent the number of subjects, the number of calculation units for searchlight along the x, y, z axis and 2
        represents a r-value and a p-value.
        If chl_opt=1 & sub_result=0, the shape of corrs is [n_chls, n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the
        number of calculation units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value.
        If chl_opt=0 & sub_result=1, the shape of RDMs is [n_subs, n_x, n_y, n_z, 2]. n_subs, n_x, n_y, n_z represent
        the number of subjects, the number of calculation units for searchlight along the x, y, z axis and 2 represents
        a r-value and a p-value.
        If chl_opt=0 & sub_result=0, the shape of corrs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of
        calculation units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value.
    """

    if len(np.shape(eeg_data)) != 5 or len(np.shape(fmri_data)) != 5:

        return "Invalid input!"

    # get the number of subjects
    subs = np.shape(fmri_data)[1]

    # get the size of the fMRI-img
    nx = np.shape(fmri_data)[2]
    ny = np.shape(fmri_data)[3]
    nz = np.shape(fmri_data)[4]

    # the size of the calculation units for searchlight
    kx = ksize[0]
    ky = ksize[1]
    kz = ksize[2]

    # strides for calculating along the x, y, z axis
    sx = strides[0]
    sy = strides[1]
    sz = strides[0]

    # calculate the number of the calculation units in the x, y, z directions
    n_x = int((nx - kx) / sx) + 1
    n_y = int((ny - ky) / sy) + 1
    n_z = int((nz - kz) / sz) + 1

    # get the number of channels
    chls = np.shape(eeg_data)[3]

    # calculate the fmri_rdms for searchlight
    fmri_rdms = fmriRDM(fmri_data, sub_result=sub_result, ksize=ksize, strides=strides)

    print(fmri_rdms.shape)

    # calculate the eeg_rdms
    eeg_rdms = eegRDM(eeg_data, sub_opt=sub_result, chl_opt=chl_opt)

    # chl_opt=1

    if chl_opt == 1:

        # sub_result=1
        if sub_result == 1:

            # chl_opt=1 & sub_result=1

            # initialize the corrs
            corrs = np.full([subs, chls, n_x, n_y, n_z, 2], np.nan)

            # calculate the corrs
            for sub in range(subs):
                for j in range(n_x):
                    for k in range(n_y):
                        for l in range(n_z):
                            for i in range(chls):

                                if method == "spearman":
                                    corrs[sub, i, j, k, l] = rdm_correlation_spearman(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], fisherz=fisherz,
                                                                                 rescale=rescale, permutation=permutation, iter=iter)
                                elif method == "pearson":
                                    corrs[sub, i, j, k, l] = rdm_correlation_pearson(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], fisherz=fisherz,
                                                                                rescale=rescale, permutation=permutation, iter=iter)
                                elif method == "kendall":
                                    corrs[sub, i, j, k, l] = rdm_correlation_kendall(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], fisherz=fisherz,
                                                                                rescale=rescale, permutation=permutation, iter=iter)
                                elif method == "similarity":
                                    corrs[sub, i, j, k, l, 0] = rdm_similarity(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l],
                                                                          rescale=rescale)
                                elif method == "distance":
                                    corrs[sub, i, j, k, l, 0] = rdm_distance(eeg_rdms[sub, i], fmri_rdms[sub, i, j, k],
                                                                        rescale=rescale)

            return np.abs(corrs)

        # sub_result=0

        # chl_opt=1 & sub_result=0

        # initialize the corrs
        corrs = np.full([chls, n_x, n_y, n_z, 2], np.nan)

        # calculate the corrs
        for j in range(n_x):
            for k in range(n_y):
                for l in range(n_z):
                    for i in range(chls):

                        if method == "spearman":
                            corrs[i, j, k, l] = rdm_correlation_spearman(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "pearson":
                            corrs[i, j, k, l] = rdm_correlation_pearson(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "kendall":
                            corrs[i, j, k, l] = rdm_correlation_kendall(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "similarity":
                            corrs[i, j, k, l, 0] = rdm_similarity(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale)
                        elif method == "distance":
                            corrs[i, j, k, l, 0] = rdm_distance(eeg_rdms[i], fmri_rdms[i, j, k], rescale=rescale)

        return np.abs(corrs)

    # chl_opt=0

    # sub_result=1

    if sub_result == 1:

        # chl_opt=0 & sub_result=1

        # initialize the corrs
        corrs = np.full([subs, n_x, n_y, n_z, 2], np.nan)

        # calculate the corrs
        for i in range(subs):
            for j in range(n_x):
                for k in range(n_y):
                    for l in range(n_z):

                        if method == "spearman":
                            corrs[i, j, k, l] = rdm_correlation_spearman(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz,
                                                                         rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "pearson":
                            corrs[i, j, k, l] = rdm_correlation_pearson(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz,
                                                                        rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "kendall":
                            corrs[i, j, k, l] = rdm_correlation_kendall(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz,
                                                                        rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "similarity":
                            corrs[i, j, k, l, 0] = rdm_similarity(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale)
                        elif method == "distance":
                            corrs[i, j, k, l, 0] = rdm_distance(eeg_rdms[i], fmri_rdms[i, j, k], rescale=rescale)

        return corrs

    # sub_result=0

    # chl_opt=0 & sub_result=0

    # initialize the corrs
    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    # calculate the corrs
    for i in range(n_x):
        for j in range(n_y):
            for k in range(n_z):

                if method == "spearman":
                    corrs[i, j, k] = rdm_correlation_spearman(eeg_rdms, fmri_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                elif method == "pearson":
                    corrs[i, j, k] = rdm_correlation_pearson(eeg_rdms, fmri_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                elif method == "kendall":
                    corrs[i, j, k] = rdm_correlation_kendall(eeg_rdms, fmri_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                elif method == "similarity":
                    corrs[i, j, k, 0] = rdm_similarity(eeg_rdms, fmri_rdms[i, j, k], rescale=rescale)
                elif method == "distance":
                    corrs[i, j, k, 0] = rdm_distance(eeg_rdms, fmri_rdms[i, j, k], rescale=rescale)

    return np.abs(corrs)
Exemple #8
0
def bhvANDeeg_corr(bhv_data, eeg_data, sub_opt=1, chl_opt=0, time_opt=0, time_win=5, time_step=5, method="spearman", fisherz=False, rescale=False, permutation=False, iter=5000):

    """
    Calculate the Similarities between behavioral data and EEG/MEG/fNIRS data

    Parameters
    ----------
    bhv_data : array
        The behavioral data.
        The shape of bhv_data must be [n_cons, n_subs, n_trials].
        n_cons, n_subs & n_trials represent the number of conidtions, the number of subjects & the number of trials,
        respectively.
    eeg_data : array
        The EEG/MEG/fNIRS data.
        The shape of EEGdata must be [n_cons, n_subs, n_trials, n_chls, n_ts].
        n_cons, n_subs, n_trials, n_chls & n_ts represent the number of conidtions, the number of subjects, the number
        of trials, the number of channels & the number of time-points, respectively.
    sub_opt : int 0 or 1. Default is 0.
        Calculate the RDM & similarities for each subject or not.
        If sub_opt=0, calculating based on all data.
        If sub_opt=1, calculating based on each subject's data, respectively.
    chl_opt : int 0 or 1. Default is 0.
        Calculate the RDM & similarities for each channel or not.
        If chl_opt=0, calculating based on all channels' data.
        If chl_opt=1, calculating based on each channel's data respectively.
    time_opt : int 0 or 1. Default is 0.
        Calculate the RDM & similarities for each time-point or not
        If time_opt=0, calculating based on whole time-points' data.
        If time_opt=1, calculating based on each time-points respectively.
    time_win : int. Default is 5.
        Set a time-window for calculating the RDM & similarities for different time-points.
        Only when time_opt=1, time_win works.
        If time_win=5, that means each calculation process based on 5 time-points.
    time_step : int. Default is 5.
        The time step size for each time of calculating.
        Only when time_opt=1, time_step works.
    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.
    fisherz : bool True or False. Default is False.
        Do the Fisher-Z transform of the RDMs or not.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the
        values on the diagonal.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    corrs : array
        The similarities between behavioral data and EEG/MEG/fNIRS data.
        If sub_opt=0 & chl_opt=0 & time_opt=0, return one corr result.
            The shape of corrs is [2], a r-value and a p-value. If method='similarity' or method='distance', the
            p-value is 0.
        If sub_opt=0 & chl_opt=0 & time_opt=1, return int((n_ts-time_win)/time_step)+1 corrs result.
            The shape of corrs is [int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value. If
            method='similarity' or method='distance', the p-values are all 0.
        If sub_opt=0 & chl_opt=1 & time_opt=0, return n_chls corrs result.
            The shape of corrs is [n_chls, 2]. 2 represents a r-value and a p-value. If method='similarity' or
            method='distance', the p-values are all 0.
        If sub_opt=0 & chl_opt=1 & time_opt=1, return n_chls*(int((n_ts-time_win)/time_step)+1) corrs result.
            The shape of corrs is [n_chls, int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value.
            If method='similarity' or method='distance', the p-values are all 0.
        If sub_opt=1 & chl_opt=0 & time_opt=0, return n_subs corr result.
            The shape of corrs is [n_subs, 2], a r-value and a p-value. If method='similarity' or method='distance',
            the p-values are all 0.
        If sub_opt=1 & chl_opt=0 & time_opt=1, return n_subs*(int((n_ts-time_win)/time_step)+1) corrs result.
            The shape of corrs is [n_subs, int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value.
            If method='similarity' or method='distance', the p-values are all 0.
        If sub_opt=1 & chl_opt=1 & time_opt=0, return n_subs*n_chls corrs result.
            The shape of corrs is [n_subs, n_chls, 2]. 2 represents a r-value and a p-value. If method='similarity' or
            method='distance', the p-values are all 0.
        If sub_opt=1 & chl_opt=1 & time_opt=1, return n_subs*n_chls*(int((n_ts-time_win)/time_step)+1) corrs result.
            The shape of corrs is [n_subs, n_chls, int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a
            p-value. If method='similarity' or method='distance', the p-values are all 0.
    """

    if len(np.shape(bhv_data)) != 3 or len(np.shape(eeg_data)) != 5:

        return "Invalid input!"

    # get the number of subjects
    subs = np.shape(bhv_data)[1]
    # get the number of channels
    chls = np.shape(eeg_data)[3]
    # get the number of time-points for calculating
    ts = np.shape(eeg_data)[4]
    ts = int((ts-time_win)/time_step)+1

    if sub_opt == 1:

        # calculate the bhv_rdm
        bhv_rdms = bhvRDM(bhv_data, sub_opt=sub_opt)

        if chl_opt == 0:

            if time_opt == 0:

                # sub_opt=1 & chl_opt=0 & time_opt=0

                # calculate the eeg_rdms
                eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt)

                # initialize the corrs
                corrs = np.zeros([subs, 2], dtype=np.float64)

                # calculate the corrs
                for i in range(subs):

                    if method == "spearman":
                        corrs[i] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "pearson":
                        corrs[i] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "kendall":
                        corrs[i] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "similarity":
                        corrs[i, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i], rescale=rescale)
                    elif method == "distance":
                        corrs[i, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i], rescale=rescale)

                return corrs

            # sub_opt=1 & chl_opt=0 & time_opt=1

            # calculate the eeg_rdms
            eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step)

            # initialize the corrs
            corrs = np.zeros([subs, ts, 2], dtype=np.float64)

            # calculate the corrs
            for i in range(subs):
                for j in range(ts):

                    if method == "spearman":
                        corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "pearson":
                        corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "kendall":
                        corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "similarity":
                        corrs[i, j, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale)
                    elif method == "distance":
                        corrs[i, j, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale)

            return corrs

        if time_opt == 1:

            #  sub_opt=1 & chl_opt=1 & time_opt=1

            # calculate the eeg_rdms
            eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step)

            # initialize the corrs
            corrs = np.zeros([subs, chls, ts, 2], dtype=np.float64)

            # calculate the corrs
            for i in range(subs):
                for j in range(chls):
                    for k in range(ts):

                        if method == "spearman":
                            corrs[i, j, k] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "pearson":
                            corrs[i, j, k] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "kendall":
                            corrs[i, j, k] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                        elif method == "similarity":
                            corrs[i, j, k, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i, j, k], rescale=rescale)
                        elif method == "distance":
                            corrs[i, j, k, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i, j, k], rescale=rescale)

            return corrs


        # sub_opt=1 & chl_opt=1 & time_opt=0

        eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt)

        corrs = np.zeros([subs, chls, 2], dtype=np.float64)

        for i in range(subs):
            for j in range(chls):

                if method == "spearman":
                    corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                elif method == "pearson":
                    corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                elif method == "kendall":
                    corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                elif method == "similarity":
                    corrs[i, j, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale)
                elif method == "distance":
                    corrs[i, j, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale)

        return corrs

    # if sub_opt=0

    bhv_rdm = bhvRDM(bhv_data, sub_opt=sub_opt)

    if chl_opt == 1:

        if time_opt == 1:

            # sub_opt = 0 & chl_opt = 1 & time_opt = 1

            # calculate the eeg_rdms
            eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step)

            # initialize the corrs
            corrs = np.zeros([chls, ts, 2], dtype=np.float64)

            # calculate the corrs
            for i in range(chls):
                for j in range(ts):

                    if method == "spearman":
                        corrs[i, j] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "pearson":
                        corrs[i, j] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "kendall":
                        corrs[i, j] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "similarity":
                        corrs[i, j, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i, j], rescale=rescale)
                    elif method == "distance":
                        corrs[i, j, 0] = rdm_distance(bhv_rdm, eeg_rdms[i, j], rescale=rescale)

            return corrs

        # sub_opt=0 & chl_opt=1 & time_opt=0

        # calculate the eeg_rdms
        eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt)

        # initialize the corrs
        corrs = np.zeros([chls, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(chls):

            if method == "spearman":
                corrs[i] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
            elif method == "pearson":
                corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
            elif method == "kendall":
                corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
            elif method == "similarity":
                corrs[i, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i], rescale=rescale)
            elif method == "distance":
                corrs[i, 0] = rdm_distance(bhv_rdm, eeg_rdms[i], rescale=rescale)

        return corrs

    # if chl_opt=0

    if time_opt == 1:

        # sub_opt=0 & chl_opt=0 & time_opt=1

        # calculate the eeg_rdms
        eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step)

        # initialize the corrs
        corrs = np.zeros([ts, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(ts):

            if method == "spearman":
                corrs[i] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
            elif method == "pearson":
                corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
            elif method == "kendall":
                corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
            elif method == "similarity":
                corrs[i, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i], rescale=rescale)
            elif method == "distance":
                corrs[i, 0] = rdm_distance(bhv_rdm, eeg_rdms[i], rescale=rescale)

        return corrs

    # sub_opt=0 & chl_opt=0 & time_opt=0

    # calculate the eeg_rdms
    eeg_rdm = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt)

    # initialize the corrs
    corr = np.zeros([2], dtype=np.float64)

    # calculate the corrs
    if method == "spearson":
        corr = rdm_correlation_spearman(bhv_rdm, eeg_rdm, fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
    elif method == "pearson":
        corr = rdm_correlation_pearson(bhv_rdm, eeg_rdm, fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
    elif method == "kendall":
        corr = rdm_correlation_kendall(bhv_rdm, eeg_rdm, fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
    elif method == "similarity":
        corr[0] = rdm_similarity(bhv_rdm, eeg_rdm, rescale=rescale)
    elif method == "distance":
        corr[0] = rdm_distance(bhv_rdm, eeg_rdm, rescale=rescale)

    return corr
def bhvANDecog_corr(bhv_data,
                    ele_data,
                    time_win=5,
                    time_step=5,
                    ecog_opt="allin",
                    method="spearman",
                    rescale=False):
    """
    Calculate the Similarities between behavioral data and sEEG/ECoG/eletricophysiological data

    Parameters
    ----------
    bhv_data : array
        The behavioral data.
        The shape of bhv_data must be [n_cons, n_trials].
        n_cons, n_subs & n_trials represent the number of conidtions, the number of subjects & the number of trials,
        respectively.
    ele_data : array
        The ECoG/sEEG/electrophysiology data.
        The shape of EEGdata must be [n_cons, n_trials, n_chls, n_ts].
        n_cons, n_trials, n_chls & n_ts represent the number of conidtions, the number of trials,
        the number of channels & the number of time-points, respectively.
    time_win : int. Default is 5.
        Set a time-window for calculating the RDM & similarities for different time-points.
        Only when time_opt=1, time_win works.
        If time_win=5, that means each calculation process based on 5 time-points.
    time_step : int. Default is 5.
        The time step size for each time of calculating.
        Only when time_opt=1, time_step works.
    ecog_opt : string 'channel', 'time' or 'all'. Default is 'all'.
        Calculate the RDM for each channel or for each time-point or for the whole data.
        If ecog_opt='channel', return n_chls RDMs based on each channel's data.
        If ecog_opt='time', return int((n_ts-time_win)/time_step)+1 RDMs based on each time-point's data respectively.
        If ecog_opt='allin', return only one RDM based on all data.
    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.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the
        values on the diagonal.

    Returns
    -------
    corrs : array
        The similarities between behavioral data and ECoG/sEEG/electrophysiology.
        If opt='channel', return n_chls corrs result.
            The shape of corrs is [n_chls, 2]. 2 represents a r-value and a p-value. If method='similarity' or
            method='distance', the p-values are all 0.
        If opt='time', return int(n_ts/time_win) corrs result.
            The shape of corrs is [int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value. If
            method='similarity' or method='distance', the p-values are all 0.
        If opt='all', return one corr result.
            The shape of corrs is [2], a r-value and a p-value. If method='similarity' or method='distance',
            the p-value is 0.
    """

    # sub_opt = 1, bhv_data here belongs to one subject, and its shape must be : [cons, trials]

    # get the number of conditions & the number of trials
    cons, trials = np.shape(bhv_data)

    # shape of bhv_data: [cons, trials] -> [cons, subs, trials] & subs=1
    bhv_data = np.reshape(bhv_data, [cons, 1, trials])

    # calculate bhv_rdm
    bhv_rdm = bhvRDM(bhv_data)

    # ecog_opt='channel'

    if ecog_opt == "channel":

        # calculate the ecog_rdms
        ecog_rdms = ecogRDM(ele_data, opt="channel")

        # get the number of channels
        chls_num = np.shape(ele_data)[2]

        # initialize the corrs
        corrs = np.zeros([chls_num, 2], dtype=np.float64)

        # get the corrs
        for i in range(chls_num):

            if method == "spearman":
                corrs[i] = rdm_correlation_spearman(bhv_rdm,
                                                    ecog_rdms[i],
                                                    rescale=rescale)
            elif method == "pearson":
                corrs[i] = rdm_correlation_pearson(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)
            elif method == "kendall":
                corrs[i] = rdm_correlation_kendall(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)
            elif method == "similarity":
                corrs[i, 0] = rdm_similarity(bhv_rdm,
                                             ecog_rdms[i],
                                             rescale=rescale)
            elif method == "distance":
                corrs[i, 0] = rdm_distance(bhv_rdm,
                                           ecog_rdms[i],
                                           rescale=rescale)

        return corrs

    # ecog_opt='time'

    elif ecog_opt == "time":

        # calculate the ecog_rdms
        ecog_rdms = ecogRDM(ele_data,
                            time_win=time_win,
                            time_step=time_step,
                            opt="time")

        # get the number of time-points for calculating
        ts = np.shape(ele_data)[3]
        ts = int((ts - time_win) / time_step) + 1

        # initialize the corrs
        corrs = np.zeros([ts, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(ts):

            if method == "spearman":
                corrs[i] = rdm_correlation_spearman(bhv_rdm,
                                                    ecog_rdms[i],
                                                    rescale=rescale)
            elif method == "pearson":
                corrs[i] = rdm_correlation_pearson(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)
            elif method == "kendall":
                corrs[i] = rdm_correlation_kendall(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)
            elif method == "similarity":
                corrs[i, 0] = rdm_similarity(bhv_rdm,
                                             ecog_rdms[i],
                                             rescale=rescale)
            elif method == "distance":
                corrs[i, 0] = rdm_distance(bhv_rdm,
                                           ecog_rdms[i],
                                           rescale=rescale)

        return corrs

    # ecog_opt="allin"

    # calculate the ecog_rdm
    ecog_rdm = ecogRDM(ele_data, opt="allin")

    # initialize the corr
    corr = np.zeros([2], dtype=np.float64)

    # calculate the corr
    if method == "spearman":
        corr = rdm_correlation_spearman(bhv_rdm, ecog_rdm, rescale=rescale)
    elif method == "pearson":
        corr = rdm_correlation_pearson(bhv_rdm, ecog_rdm, rescale=rescale)
    elif method == "kendall":
        corr = rdm_correlation_kendall(bhv_rdm, ecog_rdm, rescale=rescale)
    elif method == "similarity":
        corr[0] = rdm_similarity(bhv_rdm, ecog_rdm, rescale=rescale)
    elif method == "distance":
        corr[0] = rdm_distance(bhv_rdm, ecog_rdm, rescale=rescale)

    return corr
Exemple #10
0
def eegANDfmri_corr(eeg_data,
                    fmri_data,
                    chl_opt=0,
                    ksize=[3, 3, 3],
                    strides=[1, 1, 1],
                    method="spearman",
                    rescale=False):
    # sub_opt=0, time_opt=0

    nx = np.shape(fmri_data)[2]
    ny = np.shape(fmri_data)[3]
    nz = np.shape(fmri_data)[4]

    cons = np.shape(eeg_data)[0]

    kx = ksize[0]
    ky = ksize[1]
    kz = ksize[2]

    sx = strides[0]
    sy = strides[1]
    sz = strides[0]

    n_x = int((nx - kx) / sx) + 1
    n_y = int((ny - ky) / sy) + 1
    n_z = int((nz - kz) / sz) + 1

    fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides)

    if chl_opt == 1:

        chls = np.shape(eeg_data)[3]

        eeg_rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=1, time_opt=0)

        corrs = np.full([chls, n_x, n_y, n_z, 2], np.nan)

        for j in range(n_x):

            for k in range(n_y):

                for l in range(n_z):
                    """index = 0

                    for m in range(cons):

                        for n in range(cons):

                            if math.isnan(fmri_rdms[j, k, l]) == True:

                                index = index + 1"""

                    for i in range(chls):
                        """if index != 0:

                            corrs[i, j, k, l, 0] = 0
                            corrs[i, j, k, l, 1] = 1"""

                        if method == "spearman":

                            corrs[i, j, k, l] = rdm_correlation_spearman(
                                eeg_rdms[i],
                                fmri_rdms[j, k, l],
                                rescale=rescale)

                        elif method == "pearson":

                            corrs[i, j, k,
                                  l] = rdm_correlation_pearson(eeg_rdms[i],
                                                               fmri_rdms[j, k,
                                                                         l],
                                                               rescale=rescale)

                        elif method == "kendall":

                            corrs[i, j, k,
                                  l] = rdm_correlation_kendall(eeg_rdms[i],
                                                               fmri_rdms[j, k,
                                                                         l],
                                                               rescale=rescale)

                        elif method == "similarity":

                            corrs[i, j, k, l,
                                  0] = rdm_similarity(eeg_rdms[i],
                                                      fmri_rdms[j, k, l],
                                                      rescale=rescale)

                        elif method == "distance":

                            corrs[i, j, k, l,
                                  0] = rdm_distance(eeg_rdms[i],
                                                    fmri_rdms[i, j, k],
                                                    rescale=rescale)

        return np.abs(corrs)

    # if chl_opt=0

    eeg_rdm = eegRDM(eeg_data, sub_opt=0, chl_opt=0, time_opt=0)

    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    for i in range(n_x):

        for j in range(n_y):

            for k in range(n_z):
                """index = 0

                for m in range(cons):

                    for n in range(cons):

                        if math.isnan(fmri_rdms[i, j, k]) == True:

                            index = index + 1

                if index != 0:

                    corrs[i, j, k, 0] = 0
                    corrs[i, j, k, 1] = 1"""

                if method == "spearman":

                    corrs[i, j, k] = rdm_correlation_spearman(eeg_rdm,
                                                              fmri_rdms[i, j,
                                                                        k],
                                                              rescale=rescale)

                elif method == "pearson":

                    corrs[i, j, k] = rdm_correlation_pearson(eeg_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)

                elif method == "kendall":

                    corrs[i, j, k] = rdm_correlation_kendall(eeg_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)

                elif method == "similarity":

                    corrs[i, j, k, 0] = rdm_similarity(eeg_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale)

                elif method == "distance":

                    corrs[i, j, k, 0] = rdm_distance(eeg_rdm,
                                                     fmri_rdms[i, j, k],
                                                     rescale=rescale)

    return np.abs(corrs)
Exemple #11
0
def bhvANDfmri_corr(bhv_data,
                    fmri_data,
                    bhv_data_opt=1,
                    ksize=[3, 3, 3],
                    strides=[1, 1, 1],
                    method="spearman",
                    rescale=False):
    # sub_opt=1

    if bhv_data_opt == 0:

        bhv_rdm = bhvRDM(bhv_data, sub_opt=0, data_opt=0)

    # if bhv_data_opt=1

    else:

        bhv_rdm = bhvRDM(bhv_data, sub_opt=0, data_opt=1)

    print("****************")

    print("get behavior RDM")

    print(bhv_rdm)

    fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides)

    print("****************")

    print("get fMRI RDM")

    print(np.shape(fmri_rdms))

    cons = np.shape(bhv_data)[0]

    nx = np.shape(fmri_data)[2]
    ny = np.shape(fmri_data)[3]
    nz = np.shape(fmri_data)[4]

    kx = ksize[0]
    ky = ksize[1]
    kz = ksize[2]

    sx = strides[0]
    sy = strides[1]
    sz = strides[2]

    n_x = int((nx - kx) / sx) + 1
    n_y = int((ny - ky) / sy) + 1
    n_z = int((nz - kz) / sz) + 1

    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    for i in range(n_x):

        for j in range(n_y):

            for k in range(n_z):
                """index = 0

                for m in range(cons):

                    for n in range(cons):

                         if math.isnan(fmri_rdms[i, j, k, m, n]) == True:

                             index = index + 1

                if index != 0:

                    corrs[i, j, k, 0] = 0
                    corrs[i, j, k, 1] = 1"""

                if method == "spearman":

                    corrs[i, j, k] = rdm_correlation_spearman(bhv_rdm,
                                                              fmri_rdms[i, j,
                                                                        k],
                                                              rescale=rescale)

                elif method == "pearson":

                    corrs[i, j, k] = rdm_correlation_pearson(bhv_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)

                elif method == "kendall":

                    corrs[i, j, k] = rdm_correlation_kendall(bhv_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)

                elif method == "similarity":

                    corrs[i, j, k, 0] = rdm_similarity(bhv_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale)

                elif method == "distance":

                    corrs[i, j, k, 0] = rdm_distance(bhv_rdm,
                                                     fmri_rdms[i, j, k],
                                                     rescale=rescale)

                print(corrs[i, j, k])

    return corrs
Exemple #12
0
def bhvANDecog_corr(bhv_data,
                    ele_data,
                    time_win=5,
                    ecog_opt="allin",
                    method="spearman",
                    rescale=False):

    # sub_opt = 1, bhv_data here belongs to one subject, and its shape must be : [cons, trials]

    cons, trials = np.shape(bhv_data)
    ts = np.shape(ele_data)[3]

    bhv_data = np.reshape(bhv_data, [cons, 1, trials])

    bhv_rdm = np.reshape(bhvRDM(bhv_data, sub_opt=1, data_opt=1), [cons, cons])

    if ecog_opt == "channel":

        ecog_rdms = ecogRDM(ele_data, opt="channel")

        chls_num = np.shape(ele_data)[2]

        corrs = np.zeros([chls_num, 2], dtype=np.float64)

        for i in range(chls_num):

            if method == "spearman":

                corrs[i] = rdm_correlation_spearman(bhv_rdm,
                                                    ecog_rdms[i],
                                                    rescale=rescale)

            elif method == "pearson":

                corrs[i] = rdm_correlation_pearson(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)

            elif method == "kendall":

                corrs[i] = rdm_correlation_kendall(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)

            elif method == "similarity":

                corrs[i, 0] = rdm_similarity(bhv_rdm,
                                             ecog_rdms[i],
                                             rescale=rescale)

            elif method == "distance":

                corrs[i, 0] = rdm_distance(bhv_rdm,
                                           ecog_rdms[i],
                                           rescale=rescale)

        return corrs

    elif ecog_opt == "time":

        ecog_rdms = ecogRDM(ele_data, time_win=5, opt="time")

        ts = int(np.shape(ele_data)[3] / time_win)

        corrs = np.zeros([ts, 2], dtype=np.float64)

        for i in range(ts):

            if method == "spearman":

                corrs[i] = rdm_correlation_spearman(bhv_rdm,
                                                    ecog_rdms[i],
                                                    rescale=rescale)

            elif method == "pearson":

                corrs[i] = rdm_correlation_pearson(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)

            elif method == "kendall":

                corrs[i] = rdm_correlation_kendall(bhv_rdm,
                                                   ecog_rdms[i],
                                                   rescale=rescale)

            elif method == "similarity":

                corrs[i, 0] = rdm_similarity(bhv_rdm,
                                             ecog_rdms[i],
                                             rescale=rescale)

            elif method == "distance":

                corrs[i, 0] = rdm_distance(bhv_rdm,
                                           ecog_rdms[i],
                                           rescale=rescale)

        return corrs

    # if ecog_opt="allin"

    ecog_rdm = ecogRDM(ele_data, opt="allin")

    corr = np.zeros([2], dtype=np.float64)

    if method == "spearman":

        corr = rdm_correlation_spearman(bhv_rdm, ecog_rdm, rescale=rescale)

    elif method == "pearson":

        corr = rdm_correlation_pearson(bhv_rdm, ecog_rdm, rescale=rescale)

    elif method == "kendall":

        corr = rdm_correlation_kendall(bhv_rdm, ecog_rdm, rescale=rescale)

    elif method == "similarity":

        corr[0] = rdm_similarity(bhv_rdm, ecog_rdm, rescale=rescale)

    elif method == "distance":

        corr[0] = rdm_distance(bhv_rdm, ecog_rdm, rescale=rescale)

    return corr
Exemple #13
0
def bhvANDeeg_corr(bhv_data,
                   EEG_data,
                   sub_opt=0,
                   bhv_data_opt=1,
                   time_win=5,
                   chl_opt=0,
                   time_opt=0,
                   method="spearman",
                   rescale=False):

    subs = np.shape(bhv_data)[1]
    chls = np.shape(EEG_data)[3]
    ts = int(np.shape(EEG_data)[4] / time_win)

    if sub_opt == 1:

        if bhv_data_opt == 0:

            return None

        # if bhv_data_opt=1

        bhv_rdms = bhvRDM(bhv_data, sub_opt=sub_opt, data_opt=bhv_data_opt)

        if chl_opt == 0:

            if time_opt == 0:

                eeg_rdms = eegRDM(EEG_data,
                                  time_win=time_win,
                                  sub_opt=sub_opt,
                                  chl_opt=chl_opt,
                                  time_opt=time_opt)

                corrs = np.zeros([subs, 2], dtype=np.float64)

                for i in range(subs):

                    if method == "spearman":

                        corrs[i] = rdm_correlation_spearman(bhv_rdms[i],
                                                            eeg_rdms[i],
                                                            rescale=rescale)

                    elif method == "pearson":

                        corrs[i] = rdm_correlation_pearson(bhv_rdms[i],
                                                           eeg_rdms[i],
                                                           rescale=rescale)

                    elif method == "kendall":

                        corrs[i] = rdm_correlation_kendall(bhv_rdms[i],
                                                           eeg_rdms[i],
                                                           rescale=rescale)

                    elif method == "similarity":

                        corrs[i, 0] = rdm_similarity(bhv_rdms[i],
                                                     eeg_rdms[i],
                                                     rescale=rescale)

                    elif method == "distance":

                        corrs[i, 0] = rdm_distance(bhv_rdms[i],
                                                   eeg_rdms[i],
                                                   rescale=rescale)

                return corrs

            # if time_opt=1

            eeg_rdms = eegRDM(EEG_data,
                              sub_opt=sub_opt,
                              chl_opt=chl_opt,
                              time_opt=time_opt)

            corrs = np.zeros([subs, ts, 2], dtype=np.float64)

            for i in range(subs):

                for j in range(ts):

                    if method == "spearman":

                        corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i],
                                                               eeg_rdms[i, j],
                                                               rescale=rescale)

                    elif method == "pearson":

                        corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i],
                                                              eeg_rdms[i, j],
                                                              rescale=rescale)

                    elif method == "kendall":

                        corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i],
                                                              eeg_rdms[i, j],
                                                              rescale=rescale)

                    elif method == "similarity":

                        corrs[i, j, 0] = rdm_similarity(bhv_rdms[i],
                                                        eeg_rdms[i, j],
                                                        rescale=rescale)

                    elif method == "distance":

                        corrs[i, j, 0] = rdm_distance(bhv_rdms[i],
                                                      eeg_rdms[i, j],
                                                      rescale=rescale)

            return corrs

        # chl_opt=1

        if time_opt == 1:
            return None

        # time_opt=0

        eeg_rdms = eegRDM(EEG_data,
                          sub_opt=sub_opt,
                          chl_opt=chl_opt,
                          time_opt=time_opt)

        corrs = np.zeros([subs, chls], dtype=np.float64)

        for i in range(subs):

            for j in range(chls):

                if method == "spearman":

                    corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i],
                                                           eeg_rdms[i, j],
                                                           rescale=rescale)

                elif method == "pearson":

                    corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i],
                                                          eeg_rdms[i, j],
                                                          rescale=rescale)

                elif method == "kendall":

                    corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i],
                                                          eeg_rdms[i, j],
                                                          rescale=rescale)

                elif method == "similarity":

                    corrs[i, j, 0] = rdm_similarity(bhv_rdms[i],
                                                    eeg_rdms[i, j],
                                                    rescale=rescale)

                elif method == "distance":

                    corrs[i, j, 0] = rdm_distance(bhv_rdms[i],
                                                  eeg_rdms[i, j],
                                                  rescale=rescale)

        return corrs

    # if sub_opt=0

    bhv_rdm = bhvRDM(bhv_data, sub_opt=sub_opt, data_opt=bhv_data_opt)

    if chl_opt == 1:

        if time_opt == 1:

            eeg_rdms = eegRDM(EEG_data,
                              sub_opt=sub_opt,
                              chl_opt=chl_opt,
                              time_opt=time_opt)

            corrs = np.zeros([chls, ts, 2], dtype=np.float64)

            for i in range(chls):

                for j in range(ts):

                    if method == "spearman":

                        corrs[i, j] = rdm_correlation_spearman(bhv_rdm,
                                                               eeg_rdms[i, j],
                                                               rescale=rescale)

                    elif method == "pearson":

                        corrs[i, j] = rdm_correlation_pearson(bhv_rdm,
                                                              eeg_rdms[i, j],
                                                              rescale=rescale)

                    elif method == "kendall":

                        corrs[i, j] = rdm_correlation_kendall(bhv_rdm,
                                                              eeg_rdms[i, j],
                                                              rescale=rescale)

                    elif method == "similarity":

                        corrs[i, j, 0] = rdm_similarity(bhv_rdm,
                                                        eeg_rdms[i, j],
                                                        rescale=rescale)

                    elif method == "distance":

                        corrs[i, j, 0] = rdm_distance(bhv_rdm,
                                                      eeg_rdms[i, j],
                                                      rescale=rescale)

            return corrs

        # if time_opt=0

        eeg_rdms = eegRDM(EEG_data,
                          sub_opt=sub_opt,
                          chl_opt=chl_opt,
                          time_opt=time_opt)

        corrs = np.zeros([chls, 2], dtype=np.float64)

        for i in range(chls):

            if method == "spearman":

                corrs[i] = rdm_correlation_spearman(bhv_rdm,
                                                    eeg_rdms[i],
                                                    rescale=rescale)

            elif method == "pearson":

                corrs[i] = rdm_correlation_pearson(bhv_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale)

            elif method == "kendall":

                corrs[i] = rdm_correlation_kendall(bhv_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale)

            elif method == "similarity":

                corrs[i, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i])

            elif method == "distance":

                corrs[i, 0] = rdm_distance(bhv_rdm, eeg_rdms[i])

        return corrs

    # if chl_opt=0

    if time_opt == 1:

        eeg_rdms = eegRDM(EEG_data, sub_opt=0, chl_opt=0, time_opt=1)

        corrs = np.zeros([ts, 2], dtype=np.float64)

        for i in range(ts):

            if method == "spearman":

                corrs[i] = rdm_correlation_spearman(bhv_rdm,
                                                    eeg_rdms[i],
                                                    rescale=rescale)

            elif method == "pearson":

                corrs[i] = rdm_correlation_pearson(bhv_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale)

            elif method == "kendall":

                corrs[i] = rdm_correlation_kendall(bhv_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale)

            elif method == "similarity":

                corrs[i, 0] = rdm_similarity(bhv_rdm,
                                             eeg_rdms[i],
                                             rescale=rescale)

            elif method == "distance":

                corrs[i, 0] = rdm_distance(bhv_rdm,
                                           eeg_rdms[i],
                                           rescale=rescale)

        return corrs

    # if time_opt=0

    eeg_rdm = eegRDM(EEG_data, sub_opt=0, chl_opt=0, time_opt=0)

    corr = np.zeros([2], dtype=np.float64)

    if method == "spearson":

        corr = rdm_correlation_spearman(bhv_rdm, eeg_rdm, rescale=rescale)

    elif method == "pearson":

        corr = rdm_correlation_pearson(bhv_rdm, eeg_rdm, rescale=rescale)

    elif method == "kendall":

        corr = rdm_correlation_kendall(bhv_rdm, eeg_rdm, rescale=rescale)

    elif method == "similarity":

        corr[0] = rdm_similarity(bhv_rdm, eeg_rdm, rescale=rescale)

    elif method == "distance":

        corr[0] = rdm_distance(bhv_rdm, eeg_rdm, rescale=rescale)

    return corr
Exemple #14
0
def rdms_corr(demo_rdm, eeg_rdms, method="spearman", rescale=False):
    """
    Calculate the Similarities between EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDMs and a demo RDM

    Parameters
    ----------
    demo_rdm : array [n_cons, n_cons]
        A demo RDM.
    eeg_rdms : array
        The EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDM(s).
        The shape can be [n_cons, n_cons] or [n1, n_cons, n_cons] or [n1, n2, n_cons, n_cons] or
        [n1, n2, n3, n_cons, n_cons]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs.
    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.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.

    Returns
    -------
    corrs : array
        The similarities between EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDMs and a demo RDM
        If the shape of eeg_rdms is [n_cons, n_cons], the shape of corrs will be [2]. If the shape of eeg_rdms is
        [n1, n_cons, n_cons], the shape of corrs will be [n1, 2]. If the shape of eeg_rdms is [n1, n2, n_cons, n_cons],
        the shape of corrs will be [n1, n2, 2]. If the shape of eeg_rdms is [n1, n2, n3, n_cons, n_cons], the shape of
        corrs will be [n1, n2, n3, 2]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs. 2 represents a r-value
        and a p-value.
    """

    if len(eeg_rdms.shape) == 5:

        n1, n2, n3 = eeg_rdms.shape[:3]

        # initialize the corrs
        corrs = np.zeros([n1, n2, n3, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(n1):
            for j in range(n2):
                for k in range(n3):

                    if method == "spearman":
                        corrs[i, j] = rdm_correlation_spearman(demo_rdm,
                                                               eeg_rdms[i, j],
                                                               rescale=rescale)
                    elif method == "pearson":
                        corrs[i, j] = rdm_correlation_pearson(demo_rdm,
                                                              eeg_rdms[i, j],
                                                              rescale=rescale)
                    elif method == "kendall":
                        corrs[i, j] = rdm_correlation_kendall(demo_rdm,
                                                              eeg_rdms[i, j],
                                                              rescale=rescale)
                    elif method == "similarity":
                        corrs[i, j, 0] = rdm_similarity(demo_rdm,
                                                        eeg_rdms[i, j],
                                                        rescale=rescale)
                    elif method == "distance":
                        corrs[i, j, 0] = rdm_distance(demo_rdm,
                                                      eeg_rdms[i, j],
                                                      rescale=rescale)

        return corrs

    if len(eeg_rdms.shape) == 4:

        n1, n2 = eeg_rdms.shape[:2]

        # initialize the corrs
        corrs = np.zeros([n1, n2, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(n1):
            for j in range(n2):

                if method == "spearman":
                    corrs[i, j] = rdm_correlation_spearman(demo_rdm,
                                                           eeg_rdms[i, j],
                                                           rescale=rescale)
                elif method == "pearson":
                    corrs[i, j] = rdm_correlation_pearson(demo_rdm,
                                                          eeg_rdms[i, j],
                                                          rescale=rescale)
                elif method == "kendall":
                    corrs[i, j] = rdm_correlation_kendall(demo_rdm,
                                                          eeg_rdms[i, j],
                                                          rescale=rescale)
                elif method == "similarity":
                    corrs[i, j, 0] = rdm_similarity(demo_rdm,
                                                    eeg_rdms[i, j],
                                                    rescale=rescale)
                elif method == "distance":
                    corrs[i, j, 0] = rdm_distance(demo_rdm,
                                                  eeg_rdms[i, j],
                                                  rescale=rescale)

        return corrs

    if len(eeg_rdms.shape) == 3:

        n1 = eeg_rdms.shape[0]

        # initialize the corrs
        corrs = np.zeros([n1, 2], dtype=np.float64)

        # calculate the corrs
        for i in range(n1):
            if method == "spearman":
                corrs[i] = rdm_correlation_spearman(demo_rdm,
                                                    eeg_rdms[i],
                                                    rescale=rescale)
            elif method == "pearson":
                corrs[i] = rdm_correlation_pearson(demo_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale)
            elif method == "kendall":
                corrs[i] = rdm_correlation_kendall(demo_rdm,
                                                   eeg_rdms[i],
                                                   rescale=rescale)
            elif method == "similarity":
                corrs[i, 0] = rdm_similarity(demo_rdm,
                                             eeg_rdms[i],
                                             rescale=rescale)
            elif method == "distance":
                corrs[i, 0] = rdm_distance(demo_rdm,
                                           eeg_rdms[i],
                                           rescale=rescale)

        return corrs

    # initialize the corrs
    corr = np.zeros([2], dtype=np.float64)

    # calculate the corrs
    if method == "spearman":
        corr = rdm_correlation_spearman(demo_rdm, eeg_rdms, rescale=rescale)
    elif method == "pearson":
        corr = rdm_correlation_pearson(demo_rdm, eeg_rdms, rescale=rescale)
    elif method == "kendall":
        corr = rdm_correlation_kendall(demo_rdm, eeg_rdms, rescale=rescale)
    elif method == "similarity":
        corr[0] = rdm_similarity(demo_rdm, eeg_rdms, rescale=rescale)
    elif method == "distance":
        corr[0] = rdm_distance(demo_rdm, eeg_rdms, rescale=rescale)

    return corr
Exemple #15
0
def fmrirdms_corr(demo_rdm, fmri_rdms, method="spearman", rescale=False):
    """
    Calculate the Similarities between fMRI searchlight RDMs and a demo RDM

    Parameters
    ----------
    demo_rdm : array [n_cons, n_cons]
        A demo RDM.
    fmri_rdms : array
        The fMRI-Searchlight RDMs.
        The shape of RDMs is [n_x, n_y, n_z, n_cons, n_cons]. n_x, n_y, n_z represent the number of calculation units
        for searchlight along the x, y, z axis.
    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.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.

    Returns
    -------
    corrs : array
        The similarities between fMRI searchlight RDMs and a demo RDM
        The shape of RDMs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units for searchlight
        along the x, y, z axis and 2 represents a r-value and a p-value.
    """
    # calculate the number of the calculation units in the x, y, z directions
    n_x = np.shape(fmri_rdms)[0]
    n_y = np.shape(fmri_rdms)[1]
    n_z = np.shape(fmri_rdms)[2]

    # initialize the corrs
    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    # calculate the corrs
    for i in range(n_x):
        for j in range(n_y):
            for k in range(n_z):

                if method == "spearman":
                    corrs[i, j, k] = rdm_correlation_spearman(demo_rdm,
                                                              fmri_rdms[i, j,
                                                                        k],
                                                              rescale=rescale)
                elif method == "pearson":
                    corrs[i, j, k] = rdm_correlation_pearson(demo_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)
                elif method == "kendall":
                    corrs[i, j, k] = rdm_correlation_kendall(demo_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)
                elif method == "similarity":
                    corrs[i, j, k, 0] = rdm_similarity(demo_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale)
                elif method == "distance":
                    corrs[i, j, k, 0] = rdm_distance(demo_rdm,
                                                     fmri_rdms[i, j, k],
                                                     rescale=rescale)

                print(corrs[i, j, k])

    return np.abs(corrs)
def fmrirdms_corr(demo_rdm,
                  fmri_rdms,
                  method="spearman",
                  rescale=False,
                  permutation=False,
                  iter=5000):
    """
    Calculate the similarity between fMRI searchlight RDMs and a demo RDM

    Parameters
    ----------
    demo_rdm : array [n_cons, n_cons]
        A demo RDM.
    fmri_rdms : array
        The fMRI-Searchlight RDMs.
        The shape of RDMs is [n_x, n_y, n_z, n_cons, n_cons]. n_x, n_y, n_z represent the number of calculation units
        for searchlight along the x, y, z axis.
    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.
    fisherz : bool True or False. Default is False.
        Do the Fisher-Z transform of the RDMs or not.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    corrs : array
        The similarities between fMRI searchlight RDMs and a demo RDM
        The shape of RDMs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units for searchlight
        along the x, y, z axis and 2 represents a r-value and a p-value.
    """

    if len(np.shape(demo_rdm)) != 2 or len(np.shape(fmri_rdms)) != 5 or np.shape(demo_rdm)[0] != np.shape(demo_rdm)[1] \
            or np.shape(fmri_rdms)[3] != np.shape(fmri_rdms)[4]:

        return "Invalid input!"

    # calculate the number of the calculation units in the x, y, z directions
    n_x = np.shape(fmri_rdms)[0]
    n_y = np.shape(fmri_rdms)[1]
    n_z = np.shape(fmri_rdms)[2]

    # initialize the corrs
    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    # calculate the corrs
    for i in range(n_x):
        for j in range(n_y):
            for k in range(n_z):

                if method == "spearman":
                    corrs[i, j, k] = rdm_correlation_spearman(
                        demo_rdm,
                        fmri_rdms[i, j, k],
                        rescale=rescale,
                        permutation=permutation,
                        iter=iter)
                elif method == "pearson":
                    corrs[i, j,
                          k] = rdm_correlation_pearson(demo_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale,
                                                       permutation=permutation,
                                                       iter=iter)
                elif method == "kendall":
                    corrs[i, j,
                          k] = rdm_correlation_kendall(demo_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale,
                                                       permutation=permutation,
                                                       iter=iter)
                elif method == "similarity":
                    corrs[i, j, k, 0] = rdm_similarity(demo_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale)
                elif method == "distance":
                    corrs[i, j, k, 0] = rdm_distance(demo_rdm,
                                                     fmri_rdms[i, j, k],
                                                     rescale=rescale)

    return corrs
Exemple #17
0
# Plot the RDM of -100ms, 0ms, 100ms, 200ms, 30ms, 400ms
times = [0, 10, 20, 30, 40, 50]
for t in times:
    plot_rdm(rdms[t], percentile=True)


# In[6]
"""**********       Section 6: Calculating the Similarity between two RDMs     **********"""

# RDM of 200ms
rdm_sample1 = rdms[30]
# RDM of 800ms
rdm_sample2 = rdms[90]

# calculate the correlation coefficient between these two RDMs
corr = rdm_correlation_spearman(rdm_sample1, rdm_sample2)
print(corr)


# In[7]
"""**********       Section 7: Calculating the Similarity and Plotting        **********"""

# Calculate the representational similarity between 200ms and all the time points
corrs1 = rdms_corr(rdm_sample1, rdms)

# Plot the corrs1
corrs1 = np.reshape(corrs1, [1, 110, 2])
plot_corrs_by_time(corrs1, time_unit=[-0.1, 0.01])

# Calculate and Plot multi-corrs
corrs2 = rdms_corr(rdm_sample2, rdms)
Exemple #18
0
def bhvANDfmri_corr(bhv_data,
                    fmri_data,
                    ksize=[3, 3, 3],
                    strides=[1, 1, 1],
                    method="spearman",
                    rescale=False):
    """
    Calculate the Similarities between behavioral data and fMRI data for searchlight

    Parameters
    ----------
    bhv_data : array
        The behavioral data.
        The shape of bhv_data must be [n_cons, n_subs, n_trials].
        n_cons, n_subs & n_trials represent the number of conidtions, the number of subjects & the number of trials,
        respectively.
    fmri_data : array
        The fmri data.
        The shape of fmri_data must be [n_cons, n_subs, n_chls, nx, ny, nz].
        n_cons, n_chls, nx, ny, nz represent the number of conidtions, the number of channels &
        the size of fMRI-img, respectively.
    ksize : array or list [kx, ky, kz]. Default is [3, 3, 3].
        The size of the fMRI-img. nx, ny, nz represent the number of voxels along the x, y, z axis.
    strides : array or list [sx, sy, sz]. Default is [1, 1, 1].
        The strides for calculating along the x, y, z axis.
    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.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the
        values on the diagonal.

    Returns
    -------
    corrs : array
        The similarities between behavioral data and fMRI data for searchlight.
        The shape of RDMs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units for searchlight
        along the x, y, z axis and 2 represents a r-value and a p-value.
    """

    # calculate the bhv_rdm
    bhv_rdm = bhvRDM(bhv_data, sub_opt=0)

    print("****************")
    print("get behavior RDM")
    print(bhv_rdm)

    # calculate the fmri_rdms for searchlight
    fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides)

    print("****************")
    print("get fMRI RDM")
    print(np.shape(fmri_rdms))

    # get the size of the fMRI-img
    nx = np.shape(fmri_data)[2]
    ny = np.shape(fmri_data)[3]
    nz = np.shape(fmri_data)[4]

    # the size of the calculation units for searchlight
    kx = ksize[0]
    ky = ksize[1]
    kz = ksize[2]

    # strides for calculating along the x, y, z axis
    sx = strides[0]
    sy = strides[1]
    sz = strides[2]

    # calculate the number of the calculation units in the x, y, z directions
    n_x = int((nx - kx) / sx) + 1
    n_y = int((ny - ky) / sy) + 1
    n_z = int((nz - kz) / sz) + 1

    # initialize the corrs
    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    # calculate the corrs
    for i in range(n_x):
        for j in range(n_y):
            for k in range(n_z):

                if method == "spearman":
                    corrs[i, j, k] = rdm_correlation_spearman(bhv_rdm,
                                                              fmri_rdms[i, j,
                                                                        k],
                                                              rescale=rescale)
                elif method == "pearson":
                    corrs[i, j, k] = rdm_correlation_pearson(bhv_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)
                elif method == "kendall":
                    corrs[i, j, k] = rdm_correlation_kendall(bhv_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)
                elif method == "similarity":
                    corrs[i, j, k, 0] = rdm_similarity(bhv_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale)
                elif method == "distance":
                    corrs[i, j, k, 0] = rdm_distance(bhv_rdm,
                                                     fmri_rdms[i, j, k],
                                                     rescale=rescale)

                print(corrs[i, j, k])

    return corrs
Exemple #19
0
def bhvANDfmri_corr(bhv_data, fmri_data, ksize=[3, 3, 3], strides=[1, 1, 1], sub_result=1, method="spearman", fisherz=False, rescale=False, permutation=False, iter=5000):

    """
    Calculate the Similarities between behavioral data and fMRI data for searchlight

    Parameters
    ----------
    bhv_data : array
        The behavioral data.
        The shape of bhv_data must be [n_cons, n_subs, n_trials].
        n_cons, n_subs & n_trials represent the number of conidtions, the number of subjects & the number of trials,
        respectively.
    fmri_data : array
        The fmri data.
        The shape of fmri_data must be [n_cons, n_subs, nx, ny, nz]. n_cons, nx, ny, nz represent the number of
        conditions, the number of subs & the size of fMRI-img, respectively.
    ksize : array or list [kx, ky, kz]. Default is [3, 3, 3].
        The size of the calculation unit for searchlight.
        kx, ky, kz represent the number of voxels along the x, y, z axis.
    strides : array or list [sx, sy, sz]. Default is [1, 1, 1].
        The strides for calculating along the x, y, z axis.
    sub_result: int 0 or 1. Default is 1.
        Return the subject-result or average-result.
        If sub_result=0, return the average result.
        If sub_result=1, return the results of each subject.
    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.
    fisherz : bool True or False. Default is False.
        Do the Fisher-Z transform of the RDMs or not.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the
        values on the diagonal.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    corrs : array
        The similarities between behavioral data and fMRI data for searchlight.
        If sub_result=0, the shape of corrs is [n_x, n_y, n_z, 2].
        If sub_result=1, the shape of corrs is [n_subs, n_x, n_y, n_z, 2].
        n_x, n_y, n_z represent the number of calculation units for searchlight along the x, y, z axis and 2 represents
        a r-value and a p-value.
    """

    if len(np.shape(bhv_data)) != 3 or len(np.shape(fmri_data)) != 5:

        return "Invalid input!"

    # calculate the bhv_rdm
    bhv_rdm = bhvRDM(bhv_data, sub_opt=0)

    print("****************")
    print("get behavior RDM")
    print(bhv_rdm)

    # calculate the fmri_rdms for searchlight
    fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides, sub_result=sub_result)

    print("****************")
    print("get fMRI RDM")
    print(np.shape(fmri_rdms))

    # get the number of subjects
    subs = np.shape(fmri_data)[1]

    # get the size of the fMRI-img
    nx = np.shape(fmri_data)[2]
    ny = np.shape(fmri_data)[3]
    nz = np.shape(fmri_data)[4]

    # the size of the calculation units for searchlight
    kx = ksize[0]
    ky = ksize[1]
    kz = ksize[2]

    # strides for calculating along the x, y, z axis
    sx = strides[0]
    sy = strides[1]
    sz = strides[2]

    # calculate the number of the calculation units in the x, y, z directions
    n_x = int((nx - kx) / sx) + 1
    n_y = int((ny - ky) / sy) + 1
    n_z = int((nz - kz) / sz) + 1

    # sub_result=0
    if sub_result == 0:

        # initialize the corrs
        corrs = np.full([n_x, n_y, n_z, 2], np.nan)

        # calculate the corrs
        for i in range(n_x):
            for j in range(n_y):
                for k in range(n_z):

                    if method == "spearman":
                        corrs[i, j, k] = rdm_correlation_spearman(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "pearson":
                        corrs[i, j, k] = rdm_correlation_pearson(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "kendall":
                        corrs[i, j, k] = rdm_correlation_kendall(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "similarity":
                        corrs[i, j, k, 0] = rdm_similarity(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale)
                    elif method == "distance":
                        corrs[i, j, k, 0] = rdm_distance(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale)

                    print(corrs[i, j, k])

        return corrs

    # sub_result=1

    # initialize the corrs
    corrs = np.full([subs, n_x, n_y, n_z, 2], np.nan)

    # calculate the corrs
    for sub in range(subs):
        for i in range(n_x):
            for j in range(n_y):
                for k in range(n_z):

                    if method == "spearman":
                        corrs[sub, i, j, k] = rdm_correlation_spearman(bhv_rdm, fmri_rdms[sub, i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "pearson":
                        corrs[sub, i, j, k] = rdm_correlation_pearson(bhv_rdm, fmri_rdms[sub, i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "kendall":
                        corrs[sub, i, j, k] = rdm_correlation_kendall(bhv_rdm, fmri_rdms[sub, i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter)
                    elif method == "similarity":
                        corrs[sub, i, j, k, 0] = rdm_similarity(bhv_rdm, fmri_rdms[sub, i, j, k], rescale=rescale)
                    elif method == "distance":
                        corrs[sub, i, j, k, 0] = rdm_distance(bhv_rdm, fmri_rdms[sub, i, j, k], rescale=rescale)

                    print(corrs[sub, i, j, k])

    return corrs
Exemple #20
0
def eegANDfmri_corr(eeg_data,
                    fmri_data,
                    chl_opt=0,
                    ksize=[3, 3, 3],
                    strides=[1, 1, 1],
                    method="spearman",
                    rescale=False):
    """
    Calculate the Similarities between EEG/MEG/fNIRS data and fMRI data for searchligt

    Parameters
    ----------
    eeg_data : array
        The EEG/MEG/fNIRS data.
        The shape of EEGdata must be [n_cons, n_subs, n_trials, n_chls, n_ts].
        n_cons, n_subs, n_trials, n_chls & n_ts represent the number of conidtions, the number of subjects, the number
        of trials, the number of channels & the number of time-points, respectively.
    fmri_data : array
        The fmri data.
        The shape of fmri_data must be [n_cons, n_subs, n_chls, nx, ny, nz].
        n_cons, n_chls, nx, ny, nz represent the number of conidtions, the number of channels &
        the size of fMRI-img, respectively.
    chl_opt : int 0 or 1. Default is 0.
        Calculate the RDM & similarities for each channel or not.
        If chl_opt=0, calculating based on all channels' data.
        If chl_opt=1, calculating based on each channel's data respectively.
    ksize : array or list [kx, ky, kz]. Default is [3, 3, 3].
        The size of the fMRI-img. nx, ny, nz represent the number of voxels along the x, y, z axis.
    strides : array or list [sx, sy, sz]. Default is [1, 1, 1].
        The strides for calculating along the x, y, z axis.
    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.
    rescale : bool True or False.
        Rescale the values in RDM or not.
        Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal.

    Returns
    -------
    corrs : array
        The similarities between EEG/MEG/fNIRS data and fMRI data for searchlight.
        If chl_opt=1, the shape of RDMs is [n_chls, n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation
        units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value.
        If chl_opt=0, the shape of RDMs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units
        for searchlight along the x, y, z axis and 2 represents a r-value and a p-value.
    """

    # get the size of the fMRI-img
    nx = np.shape(fmri_data)[2]
    ny = np.shape(fmri_data)[3]
    nz = np.shape(fmri_data)[4]

    # the size of the calculation units for searchlight
    kx = ksize[0]
    ky = ksize[1]
    kz = ksize[2]

    # strides for calculating along the x, y, z axis
    sx = strides[0]
    sy = strides[1]
    sz = strides[0]

    # calculate the number of the calculation units in the x, y, z directions
    n_x = int((nx - kx) / sx) + 1
    n_y = int((ny - ky) / sy) + 1
    n_z = int((nz - kz) / sz) + 1

    # calculate the fmri_rdms for searchlight
    fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides)

    # chl_opt=1

    if chl_opt == 1:

        # get the number of channels
        chls = np.shape(eeg_data)[3]

        # get the eeg_rdms
        eeg_rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=1, time_opt=0)

        # initialize the corrs
        corrs = np.full([chls, n_x, n_y, n_z, 2], np.nan)

        # calculate the corrs
        for j in range(n_x):
            for k in range(n_y):
                for l in range(n_z):
                    for i in range(chls):

                        if method == "spearman":
                            corrs[i, j, k, l] = rdm_correlation_spearman(
                                eeg_rdms[i],
                                fmri_rdms[j, k, l],
                                rescale=rescale)
                        elif method == "pearson":
                            corrs[i, j, k,
                                  l] = rdm_correlation_pearson(eeg_rdms[i],
                                                               fmri_rdms[j, k,
                                                                         l],
                                                               rescale=rescale)
                        elif method == "kendall":
                            corrs[i, j, k,
                                  l] = rdm_correlation_kendall(eeg_rdms[i],
                                                               fmri_rdms[j, k,
                                                                         l],
                                                               rescale=rescale)
                        elif method == "similarity":
                            corrs[i, j, k, l,
                                  0] = rdm_similarity(eeg_rdms[i],
                                                      fmri_rdms[j, k, l],
                                                      rescale=rescale)
                        elif method == "distance":
                            corrs[i, j, k, l,
                                  0] = rdm_distance(eeg_rdms[i],
                                                    fmri_rdms[i, j, k],
                                                    rescale=rescale)

        return np.abs(corrs)

    # chl_opt=0

    # get the eeg_rdms
    eeg_rdm = eegRDM(eeg_data, sub_opt=0, chl_opt=0, time_opt=0)

    # initialize the corrs
    corrs = np.full([n_x, n_y, n_z, 2], np.nan)

    # calculate the corrs
    for i in range(n_x):
        for j in range(n_y):
            for k in range(n_z):

                if method == "spearman":
                    corrs[i, j, k] = rdm_correlation_spearman(eeg_rdm,
                                                              fmri_rdms[i, j,
                                                                        k],
                                                              rescale=rescale)
                elif method == "pearson":
                    corrs[i, j, k] = rdm_correlation_pearson(eeg_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)
                elif method == "kendall":
                    corrs[i, j, k] = rdm_correlation_kendall(eeg_rdm,
                                                             fmri_rdms[i, j,
                                                                       k],
                                                             rescale=rescale)
                elif method == "similarity":
                    corrs[i, j, k, 0] = rdm_similarity(eeg_rdm,
                                                       fmri_rdms[i, j, k],
                                                       rescale=rescale)
                elif method == "distance":
                    corrs[i, j, k, 0] = rdm_distance(eeg_rdm,
                                                     fmri_rdms[i, j, k],
                                                     rescale=rescale)

    return np.abs(corrs)
Exemple #21
0
# Plot the RDM of 0ms, 50ms, 100ms, 150ms, 200ms
times = [10, 20, 30, 40, 50]
for t in times:
    plot_rdm(rdms[t], rescale=True)

#%% 对比两个rdm之间的相似性,只有一个值出来(想到MVPA的那个图)
"""**********       Section 6: Calculating the Similarity between two RDMs     **********"""

# RDM of 200ms
rdm_sample1 = rdms[30]
# RDM of 800ms
rdm_sample2 = rdms[90]

# calculate the correlation coefficient between these two RDMs
# r p值都有
corr = rdm_correlation_spearman(rdm_sample1, rdm_sample2, rescale=True)
print(corr)

#%%
"""**********       Section 7: Calculating the Similarity and Plotting        **********"""

# Calculate the representational similarity between 200ms and all the time points
# 这个思路就清晰了!自己设计的rdm和真实情况的rdm进行对比
# 到时候你算的时候就……也是先算好rdms,然后再用一个去对比
corrs1 = rdms_corr(rdm_sample1, rdms)

# Plot the corrs1
corrs1 = np.reshape(corrs1, [1, 110, 2])
plot_corrs_by_time(corrs1, time_unit=[-0.1, 0.01])

# Calculate and Plot multi-corrs
Exemple #22
0
"""

import numpy as np
from neurora.rdm_corr import rdm_correlation_spearman

model_rdm = np.loadtxt("../codingmodel_rdm/ori_rdm.txt")

for sub in range(6):
    eegrdms = np.loadtxt("../eegrdm/theta_power_po/sub" + str(sub + 1) +
                         ".txt")
    eegrdms = np.reshape(eegrdms, [851, 18, 18])

    sub_corrs = np.zeros([851, 2], dtype=np.float)

    for i in range(851):
        sub_corrs[i] = rdm_correlation_spearman(model_rdm, eegrdms[i])

    np.savetxt("corrs/theta_power_po/ori_subs" + str(sub + 1) + ".txt",
               sub_corrs)

model_rdm = np.loadtxt("../codingmodel_rdm/pos_rdm.txt")

for sub in range(6):
    eegrdms = np.loadtxt("../eegrdm/theta_power_po/sub" + str(sub + 1) +
                         ".txt")
    eegrdms = np.reshape(eegrdms, [851, 18, 18])

    sub_corrs = np.zeros([851, 2], dtype=np.float)

    for i in range(851):
        sub_corrs[i] = rdm_correlation_spearman(model_rdm, eegrdms[i])