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()

2 Likes

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

raw.plot()
raw_cleaned.plot()

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)
file1.pick_types(eeg=True)

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

ica.fit(file1)

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)
print(
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):
axs[channel_idx].clear()

    # 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')
    axs[channel_idx].set(ylabel='{}'.format(channel_names[channel_idx]))

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

plt.draw()

update_plot(0)

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)
update_plot(current_epoch[0])

btn_prev.on_clicked(prev_clicked)

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

btn_next.on_clicked(next_clicked)

plt.show()

All the best
Soren

1 Like

thank you!,
i will try with this code

Regards :blush: