In [1]:
%matplotlib
# %matplotlib inline
import warnings
warnings.filterwarnings('ignore')


Using matplotlib backend: Qt5Agg

In [2]:
from params_ica import subject_ids, sessions
from params_ica import main_path, data_path, preproc_pipeline_name
from ipywidgets import widgets
from IPython.display import display, clear_output, Javascript
import mne
from mne.io import read_raw_fif
from mne.preprocessing import read_ica
from mne.preprocessing import create_ecg_epochs, create_eog_epochs
import numpy as np
import getpass
import os
# Widget related imports
from traitlets import Unicode
# nbconvert related imports
from nbconvert import get_export_names, export_by_name
from nbconvert.writers import FilesWriter
from nbformat import read, NO_CONVERT
from nbconvert.utils.exceptions import ConversionException
warnings.filterwarnings('ignore')

Choose subject ID:


In [3]:
name_sel = widgets.Select(
    description='Subject ID:',
    options=subject_ids
)
display(name_sel)

cond_sel = widgets.RadioButtons(
    description='Condition:',
    options=sessions,
)
display(cond_sel)



In [4]:
%%capture
if cond_sel.value == sessions[0]:
    session = sessions[0]
elif cond_sel.value == sessions[1]:
    session = sessions[1]
subj_ID = name_sel.value

In [7]:
data_path = os.path.join(main_path, subj_ID)
pipeline_path = os.path.join(main_path, preproc_pipeline_name)
sbj_data_path = os.path.join(main_path, subj_ID, session, 'meg')

basename = subj_ID + '_task-rest_run-01_meg_raw_filt_dsamp'
results_folder = os.path.join('preproc_meeg', '_sess_index_' + session + '_subject_id_' + subj_ID)

raw_fname = basename + '.fif'
ica_fname = basename + '_ica.fif'
ica_TS_fname = basename + '_ica-tseries.fif'
report_fname = basename + '-report.html'
ica_solution_fname = basename + '_ica_solution.fif'

raw_file = os.path.join(pipeline_path, results_folder, 'preproc', raw_fname)  # filtered data
raw_ica_file = os.path.join(pipeline_path, results_folder, 'ica', ica_fname)  # cleaned data
new_raw_ica_file = os.path.join(sbj_data_path, ica_fname)  # path where to save the cleaned data after inspection
ica_TS_file = os.path.join(pipeline_path, results_folder, 'ica', ica_TS_fname)
ica_solution_file = os.path.join(pipeline_path, results_folder, 'ica', ica_solution_fname)
report_file = os.path.join(pipeline_path, results_folder, 'ica', report_fname)

Load data


In [8]:
# Load data -> we load the filtered data to see the TS of all ICs
print('Load raw file -> {} \n\n'.format(raw_file))
raw = read_raw_fif(raw_file, preload=True)
ica = read_ica(ica_solution_file)
ica.labels_ = dict()
ica_TS = ica.get_sources(raw)


Load raw file -> /media/pasca/paska/meg_data/omega/sample_BIDS_omega/preprocessing_pipeline/preproc_meeg/_sess_index_ses-0001_subject_id_sub-0003/preproc/sub-0003_task-rest_run-01_meg_0_60_raw_filt_dsamp.fif 


Opening raw data file /media/pasca/paska/meg_data/omega/sample_BIDS_omega/preprocessing_pipeline/preproc_meeg/_sess_index_ses-0001_subject_id_sub-0003/preproc/sub-0003_task-rest_run-01_meg_0_60_raw_filt_dsamp.fif...
This filename (/media/pasca/paska/meg_data/omega/sample_BIDS_omega/preprocessing_pipeline/preproc_meeg/_sess_index_ses-0001_subject_id_sub-0003/preproc/sub-0003_task-rest_run-01_meg_0_60_raw_filt_dsamp.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, raw.fif.gz, raw_sss.fif.gz or raw_tsss.fif.gz
    Read 5 compensation matrices
    Range : 0 ... 47999 =      0.000 ...    59.999 secs
Ready.
Current compensation grade : 3
Reading 0 ... 47999  =      0.000 ...    59.999 secs...
This filename (/media/pasca/paska/meg_data/omega/sample_BIDS_omega/preprocessing_pipeline/preproc_meeg/_sess_index_ses-0001_subject_id_sub-0003/ica/sub-0003_task-rest_run-01_meg_0_60_raw_filt_dsamp_ica_solution.fif) does not conform to MNE naming conventions. All ICA files should end with -ica.fif, -ica.fif.gz, _ica.fif or _ica.fif.gz
Reading /media/pasca/paska/meg_data/omega/sample_BIDS_omega/preprocessing_pipeline/preproc_meeg/_sess_index_ses-0001_subject_id_sub-0003/ica/sub-0003_task-rest_run-01_meg_0_60_raw_filt_dsamp_ica_solution.fif ...
Now restoring ICA solution ...
Ready.

Cell below opens an html report in a web-browser


In [9]:
%%bash -s "$report_file"
firefox -new-window $1

In [10]:
ica.exclude


Out[10]:
[14, 25, 22, 2, 5]

In [11]:
ica.plot_sources(raw)


Out[11]:

In [10]:
ica.plot_components(inst=raw)


Out[10]:
[<Figure size 750x700 with 20 Axes>, <Figure size 750x400 with 10 Axes>]
    using multitaper spectrum estimation with 7 DPSS windows
    using multitaper spectrum estimation with 7 DPSS windows

In [18]:
# ica.exclude
if ica.exclude:
    ica.plot_properties(raw, picks=ica.exclude)


    using multitaper spectrum estimation with 7 DPSS windows

Exclude ICA components

To exclude/include an ICA component click on mne_browse window: the red ones will be excluded. To keep the new excluded ICA components CLOSE the mne_browe window!

Apply ica solution to raw data and save the result

Check in the next cells if you are excluding the components you want!!! You can choose to save the cleaned raw file either in the ica folder (raw_ica_file) of workflow dir or in the subject folder (new_raw_ica_file)


In [19]:
print('You want to exclude the following components: ***  {} ***'.format(ica.exclude))


You want to exclude the following components: ***  [0, 14, 16, 21, 25] ***

In [20]:
%%capture
ica.apply(raw)
raw.save(raw_ica_file, overwrite=True)  # save in workflow dir
# raw.save(new_raw_ica_file, overwrite=True)  # save in subject dir
ica.save(ica_solution_file)

In [21]:
print('You REMOVED the following components: ***  {} *** \n'.format(ica.exclude))
print('You SAVED the new CLEANED file here: ***  {} ***'.format(raw_ica_file))


You REMOVED the following components: ***  [0, 14, 16, 21, 25] *** 

You SAVED the new CLEANED file here: ***  /media/pasca/paska/meg_data/omega/sample_BIDS_omega/preprocessing_pipeline/preproc_meeg/_sess_index_ses-0001_subject_id_sub-0003/ica/sub-0003_task-rest_run-01_meg_raw_filt_dsamp_ica.fif ***

In [ ]: