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

[MAINT][WIP] Add pytest-mpl unit tests to check ouput changes in plots #4123

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Test if public plotting functions' output has changed.

Sometimes, the output of a plotting function may unintentionanly change as a
side effect of changing another function or piece of code that it depends on.
These tests ensure that the outputs are not accidentally changed.

Failures are expected at times when the output is changed intentionally
(e.g. fixing a bug or adding features) for a particular function. In such
cases, the output needs to be manually/visually checked as part of the PR
review process and then a new baseline set for comparison.

Set a new baseline by running:

pytest nilearn/plotting/tests/test_img_plotting/test_baseline_comparisons.py \
--mpl-generate-path=nilearn/plotting/tests/test_img_plotting/baseline

"""

import pytest

from nilearn.plotting import (
plot_anat,
plot_carpet,
plot_epi,
plot_glass_brain,
plot_img,
plot_prob_atlas,
plot_roi,
plot_stat_map,
)
from nilearn.plotting.img_plotting import MNI152TEMPLATE

PLOTTING_FUNCS_3D = {
plot_img,
plot_anat,
plot_stat_map,
plot_roi,
plot_epi,
plot_glass_brain,
}

PLOTTING_FUNCS_4D = {plot_prob_atlas, plot_carpet}


@pytest.mark.mpl_image_compare
@pytest.mark.parametrize("plot_func", PLOTTING_FUNCS_3D)
def test_plot_functions_3d_default_params(plot_func, img_3d_mni):
"""Smoke tests for 3D plotting functions with default parameters."""
return plot_func(img_3d_mni)


@pytest.mark.mpl_image_compare
def test_plot_carpet_default_params(img_4d_mni, img_3d_ones_mni):
"""Smoke-test for 4D plot_carpet with default arguments."""
return plot_carpet(img_4d_mni, mask_img=img_3d_ones_mni)


@pytest.mark.mpl_image_compare
def test_plot_prob_atlas_default_params(img_3d_mni, img_4d_mni):
"""Smoke-test for plot_prob_atlas with default arguments."""
return plot_prob_atlas(img_4d_mni, bg_img=img_3d_mni)


@pytest.mark.mpl_image_compare
@pytest.mark.parametrize("anat_img", [False, MNI152TEMPLATE])
@pytest.mark.parametrize("display_mode", ["z", "ortho"])
def test_plot_anat_MNI(anat_img, display_mode, tmp_path):
"""Tests for plot_anat with MNI template."""
return plot_anat(anat_img=anat_img, display_mode=display_mode)
10 changes: 9 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ deps =
plotly
kaleido ; platform_system != 'Windows'
kaleido==0.1.0.post1 ; platform_system == 'Windows'
pytest-mpl


; COMMANDS
Expand Down Expand Up @@ -85,8 +86,9 @@ extras = test
deps =
{[plotting]deps}
commands =
{[testenv:test_latest]commands}
{[testenv:test_latest]commands}
{[testenv:test_doc]commands}
{[testenv:test_pytest_mpl]commands}

[testenv:test_doc]
description = run tests on doc
Expand Down Expand Up @@ -142,6 +144,12 @@ deps =
commands =
pytest --pyargs nilearn --random-order {posargs:}

[testenv:test_pytest_mpl]
description = run pytest-mpl tests
passenv = USERNAME
commands =
pytest nilearn/plotting/tests/test_img_plotting/test_baseline_comparisons.py --mpl

[testenv:test_numpy2]
description = run tests on latest version of all dependencies
passenv = USERNAME
Expand Down