Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ Bugs

- Fix bug with coordinate frames when performing volumetric morphs via :func:`mne.compute_source_morph` and :meth:`mne.SourceMorph.apply` that could lead to ~5 mm bias (:gh:`8642` by `Eric Larson`_)

- Fix bug with volumetric rendering alpha in :meth:`mne.VolSourceEstimate.plot_3d` and related functions (:gh:`8663` by `Eric Larson`_)

- Fix missing documentation of :func:`mne.io.read_raw_nihon` in :ref:`tut-imorting-eeg-data` (:gh`8320` by `Adam Li`_)

- Fix bug with :func:`mne.add_reference_channels` when :func:`mne.io.Raw.reorder_channels` or related methods are used afterward (:gh:`8303`, :gh:`#8484` by `Eric Larson`_)
Expand Down
15 changes: 10 additions & 5 deletions mne/viz/_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,14 +1242,22 @@ def _sensor_shape(coil):
return rrs, tris


def _get_cmap(colormap):
import matplotlib.pyplot as plt
if isinstance(colormap, str) and colormap in ('mne', 'mne_analyze'):
colormap = mne_analyze_colormap([0, 1, 2], format='matplotlib')
else:
colormap = plt.get_cmap(colormap)
return colormap


def _process_clim(clim, colormap, transparent, data=0., allow_pos_lims=True):
"""Convert colormap/clim options to dict.

This fills in any "auto" entries properly such that round-trip
calling gives the same results.
"""
# Based on type of limits specified, get cmap control points
import matplotlib.pyplot as plt
from matplotlib.colors import Colormap
_validate_type(colormap, (str, Colormap), 'colormap')
data = np.asarray(data)
Expand All @@ -1265,10 +1273,7 @@ def _process_clim(clim, colormap, transparent, data=0., allow_pos_lims=True):
colormap = 'hot'
else: # 'pos_lims' in clim
colormap = 'mne'
if colormap in ('mne', 'mne_analyze'):
colormap = mne_analyze_colormap([0, 1, 2], format='matplotlib')
else:
colormap = plt.get_cmap(colormap)
colormap = _get_cmap(colormap)
assert isinstance(colormap, Colormap)
diverging_maps = ['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr',
Expand Down
11 changes: 7 additions & 4 deletions mne/viz/_brain/_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def __init__(self, scalars, colormap, rng, opacity):
self._opacity = opacity

def to_colors(self):
from matplotlib.cm import get_cmap
from .._3d import _get_cmap
from matplotlib.colors import ListedColormap
if isinstance(self._colormap, str):
cmap = get_cmap(self._colormap)
cmap = _get_cmap(self._colormap)
else:
cmap = ListedColormap(self._colormap / 255.)

Expand Down Expand Up @@ -344,8 +344,8 @@ def __init__(self, subject_id, hemi, surf, title=None,
offscreen=False, interaction='trackball', units='mm',
view_layout='vertical', show=True):
from ..backends.renderer import backend, _get_renderer, _get_3d_backend
from .._3d import _get_cmap
from matplotlib.colors import colorConverter
from matplotlib.cm import get_cmap

if hemi in ('both', 'split'):
self._hemis = ('lh', 'rh')
Expand Down Expand Up @@ -414,7 +414,7 @@ def __init__(self, subject_id, hemi, surf, title=None,
geo_kwargs = self._cortex_colormap(cortex)
# evaluate at the midpoint of the used colormap
val = -geo_kwargs['vmin'] / (geo_kwargs['vmax'] - geo_kwargs['vmin'])
self._brain_color = get_cmap(geo_kwargs['colormap'])(val)
self._brain_color = _get_cmap(geo_kwargs['colormap'])(val)

# load geometry for one or both hemispheres as necessary
offset = None if (not offset or hemi != 'both') else 0.0
Expand Down Expand Up @@ -3098,6 +3098,9 @@ def LeaveEvent(self):
def SetEventInformation(self, *args, **kwargs):
pass

def CharEvent(self):
pass

def KeyPressEvent(self, *args, **kwargs):
pass

Expand Down
4 changes: 2 additions & 2 deletions mne/viz/_brain/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

def create_lut(cmap, n_colors=256, center=None):
"""Return a colormap suitable for setting as a LUT."""
from matplotlib import cm
from .._3d import _get_cmap
assert not (isinstance(cmap, str) and cmap == 'auto')
cmap = cm.get_cmap(cmap)
cmap = _get_cmap(cmap)
lut = np.round(cmap(np.linspace(0, 1, n_colors)) * 255.0).astype(np.int64)
return lut

Expand Down
24 changes: 16 additions & 8 deletions mne/viz/_brain/tests/test_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import pytest
import numpy as np
from numpy.testing import assert_allclose
from numpy.testing import assert_allclose, assert_array_equal

from mne import (read_source_estimate, SourceEstimate, MixedSourceEstimate,
VolSourceEstimate)
Expand Down Expand Up @@ -424,13 +424,18 @@ def test_brain_traces(renderer_interactive, hemi, src, tmpdir,
brain = _create_testing_brain(
hemi=hemi, surf='white', src=src, show_traces=0.5, initial_time=0,
volume_options=None, # for speed, don't upsample
n_time=1 if src == 'mixed' else 5,
n_time=1 if src == 'mixed' else 5, diverging=True,
add_data_kwargs=dict(colorbar_kwargs=dict(n_labels=3)),
)
assert brain.show_traces
assert hasattr(brain, "picked_points")
assert hasattr(brain, "_spheres")
assert brain.plotter.scalar_bar.GetNumberOfLabels() == 3
# mne_analyze should be chosen
ctab = brain._data['ctable']
assert_array_equal(ctab[0], [0, 255, 255, 255]) # opaque cyan
assert_array_equal(ctab[-1], [255, 255, 0, 255]) # opaque yellow
assert_allclose(ctab[len(ctab) // 2], [128, 128, 128, 0], atol=3)

# add foci should work for volumes
brain.add_foci([[0, 0, 0]], hemi='lh' if src == 'surface' else 'vol')
Expand Down Expand Up @@ -685,7 +690,7 @@ def test_calculate_lut():


def _create_testing_brain(hemi, surf='inflated', src='surface', size=300,
n_time=5, **kwargs):
n_time=5, diverging=False, **kwargs):
assert src in ('surface', 'mixed', 'volume')
meth = 'plot'
if src in ('surface', 'mixed'):
Expand Down Expand Up @@ -715,14 +720,17 @@ def _create_testing_brain(hemi, surf='inflated', src='surface', size=300,
stc_data[(rng.rand(stc_size // 20) * stc_size).astype(int)] = \
rng.rand(stc_data.size // 20)
stc_data.shape = (n_verts, n_time)
if diverging:
stc_data -= 0.5
stc = klass(stc_data, vertices, 1, 1)

fmin = stc.data.min()
fmax = stc.data.max()
fmid = (fmin + fmax) / 2.
clim = dict(kind='value', lims=[0.1, 0.2, 0.3])
if diverging:
clim['pos_lims'] = clim.pop('lims')

brain_data = getattr(stc, meth)(
subject=subject_id, hemi=hemi, surface=surf, size=size,
subjects_dir=subjects_dir, colormap='hot',
clim=dict(kind='value', lims=(fmin, fmid, fmax)), src=sample_src,
subjects_dir=subjects_dir, colormap='auto',
clim=clim, src=sample_src,
**kwargs)
return brain_data
4 changes: 2 additions & 2 deletions mne/viz/backends/_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,8 @@ def _set_volume_range(volume, ctable, alpha, scalar_bar, rng):
color_tf = vtk.vtkColorTransferFunction()
opacity_tf = vtk.vtkPiecewiseFunction()
for loc, color in zip(np.linspace(*rng, num=len(ctable)), ctable):
color_tf.AddRGBPoint(loc, *color[:-1])
opacity_tf.AddPoint(loc, color[-1] * alpha / 255. / (len(ctable) - 1))
color_tf.AddRGBPoint(loc, *(color[:-1] / 255.))
opacity_tf.AddPoint(loc, color[-1] * alpha / 255.)
color_tf.ClampingOn()
opacity_tf.ClampingOn()
volume.GetProperty().SetColor(color_tf)
Expand Down
1 change: 1 addition & 0 deletions tutorials/misc/plot_seeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
:ref:`tut_working_with_ecog`. In the ECoG example, we show
how to visualize surface grid channels on the brain.
"""

# Authors: Eric Larson <larson.eric.d@gmail.com>
# Adam Li <adam2392@gmail.com>
#
Expand Down
3 changes: 2 additions & 1 deletion tutorials/source-modeling/plot_beamformer_lcmv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
:depth: 2

"""
# Author: Britta Westner <britta.wstnr@gmail.com>
# Authors: Britta Westner <britta.wstnr@gmail.com>
# Eric Larson <larson.eric.d@gmail.com>
#
# License: BSD (3-clause)

Expand Down