Repository: mne-tools/mne-python

Issue:
MNE Coreg fiducials disappear
We are using MNE coreg to mark the fiducial locations in MRI images. We observe some strange behavior whenver one of the fiducials is in front of the head. In this case the fiducial disappears and is not visible anymore. To illustrate this with this screenshots: 

![coreg_1](https://user-images.githubusercontent.com/22342465/43404497-ee10bc2c-9417-11e8-964d-09f7036b2951.png)
Here all three fiducials are set, but only the lpa and rpa are have "open space" behind them and are not occluded.
![coreg2](https://user-images.githubusercontent.com/22342465/43404501-f188031a-9417-11e8-8683-f0e1c0ffcb06.png)
Rotating the view such that the head is behind all fiducials ocludes all of them.

![coreg3](https://user-images.githubusercontent.com/22342465/43404506-f3ff391a-9417-11e8-90a6-13af8887c1c2.png)

Turning the view even more makes the nasion visible (at least those parts that are not in front of the head).

We run the latest MNE version (from master, python 3.6) on a ubuntu (16) linux machine where we have followed the MNE install instructions. Our VTK version is 8.1.1. The error also occurs after updating mayavi to the latest version from master. 

Any ideas how to fix this? E.g. different VTK versions?


Discussion:
We draw the *inside* of the head opaque at all times. I suspect the *outside* of the head surface for you is fully transparent (the transparency is controllable). Can you cehck in `Display options...` if you have the head opacity at or near 0?

This behavior also manifests as the head turning "the wrong direction" when you click-drag to rotate.
Thanks for the quick reply! This behavior does not change when we change the opacity slider in the display options. So fiducials disappear across the full range of opacity values.
If you have time, you could click the somewhat-triangular icon in the upper left corner of the display area. This is the Mayavi pipeline editor. You should be able to tweak params of the surfaces we display (there should be two, one for inside and one for outside the head).

I'm not sure offhand what would cause this. I would try changing the visibility of the inside- and outside-of-head surfaces and see which combinations work and don't work. In theory setting both to have opacity 0.99 or so (instead of 1.) might also "fix" it.
Yes! Setting both to .5 makes them visible, but they are still occluded by both surfaces. Setting only one to 0.5 does not work. But this is definetly workable! Thank you very much!
Does setting both to 0.99 work? Sometimes with an actual value of `1.` Mayavi messes things up.
No, that doesn't work... We are happy to help by trying out more suggestions if anything comes to mind!
So in the front-left view (second screenshot you posted), you don't see any of the nasion or LPA?
Yes, even though all screenshots are taken from the same scene, so nasion and LPA should be visible.
So far I have been unable to replicate this. @genisprat can you give the output of `>>> mne.sys_info()` and also `$ glxinfo | grep version`?
>>> mne.sys_info()
Platform:      Linux-4.15.0-30-generic-x86_64-with-debian-stretch-sid
Python:        3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51)  [GCC 7.2.0]
Executable:    /home/gprat/anaconda2/envs/mne/bin/python
CPU:           x86_64: 4 cores
Memory:        3.8 GB

mne:           0.17.dev0
numpy:         1.14.5 {blas=mkl_rt, lapack=mkl_rt}
scipy:         1.1.0
matplotlib:    2.2.2 {backend=agg}

sklearn:       0.19.1
nibabel:       2.3.0
mayavi:        4.6.2.dev0 {qt_api=pyqt5}
pycuda:        Not found
skcuda:        Not found
pandas:        0.23.3




glxinfo | grep version
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
    Max core profile version: 4.2
    Max compat profile version: 3.0
    Max GLES1 profile version: 1.1
    Max GLES[23] profile version: 3.0
OpenGL core profile version string: 4.2 (Core Profile) Mesa 17.2.8
OpenGL core profile shading language version string: 4.20
OpenGL version string: 3.0 Mesa 17.2.8
OpenGL shading language version string: 1.30
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 17.2.8
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.00



@genisprat are you using saftware rendering, then? If so, can you try using `llvmpipe` rather than `swr` (assuming that's what you're running)? You can check with `glxinfo | grep renderer`
@larsoner I have recently faced some similar problems.
##################
mne.sys_info() output:
Platform:      Linux-4.15.0-33-generic-x86_64-with-debian-buster-sid
CPU:           x86_64: 4 cores
Memory:        15.6 GB
mne:           0.17.dev0
numpy:         1.15.1 {blas=mkl_rt, lapack=mkl_rt}
scipy:         1.1.0
matplotlib:    2.2.2 {backend=Qt5Agg}
sklearn:       0.19.1
nibabel:       2.3.0
mayavi:        4.6.2 {qt_api=pyqt5}
cupy:          Not found
pandas:        0.23.4
dipy:          0.14.0
############### 
glxinfo | grep version output:
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
Max core profile version: 4.5
Max compat profile version: 3.0
Max GLES1 profile version: 1.1
Max GLES[23] profile version: 3.2
OpenGL core profile version string: 4.5 (Core Profile) Mesa 18.0.5
OpenGL core profile shading language version string: 4.50
OpenGL version string: 3.0 Mesa 18.0.5
OpenGL shading language version string: 1.30
OpenGL ES profile version string: OpenGL ES 3.2 Mesa 18.0.5
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20

#############################################
Once I try to change the position of the fiducials, the coordinates are not updated except for the LPA. However, if I switch from RPA to LPA or Nasion and to switch back to RPA again, then coordinates for RPA are updated. Its also the same for Nasion. Fiducials are still missing but I can see them if I turn the head model (see the pic below).   ![screenshot from 2018-09-13 14-59-56](https://user-images.githubusercontent.com/22345222/45490675-10bb6b00-b768-11e8-9041-0f0216afd22b.png)

I tried to tweak mayavi pipeline according to your comment, but I can't find any visibility option.  Am I looking into the wrong settings?  (pics below).
![screenshot from 2018-09-13 14-59-34](https://user-images.githubusercontent.com/22345222/45490843-77d91f80-b768-11e8-96fa-25329cf36c34.png)
## display options: 
![screenshot from 2018-09-13 15-01-49](https://user-images.githubusercontent.com/22345222/45491306-b6bba500-b769-11e8-8c38-037fc7d8cbfc.png)

Last but not least, whenever I try to load a different subject file into mne coreg, the display automatically closes. This is the error message that I am getting in the terminal: (Error!)  'malloc(): smallbin double linked list corrupted'. Not sure why I am getting this error.  

Any help would be appreciated. Thanks! 




  
I have solved the Fiducials's visualization issue. I had to change the Actor's opacity properties under Glyph option. You are right about the value of 0.99. But RPA and Nasion coordinates are still not updated if I change the corresponding fiducial position.     
Hi all, I had a similar problem and I fixed it by [installing the NVIDIA drivers](https://linuxconfig.org/how-to-install-the-nvidia-drivers-on-ubuntu-18-04-bionic-beaver-linux). Hope it helps.
I am having this issue as well

> >>> mne.sys_info()
> /opt/anaconda3/envs/mne-devel-py3/lib/python3.6/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
>   import imp
> Platform:      Linux-4.13.0-46-generic-x86_64-with-debian-stretch-sid
> Python:        3.6.6 |Anaconda, Inc.| (default, Oct  9 2018, 12:34:16)  [GCC 7.3.0]
> Executable:    /opt/anaconda3/envs/mne-devel-py3/bin/python
> CPU:           x86_64: 4 cores
> Memory:        19.0 GB
> 
> mne:           0.17.dev0
> numpy:         1.15.3 {blas=mkl_rt, lapack=mkl_rt}
> scipy:         1.1.0
> matplotlib:    3.0.0 {backend=Qt5Agg}
> 
> sklearn:       0.20.0
> nibabel:       2.3.1
> mayavi:        4.6.2 {qt_api=pyqt5, PyQt5=5.9.2}
> cupy:          Not found
> pandas:        0.23.4
> dipy:          0.14.0

> 
> (mne-devel-py3) bloyl@calisto:~$ glxinfo |grep version
> server glx version string: 1.4
> client glx version string: 1.4
> GLX version: 1.4
>     Max core profile version: 4.5
>     Max compat profile version: 3.0
>     Max GLES1 profile version: 1.1
>     Max GLES[23] profile version: 3.2
> OpenGL core profile version string: 4.5 (Core Profile) Mesa 17.2.8
> OpenGL core profile shading language version string: 4.50
> OpenGL version string: 3.0 Mesa 17.2.8
> OpenGL shading language version string: 1.30
> OpenGL ES profile version string: OpenGL ES 3.2 Mesa 17.2.8
> OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.20
> 


Resolving patch:
diff --git a/mne/gui/_coreg_gui.py b/mne/gui/_coreg_gui.py
--- a/mne/gui/_coreg_gui.py
+++ b/mne/gui/_coreg_gui.py
@@ -1841,12 +1841,17 @@ def _set_title(self):
     @on_trait_change('scene:activated')
     def _init_plot(self):
         _toggle_mlab_render(self, False)
+        if hasattr(getattr(self.scene, 'renderer', None), 'use_fxaa'):
+            self.scene.renderer.use_fxaa = True
 
         lpa_color = defaults['lpa_color']
         nasion_color = defaults['nasion_color']
         rpa_color = defaults['rpa_color']
 
         # MRI scalp
+        #
+        # Due to MESA rendering / z-order bugs, this should be added and
+        # rendered first (see gh-5375).
         color = defaults['head_color']
         self.mri_obj = SurfaceObject(
             points=np.empty((0, 3)), color=color, tris=np.empty((0, 3)),
@@ -1857,6 +1862,8 @@ def _init_plot(self):
         )
         self.mri_obj.opacity = self._initial_kwargs['head_opacity']
         self.data_panel.fid_panel.hsp_obj = self.mri_obj
+        self._update_mri_obj()
+        self.mri_obj.plot()
         # Do not do sync_trait here, instead use notifiers elsewhere
 
         # MRI Fiducials
@@ -1952,9 +1959,7 @@ def _init_plot(self):
 
         self.sync_trait('bgcolor', self.scene, 'background')
 
-        self._update_mri_obj()
         self._update_projections()
-        self.mri_obj.plot()
 
         _toggle_mlab_render(self, True)
         self.scene.render()
diff --git a/mne/gui/_viewer.py b/mne/gui/_viewer.py
--- a/mne/gui/_viewer.py
+++ b/mne/gui/_viewer.py
@@ -186,7 +186,8 @@ class Object(HasPrivateTraits):
 
     # This should be Tuple, but it is broken on Anaconda as of 2016/12/16
     color = RGBColor((1., 1., 1.))
-    opacity = Range(low=0., high=1., value=1.)
+    # Due to a MESA bug, we use 0.99 opacity to force alpha blending
+    opacity = Range(low=0., high=1., value=0.99)
     visible = Bool(True)
 
     def _update_points(self):
diff --git a/mne/viz/_3d.py b/mne/viz/_3d.py
--- a/mne/viz/_3d.py
+++ b/mne/viz/_3d.py
@@ -351,7 +351,7 @@ def plot_evoked_field(evoked, surf_maps, time=None, time_label='t = %0.0f ms',
                                      np.tile([0., 0., 0., 255.], (2, 1)),
                                      np.tile([255., 0., 0., 255.], (127, 1))])
 
-    fig = mlab.figure(bgcolor=(0.0, 0.0, 0.0), size=(600, 600))
+    fig = _mlab_figure(bgcolor=(0.0, 0.0, 0.0), size=(600, 600))
     _toggle_mlab_render(fig, False)
 
     for ii, this_map in enumerate(surf_maps):
@@ -1041,7 +1041,7 @@ def plot_alignment(info, trans=None, subject=None, subjects_dir=None,
             hpi_loc = np.array([d['r'] for d in info['dig']
                                 if d['kind'] == FIFF.FIFFV_POINT_HPI])
             ext_loc = np.array([d['r'] for d in info['dig']
-                               if d['kind'] == FIFF.FIFFV_POINT_EXTRA])
+                                if d['kind'] == FIFF.FIFFV_POINT_EXTRA])
         car_loc = _fiducial_coords(info['dig'])
         # Transform from head coords if necessary
         if coord_frame == 'meg':
@@ -1062,7 +1062,7 @@ def plot_alignment(info, trans=None, subject=None, subjects_dir=None,
 
     # initialize figure
     if fig is None:
-        fig = mlab.figure(bgcolor=(0.5, 0.5, 0.5), size=(800, 800))
+        fig = _mlab_figure(bgcolor=(0.5, 0.5, 0.5), size=(800, 800))
     if interaction == 'terrain' and fig.scene is not None:
         fig.scene.interactor.interactor_style = \
             tvtk.InteractorStyleTerrain()
@@ -1976,7 +1976,7 @@ def _onclick(event, params):
             ax_z.clear()
             params.update({'img_idx': index_img(img, idx)})
             params.update({'title': 'Activation (t=%.3f s.)'
-                          % params['stc'].times[idx]})
+                           % params['stc'].times[idx]})
             plot_map_callback(
                 params['img_idx'], title='',
                 cut_coords=cut_coords)
@@ -2380,8 +2380,7 @@ def plot_sparse_source_estimates(src, stcs, colors=None, linewidth=2,
 
     color_converter = ColorConverter()
 
-    f = mlab.figure(figure=fig_name, bgcolor=bgcolor, size=(600, 600))
-    mlab.clf()
+    f = _mlab_figure(figure=fig_name, bgcolor=bgcolor, size=(600, 600))
     _toggle_mlab_render(f, False)
     with warnings.catch_warnings(record=True):  # traits warnings
         surface = mlab.triangular_mesh(points[:, 0], points[:, 1],
@@ -2448,6 +2447,16 @@ def plot_sparse_source_estimates(src, stcs, colors=None, linewidth=2,
     return surface
 
 
+def _mlab_figure(**kwargs):
+    """Create a Mayavi figure using our defaults."""
+    from mayavi import mlab
+    fig = mlab.figure(**kwargs)
+    # If using modern VTK/Mayavi, improve rendering with FXAA
+    if hasattr(getattr(fig.scene, 'renderer', None), 'use_fxaa'):
+        fig.scene.renderer.use_fxaa = True
+    return fig
+
+
 def _toggle_mlab_render(fig, render):
     mlab = _import_mlab()
     if mlab.options.backend != 'test':


Test patch: