SURE-tools 2.1.9__tar.gz → 2.1.11__tar.gz
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_tools-2.1.9 → sure_tools-2.1.11}/PKG-INFO +1 -1
- sure_tools-2.1.11/SURE/flow/plot_quiver.py +163 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/perturb/perturb.py +2 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE_tools.egg-info/PKG-INFO +1 -1
- {sure_tools-2.1.9 → sure_tools-2.1.11}/setup.py +1 -1
- sure_tools-2.1.9/SURE/flow/plot_quiver.py +0 -52
- {sure_tools-2.1.9 → sure_tools-2.1.11}/LICENSE +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/README.md +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/PerturbFlow.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/SURE.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/__init__.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/assembly/__init__.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/assembly/assembly.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/assembly/atlas.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/atac/__init__.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/atac/utils.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/codebook/__init__.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/codebook/codebook.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/flow/__init__.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/flow/flow_stats.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/perturb/__init__.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/utils/__init__.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/utils/custom_mlp.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/utils/queue.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE/utils/utils.py +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE_tools.egg-info/SOURCES.txt +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE_tools.egg-info/dependency_links.txt +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE_tools.egg-info/entry_points.txt +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE_tools.egg-info/requires.txt +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/SURE_tools.egg-info/top_level.txt +0 -0
- {sure_tools-2.1.9 → sure_tools-2.1.11}/setup.cfg +0 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
from sklearn.decomposition import PCA
|
|
4
|
+
import scanpy as sc
|
|
5
|
+
from matplotlib.colors import ListedColormap
|
|
6
|
+
|
|
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):
|
|
118
|
+
"""
|
|
119
|
+
从高维潜在空间选择2个维度进行quiver可视化
|
|
120
|
+
"""
|
|
121
|
+
if method == 'variance':
|
|
122
|
+
# 方法1: 选择方差最大的2个维度
|
|
123
|
+
variances = np.var(z_points, axis=0)
|
|
124
|
+
dims = np.argsort(variances)[-2:] # 选择方差最大的两个维度
|
|
125
|
+
dim_names = [f'z[{d}]' for d in dims]
|
|
126
|
+
|
|
127
|
+
elif method == 'pca':
|
|
128
|
+
# 方法2: 使用PCA的前两个主成分
|
|
129
|
+
pca = PCA(n_components=2)
|
|
130
|
+
z_2d = pca.fit_transform(z_points)
|
|
131
|
+
delta_z_2d = pca.transform(z_points + delta_z) - z_2d
|
|
132
|
+
dim_names = ['PC1', 'PC2']
|
|
133
|
+
|
|
134
|
+
elif method == 'manual':
|
|
135
|
+
# 方法3: 手动选择感兴趣的维度
|
|
136
|
+
dims = [0, 1] # 选择前两个维度
|
|
137
|
+
z_2d = z_points[:, dims]
|
|
138
|
+
delta_z_2d = delta_z[:, dims]
|
|
139
|
+
dim_names = [f'z[{d}]' for d in dims]
|
|
140
|
+
|
|
141
|
+
elif method == 'umap':
|
|
142
|
+
ad = sc.AnnData(np.vstack([z_points, z_points+delta_z]))
|
|
143
|
+
sc.pp.neighbors(ad)
|
|
144
|
+
sc.tl.umap(ad)
|
|
145
|
+
z_2d = ad[:z_points.shape[0]].obsm['X_umap']
|
|
146
|
+
delta_z_2d = ad[z_points.shape[0]:].obsm['X_umap'] - z_2d
|
|
147
|
+
dim_names = ['UMAP1', 'UMAP2']
|
|
148
|
+
|
|
149
|
+
# 绘制quiver图
|
|
150
|
+
plt.figure(figsize=figsize, dpi=dpi)
|
|
151
|
+
plt.quiver(z_2d[:, 0], z_2d[:, 1],
|
|
152
|
+
delta_z_2d[:, 0], delta_z_2d[:, 1],
|
|
153
|
+
angles='xy', scale_units='xy', scale=1,
|
|
154
|
+
color='blue', alpha=0.6, width=0.005)
|
|
155
|
+
|
|
156
|
+
plt.scatter(z_2d[:, 0], z_2d[:, 1], c='gray', alpha=0.5, s=10)
|
|
157
|
+
plt.xlabel(dim_names[0])
|
|
158
|
+
plt.ylabel(dim_names[1])
|
|
159
|
+
plt.title(f"Latent Space Movement ({method} projection)")
|
|
160
|
+
plt.grid(alpha=0.3)
|
|
161
|
+
plt.show()
|
|
162
|
+
|
|
163
|
+
return z_2d, delta_z_2d
|
|
@@ -15,6 +15,8 @@ class LabelMatrix:
|
|
|
15
15
|
mat, self.labels_ = vectorized_label_to_matrix(labels=labels, sep_pattern=sep_pattern)
|
|
16
16
|
elif speedup=='parallel':
|
|
17
17
|
mat, self.labels_ = parallel_label_to_matrix(labels=labels, sep_pattern=sep_pattern)
|
|
18
|
+
|
|
19
|
+
self.labels_ = np.array(self.labels_)
|
|
18
20
|
return mat
|
|
19
21
|
|
|
20
22
|
def inverse_transform(self, matrix):
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
import matplotlib.pyplot as plt
|
|
3
|
-
from sklearn.decomposition import PCA
|
|
4
|
-
import scanpy as sc
|
|
5
|
-
|
|
6
|
-
def plot_quiver(z_points, delta_z, method='umap', figsize=(6,4), dpi=200):
|
|
7
|
-
"""
|
|
8
|
-
从高维潜在空间选择2个维度进行quiver可视化
|
|
9
|
-
"""
|
|
10
|
-
if method == 'variance':
|
|
11
|
-
# 方法1: 选择方差最大的2个维度
|
|
12
|
-
variances = np.var(z_points, axis=0)
|
|
13
|
-
dims = np.argsort(variances)[-2:] # 选择方差最大的两个维度
|
|
14
|
-
dim_names = [f'z[{d}]' for d in dims]
|
|
15
|
-
|
|
16
|
-
elif method == 'pca':
|
|
17
|
-
# 方法2: 使用PCA的前两个主成分
|
|
18
|
-
pca = PCA(n_components=2)
|
|
19
|
-
z_2d = pca.fit_transform(z_points)
|
|
20
|
-
delta_z_2d = pca.transform(z_points + delta_z) - z_2d
|
|
21
|
-
dim_names = ['PC1', 'PC2']
|
|
22
|
-
|
|
23
|
-
elif method == 'manual':
|
|
24
|
-
# 方法3: 手动选择感兴趣的维度
|
|
25
|
-
dims = [0, 1] # 选择前两个维度
|
|
26
|
-
z_2d = z_points[:, dims]
|
|
27
|
-
delta_z_2d = delta_z[:, dims]
|
|
28
|
-
dim_names = [f'z[{d}]' for d in dims]
|
|
29
|
-
|
|
30
|
-
elif method == 'umap':
|
|
31
|
-
ad = sc.AnnData(np.vstack([z_points, z_points+delta_z]))
|
|
32
|
-
sc.pp.neighbors(ad)
|
|
33
|
-
sc.tl.umap(ad)
|
|
34
|
-
z_2d = ad[:z_points.shape[0]].obsm['X_umap']
|
|
35
|
-
delta_z_2d = ad[z_points.shape[0]:].obsm['X_umap'] - z_2d
|
|
36
|
-
dim_names = ['UMAP1', 'UMAP2']
|
|
37
|
-
|
|
38
|
-
# 绘制quiver图
|
|
39
|
-
plt.figure(figsize=figsize, dpi=dpi)
|
|
40
|
-
plt.quiver(z_2d[:, 0], z_2d[:, 1],
|
|
41
|
-
delta_z_2d[:, 0], delta_z_2d[:, 1],
|
|
42
|
-
angles='xy', scale_units='xy', scale=1,
|
|
43
|
-
color='blue', alpha=0.6, width=0.005)
|
|
44
|
-
|
|
45
|
-
plt.scatter(z_2d[:, 0], z_2d[:, 1], c='gray', alpha=0.5, s=10)
|
|
46
|
-
plt.xlabel(dim_names[0])
|
|
47
|
-
plt.ylabel(dim_names[1])
|
|
48
|
-
plt.title(f"Latent Space Movement ({method} projection)")
|
|
49
|
-
plt.grid(alpha=0.3)
|
|
50
|
-
plt.show()
|
|
51
|
-
|
|
52
|
-
return z_2d, delta_z_2d
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|