def _create_ica_properties_fig(self, idx): """Show ICA properties for the selected component.""" from mne.viz.ica import (_create_properties_layout, _prepare_data_ica_properties, _fast_plot_ica_properties) ch_name = self.mne.ch_names[idx] if ch_name not in self.mne.ica._ica_names: # for EOG chans: do nothing return pick = self.mne.ica._ica_names.index(ch_name) title = f'{ch_name} properties' fig = self._new_child_figure(figsize=(7, 6), fig_name=None, window_title=title) fig.suptitle(title) fig, axes = _create_properties_layout(fig=fig) if not hasattr(self.mne, 'data_ica_properties'): # Precompute epoch sources only once self.mne.data_ica_properties = _prepare_data_ica_properties( self.mne.ica_inst, self.mne.ica) _fast_plot_ica_properties( self.mne.ica, self.mne.ica_inst, picks=pick, axes=axes, precomputed_data=self.mne.data_ica_properties, show=False) return fig
def test_plot_ica_properties(): """Test plotting of ICA properties.""" import matplotlib.pyplot as plt 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) with warnings.catch_warnings(record=True): # bad proj 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_pca_components=2, n_pca_components=2) with warnings.catch_warnings(record=True): # bad proj 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]) plt.close('all') assert_raises(ValueError, ica.plot_properties, epochs, dB=list('abc')) assert_raises(ValueError, ica.plot_properties, epochs, plot_std=[]) assert_raises(ValueError, ica.plot_properties, ica) assert_raises(ValueError, ica.plot_properties, [0.2]) assert_raises(ValueError, plot_ica_properties, epochs, epochs) assert_raises(ValueError, ica.plot_properties, epochs, psd_args='not dict') 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) assert_raises(ValueError, plot_ica_properties, epochs, ica, picks=[0, 1], axes=ax) assert_raises(ValueError, ica.plot_properties, epochs, axes='not axes') plt.close('all') # Test merging grads. raw = _get_raw(preload=True) picks = pick_types(raw.info, meg='grad')[:10] ica = ICA(n_components=2) ica.fit(raw, picks=picks) ica.plot_properties(raw) plt.close('all')
def test_plot_ica_properties(): """Test plotting of ICA properties.""" import matplotlib.pyplot as plt 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) with warnings.catch_warnings(record=True): # bad proj 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_pca_components=2, n_pca_components=2) with warnings.catch_warnings(record=True): # bad proj ica.fit(raw) # test _create_properties_layout fig, ax = _create_properties_layout() assert_equal(len(ax), 5) topoargs = dict(topomap_args={'res': 10}) 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]) plt.close('all') assert_raises(ValueError, ica.plot_properties, epochs, dB=list('abc')) assert_raises(ValueError, ica.plot_properties, epochs, plot_std=[]) assert_raises(ValueError, ica.plot_properties, ica) assert_raises(ValueError, ica.plot_properties, [0.2]) assert_raises(ValueError, plot_ica_properties, epochs, epochs) assert_raises(ValueError, ica.plot_properties, epochs, psd_args='not dict') fig, ax = plt.subplots(2, 3) ax = ax.ravel()[:-1] ica.plot_properties(epochs, picks=1, axes=ax) fig = ica.plot_properties(raw, picks=[0, 1], **topoargs) assert_equal(len(fig), 2) assert_raises(ValueError, plot_ica_properties, epochs, ica, picks=[0, 1], axes=ax) assert_raises(ValueError, ica.plot_properties, epochs, axes='not axes') plt.close('all') # Test merging grads. raw = _get_raw(preload=True) picks = pick_types(raw.info, meg='grad')[:10] ica = ICA(n_components=2) ica.fit(raw, picks=picks) ica.plot_properties(raw) plt.close('all')
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_pca_components=2, n_pca_components=2) 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. raw = _get_raw(preload=True) picks = pick_types(raw.info, meg='grad')[:10] ica = ICA(n_components=2) ica.fit(raw, picks=picks) ica.plot_properties(raw) plt.close('all')
def test_plot_ica_properties(): """Test plotting of ICA properties.""" import matplotlib.pyplot as plt 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) with warnings.catch_warnings(record=True): # bad proj 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_pca_components=2, n_pca_components=2) with warnings.catch_warnings(record=True): # bad proj ica.fit(raw) # test _create_properties_layout fig, ax = _create_properties_layout() assert_equal(len(ax), 5) topoargs = dict(topomap_args={"res": 10}) 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.0}, plot_std=False, figsize=[4.5, 4.5], ) plt.close("all") assert_raises(ValueError, ica.plot_properties, epochs, dB=list("abc")) assert_raises(ValueError, ica.plot_properties, epochs, plot_std=[]) assert_raises(ValueError, ica.plot_properties, ica) assert_raises(ValueError, ica.plot_properties, [0.2]) assert_raises(ValueError, plot_ica_properties, epochs, epochs) assert_raises(ValueError, ica.plot_properties, epochs, psd_args="not dict") fig, ax = plt.subplots(2, 3) ax = ax.ravel()[:-1] ica.plot_properties(epochs, picks=1, axes=ax) fig = ica.plot_properties(raw, picks=[0, 1], **topoargs) assert_equal(len(fig), 2) assert_raises(ValueError, plot_ica_properties, epochs, ica, picks=[0, 1], axes=ax) assert_raises(ValueError, ica.plot_properties, epochs, axes="not axes") plt.close("all")
def test_plot_ica_properties(): """Test plotting of ICA properties.""" raw = _get_raw(preload=True).crop(0, 5) raw.add_proj([], remove_existing=True) with raw.info._unlock(): raw.info['highpass'] = 1.0 # fake high-pass filtering events = make_fixed_length_events(raw) 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[:3], event_id, tmin, tmax, baseline=(None, 0), preload=True) ica = ICA(noise_cov=read_cov(cov_fname), n_components=2, max_iter=1, 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) with pytest.raises(ValueError, match='specify both fig and figsize'): _create_properties_layout(figsize=(2, 2), fig=fig) topoargs = dict(topomap_args={'res': 4, 'contours': 0, "sensors": False}) with catch_logging() as log: ica.plot_properties(raw, picks=0, verbose='debug', **topoargs) log = log.getvalue() assert raw.ch_names[0] == 'MEG 0113' assert 'Interpolation mode local to mean' in log, log 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': 4, 'colorbar': True }, psd_args={'fmax': 65.}, plot_std=False, figsize=[4.5, 4.5], reject=reject) plt.close('all') with pytest.raises(TypeError, match='must be an instance'): ica.plot_properties(epochs, dB=list('abc')) with pytest.raises(TypeError, match='must be an instance'): ica.plot_properties(ica) with pytest.raises(TypeError, match='must be an instance'): ica.plot_properties([0.2]) with pytest.raises(TypeError, match='must be an instance'): plot_ica_properties(epochs, epochs) with pytest.raises(TypeError, match='must be an instance'): ica.plot_properties(epochs, psd_args='not dict') with pytest.raises(TypeError, match='must be an instance'): 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) 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).crop(0, 5) 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 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 # Usually UserWarning: Infinite value .* for epo with _record_warnings(): ica.plot_properties(epochs, **topoargs) plt.close('all') # Test Raw with annotations annot = Annotations(onset=[1], duration=[1], description=['BAD']) raw_annot = _get_raw(preload=True).set_annotations(annot).crop(0, 8) raw_annot.pick(np.arange(10)) raw_annot.del_proj() with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw_annot) # drop bad data segments fig = ica.plot_properties(raw_annot, picks=[0, 1], **topoargs) assert_equal(len(fig), 2) # don't drop ica.plot_properties(raw_annot, reject_by_annotation=False, **topoargs)
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') # Test Raw with annotations annot = Annotations(onset=[1], duration=[1], description=['BAD']) raw_annot = _get_raw(preload=True).set_annotations(annot) with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw_annot) # drop bad data segments ica.plot_properties(raw_annot) # don't drop ica.plot_properties(raw_annot, reject_by_annotation=False) # fitting with bad data with pytest.warns(UserWarning, match='did not converge'): ica.fit(raw_annot, reject_by_annotation=False) # drop bad data when plotting ica.plot_properties(raw_annot) # don't drop bad data when plotting ica.plot_properties(raw_annot, reject_by_annotation=False) plt.close('all')