Using rescale function on epochs object

Hello everyone,

Here are my system specs:

  • MNE-Python version: 0.23
  • operating system: windows, linux

For the purpose of my analysis, I need to do baseline correction in different ways on different data (dividing by mean, subtracting by mean…). As part of my preprocessing, I do not do any baseline correction when epoching. My goal would be to then do baseline correction in each subsequent analyzes as to ensure that I can tune it for each analysis separately, without having to re-preprocess everything.

However, I would like to apply the baseline correction on the mne epochs object directly, as opposed to extracting the data and use the rescale function on a numpy array. My goal would therefore be to combine the epochs.apply_function with the rescale function. The problem I am facing is that the rescale function expects three different args and a couple of kwargs. The apply function however only seems to accept functions with one args and then as many kwargs as one wants. I therefore didn’t manage to implement it.

Here is the function I created:

def baseline_correction(epochs, correction_method="ratio", baseline=(None, 0), picks=None, n_jobs=1):

    times = [epochs.times[0], epochs.times[-1]]
    epochs.apply_function(rescale, times, baseline, picks=picks, n_jobs=n_jobs, **{"mode": correction_method})

    return None

Unfortunately, I always get an error as the apply_function complains as I am passing the times and baseline to it, where it only expects on args for the function. The other solution would be to pass times and baseline together with the kwargs, but I couldn’t figure out a way to do that.

Another solution would be to do something like that:

def baseline_correction(epochs, correction_method="ratio", baseline=(None, 0), picks=None, n_jobs=1):
    times = [epochs.times[0], epochs.times[-1]]
    epochs._data = rescale(epochs.get_data(), times, baseline)

    return epochs

But that doesn’t feel clean :confused:

Does anyone know if what I am trying to achieve is possible and if yes, how?

Thanks in advance for the support,

Kind regards,

Alex

Hello @AlexLepauvre,

Epochs.apply_function()

passes keyword arguments to the invoked function. Simply calling

epochs.apply_function(rescale, times=times, baseline=baseline, …)

should do the trick (not tested)

Best wishes,
Richard

Dear Richard,

That worked a treat. I tested it on my end, worked exactly as expected.

Thanks for the help,

Kind regards,

Alex

1 Like