Logo
  • Home
  • Update history

Getting started

  • Installation

Phoneme-related potential

  • Preparing your data
  • PRP basics
  • F-statistic of manner separability

Temporal response function

  • Introduction
  • Getting started
  • Envelope and onset predictors
  • Preparing EEG data
  • Plotting EEG and stimuli
    • Visualizing EEG, waveform, and predictors
    • Pro tips
      • 1. Customizing your plots
  • Running TRF model
  • Submitting Slurm jobs
  • Analyzing results
  • Predictors from TextGrids
  • Predictors from tables
  • Pitch predictors
Neuraspeech
  • Temporal response function
  • Plotting EEG and stimuli
  • Edit on zc-guo/neuraspeech

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:

In [2]:
Copied!
import neuraspeech as ns
import neuraspeech as ns
In [3]:
Copied!
# 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
    )
# 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 )
No description has been provided for this image

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.

In [4]:
Copied!
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]
    )
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(
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
Previous Next

Built with MkDocs using a theme provided by Read the Docs.
zc-guo/neuraspeech « Previous Next »