Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose colormap in plot_sensors_connectivity #141

Merged
merged 7 commits into from
Jul 15, 2023
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
5 changes: 3 additions & 2 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ Enhancements
~~~~~~~~~~~~

- Add the option to set the number of connections plotted in :func:`mne_connectivity.viz.plot_sensors_connectivity` by `Qianliang Li`_ (:pr:`133`).
- Allow setting colormap via new parameter ``cmap`` in :func:`mne_connectivity.viz.plot_sensors_connectivity` by `Daniel McCloy`_ (:pr:`141`).

Bug
~~~

-
-

API
~~~

-
-

Authors
~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions mne_connectivity/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def pytest_configure(config):
always::ResourceWarning
# pydarkstyle
ignore:.*Setting theme='dark' is not yet supported.*:RuntimeWarning
# imageio-ffmpeg (still happening as of version 0.4.8):
ignore:pkg_resources is deprecated as an API:DeprecationWarning
ignore:Deprecated call to `pkg_resources.declare_namespace.*:DeprecationWarning
""" # noqa: E501
for warning_line in warning_lines.split('\n'):
warning_line = warning_line.strip()
Expand Down
9 changes: 7 additions & 2 deletions mne_connectivity/viz/_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@fill_doc
def plot_sensors_connectivity(info, con, picks=None,
cbar_label='Connectivity',
n_con=20):
n_con=20, cmap='RdBu'):
"""Visualize the sensor connectivity in 3D.

Parameters
Expand All @@ -38,6 +38,10 @@ def plot_sensors_connectivity(info, con, picks=None,
Label for the colorbar.
n_con : int
Number of strongest connections shown. By default 20.
cmap : str | instance of matplotlib.colors.Colormap
Colormap for coloring connections by strength. If :class:`str`, must
be a valid Matplotlib colormap (i.e. a valid key of
``matplotlib.colormaps``). Default is ``"RdBu"``.

Returns
-------
Expand Down Expand Up @@ -92,7 +96,8 @@ def plot_sensors_connectivity(info, con, picks=None,
destination=np.c_[x2, y2, z2],
scalars=np.c_[val, val],
vmin=vmin, vmax=vmax,
reverse_lut=True)
reverse_lut=True,
colormap=cmap)

renderer.scalarbar(source=tube, title=cbar_label)

Expand Down
17 changes: 15 additions & 2 deletions mne_connectivity/viz/tests/test_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import numpy as np
import pytest

from matplotlib import colormaps
from numpy.testing import assert_almost_equal

import mne
from mne.datasets import testing

Expand All @@ -27,7 +30,17 @@
plot_sensors_connectivity(info='foo', con=con, picks=picks)
with pytest.raises(ValueError, match='does not correspond to the size'):
plot_sensors_connectivity(info=info, con=con[::2, ::2], picks=picks)

fig = plot_sensors_connectivity(info=info, con=con, picks=picks)
cmap = 'viridis'
fig = plot_sensors_connectivity(info=info, con=con, picks=picks, cmap=cmap)

Check warning on line 34 in mne_connectivity/viz/tests/test_3d.py

View check run for this annotation

Codecov / codecov/patch

mne_connectivity/viz/tests/test_3d.py#L33-L34

Added lines #L33 - L34 were not covered by tests
# check colormap
cmap_from_mpl = np.array(colormaps[cmap].colors)
cmap_from_vtk = np.array(

Check warning on line 37 in mne_connectivity/viz/tests/test_3d.py

View check run for this annotation

Codecov / codecov/patch

mne_connectivity/viz/tests/test_3d.py#L36-L37

Added lines #L36 - L37 were not covered by tests
fig.plotter.scalar_bar.GetLookupTable().GetTable()
)
# discard alpha channel and convert uint8 -> norm
cmap_from_vtk = cmap_from_vtk[:, :3] / 255
cmap_from_vtk = cmap_from_vtk[::-1] # for some reason order is flipped
assert_almost_equal(cmap_from_mpl, cmap_from_vtk, decimal=2)

Check warning on line 43 in mne_connectivity/viz/tests/test_3d.py

View check run for this annotation

Codecov / codecov/patch

mne_connectivity/viz/tests/test_3d.py#L41-L43

Added lines #L41 - L43 were not covered by tests
# check title
title = list(fig.plotter.scalar_bars.values())[0].GetTitle()
assert title == 'Connectivity'
Loading