From 65a308dbc4c6a0fcf8f438fa899b5f4d8a69dd5c Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 14 Jul 2023 16:01:36 -0500 Subject: [PATCH 1/7] expose cmap in plot_sensors_connectivity [ci skip] --- mne_connectivity/viz/_3d.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mne_connectivity/viz/_3d.py b/mne_connectivity/viz/_3d.py index 7c7c2c89..ea561862 100644 --- a/mne_connectivity/viz/_3d.py +++ b/mne_connectivity/viz/_3d.py @@ -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 @@ -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 ------- @@ -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) From b83b66bde178108a04644b48fa3f1e3f806b9c95 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 14 Jul 2023 16:27:06 -0500 Subject: [PATCH 2/7] ignore dependency deprecation warnings --- mne_connectivity/conftest.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mne_connectivity/conftest.py b/mne_connectivity/conftest.py index d1366cfb..db908b67 100644 --- a/mne_connectivity/conftest.py +++ b/mne_connectivity/conftest.py @@ -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() From a46400a78240ed526114e3a8c120d658dc05ce4d Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 14 Jul 2023 16:27:31 -0500 Subject: [PATCH 3/7] add test that colormap correctly set --- mne_connectivity/viz/tests/test_3d.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mne_connectivity/viz/tests/test_3d.py b/mne_connectivity/viz/tests/test_3d.py index 859f4940..169def0a 100644 --- a/mne_connectivity/viz/tests/test_3d.py +++ b/mne_connectivity/viz/tests/test_3d.py @@ -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 @@ -27,7 +30,14 @@ def test_plot_sensors_connectivity(renderer): 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 colormap + cmap_from_mpl = np.array(colormaps[cmap].colors) + cmap_from_vtk = np.array(fig.plotter.scalar_bar.GetLookupTable().GetTable()) + cmap_from_vtk = cmap_from_vtk[:, :3] / cmap_from_vtk[:, [-1]] + 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 title title = list(fig.plotter.scalar_bars.values())[0].GetTitle() assert title == 'Connectivity' From 1c74f2b0aa39ad0eadce673d36f1f82c9d3af096 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 14 Jul 2023 16:31:10 -0500 Subject: [PATCH 4/7] changelog --- doc/whats_new.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index 672f290a..06a225c4 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -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.vis.plot_sensors_connectivity` by `Daniel McCloy`_ (:pr:`141`). Bug ~~~ -- +- API ~~~ -- +- Authors ~~~~~~~ From 37b7aec251d9948909cbf71396636551d55d4eeb Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 14 Jul 2023 16:48:18 -0500 Subject: [PATCH 5/7] Update doc/whats_new.rst --- doc/whats_new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index 06a225c4..5016010d 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -24,7 +24,7 @@ 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.vis.plot_sensors_connectivity` by `Daniel McCloy`_ (:pr:`141`). +- Allow setting colormap via new parameter ``cmap`` in :func:`mne_connectivity.viz.plot_sensors_connectivity` by `Daniel McCloy`_ (:pr:`141`). Bug ~~~ From e4e66d6c2cd95c247d889d0fa84ef06c759b33d1 Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Fri, 14 Jul 2023 17:06:35 -0500 Subject: [PATCH 6/7] flake (I'm getting too used to precommit) --- mne_connectivity/viz/_3d.py | 6 +++--- mne_connectivity/viz/tests/test_3d.py | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mne_connectivity/viz/_3d.py b/mne_connectivity/viz/_3d.py index ea561862..6b3be24b 100644 --- a/mne_connectivity/viz/_3d.py +++ b/mne_connectivity/viz/_3d.py @@ -39,9 +39,9 @@ def plot_sensors_connectivity(info, con, picks=None, 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"``. + 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 ------- diff --git a/mne_connectivity/viz/tests/test_3d.py b/mne_connectivity/viz/tests/test_3d.py index 169def0a..b15888c0 100644 --- a/mne_connectivity/viz/tests/test_3d.py +++ b/mne_connectivity/viz/tests/test_3d.py @@ -34,7 +34,9 @@ def test_plot_sensors_connectivity(renderer): fig = plot_sensors_connectivity(info=info, con=con, picks=picks, cmap=cmap) # check colormap cmap_from_mpl = np.array(colormaps[cmap].colors) - cmap_from_vtk = np.array(fig.plotter.scalar_bar.GetLookupTable().GetTable()) + cmap_from_vtk = np.array( + fig.plotter.scalar_bar.GetLookupTable().GetTable() + ) cmap_from_vtk = cmap_from_vtk[:, :3] / cmap_from_vtk[:, [-1]] cmap_from_vtk = cmap_from_vtk[::-1] # for some reason order is flipped assert_almost_equal(cmap_from_mpl, cmap_from_vtk, decimal=2) From 12c600cdad9a6acae16848ae1b8faf87b1d33b32 Mon Sep 17 00:00:00 2001 From: Adam Li Date: Sat, 15 Jul 2023 14:46:37 -0400 Subject: [PATCH 7/7] Update mne_connectivity/viz/tests/test_3d.py Co-authored-by: Daniel McCloy --- mne_connectivity/viz/tests/test_3d.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mne_connectivity/viz/tests/test_3d.py b/mne_connectivity/viz/tests/test_3d.py index b15888c0..afcca3f7 100644 --- a/mne_connectivity/viz/tests/test_3d.py +++ b/mne_connectivity/viz/tests/test_3d.py @@ -37,7 +37,8 @@ def test_plot_sensors_connectivity(renderer): cmap_from_vtk = np.array( fig.plotter.scalar_bar.GetLookupTable().GetTable() ) - cmap_from_vtk = cmap_from_vtk[:, :3] / cmap_from_vtk[:, [-1]] + # 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 title