how to use the regression-based correction method on multiple channels

Hi
As the title descripted, I want to apply the regression-based baseline correction on multi-channels, but the tutorials only show how to use it on one channel. When I copy the code (I changed the variables), the results reported a wrong message.

My code showed as follows:

sha_predictor = epochs.events[:, 2] == epochs.event_id[“shape-s”]
for_predictor = epochs.events[:, 2] == epochs.event_id[“form-s”]

baseline_predictor = (
epochs.copy()
.crop(*baseline)
.pick_channels([‘FP1’, ‘FP2’, ‘F7’, ‘F3’, ‘FZ’, ‘F4’, ‘F8’, ‘T7’, ‘C3’, ‘CZ’, ‘C4’, ‘T8’, ‘M1’, ‘P7’, ‘P3’, ‘PZ’, ‘P4’, ‘P8’, ‘M2’, ‘O1’, ‘O2’, ‘TP8’, ‘TP7’, ‘OZ’, ‘CP4’, ‘FC3’, ‘FT8’, ‘CP3’, ‘FT7’, ‘CPZ’, ‘FC4’, ‘FCZ’])
.get_data() # convert to NumPy array
.mean(axis=-1) # average across timepoints
)
baseline_predictor *= 1e6 # convert V → μV

design_matrix = np.vstack(
[
sha_predictor,
for_predictor,
baseline_predictor,
baseline_predictor * for_predictor,
]
).T

The error message was: “ValueError: operands could not be broadcast together with shapes (280,32) (280,)”

How could I change the code to let it work?

Thanks

------------------------------------------The End line---------------------------------------------------

  • MNE version: 1.6.0
  • operating system: Windows 10

Hello @Huizhong and welcomt to the forum!

Your code is hard to read because it’s not formatted properly; could you please edit your posting and format your Python code as “Preformatted text”? This posting describes how to do it:

Thanks!

Richard

Sorry, please see the code as bellow, would that be better?

sha_predictor = epochs.events[:, 2] == epochs.event_id[“shape-s”]
for_predictor = epochs.events[:, 2] == epochs.event_id[“form-s”]
baseline_predictor = (
     epochs.copy()
    .crop(*baseline)
    .pick_channels ([‘FP1’, ‘FP2’, ‘F7’, ‘F3’, ‘FZ’, ‘F4’, ‘F8’, ‘T7’, ‘C3’, ‘CZ’, ‘C4’, ‘T8’, ‘M1’, ‘P7’, ‘P3’, ‘PZ’, ‘P4’, ‘P8’, ‘M2’, ‘O1’, ‘O2’, ‘TP8’, ‘TP7’, ‘OZ’, ‘CP4’, ‘FC3’, ‘FT8’, ‘CP3’, ‘FT7’, ‘CPZ’, ‘FC4’, ‘FCZ’])
    .get_data(copy=False)  
    .mean(axis=-1)
 )
baseline_predictor *= 1e6  # convert V → μV
design_matrix = np.vstack(
    [
        sha_predictor,
        for_predictor,
        baseline_predictor,
        baseline_predictor * for_predictor,
    ]
).T

I thought some variables’ value should be changed as (280, 32), since the error suggested “ValueError: operands could not be broadcast together with shapes (280,32) (280,)”. But, I cannot figure out which variable should be changed and how to change it.

1 Like

I ran into this problem as well. The code I used to implement baseline regression across all channels (individually) is available in the repository for the resulting publication:

…specifically in the file https://github.com/aaronjnewman/SWOP/blob/main/scripts/SWOP%203%20-%20Baseline%20Regression.ipynb

Basically, I just put the regression fitting in a loop over channels.
let me know if you have any questions!

1 Like