SURE-tools 1.0.10__tar.gz → 2.0.2__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.

Potentially problematic release.


This version of SURE-tools might be problematic. Click here for more details.

Files changed (27) hide show
  1. {sure_tools-1.0.10 → sure_tools-2.0.2}/PKG-INFO +1 -1
  2. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/SURE.py +59 -6
  3. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/__init__.py +2 -1
  4. sure_tools-2.0.2/SURE/flow/__init__.py +1 -0
  5. sure_tools-2.0.2/SURE/flow/quiver.py +52 -0
  6. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE_tools.egg-info/PKG-INFO +1 -1
  7. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE_tools.egg-info/SOURCES.txt +2 -1
  8. {sure_tools-1.0.10 → sure_tools-2.0.2}/setup.py +1 -1
  9. sure_tools-1.0.10/SURE/SURE2.py +0 -1236
  10. {sure_tools-1.0.10 → sure_tools-2.0.2}/LICENSE +0 -0
  11. {sure_tools-1.0.10 → sure_tools-2.0.2}/README.md +0 -0
  12. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/assembly/__init__.py +0 -0
  13. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/assembly/assembly.py +0 -0
  14. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/assembly/atlas.py +0 -0
  15. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/atac/__init__.py +0 -0
  16. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/atac/utils.py +0 -0
  17. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/codebook/__init__.py +0 -0
  18. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/codebook/codebook.py +0 -0
  19. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/utils/__init__.py +0 -0
  20. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/utils/custom_mlp.py +0 -0
  21. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/utils/queue.py +0 -0
  22. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE/utils/utils.py +0 -0
  23. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE_tools.egg-info/dependency_links.txt +0 -0
  24. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE_tools.egg-info/entry_points.txt +0 -0
  25. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE_tools.egg-info/requires.txt +0 -0
  26. {sure_tools-1.0.10 → sure_tools-2.0.2}/SURE_tools.egg-info/top_level.txt +0 -0
  27. {sure_tools-1.0.10 → sure_tools-2.0.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SURE-tools
3
- Version: 1.0.10
3
+ Version: 2.0.2
4
4
  Summary: Succinct Representation of Single Cells
5
5
  Home-page: https://github.com/ZengFLab/SURE
6
6
  Author: Feng Zeng
@@ -97,6 +97,7 @@ class SURE(nn.Module):
97
97
  input_size: int,
98
98
  codebook_size: int = 200,
99
99
  cell_factor_size: int = 0,
100
+ cell_factor_names: list = None,
100
101
  supervised_mode: bool = False,
101
102
  z_dim: int = 10,
102
103
  z_dist: Literal['normal','studentt','laplacian','cauchy','gumbel'] = 'normal',
@@ -134,6 +135,7 @@ class SURE(nn.Module):
134
135
  self.post_layer_fct = post_layer_fct
135
136
  self.post_act_fct = post_act_fct
136
137
  self.hidden_layer_activation = hidden_layer_activation
138
+ self.cell_factor_names = cell_factor_names
137
139
 
138
140
  self.codebook_weights = None
139
141
 
@@ -232,8 +234,10 @@ class SURE(nn.Module):
232
234
  )
233
235
 
234
236
  if self.cell_factor_size>0:
235
- self.cell_factor_effect = MLP(
236
- [self.cell_factor_size] + self.decoder_hidden_layers + [self.latent_dim],
237
+ self.cell_factor_effect = nn.ModuleList()
238
+ for i in np.arange(self.cell_factor_size):
239
+ self.cell_factor_effect.append(MLP(
240
+ [self.z_dim+1] + hidden_sizes + [self.z_dim],
237
241
  activation=activate_fct,
238
242
  output_activation=None,
239
243
  post_layer_fct=post_layer_fct,
@@ -241,6 +245,7 @@ class SURE(nn.Module):
241
245
  allow_broadcast=self.allow_broadcast,
242
246
  use_cuda=self.use_cuda,
243
247
  )
248
+ )
244
249
 
245
250
  self.decoder_concentrate = MLP(
246
251
  [self.latent_dim] + self.decoder_hidden_layers + [self.input_size],
@@ -444,8 +449,14 @@ class SURE(nn.Module):
444
449
  zns = pyro.sample('zn', dist.Gumbel(zn_loc, zn_scale).to_event(1))
445
450
 
446
451
  if self.cell_factor_size>0:
447
- zus = self.cell_factor_effect(us)
448
- zs = zns + zus
452
+ #zus = self.decoder_undesired([zns,us])
453
+ zus = None
454
+ for i in np.arange(self.cell_factor_size):
455
+ if i==0:
456
+ zus = self.cell_factor_effect[i]([zns,us[:,i].reshape(-1,1)])
457
+ else:
458
+ zus = zus + self.cell_factor_effect[i]([zns,us[:,i].reshape(-1,1)])
459
+ zs = zns+zus
449
460
  else:
450
461
  zs = zns
451
462
 
@@ -634,8 +645,14 @@ class SURE(nn.Module):
634
645
  zns = pyro.sample('zn', dist.Gumbel(zn_loc, zn_scale).to_event(1), obs=embeds)
635
646
 
636
647
  if self.cell_factor_size>0:
637
- zus = self.cell_factor_effect(us)
638
- zs = zus + zns
648
+ #zus = self.decoder_undesired([zns,us])
649
+ zus = None
650
+ for i in np.arange(self.cell_factor_size):
651
+ if i==0:
652
+ zus = self.cell_factor_effect[i]([zns,us[:,i].reshape(-1,1)])
653
+ else:
654
+ zus = zus + self.cell_factor_effect[i]([zns,us[:,i].reshape(-1,1)])
655
+ zs = zns+zus
639
656
  else:
640
657
  zs = zns
641
658
 
@@ -796,6 +813,42 @@ class SURE(nn.Module):
796
813
  A = np.concatenate(A)
797
814
  return A
798
815
 
816
+ def _cell_move(self, xs, factor_idx, perturb):
817
+ zns = self.encoder_zn(xs)
818
+ if type(factor_idx) == str:
819
+ factor_idx = np.where(self.cell_factor_names==factor_idx)
820
+
821
+ if perturb.ndim==2:
822
+ ms = self.cell_factor_effect[factor_idx]([zns, perturb])
823
+ else:
824
+ ms = self.cell_factor_effect[factor_idx]([zns, perturb.reshape(-1,1)])
825
+
826
+ return ms
827
+
828
+ def get_cell_move(self,
829
+ xs,
830
+ factor_idx,
831
+ perturb,
832
+ batch_size: int = 1024):
833
+ """
834
+ Return cells' changes in the latent space induced by specific perturbation of a factor
835
+
836
+ """
837
+ xs = self.preprocess(xs)
838
+ xs = convert_to_tensor(xs, device=self.get_device())
839
+ dataset = CustomDataset(xs)
840
+ dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
841
+
842
+ Z = []
843
+ with tqdm(total=len(dataloader), desc='', unit='batch') as pbar:
844
+ for X_batch, _ in dataloader:
845
+ zns = self._cell_move(X_batch, factor_idx, perturb)
846
+ Z.append(tensor_to_numpy(zns))
847
+ pbar.update(1)
848
+
849
+ Z = np.concatenate(Z)
850
+ return Z
851
+
799
852
  def preprocess(self, xs, threshold=0):
800
853
  if self.loss_func == 'bernoulli':
801
854
  ad = sc.AnnData(xs)
@@ -6,5 +6,6 @@ from . import codebook
6
6
  from . import SURE
7
7
  from . import SURE2
8
8
  from . import atac
9
+ from . import flow
9
10
 
10
- __all__ = ['SURE','SURE2', 'atac', 'utils', 'codebook']
11
+ __all__ = ['SURE','SURE2', 'flow', 'atac', 'utils', 'codebook']
@@ -0,0 +1 @@
1
+ from .quiver import plot_quiver
@@ -0,0 +1,52 @@
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_high_dim(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]:] - 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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: SURE-tools
3
- Version: 1.0.10
3
+ Version: 2.0.2
4
4
  Summary: Succinct Representation of Single Cells
5
5
  Home-page: https://github.com/ZengFLab/SURE
6
6
  Author: Feng Zeng
@@ -2,7 +2,6 @@ LICENSE
2
2
  README.md
3
3
  setup.py
4
4
  SURE/SURE.py
5
- SURE/SURE2.py
6
5
  SURE/__init__.py
7
6
  SURE/assembly/__init__.py
8
7
  SURE/assembly/assembly.py
@@ -11,6 +10,8 @@ SURE/atac/__init__.py
11
10
  SURE/atac/utils.py
12
11
  SURE/codebook/__init__.py
13
12
  SURE/codebook/codebook.py
13
+ SURE/flow/__init__.py
14
+ SURE/flow/quiver.py
14
15
  SURE/utils/__init__.py
15
16
  SURE/utils/custom_mlp.py
16
17
  SURE/utils/queue.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setup(
7
7
  name='SURE-tools',
8
- version='1.0.10',
8
+ version='2.0.2',
9
9
  description='Succinct Representation of Single Cells',
10
10
  long_description=long_description,
11
11
  long_description_content_type="text/markdown",