Skip to content

Commit d158578

Browse files
committed
docs(connectivity): update documentation with improved examples
1 parent 790f43b commit d158578

4 files changed

Lines changed: 162 additions & 84 deletions

File tree

docs/usage/brain_connectivity.md

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,76 @@
1-
# 脑连接图
2-
3-
透明的大脑图,可以展示脑区间的连接关系。
4-
需要准备左右半脑的surface文件、脑区相关的nii.gz文件以及连接矩阵。
5-
6-
7-
```python
8-
import numpy as np
9-
from plotfig import *
10-
11-
# 生成一个 31x31 的连接矩阵(对称加权矩阵,对角线为0)
12-
matrix = np.zeros((31, 31))
13-
matrix[0, 1] = 1
14-
matrix[0, 2] = 2
15-
matrix[0, 3] = 3
16-
matrix[4, 1] = -1
17-
matrix[4, 2] = -2
18-
matrix[4, 3] = -3
19-
matrix = (matrix + matrix.T) / 2 # 矩阵对称
20-
21-
connectome = matrix
22-
23-
output_file = "./figures/brain_connection.html"
24-
25-
lh_surfgii_file = r"e:\6_Self\plot_self_brain_connectivity\103818.L.midthickness.32k_fs_LR.surf.gii"
26-
rh_surfgii_file = r"e:\6_Self\plot_self_brain_connectivity\103818.R.midthickness.32k_fs_LR.surf.gii"
27-
niigz_file = rf"e:\6_Self\plot_self_brain_connectivity\human_Self_processing_network.nii.gz"
28-
29-
fig = plot_brain_connection_figure(
30-
connectome,
31-
lh_surfgii_file=lh_surfgii_file,
32-
rh_surfgii_file=rh_surfgii_file,
33-
niigz_file=niigz_file,
34-
output_file=output_file,
35-
scale_method="width",
36-
line_width=10,
37-
)
38-
```
39-
40-
41-
42-
![png](brain_connectivity_files/human.gif)
43-
44-
45-
46-
html文件可以在浏览器中交互。可以手动截图,也可以使用以下命令来生成图片。
47-
48-
49-
```python
50-
from pathlib import Path
51-
52-
53-
Path(f"./figures/brain_connection").mkdir(parents=True, exist_ok=True) # 新建文件夹保存帧图
54-
save_brain_connection_frames(fig, output_dir=rf"./figures/brain_connection", n_frames=36)
55-
```
56-
57-
100%|██████████| 36/36 [02:01<00:00, 3.37s/it]
58-
59-
保存了 36 张图片在 ./figures/brain_connection
60-
61-
62-
63-
1+
# 脑连接图
2+
3+
透明的大脑图,可以展示脑区间的连接关系。
4+
需要准备左右半脑的surface文件、脑区相关的nii.gz文件以及连接矩阵。
5+
6+
7+
```python
8+
from plotfig import plot_brain_connection_figure, gen_symmetric_matrix
9+
10+
11+
# 生成随机的连接矩阵
12+
connectome = gen_symmetric_matrix(31, sparsity=0.1, seed=42)
13+
14+
# 左右脑surface文件以及网络节点文件需自行提供
15+
lh_surfgii_file = r"example_data/103818.L.midthickness.32k_fs_LR.surf.gii"
16+
rh_surfgii_file = r"example_data/103818.R.midthickness.32k_fs_LR.surf.gii"
17+
niigz_file = r"example_data/network.nii.gz"
18+
19+
# html文件输出位置
20+
output_file = "example_data/brain_connection.html"
21+
22+
fig = plot_brain_connection_figure(
23+
connectome,
24+
lh_surfgii_file=lh_surfgii_file,
25+
rh_surfgii_file=rh_surfgii_file,
26+
niigz_file=niigz_file,
27+
output_file=output_file,
28+
scale_method="width",
29+
line_width=10,
30+
nodes_name=[f"ROI_{i}" for i in range(connectome.shape[0])],
31+
)
32+
33+
```
34+
35+
## 结果展示
36+
37+
![output](brain_connectivity_files/output.gif)
38+
39+
html文件可以在浏览器中交互。可以手动截图,也可以使用以下命令来批量生成多视角生成图片。
40+
41+
42+
```python
43+
from pathlib import Path
44+
from plotfig import save_brain_connection_frames
45+
46+
47+
# 新建文件夹保存帧图
48+
Path(f"./example_data/brain_connection_figures").mkdir(parents=True, exist_ok=True)
49+
save_brain_connection_frames(
50+
fig,
51+
output_dir=rf"./example_data/brain_connection_figures",
52+
n_frames=36
53+
)
54+
55+
```
56+
57+
100%|██████████| 36/36 [01:55<00:00, 3.19s/it]
58+
2025-11-24 11:02:55.867 | INFO | plotfig.brain_connection:save_brain_connection_frames:323 - 保存了 36 张图片在 ./example_data/brain_connection_figures
59+
60+
61+
plotfig 提供了将图片序列整合生成 GIF 动画的工具函数。
62+
63+
64+
```python
65+
from pathlib import Path
66+
from plotfig import create_gif_from_images
67+
68+
create_gif_from_images(
69+
Path("example_data/brain_connection_figures"),
70+
output_name="output.gif"
71+
)
72+
73+
```
74+
75+
2025-11-24 11:07:46.885 | INFO | plotfig.brain_connection:create_gif_from_images:417 - GIF 已保存到: example_data\brain_connection_figures\outpug.gif
76+
-2.59 MB
Binary file not shown.
2.88 MB
Loading

notebooks/brain_connectivity.ipynb

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,19 @@
2828
"metadata": {},
2929
"outputs": [],
3030
"source": [
31-
"import numpy as np\n",
32-
"from plotfig import *\n",
31+
"from plotfig import plot_brain_connection_figure, gen_symmetric_matrix\n",
3332
"\n",
34-
"# 生成一个 31x31 的连接矩阵(对称加权矩阵,对角线为0)\n",
35-
"matrix = np.zeros((31, 31))\n",
36-
"matrix[0, 1] = 1\n",
37-
"matrix[0, 2] = 2\n",
38-
"matrix[0, 3] = 3\n",
39-
"matrix[4, 1] = -1\n",
40-
"matrix[4, 2] = -2\n",
41-
"matrix[4, 3] = -3\n",
42-
"matrix = (matrix + matrix.T) / 2 # 矩阵对称\n",
4333
"\n",
44-
"connectome = matrix\n",
34+
"# 生成随机的连接矩阵\n",
35+
"connectome = gen_symmetric_matrix(31, sparsity=0.1, seed=42)\n",
4536
"\n",
46-
"output_file = \"./figures/brain_connection.html\"\n",
37+
"# 左右脑surface文件以及网络节点文件需自行提供\n",
38+
"lh_surfgii_file = r\"example_data/103818.L.midthickness.32k_fs_LR.surf.gii\"\n",
39+
"rh_surfgii_file = r\"example_data/103818.R.midthickness.32k_fs_LR.surf.gii\"\n",
40+
"niigz_file = r\"example_data/network.nii.gz\"\n",
4741
"\n",
48-
"lh_surfgii_file = r\"e:\\6_Self\\plot_self_brain_connectivity\\103818.L.midthickness.32k_fs_LR.surf.gii\"\n",
49-
"rh_surfgii_file = r\"e:\\6_Self\\plot_self_brain_connectivity\\103818.R.midthickness.32k_fs_LR.surf.gii\"\n",
50-
"niigz_file = rf\"e:\\6_Self\\plot_self_brain_connectivity\\human_Self_processing_network.nii.gz\"\n",
42+
"# html文件输出位置\n",
43+
"output_file = \"example_data/brain_connection.html\"\n",
5144
"\n",
5245
"fig = plot_brain_connection_figure(\n",
5346
" connectome,\n",
@@ -57,29 +50,93 @@
5750
" output_file=output_file,\n",
5851
" scale_method=\"width\",\n",
5952
" line_width=10,\n",
60-
")"
53+
" nodes_name=[f\"ROI_{i}\" for i in range(connectome.shape[0])],\n",
54+
")\n"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"id": "b2bd2fc8",
60+
"metadata": {},
61+
"source": [
62+
"## 结果展示"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"id": "b34d7c95",
68+
"metadata": {},
69+
"source": [
70+
"![output](brain_connectivity_files/output.gif)"
6171
]
6272
},
6373
{
6474
"cell_type": "markdown",
6575
"id": "3ffe6519",
6676
"metadata": {},
6777
"source": [
68-
"html文件可以在浏览器中交互。可以手动截图,也可以使用以下命令来生成图片"
78+
"html文件可以在浏览器中交互。可以手动截图,也可以使用以下命令来批量生成多视角生成图片"
6979
]
7080
},
7181
{
7282
"cell_type": "code",
7383
"execution_count": null,
7484
"id": "f257283e",
7585
"metadata": {},
76-
"outputs": [],
86+
"outputs": [
87+
{
88+
"name": "stderr",
89+
"output_type": "stream",
90+
"text": [
91+
"100%|██████████| 36/36 [01:55<00:00, 3.19s/it]\n",
92+
"\u001b[32m2025-11-24 11:02:55.867\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36mplotfig.brain_connection\u001b[0m:\u001b[36msave_brain_connection_frames\u001b[0m:\u001b[36m323\u001b[0m - \u001b[1m保存了 36 张图片在 ./example_data/brain_connection_figures\u001b[0m\n"
93+
]
94+
}
95+
],
7796
"source": [
7897
"from pathlib import Path\n",
98+
"from plotfig import save_brain_connection_frames\n",
7999
"\n",
80100
"\n",
81-
"Path(f\"./figures/brain_connection\").mkdir(parents=True, exist_ok=True) # 新建文件夹保存帧图\n",
82-
"save_brain_connection_frames(fig, output_dir=rf\"./figures/brain_connection\", n_frames=36)"
101+
"# 新建文件夹保存帧图\n",
102+
"Path(f\"./example_data/brain_connection_figures\").mkdir(parents=True, exist_ok=True)\n",
103+
"save_brain_connection_frames(\n",
104+
" fig,\n",
105+
" output_dir=rf\"./example_data/brain_connection_figures\",\n",
106+
" n_frames=36\n",
107+
")\n"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"id": "c1650800",
113+
"metadata": {},
114+
"source": [
115+
"plotfig 提供了将图片序列整合生成 GIF 动画的工具函数。"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"id": "1012f97a",
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"name": "stderr",
126+
"output_type": "stream",
127+
"text": [
128+
"\u001b[32m2025-11-24 11:07:46.885\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36mplotfig.brain_connection\u001b[0m:\u001b[36mcreate_gif_from_images\u001b[0m:\u001b[36m417\u001b[0m - \u001b[1mGIF 已保存到: example_data\\brain_connection_figures\\outpug.gif\u001b[0m\n"
129+
]
130+
}
131+
],
132+
"source": [
133+
"from pathlib import Path\n",
134+
"from plotfig import create_gif_from_images\n",
135+
"\n",
136+
"create_gif_from_images(\n",
137+
" Path(\"example_data/brain_connection_figures\"),\n",
138+
" output_name=\"output.gif\"\n",
139+
")\n"
83140
]
84141
}
85142
],
@@ -90,7 +147,15 @@
90147
"name": "python3"
91148
},
92149
"language_info": {
150+
"codemirror_mode": {
151+
"name": "ipython",
152+
"version": 3
153+
},
154+
"file_extension": ".py",
155+
"mimetype": "text/x-python",
93156
"name": "python",
157+
"nbconvert_exporter": "python",
158+
"pygments_lexer": "ipython3",
94159
"version": "3.11.11"
95160
}
96161
},

0 commit comments

Comments
 (0)