diff --git a/decoding_tf_analysis.py b/decoding_tf_analysis.py index 1b03368..ca75f86 100644 --- a/decoding_tf_analysis.py +++ b/decoding_tf_analysis.py @@ -183,7 +183,7 @@ def create_tfr(raw, condition, freqs, n_cycles, response='induced', baseline=Non return power -def time_frequency(dataset, filename, compute_tfr=True): +def time_frequency(dataset, filename, scaling='lin', compute_tfr=True): """ Runs time frequency analysis @@ -191,10 +191,13 @@ def time_frequency(dataset, filename, compute_tfr=True): :param filename: Filename of either the file from which the TFRs will be loaded or to which they will be saved :param compute_tfr: If True the TFRs will be created, else the TFRs will be loaded from a precomputed file + :param scaling: default 'lin' for linear scaling, else can be 'log' for logarithmic scaling """ # Parameters - # freqs = np.linspace(0.1, 50, num=50) # Use this for linear space scaling - freqs = np.logspace(*np.log10([0.1, 50]), num=50) + if scaling == 'lin': + freqs = np.linspace(0.1, 50, num=50) # Use this for linear space scaling + else: + freqs = np.logspace(*np.log10([0.1, 50]), num=50) n_cycles = freqs / 2 cond1 = [] cond2 = [] @@ -245,11 +248,11 @@ def time_frequency(dataset, filename, compute_tfr=True): F, clusters, cluster_p_values, h0 = mne.stats.permutation_cluster_test( [mne.grand_average(cond1).data, mne.grand_average(cond2).data], n_jobs=4, verbose='INFO', seed=123) - plot_tf_cluster(F, clusters, cluster_p_values, freqs, times) + plot_tf_cluster(F, clusters, cluster_p_values, freqs, times, scaling) if __name__ == '__main__': mne.set_log_level(verbose=VERBOSE_LEVEL) ds = 'N170' decoding(ds, 'faces_vs_cars', True) - time_frequency(ds, 'face_intact_vs_all_0.1_50hz_ncf2', True) + time_frequency(ds, 'face_intact_vs_all_0.1_50hz_ncf2', 'log', False) diff --git a/utils/plot_utils.py b/utils/plot_utils.py index 3503a2c..92a9723 100644 --- a/utils/plot_utils.py +++ b/utils/plot_utils.py @@ -56,7 +56,7 @@ def plot_grand_average(dataset): linestyles=['solid', 'solid', 'dotted', 'dotted']) -def plot_tf_cluster(F, clusters, cluster_p_values, freqs, times): +def plot_tf_cluster(F, clusters, cluster_p_values, freqs, times, scaling='lin'): """ Plot the F-Statistic values of permutation clusters with p-values <= 0.05 in color and > 0.05 in grey. Currently only works well for the linear scaling. For the logarithmic scaling a different x-axis has to be chosen @@ -66,6 +66,7 @@ def plot_tf_cluster(F, clusters, cluster_p_values, freqs, times): :param cluster_p_values: p-values of the clusters :param freqs: frequency domain :param times: time domain + :param scaling: default 'lin' for linear scaling, else can be 'log' for logarithmic scaling """ good_c = np.nan * np.ones_like(F) for clu, p_val in zip(clusters, cluster_p_values): @@ -75,12 +76,19 @@ def plot_tf_cluster(F, clusters, cluster_p_values, freqs, times): bbox = [times[0], times[-1], freqs[0], freqs[-1]] plt.imshow(F, aspect='auto', origin='lower', cmap=cm.gray, extent=bbox, interpolation='None') a = plt.imshow(good_c, cmap=cm.RdBu_r, aspect='auto', origin='lower', extent=bbox, interpolation='None') + + if scaling == 'log': + ticks = [1, 4, 8, 12, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50] + labels = [round(freqs[i], 2) for i in range(len(freqs)) if i + 1 in ticks] + plt.yticks(ticks, labels) + plt.colorbar(a) plt.xlabel('Time (s)') plt.ylabel('Frequency (Hz)') plt.show() + def plot_oscillation_bands(condition): """ Plot the oscillation bands for a given condition in the time from 130ms to 200ms