Skip to content

Selection of ICA components when plotting properties #11274

Closed

Description

@mscheltienne

mne-python/mne/viz/ica.py

Lines 422 to 423 in 45d87fb

limit = min(5, ica.n_components_) if picks is None else len(ica.ch_names)
picks = _picks_to_idx(ica.info, picks, 'all')[:limit]

Shouldn't this be limit=min(5, ica.n_components_) if picks is None else ica.n_components_?
Consider an ICA fitted with 55 components on 57 EEG channels:

ica.plot_properties(raw, picks=55)  # max is 54 since it picks on components

will currently fail with IndexError: index 55 is out of bounds for axis 1 with size 55, a bit after the previous code snippet, here:

ica_data = np.swapaxes(data[:, picks, :], 0, 1)

Overall, I think limit should be updated and the validity of the provided picks should be asserted at the beginning of this function, with an error message more informative.

EDIT: Also, the use of picks = _picks_to_idx(ica.info, picks, 'all')[:limit] doesn't feel right as _picks_to_idx selects on channels and not components.

Activity

  1. agramfort commented on Oct 24, 2022

    @agramfort
    SponsorMember
  2. added this to the 1.3 milestone on Oct 24, 2022
  3. mscheltienne commented on Oct 27, 2022

    @mscheltienne
    MemberAuthor

    Before opening a PR, I rapidly checked the code that uses _fast_plot_ica_properties:

    • ICA.plot_ica_properties, which is also used by ica.plot_components(inst=...), as expected
    • in mne.viz._figure.py, in BrowserBase._create_ica_properties_fig

    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

    I'm a bit concern by this second one and by what type might be stored in self.mne.ica._ica_names.. BrowserBase._create_ica_properties_fig is called in _create_ch_context_fig:

    def _create_ch_context_fig(self, idx):
    """Show context figure; idx is index of **visible** channels."""
    inst = self.mne.instance_type
    pick = self.mne.picks[idx]
    if inst == 'raw':
    fig = self._create_ch_location_fig(pick)
    elif inst == 'ica':
    fig = self._create_ica_properties_fig(pick)
    else:
    fig = self._create_epoch_image_fig(pick)
    return fig

    which is called by the matplotlib pick event in MNEBrowseFigure.

    I don't get in which context this chain of functions is called, the only browser I can think of related to the ICA is ica.plot_sources(inst), and I don't get where the plot_properties comes up in this one. I'm not very familiar with the plotting API, any idea?

  4. larsoner commented on Oct 31, 2022

    @larsoner
    Member

    I didn't want to have to follow our code so I tried trusting our tests by adding a raise RuntimeError early in _fast_plot_ica_properties, it looks like both failure modes are thankfully visible:

    __________________________________________________________________________ test_plot_ica_properties __________________________________________________________________________
    mne/viz/tests/test_ica.py:144: in test_plot_ica_properties
        ica.plot_properties(raw, picks=0, verbose='debug', **topoargs)
    mne/preprocessing/ica.py:2205: in plot_properties
        return plot_ica_properties(self, inst, picks=picks, axes=axes,
    <decorator-gen-164>:10: in plot_ica_properties
        ???
    mne/viz/ica.py:392: in plot_ica_properties
        return _fast_plot_ica_properties(ica, inst, picks=picks, axes=axes, dB=dB,
    mne/viz/ica.py:410: in _fast_plot_ica_properties
        raise RuntimeError
    E   RuntimeError
    _____________________________________________________________________ test_plot_ica_sources[matplotlib] ______________________________________________________________________
    mne/viz/tests/test_ica.py:279: in test_plot_ica_sources
        fig._click_ch_name(ch_index=0, button=3)
    mne/viz/_mpl_figure.py:2005: in _click_ch_name
        _click_ch_name(self, ch_index, button)
    mne/utils/_testing.py:435: in _click_ch_name
        _fake_click(fig, fig.mne.ax_main, (x, y), xform='pix',
    mne/viz/utils.py:788: in _fake_click
        fig.canvas.callbacks.process(
    ../matplotlib/lib/matplotlib/cbook/__init__.py:307: in process
        func(*args, **kwargs)
    ../matplotlib/lib/matplotlib/figure.py:2506: in pick
        super().pick(mouseevent)
    ../matplotlib/lib/matplotlib/artist.py:525: in pick
        a.pick(mouseevent)
    ../matplotlib/lib/matplotlib/artist.py:525: in pick
        a.pick(mouseevent)
    ../matplotlib/lib/matplotlib/artist.py:525: in pick
        a.pick(mouseevent)
    ../matplotlib/lib/matplotlib/artist.py:525: in pick
        a.pick(mouseevent)
    ../matplotlib/lib/matplotlib/artist.py:511: in pick
        mouseevent, self, **prop)._process()
    ../matplotlib/lib/matplotlib/backend_bases.py:1231: in _process
        self.canvas.callbacks.process(self.name, self)
    ../matplotlib/lib/matplotlib/cbook/__init__.py:307: in process
        func(*args, **kwargs)
    mne/viz/_mpl_figure.py:779: in _pick
        self._create_ch_context_fig(ind)
    mne/viz/_mpl_figure.py:782: in _create_ch_context_fig
        fig = super()._create_ch_context_fig(idx)
    mne/viz/_figure.py:444: in _create_ch_context_fig
        fig = self._create_ica_properties_fig(pick)
    mne/viz/_figure.py:494: in _create_ica_properties_fig
        _fast_plot_ica_properties(
    mne/viz/ica.py:410: in _fast_plot_ica_properties
        raise RuntimeError
    E   RuntimeError
    
  5. drammock commented on Nov 1, 2022

    @drammock
    Member

    I didn't want to have to follow our code

    aside if even @larsoner feels this way, it seems we are ripe for a refactor / code readability improvements (var names, comments, etc) 😆

  6. mscheltienne commented on Nov 1, 2022

    @mscheltienne
    MemberAuthor

    aside if even @larsoner feels this way, it seems we are ripe for a refactor / code readability improvements (var names, comments, etc) 😆

    That's a big tell ahah

    Thank you @larsoner, I did not have time to look at it yet, but that's a great starting point! I'll try to dig in this week or next week.

  7. removed this from the 1.3 milestone on Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions