Comparition of raw and pre-processed data

Hello! there is a option for comparation of raw and pre-processed data in the same window or graph ? i want to see raw data with artifacts and data processed with ICA.

Thnks! :blush:

@wmvanvliet worked very hard on a system that allows for linking figures together. I’m not sure if what you’re asking for is possible yet, but in theory we should be able to support it.

You may also want to take a look at ica.plot_overlay()


You can open two separate raw browsers simultaneously, e.g.


You might have to type %matplotlib in the interactive Python console for this to work, or maybe it works out of the box.

1 Like

thanks to both of you!

Hi Nicole,

Perhaps something like this (currently just for muscle artifacts, not eye-blinks - 90% sure it works):

import os
import mne
from mne.preprocessing import ICA, create_ecg_epochs
import matplotlib.pyplot as plt
import matplotlib.widgets as widgets
import numpy as np

os.chdir(“your path to your data”)

file1 = mne.read_epochs(‘subject1-epo.fif’, preload=True)

ica = mne.preprocessing.ICA(
n_components=19, method=‘infomax’, max_iter=“auto”, fit_params=dict(extended=True), random_state=97

muscle_idx_auto, scores = ica.find_bads_muscle(file1)
ica.plot_scores(scores, exclude=muscle_idx_auto)

ica.plot_properties(file1, picks=muscle_idx_auto, log_scale=False)
f"Automatically found muscle artifact ICA components: {muscle_idx_auto}"

epochs_corrected = ica.apply(file1.copy(), exclude=muscle_idx_auto)

Create a time vector (assumes your epochs are 4 seconds long and your sampling rate is 250 Hz)

time_vector = np.arange(0, 4, 1 / 250)

Get the number of channels and epochs

num_channels = file1.get_data().shape[1]
num_epochs = file1.get_data().shape[0]

Get channel names

channel_names = file1.ch_names

Create the initial plot

fig, axs = plt.subplots(num_channels, 1, figsize=(10, 20))
fig.suptitle(‘Comparison of Uncorrected and ICA Corrected Epochs’)

Define a function to update the plot

def update_plot(epoch_idx):
for channel_idx in range(num_channels):

    # Get the data before and after ICA correction
    epoch_uncorrected = file1.get_data()[epoch_idx, channel_idx, :]
    epoch_corrected = epochs_corrected.get_data()[epoch_idx, channel_idx, :]

    # Create the plot
    axs[channel_idx].plot(time_vector, epoch_uncorrected, color='blue', label='Uncorrected')
    axs[channel_idx].plot(time_vector, epoch_corrected, color='red', label='ICA Corrected')

    # Add labels and legend to the last subplot of each figure
    axs[-1].set_xlabel('Time (s)')
    axs[0].legend(loc='upper right')



Add next and previous buttons

ax_prev = plt.axes([0.7, 0.05, 0.1, 0.075])
ax_next = plt.axes([0.81, 0.05, 0.1, 0.075])
btn_prev = widgets.Button(ax_prev, ‘Previous’)
btn_next = widgets.Button(ax_next, ‘Next’)

Define what happens when the buttons are clicked

current_epoch = [0]

def prev_clicked(event):
current_epoch[0] = max(current_epoch[0] - 1, 0)


def next_clicked(event):
current_epoch[0] = min(current_epoch[0] + 1, num_epochs - 1)


All the best

1 Like

thank you!,
i will try with this code

Regards :blush: