Coverage for nilearn/plotting/tests/test_img_plotting/test_plot_img.py: 0%
61 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 12:32 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 12:32 +0200
1"""Tests for :func:`nilearn.plotting.plot_img`."""
3# ruff: noqa: ARG001
5import matplotlib.pyplot as plt
6import numpy as np
7import pytest
8from nibabel import Nifti1Image
10from nilearn._utils.niimg import is_binary_niimg
11from nilearn.image import get_data
12from nilearn.plotting import plot_img
15def _testdata_3d_for_plotting_for_resampling(img, binary):
16 """Return testing data for resampling tests.
18 Data can be binarize or not.
19 """
20 data = get_data(img)
21 if binary:
22 data[data > 0] = 1
23 data[data < 0] = 0
24 affine = np.array(
25 [
26 [1.0, -1.0, 0.0, 0.0],
27 [1.0, 1.0, 0.0, 0.0],
28 [0.0, 0.0, 1.0, 0.0],
29 [0.0, 0.0, 0.0, 1.0],
30 ]
31 )
32 return Nifti1Image(data, affine)
35def test_display_methods(matplotlib_pyplot, img_3d_mni):
36 """Tests display methods."""
37 display = plot_img(img_3d_mni)
38 display.add_overlay(img_3d_mni, threshold=0)
39 display.add_edges(img_3d_mni, color="c")
40 display.add_contours(
41 img_3d_mni, contours=2, linewidth=4, colors=["limegreen", "yellow"]
42 )
45def test_display_methods_invalid_threshold(matplotlib_pyplot, img_3d_mni):
46 """Tests display methods for negative threshold."""
47 with pytest.raises(
48 ValueError, match="Threshold should be a non-negative number!"
49 ):
50 display = plot_img(img_3d_mni)
51 display.add_overlay(img_3d_mni, threshold=-1)
53 with pytest.raises(
54 ValueError, match="Threshold should be a non-negative number!"
55 ):
56 display = plot_img(img_3d_mni)
57 display.add_contours(
58 img_3d_mni, contours=2, linewidth=4, threshold=-1, filled=True
59 )
62def test_plot_with_axes_or_figure(matplotlib_pyplot, img_3d_mni):
63 """Smoke tests for plot_img with providing figure or Axes."""
64 figure = plt.figure()
65 plot_img(img_3d_mni, figure=figure)
66 ax = plt.subplot(111)
67 plot_img(img_3d_mni, axes=ax)
70def test_plot_empty_slice(matplotlib_pyplot, affine_mni):
71 """Test that things don't crash when we give a map \
72 with nothing above threshold. This is only a smoke test.
73 """
74 img = Nifti1Image(np.zeros((20, 20, 20)), affine_mni)
75 plot_img(img, display_mode="y", threshold=1)
78@pytest.mark.parametrize("binary_img", [True, False])
79def test_plot_img_with_resampling(matplotlib_pyplot, binary_img, img_3d_mni):
80 """Tests for plot_img with resampling of the data image."""
81 img = _testdata_3d_for_plotting_for_resampling(img_3d_mni, binary_img)
82 if binary_img:
83 assert is_binary_niimg(img)
84 else:
85 assert not is_binary_niimg(img)
86 display = plot_img(img)
87 display.add_overlay(img)
88 display.add_contours(
89 img, contours=2, linewidth=4, colors=["limegreen", "yellow"]
90 )
91 display.add_edges(img, color="c")
94def test_display_methods_with_display_mode_tiled(
95 matplotlib_pyplot, img_3d_mni
96):
97 """Smoke tests for display methods with tiled display mode."""
98 display = plot_img(img_3d_mni, display_mode="tiled")
99 display.add_overlay(img_3d_mni, threshold=0)
100 display.add_edges(img_3d_mni, color="c")
101 display.add_contours(
102 img_3d_mni, contours=2, linewidth=4, colors=["limegreen", "yellow"]
103 )
106@pytest.mark.parametrize("transparency", [-1, 10])
107def test_plot_img_transparency_warning(
108 matplotlib_pyplot, img_3d_ones_mni, transparency
109):
110 """Test transparency is reset to proper values."""
111 with pytest.warns(
112 UserWarning, match="'transparency' must be in the interval"
113 ):
114 plot_img(img_3d_ones_mni, transparency=transparency)
117@pytest.mark.parametrize("transparency_range", [[10, -1], [5]])
118def test_plot_img_transparency_range_error(
119 matplotlib_pyplot, img_3d_ones_mni, transparency_range, transparency_image
120):
121 """Test transparency_range invalid values."""
122 with pytest.raises(
123 ValueError, match="list or tuple of 2 non-negative numbers"
124 ):
125 plot_img(
126 img_3d_ones_mni,
127 transparency=transparency_image,
128 transparency_range=transparency_range,
129 )
132def test_plot_img_transparency_binary_image(
133 matplotlib_pyplot,
134 shape_3d_default,
135 affine_mni,
136 rng,
137 img_3d_ones_mni,
138):
139 """Smoke test with transparency image as binary."""
140 transparency_data = rng.choice(
141 [0, 1], size=shape_3d_default, p=[0.5, 0.5]
142 ).astype("int8")
143 transparency_image = Nifti1Image(transparency_data, affine_mni)
145 plot_img(
146 img_3d_ones_mni,
147 transparency=transparency_image,
148 )