# Comparition of raw and pre-processed data

**URL:** <https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853>\
**Category:** Support & Discussions\
**Created:** [November 15, 2023, 12:54pm UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853 "2023-11-15T12:54:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![NicolePurkinje](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/nicolepurkinje/32/3120_2.png) [@NicolePurkinje](https://mne.discourse.group/u/NicolePurkinje)\
**Post date:** [November 15, 2023, 12:54pm UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853/1 "2023-11-15T12:54:03Z")

</div>

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! 😊

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 15, 2023, 1:04pm UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853/2 "2023-11-15T13:04:21Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 15, 2023, 1:05pm UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853/3 "2023-11-15T13:05:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [November 15, 2023, 1:37pm UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853/4 "2023-11-15T13:37:53Z")

</div>

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

```python
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.

---

<div class="post-metadata">

**Author:** ![NicolePurkinje](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/nicolepurkinje/32/3120_2.png) [@NicolePurkinje](https://mne.discourse.group/u/NicolePurkinje)\
**Post date:** [November 15, 2023, 4:37pm UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853/5 "2023-11-15T16:37:03Z")

</div>

thanks to both of you!

---

<div class="post-metadata">

**Author:** ![Soren](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Soren](https://mne.discourse.group/u/Soren)\
**Post date:** [November 17, 2023, 11:16am UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853/6 "2023-11-17T11:16:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NicolePurkinje](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/nicolepurkinje/32/3120_2.png) [@NicolePurkinje](https://mne.discourse.group/u/NicolePurkinje)\
**Post date:** [November 17, 2023, 2:08pm UTC](https://mne.discourse.group/t/comparition-of-raw-and-pre-processed-data/7853/7 "2023-11-17T14:08:41Z")

</div>

thank you!,  
i will try with this code

Regards 😊
