Skip to content

Commit cb8a179

Browse files
committed
refactor(types): replace Num type alias with explicit int | float
1 parent d242a6b commit cb8a179

9 files changed

Lines changed: 203 additions & 235 deletions

File tree

.codegraph/.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# CodeGraph data files
2+
# These are local to each machine and should not be committed
3+
4+
# Database
5+
*.db
6+
*.db-wal
7+
*.db-shm
8+
9+
# Cache
10+
cache/
11+
12+
# Logs
13+
*.log
14+
15+
# Hook markers
16+
.dirty

.deepseek/instructions.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ site/
1414
.vscode/
1515
notebooks/*
1616
tests/test.py
17-
.deepseek/*
17+
.codegraph/*

src/plotfig/brain_connection.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
warnings.filterwarnings("ignore", category=DeprecationWarning)
2020

21-
Num = int | float
22-
2321
__all__ = [
2422
"plot_brain_connection_figure",
2523
"save_brain_connection_frames",
@@ -208,11 +206,11 @@ def plot_brain_connection_figure(
208206
niigz_file: str | Path,
209207
output_file: str | Path | None = None,
210208
show_all_nodes: bool = False,
211-
nodes_size: Sequence[Num] | npt.NDArray | None = None,
209+
nodes_size: Sequence[int | float] | npt.NDArray | None = None,
212210
nodes_name: list[str] | None = None,
213211
nodes_color: list[str] | None = None,
214212
scale_method: Literal["", "width", "color", "width_color", "color_width"] = "",
215-
line_width: Num = 10,
213+
line_width: int | float = 10,
216214
line_color: str = "red",
217215
) -> go.Figure:
218216
"""绘制大脑连接图,保存在指定的html文件中。
@@ -231,7 +229,7 @@ def plot_brain_connection_figure(
231229
输出HTML文件路径。如果未指定,则使用当前时间戳生成文件名。默认为None
232230
show_all_nodes (bool, optional):
233231
是否显示所有脑区节点。如果为False,则只显示有连接的节点。默认为False
234-
nodes_size (Sequence[Num] | npt.NDArray | None, optional):
232+
nodes_size (Sequence[int | float] | npt.NDArray | None, optional):
235233
每个节点的大小,长度应与脑区数量一致。默认为None,即所有节点大小为5
236234
nodes_name (list[str] | None, optional):
237235
每个节点的名称标签,长度应与脑区数量一致。默认为None,即不显示名称
@@ -244,7 +242,7 @@ def plot_brain_connection_figure(
244242
- "color" : 根据连接强度调整颜色(使用蓝白红颜色映射),线宽固定
245243
- "width_color" or "color_width" : 同时根据连接强度调整线宽和颜色
246244
默认为 ""
247-
line_width (Num, optional):
245+
line_width (int | float, optional):
248246
连接线的基本宽度。当scale_method包含"width"时,此值作为最大宽度参考。默认为10
249247
line_color (str, optional):
250248
连接线的基本颜色。当scale_method不包含"color"时生效。默认为"#ff0000"(红色)

src/plotfig/correlation.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
)
1414
from scipy import stats
1515

16-
Num: TypeAlias = int | float | np.integer | np.floating
16+
1717
StatsMethod: TypeAlias = Literal["spearman", "pearson"]
1818
AxisFormat: TypeAlias = Literal["normal", "sci", "1f", "percent"]
1919

2020
__all__ = ["plot_correlation_figure"]
2121

2222

2323
def plot_correlation_figure(
24-
data1: Sequence[Num] | np.ndarray,
25-
data2: Sequence[Num] | np.ndarray,
24+
data1: Sequence[float] | np.ndarray,
25+
data2: Sequence[float] | np.ndarray,
2626
ax: Axes | None = None,
2727
stats_method: StatsMethod = "spearman",
2828
ci: bool = False,
@@ -53,15 +53,15 @@ def plot_correlation_figure(
5353
hexbin: bool = False,
5454
hexbin_cmap: Colormap | None = None,
5555
hexbin_gridsize: int = 50,
56-
xlim: list[Num] | tuple[Num, Num] | None = None,
57-
ylim: list[Num] | tuple[Num, Num] | None = None,
56+
xlim: tuple[float, float] | None = None,
57+
ylim: tuple[float, float] | None = None,
5858
) -> Axes | PolyCollection:
5959
"""
6060
绘制两个数据集之间的相关性图,支持线性回归、置信区间和统计方法(Spearman 或 Pearson)。
6161
6262
Args:
63-
data1 (Sequence[Num] | np.ndarray): 第一个数据集。
64-
data2 (Sequence[Num] | np.ndarray): 第二个数据集。
63+
data1 (Sequence[float] | np.ndarray): 第一个数据集。
64+
data2 (Sequence[float] | np.ndarray): 第二个数据集。
6565
ax (Axes | None, optional): matplotlib 的 Axes 对象。默认为 None(使用当前 Axes)。
6666
stats_method (StatsMethod, optional): 相关性统计方法,支持 "spearman" 和 "pearson"。默认为 "spearman"。
6767
ci (bool, optional): 是否绘制置信区间带。默认为 False。
@@ -92,8 +92,8 @@ def plot_correlation_figure(
9292
hexbin (bool, optional): 是否使用六边形箱图。默认为 False。
9393
hexbin_cmap (Colormap | None, optional): 六边形箱图的颜色映射。默认为 None。
9494
hexbin_gridsize (int, optional): 六边形箱图网格大小。默认为 50。
95-
xlim (list[Num] | tuple[Num, Num] | None, optional): X 轴范围限制。默认为 None。
96-
ylim (list[Num] | tuple[Num, Num] | None, optional): Y 轴范围限制。默认为 None。
95+
xlim (tuple[float, float] | None, optional): X 轴范围限制。默认为 None。
96+
ylim (tuple[float, float] | None, optional): Y 轴范围限制。默认为 None。
9797
9898
Returns:
9999
Axes | PolyCollection: 默认返回 Axes;当 hexbin=True 时返回 hexbin 对象。
@@ -103,13 +103,13 @@ def set_axis(
103103
ax: Axes,
104104
axis: Literal["x", "y"],
105105
label: str,
106-
labelsize: Num,
107-
ticksize: Num,
108-
rotation: Num,
106+
labelsize: float,
107+
ticksize: float,
108+
rotation: float,
109109
locator: float | None,
110110
max_tick_value: float | None,
111111
fmt: AxisFormat,
112-
lim: list[Num] | tuple[Num, Num] | None,
112+
lim: tuple[float, float] | None,
113113
) -> None:
114114
if axis == "x":
115115
set_label = ax.set_xlabel
@@ -153,7 +153,7 @@ def set_axis(
153153
A = np.asarray(data1)
154154
B = np.asarray(data2)
155155

156-
slope, intercept, r_value, p_value, _ = stats.linregress(A, B)
156+
slope, intercept, _, _, _ = stats.linregress(A, B)
157157
x_seq = np.linspace(A.min(), A.max(), 100)
158158
y_pred = slope * x_seq + intercept
159159

src/plotfig/matrix.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from matplotlib.axes import Axes
66
from mpl_toolkits.axes_grid1 import make_axes_locatable
77

8-
Num = int | float
9-
108
__all__ = ["plot_matrix_figure"]
119

1210

@@ -16,21 +14,21 @@ def plot_matrix_figure(
1614
row_labels_name: Sequence[str] | None = None,
1715
col_labels_name: Sequence[str] | None = None,
1816
cmap: str = "bwr",
19-
vmin: Num | None = None,
20-
vmax: Num | None = None,
17+
vmin: int | float | None = None,
18+
vmax: int | float | None = None,
2119
aspect: str = "equal",
2220
colorbar: bool = True,
2321
colorbar_label_name: str = "",
24-
colorbar_pad: Num = 0.1,
25-
colorbar_label_fontsize: Num = 10,
26-
colorbar_tick_fontsize: Num = 10,
27-
colorbar_tick_rotation: Num = 0,
28-
row_labels_fontsize: Num = 10,
29-
col_labels_fontsize: Num = 10,
30-
x_rotation: Num = 60,
22+
colorbar_pad: int | float = 0.1,
23+
colorbar_label_fontsize: int | float = 10,
24+
colorbar_tick_fontsize: int | float = 10,
25+
colorbar_tick_rotation: int | float = 0,
26+
row_labels_fontsize: int | float = 10,
27+
col_labels_fontsize: int | float = 10,
28+
x_rotation: int | float = 60,
3129
title_name: str = "",
32-
title_fontsize: Num = 15,
33-
title_pad: Num = 20,
30+
title_fontsize: int | float = 15,
31+
title_pad: int | float = 20,
3432
diag_border: bool = False,
3533
xlabel: str | None = None,
3634
ylabel: str | None = None,
@@ -45,21 +43,21 @@ def plot_matrix_figure(
4543
row_labels_name (Sequence[str] | None): 行标签列表。
4644
col_labels_name (Sequence[str] | None): 列标签列表。
4745
cmap (str): 矩阵使用的颜色映射。
48-
vmin (Num | None): 颜色缩放的最小值,默认使用 data.min()。
49-
vmax (Num | None): 颜色缩放的最大值,默认使用 data.max()。
46+
vmin (int | float | None): 颜色缩放的最小值,默认使用 data.min()。
47+
vmax (int | float | None): 颜色缩放的最大值,默认使用 data.max()。
5048
aspect (str): 图像的纵横比,通常为 "equal" 或 "auto"。
5149
colorbar (bool): 是否显示颜色条。
5250
colorbar_label_name (str): 颜色条的标签。
53-
colorbar_pad (Num): 颜色条与矩阵之间的间距。
54-
colorbar_label_fontsize (Num): 颜色条标签的字体大小。
55-
colorbar_tick_fontsize (Num): 颜色条刻度的字体大小。
56-
colorbar_tick_rotation (Num): 颜色条刻度标签的旋转角度。
57-
row_labels_fontsize (Num): 行标签的字体大小。
58-
col_labels_fontsize (Num): 列标签的字体大小。
59-
x_rotation (Num): x 轴(列)标签的旋转角度。
60-
title_name (Num): 图表标题。
61-
title_fontsize (Num): 标题的字体大小。
62-
title_pad (Num): 标题上方的间距。
51+
colorbar_pad (int | float): 颜色条与矩阵之间的间距。
52+
colorbar_label_fontsize (int | float): 颜色条标签的字体大小。
53+
colorbar_tick_fontsize (int | float): 颜色条刻度的字体大小。
54+
colorbar_tick_rotation (int | float): 颜色条刻度标签的旋转角度。
55+
row_labels_fontsize (int | float): 行标签的字体大小。
56+
col_labels_fontsize (int | float): 列标签的字体大小。
57+
x_rotation (int | float): x 轴(列)标签的旋转角度。
58+
title_name (str): 图表标题。
59+
title_fontsize (int | float): 标题的字体大小。
60+
title_pad (int | float): 标题上方的间距。
6361
diag_border (bool): 是否绘制对角线单元格边框。
6462
xlabel (str | None): X轴的整体标签名称。
6563
ylabel (str | None): Y轴的整体标签名称。

src/plotfig/multi_bars.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
warnings.simplefilter("always")
1515

16-
Num = int | float | np.integer | np.floating
1716

1817
__all__ = [
1918
"plot_multi_group_bar_figure",
@@ -72,14 +71,14 @@ def plot_multi_group_bar_figure(
7271
ax: Axes | None = None,
7372
group_labels: list[str] | None = None,
7473
bar_labels: list[str] | None = None,
75-
bar_width: Num = 0.2,
76-
bar_gap: Num = 0.1,
74+
bar_width: float = 0.2,
75+
bar_gap: float = 0.1,
7776
bar_color: list[str] | None = None,
7877
errorbar_type: str = "sd",
7978
dots_color: str = "gray",
8079
dots_size: int = 35,
8180
legend: bool = True,
82-
legend_position: tuple[Num, Num] = (1.2, 1),
81+
legend_position: tuple[float, float] = (1.2, 1),
8382
title_name: str = "",
8483
title_fontsize=12,
8584
title_pad=10,
@@ -95,7 +94,7 @@ def plot_multi_group_bar_figure(
9594
y_lim: tuple[float, float] | None = None,
9695
statistic: bool = False,
9796
test_method: str = "external",
98-
p_list: Sequence[Sequence[Num]] | None = None,
97+
p_list: Sequence[Sequence[float]] | None = None,
9998
line_color="0.5",
10099
asterisk_fontsize=10,
101100
asterisk_color="k",
@@ -120,14 +119,14 @@ def plot_multi_group_bar_figure(
120119
ax (Axes | None): matplotlib 的 Axes 对象。如果为 None,使用当前活动的 Axes。
121120
group_labels (list[str] | None): 每个组的标签。如果为 None,自动生成 "Group 1", "Group 2" 等。
122121
bar_labels (list[str] | None): 每个柱子的标签,用于图例。如果为 None,自动生成 "Bar 1", "Bar 2" 等。
123-
bar_width (Num): 柱子的宽度。默认为 0.2。
124-
bar_gap (Num): 同一组内柱子之间的间隔。默认为 0.1。
122+
bar_width (float): 柱子的宽度。默认为 0.2。
123+
bar_gap (float): 同一组内柱子之间的间隔。默认为 0.1。
125124
bar_color (list[str] | None): 每个柱子的颜色列表。如果为 None,所有柱子使用灰色。
126125
errorbar_type (str): 误差线类型,'sd' 表示标准差,'se' 表示标准误。默认为 'sd'。
127126
dots_color (str): 散点的颜色。默认为 'gray'。
128127
dots_size (int): 散点的大小。默认为 35。
129128
legend (bool): 是否显示图例。默认为 True。
130-
legend_position (tuple[Num, Num]): 图例位置,使用 bbox_to_anchor 坐标。默认为 (1.2, 1)。
129+
legend_position (tuple[float, float]): 图例位置,使用 bbox_to_anchor 坐标。默认为 (1.2, 1)。
131130
title_name (str): 图表标题。默认为空字符串。
132131
title_fontsize (int): 标题字体大小。默认为 12。
133132
title_pad (int): 标题与图表的间距。默认为 10。
@@ -144,7 +143,7 @@ def plot_multi_group_bar_figure(
144143
如果为 None,根据数据自动计算。
145144
statistic (bool): 是否添加统计显著性标注。默认为 False。
146145
test_method (str): 统计检验方法。当前仅支持 'external'(使用外部提供的 p 值)。
147-
p_list (list[list[Num]] | None): 外部提供的 p 值列表。
146+
p_list (list[list[float]] | None): 外部提供的 p 值列表。
148147
结构为 [组1的p值列表, 组2的p值列表, ...],每个组的 p 值列表对应该组内所有两两比较。
149148
当 statistic=True 且 test_method='external' 时必须提供。
150149
line_color (str): 显著性标注连线的颜色。默认为 '0.5'(中灰色)。

0 commit comments

Comments
 (0)