Skip to content

Commit

Permalink
[FIX] allow using symmetric maps with plotly (#3942)
Browse files Browse the repository at this point in the history
* fix #3941

* fix kwargs

* implement test for matplotlib

* update changelog

* flake 8

* fix failed ref

* simplify

* simplify

* flake8

* fix other argument

* rename test

* update changelog

* flake8
  • Loading branch information
Remi-Gau committed Sep 2, 2023
1 parent a00306f commit 6578329
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
2 changes: 2 additions & 0 deletions doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Fixes
-----
- Fix bug in ``nilearn.plotting.surf_plotting._plot_surf_matplotlib`` that would make vertices transparent when saving in PDF or SVG format (:gh:`3860` by `Mathieu Dugré`_).

- Fix bug that would prevent using `symmetric_cmap=True` or the `avg_method` argument with :func:`~plotting.plot_surf_roi` (:gh:`3942` by `Rémi Gau`_).

- Fix bug that would prevent loading the confounds of a gifti file in actual fmriprep datasets (:gh:`3819` by `Rémi Gau`_).

- Fix bug that prevented using dataframes as input for second level GLM when computing contrasts (:gh:`3879` by `Rémi Gau`_).
Expand Down
67 changes: 50 additions & 17 deletions nilearn/plotting/surf_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,14 +1469,27 @@ def plot_img_on_surf(stat_map, surf_mesh='fsaverage5', mask_img=None,


@fill_doc
def plot_surf_roi(surf_mesh, roi_map, bg_map=None,
hemi='left', view='lateral', engine='matplotlib',
threshold=1e-14, alpha='auto', vmin=None, vmax=None,
cmap='gist_ncar', cbar_tick_format="auto",
bg_on_data=False, darkness=.7,
title=None, title_font_size=18,
output_file=None, axes=None,
figure=None, **kwargs):
def plot_surf_roi(surf_mesh,
roi_map,
bg_map=None,
hemi='left',
view='lateral',
engine='matplotlib',
avg_method='median',
threshold=1e-14,
alpha='auto',
vmin=None,
vmax=None,
cmap='gist_ncar',
cbar_tick_format="auto",
bg_on_data=False,
darkness=0.7,
title=None,
title_font_size=18,
output_file=None,
axes=None,
figure=None,
**kwargs):
"""Plot ROI on a surface mesh with optional background.
.. versionadded:: 0.3
Expand Down Expand Up @@ -1531,6 +1544,14 @@ def plot_surf_roi(surf_mesh, roi_map, bg_map=None,
Default='matplotlib'.
%(avg_method)s
.. note::
This option is currently only implemented for the
``matplotlib`` engine.
Default='median'.
threshold : a number or None, optional
Threshold regions that are labelled 0.
If you want to use 0 as a label, set threshold to None.
Expand Down Expand Up @@ -1623,15 +1644,27 @@ def plot_surf_roi(surf_mesh, roi_map, bg_map=None,

if cbar_tick_format == "auto":
cbar_tick_format = "." if engine == "plotly" else "%i"
display = plot_surf(mesh, surf_map=roi, bg_map=bg_map,
hemi=hemi, view=view, engine=engine,
avg_method='median', threshold=threshold,
cmap=cmap, symmetric_cmap=False,

display = plot_surf(mesh,
surf_map=roi,
bg_map=bg_map,
hemi=hemi,
view=view,
engine=engine,
avg_method=avg_method,
threshold=threshold,
cmap=cmap,
cbar_tick_format=cbar_tick_format,
alpha=alpha, bg_on_data=bg_on_data,
darkness=darkness, vmin=vmin, vmax=vmax,
title=title, title_font_size=title_font_size,
output_file=output_file, axes=axes,
figure=figure, **kwargs)
alpha=alpha,
bg_on_data=bg_on_data,
darkness=darkness,
vmin=vmin,
vmax=vmax,
title=title,
title_font_size=title_font_size,
output_file=output_file,
axes=axes,
figure=figure,
**kwargs)

return display
16 changes: 16 additions & 0 deletions nilearn/plotting/tests/test_surf_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,3 +1114,19 @@ def test_compute_facecolors_matplotlib():
0.5,
alpha,
)


@pytest.mark.parametrize("avg_method", ["mean", "median"])
@pytest.mark.parametrize("symmetric_cmap", [True, False, None])
@pytest.mark.parametrize("engine", ["matplotlib", "plotly"])
def test_plot_surf_roi_default_arguments(engine, symmetric_cmap, avg_method):
"""Regression test for https://github.com/nilearn/nilearn/issues/3941"""
if not PLOTLY_INSTALLED:
pytest.skip('Plotly is not installed; required for this test.')
mesh, roi_map, _ = _generate_data_test_surf_roi()
plot_surf_roi(mesh, roi_map=roi_map,
engine=engine,
symmetric_cmap=symmetric_cmap,
darkness=None, # to avoid deprecation warning
cmap="RdYlBu_r",
avg_method=avg_method)

0 comments on commit 6578329

Please sign in to comment.