# Index errors with source estimate visualization

**URL:** <https://mne.discourse.group/t/index-errors-with-source-estimate-visualization/2737>\
**Category:** Support & Discussions\
**Tags:** ﻿source-localization, visualization\
**Created:** [February 21, 2021, 5:16am UTC](https://mne.discourse.group/t/index-errors-with-source-estimate-visualization/2737 "2021-02-21T05:16:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mengjin123](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mengjin123/32/217_2.png) [@mengjin123](https://mne.discourse.group/u/mengjin123)\
**Post date:** [February 21, 2021, 5:16am UTC](https://mne.discourse.group/t/index-errors-with-source-estimate-visualization/2737/1 "2021-02-21T05:16:23Z")

</div>

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

- MNE-Python version: 0.23
- operating system: Linux 18  
[Index errors with PySurfer real-time source estimate visualization](https://mne.discourse.group/t/index-errors-with-pysurfer-real-time-source-estimate-visualization/761)

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:

```python
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?

👉 👉 👉 Thank you very much in advance ! 👈 👈 👈

Best regards

Mengjin

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [February 21, 2021, 9:09am UTC](https://mne.discourse.group/t/index-errors-with-source-estimate-visualization/2737/2 "2021-02-21T09:09:57Z")

</div>

cc @GuillaumeFavelier

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [February 24, 2021, 1:52pm UTC](https://mne.discourse.group/t/index-errors-with-source-estimate-visualization/2737/3 "2021-02-24T13:52:59Z")

</div>

Did you see the reply over in [Mne coreg some confusions - #7 by agramfort](https://mne.discourse.group/t/mne-coreg-some-confusions/2709/7) ?
