def test_ica_ctf(): """Test run ICA computation on ctf data with/without compensation.""" method = 'fastica' raw = read_raw_ctf(ctf_fname, preload=True) events = make_fixed_length_events(raw, 99999) for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True) evoked = epochs.average() # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(UserWarning, match='did not converge'): ica.fit(inst) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) # test mixed compensation case raw.apply_gradient_compensation(0) ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw) raw.apply_gradient_compensation(1) epochs = Epochs(raw, events, None, -0.2, 0.2, preload=True) evoked = epochs.average() for inst in [raw, epochs, evoked]: with pytest.raises(RuntimeError, match='Compensation grade of ICA'): ica.apply(inst) with pytest.raises(RuntimeError, match='Compensation grade of ICA'): ica.get_sources(inst)
def test_ica_eeg(): """Test ICA on EEG.""" method = 'fastica' raw_fif = read_raw_fif(fif_fname, preload=True) with pytest.warns(RuntimeWarning, match='events'): raw_eeglab = read_raw_eeglab(input_fname=eeglab_fname, montage=eeglab_montage, preload=True) for raw in [raw_fif, raw_eeglab]: events = make_fixed_length_events(raw, 99999, start=0, stop=0.3, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = [] picks_all.extend(picks_meg) picks_all.extend(picks_eeg) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst, picks=picks) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) with pytest.warns(RuntimeWarning, match='MISC channel'): raw = read_raw_ctf(ctf_fname2, preload=True) events = make_fixed_length_events(raw, 99999, start=0, stop=0.2, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = picks_meg + picks_eeg for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst)
def test_ica_full_data_recovery(method): """Test recovery of full data when no source is rejected.""" # Most basic recovery _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(0.5, stop).load_data() events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] with pytest.warns(RuntimeWarning, match='projection'): epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) evoked = epochs.average() n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() data_evoked = evoked.data raw.set_annotations(Annotations([0.5], [0.5], ['BAD'])) methods = [method] for method in methods: stuff = [(2, n_channels, True), (2, n_channels // 2, False)] for n_components, n_pca_components, ok in stuff: ica = ICA(n_components=n_components, random_state=0, max_pca_components=n_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1) with pytest.warns(UserWarning, match=None): # sometimes warns ica.fit(raw, picks=list(range(n_channels))) raw2 = ica.apply(raw.copy(), exclude=[]) if ok: assert_allclose(data[:n_channels], raw2._data[:n_channels], rtol=1e-10, atol=1e-15) else: diff = np.abs(data[:n_channels] - raw2._data[:n_channels]) assert (np.max(diff) > 1e-14) ica = ICA(n_components=n_components, method=method, max_pca_components=n_pca_components, n_pca_components=n_pca_components, random_state=0) with pytest.warns(None): # sometimes warns ica.fit(epochs, picks=list(range(n_channels))) epochs2 = ica.apply(epochs.copy(), exclude=[]) data2 = epochs2.get_data()[:, :n_channels] if ok: assert_allclose(data_epochs[:, :n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(data_epochs[:, :n_channels] - data2) assert (np.max(diff) > 1e-14) evoked2 = ica.apply(evoked.copy(), exclude=[]) data2 = evoked2.data[:n_channels] if ok: assert_allclose(data_evoked[:n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(evoked.data[:n_channels] - data2) assert (np.max(diff) > 1e-14) pytest.raises(ValueError, ICA, method='pizza-decomposision')
def test_ica_full_data_recovery(): """Test recovery of full data when no source is rejected""" # Most basic recovery raw = Raw(raw_fname).crop(0.5, stop, False) raw.load_data() events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] with warnings.catch_warnings(record=True): # bad proj epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) evoked = epochs.average() n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() data_evoked = evoked.data for method in ['fastica']: stuff = [(2, n_channels, True), (2, n_channels // 2, False)] for n_components, n_pca_components, ok in stuff: ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1) with warnings.catch_warnings(record=True): ica.fit(raw, picks=list(range(n_channels))) raw2 = ica.apply(raw, exclude=[], copy=True) if ok: assert_allclose(data[:n_channels], raw2._data[:n_channels], rtol=1e-10, atol=1e-15) else: diff = np.abs(data[:n_channels] - raw2._data[:n_channels]) assert_true(np.max(diff) > 1e-14) ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components) with warnings.catch_warnings(record=True): ica.fit(epochs, picks=list(range(n_channels))) epochs2 = ica.apply(epochs, exclude=[], copy=True) data2 = epochs2.get_data()[:, :n_channels] if ok: assert_allclose(data_epochs[:, :n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(data_epochs[:, :n_channels] - data2) assert_true(np.max(diff) > 1e-14) evoked2 = ica.apply(evoked, exclude=[], copy=True) data2 = evoked2.data[:n_channels] if ok: assert_allclose(data_evoked[:n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(evoked.data[:n_channels] - data2) assert_true(np.max(diff) > 1e-14) assert_raises(ValueError, ICA, method='pizza-decomposision')
def test_ica_rank_reduction(): """Test recovery of full data when no source is rejected""" # Most basic recovery raw = Raw(raw_fname).crop(0.5, stop, False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] n_components = 5 max_pca_components = len(picks) for n_pca_components in [6, 10]: with warnings.catch_warnings(record=True): # non-convergence warnings.simplefilter('always') ica = ICA(n_components=n_components, max_pca_components=max_pca_components, n_pca_components=n_pca_components, method='fastica', max_iter=1).fit(raw, picks=picks) rank_before = raw.estimate_rank(picks=picks) assert_equal(rank_before, len(picks)) raw_clean = ica.apply(raw, copy=True) rank_after = raw_clean.estimate_rank(picks=picks) # interaction between ICA rejection and PCA components difficult # to preduct. Rank_after often seems to be 1 higher then # n_pca_components assert_true(n_components < n_pca_components <= rank_after <= rank_before)
def test_ica_rank_reduction(method): """Test recovery ICA rank reduction.""" _skip_check_picard(method) # Most basic recovery raw = read_raw_fif(raw_fname).crop(0.5, stop).load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] n_components = 5 max_pca_components = len(picks) for n_pca_components in [6, 10]: with pytest.warns(UserWarning, match='did not converge'): ica = ICA(n_components=n_components, max_pca_components=max_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1).fit(raw, picks=picks) rank_before = _compute_rank_int(raw.copy().pick(picks), proj=False) assert_equal(rank_before, len(picks)) raw_clean = ica.apply(raw.copy()) rank_after = _compute_rank_int(raw_clean.copy().pick(picks), proj=False) # interaction between ICA rejection and PCA components difficult # to preduct. Rank_after often seems to be 1 higher then # n_pca_components assert (n_components < n_pca_components <= rank_after <= rank_before)
def test_ica_twice(): """Test running ICA twice.""" raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() picks = pick_types(raw.info, meg='grad', exclude='bads') n_components = 0.9 max_pca_components = None n_pca_components = 1.1 with warnings.catch_warnings(record=True): ica1 = ICA(n_components=n_components, max_pca_components=max_pca_components, n_pca_components=n_pca_components, random_state=0) ica1.fit(raw, picks=picks, decim=3) raw_new = ica1.apply(raw, n_pca_components=n_pca_components) ica2 = ICA(n_components=n_components, max_pca_components=max_pca_components, n_pca_components=1.0, random_state=0) ica2.fit(raw_new, picks=picks, decim=3) assert_equal(ica1.n_components_, ica2.n_components_)
def test_ica_twice(method): """Test running ICA twice.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() picks = pick_types(raw.info, meg='grad', exclude='bads') n_components = 0.9 max_pca_components = None n_pca_components = 1.1 ica1 = ICA(n_components=n_components, method=method, max_pca_components=max_pca_components, n_pca_components=n_pca_components, random_state=0) ica1.fit(raw, picks=picks, decim=3) raw_new = ica1.apply(raw, n_pca_components=n_pca_components) ica2 = ICA(n_components=n_components, method=method, max_pca_components=max_pca_components, n_pca_components=1.0, random_state=0) ica2.fit(raw_new, picks=picks, decim=3) assert_equal(ica1.n_components_, ica2.n_components_)
def test_ica_twice(): """Test running ICA twice""" raw = io.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) picks = pick_types(raw.info, meg="grad", exclude="bads") n_components = 0.9 max_pca_components = None n_pca_components = 1.1 with warnings.catch_warnings(record=True): ica1 = ICA( n_components=n_components, max_pca_components=max_pca_components, n_pca_components=n_pca_components, random_state=0, ) ica1.fit(raw, picks=picks, decim=3) raw_new = ica1.apply(raw, n_pca_components=n_pca_components) ica2 = ICA( n_components=n_components, max_pca_components=max_pca_components, n_pca_components=1.0, random_state=0 ) ica2.fit(raw_new, picks=picks, decim=3) assert_equal(ica1.n_components_, ica2.n_components_)
def preprocess_ICA_fif_to_ts(fif_file, ECG_ch_name, EoG_ch_name, l_freq, h_freq, down_sfreq, variance, is_sensor_space, data_type): import os import numpy as np import mne from mne.io import Raw from mne.preprocessing import ICA, read_ica from mne.preprocessing import create_ecg_epochs, create_eog_epochs from mne.report import Report from nipype.utils.filemanip import split_filename as split_f report = Report() subj_path, basename, ext = split_f(fif_file) (data_path, sbj_name) = os.path.split(subj_path) print data_path # Read raw # If None the compensation in the data is not modified. # If set to n, e.g. 3, apply gradient compensation of grade n as for # CTF systems (compensation=3) raw = Raw(fif_file, preload=True) # select sensors select_sensors = mne.pick_types(raw.info, meg=True, ref_meg=False, exclude='bads') picks_meeg = mne.pick_types(raw.info, meg=True, eeg=True, exclude='bads') # save electrode locations sens_loc = [raw.info['chs'][i]['loc'][:3] for i in select_sensors] sens_loc = np.array(sens_loc) channel_coords_file = os.path.abspath("correct_channel_coords.txt") print '*** ' + channel_coords_file + '***' np.savetxt(channel_coords_file, sens_loc, fmt='%s') # save electrode names sens_names = np.array([raw.ch_names[pos] for pos in select_sensors],dtype = "str") # AP 21032016 # channel_names_file = os.path.join(data_path, "correct_channel_names.txt") channel_names_file = os.path.abspath("correct_channel_names.txt") np.savetxt(channel_names_file,sens_names , fmt = '%s') ### filtering + downsampling raw.filter(l_freq=l_freq, h_freq=h_freq, picks=picks_meeg, method='iir', n_jobs=8) # raw.filter(l_freq = l_freq, h_freq = h_freq, picks = picks_meeg, # method='iir') # raw.resample(sfreq=down_sfreq, npad=0) ### 1) Fit ICA model using the FastICA algorithm # Other available choices are `infomax` or `extended-infomax` # We pass a float value between 0 and 1 to select n_components based on the # percentage of variance explained by the PCA components. ICA_title = 'Sources related to %s artifacts (red)' is_show = False # visualization reject = dict(mag=4e-12, grad=4000e-13) # check if we have an ICA, if yes, we load it ica_filename = os.path.join(subj_path,basename + "-ica.fif") if os.path.exists(ica_filename) is False: ica = ICA(n_components=variance, method='fastica', max_iter=500) # , max_iter=500 ica.fit(raw, picks=select_sensors, reject=reject) # decim = 3, has_ICA = False else: has_ICA = True print ica_filename + ' exists!!!' ica = read_ica(ica_filename) ica.exclude = [] # 2) identify bad components by analyzing latent sources. # generate ECG epochs use detection via phase statistics # if we just have exclude channels we jump these steps # if len(ica.exclude)==0: n_max_ecg = 3 n_max_eog = 2 # check if ECG_ch_name is in the raw channels if ECG_ch_name in raw.info['ch_names']: ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5, picks=select_sensors, ch_name=ECG_ch_name) # if not a synthetic ECG channel is created from cross channel average else: ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5, picks=select_sensors) # ICA for ECG artifact # threshold=0.25 come default ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') print scores print '\n len ecg_inds *** ' + str(len(ecg_inds)) + '***\n' if len(ecg_inds) > 0: ecg_evoked = ecg_epochs.average() fig1 = ica.plot_scores(scores, exclude=ecg_inds, title=ICA_title % 'ecg', show=is_show) show_picks = np.abs(scores).argsort()[::-1][:5] # Pick the five largest scores and plot them # Plot estimated latent sources given the unmixing matrix. #ica.plot_sources(raw, show_picks, exclude=ecg_inds, title=ICA_title % 'ecg', show=is_show) t_start = 0 t_stop = 30 # take the fist 30s fig2 = ica.plot_sources(raw, show_picks, exclude=ecg_inds, title=ICA_title % 'ecg' + ' in 30s' ,start = t_start, stop = t_stop, show=is_show) # topoplot of unmixing matrix columns fig3 = ica.plot_components(show_picks, title=ICA_title % 'ecg', colorbar=True, show=is_show) ecg_inds = ecg_inds[:n_max_ecg] ica.exclude += ecg_inds fig4 = ica.plot_sources(ecg_evoked, exclude=ecg_inds, show=is_show) # plot ECG sources + selection fig5 = ica.plot_overlay(ecg_evoked, exclude=ecg_inds, show=is_show) # plot ECG cleaning fig = [fig1, fig2, fig3, fig4, fig5] report.add_figs_to_section(fig, captions=['Scores of ICs related to ECG', 'Time Series plots of ICs (ECG)', 'TopoMap of ICs (ECG)', 'Time-locked ECG sources', 'ECG overlay'], section = 'ICA - ECG') # check if EoG_ch_name is in the raw channels # if EoG_ch_name is empty if data_type is fif, ICA routine automatically looks for EEG61, EEG62 # otherwise if data_type is ds we jump this step if not EoG_ch_name and data_type=='ds': eog_inds = [] else: if EoG_ch_name in raw.info['ch_names']: ### ICA for eye blink artifact - detect EOG by correlation eog_inds, scores = ica.find_bads_eog(raw, ch_name = EoG_ch_name) else: eog_inds, scores = ica.find_bads_eog(raw) if len(eog_inds) > 0: fig6 = ica.plot_scores(scores, exclude=eog_inds, title=ICA_title % 'eog', show=is_show) report.add_figs_to_section(fig6, captions=['Scores of ICs related to EOG'], section = 'ICA - EOG') # check how many EoG ch we have rs = np.shape(scores) if len(rs)>1: rr = rs[0] show_picks = [np.abs(scores[i][:]).argsort()[::-1][:5] for i in range(rr)] for i in range(rr): fig7 = ica.plot_sources(raw, show_picks[i][:], exclude=eog_inds, start = raw.times[0], stop = raw.times[-1], title=ICA_title % 'eog',show=is_show) fig8 = ica.plot_components(show_picks[i][:], title=ICA_title % 'eog', colorbar=True, show=is_show) # ICA nel tempo fig = [fig7, fig8] report.add_figs_to_section(fig, captions=['Scores of ICs related to EOG', 'Time Series plots of ICs (EOG)'], section = 'ICA - EOG') else: show_picks = np.abs(scores).argsort()[::-1][:5] fig7 = ica.plot_sources(raw, show_picks, exclude=eog_inds, title=ICA_title % 'eog', show=is_show) fig8 = ica.plot_components(show_picks, title=ICA_title % 'eog', colorbar=True, show=is_show) fig = [fig7, fig8] report.add_figs_to_section(fig, captions=['Time Series plots of ICs (EOG)', 'TopoMap of ICs (EOG)',], section = 'ICA - EOG') eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds if EoG_ch_name in raw.info['ch_names']: eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5, picks=select_sensors, ch_name=EoG_ch_name).average() else: eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5, picks=select_sensors).average() fig9 = ica.plot_sources(eog_evoked, exclude=eog_inds, show=is_show) # plot EOG sources + selection fig10 = ica.plot_overlay(eog_evoked, exclude=eog_inds, show=is_show) # plot EOG cleaning fig = [fig9, fig10] report.add_figs_to_section(fig, captions=['Time-locked EOG sources', 'EOG overlay'], section = 'ICA - EOG') fig11 = ica.plot_overlay(raw, show=is_show) report.add_figs_to_section(fig11, captions=['Signal'], section = 'Signal quality') ### plot all topographies and time seris of the ICA components n_ica_components = ica.mixing_matrix_.shape[1] n_topo = 10; n_fig = n_ica_components/n_topo; n_plot = n_ica_components%n_topo; print '*************** n_fig = ' + str(n_fig) + ' n_plot = ' + str(n_plot) + '********************' fig = [] t_start = 0 t_stop = None # 60 if we want to take the fist 60s for n in range(0,n_fig): fig_tmp = ica.plot_components(range(n_topo*n,n_topo*(n+1)),title='ICA components', show=is_show) fig.append(fig_tmp) fig_tmp = ica.plot_sources(raw, range(n_topo*n,n_topo*(n+1)), start = t_start, stop = t_stop, title='ICA components') fig.append(fig_tmp) # if n_plot > 0: # fig_tmp = ica.plot_components(range(n_fig*n_topo,n_ica_components), title='ICA components', show=is_show) # fig.append(fig_tmp) # fig_tmp = ica.plot_sources(raw, range(n_fig*n_topo,n_ica_components), # start = t_start, stop = t_stop, # title='ICA components') # fig.append(fig_tmp) # # for n in range(0, len(fig)): # report.add_figs_to_section(fig[n], captions=['TOPO'], section = 'ICA Topo Maps') if n_plot > 5: n_fig_l = n_plot//5 print '*************************** ' + str(n_fig_l) + ' *********************************' for n in range(0,n_fig_l): print range(n_fig*n_topo+5*n, n_fig*n_topo+5*(n+1)) fig_tmp = ica.plot_components(range(n_fig*n_topo+5*n, n_fig*n_topo+5*(n+1)),title='ICA components') fig.append(fig_tmp) fig_tmp = ica.plot_sources(raw, range(n_fig*n_topo+5*n, n_fig*n_topo+5*(n+1)), start = t_start, stop = t_stop, title='ICA components') fig.append(fig_tmp) print range(n_fig*n_topo+5*(n+1),n_ica_components) fig_tmp = ica.plot_components(range(n_fig*n_topo+5*(n+1),n_ica_components), title='ICA components') fig.append(fig_tmp) fig_tmp = ica.plot_sources(raw, range(n_fig*n_topo+5*(n+1),n_ica_components), start = t_start, stop = t_stop, title='ICA components') fig.append(fig_tmp) for n in range(0, len(fig)): report.add_figs_to_section(fig[n], captions=['TOPO'], section = 'ICA Topo Maps') report_filename = os.path.join(subj_path,basename + "-report.html") print '******* ' + report_filename report.save(report_filename, open_browser=False, overwrite=True) # 3) apply ICA to raw data and save solution and report # check the amplitudes do not change # raw_ica_file = os.path.abspath(basename[:i_raw] + 'ica-raw.fif') raw_ica_file = os.path.join(subj_path, basename + '-preproc-raw.fif') raw_ica = ica.apply(raw) raw_ica.resample(sfreq=down_sfreq, npad=0) raw_ica.save(raw_ica_file, overwrite=True) # save ICA solution print ica_filename if has_ICA is False: ica.save(ica_filename) # 4) save data data_noIca, times = raw[select_sensors, :] data, times = raw_ica[select_sensors, :] print data.shape print raw.info['sfreq'] ts_file = os.path.abspath(basename + "_ica.npy") np.save(ts_file, data) print '***** TS FILE ' + ts_file + '*****' if is_sensor_space: return ts_file, channel_coords_file, channel_names_file, raw.info['sfreq'] else: # return raw_ica, channel_coords_file, channel_names_file, raw.info['sfreq'] return raw_ica_file, channel_coords_file, channel_names_file, raw.info['sfreq']
def test_ica_core(): """Test ICA on raw and epochs""" raw = Raw(raw_fname).crop(1.5, stop, copy=False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') # XXX. The None cases helped revealing bugs but are time consuming. test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [2, 1.0] # for future dbg add cases max_pca_components = [3] picks_ = [picks] methods = ['fastica'] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_, methods) # # test init catchers assert_raises(ValueError, ICA, n_components=3, max_pca_components=2) assert_raises(ValueError, ICA, n_components=2.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks, method in iter_ica_params: # Test ICA raw ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method, max_iter=1) assert_raises(ValueError, ica.__contains__, 'mag') print(ica) # to test repr # test fit checker assert_raises(RuntimeError, ica.get_sources, raw) assert_raises(RuntimeError, ica.get_sources, epochs) # test decomposition with warnings.catch_warnings(record=True): ica.fit(raw, picks=pcks, start=start, stop=stop) repr(ica) # to test repr assert_true('mag' in ica) # should now work without error # test re-fit unmixing1 = ica.unmixing_matrix_ with warnings.catch_warnings(record=True): ica.fit(raw, picks=pcks, start=start, stop=stop) assert_array_almost_equal(unmixing1, ica.unmixing_matrix_) sources = ica.get_sources(raw)[:, :][0] assert_true(sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3.preload = False assert_raises(ValueError, ica.apply, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) with warnings.catch_warnings(record=True): ica.fit(epochs, picks=picks) data = epochs.get_data()[:, 0, :] n_samples = np.prod(data.shape) assert_equal(ica.n_samples_, n_samples) print(ica) # to test repr sources = ica.get_sources(epochs).get_data() assert_true(sources.shape[1] == ica.n_components_) assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False assert_raises(ValueError, ica.apply, epochs3, include=[1, 2]) # test for bug with whitener updating _pre_whitener = ica._pre_whitener.copy() epochs._data[:, 0, 10:15] *= 1e12 ica.apply(epochs.copy()) assert_array_equal(_pre_whitener, ica._pre_whitener) # test expl. var threshold leading to empty sel ica.n_components = 0.1 assert_raises(RuntimeError, ica.fit, epochs) offender = 1, 2, 3, assert_raises(ValueError, ica.get_sources, offender) assert_raises(ValueError, ica.fit, offender) assert_raises(ValueError, ica.apply, offender)
# even if no ``exclude`` parameter is passed, and the list of excluded # components will be preserved when using :meth:`mne.preprocessing.ICA.save` # and :func:`mne.preprocessing.read_ica`. ica.exclude = [0, 1] # indices chosen based on various plots above ############################################################################### # Now that the exclusions have been set, we can reconstruct the sensor signals # with artifacts removed using the :meth:`~mne.preprocessing.ICA.apply` method # (remember, we're applying the ICA solution from the *filtered* data to the # original *unfiltered* signal). Plotting the original raw data alongside the # reconstructed data shows that the heartbeat and blink artifacts are repaired. # ica.apply() changes the Raw object in-place, so let's make a copy first: reconst_raw = raw.copy() ica.apply(reconst_raw) raw.plot(order=artifact_picks, n_channels=len(artifact_picks)) reconst_raw.plot(order=artifact_picks, n_channels=len(artifact_picks)) del reconst_raw ############################################################################### # Using an EOG channel to select ICA components # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # It may have seemed easy to review the plots and manually select which ICs to # exclude, but when processing dozens or hundreds of subjects this can become # a tedious, rate-limiting step in the analysis pipeline. One alternative is to # use dedicated EOG or ECG sensors as a "pattern" to check the ICs against, and # automatically mark for exclusion any ICs that match the EOG/ECG pattern. Here # we'll use :meth:`~mne.preprocessing.ICA.find_bads_eog` to automatically find
overwrite=True) # overwrite w/ bad channel info/interpolated bads # step 3- apply ICA to the conjoint data picks = pick_types(raw.info, meg=True, exclude='bads') ica = ICA(n_components=0.95, method='fastica') # get ica components ica.exclude = [] ica.fit(raw, picks=picks) ica.save(ica_fname) # save solution # view components and make rejections ica.plot_sources(raw) # apply ica to raw and save resulting clean raw file ica.apply(raw) raw.save(ica_raw_fname, overwrite=True) # step 4- filter raw = raw.filter(filt_l, filt_h) # step 5- make epochs events = find_events(raw) # the output of this is a 3 x n_trial np array # note: you may want to add some decimation here epochs = Epochs(raw, events, tmin=tmin, tmax=tmax, decim=5, baseline=None) trial_info = pd.read_csv(trial_info_fname) # step 6- reject epochs based on threshold (2e-12) # opens the gui, "mark" is to mark in red the channel closest to the eyes if op.isfile(ica_rej_fname):
eog_inds_fruit = eog_inds_fruit [:n_max_eog] eog_inds_odour = eog_inds_odour[:n_max_eog] eog_inds_milk = eog_inds_milk [:n_max_eog] eog_inds_LD = eog_inds_LD [:n_max_eog] # Excluding the list of sources indices obtained in the previous line ica_fruit.exclude += eog_inds_fruit ica_odour.exclude += eog_inds_odour ica_milk.exclude += eog_inds_milk ica_LD.exclude += eog_inds_LD # Removing the selected components from the data ica_fruit.apply(inst=raw_fruit, exclude=eog_inds_fruit) ica_odour.apply(inst=raw_odour, exclude=eog_inds_odour) ica_milk.apply(inst=raw_milk , exclude=eog_inds_milk) ica_LD.apply(inst=raw_LD , exclude=eog_inds_LD) # checking for the desired directory to save the data if not os.path.isdir(data_path + meg): os.makedirs(data_path + meg) out_fname_fruit = data_path + meg + 'block_fruit_tsss_notch_BPF0.1_45_ICAeog_raw.fif' out_fname_odour = data_path + meg + 'block_odour_tsss_notch_BPF0.1_45_ICAeog_raw.fif' out_fname_milk = data_path + meg + 'block_milk_tsss_notch_BPF0.1_45_ICAeog_raw.fif' out_fname_LD = data_path + meg + 'block_LD_tsss_notch_BPF0.1_45_ICAeog_raw.fif'
# ica.plot_sources(raw, show_picks, exclude=eog_inds, # title="Sources related to EOG artifacts (red)") ica.plot_components(eog_inds, title="Sources related to EOG artifacts", colorbar=True) eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds ########################################################################### # 3) Assess component selection and unmixing quality # estimate average artifact ecg_evoked = ecg_epochs.average() # plot ECG sources + selection ica.plot_sources(ecg_evoked, exclude=ecg_inds) # plot ECG cleaning ica.plot_overlay(ecg_evoked, exclude=ecg_inds) eog_evoked = create_eog_epochs(raw, tmin=-0.5, tmax=0.5, picks=picks).average() # plot EOG sources + selection # ica.plot_sources(eog_evoked, exclude=eog_inds) ica.plot_overlay(eog_evoked, exclude=eog_inds) # plot EOG cleaning # check the amplitudes do not change ica.plot_overlay(raw) # EOG artifacts remain ########################################################################## # Apply the solution to Raw, Epochs or Evoked like this: raw_ica = ica.apply(raw, copy=False) raw_ica.save(data_path + "tone_task-%s-tsss-mc-autobad-ica_raw.fif" % (condition), overwrite=True)
def run_epoch(subject_id): subject = "sub_%03d" % subject_id print("processing subject: %s" % subject) in_path = op.join( data_path, "EEG_Process") #make map yourself in cwd called 'Subjects' process_path = op.join( data_path, "EEG_Process") #make map yourself in cwd called 'EEG_Process' raw_list = list() events_list = list() for run in range(1, 2): fname = op.join(in_path, 'sub_%03d_raw.fif' % (subject_id, )) raw = mne.io.read_raw_fif(fname, preload=True) print(" S %s - R %s" % (subject, run)) #####ICA##### ica = ICA(random_state=97, n_components=15) picks = mne.pick_types(raw.info, eeg=True, eog=True, stim=False, exclude='bads') ica.fit(raw, picks=picks) raw.load_data() #make epochs around stimuli events fname_events = op.join(process_path, 'events_%03d-eve.fif' % (subject_id, )) delay = int(round(0.0345 * raw.info['sfreq'])) events = mne.read_events(fname_events) events[:, 0] = events[:, 0] + delay events_list.append(events) epochs = mne.Epochs(raw, events, events_id, tmin=-0.2, tmax=0.5, proj=True, picks=picks, baseline=(None, 0), preload=False, reject=None) #get EOG epochs eog_epochs = create_eog_epochs(raw, tmin=-.5, tmax=.5, preload=False) n_max_eog = 3 # use max 2 components eog_epochs.load_data() eog_epochs.apply_baseline((None, None)) eog_inds, scores_eog = ica.find_bads_eog(eog_epochs) print(' Found %d EOG indices' % (len(eog_inds), )) ica.exclude.extend(eog_inds[:n_max_eog]) eog_epochs.average() del eog_epochs #apply ICA on epochs epochs.load_data() ica.apply(epochs) reject = get_rejection_threshold(epochs, random_state=97) epochs.drop_bad(reject=reject) print(' Dropped %0.1f%% of epochs' % (epochs.drop_log_stats(), )) #epochs.plot(picks=('Oz'), title='epochs, electrode Oz') #save epochs epochs.save( op.join(process_path, "sub_%03d_raw-epo.fif" % (subject_id, )))
def saflow_preproc(filepath, savepath, reportpath): report = mne.Report(verbose=True) raw = read_raw_ctf(filepath, preload=True) raw_data = raw.copy().apply_gradient_compensation( grade=3) #required for source reconstruction picks = mne.pick_types(raw_data.info, meg=True, eog=True, exclude='bads') fig = raw_data.plot(show=False) report.add_figs_to_section(fig, captions='Time series', section='Raw data') close(fig) fig = raw_data.plot_psd(average=False, picks=picks, show=False) report.add_figs_to_section(fig, captions='PSD', section='Raw data') close(fig) ## Filtering high_cutoff = 200 low_cutoff = 0.5 raw_data.filter(low_cutoff, high_cutoff, fir_design="firwin") raw_data.notch_filter(np.arange(60, high_cutoff + 1, 60), picks=picks, filter_length='auto', phase='zero', fir_design="firwin") fig = raw_data.plot_psd(average=False, picks=picks, fmax=120, show=False) report.add_figs_to_section(fig, captions='PSD', section='Filtered data') close(fig) ## ICA ica = ICA(n_components=20, random_state=0).fit(raw_data, decim=3) fig = ica.plot_sources(raw_data, show=False) report.add_figs_to_section(fig, captions='Independent Components', section='ICA') close(fig) ## FIND ECG COMPONENTS ecg_threshold = 0.50 ecg_epochs = create_ecg_epochs(raw_data, ch_name='EEG059') ecg_inds, ecg_scores = ica.find_bads_ecg(ecg_epochs, ch_name='EEG059', method='ctps', threshold=ecg_threshold) fig = ica.plot_scores(ecg_scores, ecg_inds, show=False) report.add_figs_to_section(fig, captions='Correlation with ECG (EEG059)', section='ICA - ECG') close(fig) fig = list() try: fig = ica.plot_properties(ecg_epochs, picks=ecg_inds, image_args={'sigma': 1.}, show=False) for i, figure in enumerate(fig): report.add_figs_to_section(figure, captions='Detected component ' + str(i), section='ICA - ECG') close(figure) except: print('No component to remove') ## FIND EOG COMPONENTS eog_threshold = 4 eog_epochs = create_eog_epochs(raw_data, ch_name='EEG057') eog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name='EEG057', threshold=eog_threshold) fig = ica.plot_scores(eog_scores, eog_inds, show=False) report.add_figs_to_section(fig, captions='Correlation with EOG (EEG057)', section='ICA - EOG') close(fig) fig = list() try: fig = ica.plot_properties(eog_epochs, picks=eog_inds, image_args={'sigma': 1.}, show=False) for i, figure in enumerate(fig): report.add_figs_to_section(figure, captions='Detected component ' + str(i), section='ICA - EOG') close(figure) except: print('No component to remove') ## EXCLUDE COMPONENTS ica.exclude = ecg_inds ica.apply(raw_data) ica.exclude = eog_inds ica.apply(raw_data) fig = raw_data.plot(show=False) # Plot the clean signal. report.add_figs_to_section(fig, captions='After filtering + ICA', section='Raw data') close(fig) ## SAVE PREPROCESSED FILE report.save(reportpath, open_browser=False, overwrite=True) raw_data.save(savepath, overwrite=False) return raw_data
def test_ica_eeg(): method = 'fastica' raw_fif = read_raw_fif(fif_fname, preload=True) with warnings.catch_warnings(record=True): # events etc. raw_eeglab = read_raw_eeglab(input_fname=eeglab_fname, montage=eeglab_montage, preload=True) for raw in [raw_fif, raw_eeglab]: events = make_fixed_length_events(raw, 99999, start=0, stop=0.3, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = [] picks_all.extend(picks_meg) picks_all.extend(picks_eeg) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with warnings.catch_warnings(record=True): # convergence ica.fit(inst, picks=picks) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) with warnings.catch_warnings(record=True): # misc channel raw = read_raw_ctf(ctf_fname2, preload=True) events = make_fixed_length_events(raw, 99999, start=0, stop=0.2, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = picks_meg + picks_eeg for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with warnings.catch_warnings(record=True): # convergence ica.fit(inst) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst)
def test_ica_full_data_recovery(): """Test recovery of full data when no source is rejected.""" # Most basic recovery raw = read_raw_fif(raw_fname).crop(0.5, stop).load_data() events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] with warnings.catch_warnings(record=True): # bad proj epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) evoked = epochs.average() n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() data_evoked = evoked.data raw.annotations = Annotations([0.5], [0.5], ['BAD']) for method in ['fastica']: stuff = [(2, n_channels, True), (2, n_channels // 2, False)] for n_components, n_pca_components, ok in stuff: ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1) with warnings.catch_warnings(record=True): ica.fit(raw, picks=list(range(n_channels))) raw2 = ica.apply(raw.copy(), exclude=[]) if ok: assert_allclose(data[:n_channels], raw2._data[:n_channels], rtol=1e-10, atol=1e-15) else: diff = np.abs(data[:n_channels] - raw2._data[:n_channels]) assert_true(np.max(diff) > 1e-14) ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components) with warnings.catch_warnings(record=True): ica.fit(epochs, picks=list(range(n_channels))) epochs2 = ica.apply(epochs.copy(), exclude=[]) data2 = epochs2.get_data()[:, :n_channels] if ok: assert_allclose(data_epochs[:, :n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(data_epochs[:, :n_channels] - data2) assert_true(np.max(diff) > 1e-14) evoked2 = ica.apply(evoked.copy(), exclude=[]) data2 = evoked2.data[:n_channels] if ok: assert_allclose(data_evoked[:n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(evoked.data[:n_channels] - data2) assert_true(np.max(diff) > 1e-14) assert_raises(ValueError, ICA, method='pizza-decomposision')
def ICA_pipeline( mne_array, regions, chans_to_plot=20, base_name="", exclude=None, skip_plots=False, ): """This is example code using mne.""" raw = mne_array filt_raw = raw.copy() filt_raw.load_data().filter(l_freq=1.0, h_freq=90) if not skip_plots: # Plot raw signal filt_raw.plot( n_channels=chans_to_plot, block=True, duration=50, show=True, clipping="transparent", title="Raw LFP Data from {}".format(base_name), remove_dc=False, scalings=dict(eeg=350e-6), ) ica = ICA(method="fastica", random_state=42) ica.fit(filt_raw) # ica.exclude = [4, 6, 12] raw.load_data() if exclude is None: # Plot raw ICAs ica.plot_sources(filt_raw) # Overlay ICA cleaned signal over raw. Seperate plot for each region. # TODO Add scroll bar or include window selection option. cont = input("Plot region overlay? (y|n) \n") if cont.strip().lower() == "y": reg_grps = [] for reg in set(regions): temp_grp = [] for ch in raw.info.ch_names: if reg in ch: temp_grp.append(ch) reg_grps.append(temp_grp) for grps in reg_grps: ica.plot_overlay(raw, stop=int(30 * 250), title="{}".format(grps[0][:3]), picks=grps) else: # ICAs to exclude ica.exclude = exclude if not skip_plots: ica.plot_sources(filt_raw) # Apply ICA exclusion reconst_raw = filt_raw.copy() exclude_raw = filt_raw.copy() print("ICAs excluded: ", ica.exclude) ica.apply(reconst_raw) if not skip_plots: # change exclude to all except chosen ICs all_ICs = list(range(ica.n_components_)) for i in ica.exclude: all_ICs.remove(i) ica.exclude = all_ICs ica.apply(exclude_raw) # Plot excluded ICAs exclude_raw.plot( block=True, show=True, clipping="transparent", duration=50, title="Excluded ICs from {}".format(base_name), remove_dc=False, scalings=dict(eeg=350e-6), ) # Plot reconstructed signals w/o excluded ICAs reconst_raw.plot( block=True, show=True, clipping="transparent", duration=50, title="Reconstructed LFP Data from {}".format(base_name), remove_dc=False, scalings=dict(eeg=350e-6), ) return reconst_raw
def runICA(raw,saveRoot,name): saveRoot = saveRoot icaList = [] ica = [] n_max_ecg = 3 # max number of ecg components # n_max_eog_1 = 2 # max number of vert eog comps # n_max_eog_2 = 2 # max number of horiz eog comps ecg_source_idx, ecg_scores, ecg_exclude = [], [], [] eog_source_idx, eog_scores, eog_exclude = [], [], [] #horiz = 1 # will later be modified to horiz = 0 if no horizontal EOG components are identified ica = ICA(n_components=0.90,n_pca_components=64,max_pca_components=100,noise_cov=None) ica.fit(raw) #************* eog_picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, eog=True, ecg=False, emg=False)[0] ecg_picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, ecg=True, eog=False, emg=False)[0] ica_picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, ecg=False, stim=False, exclude='bads') ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5, picks=ica_picks) ecg_evoked = ecg_epochs.average() eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5, picks=ica_picks).average() ecg_source_idx, ecg_scores = ica.find_bads_ecg(ecg_epochs, method='ctps') eog_source_idx, eog_scores = ica.find_bads_eog(raw,ch_name=raw.ch_names[eog_picks].encode('ascii', 'ignore')) # defining a title-frame for later use title = 'Sources related to %s artifacts (red)' # extracting number of ica-components and plotting their topographies source_idx = range(0, ica.n_components_) ica_plot = ica.plot_components(source_idx, ch_type="mag") # select ICA sources and reconstruct MEG signals, compute clean ERFs # Add detected artefact sources to exclusion list # We now add the eog artefacts to the ica.exclusion list if not ecg_source_idx: print("No ECG components above threshold were identified for subject " + name + " - selecting the component with the highest score under threshold") ecg_exclude = [np.absolute(ecg_scores).argmax()] ecg_source_idx=[np.absolute(ecg_scores).argmax()] elif ecg_source_idx: ecg_exclude += ecg_source_idx[:n_max_ecg] ica.exclude += ecg_exclude if not eog_source_idx: if np.absolute(eog_scores).any>0.3: eog_exclude=[np.absolute(eog_scores).argmax()] eog_source_idx=[np.absolute(eog_scores).argmax()] print("No EOG components above threshold were identified " + name + " - selecting the component with the highest score under threshold above 0.3") elif not np.absolute(eog_scores).any>0.3: eog_exclude=[] print("No EOG components above threshold were identified" + name) elif eog_source_idx: eog_exclude += eog_source_idx ica.exclude += eog_exclude print('########## saving') if len(eog_exclude) == 0: if len(ecg_exclude) == 0: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg_none' + '.pdf', format = 'pdf') elif len(ecg_exclude) == 1: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg' + map(str, ecg_exclude)[0] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 2: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 3: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '_' + map(str, ecg_exclude)[2] + '.pdf', format = 'pdf') elif len(eog_exclude) == 1: if len(ecg_exclude) == 0: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg_none' + '.pdf', format = 'pdf') elif len(ecg_exclude) == 1: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg' + map(str, ecg_exclude)[0] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 2: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 3: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '_' + map(str, ecg_exclude)[2] + '.pdf', format = 'pdf') elif len(eog_exclude) == 2: if len(ecg_exclude) == 0: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg_none' + '.pdf', format = 'pdf') elif len(ecg_exclude) == 1: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg' + map(str, ecg_exclude)[0] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 2: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 3: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '_' + map(str, ecg_exclude)[2] + '.pdf', format = 'pdf') # plot the scores for the different components highlighting in red that/those related to ECG scores_plots_ecg=ica.plot_scores(ecg_scores, exclude=ecg_source_idx, title=title % 'ecg') scores_plots_ecg.savefig(saveRoot + name + '_ecg_scores.pdf', format = 'pdf') scores_plots_eog=ica.plot_scores(eog_scores, exclude=eog_source_idx, title=title % 'eog') scores_plots_eog.savefig(saveRoot + name + '_eog_scores.pdf', format = 'pdf') source_source_ecg=ica.plot_sources(ecg_evoked, exclude=ecg_source_idx) source_source_ecg.savefig(saveRoot + name + '_ecg_source.pdf', format = 'pdf') #ax = plt.subplot(2,1,2) source_clean_ecg=ica.plot_overlay(ecg_evoked, exclude=ecg_source_idx) source_clean_ecg.savefig(saveRoot + name + '_ecg_clean.pdf', format = 'pdf') #clean_plot.savefig(saveRoot + name + '_ecg_clean.pdf', format = 'pdf') #if len(eog_exclude) > 0: source_source_eog=ica.plot_sources(eog_evoked, exclude=eog_source_idx) source_source_eog.savefig(saveRoot + name + '_eog_source.pdf', format = 'pdf') source_clean_eog=ica.plot_overlay(eog_evoked, exclude=eog_source_idx) source_clean_eog.savefig(saveRoot + name + '_eog_clean.pdf', format = 'pdf') overl_plot = ica.plot_overlay(raw) overl_plot.savefig(saveRoot + name + '_overl.pdf', format = 'pdf') event_id = 999 ecg_events, _, _ = mne.preprocessing.find_ecg_events(raw, event_id, ch_name=raw.ch_names[ecg_picks].encode('UTF8')) picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, ecg=True) tmin, tmax = -0.1, 0.1 epochs_ecg = mne.Epochs(raw, ecg_events, event_id, tmin, tmax,picks=picks) data_ecg = epochs_ecg.get_data() event_id = 998 eog_events = mne.preprocessing.find_eog_events(raw, event_id, ch_name=raw.ch_names[eog_picks].encode('UTF8')) picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, eog=True, exclude='bads') tmin, tmax = -0.5, 0.5 epochs_eog = mne.Epochs(raw, eog_events, event_id, tmin, tmax,picks=[picks[0]])#, data_eog = epochs_eog.get_data() plSize = 1 pltRes = 128 ecg_eogAvg=plt.figure(figsize=(20,10)) ax = plt.subplot(2,3,1) plt.plot(1e3 * epochs_ecg.times, np.squeeze(data_ecg).T) plt.xlabel('Times (ms)') plt.title('ECG') plt.ylabel('ECG') plt.show() ax2 = plt.subplot(2,3,2) plot_evoked_topomap(ecg_evoked, times=0, average=0.02, ch_type='mag',colorbar=False, size=plSize, res=pltRes, axes=ax2) ax3= plt.subplot(2,3,3) plot_evoked_topomap(ecg_evoked, times=0, average=0.02, ch_type='grad',colorbar=False, size=plSize, res=pltRes, axes=ax3) ax = plt.subplot(2,3,4) plt.plot(1e3 * epochs_eog.times, np.squeeze(data_eog).T) plt.xlabel('Times (ms)') plt.title('EOG') plt.ylabel('EOG') ax = plt.subplot(2,3,5) plot_evoked_topomap(eog_evoked, times=0, average=0.05, ch_type='mag',colorbar=False, size=plSize, res=pltRes, axes=ax) ax = plt.subplot(2,3,6) plot_evoked_topomap(eog_evoked, times=0, average=0.05, ch_type='grad',colorbar=False, size=plSize, res=pltRes, show=False, axes=ax) plt.tight_layout() ecg_eogAvg.savefig(saveRoot + name +'_ecg_eog_Avg.pdf',format = 'pdf') plt.close('all') ## restore sensor space data icaList = ica.apply(raw) return(icaList, ica)
if num in {88, 89, 92, 100}: continue for run in runs: raw = load_subject(num, run) fix_channels(raw) add_montage(raw) raw.pick_channels(ch_names) # Band-pass filter to capture the relevant signal (alpha, beta, # and mu ranges). Butterworth filter is implied by method='iir' # with iir_params=None or left out. raw.filter(7.0, 30.0, method='iir', n_jobs=n_cores) ica = ICA(n_components=0.95, random_state=random_state) ica.fit(raw, decim=3) ica.apply(raw) events = find_events(raw, consecutive=False) epochs = Epochs(raw, events, event_id, tmin, tmax, baseline=baseline, preload=True, proj=False) evoked_avg = [epochs[cond].average() for cond in ['left_fist', 'right_fist']] filename = splitext(raw.info['filename'])[0] epochs.save(filename + '-epo.fif') write_evokeds(filename + '-ave.fif', evoked_avg)
# ballistocardiac artifacts fig=ica.plot_scores(scores, title='ICA component scores', exclude=ecg_inds, show=True, axhline=0.4) # To improve your selection, inspect the ica components' # source signal time course and compare it to the average ecg artifact ica.plot_sources(ecg_average, exclude=ecg_inds) # If no fruther artifact rejection improvement is required, use the ica.apply # for ica components to be zeroed out and removed from the signal # Enter an array of bad indices in exclude to remove components # start and end arguments mark the first and last sample of the set to be # affected by the removal ica.apply(raw, exclude=[]) # Epoch and average data tmin, tmax = -0.2, 0.8 epochs = mne.Epochs(raw, events=events, event_id=event_id, picks=picks, tmin=tmin, tmax=tmax, preload=True, baseline=None) reward = epochs['reward'].average() no_reward = epochs['no_reward'].average() # Plot results reward.plot_joint() reward.plot_joint() # An alterntive approach for epoching would be to import the event file from
fig = ica.plot_scores(scores, exclude=eog_inds, title=title % ('eog', subject)) fig.savefig(save_folder + "pics/%s_%s_eog_scores.png" % (subject, condition)) fig = ica.plot_sources(eog_average, exclude=None) fig.savefig(save_folder + "pics/%s_%s_eog_source.png" % (subject, condition)) fig = ica.plot_components(ica.exclude, title=title % ('eog', subject), colorbar=True) fig.savefig(save_folder + "pics/%s_%s_eog_component.png" % (subject, condition)) fig = ica.plot_overlay(eog_average, exclude=None, show=False) fig.savefig(save_folder + "pics/%s_%s_eog_excluded.png" % (subject, condition)) # del eog_epochs, eog_average ########################################################################## # Apply the solution to Raw, Epochs or Evoked like this: raw_ica = ica.apply(raw) ica.save(save_folder + "%s_%s-ica.fif" % (subject, condition)) # save ICA # componenets # Save raw with ICA removed raw_ica.save(save_folder + "%s_%s_filtered_ica_mc_tsss-raw.fif" % ( subject, condition), overwrite=True)
eog_inds, eog_scores = ica.find_bads_eog(raw, ch_name='Fpz') #%% #Plotto le concentrazioni ica.plot_sources(raw) ica.plot_components() #PLotto le proprietà della singola componente ica.plot_properties(raw, dB=False,plot_std=False, picks=[0]) #%% DEFINIZIONE COMPONENTI #Definisco delle componenti da escludere exc = [1,0,12,11,18,29,28,36,34,49,47,45,63,51,52,53,56] attesa = [4,23,49] prot = [51,1,12, 36, 27] #%% RICOSTRUZIONE reconst_raw = raw.copy() ica.plot_overlay(reconst_raw, exclude=exc) ica.apply(reconst_raw, exclude=exc) reconst_raw.plot_psd(area_mode=None, show=False, average=False, fmin=1.0, fmax=80.0, dB=False, n_fft=160) #%% SPLITTO IL SEGNALE IN EPOCHE #Carico gli eventi dal canale annotations events, _ = events_from_annotations(reconst_raw, event_id=dict(T1=2, T2=3)) #Seleziono solo alcuni canali picks = pick_channels(reconst_raw.info["ch_names"], ["C3", "Cz", "C2"]) # Definisco onset e offset delle epoche (secondi) tmin, tmax = -1, 4 #Mappo i nomi degli eventi event_ids = dict(left=2, right=3) #Divido il tracciato in epoche epochs = Epochs(reconst_raw, events, event_ids, tmin - 0.5, tmax + 0.5, picks=picks, baseline=None, preload=True) #%% ERD #Selezione un range di frequenza freqs = np.arange(2, 50, 1)
def compute_ica(subject): """Function will compute ICA on raw and apply the ICA. params: subject : str the subject id to be loaded """ raw = Raw(save_folder + "%s_filtered_data_mc_raw_tsss.fif" % subject, preload=True) # ICA Part ica = ICA(n_components=0.95, method='fastica', max_iter=256) picks = mne.pick_types(raw.info, meg=True, eeg=True, stim=False, exclude='bads') ica.fit(raw, picks=picks, decim=decim, reject=reject) # maximum number of components to reject n_max_ecg, n_max_eog = 3, 1 ########################################################################## # 2) identify bad components by analyzing latent sources. title = 'Sources related to %s artifacts (red) for sub: %s' # generate ECG epochs use detection via phase statistics ecg_epochs = create_ecg_epochs(raw, ch_name="ECG002", tmin=-.5, tmax=.5, picks=picks) n_ecg_epochs_found = len(ecg_epochs.events) sel_ecg_epochs = np.arange(0, n_ecg_epochs_found, 10) ecg_epochs = ecg_epochs[sel_ecg_epochs] ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') fig = ica.plot_scores(scores, exclude=ecg_inds, title=title % ('ecg', subject)) fig.savefig(save_folder + "pics/%s_ecg_scores.png" % subject) if ecg_inds: show_picks = np.abs(scores).argsort()[::-1][:5] fig = ica.plot_sources(raw, show_picks, exclude=ecg_inds, title=title % ('ecg', subject), show=False) fig.savefig(save_folder + "pics/%s_ecg_sources.png" % subject) fig = ica.plot_components(ecg_inds, title=title % ('ecg', subject), colorbar=True) fig.savefig(save_folder + "pics/%s_ecg_component.png" % subject) ecg_inds = ecg_inds[:n_max_ecg] ica.exclude += ecg_inds # estimate average artifact ecg_evoked = ecg_epochs.average() del ecg_epochs # plot ECG sources + selection fig = ica.plot_sources(ecg_evoked, exclude=ecg_inds) fig.savefig(save_folder + "pics/%s_ecg_sources_ave.png" % subject) # plot ECG cleaning ica.plot_overlay(ecg_evoked, exclude=ecg_inds) fig.savefig(save_folder + "pics/%s_ecg_sources_clean_ave.png" % subject) # DETECT EOG BY CORRELATION # HORIZONTAL EOG eog_epochs = create_eog_epochs(raw, ch_name="EOG001") eog_inds, scores = ica.find_bads_eog(raw) fig = ica.plot_scores(scores, exclude=eog_inds, title=title % ('eog', subject)) fig.savefig(save_folder + "pics/%s_eog_scores.png" % subject) fig = ica.plot_components(eog_inds, title=title % ('eog', subject), colorbar=True) fig.savefig(save_folder + "pics/%s_eog_component.png" % subject) eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds del eog_epochs ########################################################################## # Apply the solution to Raw, Epochs or Evoked like this: raw_ica = ica.apply(raw, copy=False) ica.save(save_folder + "%s-ica.fif" % subject) # save ICA componenets # Save raw with ICA removed raw_ica.save(save_folder + "%s_filtered_ica_mc_raw_tsss.fif" % subject, overwrite=True) plt.close("all")
class MNEprepro(): """ Class to preproces CTF data Usage: raw_prepro = MNEprepro(subject, experiment, paths_dic) paths_dic = { "root": "~/Desktop/projects/MNE/data", "subj_anat": 'anatomy' "out": "~/Desktop/projects/MNE/data_prep" } subject = '18011014C' experiment = 'Movie' root = folder where subjects name folders are subj_anat = name anatomy folder in subject folder out = path to output files FOLDER EXAMPLE: /path2/MEG_data: /subject_name /experiment1.ds /experiment2.ds /anatomy /t1.nii.gz /cortical_surf_4k.gii /polhemus.pos STEPS to do 1- load the data 2- Load previous prepro info -> bad channels, mov, etc if any 3- plot and find bad channels 4- movement rejection 5- muscle rejection 6- ICA or SSP 7- epoch (save or no save) 7a- epoch based on photodiode 8- head model 8- do sensor level: Pow ERF Conn 9- source level: ... out directory: /bad_chn_annot /ICA """ def __init__(self, subject, experiment, paths_dic): self.subject = subject self.experiment = experiment self.pth_root = op.expanduser(paths_dic["root"]) self.pth_out = op.expanduser(paths_dic["out"]) self.pth_FS = op.expanduser(paths_dic["FS"]) mne.set_config('SUBJECTS_DIR', self.pth_FS) self.check_outdir() self.pth_subject = op.join(self.pth_root, subject) if self.check_MXfilter_sbjs() is True: print('Using MaxFilter preprocessed data') self.raw = mne.io.read_raw_fif(self.pth_raw, preload=False) else: self.pth_raw = glob.glob(op.join(self.pth_subject, subject) + '_' + experiment + '*')[-1] self.raw = mne.io.read_raw_ctf(self.pth_raw, preload=False) if self.raw.compensation_grade != 3: self.raw.apply_gradient_compensation(3) def check_outdir(self): from os import makedirs out_dir = self.pth_out self.out_bd_ch = op.join(out_dir, 'bad_chans') self.out_annot = op.join(out_dir, 'annots') self.out_ICAs = op.join(out_dir, 'ICAs') self.out_srcData = op.join(out_dir, 'data2src') makedirs(self.out_bd_ch, exist_ok=True) # I want to loop it makedirs(self.out_annot, exist_ok=True) makedirs(self.out_ICAs, exist_ok=True) makedirs(self.out_srcData, exist_ok=True) def check_MXfilter_sbjs(self): pth_mx_sbj = self.pth_out + '/MX_filter_subj/' + self.subject if op.exists(pth_mx_sbj): self.pth_raw = glob.glob(op.join(pth_mx_sbj, self.subject + '_' + self.experiment + '*'))[-1] is_MX = True else: is_MX = False return is_MX def detect_bad_channels(self, zscore_v=4, overwrite=False, method='both', neigh_max_distance=.035): """ zscore_v = zscore threshold, save_csv: path_tosaveCSV""" fname = self.subject + '_' + self.experiment + '_bads.csv' out_csv_f = op.join(self.out_bd_ch, fname) if op.exists(out_csv_f) and not overwrite: bad_chns = csv_read(out_csv_f) print('Reading from file, bad chans are:', bad_chns) else: from itertools import compress print('Looking for bad channels') # set recording length Fs = self.raw.info['sfreq'] t1x = 30 t2x = 220 t2 = min(self.raw.last_samp/Fs, t2x) t1 = max(0, t1x + t2-t2x) # Start earlier if recording is shorter # Get data raw_copy = self.raw.copy().crop(t1, t2).load_data() raw_copy = raw_copy.pick_types(meg=True, ref_meg=False)\ .filter(1, 45).resample(150, npad='auto') data_chans = raw_copy.get_data() # Get channel distances matrix chns_locs = np.asarray([x['loc'][:3] for x in raw_copy.info['chs']]) chns_dist = np.linalg.norm(chns_locs - chns_locs[:, None], axis=-1) chns_dist[chns_dist > neigh_max_distance] = 0 # Get avg channel uncorrelation between neighbours chns_corr = np.abs(np.corrcoef(data_chans)) weig = np.array(chns_dist, dtype=bool) chn_nei_corr = np.average(chns_corr, axis=1, weights=weig) chn_nei_uncorr_z = zscore(1-chn_nei_corr) # l ower corr higer Z # Get channel magnitudes max_Pow = np.sqrt(np.sum(data_chans ** 2, axis=1)) max_Z = zscore(max_Pow) if method == 'corr': # Based on local uncorrelation feat_vec = chn_nei_uncorr_z max_th = feat_vec > zscore_v elif method == 'norm': # Based on magnitude feat_vec = max_Z max_th = feat_vec > zscore_v elif method == 'both': # Combine uncorrelation with magnitude feat_vec = (chn_nei_uncorr_z+max_Z)/2 max_th = (feat_vec) > zscore_v bad_chns = list(compress(raw_copy.info['ch_names'], max_th)) raw_copy.info['bads'] = bad_chns if bad_chns: print(['Plotting data,bad chans are:'] + bad_chns) pfig(), pplot(feat_vec), plt.axhline(zscore_v) plt.title(['Plotting data,bad chans are:'] + bad_chns) raw_copy.plot(n_channels=100, block=True, bad_color='r') bad_chns = raw_copy.info['bads'] print('Bad chans are:', bad_chns) else: print('No bad chans found') csv_save(bad_chns, out_csv_f) self.ch_max_Z = max_Z self.raw.info['bads'] = bad_chns def detect_movement(self, thr_mov=.01, plot=True, overwrite=False, save=True): from mne.transforms import read_trans fname = self.subject + '_' + self.experiment + '_mov.txt' out_csv_f = op.join(self.out_annot, fname) fname_t = self.subject + '_' + self.experiment + '_dev2head-trans.fif' out_csv_f_t = op.join(self.out_annot, fname_t) if op.exists(out_csv_f) and not overwrite: mov_annot = read_annotations(out_csv_f) print('Reading from file, mov segments are:', mov_annot) print('Reading from file, dev to head transformation') dev_head_t = read_trans(out_csv_f_t) else: print('Calculating head pos') pos = mne.chpi._calculate_head_pos_ctf(self.raw, gof_limit=-1) mov_annot, hpi_disp, dev_head_t = annotate_motion(self.raw, pos, thr=thr_mov) if plot is True: plt.figure() plt.plot(hpi_disp) plt.axhline(y=thr_mov, color='r') plt.show(block=True) if save is True: mov_annot.save(out_csv_f) dev_head_t.save(out_csv_f_t) #fig.savefig(out_csv_f[:-4]+'.png') old_annot = self.raw.annotations # if orig_time cant + with none time self.raw.set_annotations(mov_annot) self.raw.set_annotations(self.raw.annotations + old_annot) self.raw.info['dev_head_t_old'] = self.raw.info['dev_head_t'] self.raw.info['dev_head_t'] = dev_head_t self.annot_movement = mov_annot def detect_muscle(self, thr=1.5, t_min=.5, plot=True, overwrite=False): """Find and annotate mucsle artifacts - by Luke Bloy""" fname = self.subject + '_' + self.experiment + '_mus.txt' out_csv_f = op.join(self.out_annot, fname) if op.exists(out_csv_f) and not overwrite: mus_annot = read_annotations(out_csv_f) print('Reading from file, muscle segments are:', mus_annot) else: print('Calculating muscle artifacts') raw = self.raw.copy().load_data() raw.pick_types(meg=True, ref_meg=False) raw.notch_filter(np.arange(60, 241, 60), fir_design='firwin') raw.filter(110, 140, fir_design='firwin') raw.apply_hilbert(envelope=True) sfreq = raw.info['sfreq'] art_scores = stats.zscore(raw._data, axis=1) # band pass filter the data art_scores_filt = mne.filter.filter_data(art_scores.mean(axis=0), sfreq, None, 4) art_mask = art_scores_filt > thr # remove artifact free periods shorter than t_min idx_min = t_min * sfreq comps, num_comps = label(art_mask == 0) for l in range(1, num_comps+1): l_idx = np.nonzero(comps == l)[0] if len(l_idx) < idx_min: art_mask[l_idx] = True mus_annot = _annotations_from_mask(raw.times, art_mask, 'Bad-muscle') if plot: del raw print('Plotting data, mark or delete art, by pressing a \n' 'Marked or demarked channels will be saved') old_bd_chns = self.raw.info['bads'] raw = self.raw.copy().load_data().pick_types(meg=True, ref_meg=False) raw.notch_filter(np.arange(60, 181, 60), fir_design='firwin') raw.filter(1, 140) old_annot = raw.annotations # if orig_time cant + none raw.set_annotations(mus_annot) raw.set_annotations(raw.annotations + old_annot) raw.plot(n_channels=140, block=True, bad_color='r') mus_annot = raw.annotations if not (old_bd_chns == raw.info['bads']): bad_chns = raw.info['bads'] print('Saving new bad channels list \n ') print('Bad chans are:', bad_chns) fname = self.subject + '_' + self.experiment + '_bads.csv' csv_save(bad_chns, op.join(self.out_bd_ch, fname)) mus_annot.save(out_csv_f) old_annot = self.raw.annotations # if orig_time cant + with none time self.raw.set_annotations(mus_annot) self.raw.set_annotations(self.raw.annotations + old_annot) self.annot_muscle = mus_annot def run_ICA(self, overwrite=False): fname = self.subject + '_' + self.experiment + '-ica.fif.gz' out_fname = op.join(self.out_ICAs, fname) if op.exists(out_fname) and not overwrite: self.ica = mne.preprocessing.read_ica(out_fname) else: from mne.preprocessing import ICA raw_copy = self.raw.copy().load_data().filter(1, 45) self.ica = ICA(method='fastica', random_state=42, n_components=0.99, max_iter=1000) picks = mne.pick_types(raw_copy.info, meg=True, ref_meg=False, stim=False, exclude='bads') reject = dict(grad=4000e-13, mag=6e-12) # what rejec intervals? self.ica.fit(raw_copy, picks=picks, reject=reject, decim=3) self.ica.detect_artifacts(raw_copy) self.ica.done = False self.ica.save(out_fname) def plot_ICA(self, check_if_done=True, overwrite=False): fname = self.subject + '_' + self.experiment + '-ica.fif.gz' out_fname = op.join(self.out_ICAs, fname) # Load previous ICA instance if op.exists(out_fname) and not overwrite: self.ica = mne.preprocessing.read_ica(out_fname) else: # self.run_ICA(self) return # Check if ICA comps were inspected data_not_clean = True if check_if_done is True: if self.ica.info['description'] == 'done': data_not_clean = False # Plot interactively to select bad comps if data_not_clean is True: raw_copy = self.raw.copy().load_data().filter(1, 45) while data_not_clean is True: # ICA comp plotting self.ica.plot_components(inst=raw_copy) self.ica.plot_sources(raw_copy, block=True) # Clean and raw sensor plotting raw_plot = raw_copy.copy().pick_types(meg=True, ref_meg=False) raw_plot.plot(n_channels=80, title='NO ICA') raw_ica = raw_copy.copy().pick_types(meg=True, ref_meg=False) self.ica.apply(raw_ica) raw_ica.plot(n_channels=80, title='ICA cleaned', block=True) data_not_clean = bool(int(input("Select other ICA components? " "[0-no, 1-yes]: "))) if data_not_clean is False: self.ica.info['description'] = 'done' self.ica.save(out_fname) #self.ica.apply(self.raw.load_data()) def get_events(self, plot=False, movie_annot=None): # general description of the data raw_copy = self.raw.copy() task = self.experiment fs = raw_copy.info['sfreq'] time = raw_copy.buffer_size_sec N_samples = raw_copy.n_times ID = self.subject print('Data for subject ' + ID + ' is composed from') print(str(N_samples) + ' samples with ' + str(fs) + ' Hz sampl rate') print('Total time = ' + str(time) + 's') # get photodiode events from CTF data PD_ts, Ind_PD_ON, Ind_PD_OFF, T_PD = get_photodiode_events(raw_copy, fs) # pick Trigger channel time series from CTF data Trig = mne.io.pick.pick_channels_regexp(raw_copy.info['ch_names'], 'UPPT001') Trig_ts = raw_copy.get_data(picks=Trig) # get events from trigger channel events_trig = mne.find_events(raw_copy, stim_channel='UPPT001', shortest_event=1) print(str(len(Ind_PD_ON)) + ' PD ONSETS FOUND') if task == 'CarTask': event_id = {'Transp/H2L': 10, 'Transp/L2H': 20, 'NotTransp/H2L': 30, 'NotTransp/L2H': 40} # get trigger names for PD ON states events = get_triger_names_PD(event_id, Ind_PD_ON, events_trig) all_trl_info, col_info = get_all_trl_info(event_id, Ind_PD_ON, Ind_PD_OFF, events_trig) self.all_trl_info = all_trl_info self.all_trl_info_col_names = col_info # get different event length for each condition event_len = {'Transp/H2L': [-.86, 2.42], 'Transp/L2H': [-3.17, 2.3], 'NotTransp/H2L': [-.9, 2.37], 'NotTransp/L2H': [-3.17, 2.1]} self.event_len = event_len elif task == 'Movie': if movie_annot is not None: if fs != 600: raise ValueError('Sampling must be 600Hz') event_id, events = get_pd_annotations(Ind_PD_ON, events_trig, movie_annot) else: event_id = {'SceneOnset': 1} events = np.zeros((len(Ind_PD_ON), 3)) events[:, 0] = Ind_PD_ON events[:, 2] = 1 all_trl_info = None elif task == 'Flanker': event_id = {'Incongruent/Left': 3, 'Incongruent/Right': 5, 'Congruent/Left': 4, 'Congruent/Right': 6, 'Reward/Face': 7, 'Reward/Coin': 8, 'Reward/Neut_FaceTRL': 9, 'Reward/Neut_CoinTRL': 10} # get trigger names for PD ON states events = get_triger_names_PD(event_id, Ind_PD_ON, events_trig) all_trl_info = None events = events.astype(np.int64) # plot trig and PD if plot: plt.figure() plot_events(PD_ts, Ind_PD_ON, T_PD, Ind_PD_OFF, Trig_ts, events, task, ID, all_trl_info=all_trl_info) self.event_id = event_id self.events = events def epoching(self, tmin=-0.5, tmax=0.5, plot=False, f_min=1, f_max=45, overwrite=False, apply_ica=True, cond_name=None, movie_annot=None, save=True): if cond_name is not None: fname = "%s_%s_%s-epo.fif" % (self.subject, self.experiment, cond_name.replace('/', '')) else: fname = "%s_%s-epo.fif" % (self.subject, self.experiment) out_fname = self.out_srcData + '/' + fname if op.exists(out_fname) and not overwrite: print('Reading epoched data from file') self.epochs = mne.read_epochs(out_fname) else: self.get_events(plot, movie_annot) raw_copy = self.raw.copy().load_data() info = raw_copy.info picks = mne.pick_types(info, meg=True, ref_meg=False) stim_ch = ['UADC015-3007', 'UPPT001'] pick_stim = mne.pick_channels(info['ch_names'], stim_ch) pick_epo = np.append(picks, pick_stim) raw_copy.pick(pick_epo) pick_fil = mne.pick_types(raw_copy.info, meg=True, ref_meg=False) raw_copy.filter(f_min, f_max, picks=pick_fil) if cond_name is not None: # Different conds diff lengths event = self.events ids = self.event_id[cond_name] tmin, tmax = self.event_len[cond_name] events = event[event[:, 2] == ids] epochs = mne.Epochs(raw_copy, events=events, tmin=tmin, tmax=tmax, event_id={cond_name: ids}, baseline=(tmin, 0.0)).load_data() else: epochs = mne.Epochs(raw_copy, events=self.events, tmin=tmin, tmax=tmax, event_id=self.event_id, baseline=(tmin, 0.0)).load_data() self.epochs = epochs if apply_ica is True: if hasattr(self, 'ica'): # Do ICA only on meg chns piks = mne.pick_types(epochs.info, meg=True) epochs_data = epochs.copy().pick(piks) self.ica.apply(epochs_data) self.epochs._data[:, piks, :] = epochs_data._data else: return self.epochs.save(out_fname, overwrite=overwrite) def src_modelling(self, spacing=['oct5'], overwrite=False): from mne import (read_forward_solution, make_forward_solution, write_forward_solution, setup_source_space) subject = self.subject task = self.experiment mne.set_config('SUBJECTS_DIR', self.pth_FS) FS_subj = op.join(self.pth_FS, subject) fname_trans = op.join(FS_subj, subject + '-trans.fif') fname_bem = op.join(FS_subj, '%s-bem_sol.fif' % subject) if not op.exists(fname_bem) or overwrite: # make_watershed_bem already run in the sh script # mne.bem.make_watershed_bem(subject, overwrite=True, # volume='T1', atlas=True, gcaatlas=False, # preflood=None) model = mne.make_bem_model(subject, ico=4, conductivity=[0.3]) bem = mne.make_bem_solution(model) mne.write_bem_solution(fname_bem, bem) else: bem = mne.read_bem_solution(fname_bem) for space in spacing: fname_src = op.join(FS_subj, 'bem', '%s-src.fif' % space) bname_fwd = '%s_%s_%s-fwd.fif' % (subject, task, space) fname_fwd = op.join(self.out_srcData, bname_fwd) if not op.exists(fname_src) or overwrite: src = setup_source_space(subject, space, subjects_dir=self.pth_FS) src.save(fname_src, overwrite=overwrite) if op.exists(fname_fwd) and not overwrite: self.fwd = read_forward_solution(fname_fwd) else: self.fwd = make_forward_solution(self.raw.info, fname_trans, fname_src, fname_bem) write_forward_solution(fname_fwd, self.fwd, overwrite) def mne_cov(self, overwrite=False): from mne import compute_covariance, read_cov fname = self.subject + '_' + self.experiment + '_ncov-cov.fif.gz' out_fname = op.join(self.out_srcData, fname) if op.exists(out_fname) and not overwrite: print('Reading noise covariance from file') self.ncov = read_cov(out_fname) else: self.ncov = compute_covariance(self.epochs, tmax=0, method='shrunk') self.ncov.save(out_fname) def mne_inv_operator(self, overwrite=False): from mne.minimum_norm import (read_inverse_operator, make_inverse_operator, write_inverse_operator) fname = self.subject + '_' + self.experiment + '_mne-inv.fif.gz' out_fname = op.join(self.out_srcData, fname) if op.exists(out_fname) and not overwrite: print('Reading inverse operator from file') self.inv = read_inverse_operator(out_fname) else: self.inv = make_inverse_operator(self.epochs.info, self.fwd, self.ncov, loose=0.2, depth=0.8) write_inverse_operator(out_fname, self.inv)
# from now on the ICA will reject this component even if no exclude # parameter is passed, and this information will be stored to disk # on saving # uncomment this for reading and writing # ica.save('my-ica.fif') # ica = read_ica('my-ica.fif') ############################################################################### # Note that nothing is yet removed from the raw data. To remove the effects of # the rejected components, # :meth:`the apply method <mne.preprocessing.ICA.apply>` must be called. # Here we apply it on the copy of the first ten seconds, so that the rest of # this tutorial still works as intended. raw_copy = raw.copy().crop(0, 10) ica.apply(raw_copy) raw_copy.plot() # check the result ############################################################################### # Exercise: find and remove ECG artifacts using ICA! ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') ica.plot_properties(ecg_epochs, picks=ecg_inds, psd_args={'fmax': 35.}) ############################################################################### # What if we don't have an EOG channel? # ------------------------------------- # # We could either: # # 1. make a bipolar reference from frontal EEG sensors and use as virtual EOG
def test_ica_core(method): """Test ICA on raw and epochs.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() # XXX. The None cases helped revealing bugs but are time consuming. test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [2, 1.0] # for future dbg add cases max_pca_components = [3] picks_ = [picks] methods = [method] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_, methods) # # test init catchers pytest.raises(ValueError, ICA, n_components=3, max_pca_components=2) pytest.raises(ValueError, ICA, n_components=2.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks, method in iter_ica_params: # Test ICA raw ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method, max_iter=1) pytest.raises(ValueError, ica.__contains__, 'mag') print(ica) # to test repr # test fit checker pytest.raises(RuntimeError, ica.get_sources, raw) pytest.raises(RuntimeError, ica.get_sources, epochs) # test decomposition with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=pcks, start=start, stop=stop) repr(ica) # to test repr assert ('mag' in ica) # should now work without error # test re-fit unmixing1 = ica.unmixing_matrix_ with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=pcks, start=start, stop=stop) assert_array_almost_equal(unmixing1, ica.unmixing_matrix_) raw_sources = ica.get_sources(raw) # test for #3804 assert_equal(raw_sources._filenames, [None]) print(raw_sources) # test for gh-6271 (scaling of ICA traces) fig = raw_sources.plot() assert len(fig.axes[0].lines) in (4, 5) for line in fig.axes[0].lines[1:-1]: # first and last are markers y = line.get_ydata() assert np.ptp(y) < 10 plt.close('all') sources = raw_sources[:, :][0] assert (sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3.preload = False pytest.raises(RuntimeError, ica.apply, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method) with pytest.warns(None): # sometimes warns ica.fit(epochs, picks=picks) data = epochs.get_data()[:, 0, :] n_samples = np.prod(data.shape) assert_equal(ica.n_samples_, n_samples) print(ica) # to test repr sources = ica.get_sources(epochs).get_data() assert (sources.shape[1] == ica.n_components_) pytest.raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False pytest.raises(RuntimeError, ica.apply, epochs3, include=[1, 2]) # test for bug with whitener updating _pre_whitener = ica.pre_whitener_.copy() epochs._data[:, 0, 10:15] *= 1e12 ica.apply(epochs.copy()) assert_array_equal(_pre_whitener, ica.pre_whitener_) # test expl. var threshold leading to empty sel ica.n_components = 0.1 pytest.raises(RuntimeError, ica.fit, epochs) offender = 1, 2, 3, pytest.raises(ValueError, ica.get_sources, offender) pytest.raises(TypeError, ica.fit, offender) pytest.raises(TypeError, ica.apply, offender)
class Preprocessing(): #--------------------------------------------------------------------------------------------------------------------------------- # VARIABLES #--------------------------------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------------- # PROCESSES AND FUNCTIONS #--------------------------------------------------------------------------------------------------------------------------------- """******************************************************************* * Name: __init__ * Function: Constructor of the class * Parameters: self: instance of the class * data_file: file to load where there is the raw data * Return: - *****************************************************************""" def __init__(self, data_file): self.DATA_FILE = data_file self._raw_original = mne.io.read_raw_egi(self.DATA_FILE) self.raw = self._raw_original self._raw_notch = None self.picks = None """******************************************************************* * Name: decrease_time * Function: Selects the time and channels to work with * Parameters: self: instance of the class * Return: - *****************************************************************""" def decrease_time_channels(self): print(Data.INF_DECREASE_DATA) #Ask the user if wants to decrease the time to visualize if (input(Data.Q_TIME_SELECT).lower() == User_answers.YES): #And the cutoff frequencies init_time = 0 final_time = -1 #While the values are not correct while (init_time > final_time): #Ask the init time init_time = int(input(Data.Q_INIT_DATA_TIME)) #And the end final_time = int(input(Data.Q_FINAL_DATA_TIME)) if (init_time > final_time): print(Data.KO_DATA_TIMES) #And crops data self.raw.crop(init_time, final_time).load_data() if (not (input(Data.Q_ALL_DATA).lower() == User_answers.YES)): self.picks = list(input(Data.Q_WHAT_DATA_2_LOAD).split()) """******************************************************************* * Name: notch_filter * Function: Applies a notch filter to the data * Parameters: self: instance of the class * Return: APPLY_NOTCH_FILTER if applied and DONT_APPLY_NOTCH_FILTER * otherwise *****************************************************************""" def notch_filter(self): #Ask if a notch filter is required if (input( Notch_filter.Q_NEED_NOTCH_FILTER).lower() == User_answers.YES): freq = int(self.raw.info['sfreq']) #And ask the frequenciy that wants to attenuate while (freq >= int(self.raw.info['sfreq'] / 2)): freq = int(input(Notch_filter.Q_NF_FC)) #If the frequency is higher than Nyquist if (freq >= int(self.raw.info['sfreq'] / 2)): #We show an error print( Notch_filter.KO_NF_VALUE.format( bcolors.FAIL, int(self.raw.info['sfreq']) / 2, bcolors.ENDC)) #Indicate that it is all ok print(Notch_filter.OK_NF_VALUE) #Create an array with the frequency and its multiples freqs_notch = (freq, freq * 2, freq * 3, freq * 4) #Apply the filter self._raw_notch = self.raw.copy().notch_filter(freqs=freqs_notch, picks=self.picks) #And actualizate the raw data self.raw = self._raw_notch return Notch_filter.APPLY_NOTCH_FILTER return Notch_filter.DONT_APPLY_NOTCH_FILTER """****************************************************************** * Name: ica_filter * Function: Asks to user if an ica filter is needed and applies * it if so * Parameters: self: instance of the class * v: class for the visualization * Return: - *****************************************************************""" def ica_filter(self, v): #Ask if an ica filter is required if (input(ICA_filter.Q_NEED_ICA_FILTER).lower() == User_answers.YES): #Ask the number of components to use num_components = int(input(ICA_filter.Q_NUM_COMPONENTS)) #We create a copy of the data self.filt_raw = self.raw.copy() self.filt_raw.load_data() #And create the ica components self.ica = ICA(n_components=num_components) #And train it self.ica.fit(self.filt_raw) #We show the ica components v.plot_ICA(self.ica, self.raw) #User selects the components components = list( map(int, input(ICA_filter.Q_ICA_COMPONENTS_2_USE).split())) #Select the components self.ica.exclude = components #And apply the filter self.reconst_ica_raw = self.raw.copy() self.ica.apply(self.reconst_ica_raw) v.plot_data(self.reconst_ica_raw) self.raw = self.reconst_ica_raw #TODO: crec que no calen els return return ICA_filter.APPLY_ICA_FILTER return ICA_filter.DONT_APPLY_ICA_FILTER
# from now on the ICA will reject this component even if no exclude # parameter is passed, and this information will be stored to disk # on saving # uncomment this for reading and writing # ica.save('my-ica.fif') # ica = read_ica('my-ica.fif') ############################################################################### # Note that nothing is yet removed from the raw data. To remove the effects of # the rejected components, # :meth:`the apply method <mne.preprocessing.ICA.apply>` must be called. # Here we apply it on the copy of the first ten seconds, so that the rest of # this tutorial still works as intended. raw_copy = raw.copy().crop(0, 10) ica.apply(raw_copy) raw_copy.plot() # check the result ############################################################################### # Exercise: find and remove ECG artifacts using ICA! ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') ica.plot_properties(ecg_epochs, picks=ecg_inds, psd_args={'fmax': 35.}) ############################################################################### # What if we don't have an EOG channel? # ------------------------------------- # # We could either: # # 1. make a bipolar reference from frontal EEG sensors and use as virtual EOG
def applyICA(self, raw, ica_fit, method='extended-infomax', decim=None, fit_params=None, inspect=True): ''' Arguments - - - - - self(object): Epochs object raw (object): n_components (): method (str): decim (): Returns - - - - self ''' # make sure that bad electrodes and 'good' epochs match between both data sets ica_fit.info['bads'] = self.info['bads'] if str(type(ica_fit))[-3] == 's': print('fitting data on epochs object') to_drop = [ i for i, v in enumerate(ica_fit.selection) if v not in self.selection ] ica_fit.drop(to_drop) # initiate ica logging.info('Started ICA') picks = mne.pick_types(self.info, eeg=True, exclude='bads') ica = ICA(n_components=picks.size, method=method, fit_params=fit_params) # ica is fitted on epoched data ica.fit(ica_fit, picks=picks, decim=decim) # plot the components ica.plot_components(colorbar=True, picks=range(picks.size), show=False) plt.savefig( self.FolderTracker(extension=[ 'preprocessing', 'subject-{}'.format(self.sj), self.session ], filename='components.pdf')) plt.close() # advanced artifact detection eog_epochs = create_eog_epochs(raw, baseline=(None, None)) eog_inds_a, scores = ica.find_bads_eog(eog_epochs) ica.plot_scores(scores, exclude=eog_inds_a, show=False) plt.savefig( self.FolderTracker(extension=[ 'preprocessing', 'subject-{}'.format(self.sj), self.session ], filename='ica_scores.pdf')) plt.close() # diagnostic plotting ica.plot_sources(self, show_scrollbars=False, show=False) if inspect: plt.show() else: plt.savefig( self.FolderTracker(extension=[ 'preprocessing', 'subject-{}'.format(self.sj), self.session ], filename='sources.pdf')) plt.close() # double check selected component with user input time.sleep(5) tcflush(sys.stdin, TCIFLUSH) print('You are preprocessing subject {}, session {}'.format( self.sj, self.session)) conf = input( 'Advanced detection selected component(s) {}. Do you agree (y/n)'. format(eog_inds_a)) if conf == 'y': eog_inds = eog_inds_a else: eog_inds = [] nr_comp = input('How many components do you want to select (<10)?') for i in range(int(nr_comp)): eog_inds.append( int(input('What is component nr {}?'.format(i + 1)))) for i, cmpt in enumerate(eog_inds): ica.plot_properties(self, picks=cmpt, psd_args={'fmax': 35.}, image_args={'sigma': 1.}, show=False) plt.savefig( self.FolderTracker(extension=[ 'preprocessing', 'subject-{}'.format(self.sj), self.session ], filename='property{}.pdf'.format(cmpt))) plt.close() ica.plot_overlay( raw, exclude=eog_inds, picks=[ self.ch_names.index(e) for e in ['Fp1', 'Fpz', 'Fp2', 'AF7', 'AF3', 'AFz', 'AF4', 'AF8'] ], show=False) plt.savefig( self.FolderTracker(extension=[ 'preprocessing', 'subject-{}'.format(self.sj), self.session ], filename='ica-frontal.pdf')) plt.close() ica.plot_overlay( raw, exclude=eog_inds, picks=[ self.ch_names.index(e) for e in ['PO7', 'PO8', 'PO3', 'PO4', 'O1', 'O2', 'POz', 'Oz', 'Iz'] ], show=False) plt.savefig( self.FolderTracker(extension=[ 'preprocessing', 'subject-{}'.format(self.sj), self.session ], filename='ica-posterior.pdf')) plt.close() # remove selected component ica.apply(self, exclude=eog_inds) logging.info( 'The following components were removed from raw eeg with ica: {}'. format(eog_inds))
def _compute_ica(fif_file, ecg_ch_name, eog_ch_name, n_components, reject): """Compute ica solution.""" subj_path, basename, ext = split_filename(fif_file) raw = read_raw_fif(fif_file, preload=True) # select sensors select_sensors = pick_types(raw.info, meg=True, ref_meg=False, exclude='bads') # 1) Fit ICA model using the FastICA algorithm # Other available choices are `infomax` or `extended-infomax` # We pass a float value between 0 and 1 to select n_components based on the # percentage of variance explained by the PCA components. flat = dict(mag=1e-13, grad=1e-13) ica = ICA(n_components=n_components, method='fastica', max_iter=500) ica.fit(raw, picks=select_sensors, reject=reject, flat=flat) # -------------------- Save ica timeseries ---------------------------- # ica_ts_file = os.path.abspath(basename + "_ica-tseries.fif") ica_src = ica.get_sources(raw) ica_src.save(ica_ts_file, overwrite=True) ica_src = None # --------------------------------------------------------------------- # # 2) identify bad components by analyzing latent sources. # generate ECG epochs use detection via phase statistics # if we just have exclude channels we jump these steps n_max_ecg = 3 n_max_eog = 2 # check if ecg_ch_name is in the raw channels if ecg_ch_name in raw.info['ch_names']: raw.set_channel_types({ecg_ch_name: 'ecg'}) else: ecg_ch_name = None # set ref_meg to 'auto' select_sensors = pick_types(raw.info, meg=True, ref_meg='auto', exclude='bads') ecg_epochs = create_ecg_epochs(raw, tmin=-0.5, tmax=0.5, picks=select_sensors, ch_name=ecg_ch_name) ecg_inds, ecg_scores = ica.find_bads_ecg(ecg_epochs, method='ctps') ecg_evoked = ecg_epochs.average() ecg_epochs = None ecg_inds = ecg_inds[:n_max_ecg] ica.exclude += ecg_inds eog_ch_name = eog_ch_name.replace(' ', '') if set(eog_ch_name.split(',')).issubset(set(raw.info['ch_names'])): print('*** EOG CHANNELS FOUND ***') eog_inds, eog_scores = ica.find_bads_eog(raw, ch_name=eog_ch_name) eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds eog_evoked = create_eog_epochs(raw, tmin=-0.5, tmax=0.5, picks=select_sensors, ch_name=eog_ch_name).average() else: print('*** NO EOG CHANNELS FOUND!!! ***') eog_inds = eog_scores = eog_evoked = None report_file = _generate_report(raw=raw, ica=ica, subj_name=fif_file, basename=basename, ecg_evoked=ecg_evoked, ecg_scores=ecg_scores, ecg_inds=ecg_inds, ecg_ch_name=ecg_ch_name, eog_evoked=eog_evoked, eog_scores=eog_scores, eog_inds=eog_inds, eog_ch_name=eog_ch_name) report_file = os.path.abspath(report_file) ica_sol_file = os.path.abspath(basename + '_ica_solution.fif') ica.save(ica_sol_file) raw_ica = ica.apply(raw) raw_ica_file = os.path.abspath(basename + '_ica' + ext) raw_ica.save(raw_ica_file, overwrite=True) return raw_ica_file, ica_sol_file, ica_ts_file, report_file
picks=picks, baseline=baseline, preload=True, reject=reject) # Fit ICA, find and remove major artifacts ica = ICA(n_components=0.95, random_state=0).fit(raw, decim=1, reject=reject) # compute correlation scores, get bad indices sorted by score eog_epochs = create_eog_epochs(raw, ch_name='MRT31-2908', reject=reject) eog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name='MRT31-2908') ica.plot_scores(eog_scores, eog_inds) # see scores the selection is based on ica.plot_components(eog_inds) # view topographic sensitivity of components ica.exclude += eog_inds[:1] # we saw the 2nd ECG component looked too dipolar ica.plot_overlay(eog_epochs.average()) # inspect artifact removal ica.apply(epochs) # clean data, default in place evoked = [epochs[k].average() for k in event_ids] contrast = combine_evoked(evoked, weights=[-1, 1]) # Faces - scrambled evoked.append(contrast) for e in evoked: e.plot(ylim=dict(mag=[-400, 400])) plt.show() # estimate noise covarariance noise_cov = mne.compute_covariance(epochs, tmax=0, method='shrunk')
def raw_preprocessing(self, raw, use_ica=True, use_ssp=True): # Filter raw.info['bads'] = self.bad_channels # Remove power-line noise raw.notch_filter(np.arange(60, 241, 60), picks=self.picks, filter_length='auto', phase=self.phase) raw.filter(l_freq=self.l_freq, h_freq=self.h_freq, phase=self.phase, fir_window=self.fir_window, fir_design=self.fir_design) # Add EEG reference raw.set_eeg_reference(projection=True) # TODO: some mechanism to control this if use_ica: # supported ica_channels = ['mag', 'grad', 'eeg', 'seeg', 'ecog', 'hbo', 'hbr', 'eog'] ica_picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude='bads') n_components = 25 decim = 3 # maximum number of components to reject n_max_ecg, n_max_eog = 3, 1 # here we don't expect horizontal EOG components # ica = run_ica(raw, n_components=0.95) ica = ICA(n_components=n_components, method='fastica', noise_cov=None) ica.fit(raw, decim=decim, picks=ica_picks, reject=self.reject) # generate ECG epochs use detection via phase statistics ecg_epochs = create_ecg_epochs(raw, reject=self.reject) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') ecg_inds = ecg_inds[:n_max_ecg] ica.exclude += ecg_inds # detect EOG by correlation eog_inds, scores = ica.find_bads_eog(raw) eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds ica.apply(raw) if use_ssp: if self.ecg: ecg_projs, _ = compute_proj_ecg(raw) raw.info['projs'] += ecg_projs if self.blink: eog_projs, _ = compute_proj_eog(raw) raw.info['projs'] += eog_projs raw.apply_proj() return raw
def test_ica_additional(): """Test additional ICA functionality""" tempdir = _TempDir() stop2 = 500 raw = Raw(raw_fname).crop(1.5, stop, copy=False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) # test if n_components=None works with warnings.catch_warnings(record=True): ica = ICA(n_components=None, max_pca_components=None, n_pca_components=None, random_state=0) ica.fit(epochs, picks=picks, decim=3) # for testing eog functionality picks2 = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=True, exclude='bads') epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2, baseline=(None, 0), preload=True) test_cov2 = test_cov.copy() ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4, n_pca_components=4) assert_true(ica.info is None) with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5]) assert_true(isinstance(ica.info, Info)) assert_true(ica.n_components_ < 5) ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) assert_raises(RuntimeError, ica.save, '') with warnings.catch_warnings(record=True): ica.fit(raw, picks=[1, 2, 3, 4, 5], start=start, stop=stop2) # test corrmap ica2 = ica.copy() ica3 = ica.copy() corrmap([ica, ica2], (0, 0), threshold='auto', label='blinks', plot=True, ch_type="mag") corrmap([ica, ica2], (0, 0), threshold=2, plot=False, show=False) assert_true(ica.labels_["blinks"] == ica2.labels_["blinks"]) assert_true(0 in ica.labels_["blinks"]) template = _get_ica_map(ica)[0] corrmap([ica, ica3], template, threshold='auto', label='blinks', plot=True, ch_type="mag") assert_true(ica2.labels_["blinks"] == ica3.labels_["blinks"]) plt.close('all') # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ica_badname = op.join(op.dirname(tempdir), 'test-bad-name.fif.gz') ica.save(ica_badname) read_ica(ica_badname) assert_naming(w, 'test_ica.py', 2) # test decim ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw_ = raw.copy() for _ in range(3): raw_.append(raw_) n_samples = raw_._data.shape[1] with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(raw_._data.shape[1], n_samples) # test expl var ica = ICA(n_components=1.0, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(ica.n_components_ == 4) # epochs extraction from raw fit assert_raises(RuntimeError, ica.get_sources, epochs) # test reading and writing test_ica_fname = op.join(op.dirname(tempdir), 'test-ica.fif') for cov in (None, test_cov): ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): # ICA does not converge ica.fit(raw, picks=picks, start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() assert_true(ica.mixing_matrix_.shape == (2, 2)) assert_true(ica.unmixing_matrix_.shape == (2, 2)) assert_true(ica.pca_components_.shape == (4, len(picks))) assert_true(sources.shape[1] == ica.n_components_) for exclude in [[], [0]]: ica.exclude = exclude ica.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.exclude == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.exclude = [] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert_true(ica.exclude == [ica_raw.ch_names.index(e) for e in ica_raw.info['bads']]) # test filtering d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.filter(4, 20) assert_true((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.notch_filter([10]) assert_true((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.method = 'fake' ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.n_pca_components == ica_read.n_pca_components) assert_equal(ica.method, ica_read.method) assert_equal(ica.labels_, ica_read.labels_) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ _pre_whitener') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) ica.n_pca_components = 4 ica_read.n_pca_components = 4 ica.exclude = [] ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) for attr in ['mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', '_pre_whitener']: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert_true(ica.ch_names == ica_read.ch_names) assert_true(isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check scrore funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # varicance, kurtosis idx params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) with warnings.catch_warnings(record=True): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) ica.labels_ = None idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) assert_raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') assert_raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 idx, scores = ica.find_bads_eog(raw) assert_true(isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) assert_true(len(ica_raw._filenames) == 0) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = Raw(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs._raw is None) assert_true(ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert_true(ncomps_ == expected)
def compute_ica(subject, data_folder): """Function will compute ICA on raw and apply the ICA. Parameters ---------- subject : string the subject id to be loaded """ raw = mne.io.Raw(data_folder + "%s_bp-raw.fif" % subject, preload=True) raw.set_montage = montage raw.apply_proj() # raw.resample(512, n_jobs=2) # ICA Part ica = ICA(n_components=None, max_pca_components=40, method='fastica', max_iter=256) picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, exclude='bads') ica.fit(raw, picks=picks, decim=decim, reject=reject) # maximum number of components to reject n_max_eog = 1 ########################################################################## # 2) identify bad components by analyzing latent sources. title = 'Sources related to %s artifacts (red) for sub: %s' # # # generate ECG epochs use detection via phase statistics # ecg_epochs = create_ecg_epochs(raw, ch_name="Ext4", # tmin=-.5, tmax=.5, picks=picks) # n_ecg_epochs_found = len(ecg_epochs.events) # sel_ecg_epochs = np.arange(0, n_ecg_epochs_found, 10) # ecg_epochs = ecg_epochs[sel_ecg_epochs] # # ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') # fig = ica.plot_scores(scores, exclude=ecg_inds, # title=title % ('ecg', subject)) # fig.savefig(data_folder + "pics/%s_ecg_scores.png" % subject) # # if ecg_inds: # show_picks = np.abs(scores).argsort()[::-1][:5] # # fig = ica.plot_sources(raw, show_picks, exclude=ecg_inds, # title=title % ('ecg', subject), show=False) # fig.savefig(data_folder + "pics/%s_ecg_sources.png" % subject) # fig = ica.plot_components(ecg_inds, title=title % ('ecg', subject), # colorbar=True) # fig.savefig(data_folder + "pics/%s_ecg_component.png" % subject) # # ecg_inds = ecg_inds[:n_max_ecg] # ica.exclude += ecg_inds # # # estimate average artifact # ecg_evoked = ecg_epochs.average() # del ecg_epochs # # # plot ECG sources + selection # fig = ica.plot_sources(ecg_evoked, exclude=ecg_inds) # fig.savefig(data_folder + "pics/%s_ecg_sources_ave.png" % subject) # # # plot ECG cleaning # ica.plot_overlay(ecg_evoked, exclude=ecg_inds) # fig.savefig(data_folder + "pics/%s_ecg_sources_clean_ave.png" % subject) # DETECT EOG BY CORRELATION # HORIZONTAL EOG eog_epochs = create_eog_epochs(raw, ch_name="EXG4") eog_indices, scores = ica.find_bads_eog(raw, ch_name="EXG4") if eog_indices: fig = ica.plot_scores(scores, exclude=eog_indices, title=title % ('eog', subject)) fig.savefig(data_folder + "pics/%s_eog_scores.png" % subject) # fig = ica.plot_components(picks=np.arange(0,20, 1), # title=title % ('eog', subject), colorbar=True) # fig.savefig(data_folder + "pics/%s_eog_component.png" % subject) fig = ica.plot_properties(raw, picks=eog_indices)[0] fig.savefig(data_folder + "pics/%s_eog_properties.png" % subject) eog_indices = eog_indices[:n_max_eog] ica.exclude += eog_indices del eog_epochs ########################################################################## # Apply the solution to Raw, Epochs or Evoked like this: raw_ica = ica.apply(raw) ica.save(data_folder + "%s-ica.fif" % subject) # save ICA componenets # Save raw with ICA removed raw_ica.save(data_folder + "%s_bp_ica-raw.fif" % subject, overwrite=True) plt.close("all")
# now do visual inspection of ICA components # ------------------------------------------------------- sources = ica.get_sources(raw, add_channels=['EOG 002', 'ECG 001', 'EOG 001']) sources.plot(n_channels=25, duration=120, scalings=dict(misc=3, eog=4e-4, ecg=1e-3)) ''' The noisy indices should minus 1 before type them into the excluding list. ''' ica.exclude = list( np.unique(list(idx_ecg) + list(idx_eog1) + list(idx_eog2) + list([]))) # ------------------------------------------------------- # apply ICA cleaning on original unfiltered data !! # ------------------------------------------------------- raw_new = ica.apply(raw_orig.copy(), exclude=ica.exclude, n_pca_components=0.99) # check your new raw cleaned data, and reject bad channels by visual inspection #raw_new.plot(start=0,duration=120) # ------------------------------------------------------- # save ICA object and plot ICA rejection performance # ------------------------------------------------------- fn_ica = fn_raw[:-8] + ',ica.fif' fn_perf = fn_ica[:-4] + ',perf' ica.save(fn_ica) # Note, there is a problem with the MNE peak detection of EOG peaks # therefore the we only find a few peaks in the EOG signal #raw_orig = mne.io.read_raw_fif(fn_raw, preload=True) #plot_performance_artifact_rejection(raw_orig, ica, fn_perf, meg_clean=raw_new, show=True) plot_performance_artifact_rejection(raw_orig, ica, fn_perf, show=True)
del eog_epochs, eog_average # ECG ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs) ecg_inds = ecg_inds[:n_max_ecg] ica.exclude.extend(ecg_inds) if ecg_inds: fig = ica.plot_components(ecg_inds, title=title % ('ecg', subject), colorbar=True) fig.savefig(ica_folder + "plots/%s_%s_ecg_component_2.png" % (subject, condition)) fig = ica.plot_overlay(raw, exclude=ecg_inds, show=False) fig.savefig(ica_folder + "plots/%s_%s_ecg_excluded_2.png" % (subject, condition)) fig = ica.plot_properties(raw, picks=ecg_inds) fig[0].savefig(ica_folder + "plots/%s_%s_plot_properties_2.png" % (subject, condition)) ########################################################################## # Apply the solution to Raw, Epochs or Evoked like this: raw_ica = ica.apply(raw) ica.save(ica_folder + "%s_%s-ica_2.fif" % (subject, condition)) # save ICA # componenets # Save raw with ICA removed raw_ica.save(ica_folder + "%s_%s_ica-raw.fif" % (subject, condition), overwrite=True)
reject = dict(mag=5e-12) epochs = mne.Epochs(raw, events, event_ids, tmin, tmax, picks=picks, baseline=baseline, preload=True, reject=reject) # Fit ICA, find and remove major artifacts ica = ICA(n_components=0.95).fit(raw, decim=6, reject=reject) # compute correlation scores, get bad indices sorted by score eog_epochs = create_eog_epochs(raw, ch_name='MRT31-2908', reject=reject) eog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name='MRT31-2908') ica.plot_scores(eog_scores, eog_inds) # see scores the selection is based on ica.plot_components(eog_inds) # view topographic sensitivity of components ica.exclude += eog_inds[:1] # we saw the 2nd ECG component looked too dipolar ica.plot_overlay(eog_epochs.average()) # inspect artifact removal epochs_cln = ica.apply(epochs, copy=True) # clean data, default in place evoked = [epochs_cln[k].average() for k in event_ids] contrast = evoked[1] - evoked[0] evoked.append(contrast) for e in evoked: e.plot(ylim=dict(mag=[-400, 400])) plt.show() # estimate noise covarariance noise_cov = mne.compute_covariance(epochs_cln, tmax=0)
def remove_linenoise(raw, noise_freq, width=2, shuffle_time=True, decim=100, n_component=1, plot=False, copy=True, picks=None, harmonics=True): import matplotlib.pyplot as plt from mne import pick_types from mne.preprocessing import ICA from mne.time_frequency.psd import psd_welch # Setup line frequency if isinstance(noise_freq, str): # automatic harmonics if noise_freq == 'us': noise_freq = 60 else: noise_freq = 50 elif not isinstance(noise_freq, (float, int)): raise NotImplementedError('Multiple bands') def plot_psd(psd, freqs, ax, title): for psd_ in psd: ax.plot(freqs, np.log10(psd_)) ax.set_xlabel('Frequencies') ax.set_title(title) if copy: raw = raw.copy() if picks is None: picks = pick_types(raw.info, eeg=True, meg=True, seeg=True) if plot: fig, axes = plt.subplots(1, 3, sharex=True) psd, freqs = psd_welch(raw, picks=picks) plot_psd(psd, freqs, axes[0], 'Raw Sensors') # Fit ICA on filtered data raw_ = raw.copy() if harmonics: # set up harmonics n_harm = raw_.info['sfreq'] // (2. * noise_freq) + 1 harmonics = noise_freq * np.arange(1, n_harm) # Band pass filtering outside lowest harmonics and nquist raw_.filter(noise_freq - width, harmonics[-1] + width) # Band stop filter in between harmonics raw_.notch_filter(freqs=harmonics[:-1]+noise_freq//2, notch_widths=noise_freq - 2*width) else: raw_.filter(noise_freq-width, noise_freq+width) # Shuffle time axis to avoid decimation aliasing if shuffle_time: time = np.arange(raw_.n_times) np.random.shuffle(time) raw_._data[:, time] = raw_._data ica = ICA(verbose=False) ica.fit(raw_, decim=decim, picks=picks) # Compute PSD of components raw_._data[picks, :] = np.dot(ica.mixing_matrix_, raw._data[picks, :]) psd, freqs = psd_welch(raw_, picks=picks) if plot: plot_psd(psd, freqs, axes[1], 'Components') # Find noise component and remove freq = np.where(freqs >= noise_freq)[0][0] sel = np.argsort(psd[:, freq])[-n_component:].tolist() raw_ = ica.apply(raw, exclude=sel, copy=True) if plot: psd, freqs = psd_welch(raw_, picks=picks) plot_psd(psd, freqs, axes[2], 'Clean sensors') return raw_
def ica_method(raw, picks, plot='n', save ='n'): ############################################################################### # 1) Fit ICA model using the FastICA algorithm # Other available choices are `infomax` or `extended-infomax` # We pass a float value between 0 and 1 to select n_components based on the # percentage of variance explained by the PCA components. ica = ICA(n_components=0.95, method='fastica') picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=False, stim=False, exclude='bads') ica.fit(raw, picks=picks, decim=3) # maximum number of components to reject n_max_eog = 1 # here we don't expect horizontal EOG components ############################################################################### # 2) identify bad components by analyzing latent sources. # detect EOG by correlation eog_inds, scores = ica.find_bads_eog(raw, threshold=2.5) show_picks = np.abs(scores).argsort()[::-1][:5] eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds ############################################################################### # 3) Assess component selection and unmixing quality eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5, picks=picks).average() if plot=='y': title = 'Sources related to %s artifacts (red)' ica.plot_scores(scores, exclude=eog_inds, title=title % 'eog', labels='eog') if save=='y': pylab.savefig('2.png') ica.plot_sources(raw, show_picks, exclude=eog_inds, title=title % 'eog') if save=='y': pylab.savefig('3.png') ica.plot_components(eog_inds, title=title % 'eog', colorbar=True) if save=='y': pylab.savefig('4.png') ica.plot_overlay(raw) # EOG artifacts remain if save=='y': pylab.savefig('5.png') ica.plot_sources(eog_evoked, exclude=eog_inds) # plot EOG sources + selection if save=='y': pylab.savefig('6.png') ica.plot_overlay(eog_evoked, exclude=eog_inds) # plot EOG cleaning if save=='y': pylab.savefig('7.png') ica.apply(raw, exclude=eog_inds) eeg_only_after=raw.pick_types(meg=False, eeg=True) return eeg_only_after
def test_ica_additional(method): """Test additional ICA functionality.""" _skip_check_picard(method) tempdir = _TempDir() stop2 = 500 raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() raw.del_proj() # avoid warnings raw.set_annotations(Annotations([0.5], [0.5], ['BAD'])) # XXX This breaks the tests :( # raw.info['bads'] = [raw.ch_names[1]] test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[1::2] epochs = Epochs(raw, events, None, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, proj=False) epochs.decimate(3, verbose='error') assert len(epochs) == 4 # test if n_components=None works ica = ICA(n_components=None, max_pca_components=None, n_pca_components=None, random_state=0, method=method, max_iter=1) with pytest.warns(UserWarning, match='did not converge'): ica.fit(epochs) # for testing eog functionality picks2 = np.concatenate([picks, pick_types(raw.info, False, eog=True)]) epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2, baseline=(None, 0), preload=True) del picks2 test_cov2 = test_cov.copy() ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4, n_pca_components=4, method=method) assert (ica.info is None) with pytest.warns(RuntimeWarning, match='normalize_proj'): ica.fit(raw, picks[:5]) assert (isinstance(ica.info, Info)) assert (ica.n_components_ < 5) ica = ICA(n_components=3, max_pca_components=4, method=method, n_pca_components=4, random_state=0) pytest.raises(RuntimeError, ica.save, '') ica.fit(raw, picks=[1, 2, 3, 4, 5], start=start, stop=stop2) # check passing a ch_name to find_bads_ecg with pytest.warns(RuntimeWarning, match='longer'): _, scores_1 = ica.find_bads_ecg(raw) _, scores_2 = ica.find_bads_ecg(raw, raw.ch_names[1]) assert scores_1[0] != scores_2[0] # test corrmap ica2 = ica.copy() ica3 = ica.copy() corrmap([ica, ica2], (0, 0), threshold='auto', label='blinks', plot=True, ch_type="mag") corrmap([ica, ica2], (0, 0), threshold=2, plot=False, show=False) assert (ica.labels_["blinks"] == ica2.labels_["blinks"]) assert (0 in ica.labels_["blinks"]) # test retrieval of component maps as arrays components = ica.get_components() template = components[:, 0] EvokedArray(components, ica.info, tmin=0.).plot_topomap([0], time_unit='s') corrmap([ica, ica3], template, threshold='auto', label='blinks', plot=True, ch_type="mag") assert (ica2.labels_["blinks"] == ica3.labels_["blinks"]) plt.close('all') ica_different_channels = ICA(n_components=2, random_state=0).fit( raw, picks=[2, 3, 4, 5]) pytest.raises(ValueError, corrmap, [ica_different_channels, ica], (0, 0)) # test warnings on bad filenames ica_badname = op.join(op.dirname(tempdir), 'test-bad-name.fif.gz') with pytest.warns(RuntimeWarning, match='-ica.fif'): ica.save(ica_badname) with pytest.warns(RuntimeWarning, match='-ica.fif'): read_ica(ica_badname) # test decim ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4, method=method, max_iter=1) raw_ = raw.copy() for _ in range(3): raw_.append(raw_) n_samples = raw_._data.shape[1] with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=picks[:5], decim=3) assert raw_._data.shape[1] == n_samples # test expl var ica = ICA(n_components=1.0, max_pca_components=4, n_pca_components=4, method=method, max_iter=1) with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=None, decim=3) assert (ica.n_components_ == 4) ica_var = _ica_explained_variance(ica, raw, normalize=True) assert (np.all(ica_var[:-1] >= ica_var[1:])) # test ica sorting ica.exclude = [0] ica.labels_ = dict(blink=[0], think=[1]) ica_sorted = _sort_components(ica, [3, 2, 1, 0], copy=True) assert_equal(ica_sorted.exclude, [3]) assert_equal(ica_sorted.labels_, dict(blink=[3], think=[2])) # epochs extraction from raw fit pytest.raises(RuntimeError, ica.get_sources, epochs) # test reading and writing test_ica_fname = op.join(op.dirname(tempdir), 'test-ica.fif') for cov in (None, test_cov): ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4, n_pca_components=4, method=method, max_iter=1) with pytest.warns(None): # ICA does not converge ica.fit(raw, picks=picks[:10], start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() assert (ica.mixing_matrix_.shape == (2, 2)) assert (ica.unmixing_matrix_.shape == (2, 2)) assert (ica.pca_components_.shape == (4, 10)) assert (sources.shape[1] == ica.n_components_) for exclude in [[], [0], np.array([1, 2, 3])]: ica.exclude = exclude ica.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (list(ica.exclude) == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.apply(raw) ica.exclude = [] ica.apply(raw, exclude=[1]) assert (ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert (ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert (ica.exclude == [ica_raw.ch_names.index(e) for e in ica_raw.info['bads']]) # test filtering d1 = ica_raw._data[0].copy() ica_raw.filter(4, 20, fir_design='firwin2') assert_equal(ica_raw.info['lowpass'], 20.) assert_equal(ica_raw.info['highpass'], 4.) assert ((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() ica_raw.notch_filter([10], trans_bandwidth=10, fir_design='firwin') assert ((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.method = 'fake' ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (ica.n_pca_components == ica_read.n_pca_components) assert_equal(ica.method, ica_read.method) assert_equal(ica.labels_, ica_read.labels_) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ pre_whitener_') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) ica.n_pca_components = 4 ica_read.n_pca_components = 4 ica.exclude = [] ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) for attr in ['mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', 'pre_whitener_']: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert (ica.ch_names == ica_read.ch_names) assert (isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, start=0, stop=50, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # variance, kurtosis params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) # Make sure detect_artifacts marks the right components. # For int criterion, the doc says "E.g. range(2) would return the two # sources with the highest score". Assert that's what it does. # Only test for skew, since it's always the same code. ica.exclude = [] ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=None, eog_ch=None, skew_criterion=0, var_criterion=None, kurt_criterion=None) assert np.abs(scores[ica.exclude]) == np.max(np.abs(scores)) evoked = epochs.average() evoked_data = evoked.data.copy() raw_data = raw[:][0].copy() epochs_data = epochs.get_data().copy() with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) pytest.raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') pytest.raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert (isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) idx, scores = ica.find_bads_eog(evoked, ch_name='MEG 1441') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(evoked, method='correlation') assert_equal(len(scores), ica.n_components_) assert_array_equal(raw_data, raw[:][0]) assert_array_equal(epochs_data, epochs.get_data()) assert_array_equal(evoked_data, evoked.data) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): ecg_events = ica_find_ecg_events( raw, sources[np.abs(ecg_scores).argmax()]) assert (ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): eog_events = ica_find_eog_events( raw, sources[np.abs(eog_scores).argmax()]) assert (eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert (ica_raw.last_samp - ica_raw.first_samp == 100) assert_equal(len(ica_raw._filenames), 1) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = read_raw_fif(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert (ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) assert (ica.n_components_ == ica_epochs.get_data().shape[1]) assert (ica_epochs._raw is None) assert (ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert (ncomps_ == expected) ica = ICA(method=method) with pytest.warns(None): # sometimes does not converge ica.fit(raw, picks=picks[:5]) with pytest.warns(RuntimeWarning, match='longer'): ica.find_bads_ecg(raw) ica.find_bads_eog(epochs, ch_name='MEG 0121') assert_array_equal(raw_data, raw[:][0]) raw.drop_channels(['MEG 0122']) pytest.raises(RuntimeError, ica.find_bads_eog, raw) with pytest.warns(RuntimeWarning, match='longer'): pytest.raises(RuntimeError, ica.find_bads_ecg, raw)
reject = dict(mag=5e-12) epochs = mne.Epochs(raw, events, event_ids, tmin, tmax, picks=picks, baseline=baseline, preload=True, reject=reject) # Fit ICA, find and remove major artifacts ica = ICA(n_components=0.95, random_state=0).fit(raw, decim=1, reject=reject) # compute correlation scores, get bad indices sorted by score eog_epochs = create_eog_epochs(raw, ch_name='MRT31-2908', reject=reject) eog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name='MRT31-2908') ica.plot_scores(eog_scores, eog_inds) # see scores the selection is based on ica.plot_components(eog_inds) # view topographic sensitivity of components ica.exclude += eog_inds[:1] # we saw the 2nd ECG component looked too dipolar ica.plot_overlay(eog_epochs.average()) # inspect artifact removal ica.apply(epochs) # clean data, default in place evoked = [epochs[k].average() for k in event_ids] contrast = combine_evoked(evoked, weights=[-1, 1]) # Faces - scrambled evoked.append(contrast) for e in evoked: e.plot(ylim=dict(mag=[-400, 400])) plt.show() # estimate noise covarariance noise_cov = mne.compute_covariance(epochs, tmax=0, method='shrunk', rank=None)
inst=eyes_open) #needs the channel locations ica.plot_components(picks=range(50, 90), ch_type=None, title="EO 50-90", inst=eyes_open) verification_ic = input( 'The ICs marked for rejection are: ' + str(ica.exclude) + '\n Are you sure you want to proceed? [y/n]: ') if verification_ic == 'y': plot = 0 else: print('Please select which ICs you would like to reject') EO_without_ICA = eyes_open.copy() EO_with_ICA = eyes_open.copy() EO_with_ICA = ica.apply(EO_with_ICA, exclude=ica.exclude) EO_with_ICA.plot(title='EO, AFTER ICA', n_epochs=4, n_channels=30, scalings=scalings) EO_without_ICA.plot(block=True, title='EO, BEFORE ICA', n_epochs=4, n_channels=30, scalings=scalings) cont = input('Continue on to epoch rejection? [y/n]: ') if cont == 'y': eyes_open = EO_with_ICA.drop_channels(extChannels) repeatICA = 0
def main(fname): from mne import pick_types from sys import argv from mne.io import read_raw_fif from mne.preprocessing import ICA, create_eog_epochs, create_ecg_epochs import numpy as np import matplotlib.pyplot as plt #%% parameters #fname=argv[1] reject_ica = {'mag': 6e-12, 'grad': 6e-10} #ecg_ch='MEG0141' #%% read and filter data Raw = read_raw_fif(fname, preload=True) Raw.filter(1, 40, fir_design='firwin') #Events=find_events(Raw, min_duration=0.003) #%% run ICA for automatic ocular & cardiac rejection (plot the rejected topos) picks_ica = pick_types(Raw.info, meg=True) ica = ICA(n_components=0.98, method='fastica') ica.fit(Raw, picks=picks_ica, reject=reject_ica, decim=4) ecg_epochs = create_ecg_epochs( Raw, reject=reject_ica ) # ch_name='MEG0111') # find the ECG events automagically eog_epochs = create_eog_epochs( Raw, reject=reject_ica) # find the EOG events automagically #ecg_epochs.average().plot_joint(times=0) #eog_epochs.average().plot_joint(times=0) ecg_inds, ecg_scores = ica.find_bads_ecg(ecg_epochs, method='ctps') print(ecg_inds) eog_inds, eog_scores = ica.find_bads_eog(eog_epochs) print(eog_inds) #ica.plot_overlay(ecg_average, exclude=ecg_inds) # does not work? try: ica.plot_components(ch_type='mag', picks=ecg_inds, title=fname, show=False) except IndexError as exc: pass except ValueError as exc: pass try: ica.plot_components(ch_type='mag', picks=eog_inds, title=fname, show=False) except IndexError as exc: pass except ValueError as exc: pass plt.show(block=False) ica.exclude.extend(eog_inds) ica.exclude.extend(ecg_inds) # Save changes to the data: #Raw.copy().crop(10,20).plot() Raw = ica.apply(Raw) #Raw.copy().crop(10,20).plot() #Raw=ica.apply(Raw,exclude=[eog_inds]) # list of the ICA components to exclude!!! return Raw, ica
def compute_ica(subject, data_folder): """Function will compute ICA on raw and apply the ICA. Parameters ---------- subject : string the subject id to be loaded """ raw = mne.io.Raw(data_folder + "%s_bp-raw.fif" % subject, preload=True) raw.set_montage = montage raw.apply_proj() # raw.resample(512, n_jobs=2) # ICA Part ica = ICA(n_components=None, max_pca_components=40, method='fastica', max_iter=256) picks = mne.pick_types( raw.info, meg=False, eeg=True, stim=False, exclude='bads') ica.fit(raw, picks=picks, decim=decim, reject=reject) # maximum number of components to reject n_max_eog = 1 ########################################################################## # 2) identify bad components by analyzing latent sources. title = 'Sources related to %s artifacts (red) for sub: %s' # # # generate ECG epochs use detection via phase statistics # ecg_epochs = create_ecg_epochs(raw, ch_name="Ext4", # tmin=-.5, tmax=.5, picks=picks) # n_ecg_epochs_found = len(ecg_epochs.events) # sel_ecg_epochs = np.arange(0, n_ecg_epochs_found, 10) # ecg_epochs = ecg_epochs[sel_ecg_epochs] # # ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') # fig = ica.plot_scores(scores, exclude=ecg_inds, # title=title % ('ecg', subject)) # fig.savefig(data_folder + "pics/%s_ecg_scores.png" % subject) # # if ecg_inds: # show_picks = np.abs(scores).argsort()[::-1][:5] # # fig = ica.plot_sources(raw, show_picks, exclude=ecg_inds, # title=title % ('ecg', subject), show=False) # fig.savefig(data_folder + "pics/%s_ecg_sources.png" % subject) # fig = ica.plot_components(ecg_inds, title=title % ('ecg', subject), # colorbar=True) # fig.savefig(data_folder + "pics/%s_ecg_component.png" % subject) # # ecg_inds = ecg_inds[:n_max_ecg] # ica.exclude += ecg_inds # # # estimate average artifact # ecg_evoked = ecg_epochs.average() # del ecg_epochs # # # plot ECG sources + selection # fig = ica.plot_sources(ecg_evoked, exclude=ecg_inds) # fig.savefig(data_folder + "pics/%s_ecg_sources_ave.png" % subject) # # # plot ECG cleaning # ica.plot_overlay(ecg_evoked, exclude=ecg_inds) # fig.savefig(data_folder + "pics/%s_ecg_sources_clean_ave.png" % subject) # DETECT EOG BY CORRELATION # HORIZONTAL EOG eog_epochs = create_eog_epochs(raw, ch_name="EXG4") eog_indices, scores = ica.find_bads_eog(raw, ch_name="EXG4") fig = ica.plot_scores( scores, exclude=eog_indices, title=title % ('eog', subject)) fig.savefig(data_folder + "pics/%s_eog_scores.png" % subject) fig = ica.plot_components( eog_indices, title=title % ('eog', subject), colorbar=True) fig.savefig(data_folder + "pics/%s_eog_component.png" % subject) eog_indices = eog_indices[:n_max_eog] ica.exclude += eog_indices del eog_epochs ########################################################################## # Apply the solution to Raw, Epochs or Evoked like this: raw_ica = ica.apply(raw) ica.save(data_folder + "%s-ica.fif" % subject) # save ICA componenets # Save raw with ICA removed raw_ica.save(data_folder + "%s_bp_ica-raw.fif" % subject, overwrite=True) plt.close("all")
def test_ica_core(): """Test ICA on raw and epochs""" raw = Raw(raw_fname).crop(1.5, stop, False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') # XXX. The None cases helped revealing bugs but are time consuming. test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [2, 1.0] # for future dbg add cases max_pca_components = [3] picks_ = [picks] methods = ['fastica'] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_, methods) # # test init catchers assert_raises(ValueError, ICA, n_components=3, max_pca_components=2) assert_raises(ValueError, ICA, n_components=2.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks, method in iter_ica_params: # Test ICA raw ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method, max_iter=1) assert_raises(ValueError, ica.__contains__, 'mag') print(ica) # to test repr # test fit checker assert_raises(RuntimeError, ica.get_sources, raw) assert_raises(RuntimeError, ica.get_sources, epochs) # test decomposition with warnings.catch_warnings(record=True): ica.fit(raw, picks=pcks, start=start, stop=stop) repr(ica) # to test repr assert_true('mag' in ica) # should now work without error # test re-fit unmixing1 = ica.unmixing_matrix_ with warnings.catch_warnings(record=True): ica.fit(raw, picks=pcks, start=start, stop=stop) assert_array_almost_equal(unmixing1, ica.unmixing_matrix_) sources = ica.get_sources(raw)[:, :][0] assert_true(sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3.preload = False assert_raises(ValueError, ica.apply, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) with warnings.catch_warnings(record=True): ica.fit(epochs, picks=picks) data = epochs.get_data()[:, 0, :] n_samples = np.prod(data.shape) assert_equal(ica.n_samples_, n_samples) print(ica) # to test repr sources = ica.get_sources(epochs).get_data() assert_true(sources.shape[1] == ica.n_components_) assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False assert_raises(ValueError, ica.apply, epochs3, include=[1, 2]) # test for bug with whitener updating _pre_whitener = ica._pre_whitener.copy() epochs._data[:, 0, 10:15] *= 1e12 ica.apply(epochs, copy=True) assert_array_equal(_pre_whitener, ica._pre_whitener) # test expl. var threshold leading to empty sel ica.n_components = 0.1 assert_raises(RuntimeError, ica.fit, epochs) offender = 1, 2, 3, assert_raises(ValueError, ica.get_sources, offender) assert_raises(ValueError, ica.fit, offender) assert_raises(ValueError, ica.apply, offender)
#ica.plot_components(picks=range(50,90),ch_type=None,title=str(ep.event_id),inst=ep) verification_ic = input( 'The ICs marked for rejection are: ' + str(ica.exclude) + '\n Are you sure you want to proceed? [y/n]: ') if verification_ic == 'y': plotICA = 0 else: print( 'Please select which ICs you would like to reject' ) ep_w_IC = ep.copy() ep_w_no_IC = ep.copy() ep_w_IC = ica.apply(ep_w_IC, exclude=ica.exclude) ep_w_IC.plot(title='AFTER ICA', n_epochs=4, n_channels=30) ep_w_no_IC.plot(block=True, title='BEFORE ICA', n_epochs=4, n_channels=30) cont = input('Continue on to epoch rejection? [y/n]: ') if cont == 'y': ep = ep_w_IC.drop_channels(extChannels) repeatICA = 0 plotEpoch = 1 while plotEpoch: ep_copy = ep.copy()
def test_ica_additional(): """Test additional ICA functionality""" stop2 = 500 raw = io.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude="bads") test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude="bads") epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) # for testing eog functionality picks2 = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=True, exclude="bads") epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2, baseline=(None, 0), preload=True) test_cov2 = deepcopy(test_cov) ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4, n_pca_components=4) assert_true(ica.info is None) with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5]) assert_true(isinstance(ica.info, Info)) assert_true(ica.n_components_ < 5) ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) assert_raises(RuntimeError, ica.save, "") with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, start=start, stop=stop2) # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") ica_badname = op.join(op.dirname(tempdir), "test-bad-name.fif.gz") ica.save(ica_badname) read_ica(ica_badname) assert_true(len(w) == 2) # test decim ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw_ = raw.copy() for _ in range(3): raw_.append(raw_) n_samples = raw_._data.shape[1] with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(raw_._data.shape[1], n_samples) # test expl var ica = ICA(n_components=1.0, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(ica.n_components_ == 4) # epochs extraction from raw fit assert_raises(RuntimeError, ica.get_sources, epochs) # test reading and writing test_ica_fname = op.join(op.dirname(tempdir), "test-ica.fif") for cov in (None, test_cov): ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): # ICA does not converge ica.fit(raw, picks=picks, start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() assert_true(ica.mixing_matrix_.shape == (2, 2)) assert_true(ica.unmixing_matrix_.shape == (2, 2)) assert_true(ica.pca_components_.shape == (4, len(picks))) assert_true(sources.shape[1] == ica.n_components_) for exclude in [[], [0]]: ica.exclude = [0] ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.exclude == ica_read.exclude) ica.exclude = [] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert_true(ica.exclude == [ica_raw.ch_names.index(e) for e in ica_raw.info["bads"]]) # test filtering d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.filter(4, 20) assert_true((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.notch_filter([10]) assert_true((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.n_pca_components == ica_read.n_pca_components) # check type consistency attrs = "mixing_matrix_ unmixing_matrix_ pca_components_ " "pca_explained_variance_ _pre_whitener" f = lambda x, y: getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) ica.n_pca_components = 4 ica_read.n_pca_components = 4 ica.exclude = [] ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) for attr in [ "mixing_matrix_", "unmixing_matrix_", "pca_components_", "pca_mean_", "pca_explained_variance_", "_pre_whitener", ]: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert_true(ica.ch_names == ica_read.ch_names) assert_true(isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check scrore funcs for name, func in score_funcs.items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target="EOG 061", score_func=func, start=0, stop=10) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # varicance, kurtosis idx params params += [(None, "MEG 1531")] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts( raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx, ) idx, scores = ica.find_bads_ecg(raw, method="ctps") assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(raw, method="correlation") assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method="ctps") assert_equal(len(scores), ica.n_components_) assert_raises(ValueError, ica.find_bads_ecg, epochs.average(), method="ctps") assert_raises(ValueError, ica.find_bads_ecg, raw, method="crazy-coupling") idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info["chs"][raw.ch_names.index("EOG 061") - 1]["kind"] = 202 idx, scores = ica.find_bads_eog(raw) assert_true(isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) # check score funcs for name, func in score_funcs.items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target="EOG 061", score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target="MEG 1531", score_func="pearsonr") with warnings.catch_warnings(record=True): # filter attenuation warning ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target="EOG 061", score_func="pearsonr") with warnings.catch_warnings(record=True): # filter attenuation warning eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) assert_true(len(ica_raw._filenames) == 0) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if "ICA" in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), "test-ica_raw.fif") ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = io.Raw(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if "ICA" in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs.raw is None) assert_true(ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = _check_n_pca_components(ica, ncomps) assert_true(ncomps_ == expected)
def test_ica_full_data_recovery(method): """Test recovery of full data when no source is rejected.""" # Most basic recovery _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(0.5, stop).load_data() events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:10] with pytest.warns(RuntimeWarning, match='projection'): epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) evoked = epochs.average() n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() data_evoked = evoked.data raw.set_annotations(Annotations([0.5], [0.5], ['BAD'])) methods = [method] for method in methods: stuff = [(2, n_channels, True), (2, n_channels // 2, False)] for n_components, n_pca_components, ok in stuff: ica = ICA(n_components=n_components, random_state=0, max_pca_components=n_pca_components, n_pca_components=n_pca_components, method=method, max_iter=1) with pytest.warns(UserWarning, match=None): # sometimes warns ica.fit(raw, picks=list(range(n_channels))) _assert_ica_attributes(ica) raw2 = ica.apply(raw.copy(), exclude=[]) if ok: assert_allclose(data[:n_channels], raw2._data[:n_channels], rtol=1e-10, atol=1e-15) else: diff = np.abs(data[:n_channels] - raw2._data[:n_channels]) assert (np.max(diff) > 1e-14) ica = ICA(n_components=n_components, method=method, max_pca_components=n_pca_components, n_pca_components=n_pca_components, random_state=0) with pytest.warns(None): # sometimes warns ica.fit(epochs, picks=list(range(n_channels))) _assert_ica_attributes(ica) epochs2 = ica.apply(epochs.copy(), exclude=[]) data2 = epochs2.get_data()[:, :n_channels] if ok: assert_allclose(data_epochs[:, :n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(data_epochs[:, :n_channels] - data2) assert (np.max(diff) > 1e-14) evoked2 = ica.apply(evoked.copy(), exclude=[]) data2 = evoked2.data[:n_channels] if ok: assert_allclose(data_evoked[:n_channels], data2, rtol=1e-10, atol=1e-15) else: diff = np.abs(evoked.data[:n_channels] - data2) assert (np.max(diff) > 1e-14) pytest.raises(ValueError, ICA, method='pizza-decomposision')
def test_ica_eeg(): """Test ICA on EEG.""" method = 'fastica' raw_fif = read_raw_fif(fif_fname, preload=True) raw_eeglab = read_raw_eeglab(input_fname=eeglab_fname, preload=True) for raw in [raw_fif, raw_eeglab]: events = make_fixed_length_events(raw, 99999, start=0, stop=0.3, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = [] picks_all.extend(picks_meg) picks_all.extend(picks_eeg) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst, picks=picks) _assert_ica_attributes(ica) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst) with pytest.warns(RuntimeWarning, match='MISC channel'): raw = read_raw_ctf(ctf_fname2, preload=True) events = make_fixed_length_events(raw, 99999, start=0, stop=0.2, duration=0.1) picks_meg = pick_types(raw.info, meg=True, eeg=False)[:2] picks_eeg = pick_types(raw.info, meg=False, eeg=True)[:2] picks_all = picks_meg + picks_eeg for comp in [0, 1]: raw.apply_gradient_compensation(comp) epochs = Epochs(raw, events, None, -0.1, 0.1, preload=True) evoked = epochs.average() for picks in [picks_meg, picks_eeg, picks_all]: if len(picks) == 0: continue # test fit for inst in [raw, epochs]: ica = ICA(n_components=2, random_state=0, max_iter=2, method=method) with pytest.warns(None): ica.fit(inst) _assert_ica_attributes(ica) # test apply and get_sources for inst in [raw, epochs, evoked]: ica.apply(inst) ica.get_sources(inst)
def runICA(raw,saveRoot,name): saveRoot = saveRoot icaList = [] ica = [] n_max_ecg = 3 # max number of ecg components # n_max_eog_1 = 2 # max number of vert eog comps # n_max_eog_2 = 2 # max number of horiz eog comps ecg_source_idx, ecg_scores, ecg_exclude = [], [], [] eog_source_idx, eog_scores, eog_exclude = [], [], [] #horiz = 1 # will later be modified to horiz = 0 if no horizontal EOG components are identified ica = ICA(n_components=0.90,n_pca_components=64,max_pca_components=100,noise_cov=None) fit_picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=False, ecg=False, stim=False, exclude='bads') ica.fit(raw, picks=fit_picks) #ica.fit(raw) #************* eog_picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, eog=True, ecg=False, emg=False)[0] ecg_picks = mne.pick_types(raw.info, meg=False, eeg=False, stim=False, ecg=True, eog=False, emg=False)[0] ica_picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=False, ecg=False, stim=False, exclude='bads') ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5, picks=ica_picks) ecg_evoked = ecg_epochs.average() eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5, picks=ica_picks).average() ecg_source_idx, ecg_scores = ica.find_bads_ecg(ecg_epochs, method='ctps') eog_source_idx, eog_scores = ica.find_bads_eog(raw,ch_name=raw.ch_names[eog_picks].encode('ascii', 'ignore')) #eog_source_idx_2, eog_scores_2 = ica.find_bads_eog(raw,ch_name='EOG002') #if not eog_source_idx_2: # horiz = 0 #show_picks = np.abs(scores).argsort()[::-1][:5] #ica.plot_sources(raw, show_picks, exclude=ecg_inds, title=title % 'ecg') # defining a title-frame for later use title = 'Sources related to %s artifacts (red)' # extracting number of ica-components and plotting their topographies source_idx = range(0, ica.n_components_) #ica_plot = ica.plot_components(source_idx, ch_type="mag") ica_plot = ica.plot_components(source_idx) #ica_plot = ica.plot_components(source_idx) # select ICA sources and reconstruct MEG signals, compute clean ERFs # Add detected artefact sources to exclusion list # We now add the eog artefacts to the ica.exclusion list if not ecg_source_idx: print("No ECG components above threshold were identified for subject " + name + " - selecting the component with the highest score under threshold") ecg_exclude = [np.absolute(ecg_scores).argmax()] ecg_source_idx=[np.absolute(ecg_scores).argmax()] elif ecg_source_idx: ecg_exclude += ecg_source_idx[:n_max_ecg] ica.exclude += ecg_exclude if not eog_source_idx: if np.absolute(eog_scores).any>0.3: eog_exclude=[np.absolute(eog_scores).argmax()] eog_source_idx=[np.absolute(eog_scores).argmax()] print("No EOG components above threshold were identified " + name + " - selecting the component with the highest score under threshold above 0.3") elif not np.absolute(eog_scores).any>0.3: eog_exclude=[] print("No EOG components above threshold were identified" + name) elif eog_source_idx: eog_exclude += eog_source_idx ica.exclude += eog_exclude print('########## saving') if len(eog_exclude) == 0: if len(ecg_exclude) == 0: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg_none' + '.pdf', format = 'pdf') elif len(ecg_exclude) == 1: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg' + map(str, ecg_exclude)[0] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 2: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 3: ica_plot.savefig(saveRoot + name + '_comps_eog_none-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '_' + map(str, ecg_exclude)[2] + '.pdf', format = 'pdf') elif len(eog_exclude) == 1: if len(ecg_exclude) == 0: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg_none' + '.pdf', format = 'pdf') elif len(ecg_exclude) == 1: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg' + map(str, ecg_exclude)[0] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 2: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 3: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '_' + map(str, ecg_exclude)[2] + '.pdf', format = 'pdf') elif len(eog_exclude) == 2: if len(ecg_exclude) == 0: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg_none' + '.pdf', format = 'pdf') elif len(ecg_exclude) == 1: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg' + map(str, ecg_exclude)[0] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 2: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '.pdf', format = 'pdf') elif len(ecg_exclude) == 3: ica_plot.savefig(saveRoot + name + '_comps_eog' + map(str, eog_exclude)[0] + '_' + map(str, eog_exclude)[1] + '-ecg' + map(str, ecg_exclude)[0] + '_' + map(str, ecg_exclude)[1] + '_' + map(str, ecg_exclude)[2] + '.pdf', format = 'pdf') # plot the scores for the different components highlighting in red that/those related to ECG #scores_plots=plt.figure() #ax = plt.subplot(2,1,1) scores_plots_ecg=ica.plot_scores(ecg_scores, exclude=ecg_source_idx, title=title % 'ecg') scores_plots_ecg.savefig(saveRoot + name + '_ecg_scores.pdf', format = 'pdf') #ax = plt.subplot(2,1,2) scores_plots_eog=ica.plot_scores(eog_scores, exclude=eog_source_idx, title=title % 'eog') scores_plots_eog.savefig(saveRoot + name + '_eog_scores.pdf', format = 'pdf') #if len(ecg_exclude) > 0: # estimate average artifact #source_clean_ecg=plt.figure() #ax = plt.subplot(2,1,1) source_source_ecg=ica.plot_sources(ecg_evoked, exclude=ecg_source_idx) source_source_ecg.savefig(saveRoot + name + '_ecg_source.pdf', format = 'pdf') #ax = plt.subplot(2,1,2) source_clean_ecg=ica.plot_overlay(ecg_evoked, exclude=ecg_source_idx) source_clean_ecg.savefig(saveRoot + name + '_ecg_clean.pdf', format = 'pdf') #clean_plot.savefig(saveRoot + name + '_ecg_clean.pdf', format = 'pdf') #if len(eog_exclude) > 0: source_source_eog=ica.plot_sources(eog_evoked, exclude=eog_source_idx) source_source_eog.savefig(saveRoot + name + '_eog_source.pdf', format = 'pdf') source_clean_eog=ica.plot_overlay(eog_evoked, exclude=eog_source_idx) source_clean_eog.savefig(saveRoot + name + '_eog_clean.pdf', format = 'pdf') overl_plot = ica.plot_overlay(raw) overl_plot.savefig(saveRoot + name + '_overl.pdf', format = 'pdf') plt.close('all') ## restore sensor space data icaList = ica.apply(raw) return(icaList, ica)
def test_ica_core(method): """Test ICA on raw and epochs.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() # XXX. The None cases helped revealing bugs but are time consuming. test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [2, 1.0] # for future dbg add cases max_pca_components = [3] picks_ = [picks] methods = [method] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_, methods) # # test init catchers pytest.raises(ValueError, ICA, n_components=3, max_pca_components=2) pytest.raises(ValueError, ICA, n_components=2.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks, method in iter_ica_params: # Test ICA raw ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method, max_iter=1) pytest.raises(ValueError, ica.__contains__, 'mag') print(ica) # to test repr # test fit checker pytest.raises(RuntimeError, ica.get_sources, raw) pytest.raises(RuntimeError, ica.get_sources, epochs) # Test error upon empty epochs fitting with pytest.raises(RuntimeError, match='none were found'): ica.fit(epochs[0:0]) # test decomposition with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=pcks, start=start, stop=stop) repr(ica) # to test repr assert ('mag' in ica) # should now work without error # test re-fit unmixing1 = ica.unmixing_matrix_ with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=pcks, start=start, stop=stop) assert_array_almost_equal(unmixing1, ica.unmixing_matrix_) raw_sources = ica.get_sources(raw) # test for #3804 assert_equal(raw_sources._filenames, [None]) print(raw_sources) # test for gh-6271 (scaling of ICA traces) fig = raw_sources.plot() assert len(fig.axes[0].lines) in (4, 5, 6) for line in fig.axes[0].lines: y = line.get_ydata() if len(y) > 2: # actual data, not markers assert np.ptp(y) < 15 plt.close('all') sources = raw_sources[:, :][0] assert (sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3.preload = False pytest.raises(RuntimeError, ica.apply, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0, method=method) with pytest.warns(None): # sometimes warns ica.fit(epochs, picks=picks) _assert_ica_attributes(ica) data = epochs.get_data()[:, 0, :] n_samples = np.prod(data.shape) assert_equal(ica.n_samples_, n_samples) print(ica) # to test repr sources = ica.get_sources(epochs).get_data() assert (sources.shape[1] == ica.n_components_) pytest.raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False pytest.raises(RuntimeError, ica.apply, epochs3, include=[1, 2]) # test for bug with whitener updating _pre_whitener = ica.pre_whitener_.copy() epochs._data[:, 0, 10:15] *= 1e12 ica.apply(epochs.copy()) assert_array_equal(_pre_whitener, ica.pre_whitener_) # test expl. var threshold leading to empty sel ica.n_components = 0.1 pytest.raises(RuntimeError, ica.fit, epochs) offender = 1, 2, 3, pytest.raises(ValueError, ica.get_sources, offender) pytest.raises(TypeError, ica.fit, offender) pytest.raises(TypeError, ica.apply, offender)
def test_ica_additional(method): """Test additional ICA functionality.""" _skip_check_picard(method) tempdir = _TempDir() stop2 = 500 raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() raw.del_proj() # avoid warnings raw.set_annotations(Annotations([0.5], [0.5], ['BAD'])) # XXX This breaks the tests :( # raw.info['bads'] = [raw.ch_names[1]] test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[1::2] epochs = Epochs(raw, events, None, tmin, tmax, picks=picks, baseline=(None, 0), preload=True, proj=False) epochs.decimate(3, verbose='error') assert len(epochs) == 4 # test if n_components=None works ica = ICA(n_components=None, max_pca_components=None, n_pca_components=None, random_state=0, method=method, max_iter=1) with pytest.warns(UserWarning, match='did not converge'): ica.fit(epochs) _assert_ica_attributes(ica) # for testing eog functionality picks2 = np.concatenate([picks, pick_types(raw.info, False, eog=True)]) epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2, baseline=(None, 0), preload=True) del picks2 test_cov2 = test_cov.copy() ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4, n_pca_components=4, method=method) assert (ica.info is None) with pytest.warns(RuntimeWarning, match='normalize_proj'): ica.fit(raw, picks[:5]) _assert_ica_attributes(ica) assert (isinstance(ica.info, Info)) assert (ica.n_components_ < 5) ica = ICA(n_components=3, max_pca_components=4, method=method, n_pca_components=4, random_state=0) pytest.raises(RuntimeError, ica.save, '') ica.fit(raw, picks=[1, 2, 3, 4, 5], start=start, stop=stop2) _assert_ica_attributes(ica) # check passing a ch_name to find_bads_ecg with pytest.warns(RuntimeWarning, match='longer'): _, scores_1 = ica.find_bads_ecg(raw) _, scores_2 = ica.find_bads_ecg(raw, raw.ch_names[1]) assert scores_1[0] != scores_2[0] # test corrmap ica2 = ica.copy() ica3 = ica.copy() corrmap([ica, ica2], (0, 0), threshold='auto', label='blinks', plot=True, ch_type="mag") with pytest.raises(RuntimeError, match='No component detected'): corrmap( [ica, ica2], (0, 0), threshold=2, plot=False, show=False, ) corrmap([ica, ica2], (0, 0), threshold=0.5, plot=False, show=False) assert (ica.labels_["blinks"] == ica2.labels_["blinks"]) assert (0 in ica.labels_["blinks"]) # test retrieval of component maps as arrays components = ica.get_components() template = components[:, 0] EvokedArray(components, ica.info, tmin=0.).plot_topomap([0], time_unit='s') corrmap([ica, ica3], template, threshold='auto', label='blinks', plot=True, ch_type="mag") assert (ica2.labels_["blinks"] == ica3.labels_["blinks"]) plt.close('all') # No match bad_ica = ica2.copy() bad_ica.mixing_matrix_[:] = 0. with pytest.warns(RuntimeWarning, match='divide'): with catch_logging() as log: corrmap([ica, bad_ica], (0, 0), threshold=0.5, plot=False, show=False, verbose=True) log = log.getvalue() assert 'No maps selected' in log # make sure a single threshold in a list works corrmap([ica, ica3], template, threshold=[0.5], label='blinks', plot=True, ch_type="mag") ica_different_channels = ICA(n_components=2, random_state=0).fit(raw, picks=[2, 3, 4, 5]) pytest.raises(ValueError, corrmap, [ica_different_channels, ica], (0, 0)) # test warnings on bad filenames ica_badname = op.join(op.dirname(tempdir), 'test-bad-name.fif.gz') with pytest.warns(RuntimeWarning, match='-ica.fif'): ica.save(ica_badname) with pytest.warns(RuntimeWarning, match='-ica.fif'): read_ica(ica_badname) # test decim ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4, method=method, max_iter=1) raw_ = raw.copy() for _ in range(3): raw_.append(raw_) n_samples = raw_._data.shape[1] with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=picks[:5], decim=3) _assert_ica_attributes(ica) assert raw_._data.shape[1] == n_samples # test expl var ica = ICA(n_components=1.0, max_pca_components=4, n_pca_components=4, method=method, max_iter=1) with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw, picks=None, decim=3) _assert_ica_attributes(ica) assert (ica.n_components_ == 4) ica_var = _ica_explained_variance(ica, raw, normalize=True) assert (np.all(ica_var[:-1] >= ica_var[1:])) # test ica sorting ica.exclude = [0] ica.labels_ = dict(blink=[0], think=[1]) ica_sorted = _sort_components(ica, [3, 2, 1, 0], copy=True) assert_equal(ica_sorted.exclude, [3]) assert_equal(ica_sorted.labels_, dict(blink=[3], think=[2])) # epochs extraction from raw fit pytest.raises(RuntimeError, ica.get_sources, epochs) # test reading and writing test_ica_fname = op.join(op.dirname(tempdir), 'test-ica.fif') for cov in (None, test_cov): ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4, n_pca_components=4, method=method, max_iter=1) with pytest.warns(None): # ICA does not converge ica.fit(raw, picks=picks[:10], start=start, stop=stop2) _assert_ica_attributes(ica) sources = ica.get_sources(epochs).get_data() assert (ica.mixing_matrix_.shape == (2, 2)) assert (ica.unmixing_matrix_.shape == (2, 2)) assert (ica.pca_components_.shape == (4, 10)) assert (sources.shape[1] == ica.n_components_) for exclude in [[], [0], np.array([1, 2, 3])]: ica.exclude = exclude ica.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (list(ica.exclude) == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.apply(raw) ica.exclude = [] ica.apply(raw, exclude=[1]) assert (ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert (ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert (ica.exclude == [ ica_raw.ch_names.index(e) for e in ica_raw.info['bads'] ]) # test filtering d1 = ica_raw._data[0].copy() ica_raw.filter(4, 20, fir_design='firwin2') assert_equal(ica_raw.info['lowpass'], 20.) assert_equal(ica_raw.info['highpass'], 4.) assert ((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() ica_raw.notch_filter([10], trans_bandwidth=10, fir_design='firwin') assert ((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.method = 'fake' ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert (ica.n_pca_components == ica_read.n_pca_components) assert_equal(ica.method, ica_read.method) assert_equal(ica.labels_, ica_read.labels_) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ pre_whitener_') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) ica.n_pca_components = 4 ica_read.n_pca_components = 4 ica.exclude = [] ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) for attr in [ 'mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', 'pre_whitener_' ]: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert (ica.ch_names == ica_read.ch_names) assert (isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, start=0, stop=50, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # variance, kurtosis params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) # Make sure detect_artifacts marks the right components. # For int criterion, the doc says "E.g. range(2) would return the two # sources with the highest score". Assert that's what it does. # Only test for skew, since it's always the same code. ica.exclude = [] ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=None, eog_ch=None, skew_criterion=0, var_criterion=None, kurt_criterion=None) assert np.abs(scores[ica.exclude]) == np.max(np.abs(scores)) evoked = epochs.average() evoked_data = evoked.data.copy() raw_data = raw[:][0].copy() epochs_data = epochs.get_data().copy() with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) pytest.raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') pytest.raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 with pytest.warns(RuntimeWarning, match='longer'): idx, scores = ica.find_bads_eog(raw) assert (isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) idx, scores = ica.find_bads_eog(evoked, ch_name='MEG 1441') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(evoked, method='correlation') assert_equal(len(scores), ica.n_components_) assert_array_equal(raw_data, raw[:][0]) assert_array_equal(epochs_data, epochs.get_data()) assert_array_equal(evoked_data, evoked.data) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert (ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling pytest.raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert (ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with pytest.warns(RuntimeWarning, match='longer'): eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert (eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert (ica_raw.last_samp - ica_raw.first_samp == 100) assert_equal(len(ica_raw._filenames), 1) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = read_raw_fif(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert (ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert (ica.n_components_ == len(ica_chans)) assert (ica.n_components_ == ica_epochs.get_data().shape[1]) assert (ica_epochs._raw is None) assert (ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert (ncomps_ == expected) ica = ICA(method=method) with pytest.warns(None): # sometimes does not converge ica.fit(raw, picks=picks[:5]) _assert_ica_attributes(ica) with pytest.warns(RuntimeWarning, match='longer'): ica.find_bads_ecg(raw) ica.find_bads_eog(epochs, ch_name='MEG 0121') assert_array_equal(raw_data, raw[:][0]) raw.drop_channels(['MEG 0122']) pytest.raises(RuntimeError, ica.find_bads_eog, raw) with pytest.warns(RuntimeWarning, match='longer'): pytest.raises(RuntimeError, ica.find_bads_ecg, raw)
def test_ica_additional(): """Test additional ICA functionality""" tempdir = _TempDir() stop2 = 500 raw = Raw(raw_fname).crop(1.5, stop, False) raw.load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') test_cov = read_cov(test_cov_name) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) # test if n_components=None works with warnings.catch_warnings(record=True): ica = ICA(n_components=None, max_pca_components=None, n_pca_components=None, random_state=0) ica.fit(epochs, picks=picks, decim=3) # for testing eog functionality picks2 = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=True, exclude='bads') epochs_eog = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks2, baseline=(None, 0), preload=True) test_cov2 = test_cov.copy() ica = ICA(noise_cov=test_cov2, n_components=3, max_pca_components=4, n_pca_components=4) assert_true(ica.info is None) with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5]) assert_true(isinstance(ica.info, Info)) assert_true(ica.n_components_ < 5) ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) assert_raises(RuntimeError, ica.save, '') with warnings.catch_warnings(record=True): ica.fit(raw, picks=[1, 2, 3, 4, 5], start=start, stop=stop2) # test corrmap ica2 = ica.copy() corrmap([ica, ica2], (0, 0), threshold='auto', label='blinks', plot=True, ch_type="mag") corrmap([ica, ica2], (0, 0), threshold=2, plot=False, show=False) assert_true(ica.labels_["blinks"] == ica2.labels_["blinks"]) assert_true(0 in ica.labels_["blinks"]) plt.close('all') # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ica_badname = op.join(op.dirname(tempdir), 'test-bad-name.fif.gz') ica.save(ica_badname) read_ica(ica_badname) assert_naming(w, 'test_ica.py', 2) # test decim ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw_ = raw.copy() for _ in range(3): raw_.append(raw_) n_samples = raw_._data.shape[1] with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(raw_._data.shape[1], n_samples) # test expl var ica = ICA(n_components=1.0, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): ica.fit(raw, picks=None, decim=3) assert_true(ica.n_components_ == 4) # epochs extraction from raw fit assert_raises(RuntimeError, ica.get_sources, epochs) # test reading and writing test_ica_fname = op.join(op.dirname(tempdir), 'test-ica.fif') for cov in (None, test_cov): ica = ICA(noise_cov=cov, n_components=2, max_pca_components=4, n_pca_components=4) with warnings.catch_warnings(record=True): # ICA does not converge ica.fit(raw, picks=picks, start=start, stop=stop2) sources = ica.get_sources(epochs).get_data() assert_true(ica.mixing_matrix_.shape == (2, 2)) assert_true(ica.unmixing_matrix_.shape == (2, 2)) assert_true(ica.pca_components_.shape == (4, len(picks))) assert_true(sources.shape[1] == ica.n_components_) for exclude in [[], [0]]: ica.exclude = [0] ica.labels_ = {'foo': [0]} ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.exclude == ica_read.exclude) assert_equal(ica.labels_, ica_read.labels_) ica.exclude = [] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == []) ica.exclude = [0, 1] ica.apply(raw, exclude=[1]) assert_true(ica.exclude == [0, 1]) ica_raw = ica.get_sources(raw) assert_true(ica.exclude == [ica_raw.ch_names.index(e) for e in ica_raw.info['bads']]) # test filtering d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.filter(4, 20) assert_true((d1 != ica_raw._data[0]).any()) d1 = ica_raw._data[0].copy() with warnings.catch_warnings(record=True): # dB warning ica_raw.notch_filter([10]) assert_true((d1 != ica_raw._data[0]).any()) ica.n_pca_components = 2 ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) assert_true(ica.n_pca_components == ica_read.n_pca_components) # check type consistency attrs = ('mixing_matrix_ unmixing_matrix_ pca_components_ ' 'pca_explained_variance_ _pre_whitener') def f(x, y): return getattr(x, y).dtype for attr in attrs.split(): assert_equal(f(ica_read, attr), f(ica, attr)) ica.n_pca_components = 4 ica_read.n_pca_components = 4 ica.exclude = [] ica.save(test_ica_fname) ica_read = read_ica(test_ica_fname) for attr in ['mixing_matrix_', 'unmixing_matrix_', 'pca_components_', 'pca_mean_', 'pca_explained_variance_', '_pre_whitener']: assert_array_almost_equal(getattr(ica, attr), getattr(ica_read, attr)) assert_true(ica.ch_names == ica_read.ch_names) assert_true(isinstance(ica_read.info, Info)) sources = ica.get_sources(raw)[:, :][0] sources2 = ica_read.get_sources(raw)[:, :][0] assert_array_almost_equal(sources, sources2) _raw1 = ica.apply(raw, exclude=[1]) _raw2 = ica_read.apply(raw, exclude=[1]) assert_array_almost_equal(_raw1[:, :][0], _raw2[:, :][0]) os.remove(test_ica_fname) # check scrore funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(raw, target='EOG 061', score_func=func, start=0, stop=10) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(raw, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, raw, target=np.arange(1)) params = [] params += [(None, -1, slice(2), [0, 1])] # varicance, kurtosis idx params params += [(None, 'MEG 1531')] # ECG / EOG channel params for idx, ch_name in product(*params): ica.detect_artifacts(raw, start_find=0, stop_find=50, ecg_ch=ch_name, eog_ch=ch_name, skew_criterion=idx, var_criterion=idx, kurt_criterion=idx) with warnings.catch_warnings(record=True): idx, scores = ica.find_bads_ecg(raw, method='ctps') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(raw, method='correlation') assert_equal(len(scores), ica.n_components_) idx, scores = ica.find_bads_ecg(epochs, method='ctps') assert_equal(len(scores), ica.n_components_) assert_raises(ValueError, ica.find_bads_ecg, epochs.average(), method='ctps') assert_raises(ValueError, ica.find_bads_ecg, raw, method='crazy-coupling') idx, scores = ica.find_bads_eog(raw) assert_equal(len(scores), ica.n_components_) raw.info['chs'][raw.ch_names.index('EOG 061') - 1]['kind'] = 202 idx, scores = ica.find_bads_eog(raw) assert_true(isinstance(scores, list)) assert_equal(len(scores[0]), ica.n_components_) # check score funcs for name, func in get_score_funcs().items(): if name in score_funcs_unsuited: continue scores = ica.score_sources(epochs_eog, target='EOG 061', score_func=func) assert_true(ica.n_components_ == len(scores)) # check univariate stats scores = ica.score_sources(epochs, score_func=stats.skew) # check exception handling assert_raises(ValueError, ica.score_sources, epochs, target=np.arange(1)) # ecg functionality ecg_scores = ica.score_sources(raw, target='MEG 1531', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning ecg_events = ica_find_ecg_events(raw, sources[np.abs(ecg_scores).argmax()]) assert_true(ecg_events.ndim == 2) # eog functionality eog_scores = ica.score_sources(raw, target='EOG 061', score_func='pearsonr') with warnings.catch_warnings(record=True): # filter attenuation warning eog_events = ica_find_eog_events(raw, sources[np.abs(eog_scores).argmax()]) assert_true(eog_events.ndim == 2) # Test ica fiff export ica_raw = ica.get_sources(raw, start=0, stop=100) assert_true(ica_raw.last_samp - ica_raw.first_samp == 100) assert_true(len(ica_raw._filenames) == 0) # API consistency ica_chans = [ch for ch in ica_raw.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) test_ica_fname = op.join(op.abspath(op.curdir), 'test-ica_raw.fif') ica.n_components = np.int32(ica.n_components) ica_raw.save(test_ica_fname, overwrite=True) ica_raw2 = Raw(test_ica_fname, preload=True) assert_allclose(ica_raw._data, ica_raw2._data, rtol=1e-5, atol=1e-4) ica_raw2.close() os.remove(test_ica_fname) # Test ica epochs export ica_epochs = ica.get_sources(epochs) assert_true(ica_epochs.events.shape == epochs.events.shape) ica_chans = [ch for ch in ica_epochs.ch_names if 'ICA' in ch] assert_true(ica.n_components_ == len(ica_chans)) assert_true(ica.n_components_ == ica_epochs.get_data().shape[1]) assert_true(ica_epochs._raw is None) assert_true(ica_epochs.preload is True) # test float n pca components ica.pca_explained_variance_ = np.array([0.2] * 5) ica.n_components_ = 0 for ncomps, expected in [[0.3, 1], [0.9, 4], [1, 1]]: ncomps_ = ica._check_n_pca_components(ncomps) assert_true(ncomps_ == expected)
fig = ica.plot_overlay(epochs.average(), exclude=eog_inds, show=False) fig.savefig(ica_folder + "plots/%s_%s_eog_excluded.png" % (subject, condition)) fig = ica.plot_properties(epochs, picks=eog_inds) fig[0].savefig(ica_folder + "plots/%s_%s_plot_properties.png" % ( subject, condition)) # ECG ecg_inds, scores = ica.find_bads_ecg(epochs) ica.exclude += ecg_inds if ecg_inds: fig = ica.plot_components( ecg_inds, title=title % ('ecg', subject), colorbar=True) fig.savefig(ica_folder + "plots/%s_%s_ecg_component.png" % (subject, condition)) fig = ica.plot_overlay(epochs.average(), exclude=ecg_inds, show=False) fig.savefig(ica_folder + "plots/%s_%s_ecg_excluded.png" % (subject, condition)) fig = ica.plot_properties(epochs, picks=ecg_inds) fig[0].savefig(ica_folder + "plots/%s_%s_plot_properties.png" % ( subject, condition)) ########################################################################## # Apply the solution to Raw, Epochs or Evoked like this: epochs_ica = ica.apply(epochs) ica.save(ica_folder + "%s_%s-ica.fif" % (subject, condition)) # save ICA # componenets # Save raw with ICA removed epochs_ica.save(ica_folder + "%s_%s_ar_ica-epo.fif" % (subject, condition))