Coverage for nilearn/plotting/tests/test_img_plotting/test_plot_glass_brain.py: 0%
54 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_glass_brain`."""
3# ruff: noqa: ARG001
5import matplotlib.pyplot as plt
6import numpy as np
7import pytest
8from nibabel import Nifti1Image
10from nilearn.image import get_data
11from nilearn.plotting import plot_glass_brain
14@pytest.mark.parametrize("plot_abs", [True, False])
15@pytest.mark.parametrize("resampling_interpolation", ["nearest", "continuous"])
16def test_plot_glass_brain_absolute(
17 matplotlib_pyplot, img_3d_mni, plot_abs, resampling_interpolation
18):
19 """Smoke test resampling_interpolation and for absolute value plotting."""
20 plot_glass_brain(img_3d_mni, plot_abs=plot_abs)
23def test_plot_glass_brain_file_output(matplotlib_pyplot, img_3d_mni, tmp_path):
24 """Smoke-test for hemispheric glass brain with file output."""
25 filename = tmp_path / "test.png"
26 plot_glass_brain(
27 img_3d_mni,
28 output_file=filename,
29 display_mode="lzry",
30 )
33def test_plot_noncurrent_axes(matplotlib_pyplot, rng):
34 """Regression test for Issue #450."""
35 maps_img = Nifti1Image(rng.random((10, 10, 10)), np.eye(4))
36 fh1 = plt.figure()
37 fh2 = plt.figure()
38 ax1 = fh1.add_subplot(1, 1, 1)
40 assert plt.gcf() == fh2, "fh2 was the last plot created."
42 # Since we gave ax1, the figure should be plotted in fh1.
43 # Before #451, it was plotted in fh2.
44 slicer = plot_glass_brain(maps_img, axes=ax1, title="test")
45 for ax_name, niax in slicer.axes.items():
46 ax_fh = niax.ax.get_figure()
47 assert ax_fh == fh1, f"New axis {ax_name} should be in fh1."
50def test_add_markers_using_plot_glass_brain(matplotlib_pyplot):
51 """Tests for adding markers through plot_glass_brain."""
52 fig = plot_glass_brain(None)
53 coords = [(-34, -39, -9)]
54 fig.add_markers(coords)
55 fig.close()
58def test_add_markers_right_hemi(matplotlib_pyplot):
59 """Add a single marker in right hemisphere.
61 No marker should appear in the left hemisphere when plotting.
62 """
63 display = plot_glass_brain(None, display_mode="lyrz")
64 display.add_markers([[20, 20, 20]])
66 # Check that Axe 'l' has no marker
67 assert display.axes["l"].ax.collections[0].get_offsets().data.shape == (
68 0,
69 2,
70 )
71 # Check that all other Axes have one marker
72 for d in "ryz":
73 assert display.axes[d].ax.collections[0].get_offsets().data.shape == (
74 1,
75 2,
76 )
79def test_add_markers_left_hemi(matplotlib_pyplot):
80 """Add two markers in left hemisphere.
82 No marker should appear in the right hemisphere when plotting.
83 """
84 display = plot_glass_brain(None, display_mode="lyrz")
85 display.add_markers(
86 [[-20, 20, 20], [-10, 10, 10]], marker_color=["r", "b"]
87 )
88 # Check that Axe 'r' has no marker
89 assert display.axes["r"].ax.collections[0].get_offsets().data.shape == (
90 0,
91 2,
92 )
93 # Check that all other Axes have two markers
94 for d in "lyz":
95 assert display.axes[d].ax.collections[0].get_offsets().data.shape == (
96 2,
97 2,
98 )
101def test_plot_glass_brain_colorbar_having_nans(
102 matplotlib_pyplot, affine_eye, img_3d_mni
103):
104 """Smoke-test for plot_glass_brain and nans in the data image."""
105 data = get_data(img_3d_mni)
106 data[6, 5, 2] = np.inf
107 plot_glass_brain(Nifti1Image(data, affine_eye), colorbar=True)
110@pytest.mark.parametrize("display_mode", ["lr", "lzry"])
111def test_plot_glass_brain_display_modes_without_img(
112 matplotlib_pyplot, display_mode
113):
114 """Smoke test for work around from PR #1888."""
115 plot_glass_brain(None, display_mode=display_mode)
118@pytest.mark.parametrize("display_mode", ["lr", "lzry"])
119def test_plot_glass_brain_with_completely_masked_img(
120 matplotlib_pyplot, img_3d_mni, display_mode
121):
122 """Smoke test for PR #1888 with display modes having 'l'."""
123 plot_glass_brain(img_3d_mni, display_mode=display_mode)
126def test_plot_glass_brain_negative_vmin_with_plot_abs(
127 matplotlib_pyplot, img_3d_mni
128):
129 """Test that warning is thrown if plot_abs is True and vmin is negative."""
130 warning_message = "vmin is negative but plot_abs is True"
131 with pytest.warns(UserWarning, match=warning_message):
132 plot_glass_brain(img_3d_mni, vmin=-2, plot_abs=True)