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_n_components_none(): """Test n_components=None.""" raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() events = read_events(event_name) picks = pick_types(raw.info, eeg=True, meg=False) epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) max_pca_components = 10 n_components = None random_state = 12345 tempdir = _TempDir() output_fname = op.join(tempdir, 'test_ica-ica.fif') ica = ICA(max_pca_components=max_pca_components, n_components=n_components, random_state=random_state) with warnings.catch_warnings(record=True): # convergence ica.fit(epochs) ica.save(output_fname) ica = read_ica(output_fname) # ICA.fit() replaced max_pca_components, which was previously None, # with the appropriate integer value. assert_equal(ica.max_pca_components, 10) assert_is_none(ica.n_components)
def test_ica_reset(method): """Test ICA resetting.""" _skip_check_picard(method) 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] run_time_attrs = ( '_pre_whitener', 'unmixing_matrix_', 'mixing_matrix_', 'n_components_', 'n_samples_', 'pca_components_', 'pca_explained_variance_', 'pca_mean_' ) with warnings.catch_warnings(record=True): # convergence ica = ICA( n_components=3, max_pca_components=3, n_pca_components=3, method=method, max_iter=1).fit(raw, picks=picks) assert_true(all(hasattr(ica, attr) for attr in run_time_attrs)) assert_not_equal(ica.labels_, None) ica._reset() assert_true(not any(hasattr(ica, attr) for attr in run_time_attrs)) assert_not_equal(ica.labels_, None)
def test_plot_ica_panel(): """Test plotting of ICA panel """ ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) ica.decompose_raw(raw, picks=ica_picks) ica.plot_sources_raw(raw)
def test_ica_reset(method): """Test ICA resetting.""" _skip_check_picard(method) 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] run_time_attrs = ( 'pre_whitener_', 'unmixing_matrix_', 'mixing_matrix_', 'n_components_', 'n_samples_', 'pca_components_', 'pca_explained_variance_', 'pca_mean_' ) with pytest.warns(UserWarning, match='did not converge'): ica = ICA( n_components=3, max_pca_components=3, n_pca_components=3, method=method, max_iter=1).fit(raw, picks=picks) assert (all(hasattr(ica, attr) for attr in run_time_attrs)) assert ica.labels_ is not None ica._reset() assert (not any(hasattr(ica, attr) for attr in run_time_attrs)) assert ica.labels_ is not None
def test_eog_channel(method): """Test that EOG channel is included when performing ICA.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname, preload=True) events = read_events(event_name) picks = pick_types(raw.info, meg=True, stim=True, ecg=False, eog=True, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) n_components = 0.9 ica = ICA(n_components=n_components, method=method) # Test case for MEG and EOG data. Should have EOG channel for inst in [raw, epochs]: picks1a = pick_types(inst.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:4] picks1b = pick_types(inst.info, meg=False, stim=False, ecg=False, eog=True, exclude='bads') picks1 = np.append(picks1a, picks1b) ica.fit(inst, picks=picks1) assert (any('EOG' in ch for ch in ica.ch_names)) # Test case for MEG data. Should have no EOG channel for inst in [raw, epochs]: picks1 = pick_types(inst.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads')[:5] ica.fit(inst, picks=picks1) assert not any('EOG' in ch for ch in ica.ch_names)
def run_ica(method): ica = ICA(n_components=20, method=method, random_state=0) t0 = time() ica.fit(raw, picks=picks, reject=reject) fit_time = time() - t0 title = ('ICA decomposition using %s (took %.1fs)' % (method, fit_time)) ica.plot_components(title=title)
def test_plot_instance_components(): """Test plotting of components as instances of raw and epochs.""" import matplotlib.pyplot as plt raw = _get_raw() picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) with pytest.warns(RuntimeWarning, match='projection'): ica.fit(raw, picks=picks) fig = ica.plot_sources(raw, exclude=[0], title='Components') for key in ['down', 'up', 'right', 'left', 'o', '-', '+', '=', 'pageup', 'pagedown', 'home', 'end', 'f11', 'b']: fig.canvas.key_press_event(key) ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], 'data') _fake_click(fig, ax, [-0.1, 0.9]) # click on y-label fig.canvas.key_press_event('escape') plt.close('all') epochs = _get_epochs() fig = ica.plot_sources(epochs, exclude=[0], title='Components') for key in ['down', 'up', 'right', 'left', 'o', '-', '+', '=', 'pageup', 'pagedown', 'home', 'end', 'f11', 'b']: fig.canvas.key_press_event(key) # Test a click ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], 'data') _fake_click(fig, ax, [-0.1, 0.9]) # click on y-label fig.canvas.key_press_event('escape') plt.close('all')
def test_n_components_and_max_pca_components_none(method): """Test n_components and max_pca_components=None.""" _skip_check_picard(method) raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() events = read_events(event_name) picks = pick_types(raw.info, eeg=True, meg=False) epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), preload=True) max_pca_components = None n_components = None random_state = 12345 tempdir = _TempDir() output_fname = op.join(tempdir, 'test_ica-ica.fif') ica = ICA(max_pca_components=max_pca_components, method=method, n_components=n_components, random_state=random_state) with pytest.warns(None): # convergence ica.fit(epochs) ica.save(output_fname) ica = read_ica(output_fname) # ICA.fit() replaced max_pca_components, which was previously None, # with the appropriate integer value. assert_equal(ica.max_pca_components, epochs.info['nchan']) assert ica.n_components is None
def apply_ica(fname_filtered, n_components=0.99, decim=None): ''' Applies ICA to a list of (filtered) raw files. ''' import mne from mne.preprocessing import ICA import os if isinstance(fname_filtered, list): fnfilt = fname_filtered else: if isinstance(fname_filtered, str): fnfilt = list([fname_filtered]) else: fnfilt = list(fname_filtered) # loop across all filenames for fname in fnfilt: name = os.path.split(fname)[1] print ">>>> perform ICA signal decomposition on : "+name # load filtered data raw = mne.io.Raw(fname,preload=True) picks = mne.pick_types(raw.info, meg=True, exclude='bads') # ICA decomposition ica = ICA(n_components=n_components, max_pca_components=None) ica.fit(raw, picks=picks, decim=decim, reject={'mag': 5e-12}) # save ICA object fnica_out = fname.strip('-raw.fif') + '-ica.fif' # fnica_out = fname[0:len(fname)-4]+'-ica.fif' ica.save(fnica_out)
def test_ica_reset(): """Test ICA resetting""" 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] run_time_attrs = ( '_pre_whitener', 'unmixing_matrix_', 'mixing_matrix_', 'n_components_', 'n_samples_', 'pca_components_', 'pca_explained_variance_', 'pca_mean_' ) with warnings.catch_warnings(record=True): ica = ICA( n_components=3, max_pca_components=3, n_pca_components=3, method='fastica', max_iter=1).fit(raw, picks=picks) assert_true(all(hasattr(ica, attr) for attr in run_time_attrs)) ica._reset() assert_true(not any(hasattr(ica, attr) for attr in run_time_attrs))
def test_plot_ica_panel(): """Test plotting of ICA panel """ ica_picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude="bads") cov = read_cov(cov_fname) ica = ICA(noise_cov=cov, n_components=2, max_pca_components=3, n_pca_components=3) ica.decompose_raw(raw, picks=ica_picks) ica.plot_sources_raw(raw)
def decompose(self, X, y=None): raw_inst = RawArray(X.T, create_info(self.channel_names, self.fs, 'eeg', None)) ica = ICA(method='extended-infomax') ica.fit(raw_inst) filters = np.dot(ica.unmixing_matrix_, ica.pca_components_[:ica.n_components_]).T topographies = np.linalg.inv(filters).T scores = self.get_scores(X, filters) return scores, filters, topographies
def test_plot_ica_scores(): """Test plotting of ICA scores """ raw = _get_raw() ica_picks = pick_types(raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude="bads") ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) ica.fit(raw, picks=ica_picks) ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1]) assert_raises(ValueError, ica.plot_scores, [0.2]) plt.close("all")
def test_plot_ica_panel(): """Test plotting of ICA panel """ raw = _get_raw() ica_picks = pick_types(raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) ica.decompose_raw(raw, picks=ica_picks) ica.plot_sources_raw(raw) plt.close('all')
def test_plot_ica_scores(): """Test plotting of ICA scores """ raw = _get_raw() picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) ica.fit(raw, picks=picks) ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1]) assert_raises(ValueError, ica.plot_scores, [0.2]) plt.close('all')
def apply_ica(fname_filtered, n_components=0.99, decim=None, reject={'mag': 5e-12}, ica_method='fastica', flow=None, fhigh=None, verbose=True): ''' Applies ICA to a list of (filtered) raw files. ''' from mne.preprocessing import ICA fnfilt = get_files_from_list(fname_filtered) # loop across all filenames for fname in fnfilt: name = os.path.split(fname)[1] print ">>>> perform ICA signal decomposition on : " + name # load filtered data raw = mne.io.Raw(fname, preload=True) picks = mne.pick_types(raw.info, meg=True, ref_meg=False, exclude='bads') # check if data to estimate the optimal # de-mixing matrix should be filtered if flow or fhigh: from jumeg.filter import jumeg_filter # define filter type if not flow: filter_type = 'lp' filter_info = " --> filter parameter : filter type=low pass %dHz" % flow elif not fhigh: filter_type = 'hp' filter_info = " --> filter parameter : filter type=high pass %dHz" % flow else: filter_type = 'bp' filter_info = " --> filter parameter: filter type=band pass %d-%dHz" % (flow, fhigh) if verbose: print ">>>> NOTE: Optimal cleaning parameter are estimated from filtered data!" print filter_info fi_mne_notch = jumeg_filter(fcut1=flow, fcut2=fhigh, filter_type=filter_type, remove_dcoffset=False, sampling_frequency=raw.info['sfreq']) fi_mne_notch.apply_filter(raw._data, picks=picks) # ICA decomposition ica = ICA(method=ica_method, n_components=n_components, max_pca_components=None) ica.fit(raw, picks=picks, decim=decim, reject=reject) # save ICA object fnica_out = fname[:fname.rfind(ext_raw)] + ext_ica # fnica_out = fname[0:len(fname)-4]+'-ica.fif' ica.save(fnica_out)
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_plot_ica_components(): """Test plotting of ICA solutions.""" res = 8 fast_test = {"res": res, "contours": 0, "sensors": False} raw = _get_raw() ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) ica_picks = _get_picks(raw) with pytest.warns(RuntimeWarning, match='projection'): ica.fit(raw, picks=ica_picks) for components in [0, [0], [0, 1], [0, 1] * 2, None]: ica.plot_components(components, image_interp='bilinear', colorbar=True, **fast_test) plt.close('all') # test interactive mode (passing 'inst' arg) ica.plot_components([0, 1], image_interp='bilinear', inst=raw, res=16) fig = plt.gcf() # test title click # ---------------- lbl = fig.axes[1].get_label() ica_idx = int(lbl[-3:]) titles = [ax.title for ax in fig.axes] title_pos_midpoint = (titles[1].get_window_extent().extents .reshape((2, 2)).mean(axis=0)) # first click adds to exclude _fake_click(fig, fig.axes[1], title_pos_midpoint, xform='pix') assert ica_idx in ica.exclude # clicking again removes from exclude _fake_click(fig, fig.axes[1], title_pos_midpoint, xform='pix') assert ica_idx not in ica.exclude # test topo click # --------------- _fake_click(fig, fig.axes[1], (0., 0.), xform='data') c_fig = plt.gcf() labels = [ax.get_label() for ax in c_fig.axes] for l in ['topomap', 'image', 'erp', 'spectrum', 'variance']: assert (l in labels) topomap_ax = c_fig.axes[labels.index('topomap')] title = topomap_ax.get_title() assert (lbl == title) ica.info = None with pytest.raises(RuntimeError, match='fit the ICA'): ica.plot_components(1, ch_type='mag') plt.close('all')
def test_plot_instance_components(): """Test plotting of components as instances of raw and epochs.""" import matplotlib.pyplot as plt raw = _get_raw() picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) with warnings.catch_warnings(record=True): # bad proj ica.fit(raw, picks=picks) fig = ica.plot_sources(raw, exclude=[0], title="Components") fig.canvas.key_press_event("down") fig.canvas.key_press_event("up") fig.canvas.key_press_event("right") fig.canvas.key_press_event("left") fig.canvas.key_press_event("o") fig.canvas.key_press_event("-") fig.canvas.key_press_event("+") fig.canvas.key_press_event("=") fig.canvas.key_press_event("pageup") fig.canvas.key_press_event("pagedown") fig.canvas.key_press_event("home") fig.canvas.key_press_event("end") fig.canvas.key_press_event("f11") ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], "data") _fake_click(fig, ax, [-0.1, 0.9]) # click on y-label fig.canvas.key_press_event("escape") plt.close("all") epochs = _get_epochs() fig = ica.plot_sources(epochs, exclude=[0], title="Components") fig.canvas.key_press_event("down") fig.canvas.key_press_event("up") fig.canvas.key_press_event("right") fig.canvas.key_press_event("left") fig.canvas.key_press_event("o") fig.canvas.key_press_event("-") fig.canvas.key_press_event("+") fig.canvas.key_press_event("=") fig.canvas.key_press_event("pageup") fig.canvas.key_press_event("pagedown") fig.canvas.key_press_event("home") fig.canvas.key_press_event("end") fig.canvas.key_press_event("f11") # Test a click ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], "data") _fake_click(fig, ax, [-0.1, 0.9]) # click on y-label fig.canvas.key_press_event("escape") plt.close("all")
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_plot_instance_components(): """Test plotting of components as instances of raw and epochs.""" import matplotlib.pyplot as plt raw = _get_raw() picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) with warnings.catch_warnings(record=True): # bad proj ica.fit(raw, picks=picks) fig = ica.plot_sources(raw, exclude=[0], title='Components') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.key_press_event('right') fig.canvas.key_press_event('left') fig.canvas.key_press_event('o') fig.canvas.key_press_event('-') fig.canvas.key_press_event('+') fig.canvas.key_press_event('=') fig.canvas.key_press_event('pageup') fig.canvas.key_press_event('pagedown') fig.canvas.key_press_event('home') fig.canvas.key_press_event('end') fig.canvas.key_press_event('f11') ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], 'data') _fake_click(fig, ax, [-0.1, 0.9]) # click on y-label fig.canvas.key_press_event('escape') plt.close('all') epochs = _get_epochs() fig = ica.plot_sources(epochs, exclude=[0], title='Components') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.key_press_event('right') fig.canvas.key_press_event('left') fig.canvas.key_press_event('o') fig.canvas.key_press_event('-') fig.canvas.key_press_event('+') fig.canvas.key_press_event('=') fig.canvas.key_press_event('pageup') fig.canvas.key_press_event('pagedown') fig.canvas.key_press_event('home') fig.canvas.key_press_event('end') fig.canvas.key_press_event('f11') # Test a click ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], 'data') _fake_click(fig, ax, [-0.1, 0.9]) # click on y-label fig.canvas.key_press_event('escape') plt.close('all')
def test_ica_reject_buffer(): """Test ICA data raw buffer rejection""" 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') ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw._data[2, 1000:1005] = 5e-12 drop_log = op.join(op.dirname(tempdir), 'ica_drop.log') set_log_file(drop_log, overwrite=True) ica.decompose_raw(raw, picks[:5], reject=dict(mag=2.5e-12), decim=2, tstep=0.01, verbose=True) assert_true(raw._data[:5, ::2].shape[1] - 4 == ica.n_samples_) log = [l for l in open(drop_log) if 'detected' in l] assert_equal(len(log), 1)
def test_ica_reject_buffer(): """Test ICA data raw buffer rejection.""" raw = read_raw_fif(raw_fname).crop(1.5, stop).load_data() picks = pick_types(raw.info, meg=True, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw._data[2, 1000:1005] = 5e-12 with catch_logging() as drop_log: with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5], reject=dict(mag=2.5e-12), decim=2, tstep=0.01, verbose=True) assert_true(raw._data[:5, ::2].shape[1] - 4 == ica.n_samples_) log = [l for l in drop_log.getvalue().split('\n') if 'detected' in l] assert_equal(len(log), 1)
def test_ica_simple(method): """Test that ICA recovers the unmixing matrix in a simple case.""" _skip_check_picard(method) n_components = 3 n_samples = 1000 rng = np.random.RandomState(0) S = rng.laplace(size=(n_components, n_samples)) A = rng.randn(n_components, n_components) data = np.dot(A, S) ica = ICA(n_components=n_components, method=method, random_state=0) ica._fit(data, n_components, 0) transform = np.dot(np.dot(ica.unmixing_matrix_, ica.pca_components_), A) amari_distance = np.mean(np.sum(np.abs(transform), axis=1) / np.max(np.abs(transform), axis=1) - 1.) assert amari_distance < 0.1
def test_ica_reject_buffer(): """Test ICA data raw buffer rejection""" tempdir = _TempDir() raw = io.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") ica = ICA(n_components=3, max_pca_components=4, n_pca_components=4) raw._data[2, 1000:1005] = 5e-12 drop_log = op.join(op.dirname(tempdir), "ica_drop.log") set_log_file(drop_log, overwrite=True) with warnings.catch_warnings(record=True): ica.fit(raw, picks[:5], reject=dict(mag=2.5e-12), decim=2, tstep=0.01, verbose=True) assert_true(raw._data[:5, ::2].shape[1] - 4 == ica.n_samples_) with open(drop_log) as fid: log = [l for l in fid if "detected" in l] assert_equal(len(log), 1)
def test_plot_ica_overlay(): """Test plotting of ICA cleaning """ raw = _get_raw(preload=True) picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) ica.fit(raw, picks=picks) # don't test raw, needs preload ... ecg_epochs = create_ecg_epochs(raw, picks=picks) ica.plot_overlay(ecg_epochs.average()) eog_epochs = create_eog_epochs(raw, picks=picks) ica.plot_overlay(eog_epochs.average()) assert_raises(ValueError, ica.plot_overlay, raw[:2, :3][0]) ica.plot_overlay(raw) plt.close('all')
def test_plot_ica_sources(): """Test plotting of ICA panel """ raw = io.Raw(raw_fname, preload=True) picks = _get_picks(raw) epochs = _get_epochs() picks = np.round(np.linspace(0, len(picks) + 1, n_chan)).astype(int) raw.pick_channels([raw.ch_names[k] for k in picks]) ica_picks = pick_types(raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude="bads") ica = ICA(n_components=2, max_pca_components=3, n_pca_components=3) ica.fit(raw, picks=ica_picks) ica.plot_sources(raw) ica.plot_sources(epochs) ica.plot_sources(epochs.average()) assert_raises(ValueError, ica.plot_sources, "meeow") plt.close("all")
def test_ica_simple(method): """Test that ICA recovers the unmixing matrix in a simple case.""" if method == "fastica": try: import sklearn # noqa: F401 except ImportError: raise SkipTest("scikit-learn not installed") _skip_check_picard(method) n_components = 3 n_samples = 1000 rng = np.random.RandomState(0) S = rng.laplace(size=(n_components, n_samples)) A = rng.randn(n_components, n_components) data = np.dot(A, S) ica = ICA(n_components=n_components, method=method, random_state=0) ica._fit(data, n_components, 0) transform = np.dot(np.dot(ica.unmixing_matrix_, ica.pca_components_), A) amari_distance = np.mean(np.sum(np.abs(transform), axis=1) / np.max(np.abs(transform), axis=1) - 1.) assert amari_distance < 0.1
def test_plot_ica_sources(): """Test plotting of ICA panel.""" raw = read_raw_fif(raw_fname).crop(0, 1).load_data() picks = _get_picks(raw) epochs = _get_epochs() raw.pick_channels([raw.ch_names[k] for k in picks]) ica_picks = pick_types(raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(n_components=2, max_pca_components=3, n_pca_components=3) ica.fit(raw, picks=ica_picks) ica.exclude = [1] fig = ica.plot_sources(raw) fig.canvas.key_press_event('escape') # Sadly close_event isn't called on Agg backend and the test always passes. assert_array_equal(ica.exclude, [1]) plt.close('all') # dtype can change int->np.int after load, test it explicitly ica.n_components_ = np.int64(ica.n_components_) fig = ica.plot_sources(raw) # also test mouse clicks data_ax = fig.axes[0] assert len(plt.get_fignums()) == 1 _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label assert len(plt.get_fignums()) == 2 ica.exclude = [1] ica.plot_sources(raw) # test with annotations orig_annot = raw.annotations raw.set_annotations(Annotations([0.2], [0.1], 'Test')) fig = ica.plot_sources(raw) assert len(fig.axes[0].collections) == 1 assert len(fig.axes[1].collections) == 1 raw.set_annotations(orig_annot) raw.info['bads'] = ['MEG 0113'] with pytest.raises(RuntimeError, match="Raw doesn't match fitted data"): ica.plot_sources(inst=raw) ica.plot_sources(epochs) epochs.info['bads'] = ['MEG 0113'] with pytest.raises(RuntimeError, match="Epochs don't match fitted data"): ica.plot_sources(inst=epochs) epochs.info['bads'] = [] ica.plot_sources(epochs.average()) evoked = epochs.average() fig = ica.plot_sources(evoked) # Test a click ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], 'data') _fake_click(fig, ax, [ax.get_xlim()[0], ax.get_ylim()[1]], 'data') # plot with bad channels excluded ica.exclude = [0] ica.plot_sources(evoked) ica.labels_ = dict(eog=[0]) ica.labels_['eog/0/crazy-channel'] = [0] ica.plot_sources(evoked) # now with labels with pytest.raises(ValueError, match='must be of Raw or Epochs type'): ica.plot_sources('meeow') plt.close('all')
def test_plot_ica_scores(): """Test plotting of ICA scores.""" raw = _get_raw() picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) with pytest.warns(RuntimeWarning, match='projection'): ica.fit(raw, picks=picks) ica.labels_ = dict() ica.labels_['eog/0/foo'] = 0 ica.labels_['eog'] = 0 ica.labels_['ecg'] = 1 ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1]) ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1], labels='foo') ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1], labels='eog') ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1], labels='ecg') pytest.raises(ValueError, ica.plot_scores, [0.3, 0.2], axhline=[0.1, -0.1], labels=['one', 'one-too-many']) pytest.raises(ValueError, ica.plot_scores, [0.2]) plt.close('all')
raw = mne.io.read_raw_fif(raw_fname) raw.pick_types(meg=True, eeg=False, exclude='bads', stim=True).load_data() raw.filter(1, 30, fir_design='firwin') # peak-to-peak amplitude rejection parameters reject = dict(grad=4000e-13, mag=4e-12) # longer + more epochs for more artifact exposure events = mne.find_events(raw, stim_channel='STI 014') epochs = mne.Epochs(raw, events, event_id=None, tmin=-0.2, tmax=0.5, reject=reject) ############################################################################### # Fit ICA model using the FastICA algorithm, detect and plot components # explaining ECG artifacts. ica = ICA(n_components=0.95, method='fastica').fit(epochs) ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, threshold='auto') ica.plot_components(ecg_inds) ############################################################################### # Plot properties of ECG components: ica.plot_properties(epochs, picks=ecg_inds) ############################################################################### # Plot the estimated source of detected ECG related components: ica.plot_sources(raw, picks=ecg_inds)
def preproc_manual(subject): global report from mne.preprocessing import ICA from mne.io import read_raw_fif get_ipython().run_line_magic('matplotlib', 'inline') raw_path = op.join(rest_dir, subject, 'meg_clean', 'clean_raw.fif') raw = read_raw_fif(raw_path, preload=True) pick_meg = mne.pick_types(raw.info, meg=True, eeg=False, stim=False, ref_meg=False) fig = raw.plot_psd(area_mode='range', average=False, picks=pick_meg) report.add_figs_to_section(fig, captions=subject + ' => PSD before artifact correction', section='Filtering') ##-- Artifact removal --## random_state = 23 # we want to have the same decomposition and the same order of components as in the first preprocessing n_components = 25 ica = ICA(n_components, method='fastica', random_state=random_state) reject = dict( mag=5e-12, grad=4000e-13 ) # avoid fitting ICA on crazy environmental artifacts that would dominate the variance and decomposition ica.fit(raw, picks=pick_meg, reject=reject) fig = ica.plot_components(inst=raw) report.add_figs_to_section(fig, captions=[subject + ' => ICA components'] * len(fig), section='Artifact removal') flag = True comp_inds = [] while flag == True: aux = int(input('ICA components to remove? (100 to finish)')) if aux == 100: flag = False else: comp_inds.append(aux) ica.exclude.extend(comp_inds) str1 = ','.join(str(c) for c in comp_inds) logFile.write(subject + ": " + str1 + "\n") raw_clean = raw.copy() ica.apply(raw_clean) fig = raw_clean.plot_psd(area_mode='range', picks=pick_meg) report.add_figs_to_section(fig, captions=subject + ' => PSD after artifact correction', section='Filtering') raw_clean.save(raw_path, overwrite=True) raw.save(op.join(rest_dir, subject, 'meg_clean', 'firstclean_raw.fif'), overwrite=True)
def ica_correction(raw, picks): """Function "ica_correction" will correct artifacts in eeg data (eg. blink) using ICA and return an ICA array under "my-ica.fif" Will also plot ICA components""" from mne.preprocessing import ICA from mne.preprocessing import create_eog_epochs #ICA parameters n_components = 25 # if float, select n_components by explained variance of PCA method = 'fastica' # for comparison with EEGLAB try "extended-infomax" here decim = 3 # we need sufficient statistics, not all time points -> saves time # we will also set state of the random number generator - ICA is a # non-deterministic algorithm, but we want to have the same decomposition # and the same order of components each time this tutorial is run random_state = 23 ica = ICA(n_components=n_components, method=method, random_state=random_state) ica.fit(raw, picks=picks, decim=decim, reject=dict(eeg=200e-6)) ica.plot_components() n_max_eog = 1 #don't expect to find horizontal eog components title = 'Sources related to EOG components (red)' eog_epochs = create_eog_epochs(raw, tmin=-.5, tmax=1, picks=picks) eog_inds, scores = ica.find_bads_eog(eog_epochs) ica.plot_scores(scores, exclude=eog_inds, title=title, labels='eog') show_picks = np.abs(scores).argsort()[::-1][:5] ica.plot_sources(raw, show_picks, exclude=eog_inds, title=title) ica.plot_components(eog_inds, title=title, colorbar=True) eog_inds = eog_inds[:n_max_eog] ica.exclude += eog_inds # uncomment this for reading and writing #ica.save('my-ica.fif') #ica = read_ica('my-ica.fif') #apply to epochs ica.apply(epochs)
##..........................................................................## ## Down Sampling ## #raw_sss.resample(100, npad="auto") # set sampling frequency to 100Hz #raw_sss.plot_psd(area_mode='range', tmax=10.0, picks=picks, average=True) ##..........................................................................## ## ICA Artifact Removal ## n_components = .99 method = 'fastica' decim = 3 picks_all= mne.pick_types(raw_sss.info, meg=True, eeg=True, eog=True, stim=False) ica = ICA(n_components=n_components, method=method) reject = dict(mag=4e-12, grad=4000e-13) ica.fit(raw_sss, picks=picks_all, decim=decim, reject=reject) print(ica) ica.plot_components() #ica.plot_properties(raw_sss, picks=0) ##................................EOG Removal............................... ## title = 'Sources related to %s artifacts (red)' n_max_ecg, n_max_eog = 1, 2 #EOG061/EOG062/ECG063 #eog_average = create_eog_epochs(raw_sss, reject=reject, # picks=picks_all).average()
# Setup paths and prepare raw data data_path = sample.data_path() raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' raw = Raw(raw_fname, preload=True) raw.filter(1, 45, n_jobs=2) ############################################################################### # 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=True, eeg=False, eog=False, stim=False, exclude='bads') ica.fit(raw, picks=picks, decim=3, reject=dict(mag=4e-12, grad=4000e-13)) # maximum number of components to reject n_max_ecg, n_max_eog = 3, 1 # here we don't expect horizontal EOG components ############################################################################### # 2) identify bad components by analyzing latent sources. title = 'Sources related to %s artifacts (red)' # generate ECG epochs use detection via phase statistics
# decomposition that happens before the ICA. Therefore, we probably don't need # a huge number of components to do a good job of isolating our artifacts # (though it is usually preferable to include more components for a more # accurate solution). As a first guess, we'll run ICA with ``n_components=15`` # (use only the first 15 PCA components to compute the ICA decomposition) — a # very small number given that our data has over 300 channels, but with the # advantage that it will run quickly and we will able to tell easily whether it # worked or not (because we already know what the EOG / ECG artifacts should # look like). # # ICA fitting is not deterministic (e.g., the components may get a sign # flip on different runs, or may not always be returned in the same order), so # we'll also specify a `random seed`_ so that we get identical results each # time this tutorial is built by our web servers. ica = ICA(n_components=15, random_state=97) ica.fit(filt_raw) ############################################################################### # Some optional parameters that we could have passed to the # `~mne.preprocessing.ICA.fit` method include ``decim`` (to use only # every Nth sample in computing the ICs, which can yield a considerable # speed-up) and ``reject`` (for providing a rejection dictionary for maximum # acceptable peak-to-peak amplitudes for each channel type, just like we used # when creating epoched data in the :ref:`tut-overview` tutorial). # # Now we can examine the ICs to see what they captured. # `~mne.preprocessing.ICA.plot_sources` will show the time series of the # ICs. Note that in our call to `~mne.preprocessing.ICA.plot_sources` we # can use the original, unfiltered `~mne.io.Raw` object:
def apply_ica_data(fname, raw=None, do_run=False, verbose=False, save=True, fif_extention=".fif", fif_postfix="-ica", **kwargs): """ apply mne ica return fnica_out : fif filename of mne ica-obj raw : fif-raw obj ICAobj : mne-ica-object Attributes ---------- current_fit : str Flag informing about which data type (raw or epochs) was used for the fit. ch_names : list-like Channel names resulting from initial picking. The number of components used for ICA decomposition. n_components_` : int If fit, the actual number of components used for ICA decomposition. n_pca_components : int See above. max_pca_components : int The number of components used for PCA dimensionality reduction. verbose : bool, str, int, or None See above. pca_components_` : ndarray If fit, the PCA components pca_mean_` : ndarray If fit, the mean vector used to center the data before doing the PCA. pca_explained_variance_` : ndarray If fit, the variance explained by each PCA component mixing_matrix_` : ndarray If fit, the mixing matrix to restore observed data, else None. unmixing_matrix_` : ndarray If fit, the matrix to unmix observed data, else None. exclude : list List of sources indices to exclude, i.e. artifact components identified throughout the ICA solution. Indices added to this list, will be dispatched to the .pick_sources methods. Source indices passed to the .pick_sources method via the 'exclude' argument are added to the .exclude attribute. When saving the ICA also the indices are restored. Hence, artifact components once identified don't have to be added again. To dump this 'artifact memory' say: ica.exclude = [] info : None | instance of mne.io.meas_info.Info The measurement info copied from the object fitted. n_samples_` : int the number of samples used on fit. """ ICAobj = None if do_run: raw, fname = jumeg_base.get_raw_obj(fname, raw=raw) from mne.preprocessing import ICA picks = jumeg_base.pick_meg_nobads(raw) #--- init MNE ICA obj kwargs['global_parameter']['verbose'] = verbose ICAobj = ICA(**kwargs['global_parameter']) #--- run mne ica kwargs['fit_parameter']['verbose'] = verbose ICAobj.fit(raw, picks=picks, **kwargs['fit_parameter']) fnica_out = fname[:fname.rfind('-raw.fif' )] + fif_postfix + fif_extention # fnica_out = fname[0:len(fname)-4]+'-ica.fif' #--- save ICA object if save: ICAobj.save(fnica_out) print "===> Done JuMEG MNE ICA : " + fnica_out print "\n" return (fnica_out, raw, ICAobj)
epc = [] for ep in epochs[ epoch]: # Highest value is 79 corresponding to epoch number 80 epc = ep return epc epc = eeg(0) #" Plots " #plt.figure(1) #plt.subplot(2, 1, 1) #plt.plot(epc[0]) #plt.title("First Channel of Epoch 1") # #plt.subplot(2, 1, 2) #for p in epc: # plt.plot(p) #plt.title("All Channels of Epoch 1") # ## Epochs own plotting function #epochs.plot(n_epochs=1, n_channels=1) #plt.title("Epoch Plot - epoch 1 and channel 1") # from mne.preprocessing import ICA ica = ICA(n_components=30, method='fastica') S_ = ica.fit(epochs) # Get the estimated sources A_ = ica.mixing_matrix_ # Get estimated mixing matrix
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 warnings.catch_warnings(record=True): # convergence ica.fit(raw) # 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 warnings.catch_warnings(record=True): # convergence 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)
method = 'fastica' # for comparison with EEGLAB try "extended-infomax" here decim = 3 # we need sufficient statistics, not all time points -> saves time # we will also set state of the random number generator - ICA is a # non-deterministic algorithm, but we want to have the same decomposition # and the same order of components each time this tutorial is run random_state = 23 picks_eeg = mne.pick_types(ep.info, meg=False, eeg=True, eog=False, stim=False, exclude='bads') ############################################################################### # Define the ICA object instance ica = ICA(n_components=n_components, method=method, random_state=random_state) print(ica) #%%############################################################################## # we avoid fitting ICA on crazy environmental artifacts that would # dominate the variance and decomposition reject = dict(eeg=40e-6) ica.fit(ep, picks=picks_eeg, reject=reject, decim=decim) print(ica) #ica.plot_components() # can you spot some potential bad guys? #%%############################################################################## # Component properties # Let's take a closer look at properties of first three independent components. # first, component 0: #ica.plot_properties(d, picks=0) # we can see that the data were filtered so the spectrum plot is not
def test_plot_ica_sources(): """Test plotting of ICA panel """ import matplotlib.pyplot as plt raw = io.read_raw_fif(raw_fname, preload=False).crop(0, 1, copy=False).load_data() picks = _get_picks(raw) epochs = _get_epochs() raw.pick_channels([raw.ch_names[k] for k in picks]) ica_picks = pick_types(raw.info, meg=True, eeg=False, stim=False, ecg=False, eog=False, exclude='bads') ica = ICA(n_components=2, max_pca_components=3, n_pca_components=3) ica.fit(raw, picks=ica_picks) raw.info['bads'] = ['MEG 0113'] assert_raises(RuntimeError, ica.plot_sources, inst=raw) ica.plot_sources(epochs) epochs.info['bads'] = ['MEG 0113'] assert_raises(RuntimeError, ica.plot_sources, inst=epochs) epochs.info['bads'] = [] with warnings.catch_warnings(record=True): # no labeled objects mpl ica.plot_sources(epochs.average()) evoked = epochs.average() fig = ica.plot_sources(evoked) # Test a click ax = fig.get_axes()[0] line = ax.lines[0] _fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], 'data') _fake_click(fig, ax, [ax.get_xlim()[0], ax.get_ylim()[1]], 'data') # plot with bad channels excluded ica.plot_sources(evoked, exclude=[0]) ica.exclude = [0] ica.plot_sources(evoked) # does the same thing ica.labels_ = dict(eog=[0]) ica.labels_['eog/0/crazy-channel'] = [0] ica.plot_sources(evoked) # now with labels assert_raises(ValueError, ica.plot_sources, 'meeow') plt.close('all')
# ------- # # ICA parameters: n_components = 25 # if float, select n_components by explained variance of PCA method = 'fastica' # for comparison with EEGLAB try "extended-infomax" here decim = 3 # we need sufficient statistics, not all time points -> saves time # we will also set state of the random number generator - ICA is a # non-deterministic algorithm, but we want to have the same decomposition # and the same order of components each time this tutorial is run random_state = 23 ############################################################################### # Define the ICA object instance ica = ICA(n_components=n_components, method=method, random_state=random_state) print(ica) ############################################################################### # we avoid fitting ICA on crazy environmental artifacts that would # dominate the variance and decomposition reject = dict(mag=5e-12, grad=4000e-13) ica.fit(raw, picks=picks_meg, decim=decim, reject=reject) print(ica) ############################################################################### # Plot ICA components ica.plot_components() # can you spot some potential bad guys? ###############################################################################
tmin, tmax = -0.2, 0.6 baseline = None # no baseline as high-pass is applied 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, max_iter='auto', random_state=0) ica.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
def test_ica_labels(): """Test ICA labels.""" # The CTF data are uniquely well suited to testing the ICA.find_bads_ # methods raw = read_raw_ctf(ctf_fname, preload=True) # derive reference ICA components and append them to raw icarf = ICA(n_components=2, random_state=0, max_iter=2, allow_ref_meg=True) with pytest.warns(UserWarning, match='did not converge'): icarf.fit(raw.copy().pick_types(meg=False, ref_meg=True)) icacomps = icarf.get_sources(raw) # rename components so they are auto-detected by find_bads_ref icacomps.rename_channels({c: 'REF_' + c for c in icacomps.ch_names}) # and add them to raw raw.add_channels([icacomps]) # set the appropriate EEG channels to EOG and ECG raw.set_channel_types({'EEG057': 'eog', 'EEG058': 'eog', 'EEG059': 'ecg'}) ica = ICA(n_components=4, random_state=0, max_iter=2, method='fastica') with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw) ica.find_bads_eog(raw, l_freq=None, h_freq=None) picks = list(pick_types(raw.info, meg=False, eog=True)) for idx, ch in enumerate(picks): assert '{}/{}/{}'.format('eog', idx, raw.ch_names[ch]) in ica.labels_ assert 'eog' in ica.labels_ for key in ('ecg', 'ref_meg', 'ecg/ECG-MAG'): assert key not in ica.labels_ ica.find_bads_ecg(raw, l_freq=None, h_freq=None, method='correlation') picks = list(pick_types(raw.info, meg=False, ecg=True)) for idx, ch in enumerate(picks): assert '{}/{}/{}'.format('ecg', idx, raw.ch_names[ch]) in ica.labels_ for key in ('ecg', 'eog'): assert key in ica.labels_ for key in ('ref_meg', 'ecg/ECG-MAG'): assert key not in ica.labels_ ica.find_bads_ref(raw, l_freq=None, h_freq=None) picks = pick_channels_regexp(raw.ch_names, 'REF_ICA*') for idx, ch in enumerate(picks): assert '{}/{}/{}'.format('ref_meg', idx, raw.ch_names[ch]) in ica.labels_ for key in ('ecg', 'eog', 'ref_meg'): assert key in ica.labels_ assert 'ecg/ECG-MAG' not in ica.labels_ ica.find_bads_ecg(raw, l_freq=None, h_freq=None) for key in ('ecg', 'eog', 'ref_meg', 'ecg/ECG-MAG'): assert key in ica.labels_
annotations = mne.Annotations( annot_onset, duration, description ) raw.set_annotations(annotations) # ICA n_components = 50 method = "fastica" max_iter = 10000 ica = ICA( n_components=n_components, method=method, max_iter=max_iter ) ica.fit( raw, reject_by_annotation=True ) ica.save(ica_output_path) print("ICA saved") raw.annotations.delete(list(range(80))) raw.save(raw_output_path, overwrite=True) print("RAW saved")
def test_fit_params(method): """Test fit_params for ICA.""" _skip_check_picard(method) fit_params = {} ICA(fit_params=fit_params, method=method) # test no side effects assert_equal(fit_params, {})
def applyICA(self, raw, method='extended-infomax', decim=None): ''' Arguments - - - - - self(object): Epochs object raw (object): n_components (): method (str): decim (): Returns - - - - self ''' # initiate ica logging.info('Started ICA') picks = mne.pick_types(self.info, eeg=True, exclude='bads') ica = ICA(n_components=picks.size, method=method) # ica is fitted on epoched data ica.fit(self, 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() # 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)))) # diagnostic plotting ica.plot_sources(eog_epochs.average(), exclude=eog_inds, show=False) plt.savefig(self.FolderTracker(extension=[ 'preprocessing', 'subject-{}'.format(self.sj), self.session], filename='sources.pdf')) plt.close() for i, cmpt in enumerate(eog_inds): ica.plot_properties(eog_epochs, 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))
for run in range(2, 7): raw.append(mne.io.read_raw_fif( fname.filt(subject=subject, run=run, fmin=bandpass_fmin, fmax=bandpass_fmax), preload=False)) # SSS reduces the data rank and the noise levels, so let's include # components based on a higher proportion of variance explained (0.999) # than we would otherwise do for non-Maxwell-filtered raw data (0.98) n_components = 0.999 # Define the parameters for the ICA. There is a random component to this. # We set a specific seed for the random state, so this script produces exactly # the same results every time. print('Fitting ICA') ica = ICA(method='fastica', random_state=42, n_components=n_components) # To compute the ICA, we don't need all data. We just need enough data to # perform the statistics to a reasonable degree. Here, we use every 11th # sample. We also apply some rejection limits to discard epochs with overly # large signals that are likely due to the subject moving about. ica.fit(raw, reject=dict(grad=4000e-13, mag=4e-12), decim=11) print('Fit %d components (explaining at least %0.1f%% of the variance)' % (ica.n_components_, 100 * n_components)) # Find onsets of heart beats and blinks. Create epochs around them ecg_epochs = create_ecg_epochs(raw, tmin=-.3, tmax=.3, preload=False) eog_epochs = create_eog_epochs(raw, tmin=-.5, tmax=.5, preload=False) # Find ICA components that correlate with heart beats. ecg_epochs.decimate(5)
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") 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 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)
def test_plot_ica_components(): """Test plotting of ICA solutions.""" res = 8 fast_test = {"res": res, "contours": 0, "sensors": False} raw = _get_raw() ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) ica_picks = _get_picks(raw) with pytest.warns(RuntimeWarning, match='projection'): ica.fit(raw, picks=ica_picks) for components in [0, [0], [0, 1], [0, 1] * 2, None]: ica.plot_components(components, image_interp='bilinear', colorbar=True, **fast_test) plt.close('all') # test interactive mode (passing 'inst' arg) ica.plot_components([0, 1], image_interp='bilinear', inst=raw, res=16) fig = plt.gcf() # test title click # ---------------- lbl = fig.axes[1].get_label() ica_idx = int(lbl[-3:]) titles = [ax.title for ax in fig.axes] title_pos_midpoint = (titles[1].get_window_extent().extents.reshape( (2, 2)).mean(axis=0)) # first click adds to exclude _fake_click(fig, fig.axes[1], title_pos_midpoint, xform='pix') assert ica_idx in ica.exclude # clicking again removes from exclude _fake_click(fig, fig.axes[1], title_pos_midpoint, xform='pix') assert ica_idx not in ica.exclude # test topo click # --------------- _fake_click(fig, fig.axes[1], (0., 0.), xform='data') c_fig = plt.gcf() labels = [ax.get_label() for ax in c_fig.axes] for l in ['topomap', 'image', 'erp', 'spectrum', 'variance']: assert (l in labels) topomap_ax = c_fig.axes[labels.index('topomap')] title = topomap_ax.get_title() assert (lbl == title) ica.info = None with pytest.raises(RuntimeError, match='fit the ICA'): ica.plot_components(1, ch_type='mag') plt.close('all')
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) 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_plot_ica_overlay(): """Test plotting of ICA cleaning.""" raw = _get_raw(preload=True) picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3, random_state=0) # can't use info.normalize_proj here because of how and when ICA and Epochs # objects do picking of Raw data with pytest.warns(RuntimeWarning, match='projection'): ica.fit(raw, picks=picks) # don't test raw, needs preload ... with pytest.warns(RuntimeWarning, match='projection'): ecg_epochs = create_ecg_epochs(raw, picks=picks) ica.plot_overlay(ecg_epochs.average()) with pytest.warns(RuntimeWarning, match='projection'): eog_epochs = create_eog_epochs(raw, picks=picks) ica.plot_overlay(eog_epochs.average()) pytest.raises(TypeError, ica.plot_overlay, raw[:2, :3][0]) pytest.raises(TypeError, ica.plot_overlay, raw, exclude=2) ica.plot_overlay(raw) plt.close('all') # smoke test for CTF raw = read_raw_fif(raw_ctf_fname) raw.apply_gradient_compensation(3) picks = pick_types(raw.info, meg=True, ref_meg=False) ica = ICA(n_components=2, max_pca_components=3, n_pca_components=3) ica.fit(raw, picks=picks) with pytest.warns(RuntimeWarning, match='longer than'): ecg_epochs = create_ecg_epochs(raw) ica.plot_overlay(ecg_epochs.average()) plt.close('all')
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) # 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_plot_ica_properties(): """Test plotting of ICA properties.""" res = 8 raw = _get_raw(preload=True) raw.add_proj([], remove_existing=True) events = _get_events() picks = _get_picks(raw)[:6] pick_names = [raw.ch_names[k] for k in picks] raw.pick_channels(pick_names) reject = dict(grad=4000e-13, mag=4e-12) epochs = Epochs(raw, events[:10], event_id, tmin, tmax, baseline=(None, 0), preload=True) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_iter=1, max_pca_components=2, n_pca_components=2, random_state=0) with pytest.warns(RuntimeWarning, match='projection'): ica.fit(raw) # test _create_properties_layout fig, ax = _create_properties_layout() assert_equal(len(ax), 5) topoargs = dict(topomap_args={'res': res, 'contours': 0, "sensors": False}) ica.plot_properties(raw, picks=0, **topoargs) ica.plot_properties(epochs, picks=1, dB=False, plot_std=1.5, **topoargs) ica.plot_properties(epochs, picks=1, image_args={'sigma': 1.5}, topomap_args={ 'res': 10, 'colorbar': True }, psd_args={'fmax': 65.}, plot_std=False, figsize=[4.5, 4.5], reject=reject) plt.close('all') pytest.raises(TypeError, ica.plot_properties, epochs, dB=list('abc')) pytest.raises(TypeError, ica.plot_properties, ica) pytest.raises(TypeError, ica.plot_properties, [0.2]) pytest.raises(TypeError, plot_ica_properties, epochs, epochs) pytest.raises(TypeError, ica.plot_properties, epochs, psd_args='not dict') pytest.raises(ValueError, ica.plot_properties, epochs, plot_std=[]) fig, ax = plt.subplots(2, 3) ax = ax.ravel()[:-1] ica.plot_properties(epochs, picks=1, axes=ax, **topoargs) fig = ica.plot_properties(raw, picks=[0, 1], **topoargs) assert_equal(len(fig), 2) pytest.raises(TypeError, plot_ica_properties, epochs, ica, picks=[0, 1], axes=ax) pytest.raises(ValueError, ica.plot_properties, epochs, axes='not axes') plt.close('all') # Test merging grads. pick_names = raw.ch_names[:15:2] + raw.ch_names[1:15:2] raw = _get_raw(preload=True).pick_channels(pick_names) raw.info.normalize_proj() ica = ICA(random_state=0, max_iter=1) with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw) ica.plot_properties(raw) plt.close('all') # Test handling of zeros raw._data[:] = 0 with pytest.warns(None): # Usually UserWarning: Infinite value .* for epo ica.plot_properties(raw) ica = ICA(random_state=0, max_iter=1) epochs.pick_channels(pick_names) with pytest.warns(UserWarning, match='did not converge'): ica.fit(epochs) epochs._data[0] = 0 with pytest.warns(None): # Usually UserWarning: Infinite value .* for epo ica.plot_properties(epochs) plt.close('all')
def inscapesMEG_PP(fname, DATA_FOLDER, SAVE_FOLDER): fpath = DATA_FOLDER + fname raw = read_raw_ctf(fpath, preload=True) picks = mne.pick_types(raw.info, meg=True, eog=True, exclude='bads') raw.plot() raw.plot_psd(average=False, picks=picks) ## Filtering high_cutoff = 200 low_cutoff = 0.5 raw.filter(low_cutoff, high_cutoff, fir_design="firwin") raw.notch_filter(np.arange(60, high_cutoff + 1, 60), picks=picks, filter_length='auto', phase='zero', fir_design="firwin") raw.plot_psd(average=False, picks=picks) ## ICA ica = ICA(n_components=20, random_state=0).fit(raw, decim=3) ica.plot_sources(raw) fmax = 40. ## correlation threshold for ICA components (maybe increase to 40. ?) ## FIND ECG COMPONENTS ecg_epochs = create_ecg_epochs(raw, ch_name='EEG059') ecg_inds, ecg_scores = ica.find_bads_ecg(ecg_epochs, ch_name='EEG059') ica.plot_scores(ecg_scores, ecg_inds) ica.plot_properties(ecg_epochs, picks=ecg_inds, psd_args={'fmax': fmax}, image_args={'sigma': 1.}) ## FIND EOG COMPONENTS eog_epochs = create_eog_epochs(raw, ch_name='EEG057') eog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name='EEG057') ica.plot_scores(eog_scores, eog_inds) ica.plot_properties(eog_epochs, picks=eog_inds, psd_args={'fmax': fmax}, image_args={'sigma': 1.}) ## EXCLUDE COMPONENTS ica.exclude = ecg_inds ica.apply(raw) ica.exclude = eog_inds ica.apply(raw) raw.plot() # Plot the clean signal. ## SAVE PREPROCESSED FILE time.sleep(60) raw.save(SAVE_FOLDER + fname + '_preprocessed.fif.gz', overwrite=True) time.sleep(30) filename = SAVE_FOLDER + fname + '_log.html' #!jupyter nbconvert inscapesMEG_preproc.ipynb --output $filename clear_output()
'chunk': idx, 'step': t2.block_name, 'time': t2.elapsed_secs }, ignore_index=True) # Apply ICA with Timer( block_name='ICA', verbose=True, ) as t3: sample_raw_train = sample_raw_bandpass.copy() sample_raw_corrected = sample_raw_bandpass.copy() # Train ica = ICA(method="extended-infomax", random_state=1) ica.fit(sample_raw_corrected) timings = timings.append( { 'task': i, 'chunk': idx, 'step': t3.block_name, 'time': t3.elapsed_secs }, ignore_index=True) # Plot ICA component # eog_channels.plot(title="EOG_Channels", duration=15, n_channels=10, scalings=dict(eeg=1200e-6)) # ica.plot_sources(inst=sample_raw_train) # ica.plot_components(inst=sample_raw_train)
def test_plot_ica_scores(): """Test plotting of ICA scores """ import matplotlib.pyplot as plt raw = _get_raw() picks = _get_picks(raw) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_pca_components=3, n_pca_components=3) with warnings.catch_warnings(record=True): # bad proj ica.fit(raw, picks=picks) ica.labels_ = dict() ica.labels_['eog/0/foo'] = 0 ica.labels_['eog'] = 0 ica.labels_['ecg'] = 1 ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1]) ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1], labels='foo') ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1], labels='eog') ica.plot_scores([0.3, 0.2], axhline=[0.1, -0.1], labels='ecg') assert_raises( ValueError, ica.plot_scores, [0.3, 0.2], axhline=[0.1, -0.1], labels=['one', 'one-too-many']) assert_raises(ValueError, ica.plot_scores, [0.2]) plt.close('all')