def test_ica_full_data_recovery(): """Test recovery of full data when no source is rejected""" # Most basic recovery raw = fiff.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) events = read_events(event_name) picks = fiff.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) n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() for n_components, n_pca_components, ok in [(2, n_channels, True), (2, n_channels // 2, False)]: ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components) ica.decompose_raw(raw, picks=list(range(n_channels))) raw2 = ica.pick_sources_raw(raw, 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) ica.decompose_epochs(epochs, picks=list(range(n_channels))) epochs2 = ica.pick_sources_epochs(epochs, 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)
def test_ica_full_data_recovery(): """Test recovery of full data when no source is rejected""" # Most basic recovery raw = io.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) 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) n_channels = 5 data = raw._data[:n_channels].copy() data_epochs = epochs.get_data() for n_components, n_pca_components, ok in [(2, n_channels, True), (2, n_channels // 2, False)]: ica = ICA(n_components=n_components, max_pca_components=n_pca_components, n_pca_components=n_pca_components) ica.decompose_raw(raw, picks=list(range(n_channels))) raw2 = ica.pick_sources_raw(raw, 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) ica.decompose_epochs(epochs, picks=list(range(n_channels))) epochs2 = ica.pick_sources_epochs(epochs, 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)
def test_ica_core(): """Test ICA on raw and epochs """ 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') # 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] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_) # # 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 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) print(ica) # to test repr # test fit checker assert_raises(RuntimeError, ica.get_sources_raw, raw) assert_raises(RuntimeError, ica.get_sources_epochs, epochs) # test decomposition ica.decompose_raw(raw, picks=pcks, start=start, stop=stop) print(ica) # to test repr # test re-init exception assert_raises(RuntimeError, ica.decompose_raw, raw, picks=picks) sources = ica.get_sources_raw(raw) assert_true(sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3._preloaded = False assert_raises(ValueError, ica.pick_sources_raw, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition # test re-init exception assert_raises(RuntimeError, ica.decompose_epochs, epochs, picks=picks) ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) ica.decompose_epochs(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 # test pick block after epochs fit assert_raises(ValueError, ica.pick_sources_raw, raw) sources = ica.get_sources_epochs(epochs) assert_true(sources.shape[1] == ica.n_components_) assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False assert_raises(ValueError, ica.pick_sources_epochs, epochs3, include=[1, 2])
picks=picks, baseline=(None, -0.2), preload=True, reject=reject) # In[6]: mne.viz.plot_drop_log(epochs.drop_log) #### ICA ica = ICA(n_components=0.90, n_pca_components=64, max_pca_components=100, noise_cov=None) ica.decompose_epochs(epochs) eog_scores_1_normal = ica.find_sources_epochs(epochs, target="EOG001", score_func="pearsonr") eog_scores_2_normal = ica.find_sources_epochs(epochs, target="EOG003", score_func="pearsonr") # get maximum correlation index for EOG eog_source_idx_1_normal = np.abs(eog_scores_1_normal).argmax() eog_source_idx_2_normal = np.abs(eog_scores_2_normal).argmax() source_idx = range(0, ica.n_components_) ica.plot_topomap(source_idx, ch_type="mag") # select ICA sources and reconstruct MEG signals, compute clean ERFs
def test_ica_core(): """Test ICA on raw and epochs """ raw = fiff.Raw(raw_fname, preload=True).crop(0, stop, False).crop(1.5) picks = fiff.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 = fiff.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] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_) # # 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 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) print(ica) # to test repr # test fit checker assert_raises(RuntimeError, ica.get_sources_raw, raw) assert_raises(RuntimeError, ica.get_sources_epochs, epochs) # test decomposition ica.decompose_raw(raw, picks=pcks, start=start, stop=stop) print(ica) # to test repr # test re-init exception assert_raises(RuntimeError, ica.decompose_raw, raw, picks=picks) sources = ica.get_sources_raw(raw) assert_true(sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3._preloaded = False assert_raises(ValueError, ica.pick_sources_raw, raw3, include=[1, 2]) ####################################################################### # test epochs decomposition # test re-init exception assert_raises(RuntimeError, ica.decompose_epochs, epochs, picks=picks) ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) ica.decompose_epochs(epochs, picks=picks) print(ica) # to test repr # test pick block after epochs fit assert_raises(ValueError, ica.pick_sources_raw, raw) sources = ica.get_sources_epochs(epochs) assert_true(sources.shape[1] == ica.n_components_) assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False assert_raises(ValueError, ica.pick_sources_epochs, epochs3, include=[1, 2])
def test_ica_core(): """Test ICA on raw and epochs """ # setup parameter # XXX. The None cases helped revealing bugs but are time consuming. noise_cov = [None, test_cov] # removed None cases to speed up... n_components = [3, 1.0] # for future dbg add cases max_pca_components = [4] picks_ = [picks] iter_ica_params = product(noise_cov, n_components, max_pca_components, picks_) # # test init catchers assert_raises(ValueError, ICA, n_components=3, max_pca_components=2) assert_raises(ValueError, ICA, n_components=1.3, max_pca_components=2) # test essential core functionality for n_cov, n_comp, max_n, pcks 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) print ica # to test repr # test fit checker assert_raises(RuntimeError, ica.get_sources_raw, raw) assert_raises(RuntimeError, ica.get_sources_epochs, epochs) # test decomposition ica.decompose_raw(raw, picks=pcks, start=start, stop=stop) print ica # to test repr # test re-init exception assert_raises(RuntimeError, ica.decompose_raw, raw, picks=picks) sources = ica.get_sources_raw(raw) assert_true(sources.shape[0] == ica.n_components_) # test preload filter raw3 = raw.copy() raw3._preloaded = False assert_raises(ValueError, ica.pick_sources_raw, raw3, include=[1, 2]) for excl, incl in (([], []), ([], [1, 2]), ([1, 2], [])): raw2 = ica.pick_sources_raw(raw, exclude=excl, include=incl, copy=True) assert_array_almost_equal(raw2[:, :][1], raw[:, :][1]) ####################################################################### # test epochs decomposition # test re-init exception assert_raises(RuntimeError, ica.decompose_epochs, epochs, picks=picks) ica = ICA(noise_cov=n_cov, n_components=n_comp, max_pca_components=max_n, n_pca_components=max_n, random_state=0) ica.decompose_epochs(epochs, picks=picks) print ica # to test repr # test pick block after epochs fit assert_raises(ValueError, ica.pick_sources_raw, raw) sources = ica.get_sources_epochs(epochs) assert_true(sources.shape[1] == ica.n_components_) assert_raises(ValueError, ica.find_sources_epochs, epochs, target=np.arange(1)) # test preload filter epochs3 = epochs.copy() epochs3.preload = False assert_raises(ValueError, ica.pick_sources_epochs, epochs3, include=[1, 2]) # test source picking for excl, incl in (([], []), ([], [1, 2]), ([1, 2], [])): epochs2 = ica.pick_sources_epochs(epochs, exclude=excl, include=incl, copy=True) assert_array_almost_equal(epochs2.get_data(), epochs.get_data())