Index errors with source estimate visualization

If you have a question or issue with MNE-Python, please include the following info:

Hi everyone, I have been trying to code a program that would enable to plot trigger spesific source estimates. It always complains about index errors.

Example code for source estimate:

import matplotlib.pyplot as plt
import mne
# import mne_bids
import os 
import os.path as op
import numpy as np

from mne.preprocessing import ICA, corrmap
from mne import io, combine_evoked
from mne.minimum_norm import make_inverse_operator, apply_inverse
import pyvista as pv
from pyvistaqt import BackgroundPlotter
# from mayavi import mlab

# %matplotlib qt   # this will create seperate pop-up window

%matplotlib inline
%config InlineBackend.figure_format='svg'
mne.viz.set_3d_backend("notebook")
os.environ['ETS_TOOLKIT'] = 'qt4' ## Visualizing source estimates
os.environ['QT_API'] = 'pyqt5'

subjects_dir = '/Volumes/menglab_MJ/MEG/MEG_anatomical_data/Surf_analysis'
subject      = 'S01'

raw_fname    = 'S01_raw_filterd.fif'
fwd_fname    = 'S01-meg-oct-6-fwd.fif'
trans_fname  = 'S01-trans.fif'
epLFF6_fname = 'S01_LFF6-epo.fif'
epLHF6_fname = 'S01_LHF6-epo.fif'
src_fname    = 'S01-oct-6-src.fif'

# raw   = mne.io.read_raw_fif(raw_fname)
trans = mne.read_trans(trans_fname)
src   = mne.read_source_spaces(src_fname)
fwd = mne.read_forward_solution(fwd_fname)
epochs_LFF6 = mne.read_epochs(epLFF6_fname)
epochs_LHF6 = mne.read_epochs(epLHF6_fname)
evoked_LFF6 = epochs_LFF6.average()
evoked_LHF6 = epochs_LHF6.average()

cov_LFF6 = mne.compute_covariance(epochs_LFF6, tmin=0, tmax=0.3, 
                                            method=['shrunk','empirical'],rank=None,verbose=True)

cov_LHF6 = mne.compute_covariance(epochs_LHF6, tmin=0, tmax=0.3, 
                                            method=['shrunk','empirical'],rank=None,verbose=True)

inv_LFF6 = make_inverse_operator(evoked_LFF6.info, fwd, cov_LFF6, loose=0.2, depth=0.8)

inv_LHF6 = make_inverse_operator(evoked_LHF6.info, fwd, cov_LHF6, loose=0.2, depth=0.8)

method = "dSPM" # "dSPM"
snr = 3.
lambda2 = 1. / snr ** 2

stc_LFF6, residual_LFF6 = apply_inverse(evoked_LFF6, inv_LFF6, lambda2,
                             method=method, pick_ori=None, 
                             return_residual=True, verbose=True)

stc_LHF6, residual_LHF6 = apply_inverse(evoked_LHF6, inv_LHF6, lambda2,
                             method=method, pick_ori=None, 
                             return_residual=True, verbose=True)

surfer_kwargs = dict(hemi='lh', subjects_dir=subjects_dir,
                     clim=dict(kind='value', lims=[8, 12, 15]), views='lateral',
                     initial_time=0.3, time_unit='s', 
                     size=(800, 800), smoothing_steps=10)

brain = stc_LFF6.plot(**surfer_kwargs)

Error is for example this:


IndexError Traceback (most recent call last)
in
4 size=(800, 800), smoothing_steps=10)
5
----> 6 brain = stc_LFF6.plot(**surfer_kwargs)

~/.local/lib/python3.7/site-packages/mne/source_estimate.py in plot(self, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, verbose)
661 spacing=spacing, title=title, show_traces=show_traces,
662 src=src, volume_options=volume_options, view_layout=view_layout,
→ 663 add_data_kwargs=add_data_kwargs, verbose=verbose)
664 return brain
665

in plot_source_estimates(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, verbose)

~/.local/lib/python3.7/site-packages/mne/viz/_3d.py in plot_source_estimates(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, verbose)
1826 cortex=cortex, foreground=foreground, size=size, scale_factor=None,
1827 show_traces=show_traces, src=src, volume_options=volume_options,
→ 1828 view_layout=view_layout, add_data_kwargs=add_data_kwargs, **kwargs)
1829
1830

~/.local/lib/python3.7/site-packages/mne/viz/_3d.py in _plot_stc(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, subjects_dir, views, clim, figure, initial_time, time_unit, background, time_viewer, colorbar, transparent, brain_alpha, overlay_alpha, vector_alpha, cortex, foreground, size, scale_factor, show_traces, src, volume_options, view_layout, add_data_kwargs)
1953 vertices=vertices)
1954 with warnings.catch_warnings(record=True): # traits warnings
→ 1955 brain.add_data(**use_kwargs)
1956 if using_mayavi:
1957 brain.scale_data_colormap(fmin=scale_pts[0], fmid=scale_pts[1],

in add_data(self, array, fmin, fmid, fmax, thresh, center, transparent, colormap, alpha, vertices, smoothing_steps, time, time_label, colorbar, hemi, remove_existing, time_label_size, initial_time, scale_factor, vector_alpha, clim, src, volume_options, colorbar_kwargs, verbose)

~/.local/lib/python3.7/site-packages/mne/viz/_brain/_brain.py in add_data(self, array, fmin, fmid, fmax, thresh, center, transparent, colormap, alpha, vertices, smoothing_steps, time, time_label, colorbar, hemi, remove_existing, time_label_size, initial_time, scale_factor, vector_alpha, clim, src, volume_options, colorbar_kwargs, verbose)
1862 # _current_time
1863 self.set_time_interpolation(self.time_interpolation)
→ 1864 self.set_data_smoothing(self._data[‘smoothing_steps’])
1865
1866 # 3) add the other actors

~/.local/lib/python3.7/site-packages/mne/viz/_brain/_brain.py in set_data_smoothing(self, n_steps)
2645 self.geo[hemi].orig_faces,
2646 np.arange(len(self.geo[hemi].coords)),
→ 2647 vertices, morph_n_steps, maps, warn=False)
2648 self._data[hemi][‘smooth_mat’] = smooth_mat
2649 self.set_time_point(self._data[‘time_idx’])

~/.local/lib/python3.7/site-packages/mne/morph.py in _hemi_morph(tris, vertices_to, vertices_from, smooth, maps, warn)
1146 mm = _surf_nearest(vertices_from, e).tocsr()
1147 else:
→ 1148 mm = _surf_upsampling_mat(vertices_from, e, smooth, warn=warn)
1149 assert mm.shape == (n_vertices, len(vertices_from))
1150 if maps is not None:

~/.local/lib/python3.7/site-packages/mne/morph.py in _surf_upsampling_mat(idx_from, e, smooth, warn)
1282 data = data[idx]
1283 # smoothing multiplication
→ 1284 use_e = e[:, idx] if len(idx) < n_tot else e
1285 data = use_e * data
1286 del use_e

~/anaconda3/lib/python3.7/site-packages/scipy/sparse/_index.py in getitem(self, key)
31 “”"
32 def getitem(self, key):
—> 33 row, col = self._validate_indices(key)
34 # Dispatch to specialized methods.
35 if isinstance(row, INT_TYPES):

~/anaconda3/lib/python3.7/site-packages/scipy/sparse/_index.py in _validate_indices(self, key)
145 col += N
146 elif not isinstance(col, slice):
→ 147 col = self._asindices(col, N)
148
149 return row, col

~/anaconda3/lib/python3.7/site-packages/scipy/sparse/_index.py in _asindices(self, idx, length)
168 max_indx = x.max()
169 if max_indx >= length:
→ 170 raise IndexError(‘index (%d) out of range’ % max_indx)
171
172 min_indx = x.min()

IndexError: index (137932) out of range

I don’t know how to fix this. Do you have any ideas to help me?

:point_right: :point_right: :point_right: Thank you very much in advance ! :point_left: :point_left: :point_left:

Best regards

Mengjin

cc @GuillaumeFavelier

Did you see the reply over in Mne coreg some confusions - #7 by agramfort ?