Added the plotting option for log-scaling of the tf spectrum

This commit is contained in:
2021-03-29 16:25:38 +02:00
parent c6a6fd7012
commit d09480c5bb
2 changed files with 17 additions and 6 deletions

View File

@@ -183,7 +183,7 @@ def create_tfr(raw, condition, freqs, n_cycles, response='induced', baseline=Non
return power return power
def time_frequency(dataset, filename, compute_tfr=True): def time_frequency(dataset, filename, scaling='lin', compute_tfr=True):
""" """
Runs time frequency analysis Runs time frequency analysis
@@ -191,9 +191,12 @@ def time_frequency(dataset, filename, compute_tfr=True):
:param filename: Filename of either the file from which the TFRs will be loaded :param filename: Filename of either the file from which the TFRs will be loaded
or to which they will be saved 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 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 # Parameters
# freqs = np.linspace(0.1, 50, num=50) # Use this for linear space scaling 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) freqs = np.logspace(*np.log10([0.1, 50]), num=50)
n_cycles = freqs / 2 n_cycles = freqs / 2
cond1 = [] cond1 = []
@@ -245,11 +248,11 @@ def time_frequency(dataset, filename, compute_tfr=True):
F, clusters, cluster_p_values, h0 = mne.stats.permutation_cluster_test( 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', [mne.grand_average(cond1).data, mne.grand_average(cond2).data], n_jobs=4, verbose='INFO',
seed=123) 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__': if __name__ == '__main__':
mne.set_log_level(verbose=VERBOSE_LEVEL) mne.set_log_level(verbose=VERBOSE_LEVEL)
ds = 'N170' ds = 'N170'
decoding(ds, 'faces_vs_cars', True) 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)

View File

@@ -56,7 +56,7 @@ def plot_grand_average(dataset):
linestyles=['solid', 'solid', 'dotted', 'dotted']) 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. 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 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 cluster_p_values: p-values of the clusters
:param freqs: frequency domain :param freqs: frequency domain
:param times: time 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) good_c = np.nan * np.ones_like(F)
for clu, p_val in zip(clusters, cluster_p_values): 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]] bbox = [times[0], times[-1], freqs[0], freqs[-1]]
plt.imshow(F, aspect='auto', origin='lower', cmap=cm.gray, extent=bbox, interpolation='None') 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') 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.colorbar(a)
plt.xlabel('Time (s)') plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)') plt.ylabel('Frequency (Hz)')
plt.show() plt.show()
def plot_oscillation_bands(condition): def plot_oscillation_bands(condition):
""" """
Plot the oscillation bands for a given condition in the time from 130ms to 200ms Plot the oscillation bands for a given condition in the time from 130ms to 200ms