Example #1
0
def stats_fmri(corrs, permutation=True, iter=5000):
    """
    Calculate the statistical results for fMRI

    Parameters
    ----------
    corrs : array
        The correlation coefficients.
        The shape of corrs must be [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.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    stats : array
        The statistical results.
        The shape of stats 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 t-value and a p-value.

    Notes
    -----
    n_subs must >= 6.
    This function can be used for the results of searchlight fMRI NPS and searchlight fMRI RDM-correlations.
    """

    # get the number of subjects
    subs = np.shape(corrs)[0]

    # subs>=6
    if subs < 6:
        return print("the number of subjects is too small!")

    # get the number of the calculation units in the x, y, z directions
    n_x, n_y, n_z = np.shape(corrs)[1:4]

    # initialize the corrs
    stats = np.zeros([n_x, n_y, n_z, 2], dtype=np.float)

    # get r-map
    rs = corrs[:, :, :, :, 0]

    # Fisher r to z
    zs = 0.5 * np.log((1 + rs) / (1 - rs))

    # calculate the statistical results
    for i in range(n_x):
        for j in range(n_y):
            for k in range(n_z):
                # t test
                stats[i, j, k] = ttest_1samp(zs[:, i, j, k], 0)

                if permutation == True:
                    stats[i, j, k, 1] = permutation_test(zs[:, i, j, k],
                                                         np.zeros([subs]),
                                                         iter=iter)

    return stats
Example #2
0
def stats(corrs, permutation=True, iter=5000):
    """
    Calculate the statistical results

    Parameters
    ----------
    corrs : array
        The correlation coefficients.
        The shape of corrs must be [n_subs, n_chls, n_ts, 2]. n_subs, n_chls, n_ts represent the number of subjects, the
        number of channels and the number of time-points. 2 represents a r-value and a p-value.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    stats : array
        The statistical results.
        The shape of stats is [n_chls, n_ts, 2]. n_chls, n_ts represent the number of channels and the number of
        time-points. 2 represents a t-value and a p-value.

    Notes
    -----
    n_subs must >= 6.
    This function can be used for the correlation results of NPS, ISC, eeg-like RDMs-correlations.
    """

    # get the number of subjects, channels & time-points
    subs, chls, ts = np.shape(corrs)[:3]

    # subs>=6
    if subs < 6:
        return print("the number of subjects is too small!")

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

    # get r-map
    rs = corrs[:, :, :, 0]

    #print(zs)

    # calculate the statistical results
    for i in range(chls):
        for j in range(ts):
            # t test
            stats[i, j] = ttest_1samp(rs[:, i, j], 0)

            if permutation == True:

                # Fisher r to z
                zs = 0.5 * np.log((1 + rs) / (1 - rs))

                stats[i, j, 1] = permutation_test(zs[:, i, j],
                                                  np.zeros([subs]),
                                                  iter=iter)

    return stats
Example #3
0
def plot_tbytresults(decoding_results_dir, subs):
    f = h5py.File(decoding_results_dir, "r")
    nsubs = len(subs)
    rlts = np.zeros([nsubs, 100], dtype=np.float)
    subindex = 0
    for sub in subs:
        rlts[subindex] = np.array(f[sub])
        for t in range(100):
            if t <= 1:
                rlts[subindex, t] = np.average(rlts[subindex, :t + 3])
            if t > 1 and t < 98:
                rlts[subindex, t] = np.average(rlts[subindex, t - 2:t + 3])
            if t >= 98:
                rlts[subindex, t] = np.average(rlts[subindex, t - 2:])
        subindex = subindex + 1
    f.close()

    avg = np.average(rlts, axis=0)
    err = np.zeros([100], dtype=np.float)
    for t in range(100):
        err[t] = np.std(rlts[:, t], ddof=1) / np.sqrt(nsubs)

    ps = np.zeros([100], dtype=np.float)
    chance = np.full([16], 0.0625)
    for t in range(100):
        ps[t] = permutation_test(rlts[:, t], chance)
        if ps[t] < 0.05 and avg[t] > 0.0625:
            plt.plot(t * 0.02 - 0.5, 0.148, "s", color="orangered", alpha=0.8)
            xi = [t * 0.02 - 0.5, t * 0.02 + 0.02 - 0.5]
            ymin = [0.0625]
            ymax = [avg[t] - err[t]]
            plt.fill_between(xi, ymax, ymin, facecolor="orangered", alpha=0.15)

    ax = plt.gca()
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["left"].set_linewidth(3)
    ax.spines["bottom"].set_linewidth(3)
    ax.spines['bottom'].set_position(('data', 0.0625))
    x = np.arange(-0.5 + 0.008, 1.5 + 0.008, 0.02)
    plt.fill_between(x, avg + err, avg - err, facecolor="orangered", alpha=0.8)
    plt.ylim(0.05, 0.15)
    plt.xlim(-0.5, 1.5)
    plt.xticks([-0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5])
    plt.tick_params(labelsize=12)
    plt.xlabel("Time (s)", fontsize=16)
    plt.ylabel("Classification Accuracy", fontsize=16)
    plt.show()
Example #4
0
def stats_stps(corrs1, corrs2, permutation=True, iter=5000):
    """
    Calculate the statistical results (for STPS)

    Parameters
    ----------
    corrs1 : array
        The correlation coefficients under condition1.
        The shape of corrs1 must be [n_subs, n_chls, n_ts]. n_subs, n_chls, n_ts represent the number of subjects, the
        number of channels and the number of time-points.
    corrs2 : array
        The correlation coefficients under condition2.
        The shape of corrs2 must be [n_subs, n_chls, n_ts]. n_subs, n_chls, n_ts represent the number of subjects, the
        number of channels and the number of time-points.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    stats : array
        The statistical results.
        The shape of stats is [n_chls, n_ts, 2]. n_chls, n_ts represent the number of channels and the number of
        time-points. 2 represents a t-value and a p-value.

    Notes
    -----
    n_subs must >= 6.
    """

    # get the number of subjects, channels & time-points
    subs, chls, ts = np.shape(corrs1)

    # subs>=6
    if subs < 6:
        return print("the number of subjects is too small!")

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

    # get r-map
    rs1 = corrs1
    rs2 = corrs2

    # calculate the statistical results
    for i in range(chls):
        for j in range(ts):
            # t test
            stats[i, j] = ttest_rel(rs1[:, i, j], rs2[:, i, j])

            if permutation == True:

                # Fisher r to z
                zs1 = 0.5 * np.log((1 + rs1) / (1 - rs1))
                zs2 = 0.5 * np.log((1 + rs2) / (1 - rs2))

                stats[i, j, 1] = permutation_test(zs1[:, i, j],
                                                  zs2[:, i, j],
                                                  iter=iter)

    return stats
Example #5
0
def stats_iscfmri(corrs, permutation=True, iter=5000):
    """
    Calculate the statistical results for fMRI (ISC searchlight)

    Parameters
    ----------
    corrs : array
        The correlation coefficients.
        The shape of corrs must be [n_ts, n_subs!/(2!*(n_subs-2)!), n_x, n_y, n_z, 2]. n_ts, 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.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    stats : array
        The statistical results.
        The shape of stats is [n_ts, n_x, n_y, n_z, 2]. n_ts, n_x, n_y, n_z represent the number of time-points, the
        number of calculation units for searchlight along the x, y, z axis and 2 represents a t-value and a p-value.

    Notes
    -----
    n_subs must >= 4 (n_subs!/(2!*(n_subs-2)!) >= 6).
    """

    # get the number of time-points, pairs
    ts, npairs = np.shape(corrs)[:2]

    # n_subs!/(2!*(n_subs-2)!)>=6
    if npairs < 6:
        return print("the number of subjects is too small!")

    # get the number of the calculation units in the x, y, z directions
    n_x, n_y, n_z = np.shape(corrs)[2:5]

    # initialize the corrs
    stats = np.zeros([ts, n_x, n_y, n_z, 2], dtype=np.float)

    # get r-map
    rs = corrs[:, :, :, :, :, 0]

    # calculate the statistical results
    for t in range(ts):
        for i in range(n_x):
            for j in range(n_y):
                for k in range(n_z):
                    # t test
                    stats[t, i, j, k] = ttest_1samp(rs[t, :, i, j, k], 0)

                    if permutation == True:

                        # Fisher r to z
                        zs = 0.5 * np.log((1 + rs) / (1 - rs))
                        stats[t, i, j, k,
                              1] = permutation_test(zs[t, :, i, j, k],
                                                    np.zeros([npairs]),
                                                    iter=iter)

    return stats
Example #6
0
def stats_stps(corrs1, corrs2, fisherz=True, permutation=True, iter=1000):
    """
    Conduct the statistical analysis for results of EEG-like data(for STPS)

    Parameters
    ----------
    corrs1 : array
        The correlation coefficients under condition1.
        The shape of corrs1 must be [n_subs, n_chls, n_ts]. n_subs, n_chls, n_ts represent the number of subjects, the
        number of channels and the number of time-points.
    corrs2 : array
        The correlation coefficients under condition2.
        The shape of corrs2 must be [n_subs, n_chls, n_ts]. n_subs, n_chls, n_ts represent the number of subjects, the
        number of channels and the number of time-points.
    fisherz : bool True or False. Default is True.
        Conduct Fisher-Z transform.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 1000.
        The times for iteration.

    Returns
    -------
    stats : array
        The statistical results.
        The shape of stats is [n_chls, n_ts, 2]. n_chls, n_ts represent the number of channels and the number of
        time-points. 2 represents a t-value and a p-value.

    Notes
    -----
    n_subs must >= 6.
    """

    if len(np.shape(corrs1)) != 3 or len(np.shape(corrs2)) != 3 or np.shape(corrs1)[1] != np.shape(corrs2)[1] or \
            np.shape(corrs1)[2] != np.shape(corrs2)[2]:

        return "Invalid input!"

    # get the number of subjects, channels & time-points
    subs, chls, ts = np.shape(corrs1)

    # subs>=6
    if subs < 6:
        return print("the number of subjects is too small!")

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

    # get r-map
    rs1 = corrs1
    rs2 = corrs2

    if fisherz == True:
        # Fisher r to z
        rs1 = 0.5 * np.log((1 + rs1) / (1 - rs1))
        rs2 = 0.5 * np.log((1 + rs2) / (1 - rs2))

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

            # t test
            stats[i, j] = ttest_rel(rs1[:, i, j], rs2[:, i, j])

            if permutation == True:

                stats[i, j, 1] = permutation_test(rs1[:, i, j],
                                                  rs2[:, i, j],
                                                  iter=iter)

    return stats
Example #7
0
def stats_fmri_compare_betweengroups(corrs1,
                                     corrs2,
                                     fisherz=True,
                                     permutation=False,
                                     iter=5000):
    """
    Conduct the statistical analysis for results of fMRI data (searchlight) (between 2 groups: group1 > group2)

    Parameters
    ----------
    corrs1 : array
        The correlation coefficients for group 1.
        The shape of corrs must be [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.
    corrs2 : array
        The correlation coefficients for group 2.
        The shape of corrs must be [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.
    fisherz : bool True or False. Default is True.
        Conduct Fisher-Z transform.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 5000.
        The times for iteration.

    Returns
    -------
    stats : array
        The statistical results.
        The shape of stats 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 t-value and a p-value.

    Notes
    -----
    n_subs must >= 6.
    This function can be used for the results of searchlight fMRI NPS and searchlight fMRI RDM-correlations.
    """

    if len(np.shape(corrs1)) != 5 or len(np.shape(corrs2)) != 5:
        return "Invalid input!"

    # get the number of subjects
    subs1 = np.shape(corrs1)[0]
    subs2 = np.shape(corrs2)[0]

    # subs>=6
    if subs1 < 6 or subs2 < 6:
        return print("the number of subjects is too small!")

    # get the number of the calculation units in the x, y, z directions
    n_x, n_y, n_z = np.shape(corrs1)[1:4]

    # initialize the corrs
    stats = np.zeros([n_x, n_y, n_z, 2], dtype=np.float)

    # get r-map
    rs1 = corrs1[:, :, :, :, 0]
    rs2 = corrs2[:, :, :, :, 0]

    if fisherz == True:
        rs1 = 0.5 * np.log((1 + rs1) / (1 - rs1))
        rs2 = 0.5 * np.log((1 + rs2) / (1 - rs2))

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

                # t test
                stats[i, j, k] = ttest_ind(rs1[:, i, j, k],
                                           rs2[:, i, j, k],
                                           alternative="greater")

                if permutation == True:
                    stats[i, j, k, 1] = permutation_test(rs1[:, i, j, k],
                                                         rs2[:, i, j, k],
                                                         iter=iter)

    return stats
Example #8
0
def stats(corrs, fisherz=True, permutation=True, iter=1000):
    """
    Conduct the statistical analysis for results of EEG-like data

    Parameters
    ----------
    corrs : array
        The correlation coefficients.
        The shape of corrs must be [n_subs, n_chls, n_ts, 2]. n_subs, n_chls, n_ts represent the number of subjects, the
        number of channels and the number of time-points. 2 represents a r-value and a p-value.
    fisherz : bool True or False. Default is True.
        Conduct Fisher-Z transform.
    permutation : bool True or False. Default is False.
        Use permutation test or not.
    iter : int. Default is 1000.
        The times for iteration.

    Returns
    -------
    stats : array
        The statistical results.
        The shape of stats is [n_chls, n_ts, 2]. n_chls, n_ts represent the number of channels and the number of
        time-points. 2 represents a t-value and a p-value.

    Notes
    -----
    n_subs must >= 6.
    This function can be used for the correlation results of NPS, ISC, eeg-like RDMs-correlations.
    """

    if len(np.shape(corrs)) != 4:

        return "Invalid input!"

    # get the number of subjects, channels & time-points
    subs, chls, ts = np.shape(corrs)[:3]

    # subs>=6
    if subs < 6:
        return print("the number of subjects is too small!")

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

    # get r-map
    rs = corrs[:, :, :, 0]

    if fisherz == True:
        rs = 0.5 * np.log((1 + rs) / (1 - rs))
    #print(zs)

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

            # t test
            stats[i, j] = ttest_1samp(rs[:, i, j], 0, alternative="greater")

            if permutation == True:

                stats[i, j, 1] = permutation_test(rs[:, i, j],
                                                  np.zeros([subs]),
                                                  iter=iter)

    return stats
Example #9
0
def plot_tbytsim_withstats(Similarities,
                           start_time=0,
                           end_time=1,
                           color='r',
                           lim=[-0.1, 0.8]):
    """
    Plot the time-by-time Similarities averaging all subjects

    Parameters
    ----------
    Similarities : array
        The Similarities.
        The size of Similarities should be [n_subs, n_ts] or [n_subs, n_ts, 2]. n_subs, n_ts represent the number of
        subjects and number of time-points. 2 represents the similarity and a p-value.
    start_time : int or float. Default is 0.
        The start time.
    end_time : int or float. Default is 1.
        The end time.
    color : matplotlib color or None. Default is 'r'.
        The color for the curve.
    lim : array or list [min, max]. Default is [-0.1, 0.8].
        The corrs view lims.
    """

    if len(np.shape(Similarities)) < 2 or len(np.shape(Similarities)) > 3:

        return "Invalid input!"

    n = len(np.shape(Similarities))

    minlim = lim[0]
    maxlim = lim[1]

    if n == 3:
        Similarities = Similarities[:, :, 0]

    nsubs = np.shape(Similarities)[0]
    nts = np.shape(Similarities)[1]

    tstep = float((end_time - start_time) / nts)

    for sub in range(nsubs):
        for t in range(nts):

            if t <= 1:
                Similarities[sub, t] = np.average(Similarities[sub, :t + 3])
            if t > 1 and t < (nts - 2):
                Similarities[sub, t] = np.average(Similarities[sub,
                                                               t - 2:t + 3])
            if t >= (nts - 2):
                Similarities[sub, t] = np.average(Similarities[sub, t - 2:])

    avg = np.average(Similarities, axis=0)
    err = np.zeros([nts], dtype=np.float)

    for t in range(nts):
        err[t] = np.std(Similarities[:, t], ddof=1) / np.sqrt(nsubs)

    ps = np.zeros([nts], dtype=np.float)
    chance = np.full([nsubs], 0)

    for t in range(nts):
        ps[t] = permutation_test(Similarities[:, t], chance)
        if ps[t] < 0.05 and avg[t] > 0:
            plt.plot(t * tstep + start_time,
                     maxlim * 0.9,
                     's',
                     color=color,
                     alpha=1)
            xi = [t * tstep + start_time, t * tstep + tstep + start_time]
            ymin = [0]
            ymax = [avg[t] - err[t]]
            plt.fill_between(xi, ymax, ymin, facecolor=color, alpha=0.1)

    ax = plt.gca()
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["left"].set_linewidth(3)
    ax.spines["bottom"].set_linewidth(3)
    ax.spines['bottom'].set_position(('data', 0))

    x = np.arange(start_time + 0.5 * tstep, end_time + 0.5 * tstep, tstep)
    plt.fill_between(x, avg + err, avg - err, facecolor=color, alpha=0.8)
    plt.ylim(minlim, maxlim)
    plt.xlim(start_time, end_time)
    plt.tick_params(labelsize=12)
    plt.xlabel("Time (s)", fontsize=16)
    plt.ylabel("Representational Similarity", fontsize=16)
    plt.show()

    return 0
Example #10
0
    def test_permutation_test(self):

        v1 = np.random.rand(20)
        v2 = np.random.rand(20)
        output = permutation_test(v1, v2)
        self.assertIsNotNone(output)