Using a separate condition as baseline (e.g., a resting state)

Hi there,

Similar to an archived questioned here Using a separate condition as baseline (e.g., a rest baseline)
I would like to use a baseline based on a resting state and not pre-stimulus. The post is quite old and I could not find if it has been changed.

Is this still not possible?
i.e. is the solution still through Epochs.apply_function() ?
If so, is there any tutorial/guidance for this I can find in the documentation?

Many thanks in advance,

Rosyl

Hi all,

just posting an update here (with a follow up question for @drammock):
I have use the Epochs.apply_function() to create a baseline, but I am getting an error because the dimensions don’t match. The exact error is "ValueError: operands could not be broadcast together with shapes (827,44,548) (44,) "

The relevant code is:


loading session and resting state data

raw_intensity = mne.io.read_raw_nirx(subj_data_dir, verbose = False)
raw_rest = mne.io.read_raw_nirx(subj_rest, verbose = False)
raw_rest.load_data()

creating and applying the baseline (to epoched data)

baseline = raw_rest.get_data().mean(axis=-1) # get avg value in each channel;
epochs.apply_function(lambda x: x - baseline, channel_wise=False)


But this gives the value error I added above.

I found a fix online, but I am not sure if it’s a good solution. If I apply this, I am just left with a flat line. I have added it, just in case it is a solution but I’ve made a mistake somewhere. The resulting graph is below.

Suggested fix:
(old line) baseline = raw_rest.get_data().mean(axis=-1) # get avg value in each channel;

baseline = np.repeat(baseline[:, np.newaxis], epochs.get_data().shape[2], axis=1)

(old line) epochs.apply_function(lambda x: x - baseline, channel_wise=False)

Outcome:

(before, my graphs did show the data).
In sum, I just want to know how to fix the value error. Any advice appreciated, many thanks!

Rosyl

Looks like you have 827 epochs, 44 channels, and 548 time points. to subtract an array with shape (44,) you need to add an extra empty time axis so that the baseline has shape (44, 1). The suggestion you found online should work but is more complicated than necessary. There are two simpler ways that come to mind:

  1. baseline = raw_rest.get_data().mean(axis=-1, keepdims=True) This will avoid collapsing the time dimension entirely, resulting shape is (44,1)
  2. baseline = raw_rest.get_data().mean(axis=-1) followed by baseline = baseline[:, np.newaxis] (this has the identical result to option 1)

Either one should fix the value error.

Thank you, this appeared to have fixed the value error. I get the same image as above, I will open a new thread to ask about it! Thanks again!