SURE-tools 2.1.10__py3-none-any.whl → 2.1.11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of SURE-tools might be problematic. Click here for more details.
- SURE/flow/plot_quiver.py +112 -1
- {sure_tools-2.1.10.dist-info → sure_tools-2.1.11.dist-info}/METADATA +1 -1
- {sure_tools-2.1.10.dist-info → sure_tools-2.1.11.dist-info}/RECORD +7 -7
- {sure_tools-2.1.10.dist-info → sure_tools-2.1.11.dist-info}/WHEEL +0 -0
- {sure_tools-2.1.10.dist-info → sure_tools-2.1.11.dist-info}/entry_points.txt +0 -0
- {sure_tools-2.1.10.dist-info → sure_tools-2.1.11.dist-info}/licenses/LICENSE +0 -0
- {sure_tools-2.1.10.dist-info → sure_tools-2.1.11.dist-info}/top_level.txt +0 -0
SURE/flow/plot_quiver.py
CHANGED
|
@@ -2,8 +2,119 @@ import numpy as np
|
|
|
2
2
|
import matplotlib.pyplot as plt
|
|
3
3
|
from sklearn.decomposition import PCA
|
|
4
4
|
import scanpy as sc
|
|
5
|
+
from matplotlib.colors import ListedColormap
|
|
5
6
|
|
|
6
|
-
def plot_quiver(z_points, delta_z, method='
|
|
7
|
+
def plot_quiver(z_points, delta_z, method='pca', figsize=(6,4), dpi=200,
|
|
8
|
+
subsample=None, color_by=None, colormap='viridis',
|
|
9
|
+
arrow_scale=1.0, arrow_width=0.005, alpha=0.8):
|
|
10
|
+
"""
|
|
11
|
+
优化后的quiver可视化函数
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
z_points: 原始潜在空间点 [n_samples, n_dims]
|
|
15
|
+
delta_z: 移动向量 [n_samples, n_dims]
|
|
16
|
+
method: 降维方法 ('pca', 'variance', 'manual', 'umap')
|
|
17
|
+
figsize: 图像大小
|
|
18
|
+
dpi: 分辨率
|
|
19
|
+
subsample: 随机采样的数据点数量 (None表示不采样)
|
|
20
|
+
color_by: 颜色标签数组 [n_samples]
|
|
21
|
+
colormap: 颜色映射名称或ListedColormap对象
|
|
22
|
+
arrow_scale: 箭头缩放因子
|
|
23
|
+
arrow_width: 箭头宽度
|
|
24
|
+
alpha: 透明度
|
|
25
|
+
"""
|
|
26
|
+
# 数据采样
|
|
27
|
+
if subsample is not None and len(z_points) > subsample:
|
|
28
|
+
idx = np.random.choice(len(z_points), subsample, replace=False)
|
|
29
|
+
z_points = z_points[idx]
|
|
30
|
+
delta_z = delta_z[idx]
|
|
31
|
+
if color_by is not None:
|
|
32
|
+
color_by = color_by[idx]
|
|
33
|
+
|
|
34
|
+
# 降维投影
|
|
35
|
+
if method == 'variance':
|
|
36
|
+
variances = np.var(z_points, axis=0)
|
|
37
|
+
dims = np.argsort(variances)[-2:]
|
|
38
|
+
z_2d = z_points[:, dims]
|
|
39
|
+
delta_z_2d = delta_z[:, dims]
|
|
40
|
+
dim_names = [f'z[{d}]' for d in dims]
|
|
41
|
+
|
|
42
|
+
elif method == 'pca':
|
|
43
|
+
pca = PCA(n_components=2)
|
|
44
|
+
z_2d = pca.fit_transform(z_points)
|
|
45
|
+
delta_z_2d = pca.transform(z_points + delta_z) - z_2d
|
|
46
|
+
dim_names = ['PC1', 'PC2']
|
|
47
|
+
|
|
48
|
+
elif method == 'manual':
|
|
49
|
+
dims = [0, 1]
|
|
50
|
+
z_2d = z_points[:, dims]
|
|
51
|
+
delta_z_2d = delta_z[:, dims]
|
|
52
|
+
dim_names = [f'z[{d}]' for d in dims]
|
|
53
|
+
|
|
54
|
+
elif method == 'umap':
|
|
55
|
+
ad = sc.AnnData(np.vstack([z_points, z_points+delta_z]))
|
|
56
|
+
sc.pp.neighbors(ad)
|
|
57
|
+
sc.tl.umap(ad)
|
|
58
|
+
z_2d = ad[:z_points.shape[0]].obsm['X_umap']
|
|
59
|
+
delta_z_2d = ad[z_points.shape[0]:].obsm['X_umap'] - z_2d
|
|
60
|
+
dim_names = ['UMAP1', 'UMAP2']
|
|
61
|
+
|
|
62
|
+
# 颜色处理
|
|
63
|
+
if color_by is not None:
|
|
64
|
+
if isinstance(colormap, str):
|
|
65
|
+
cmap = plt.get_cmap(colormap)
|
|
66
|
+
else:
|
|
67
|
+
cmap = colormap
|
|
68
|
+
|
|
69
|
+
if color_by.dtype.kind in ['i', 'f']: # 数值型标签
|
|
70
|
+
colors = cmap(color_by / max(color_by.max(), 1e-8))
|
|
71
|
+
cbar_label = 'Numeric Label'
|
|
72
|
+
else: # 类别型标签
|
|
73
|
+
unique_labels = np.unique(color_by)
|
|
74
|
+
color_map = {label: cmap(i/len(unique_labels))
|
|
75
|
+
for i, label in enumerate(unique_labels)}
|
|
76
|
+
colors = [color_map[label] for label in color_by]
|
|
77
|
+
cbar_label = 'Class Label'
|
|
78
|
+
else:
|
|
79
|
+
colors = 'blue'
|
|
80
|
+
|
|
81
|
+
# 绘制
|
|
82
|
+
plt.figure(figsize=figsize, dpi=dpi)
|
|
83
|
+
|
|
84
|
+
# 绘制quiver(分颜色组绘制以获得正确图例)
|
|
85
|
+
if color_by is not None and isinstance(color_by[0], str):
|
|
86
|
+
for label in np.unique(color_by):
|
|
87
|
+
mask = color_by == label
|
|
88
|
+
plt.quiver(z_2d[mask, 0], z_2d[mask, 1],
|
|
89
|
+
delta_z_2d[mask, 0], delta_z_2d[mask, 1],
|
|
90
|
+
angles='xy', scale_units='xy', scale=1.0/arrow_scale,
|
|
91
|
+
color=colors[mask], width=arrow_width, alpha=alpha,
|
|
92
|
+
label=str(label))
|
|
93
|
+
plt.legend()
|
|
94
|
+
else:
|
|
95
|
+
q = plt.quiver(z_2d[:, 0], z_2d[:, 1],
|
|
96
|
+
delta_z_2d[:, 0], delta_z_2d[:, 1],
|
|
97
|
+
angles='xy', scale_units='xy', scale=1.0/arrow_scale,
|
|
98
|
+
color=colors, width=arrow_width, alpha=alpha)
|
|
99
|
+
|
|
100
|
+
# 添加颜色条(数值型标签)
|
|
101
|
+
if color_by is not None and color_by.dtype.kind in ['i', 'f']:
|
|
102
|
+
plt.colorbar(plt.cm.ScalarMappable(
|
|
103
|
+
norm=plt.Normalize(color_by.min(), color_by.max()),
|
|
104
|
+
cmap=cmap), label=cbar_label)
|
|
105
|
+
|
|
106
|
+
# 美化图形
|
|
107
|
+
plt.scatter(z_2d[:, 0], z_2d[:, 1], c='gray', alpha=0.3, s=5)
|
|
108
|
+
plt.xlabel(dim_names[0])
|
|
109
|
+
plt.ylabel(dim_names[1])
|
|
110
|
+
plt.title(f"Latent Space Movement ({method} projection)")
|
|
111
|
+
plt.grid(alpha=0.2)
|
|
112
|
+
plt.tight_layout()
|
|
113
|
+
plt.show()
|
|
114
|
+
|
|
115
|
+
return z_2d, delta_z_2d
|
|
116
|
+
|
|
117
|
+
def plot_quiver_old(z_points, delta_z, method='pca', figsize=(6,4), dpi=200):
|
|
7
118
|
"""
|
|
8
119
|
从高维潜在空间选择2个维度进行quiver可视化
|
|
9
120
|
"""
|
|
@@ -11,7 +11,7 @@ SURE/codebook/__init__.py,sha256=2T5gjp8JIaBayrXAnOJYSebQHsWprOs87difpR1OPNw,243
|
|
|
11
11
|
SURE/codebook/codebook.py,sha256=ZlN6gRX9Gj2D2u3P5KeOsbZri0MoMAiJo9lNeL-MK-I,17117
|
|
12
12
|
SURE/flow/__init__.py,sha256=rsAjYsh1xVIrxBCuwOE0Q_6N5th1wBgjJceV0ABPG3c,183
|
|
13
13
|
SURE/flow/flow_stats.py,sha256=3F3waCuEbIQ7bsiGga4cUvJphYdWA307SyGwEh8EzM8,10514
|
|
14
|
-
SURE/flow/plot_quiver.py,sha256=
|
|
14
|
+
SURE/flow/plot_quiver.py,sha256=FyFc4lSRuY2bMmPMQtPsfwAJ7xhw7XD_e_OCVWa4ZeM,6083
|
|
15
15
|
SURE/flow/quiver.py,sha256=_euFqSaRrDoZ_oOabOx20LOoUTJ__XPhLW-vzLNQfAo,1859
|
|
16
16
|
SURE/perturb/__init__.py,sha256=ouxShhbxZM4r5Gf7GmKiutrsmtyq7QL8rHjhgF0BU08,32
|
|
17
17
|
SURE/perturb/perturb.py,sha256=CqO3xPfNA3cG175tadDidKvGsTu_yKfJRRLn_93awKM,3303
|
|
@@ -19,9 +19,9 @@ SURE/utils/__init__.py,sha256=Htqv4KqVKcRiaaTBsR-6yZ4LSlbhbzutjNKXGD9-uds,660
|
|
|
19
19
|
SURE/utils/custom_mlp.py,sha256=07TYX1HgxfEjb_3i5MpiZfNhOhx3dKntuwGkrpteWiM,7036
|
|
20
20
|
SURE/utils/queue.py,sha256=E_5PA5EWcBoGAZj8BkKQnkCK0p4C-4-xcTPqdIXaPXU,1892
|
|
21
21
|
SURE/utils/utils.py,sha256=IUHjDDtYaAYllCWsZyIzqQwaLul6fJRvHRH4vIYcR-c,8462
|
|
22
|
-
sure_tools-2.1.
|
|
23
|
-
sure_tools-2.1.
|
|
24
|
-
sure_tools-2.1.
|
|
25
|
-
sure_tools-2.1.
|
|
26
|
-
sure_tools-2.1.
|
|
27
|
-
sure_tools-2.1.
|
|
22
|
+
sure_tools-2.1.11.dist-info/licenses/LICENSE,sha256=TFHKwmrAViXQbSX5W-NDItkWFjm45HWOeUniDrqmnu0,1065
|
|
23
|
+
sure_tools-2.1.11.dist-info/METADATA,sha256=aUC_xD4vGERur0cFsccW59T1ufKKN7mbSDMHMJXHBko,2651
|
|
24
|
+
sure_tools-2.1.11.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
25
|
+
sure_tools-2.1.11.dist-info/entry_points.txt,sha256=-nJI8rVe_qqrR0HmfAODzj-JNfEqCcSsyVh6okSqyHk,83
|
|
26
|
+
sure_tools-2.1.11.dist-info/top_level.txt,sha256=BtFTebdiJeqra4r6mm-uEtwVRFLZ_IjYsQ7OnalrOvY,5
|
|
27
|
+
sure_tools-2.1.11.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|