Predictors from tables¶
You can also build impulse-like predictors from tables or spreadsheets in which rows correspond to stimulus events and columns contain the impulse values. This is useful when you have stimulus features or annotations that were computed outside the neuraspeech workflow but want to include them as predictors. As long as these features can be saved as .csv tables or spreadsheets with one column listing the event timestamps and one or more additional columns containing predictor values, they can be converted into TRF predictors quickly and easily. Similar to predictors generated from TextGrid files, the resulting predictors are sparse arrays: they are filled with zeros except at the specified event times, where the predictor values are inserted.
Making predictors using tables¶
THis functionality is implemented in ns.trf.make_predictors_from_tables(), which takes .csv tables/spreadsheets like the one below. In this example, we create lexical/linguistic predictors containing surprisal values for the speech stimuli, with each surprisal value aligned to word onset and computed using a 5-gram language model:
example_table.head()
| sent_order | word | time | log10_prob | ngram_length | is_oov | is_content_word | surprisal | |
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | alice | 0.03 | -4.260178 | 2 | False | True | 14.152005 |
| 1 | 1 | was | 0.32 | -2.452873 | 3 | False | False | 8.148266 |
| 2 | 1 | beginning | 0.49 | -1.991067 | 3 | False | True | 6.614181 |
| 3 | 1 | to | 0.84 | -0.084916 | 4 | False | False | 0.282085 |
| 4 | 1 | get | 0.95 | -0.678081 | 5 | False | True | 2.252535 |
import neuraspeech as ns
ns.trf.make_predictors_from_tables(
eeg_sr = 128,
table_path = 'stim/alice_surprisal',
stim_path = 'stim/alice',
predictor_set = 'acou_pred_set',
predictor_name = 'lexical',
time_col = 'time',
value_cols = 'surprisal'
)
Generating predictors using tables in: C:\Users\xyc6648\OneDrive - Northwestern University\Desktop\scripts\neuraspeech\neuraspeech\trf\stim\alice_surprisal Output path: C:\Users\xyc6648\OneDrive - Northwestern University\Desktop\scripts\neuraspeech\neuraspeech\trf\predictors\acou_pred_set\lexical Processing track1.wav... Processing track10.wav... Processing track11.wav... Processing track12.wav... Processing track13.wav... Processing track14.wav... Processing track15.wav... Processing track16.wav... Processing track17.wav... Processing track18.wav... Processing track19.wav... Processing track2.wav... Processing track20.wav... Processing track21.wav... Processing track22.wav... Processing track23.wav... Processing track24.wav... Processing track25.wav... Processing track26.wav... Processing track27.wav... Processing track28.wav... Processing track29.wav... Processing track3.wav... Processing track30.wav... Processing track31.wav... Processing track32.wav... Processing track33.wav... Processing track34.wav... Processing track35.wav... Processing track36.wav... Processing track37.wav... Processing track38.wav... Processing track39.wav... Processing track4.wav... Processing track40.wav... Processing track41.wav... Processing track42.wav... Processing track43.wav... Processing track44.wav... Processing track45.wav... Processing track46.wav... Processing track47.wav... Processing track48.wav... Processing track49.wav... Processing track5.wav... Processing track50.wav... Processing track51.wav... Processing track52.wav... Processing track53.wav... Processing track54.wav... Processing track55.wav... Processing track56.wav... Processing track57.wav... Processing track58.wav... Processing track59.wav... Processing track6.wav... Processing track60.wav... Processing track7.wav... Processing track8.wav... Processing track9.wav... All done.
Key arguments you may want to change or check:
table_path: Path to the folder containing the table/spreadsheet files (.csv) for the stimuli. The files should have the same filenames as their corresponding audio files except for the file extension. For example,track1.csvshould correspond totrack1.wav.predictor_name: Name of the predictor to generate. In this example, we uselexical.target_tier: Name of the tier in the TextGrid files that contains the events you want to use.time_col: Name of the column in the tables listing the timestamps (in seconds) of stimulus events.val_cols: Name(s) of the column(s) containing the impulse values. It can be a list containing more than one column.
The surprisal predictors will be named like track1~lexical-surprisal.pickle.
Do a plot to visualize these predictors:
eeg_path = 'examples/sub-3000_04_ses-1_task-alice.pickle'
stim_path = '../../neuraspeech/trf/stim/alice'
predictor_set_path = '../../neuraspeech/trf/predictors/acou_pred_set'
stim_to_plot = 'track1.wav'
ns.trf.plot_schematic(
to_plot = ['eeg', 'waveform', 'gammatone-8', 'lexical-surprisal'],
colors = ['Spectral', None, 'inferno', '#D0495B'],
height_ratios = [1, 1, 1, 1.5],
linewidths = [1, None, None, 3.0],
as_impulses = [False, False, False, True], # If True, the predictors will be plotted as impulses instead of continuous lines.
figsize = (10, 6),
eeg_path = eeg_path,
stim_to_plot = stim_to_plot,
time_window = (0, 3.0),
stim_path = stim_path,
predictor_set_path = predictor_set_path
)
Again, if you set duration_mode = 'concat' during EEG data preparation, you'll need to run the EEG data preparation function to concatenate the newly genearated predictors.
Pro tips¶
1. Including or excluding specific events¶
You can pass dictionaries specifying inclusion/exclusion criteria to the include or exclude arguments to filter the tables before building the predictors. If you want to include only surprisals for content words and exclude those with with n-gram length of 1, you can do it like this:
ns.trf.make_predictors_from_tables(
eeg_sr = 128,
table_path = 'stim/alice_surprisal',
stim_path = 'stim/alice',
predictor_set = 'acou_pred_set',
predictor_name = 'lexical',
time_col = 'time',
value_cols = 'surprisal',
include = {'is_content_word': [True]},
exclude = {'ngram_length': [1]}
)
2. Combining predictors¶
If your val_cols is a list containing more than one predictor and you want to include them in a single predictor file rather than as separate files (which is the default behavior), you can set merge_predictors = True as in the following example. Then the output predictor file for each stimulus track will be named like track1~lexical-2.pickle, where the suffix -2 indicates the number of predictors included.
ns.trf.make_predictors_from_tables(
eeg_sr = 128,
table_path = 'stim/alice_surprisal',
stim_path = 'stim/alice',
predictor_set = 'acou_pred_set',
predictor_name = 'lexical',
time_col = 'time',
value_cols = ['surprisal', 'log10_prob'],
merge_predictors = True
)