Plotting EEG and stimuli¶
This step is optional, but it's a good idea to visualize the EEG data and predictors and check the time alignment between the predictors before moving on to specifying your TRF model.
Visualizing EEG, waveform, and predictors¶
The ns.trf.plot_schematic() function allows you to do this easily:
import neuraspeech as ns
# Path to the EEG data file you want to plot
eeg_path = 'examples/sub-3000_04_ses-1_task-alice.pickle'
# Path to the stimulus directory
stim_path = '../../neuraspeech/trf/stim/alice'
# Path to the predictor set directory
predictor_set_path = '../../neuraspeech/trf/predictors/acou_pred_set'
# Which stimulus to plot?
stim_to_plot = 'track1.wav'
ns.trf.plot_schematic(
to_plot = ['eeg', 'waveform', 'gammatone-8', 'gammatone-on-8'],
eeg_path = eeg_path,
stim_to_plot = stim_to_plot,
time_window = (0, 3), # Show the first 3 seconds of the everything
stim_path = stim_path,
predictor_set_path = predictor_set_path
)
In addition to the paths, the main argument is to_plot. By setting it to ['eeg', 'waveform', 'gammatone-8', 'gammatone-on-8'], we'll plot the EEG waveform (each line representing a channel), audio waveform, and the 8-band gammatone envelope and onsets in the order listed. Predictor names should match the part of the predictor filename after the ~ and before the file extension (e.g., 'track1~gammatone-8.pickle'). Any item in to_plot other than 'eeg' or 'waveform' is interpreted as a predictor name and the function will try to load it loaded from predictor_set_path.
Check if the stimulus waveform and predictors are properly aligned. For example, you should see a narrow vertical stripe in the 8-band gammatone onset predictor when the audio waveform transitions from silence to voiced activity. If the alignment looks incorrect, first check whether there are any issues in the predictor generation step.
Pro tips¶
1. Customizing your plots¶
The function is named plot_schematic() because it can generate a schematic figure illustrating the EEG and time-aligned stimulus features, which canb be useful for poster presentations, slides, and publications. For these purposes, it is often helpful to create a more polished figure and have better control over the plot aesthetics. ns.trf.plot_schematic() includes several optional arguments for customizing the figure. The key idea is that most of these arguments should be provided as lists whose values match the order of the items in to_plot (use None a placeholder when you want to keep the default setting for a particular item). Feel free to play around with them.
ns.trf.plot_schematic(
to_plot = ['eeg', 'waveform', 'gammatone-1', 'gammatone-on-1', 'gammatone-8', 'gammatone-on-8'],
eeg_path = eeg_path,
stim_to_plot = stim_to_plot,
time_window = (0, 3),
stim_path = stim_path,
predictor_set_path = predictor_set_path,
## Other optional plotting parameters ##
# Output figure path
# If show_colorbar is True, will also save the colorbars as separate figures in the same folder
output_fig_path = 'examples/example_schematic.svg',
# Figure size
figsize = (12, 10),
# Colorbars for 2D predictor plots
show_colorbar = True,
# Relative heights of the subplots (must match the number of subplots)
height_ratios = [0.8, 0.5, 0.5, 0.5, 1, 1],
# Colors for each subplot. Can be color names, hex codes, or colormap names.
# Whether they are color names or colormap names will be automatically inferred based on plot type.
# See here for colormap reference: https://matplotlib.org/stable/users/explain/colors/colormaps.html
colors = ['Spectral', 'gray', '#fc4400', '#fc4400', 'inferno', 'inferno'],
# Line widths for each subplot
linewidths = [2.0, 0.3, 1.75, 1.75, None, None],
# Y-axis labels (use None for no labels)
ylabels = ['Amplitude', 'Amplitude', None, None, 'Frequency [Hz]', 'Frequency [Hz]'],
# Titles for subplots
titles = ['EEG', 'Waveform', 'Broadband envelope', 'Broadband onsets', 'Multiband envelope', 'Multiband onsets'],
# Whtether to hide y-axis ticks for each subplot
hide_y_ticks = [False, False, False, False, True, True]
)
C:\Users\xyc6648\OneDrive - Northwestern University\Desktop\scripts\neuraspeech\neuraspeech\trf\plot_schematic.py:169: UserWarning: Adding colorbar to a different Figure <Figure size 1200x1000 with 6 Axes> than <Figure size 120x232.558 with 1 Axes> which fig.colorbar is called on. cbar = cbar_fig.colorbar( C:\Users\xyc6648\OneDrive - Northwestern University\Desktop\scripts\neuraspeech\neuraspeech\trf\plot_schematic.py:169: UserWarning: Adding colorbar to a different Figure <Figure size 1200x1000 with 6 Axes> than <Figure size 120x232.558 with 1 Axes> which fig.colorbar is called on. cbar = cbar_fig.colorbar(