Skip to content

Commit

Permalink
FIX: scalar_bar (#8643)
Browse files Browse the repository at this point in the history
* Fix scalar bar mapped from polydata actor

* Touch example

* DRY

* LUT still needs to be set for glyphs

* Touch example

* Improve coverage
  • Loading branch information
GuillaumeFavelier committed Dec 10, 2020
1 parent 76e386c commit e2357ad
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/inverse/plot_source_space_snr.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
ax.set(xlabel='Time (sec)', ylabel='SNR MEG-EEG')
fig.tight_layout()

# Find time point of maximum SNR:
# Find time point of maximum SNR
maxidx = np.argmax(ave)

# Plot SNR on source space at the time point of maximum SNR:
Expand Down
2 changes: 1 addition & 1 deletion examples/inverse/plot_vector_mne_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

stc_max, directions = stc.project('pca', src=inv['src'])
# These directions must by design be close to the normals because this
# inverse was computed with loose=0.2:
# inverse was computed with loose=0.2
print('Absolute cosine similarity between source normals and directions: '
f'{np.abs(np.sum(directions * inv["source_nn"][2::3], axis=-1)).mean()}')
brain_max = stc_max.plot(
Expand Down
3 changes: 2 additions & 1 deletion mne/viz/_brain/_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2407,7 +2407,8 @@ def update_lut(self, fmin=None, fmid=None, fmax=None):
mesh = self._layered_meshes[hemi]
mesh.update_overlay(name='data',
colormap=self._data['ctable'])
_set_colormap_range(mesh._actor, ctable, scalar_bar, rng)
_set_colormap_range(mesh._actor, ctable, scalar_bar, rng,
self._brain_color)
scalar_bar = None

grid_volume_pos = hemi_data.get('grid_volume_pos')
Expand Down
7 changes: 2 additions & 5 deletions mne/viz/backends/_pysurfer_mayavi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from tvtk.pyface.tvtk_scene import TVTKScene

from .base_renderer import _BaseRenderer
from ._utils import _check_color, ALLOWED_QUIVER_MODES
from ._utils import _check_color, _alpha_blend_background, ALLOWED_QUIVER_MODES
from ...surface import _normalize_vectors
from ...utils import (_import_mlab, _validate_type, SilenceStdout,
copy_base_doc_to_subclass_doc, _check_option)
Expand Down Expand Up @@ -301,10 +301,7 @@ def scalarbar(self, source, color="white", title=None, n_labels=4,
ctable = lut.table.to_array()
cbar_lut = tvtk.LookupTable()
cbar_lut.deep_copy(lut)
alphas = ctable[:, -1][:, np.newaxis] / 255.
use_lut = ctable.copy()
use_lut[:, -1] = 255.
vals = (use_lut * alphas) + bgcolor * (1 - alphas)
vals = _alpha_blend_background(ctable, bgcolor)
cbar_lut.table.from_array(vals)
cmap.scalar_bar.lookup_table = cbar_lut

Expand Down
26 changes: 14 additions & 12 deletions mne/viz/backends/_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import vtk

from .base_renderer import _BaseRenderer
from ._utils import _get_colormap_from_array, ALLOWED_QUIVER_MODES
from ._utils import (_get_colormap_from_array, _alpha_blend_background,
ALLOWED_QUIVER_MODES)
from ...fixes import _get_args
from ...utils import copy_base_doc_to_subclass_doc, _check_option
from ...externals.decorator import decorator
Expand Down Expand Up @@ -874,20 +875,21 @@ def _process_events(plotter):
plotter.app.processEvents()


def _set_colormap_range(actor, ctable, scalar_bar, rng=None):
def _set_colormap_range(actor, ctable, scalar_bar, rng=None,
background_color=None):
from vtk.util.numpy_support import numpy_to_vtk
mapper = actor.GetMapper()
lut = mapper.GetLookupTable()
# Catch: FutureWarning: Conversion of the second argument of
# issubdtype from `complex` to `np.complexfloating` is deprecated.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
lut.SetTable(numpy_to_vtk(ctable))
if rng is not None:
mapper.SetScalarRange(rng[0], rng[1])
lut.SetRange(rng[0], rng[1])
mapper = actor.GetMapper()
mapper.SetScalarRange(*rng)
lut = mapper.GetLookupTable()
lut.SetTable(numpy_to_vtk(ctable))
if scalar_bar is not None:
scalar_bar.SetLookupTable(actor.GetMapper().GetLookupTable())
lut = scalar_bar.GetLookupTable()
if background_color is not None:
background_color = np.array(background_color) * 255
ctable = _alpha_blend_background(ctable, background_color)
lut.SetTable(numpy_to_vtk(ctable, array_type=vtk.VTK_UNSIGNED_CHAR))
lut.SetRange(*rng)


def _set_volume_range(volume, ctable, alpha, scalar_bar, rng):
Expand Down
7 changes: 7 additions & 0 deletions mne/viz/backends/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ def _check_color(color):
raise TypeError("Expected type is `str` or iterable but "
"{} was given.".format(type(color)))
return color


def _alpha_blend_background(ctable, background_color):
alphas = ctable[:, -1][:, np.newaxis] / 255.
use_table = ctable.copy()
use_table[:, -1] = 255.
return (use_table * alphas) + background_color * (1 - alphas)
3 changes: 2 additions & 1 deletion mne/viz/backends/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def test_3d_backend(renderer):
scalars=np.array([[1.0, 1.0]]))

# scalar bar
rend.scalarbar(source=tube, title="Scalar Bar")
rend.scalarbar(source=tube, title="Scalar Bar",
bgcolor=[1, 1, 1])

# use text
rend.text2d(x_window=txt_x, y_window=txt_y, text=txt_text,
Expand Down
2 changes: 1 addition & 1 deletion tutorials/misc/plot_seeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
surfaces=["pial", "head"])

###############################################################################
# Next, we will get the raw data and plot its amplitude over time.
# Next, we'll get the raw data and plot its amplitude over time.

raw.plot()

Expand Down
2 changes: 1 addition & 1 deletion tutorials/source-modeling/plot_visualize_stc.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
clim=dict(kind='value', lims=[3, 6, 9]))

###############################################################################
# You can also morph it to fsaverage and visualize it using a flatmap:
# You can also morph it to fsaverage and visualize it using a flatmap

# sphinx_gallery_thumbnail_number = 3
stc_fs = mne.compute_source_morph(stc, 'sample', 'fsaverage', subjects_dir,
Expand Down

0 comments on commit e2357ad

Please sign in to comment.