In [1]:
%matplotlib
# %matplotlib inline
import warnings
warnings.filterwarnings('ignore')
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')
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)
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)
In [9]:
%%bash -s "$report_file"
firefox -new-window $1
In [10]:
ica.exclude
Out[10]:
In [11]:
ica.plot_sources(raw)
Out[11]:
In [10]:
ica.plot_components(inst=raw)
Out[10]:
In [18]:
# ica.exclude
if ica.exclude:
ica.plot_properties(raw, picks=ica.exclude)
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!
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))
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))
In [ ]: