Skip to content

ica.plot_properties(reject='auto') raises IndexError when fit-time reject dropped any segments #13879

Closed

Description

@pjdurka

Description

Calling ica.plot_properties(raw) (or ica.plot_properties(raw, reject='auto'),
the default) raises IndexError: index N is out of bounds for axis 0 with size N-K for every component whenever the underlying raw contains
at least one 2-second window whose peak-to-peak amplitude exceeds the
fit-time reject threshold (ica.reject_).

This affects the report path too: mne.Report.add_ica(...) propagates the
same error (and on Windows with joblib's loky backend, the traceback gets
rewritten to "Could not pickle the task to send it to the workers").

PR #13746 (released in 1.12) addressed a related failure mode but didn't
fix this one — the OOB just moved from epoch_var[dropped_indices] to
drop_var[dropped_indices] (1.12+ line 666 of mne/viz/ica.py).

Steps to reproduce (no external data)

import numpy as np                                        
import mne
from mne.preprocessing import ICA

sfreq, duration = 250.0, 60.0
n = int(sfreq * duration)                                                                                                    
ch_names = ["Fp1","Fp2","F7","F3","Fz","F4","F8","T3","C3","Cz",
            "C4","T4","T5","P3","Pz","P4","T6","O1","O2"]                                                                    
                                                          
rng = np.random.default_rng(0)                                                                                               
data = rng.standard_normal((19, n)) * 3e-6                                                                                   
# 5 mV spike in the LAST 2-sec window, so the bad-window epoch index
# is the maximum possible (= N), guaranteeing OOB on length-(N-1)                                                            
# post-rejection arrays.                                  
data[0, n - int(0.5*sfreq) : n - int(0.4*sfreq)] += 5e-3                                                                     
                                                          
raw = mne.io.RawArray(                                                                                                       
    data, mne.create_info(ch_names, sfreq, "eeg"), verbose=False)                                                            
raw.set_montage("standard_1020")
                                                                                                                             
ica = ICA(n_components=5, method="picard", random_state=0,
          fit_params=dict(ortho=False, extended=True))                                                                       
ica.fit(raw, reject=dict(eeg=500e-6))   # spike exceeds 500 µV -> drops window
                                                                                                                             
ica.plot_properties(raw, picks=[0], show=False)           
# IndexError: index 30 is out of bounds for axis 0 with size 29                                                              
                                                                                                                             
Expected behavior
                                                                                                                             
Either skip the dropped-segment markers gracefully or compute their                                                          
positions in the same index space as the per-epoch arrays.
                                                                                                                             
Actual behavior                                                                                                              

IndexError for every component. Affects ica.plot_properties,                                                                 
mne.Report.add_ica, and the click-to-inspect callback from
ica.plot_components.                                                                                                         

Root cause                                                                                                                   
                                                          
In mne/viz/ica.py::_prepare_data_ica_properties, when inst is Raw                                                            
and reject='auto' (which uses ica.reject_):
                                                                                                                             
1. _reject_data_segments(data, reject, ..., tstep=2) returns                                                                 
drop_inds — sample-index pairs into the original signal.                                                                     
2. The post-rejection data is wrapped into a shorter RawArray and                                                            
epoched into epochs_src with N - K epochs (where K = len(drop_inds)).                                                        
3. dropped_indices = [(d[0] // len(epochs_src.times)) + 1 for d in drop_inds]                                                
computes original-space epoch numbers (values up to N).                                                                      
4. In _plot_ica_properties / _fast_plot_ica_properties, those indices                                                        
are used to subscript epoch_var / drop_var, which were computed                                                              
from epochs_src and therefore live in post-rejection space                                                                   
(length N - K).                                                                                                              
                                                                                                                             
epoch_var[dropped_indices] (≤1.11) and drop_var[dropped_indices]                                                             
(1.12+) overflow by exactly K whenever K ≥ 1.                                                                                
                                                                                                                             
Worked example (60 s @ 250 Hz, 1 bad window at the end)                                                                      
                                                                                                                             
epoch_len = 500 samples, N = 30 epochs total                                                                                 
bad window samples = [14500, 15000) -> d[0] = 14500                                                                          
dropped_indices = [14500 // 500 + 1] = [30]                                                                                  
post-rejection length = 29                                                                                                   
drop_var[30] -> index 30 out of bounds for axis 0 with size 29                                                               
                                                                                                                             
Suggested fix
                                                                                                                             
The intent at line 663–666 (on main) is "splice zero-variance entries                                                        
back into epoch_var at the original-space drop positions". drop_var
is built from dropped_src[idx] where dropped_src is the dropped_seg                                                          
returned by _reject_data_segments, and indexed with the                                                                      
post-rejection-space np.arange(K) — not with dropped_indices.                                                                
So:                                                                                                                          
                                                                                                                             
# current (1.12, mne/viz/ica.py:660-668):                                                                                    
epoch_var = np.insert(                                                                                                       
    arr=epoch_var,
    obj=insertion_obj,                                                                                                       
    values=drop_var[dropped_indices],   # OOB             
    axis=0,                                                                                                                  
)
                                                                                                                             
# fix:                                                    
epoch_var = np.insert(
    arr=epoch_var,
    obj=insertion_obj,                                                                                                       
    values=drop_var,                    # already in 0..K-1 space
    axis=0,                                                                                                                  
)                                                         
                                                                                                                             
(I haven't traced the full data flow to verify drop_var length equals                                                        
K in every edge case)
                                                                                                                             
Versions                                                  
                                                                                                                             
Reproduced on 1.11.0, 1.12.0, 1.12.1, and main (HEAD as of 2026-05-01).        

Diagnosis and reproducer drafted with [Claude Code](https://claude.com/claude-code) (Anthropic, Claude Opus 4.7)

Activity

  1. welcome commented on May 1, 2026

    @welcome

    Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴

  2. LeonardoMelloni commented on May 4, 2026

    @LeonardoMelloni

    Hi @pjdurka , Im running into the same problem.
    In a jupyter notebook I could work around this by setting reject=None in plot_properties, but now I'm stuck when I want to use Report.add_ica().
    Let's keep updated on this, as for me right now it seems I cannot add ICA with an inst to the report.

  3. mktutaj commented on May 5, 2026

    @mktutaj

    Same blocker here on Windows. As @LeonardoMelloni noted,
    ica.plot_properties(reject=None) works around it for standalone
    calls, but Report.add_ica() has no reject= parameter, so any
    recording with at least one fit-time-rejected segment can't have its
    ICA section added to the report.

    My data point:

    • Windows 11, MNE in a Python 3.12 venv
    • 22-channel, 808 s real EEG recording (not synthetic)
    • Picard fit, 19 components after rank reduction, fit-time
      reject_uv ≈ 200 µV
    • 4 windows dropped at fit time → index 243 is out of bounds for axis 0 with size 239 (K = 243 − 239 = 4) — exact signature from
      the original report.

    For now I catch the exception and lose the ICA section of the audit
    report, which is unfortunate because that's exactly the recordings
    where reviewers would most want to look at the components. Would
    really welcome an upstream fix — happy to test a PR.

  4. larsoner commented on May 5, 2026

    @larsoner
    Member

    I got a start in #13885 but will need a bit more time to really fix 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions