Skip to content

Commit d3f50ed

Browse files
authored
Merge pull request #48 from RicardoRyn/dev
Dev
2 parents bc36625 + 5a5290e commit d3f50ed

16 files changed

+957
-90
lines changed

IFLOW.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# IFLOW.md - plotfig项目指南
2+
3+
## 项目概述
4+
5+
`plotfig` 是一个专为认知神经科学设计的Python数据可视化库,基于`matplotlib``surfplot``plotly`等主流可视化库开发。该项目旨在为科研工作者提供高效、易用且美观的图形绘制工具,特别适用于神经科学和脑连接组学领域的复杂绘图需求。
6+
7+
项目特点:
8+
- 模块化设计,功能丰富
9+
- 支持多种科学绘图类型
10+
- 专门针对神经科学数据可视化需求优化
11+
12+
## 项目结构
13+
14+
```
15+
plotfig/
16+
├── src/plotfig/ # 核心源码目录
17+
│ ├── __init__.py # 导出所有公共API
18+
│ ├── bar.py # 条形图绘制
19+
│ ├── brain_connection.py # 大脑连接可视化
20+
│ ├── brain_surface.py # 脑表面可视化
21+
│ ├── circos.py # 弦图可视化
22+
│ ├── correlation.py # 相关性矩阵可视化
23+
│ ├── matrix.py # 通用矩阵可视化
24+
│ ├── utils/ # 工具函数模块
25+
│ └── data/ # 数据文件
26+
├── tests/ # 测试文件
27+
├── notebooks/ # Jupyter笔记本示例
28+
├── docs/ # 文档文件
29+
├── pyproject.toml # 项目配置文件
30+
└── README.md # 项目说明文档
31+
```
32+
33+
## 核心功能模块
34+
35+
### 1. brain_connection.py
36+
提供大脑连接图的3D可视化功能,支持:
37+
- 基于NIfTI格式图集的ROI节点定位
38+
- 使用Plotly创建交互式3D大脑连接图
39+
- 支持根据连接强度调整线条宽度和颜色
40+
- 可生成旋转动画帧并制作GIF
41+
42+
主要函数:
43+
- `plot_brain_connection_figure`: 绘制大脑连接图
44+
- `save_brain_connection_frames`: 生成旋转动画帧
45+
- `batch_crop_images`: 批量裁剪图像
46+
- `create_gif_from_images`: 从图像生成GIF
47+
48+
### 2. correlation.py
49+
支持两个数据集之间的相关性分析和可视化:
50+
- 支持Spearman和Pearson相关性计算
51+
- 可绘制散点图、回归线和置信区间
52+
- 提供多种轴标签格式化选项
53+
54+
主要函数:
55+
- `plot_correlation_figure`: 绘制相关性图
56+
57+
### 3. 其他模块
58+
- `bar.py`: 条形图和小提琴图绘制
59+
- `matrix.py`: 通用矩阵可视化
60+
- `brain_surface.py`: 脑表面可视化
61+
- `circos.py`: 弦图可视化
62+
63+
## 依赖管理
64+
65+
项目要求Python 3.11+版本,关键依赖包括:
66+
- `matplotlib`: 基础绘图库
67+
- `plotly`: 交互式3D可视化
68+
- `surfplot`: 脑表面绘制(需从GitHub安装最新版)
69+
- `numpy`, `scipy`: 数值计算
70+
- `nibabel`: 神经影像数据处理
71+
- `imageio`: 图像处理
72+
73+
## API使用示例
74+
75+
### 大脑连接可视化
76+
```python
77+
from plotfig import plot_brain_connection_figure
78+
79+
fig = plot_brain_connection_figure(
80+
connectome=connectome_matrix,
81+
lh_surfgii_file="lh_surface.gii",
82+
rh_surfgii_file="rh_surface.gii",
83+
niigz_file="atlas.nii.gz",
84+
scale_method="width_color",
85+
line_width=10
86+
)
87+
```
88+
89+
### 相关性分析
90+
```python
91+
from plotfig import plot_correlation_figure
92+
93+
plot_correlation_figure(
94+
data1=[1, 2, 3, 4, 5],
95+
data2=[2, 4, 6, 8, 10],
96+
stats_method="pearson",
97+
ci=True,
98+
title_name="Correlation Plot"
99+
)
100+
```
101+
102+
## 开发和测试
103+
104+
项目支持开发模式安装:
105+
```bash
106+
pip install -e .
107+
```
108+
109+
测试文件位于`tests/`目录,示例代码在`notebooks/`目录。
110+
111+
## 构建和安装
112+
113+
项目使用Hatchling作为构建后端,通过`pyproject.toml`管理依赖和构建配置。
114+
115+
安装命令:
116+
- PyPI安装:`pip install plotfig`
117+
- 源码安装:`pip install .`
118+
- 开发安装:`pip install -e .`
119+
120+
## 文档和资源
121+
122+
- 在线文档:https://ricardoryn.github.io/plotfig/
123+
- 源代码:https://github.com/RicardoRyn/plotfig
124+
- 问题反馈:https://github.com/RicardoRyn/plotfig/issues

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ authors = [
99
{ name = "Ricardo Ryn", email = "ricardoRyn1317@gmail.com" }
1010
]
1111
dependencies = [
12+
"imageio>=2.37.2",
1213
"kaleido>=1.0.0",
1314
"loguru>=0.7.3",
1415
"matplotlib>=3.10.6",

src/plotfig/bar.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,13 @@ def plot_one_group_bar_figure(
383383
statistic (bool, optional):
384384
是否进行统计显著性分析. Defaults to False.
385385
test_method (list[str], optional):
386-
统计检验方法列表. Defaults to ["ttest_ind"].
386+
统计检验方法列表,包括
387+
1. `ttest_ind`,
388+
2. `ttest_rel`,
389+
3. `ttest_1samp`,
390+
4. `mannwhitneyu`,
391+
5. `external`.
392+
Defaults to ["ttest_ind"].
387393
p_list (list[float] | None, optional):
388394
预计算的p值列表,用于显著性标记. Defaults to None.
389395
popmean (Num, optional):

src/plotfig/brain_surface.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def plot_brain_surface_figure(
5050
ax: Axes | None = None,
5151
vmin: Num | None = None,
5252
vmax: Num | None = None,
53-
cmap: str = "Reds",
53+
cmap: str = "viridis",
5454
colorbar: bool = True,
5555
colorbar_location: str = "right",
5656
colorbar_label_name: str = "",
@@ -75,7 +75,7 @@ def plot_brain_surface_figure(
7575
ax (Axes | None, optional): matplotlib的坐标轴对象,如果为None则使用当前坐标轴. Defaults to None.
7676
vmin (Num | None, optional): 颜色映射的最小值,None表示使用数据中的最小值. Defaults to None.
7777
vmax (Num | None, optional): 颜色映射的最大值,None表示使用数据中的最大值. Defaults to None.
78-
cmap (str, optional): 颜色映射方案,如"Reds"、"Blues"、"viridis"等. Defaults to "Reds".
78+
cmap (str, optional): 颜色映射方案,如"viridis"、"Blues"、"Reds"等. Defaults to "viridis".
7979
colorbar (bool, optional): 是否显示颜色条. Defaults to True.
8080
colorbar_location (str, optional): 颜色条位置,可选"left"、"right"、"top"、"bottom". Defaults to "right".
8181
colorbar_label_name (str, optional): 颜色条标签名称. Defaults to "".
@@ -131,6 +131,10 @@ def plot_brain_surface_figure(
131131
"rh": "atlases/human_BNA/fsaverage.R.BNA.32k_fs_LR.label.gii",
132132
},
133133
},
134+
"sulc": {
135+
"lh": "surfaces/human_fsLR/100206.L.sulc.32k_fs_LR.shape.gii",
136+
"rh": "surfaces/human_fsLR/100206.R.sulc.32k_fs_LR.shape.gii",
137+
}
134138
},
135139
"chimpanzee": {
136140
"surf": {
@@ -166,7 +170,12 @@ def plot_brain_surface_figure(
166170
"lh": "atlases/macaque_D99/L.d99.label.gii",
167171
"rh": "atlases/macaque_D99/R.d99.label.gii",
168172
},
173+
},
174+
"sulc": {
175+
"lh": "surfaces/macaque_BNA/SC_06018.L.sulc.32k_fs_LR.shape.gii",
176+
"rh": "surfaces/macaque_BNA/SC_06018.R.sulc.32k_fs_LR.shape.gii",
169177
}
178+
170179
}
171180
}
172181

@@ -179,10 +188,29 @@ def plot_brain_surface_figure(
179188
raise ValueError(f"Unsupported {atlas} atlas for {species}")
180189

181190
# 创建Plot对象,用于绘制大脑皮层
182-
p = Plot(
183-
NEURODATA / atlas_info[species]["surf"]["lh"],
184-
NEURODATA / atlas_info[species]["surf"]["rh"],
185-
)
191+
if surf != "flat":
192+
p = Plot(
193+
NEURODATA / atlas_info[species]["surf"]["lh"],
194+
NEURODATA / atlas_info[species]["surf"]["rh"],
195+
)
196+
else:
197+
# NOTE: 目前只有人和猕猴具有flat surface,暂时不支持黑猩猩
198+
p = Plot(
199+
NEURODATA / atlas_info[species]["surf"]["lh"],
200+
NEURODATA / atlas_info[species]["surf"]["rh"],
201+
views="dorsal",
202+
zoom = 1.2,
203+
)
204+
lh_sulc_file = NEURODATA / atlas_info[species]["sulc"]["lh"]
205+
rh_sulc_file = NEURODATA / atlas_info[species]["sulc"]["rh"]
206+
p.add_layer(
207+
{
208+
"left": nib.load(lh_sulc_file).darrays[0].data,
209+
"right": nib.load(rh_sulc_file).darrays[0].data,
210+
},
211+
cmap="Grays_r",
212+
cbar=False
213+
)
186214

187215
# 分离左半球和右半球的数据
188216
hemisphere_data = {}

src/plotfig/data/neurodata/surfaces/human_fsLR/100206.L.sulc.32k_fs_LR.shape.gii

Lines changed: 87 additions & 0 deletions
Large diffs are not rendered by default.

src/plotfig/data/neurodata/surfaces/human_fsLR/100206.R.sulc.32k_fs_LR.shape.gii

Lines changed: 87 additions & 0 deletions
Large diffs are not rendered by default.

src/plotfig/data/neurodata/surfaces/human_fsLR/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ wb_command -metric-resample ${sulc164k} ${164k_sphere} ${Xk_sphere} ADAP_BARY_AR
99
```
1010

1111
Where `${Xk_sphere}` is the target sphere and `${164k_vaavg}` + `${Xk_vaavg}` are average vertex area maps.
12+
13+
基于HCP样本 `100206` 通过 HCP 处理流程生成的表面数据文件:
14+
15+
- `tpl-fsLR_den-32k_hemi-L_flat.surf.gii`
16+
- `tpl-fsLR_den-32k_hemi-R_flat.surf.gii`
17+
- `100206.L.sulc.32k_fs_LR.shape.gii`
18+
- `100206.R.sulc.32k_fs_LR.shape.gii`

src/plotfig/data/neurodata/surfaces/human_fsLR/tpl-fsLR_den-32k_hemi-L_flat.surf.gii

Lines changed: 81 additions & 0 deletions
Large diffs are not rendered by default.

src/plotfig/data/neurodata/surfaces/human_fsLR/tpl-fsLR_den-32k_hemi-R_flat.surf.gii

Lines changed: 89 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# macaque_BNA
2+
3+
基于实验室样本 `SC_06018` 通过 HCP_NHP 处理流程生成的表面数据文件:
4+
5+
- `civm.L.flat.32k_fs_LR.surf.gii`
6+
- `civm.R.flat.32k_fs_LR.surf.gii`
7+
- `SC_06018.L.sulc.32k_fs_LR.shape.gii`
8+
- `SC_06018.R.sulc.32k_fs_LR.shape.gii`
9+
10+
其他文件来自`BNA`数据。

0 commit comments

Comments
 (0)