Coverage for nilearn/plotting/tests/test_img_plotting/test_plot_carpet.py: 0%

45 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-16 12:32 +0200

1"""Tests for :func:`nilearn.plotting.plot_carpet`.""" 

2 

3# ruff: noqa: ARG001 

4 

5import warnings 

6 

7import matplotlib.pyplot as plt 

8import numpy as np 

9import pytest 

10 

11from nilearn.plotting import plot_carpet 

12 

13 

14def test_plot_carpet(matplotlib_pyplot, img_4d_mni, img_3d_ones_mni): 

15 """Check contents of plot_carpet figure against data in image.""" 

16 display = plot_carpet( 

17 img_4d_mni, img_3d_ones_mni, detrend=False, title="TEST" 

18 ) 

19 

20 # Next two lines retrieve the numpy array from the plot 

21 ax = display.axes[0] 

22 plotted_array = ax.images[0].get_array() 

23 assert plotted_array.shape == ( 

24 np.prod(img_4d_mni.shape[:-1]), 

25 img_4d_mni.shape[-1], 

26 ) 

27 # Make sure that the values in the figure match the values in the image 

28 np.testing.assert_almost_equal( 

29 plotted_array.sum(), img_4d_mni.get_fdata().sum(), decimal=3 

30 ) 

31 

32 

33def test_plot_carpet_long_acquisition( 

34 matplotlib_pyplot, img_3d_ones_mni, img_4d_long_mni 

35): 

36 """Check contents of plot_carpet for img with many volumes.""" 

37 fig, ax = plt.subplots() 

38 display = plot_carpet( 

39 img_4d_long_mni, 

40 img_3d_ones_mni, 

41 title="TEST", 

42 figure=fig, 

43 axes=ax, 

44 ) 

45 

46 # Next two lines retrieve the numpy array from the plot 

47 ax = display.axes[0] 

48 plotted_array = ax.images[0].get_array() 

49 # Check size 

50 n_items = np.prod(img_4d_long_mni.shape[:-1]) * np.ceil( 

51 img_4d_long_mni.shape[-1] / 2 

52 ) 

53 assert plotted_array.size == n_items 

54 

55 

56def test_plot_carpet_with_atlas(matplotlib_pyplot, img_4d_mni, img_atlas): 

57 """Test plot_carpet when using an atlas.""" 

58 # t_r is set explicitly for this test as well 

59 display = plot_carpet( 

60 img_4d_mni, 

61 mask_img=img_atlas["img"], 

62 t_r=2, 

63 detrend=False, 

64 title="TEST", 

65 ) 

66 

67 # Check the output 

68 # Two axes: 1 for colorbar and 1 for imshow 

69 assert len(display.axes) == 2 

70 # The y-axis label of the imshow should be 'voxels' since atlas labels are 

71 # unknown 

72 ax = display.axes[1] 

73 assert ax.get_ylabel() == "voxels" 

74 

75 # Next two lines retrieve the numpy array from the plot 

76 ax = display.axes[0] 

77 colorbar = ax.images[0].get_array() 

78 assert len(np.unique(colorbar)) == len(img_atlas["labels"]) 

79 

80 

81def test_plot_carpet_with_atlas_and_labels( 

82 matplotlib_pyplot, img_4d_mni, img_atlas 

83): 

84 """Test plot_carpet when using an atlas and labels.""" 

85 fig, ax = plt.subplots() 

86 

87 display = plot_carpet( 

88 img_4d_mni, 

89 mask_img=img_atlas["img"], 

90 mask_labels=img_atlas["labels"], 

91 detrend=True, 

92 title="TEST", 

93 figure=fig, 

94 axes=ax, 

95 ) 

96 

97 # Check the output 

98 # Two axes: 1 for colorbar and 1 for imshow 

99 assert len(display.axes) == 2 

100 ax = display.axes[0] 

101 

102 # The ytick labels of the colorbar should match the atlas labels 

103 yticklabels = ax.get_yticklabels() 

104 yticklabels = [yt.get_text() for yt in yticklabels] 

105 assert set(yticklabels) == set(img_atlas["labels"].keys()) 

106 

107 # Next two lines retrieve the numpy array from the plot 

108 ax = display.axes[0] 

109 colorbar = ax.images[0].get_array() 

110 assert len(np.unique(colorbar)) == len(img_atlas["labels"]) 

111 

112 

113def test_plot_carpet_standardize( 

114 matplotlib_pyplot, img_4d_mni, img_3d_ones_mni 

115): 

116 """Check warning is raised and then suppressed with setting standardize.""" 

117 match = "default strategy for standardize" 

118 

119 with pytest.deprecated_call(match=match): 

120 plot_carpet(img_4d_mni, mask_img=img_3d_ones_mni) 

121 

122 with warnings.catch_warnings(record=True) as record: 

123 plot_carpet( 

124 img_4d_mni, mask_img=img_3d_ones_mni, standardize="zscore_sample" 

125 ) 

126 for m in record: 

127 assert match not in m.message