def plot_tse_topo(experiment, subject, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax): """ """ meggie_tfr = subject.tfr.get(tfr_name) times, tses = _compute_tse(meggie_tfr, fmin, fmax, tmin, tmax, blmode, blstart, blend) ch_names = meggie_tfr.ch_names info = meggie_tfr.info colors = color_cycle(len(tses)) logging.getLogger('ui_logger').info('Plotting TSE from all channels..') def individual_plot(ax, info_idx, names_idx): """ """ ch_name = ch_names[names_idx] title = 'TSE_{0}_{1}'.format(tfr_name, ch_name) ax.figure.canvas.set_window_title(title) ax.figure.suptitle(title) ax.set_title('') for color_idx, (key, tse) in enumerate(tses.items()): ax.plot(times, tse[names_idx], color=colors[color_idx], label=key) ax.axhline(0) ax.axvline(0) ax.legend() ax.set_xlabel('Time (s)') ax.set_ylabel('Power ({})'.format( get_power_unit(mne.io.pick.channel_type(info, info_idx), False))) plt.show() fig = plt.figure() for ax, info_idx, names_idx in iterate_topography(fig, info, ch_names, individual_plot): handles = [] for color_idx, (key, tse) in enumerate(tses.items()): handles.append( ax.plot(tse[names_idx], linewidth=0.2, color=colors[color_idx], label=key)[0]) fig.legend(handles=handles) title = 'TSE_{0}'.format(tfr_name) fig.canvas.set_window_title(title) fig.suptitle(title) plt.show()
def test_iterate_topography(): sample_folder = mne.datasets.sample.data_path() sample_fname = os.path.join(sample_folder, 'MEG', 'sample', 'sample_audvis_raw.fif') info = mne.io.read_raw_fif(sample_fname).info fig = plt.figure() ch_names = info['ch_names'][::2] def on_pick(ax, info_idx, names_idx): pass iterated = [] for ax, info_idx, names_idx in iterate_topography(fig, info, ch_names, on_pick): iterated.append((info_idx, names_idx)) assert (iterated[0] == (0, 0)) assert (iterated[-1] == (304, 152))
def plot_tse_topo(subject, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax, ch_type): """ Plots a tse topography. """ meggie_tfr = subject.tfr.get(tfr_name) tses = _compute_tse(meggie_tfr, fmin, fmax) info = meggie_tfr.info if ch_type == 'meg': picked_channels = [ ch_name for ch_idx, ch_name in enumerate(info['ch_names']) if ch_idx in mne.pick_types(info, meg=True, eeg=False) ] else: picked_channels = [ ch_name for ch_idx, ch_name in enumerate(info['ch_names']) if ch_idx in mne.pick_types(info, meg=False, eeg=True) ] info = info.copy().pick_channels(picked_channels) ch_names = meggie_tfr.ch_names colors = color_cycle(len(tses)) def individual_plot(ax, info_idx, names_idx): """ """ ch_name = ch_names[names_idx] title = ' '.join([tfr_name, ch_name]) ax.figure.canvas.set_window_title(title.replace(' ', '_')) ax.figure.suptitle(title) ax.set_title('') for color_idx, (key, tse) in enumerate(sorted(tses.items())): times, tse = _crop_and_correct_to_baseline(tse, blmode, blstart, blend, tmin, tmax, meggie_tfr.times) ax.plot(times, tse[names_idx], color=colors[color_idx], label=key) ax.axhline(0, color='black') ax.axvline(0, color='black') ax.legend() ax.set_xlabel('Time (s)') ax.set_ylabel('Power ({})'.format( get_power_unit(mne.io.pick.channel_type(info, info_idx), False))) plt.show() fig = plt.figure() for ax, info_idx, names_idx in iterate_topography(fig, info, ch_names, individual_plot): handles = [] for color_idx, (key, tse) in enumerate(sorted(tses.items())): times, tse = _crop_and_correct_to_baseline(tse, blmode, blstart, blend, tmin, tmax, meggie_tfr.times) handles.append( ax.plot(times, tse[names_idx], linewidth=0.2, color=colors[color_idx], label=key)[0]) fig.legend(handles=handles) title = '{0}_{1}'.format(tfr_name, ch_type) fig.canvas.set_window_title(title) plt.show()
def plot_spectrum_topo(subject, name, log_transformed=True, ch_type='meg'): """ Plots spectrum topography. """ subject_name = subject.name spectrum = subject.spectrum.get(name) data = spectrum.content freqs = spectrum.freqs ch_names = spectrum.ch_names info = spectrum.info if ch_type == 'meg': picked_channels = [ ch_name for ch_idx, ch_name in enumerate(info['ch_names']) if ch_idx in mne.pick_types(info, meg=True, eeg=False) ] else: picked_channels = [ ch_name for ch_idx, ch_name in enumerate(info['ch_names']) if ch_idx in mne.pick_types(info, eeg=True, meg=False) ] info = info.copy().pick_channels(picked_channels) colors = color_cycle(len(data)) def individual_plot(ax, info_idx, names_idx): """ """ ch_name = ch_names[names_idx] for color_idx, (key, psd) in enumerate(sorted(data.items())): if log_transformed: curve = 10 * np.log10(psd[names_idx]) else: curve = psd[names_idx] ax.plot(freqs, curve, color=colors[color_idx], label=key) title = ' '.join([name, ch_name]) ax.figure.canvas.set_window_title(title.replace(' ', '_')) ax.figure.suptitle(title) ax.set_title('') ax.legend() ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('Power ({})'.format( get_power_unit(mne.io.pick.channel_type(info, info_idx), log_transformed))) plt.show() fig = plt.figure() for ax, info_idx, names_idx in iterate_topography(fig, info, ch_names, individual_plot): handles = [] for color_idx, (key, psd) in enumerate(sorted(data.items())): if log_transformed: curve = 10 * np.log10(psd[names_idx]) else: curve = psd[names_idx] handles.append( ax.plot(curve, color=colors[color_idx], linewidth=0.5, label=key)[0]) if not handles: return fig.legend(handles=handles) title = '{0}_{1}'.format(name, ch_type) fig.canvas.set_window_title(title) plt.show()
def plot_topo_fit(subject, report_item): """ Plot topography where by clicking subplots you can check the fit parameters of specific channels """ reports = report_item.content ch_names = report_item.params['ch_names'] freqs = list(reports.values())[0].freqs colors = color_cycle(len(reports)) raw = subject.get_raw() info = raw.info def on_pick(ax, info_idx, names_idx): """ When a subplot representing a specific channel is clicked on the main topography plot, show a new figure containing FOOOF fit plot for every condition """ fig = ax.figure fig.delaxes(ax) for idx, (report_key, report) in enumerate(reports.items()): report_ax = fig.add_subplot(1, len(reports), idx + 1) fooof = report.get_fooof(names_idx) # Use plot function from fooof fooof.plot( ax=report_ax, plot_peaks='dot', add_legend=False, ) # Add information about the fit to the axis title text = ("Condition: " + str(report_key) + "\n" + "R squred: " + format_float(fooof.r_squared_) + "\n" + "Peaks: \n") for peak_params in fooof.peak_params_: text = text + '{0} ({1}, {2})\n'.format( *format_floats(peak_params)) report_ax.set_title(text) fig.tight_layout() # Create a topography where one can inspect fits by clicking subplots fig = plt.figure() for ax, info_idx, names_idx in iterate_topography(fig, info, ch_names, on_pick): handles = [] for color_idx, (key, report) in enumerate(reports.items()): curve = report.power_spectra[names_idx] handles.append( ax.plot(curve, color=colors[color_idx], linewidth=0.5, label=key)[0]) fig.legend(handles=handles) fig.canvas.set_window_title(report_item.name) fig.suptitle(report_item.name) plt.show()
def plot_spectrum_topo(experiment, name, log_transformed=True): """ """ subject = experiment.active_subject subject_name = subject.name spectrum = subject.spectrum.get(name) data = spectrum.content freqs = spectrum.freqs ch_names = spectrum.ch_names info = subject.get_raw().info info_names = info['ch_names'] colors = color_cycle(len(data)) logging.getLogger('ui_logger').info( 'Plotting spectrum from all channels..') def individual_plot(ax, info_idx, names_idx): """ """ ch_name = ch_names[names_idx] for color_idx, (key, psd) in enumerate(data.items()): if log_transformed: curve = 10 * np.log10(psd[names_idx]) else: curve = psd[names_idx] ax.plot(freqs, curve, color=colors[color_idx], label=key) title = 'spectrum_{0}_{1}'.format(name, ch_name) ax.figure.canvas.set_window_title(title) ax.figure.suptitle(title) ax.set_title('') ax.legend() ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('Power ({})'.format( get_power_unit(mne.io.pick.channel_type(info, info_idx), log_transformed))) plt.show() fig = plt.figure() for ax, info_idx, names_idx in iterate_topography(fig, info, ch_names, individual_plot): handles = [] for color_idx, (key, psd) in enumerate(data.items()): if log_transformed: curve = 10 * np.log10(psd[names_idx]) else: curve = psd[names_idx] handles.append( ax.plot(curve, color=colors[color_idx], linewidth=0.5, label=key)[0]) if not handles: return fig.legend(handles=handles) title = 'spectrum_{0}'.format(name) fig.canvas.set_window_title(title) fig.suptitle(title) plt.show()