Parametric Modulation in nistats

The function make_first_level_design_matrix allows the events argument (a DataFrame) to have a “modulation” column. This will create a modulated version of the condition’s regressor (rather than separating the modulated regressor from the main effect regressor). If you want to do the latter, then I think the following approach would work best:

from nistats.design_matrix import make_first_level_design_matrix
import numpy as np

n_scans = 200
tr = 2.
frame_times = np.arange(n_scans) * tr

# assuming you have onset, duration, trial_type, and modulation columns
# mean-center modulation to orthogonalize w.r.t. main effect of condition
events['modulation'] = events['modulation'] / events['modulation'].mean()

# create design matrix with modulation
dm_pm = make_first_level_design_matrix(
    frame_times,
    events,
    )

# remove modulation column
events = events[['onset', 'duration', 'trial_type']]

# create normal design matrix with modulation column added
# this assumes that you have one trial type (trial_type), so
# you'll need to edit the regs and names if not
dm = make_first_level_design_matrix(
    frame_times,
    events,
    add_regs=dm_pm[['trial_type']],
    add_reg_names=['modulator*trial_type'],
    )

You may want to sort the columns in the final design matrix (dm) before running the first levels.

I’ve only tested this a bit, but I believe that the resulting design matrix would be what you’re looking for.

1 Like