|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import pytest |
| 3 | +from matplotlib.axes import Axes |
| 4 | + |
| 5 | +from plotfig import plot_brain_surface_figure |
| 6 | + |
| 7 | + |
| 8 | +class TestPlotBrainSurfaceFigureSuccesses: |
| 9 | + """测试 plot_brain_surface_figure 函数的正常绘图功能""" |
| 10 | + |
| 11 | + def setup_method(self): |
| 12 | + """测试前初始化:创建图形和测试数据""" |
| 13 | + self.fig, self.ax = plt.subplots() |
| 14 | + self.test_data = { |
| 15 | + "lh_V1": 1.0, |
| 16 | + "rh_V2": 2.0, |
| 17 | + } |
| 18 | + |
| 19 | + def teardown_method(self): |
| 20 | + """测试后清理:关闭图形""" |
| 21 | + plt.close(self.fig) |
| 22 | + |
| 23 | + def test_basic_plotting(self): |
| 24 | + """最基本的烟雾测试:确保函数能正常运行并返回Axes对象""" |
| 25 | + result = plot_brain_surface_figure(self.test_data, ax=self.ax) |
| 26 | + |
| 27 | + assert isinstance(result, Axes) |
| 28 | + |
| 29 | + def test_with_custom_parameters(self): |
| 30 | + """测试自定义参数(species、atlas、colorbar等)是否能正常工作""" |
| 31 | + custom_data = { |
| 32 | + "lh_MST": 1.0, |
| 33 | + "rh_FST": 2.0, |
| 34 | + } |
| 35 | + |
| 36 | + result = plot_brain_surface_figure( |
| 37 | + custom_data, |
| 38 | + species="macaque", |
| 39 | + atlas="charm6", |
| 40 | + surf="midthickness", |
| 41 | + ax=self.ax, |
| 42 | + vmin=0, |
| 43 | + vmax=5, |
| 44 | + cmap="Reds", |
| 45 | + colorbar=True, |
| 46 | + colorbar_location="right", |
| 47 | + colorbar_label_name="Myelin Content", |
| 48 | + colorbar_label_rotation=0, |
| 49 | + colorbar_decimals=2, |
| 50 | + colorbar_fontsize=10, |
| 51 | + colorbar_nticks=3, |
| 52 | + colorbar_shrink=0.2, |
| 53 | + colorbar_aspect=9, |
| 54 | + colorbar_draw_border=True, |
| 55 | + title_name="测试图", |
| 56 | + title_fontsize=13, |
| 57 | + as_outline=False, |
| 58 | + ) |
| 59 | + |
| 60 | + assert isinstance(result, Axes) |
| 61 | + assert result.get_title() == "测试图" |
| 62 | + |
| 63 | + def test_vmin_equals_vmax(self): |
| 64 | + """测试 vmin 等于 vmax 时的特殊处理""" |
| 65 | + data = {"lh_V1": 5.0, "rh_V2": 5.0} |
| 66 | + result = plot_brain_surface_figure(data, ax=self.ax, vmin=5, vmax=5) |
| 67 | + assert isinstance(result, Axes) |
| 68 | + |
| 69 | + def test_negative_values(self): |
| 70 | + """测试负数值""" |
| 71 | + data = {"lh_V1": -1.0, "rh_V2": -2.0} |
| 72 | + result = plot_brain_surface_figure(data, ax=self.ax) |
| 73 | + assert isinstance(result, Axes) |
| 74 | + |
| 75 | + def test_large_values(self): |
| 76 | + """测试大数值""" |
| 77 | + data = {"lh_V1": 1000.0, "rh_V2": 200000.0} |
| 78 | + result = plot_brain_surface_figure(data, ax=self.ax) |
| 79 | + assert isinstance(result, Axes) |
| 80 | + |
| 81 | + |
| 82 | +class TestPlotBrainSurfaceFigureErrors: |
| 83 | + """测试 plot_brain_surface_figure 函数的参数验证和异常处理""" |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def test_empty_data_raises_error(): |
| 87 | + """测试空数据应该抛出异常""" |
| 88 | + with pytest.raises(ValueError, match="data 不能为空"): |
| 89 | + plot_brain_surface_figure({}) |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + def test_vmin_greater_than_vmax_raises_error(): |
| 93 | + """测试vmin大于vmax时应该抛出异常""" |
| 94 | + data = {"lh_V1": 1.0} |
| 95 | + |
| 96 | + with pytest.raises(ValueError, match="vmin必须小于等于vmax"): |
| 97 | + plot_brain_surface_figure(data, vmin=10, vmax=5) |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def test_unsupported_species_raises_error(): |
| 101 | + """测试不支持的物种应该抛出异常""" |
| 102 | + data = {"lh_V1": 1.0} |
| 103 | + |
| 104 | + with pytest.raises(ValueError, match="不支持的物种"): |
| 105 | + plot_brain_surface_figure(data, species="dog") |
| 106 | + |
| 107 | + @staticmethod |
| 108 | + def test_unsupported_atlas_raises_error(): |
| 109 | + """测试不支持的图集应该抛出异常""" |
| 110 | + data = {"lh_V1": 1.0} |
| 111 | + |
| 112 | + with pytest.raises(ValueError, match="不支持的图集"): |
| 113 | + plot_brain_surface_figure(data, atlas="invalid_atlas") |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def test_invalid_region_label_raises_error(): |
| 117 | + """测试脑区标签在图集中找不到时应该抛出异常""" |
| 118 | + data = {"lh_invalid_region": 1.0} |
| 119 | + with pytest.raises(ValueError, match="以下脑区标签在指定图集中未找到"): |
| 120 | + plot_brain_surface_figure(data) |
0 commit comments