Skip to content

Commit d6fe349

Browse files
committed
refactor(plotfig): improve type hints for test_method and x_label_ha parameters
1 parent acfba56 commit d6fe349

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

src/plotfig/single_bar.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import warnings
2-
from typing import Sequence
2+
from typing import Literal, Sequence, TypeAlias
33

44
import matplotlib.pyplot as plt
55
import numpy as np
@@ -17,6 +17,10 @@
1717
warnings.simplefilter("always")
1818

1919
Num = int | float | np.integer | np.floating
20+
TestMethod: TypeAlias = Literal[
21+
"ttest_ind", "ttest_rel", "ttest_1samp", "mannwhitneyu", "external"
22+
]
23+
TestMethodSpec: TypeAlias = Sequence[str]
2024

2125
__all__ = [
2226
"plot_one_group_bar_figure",
@@ -200,7 +204,7 @@ def plot_one_group_bar_figure(
200204
title_fontsize: Num = 12,
201205
title_pad: Num = 10,
202206
x_label_name: str = "",
203-
x_label_ha: str = "center",
207+
x_label_ha: Literal["center", "right", "left"] = "center",
204208
x_label_fontsize: Num = 12,
205209
x_tick_fontsize: Num = 12,
206210
x_tick_rotation: Num = 0,
@@ -210,7 +214,7 @@ def plot_one_group_bar_figure(
210214
y_tick_rotation: Num = 0,
211215
y_lim: tuple[float, float] | None = None,
212216
statistic: bool = False,
213-
test_method: list[str] | None = None,
217+
test_method: TestMethodSpec | None = None,
214218
p_list: Sequence[Num] | None = None,
215219
popmean: Num = 0,
216220
statistical_line_color: str = "0.5",
@@ -263,8 +267,8 @@ def plot_one_group_bar_figure(
263267
标题与图表的间距. Defaults to 10.
264268
x_label_name (str, optional):
265269
X轴标签名称. Defaults to "".
266-
x_label_ha (str, optional):
267-
X轴标签的水平对齐方式. Defaults to "center".
270+
x_label_ha (Literal["center", "right", "left"], optional):
271+
X轴标签的水平对齐方式,可选 "center"、"right" 或 "left". Defaults to "center".
268272
x_label_fontsize (Num, optional):
269273
X轴标签字体大小. Defaults to 12.
270274
x_tick_fontsize (Num, optional):
@@ -284,12 +288,11 @@ def plot_one_group_bar_figure(
284288
statistic (bool, optional):
285289
是否进行统计显著性分析. Defaults to False.
286290
test_method (list[str], optional):
287-
统计检验方法列表,包括
288-
1. `ttest_ind`,
289-
2. `ttest_rel`,
290-
3. `ttest_1samp`,
291-
4. `mannwhitneyu`,
292-
5. `external`.
291+
统计检验方法列表,仅支持两种形式:
292+
1. 长度为1:`["ttest_ind"]`、`["ttest_rel"]`、`["ttest_1samp"]`、
293+
`["mannwhitneyu"]` 或 `["external"]`。
294+
2. 长度为2:必须包含 `ttest_1samp`,另一个元素为
295+
`ttest_ind`、`ttest_rel`、`mannwhitneyu` 或 `external` 之一。
293296
Defaults to ["ttest_ind"].
294297
p_list (Sequence[Num] | None, optional):
295298
预计算的p值列表,用于显著性标记. Defaults to None.
@@ -477,7 +480,7 @@ def plot_one_group_violin_figure(
477480
title_fontsize: Num = 12,
478481
title_pad: Num = 10,
479482
x_label_name: str = "",
480-
x_label_ha: str = "center",
483+
x_label_ha: Literal["center", "right", "left"] = "center",
481484
x_label_fontsize: Num = 10,
482485
x_tick_fontsize: Num = 8,
483486
x_tick_rotation: Num = 0,
@@ -487,7 +490,7 @@ def plot_one_group_violin_figure(
487490
y_tick_rotation: Num = 0,
488491
y_lim: tuple[float, float] | None = None,
489492
statistic: bool = False,
490-
test_method: list[str] | None = None,
493+
test_method: TestMethodSpec | None = None,
491494
popmean: Num = 0,
492495
p_list: Sequence[Num] | None = None,
493496
statistical_line_color: str = "0.5",
@@ -534,8 +537,8 @@ def plot_one_group_violin_figure(
534537
标题与图表的间距. Defaults to 10.
535538
x_label_name (str, optional):
536539
X轴标签名称. Defaults to "".
537-
x_label_ha (str, optional):
538-
X轴标签的水平对齐方式. Defaults to "center".
540+
x_label_ha (Literal["center", "right", "left"], optional):
541+
X轴标签的水平对齐方式,可选 "center"、"right" 或 "left". Defaults to "center".
539542
x_label_fontsize (Num, optional):
540543
X轴标签字体大小. Defaults to 10.
541544
x_tick_fontsize (Num, optional):
@@ -555,7 +558,12 @@ def plot_one_group_violin_figure(
555558
statistic (bool, optional):
556559
是否进行统计显著性分析. Defaults to False.
557560
test_method (list[str], optional):
558-
统计检验方法列表. Defaults to ["ttest_ind"].
561+
统计检验方法列表,仅支持两种形式:
562+
1. 长度为1:`["ttest_ind"]`、`["ttest_rel"]`、`["ttest_1samp"]`、
563+
`["mannwhitneyu"]` 或 `["external"]`。
564+
2. 长度为2:必须包含 `ttest_1samp`,另一个元素为
565+
`ttest_ind`、`ttest_rel`、`mannwhitneyu` 或 `external` 之一。
566+
Defaults to ["ttest_ind"].
559567
popmean (Num, optional):
560568
单样本t检验的假设均值. Defaults to 0.
561569
p_list (Sequence[Num] | None, optional):

0 commit comments

Comments
 (0)