Skip to content

ICA plot_components on provided axes fails #11653

Closed

Description

@mscheltienne

Description of the problem

from matplotlib import pyplot as plt
from mne.datasets import sample
from mne.io import read_raw_fif
from mne.preprocessing import ICA


# create a toy dataset
fname = sample.data_path() / "MEG" / "sample" / "sample_audvis_raw.fif"
raw = read_raw_fif(fname, preload=False)
raw.pick_types(eeg=True)
raw.crop(0, 10)
raw.load_data()
raw.filter(1.0, 40.0)
ica = ICA(n_components=5, method="picard")
ica.fit(raw)

# create figure
f, ax = plt.subplots(2, 2)
ica.plot_components(inst=raw, picks=[1, 2, 3, 4], axes=ax)

The code snippet above raises:

AttributeError: 'numpy.ndarray' object has no attribute 'set_title'

If you flatten the provided ax, it fails with:

UnboundLocalError: local variable 'fig' referenced before assignment

Working on it atm.

Activity

  1. hoechenberger commented on Apr 24, 2023

    @hoechenberger
    Member

    Looking at the docstring, this is actually the expected behavior and hence not a bug:

    axesinstance of Axes | list of Axes | None
    The axes to plot to. If None, a new Figure will be created with the correct number of axes. If Axes are provided (either as a single instance or a list of axes), the number of axes provided must match the number of times provided (unless times is None).Default is None.

    Since you pass a multi-dimensional array and not a list-like object, the method fails.

    The only bug I can see here is that the error message that is produced is not super helpful – we're obviously failing too late.

    I do appreciate your efforts in #11640 though, but you're actually adding a new functionality there (supporting NDarray input), so it's not a bug fix and would also require you to rework the docstring a bit.

    Please also note that Matplotlib uses super unhelpful terminology in that "Axes" is the singular form. Multiple Axes, as returned e.g. by plt.subplots(2, 2), are … I don't know, Axess or so. – For that reason, I usually call the singular just ax and if I have multiple, I name the variable axs. Maybe this added to your confusion?

    Edit:

    If you flatten the provided ax, it fails with:

    UnboundLocalError: local variable 'fig' referenced before assignment
    

    Okay this bit does seem like a bug to me.

  2. larsoner commented on Apr 24, 2023

    @larsoner
    Member

    To me if list of axes works then 1D ndarray of axes should also work, especially since it's what's returned by plt.subplots for example. Also the fig variable referenced before assignment seems like a(nother) genuine bug to me

  3. mscheltienne commented on Apr 24, 2023

    @mscheltienne
    MemberAuthor

    The UnboundLocalError is definitely a bug, it's also present if you pass a list of axes. But you are right that at the moment the docstring says List of Axes. The same docstring also mentions times for ICA topographic maps.. 😅

    My reasoning would be that an axes argument (or its plural form) should support any output from a matplotlib function that creates axes, as long as the correct number of subplots is provided.

    Sure, you can create them one by one, e.g.

    from matplotlib import pyplot as plt
    
    f = plt.Figure()
    axes = list()
    for k in range(4):
        axes.append(f.add_subplot(2, 2, k + 1))
    

    But you can also create them all at once with plt.subplots which returns always a 2D array of axes, except if squeeze=True (the default). IMO, output from plt.subplots should be supported out of the box by an axes argument which can take multiple subplots.

    It's difficult to make one axes docstring for all MNE-function, but for ica.plot_components, the docstring for axes would become:

    axes : Axes | array of Axes | None
        Either a single Axes or an iterable of Axes if more than one subplot
        is needed. The number of subplots must match the number of 
        selected components. If None, a new figure will be created
        The subplot(s) to plot to. The number of subplots must match the
        number of selected components. If None, new figures will be created
        with the number of subplots per figure controlled by ``nrows`` and ``ncols``
    

    It's not complete as the types at the top don't mention iterable to avoid lengthening the line to:

    axes : Axes | array of Axes | iterable of Axes | None
    

    And it does not mention the 1D / 2D ndarray, but I think that would be adding too much detail.

  4. hoechenberger commented on Apr 24, 2023

    @hoechenberger
    Member

    @larsoner

    To me if list of axes works then 1D ndarray of axes should also work,

    But @mscheltienne passed a 2d array of axes in the example

    @mscheltienne

    MO, output from plt.subplots should be supported out of the box by an axes argument which can take multiple subplots.

    I generally agree, but how would you determine the order in which to fill the axess? Is left -> right from top to bottom what users would expect? (I'd say: yes, but I'm not sure if everybody would agree?)

  5. larsoner commented on Apr 24, 2023

    @larsoner
    Member

    But @mscheltienne passed a 2d array of axes in the example

    Ahh I missed that!

    I generally agree, but how would you determine the order in which to fill the axess (see what I did :D)? Is left -> right from top to bottom what users would expect? (I'd say: yes, but I'm not sure if everybody would agree?)

    I think it's pretty clear by the default C-ordering of NumPy arrays that we will just .ravel() / .flatten() the array, which means it will go across the first row left to right, then the second row left to right, etc. If people want something else they can pass axes.T or explicitly order them.

  6. mscheltienne commented on Apr 24, 2023

    @mscheltienne
    MemberAuthor

    I think it's pretty clear by the default C-ordering of NumPy arrays that we will just .ravel() / .flatten() the array, which means it will go across the first row left to right, then the second row left to right, etc. If people want something else they can pass axes.T or explicitly order them.

    Exactly my thought. We define a default, and if they want a different order, they either provide it explicitly in a 1D array or transpose it.

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