SURE-tools 2.1.10__py3-none-any.whl → 2.1.12__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.
SURE/flow/plot_quiver.py CHANGED
@@ -2,8 +2,153 @@ 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='umap', figsize=(6,4), dpi=200):
7
+
8
+ def plot_quiver(z_points, delta_z, method='pca', figsize=(6,4), dpi=200,
9
+ subsample=None, color_by=None, colormap='viridis',
10
+ arrow_scale=1.0, arrow_width=0.005, alpha=0.8):
11
+ """
12
+ 优化后的quiver可视化函数
13
+
14
+ Args:
15
+ z_points: 原始潜在空间点 [n_samples, n_dims]
16
+ delta_z: 移动向量 [n_samples, n_dims]
17
+ method: 降维方法 ('pca', 'variance', 'manual', 'umap')
18
+ figsize: 图像大小
19
+ dpi: 分辨率
20
+ subsample: 随机采样的数据点数量 (None表示不采样)
21
+ color_by: 颜色标签数组 [n_samples]
22
+ colormap: 颜色映射名称或ListedColormap对象
23
+ arrow_scale: 箭头缩放因子
24
+ arrow_width: 箭头宽度
25
+ alpha: 透明度
26
+ """
27
+ # 保存原始数据用于散点图
28
+ original_z_points = z_points.copy()
29
+ original_delta_z = delta_z.copy()
30
+ original_color_by = color_by.copy() if color_by is not None else None
31
+
32
+ # 数据采样(仅用于quiver)
33
+ quiver_indices = None
34
+ if subsample is not None and len(z_points) > subsample:
35
+ quiver_indices = np.random.choice(len(z_points), subsample, replace=False)
36
+ z_points_quiver = z_points[quiver_indices]
37
+ delta_z_quiver = delta_z[quiver_indices]
38
+ if color_by is not None:
39
+ color_by_quiver = color_by[quiver_indices]
40
+ else:
41
+ color_by_quiver = None
42
+ else:
43
+ z_points_quiver = z_points
44
+ delta_z_quiver = delta_z
45
+ color_by_quiver = color_by
46
+
47
+ # 降维投影(使用所有数据)
48
+ if method == 'variance':
49
+ variances = np.var(original_z_points, axis=0)
50
+ dims = np.argsort(variances)[-2:]
51
+ z_2d_all = original_z_points[:, dims]
52
+ z_2d_quiver = z_points_quiver[:, dims]
53
+ delta_z_2d = delta_z_quiver[:, dims]
54
+ dim_names = [f'z[{d}]' for d in dims]
55
+
56
+ elif method == 'pca':
57
+ pca = PCA(n_components=2)
58
+ z_2d_all = pca.fit_transform(original_z_points)
59
+ z_2d_quiver = pca.transform(z_points_quiver)
60
+ delta_z_2d = pca.transform(z_points_quiver + delta_z_quiver) - z_2d_quiver
61
+ dim_names = ['PC1', 'PC2']
62
+
63
+ elif method == 'manual':
64
+ dims = [0, 1]
65
+ z_2d_all = original_z_points[:, dims]
66
+ z_2d_quiver = z_points_quiver[:, dims]
67
+ delta_z_2d = delta_z_quiver[:, dims]
68
+ dim_names = [f'z[{d}]' for d in dims]
69
+
70
+ elif method == 'umap':
71
+ # 使用所有数据进行UMAP降维
72
+ ad = sc.AnnData(np.vstack([original_z_points, original_z_points+original_delta_z]))
73
+ sc.pp.neighbors(ad)
74
+ sc.tl.umap(ad)
75
+ z_2d_all = ad[:len(original_z_points)].obsm['X_umap']
76
+ z_2d_quiver = z_2d_all[quiver_indices] if quiver_indices is not None else z_2d_all
77
+ delta_z_2d = ad[len(original_z_points):].obsm['X_umap'][quiver_indices] - z_2d_quiver if quiver_indices is not None else ad[len(original_z_points):].obsm['X_umap'] - z_2d_all
78
+ dim_names = ['UMAP1', 'UMAP2']
79
+
80
+ # 颜色处理(散点图使用所有数据,quiver使用采样数据)
81
+ if original_color_by is not None:
82
+ if isinstance(colormap, str):
83
+ cmap = plt.get_cmap(colormap)
84
+ else:
85
+ cmap = colormap
86
+
87
+ if original_color_by.dtype.kind in ['i', 'f']: # 数值型标签
88
+ # 所有数据的颜色
89
+ colors_all = cmap(original_color_by / max(original_color_by.max(), 1e-8))
90
+ # quiver数据的颜色
91
+ if color_by_quiver is not None:
92
+ colors_quiver = cmap(color_by_quiver / max(color_by_quiver.max(), 1e-8))
93
+ else:
94
+ colors_quiver = 'blue'
95
+ cbar_label = 'Numeric Label'
96
+ else: # 类别型标签
97
+ unique_labels = np.unique(original_color_by)
98
+ color_map = {label: cmap(i/len(unique_labels))
99
+ for i, label in enumerate(unique_labels)}
100
+ colors_all = [color_map[label] for label in original_color_by]
101
+ if color_by_quiver is not None:
102
+ colors_quiver = [color_map[label] for label in color_by_quiver]
103
+ else:
104
+ colors_quiver = 'blue'
105
+ cbar_label = 'Class Label'
106
+ else:
107
+ colors_all = 'gray'
108
+ colors_quiver = 'blue'
109
+
110
+ # 绘制
111
+ plt.figure(figsize=figsize, dpi=dpi)
112
+
113
+ # 1. 首先绘制所有数据点的散点图
114
+ plt.scatter(z_2d_all[:, 0], z_2d_all[:, 1],
115
+ c=colors_all, alpha=0.3, s=5, label='All data points')
116
+
117
+ # 2. 绘制quiver(仅采样数据)
118
+ if color_by_quiver is not None and isinstance(color_by_quiver[0], str):
119
+ for label in np.unique(color_by_quiver):
120
+ mask = color_by_quiver == label
121
+ plt.quiver(z_2d_quiver[mask, 0], z_2d_quiver[mask, 1],
122
+ delta_z_2d[mask, 0], delta_z_2d[mask, 1],
123
+ angles='xy', scale_units='xy', scale=1.0/arrow_scale,
124
+ color=colors_quiver[mask], width=arrow_width, alpha=alpha,
125
+ label=f'{label} (movement)')
126
+ plt.legend()
127
+ else:
128
+ q = plt.quiver(z_2d_quiver[:, 0], z_2d_quiver[:, 1],
129
+ delta_z_2d[:, 0], delta_z_2d[:, 1],
130
+ angles='xy', scale_units='xy', scale=1.0/arrow_scale,
131
+ color=colors_quiver, width=arrow_width, alpha=alpha,
132
+ label='Movement vectors')
133
+
134
+ # 添加颜色条(数值型标签)
135
+ if original_color_by is not None and original_color_by.dtype.kind in ['i', 'f']:
136
+ plt.colorbar(plt.cm.ScalarMappable(
137
+ norm=plt.Normalize(original_color_by.min(), original_color_by.max()),
138
+ cmap=cmap), label=cbar_label)
139
+
140
+ # 美化图形
141
+ plt.xlabel(dim_names[0])
142
+ plt.ylabel(dim_names[1])
143
+ plt.title(f"Latent Space Movement ({method} projection)\n{len(z_points_quiver)}/{len(original_z_points)} points with vectors")
144
+ plt.grid(alpha=0.2)
145
+ plt.legend()
146
+ plt.tight_layout()
147
+ plt.show()
148
+
149
+ return z_2d_all, z_2d_quiver, delta_z_2d
150
+
151
+ def plot_quiver_old(z_points, delta_z, method='pca', figsize=(6,4), dpi=200):
7
152
  """
8
153
  从高维潜在空间选择2个维度进行quiver可视化
9
154
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SURE-tools
3
- Version: 2.1.10
3
+ Version: 2.1.12
4
4
  Summary: Succinct Representation of Single Cells
5
5
  Home-page: https://github.com/ZengFLab/SURE
6
6
  Author: Feng Zeng
@@ -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=spcPC0BDpvC-FBk9XIu6rcJI64bS-U-IVmmKLYHHXgs,1865
14
+ SURE/flow/plot_quiver.py,sha256=MoJPL5B6T6Xb7X1I4BB1f8eJxzlE8m7wmgBmbe7hpI0,8052
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.10.dist-info/licenses/LICENSE,sha256=TFHKwmrAViXQbSX5W-NDItkWFjm45HWOeUniDrqmnu0,1065
23
- sure_tools-2.1.10.dist-info/METADATA,sha256=2KG05qwCqT3IfABXzhHK2_h0mjgUmVpOYw1mwy078MU,2651
24
- sure_tools-2.1.10.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
25
- sure_tools-2.1.10.dist-info/entry_points.txt,sha256=-nJI8rVe_qqrR0HmfAODzj-JNfEqCcSsyVh6okSqyHk,83
26
- sure_tools-2.1.10.dist-info/top_level.txt,sha256=BtFTebdiJeqra4r6mm-uEtwVRFLZ_IjYsQ7OnalrOvY,5
27
- sure_tools-2.1.10.dist-info/RECORD,,
22
+ sure_tools-2.1.12.dist-info/licenses/LICENSE,sha256=TFHKwmrAViXQbSX5W-NDItkWFjm45HWOeUniDrqmnu0,1065
23
+ sure_tools-2.1.12.dist-info/METADATA,sha256=SgGEGxa3CqqsnLa9nKQKlL0o9WG2Q8jVHUbKFr3JKUM,2651
24
+ sure_tools-2.1.12.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
25
+ sure_tools-2.1.12.dist-info/entry_points.txt,sha256=-nJI8rVe_qqrR0HmfAODzj-JNfEqCcSsyVh6okSqyHk,83
26
+ sure_tools-2.1.12.dist-info/top_level.txt,sha256=BtFTebdiJeqra4r6mm-uEtwVRFLZ_IjYsQ7OnalrOvY,5
27
+ sure_tools-2.1.12.dist-info/RECORD,,