|
28 | 28 | "metadata": {}, |
29 | 29 | "outputs": [], |
30 | 30 | "source": [ |
31 | | - "import numpy as np\n", |
32 | | - "from plotfig import *\n", |
| 31 | + "from plotfig import plot_brain_connection_figure, gen_symmetric_matrix\n", |
33 | 32 | "\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", |
43 | 33 | "\n", |
44 | | - "connectome = matrix\n", |
| 34 | + "# 生成随机的连接矩阵\n", |
| 35 | + "connectome = gen_symmetric_matrix(31, sparsity=0.1, seed=42)\n", |
45 | 36 | "\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", |
47 | 41 | "\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", |
51 | 44 | "\n", |
52 | 45 | "fig = plot_brain_connection_figure(\n", |
53 | 46 | " connectome,\n", |
|
57 | 50 | " output_file=output_file,\n", |
58 | 51 | " scale_method=\"width\",\n", |
59 | 52 | " 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 | + "" |
61 | 71 | ] |
62 | 72 | }, |
63 | 73 | { |
64 | 74 | "cell_type": "markdown", |
65 | 75 | "id": "3ffe6519", |
66 | 76 | "metadata": {}, |
67 | 77 | "source": [ |
68 | | - "html文件可以在浏览器中交互。可以手动截图,也可以使用以下命令来生成图片。" |
| 78 | + "html文件可以在浏览器中交互。可以手动截图,也可以使用以下命令来批量生成多视角生成图片。" |
69 | 79 | ] |
70 | 80 | }, |
71 | 81 | { |
72 | 82 | "cell_type": "code", |
73 | 83 | "execution_count": null, |
74 | 84 | "id": "f257283e", |
75 | 85 | "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 | + ], |
77 | 96 | "source": [ |
78 | 97 | "from pathlib import Path\n", |
| 98 | + "from plotfig import save_brain_connection_frames\n", |
79 | 99 | "\n", |
80 | 100 | "\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" |
83 | 140 | ] |
84 | 141 | } |
85 | 142 | ], |
|
90 | 147 | "name": "python3" |
91 | 148 | }, |
92 | 149 | "language_info": { |
| 150 | + "codemirror_mode": { |
| 151 | + "name": "ipython", |
| 152 | + "version": 3 |
| 153 | + }, |
| 154 | + "file_extension": ".py", |
| 155 | + "mimetype": "text/x-python", |
93 | 156 | "name": "python", |
| 157 | + "nbconvert_exporter": "python", |
| 158 | + "pygments_lexer": "ipython3", |
94 | 159 | "version": "3.11.11" |
95 | 160 | } |
96 | 161 | }, |
|
0 commit comments