scFates 1.2.7__tar.gz → 1.2.8__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.
Files changed (62) hide show
  1. {scfates-1.2.7/scFates.egg-info → scfates-1.2.8}/PKG-INFO +1 -1
  2. {scfates-1.2.7 → scfates-1.2.8}/scFates/preprocessing/diffusion.py +4 -0
  3. {scfates-1.2.7 → scfates-1.2.8}/scFates/preprocessing/pagoda2.py +4 -2
  4. {scfates-1.2.7 → scfates-1.2.8}/scFates/tests/test_w_plots.py +56 -0
  5. scfates-1.2.8/scFates/tools/cluster.py +92 -0
  6. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/covariate.py +10 -3
  7. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/root.py +4 -7
  8. {scfates-1.2.7 → scfates-1.2.8/scFates.egg-info}/PKG-INFO +1 -1
  9. scfates-1.2.7/scFates/tools/cluster.py +0 -121
  10. {scfates-1.2.7 → scfates-1.2.8}/LICENSE +0 -0
  11. {scfates-1.2.7 → scfates-1.2.8}/MANIFEST.in +0 -0
  12. {scfates-1.2.7 → scfates-1.2.8}/README.md +0 -0
  13. {scfates-1.2.7 → scfates-1.2.8}/pyproject.toml +0 -0
  14. {scfates-1.2.7 → scfates-1.2.8}/scFates/__init__.py +0 -0
  15. {scfates-1.2.7 → scfates-1.2.8}/scFates/datasets/__init__.py +0 -0
  16. {scfates-1.2.7 → scfates-1.2.8}/scFates/datasets/_datasets.py +0 -0
  17. {scfates-1.2.7 → scfates-1.2.8}/scFates/datasets/test.h5ad +0 -0
  18. {scfates-1.2.7 → scfates-1.2.8}/scFates/get/__init__.py +0 -0
  19. {scfates-1.2.7 → scfates-1.2.8}/scFates/get/get.py +0 -0
  20. {scfates-1.2.7 → scfates-1.2.8}/scFates/logging.py +0 -0
  21. {scfates-1.2.7 → scfates-1.2.8}/scFates/pl.py +0 -0
  22. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/__init__.py +0 -0
  23. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/binned_pseudotime_meta.py +0 -0
  24. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/covariate.py +0 -0
  25. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/dendrogram.py +0 -0
  26. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/features.py +0 -0
  27. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/linearity_deviation.py +0 -0
  28. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/matrix.py +0 -0
  29. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/milestones.py +0 -0
  30. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/module_inclusion.py +0 -0
  31. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/modules.py +0 -0
  32. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/palette_tools.py +0 -0
  33. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/palettes.py +0 -0
  34. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/slide_cors.py +0 -0
  35. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/synchro_path.py +0 -0
  36. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/test_association.py +0 -0
  37. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/test_fork.py +0 -0
  38. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/trajectory.py +0 -0
  39. {scfates-1.2.7 → scfates-1.2.8}/scFates/plot/utils.py +0 -0
  40. {scfates-1.2.7 → scfates-1.2.8}/scFates/pp.py +0 -0
  41. {scfates-1.2.7 → scfates-1.2.8}/scFates/preprocessing/__init__.py +0 -0
  42. {scfates-1.2.7 → scfates-1.2.8}/scFates/settings.py +0 -0
  43. {scfates-1.2.7 → scfates-1.2.8}/scFates/tl.py +0 -0
  44. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/__init__.py +0 -0
  45. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/_test_monocle3.R +0 -0
  46. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/bifurcation_tools.py +0 -0
  47. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/conversion.py +0 -0
  48. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/correlation_tools.py +0 -0
  49. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/dendrogram.py +0 -0
  50. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/fit.py +0 -0
  51. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/graph_fitting.py +0 -0
  52. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/graph_operations.py +0 -0
  53. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/linearity_deviation.py +0 -0
  54. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/pseudotime.py +0 -0
  55. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/slide_cors.py +0 -0
  56. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/test_association.py +0 -0
  57. {scfates-1.2.7 → scfates-1.2.8}/scFates/tools/utils.py +0 -0
  58. {scfates-1.2.7 → scfates-1.2.8}/scFates.egg-info/SOURCES.txt +0 -0
  59. {scfates-1.2.7 → scfates-1.2.8}/scFates.egg-info/dependency_links.txt +0 -0
  60. {scfates-1.2.7 → scfates-1.2.8}/scFates.egg-info/requires.txt +0 -0
  61. {scfates-1.2.7 → scfates-1.2.8}/scFates.egg-info/top_level.txt +0 -0
  62. {scfates-1.2.7 → scfates-1.2.8}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scFates
3
- Version: 1.2.7
3
+ Version: 1.2.8
4
4
  Summary: scanpy compatible python suite for fast tree inference and advanced pseudotime downstream analysis
5
5
  Author: Louis Faure
6
6
  Project-URL: Repository, https://github.com/LouisFaure/scFates
@@ -62,6 +62,8 @@ def diffusion(
62
62
  dict containing results from Palantir.
63
63
  """
64
64
 
65
+ adata = adata.copy() if copy else adata
66
+
65
67
  logg.info("Running Diffusion maps ", reset=True)
66
68
 
67
69
  data_df = pd.DataFrame(adata.obsm["X_pca"], index=adata.obs_names)
@@ -174,3 +176,5 @@ def diffusion(
174
176
 
175
177
  logg.info(" finished", time=True, end=" " if settings.verbosity > 2 else "\n")
176
178
  logg.hint("added \n" + logstr + uns_str)
179
+
180
+ return adata if copy else None
@@ -200,7 +200,9 @@ def batch_correct(
200
200
  gene_av = (np.array(X.sum(axis=0)).ravel() + len(batches)) / (
201
201
  depth.sum() + len(batches)
202
202
  )
203
- tc = np.vstack([X[adata.obs[batch_key] == b, :].sum(axis=0) for b in batches])
203
+ tc = np.vstack(
204
+ [X[(adata.obs[batch_key] == b).values, :].sum(axis=0) for b in batches]
205
+ )
204
206
  tc = np.log(tc + 1) - np.log(
205
207
  np.array([depth[adata.obs[batch_key].values == b].sum() for b in batches])
206
208
  + 1
@@ -211,7 +213,7 @@ def batch_correct(
211
213
 
212
214
  batch = adata.obs[batch_key].cat.rename_categories(range(nbatches))
213
215
  count_gene = np.repeat(np.arange(X.shape[0]), np.diff(X.indptr))
214
- acc = np.transpose(np.vstack([count_gene, batch[X.indices].values]))
216
+ acc = np.transpose(np.vstack([count_gene, batch.cat.codes.values[X.indices]]))
215
217
  X.data = X.data / bc.values[acc[:, 0], acc[:, 1]]
216
218
  logg.info(" depth scaling")
217
219
  X = X.transpose()
@@ -86,6 +86,11 @@ def test_pipeline():
86
86
 
87
87
  scf.tl.pseudotime(adata_2)
88
88
  scf.tl.pseudotime(adata)
89
+ # automatic root on tips with all-negative values must still pick a tip
90
+ adata.obs["neg_t"] = -adata.obs.t - 1
91
+ adata_r = scf.tl.root(adata, "neg_t", tips_only=True, copy=True)
92
+ assert adata_r.uns["graph"]["root"] == adata.uns["graph"]["root"]
93
+ del adata.obs["neg_t"]
89
94
  scf.tl.dendrogram(adata)
90
95
  ax=scf.pl.dendrogram(adata, color_milestones=True,show=False)
91
96
 
@@ -189,6 +194,9 @@ def test_pipeline():
189
194
  scf.tl.test_covariate(adata, "covariate")
190
195
  scf.tl.test_covariate(adata, "covariate", trend_test=True)
191
196
  scf.tl.test_covariate(adata, "covariate", nested=True)
197
+ # restricted to one segment of the tree
198
+ scf.tl.test_covariate(adata, "covariate", seg=str(adata.obs.seg.iloc[0]))
199
+ assert adata.var["cov_pval"].notna().all()
192
200
  scf.pl.trend_covariate(
193
201
  adata, adata.var_names[0], group_key="covariate", show_null=True
194
202
  ,show=False)
@@ -201,6 +209,14 @@ def test_pipeline():
201
209
  scf.pl.trajectory(adata_2, root_milestone="A", milestones=["B"],show=False)
202
210
 
203
211
  scf.tl.cluster(adata, n_neighbors=3, n_pcs=3)
212
+ # the requested layer is used, not .X: replacing .X by noise changes nothing
213
+ adata_n = adata.copy()
214
+ adata_n.X = np.random.default_rng(0).normal(size=adata.shape)
215
+ adata_n = scf.tl.cluster(adata_n, n_neighbors=3, n_pcs=3, copy=True)
216
+ adata_x = adata.copy()
217
+ adata_x.X = adata_x.layers["fitted"]
218
+ scf.tl.cluster(adata_x, layer="X", n_neighbors=3, n_pcs=3)
219
+ assert (adata_n.var.clusters.values == adata_x.var.clusters.values).all()
204
220
 
205
221
  g = scf.pl.trends(adata, features=adata.var_names, return_genes=True)
206
222
  scf.pl.trends(
@@ -400,3 +416,43 @@ def test_pipeline():
400
416
  [-0.31524637, -0.31332134, -0.3392098, -0.33832952, -0.31277207],
401
417
  rtol=1e-2,
402
418
  )
419
+
420
+
421
+ def test_preprocessing(monkeypatch):
422
+ import sys
423
+ import types
424
+ import anndata as ad
425
+ from scipy.sparse import csr_matrix
426
+
427
+ rng = np.random.default_rng(0)
428
+ counts = rng.poisson(rng.gamma(0.5, 2, size=(1, 300)), size=(400, 300))
429
+ adata = ad.AnnData(csr_matrix(counts.astype(np.float32)))
430
+ adata.obs["batch"] = ["a"] * 200 + ["b"] * 200
431
+
432
+ scf.pp.filter_cells(adata, subset=False)
433
+ assert "outlier" in adata.obs
434
+
435
+ X_bc = scf.pp.batch_correct(adata, "batch", inplace=False)
436
+ assert X_bc.shape == adata.shape
437
+
438
+ # stand-in for palantir (not a test dependency) to check copy behaviour
439
+ utils = types.ModuleType("palantir.utils")
440
+
441
+ def run_diffusion_maps(data_df, n_components, knn, alpha):
442
+ ev = rng.normal(size=(data_df.shape[0], n_components))
443
+ return {
444
+ "EigenVectors": pd.DataFrame(ev, index=data_df.index),
445
+ "EigenValues": pd.Series(np.linspace(1, 0.1, n_components)),
446
+ }
447
+
448
+ utils.run_diffusion_maps = run_diffusion_maps
449
+ palantir = types.ModuleType("palantir")
450
+ palantir.utils = utils
451
+ monkeypatch.setitem(sys.modules, "palantir", palantir)
452
+ monkeypatch.setitem(sys.modules, "palantir.utils", utils)
453
+
454
+ adata = scf.datasets.test_adata()
455
+ del adata.obsm["X_diffusion"]
456
+ adata_d = scf.pp.diffusion(adata, n_components=5, multiscale=False, copy=True)
457
+ assert adata_d.obsm["X_diffusion"].shape == (adata.n_obs, 4)
458
+ assert "X_diffusion" not in adata.obsm
@@ -0,0 +1,92 @@
1
+ import pandas as pd
2
+ import scanpy as sc
3
+ from anndata import AnnData
4
+
5
+ from .. import logging as logg
6
+ from .. import settings
7
+ from .graph_fitting import get_data
8
+
9
+
10
+ def cluster(
11
+ adata: AnnData,
12
+ layer="fitted",
13
+ n_neighbors: int = 20,
14
+ n_pcs: int = 50,
15
+ metric: str = "cosine",
16
+ resolution: float = 1,
17
+ device: str = "cpu",
18
+ copy: bool = False,
19
+ ):
20
+ """\
21
+ Cluster features. Uses scanpy when using cpu, and rapids-singlecell when using gpu.
22
+ Dataset is transposed, PCA is calulcated and a nearest neighbor graph is generated
23
+ from PC space. Leiden algorithm is used for community detection.
24
+
25
+
26
+ Parameters
27
+ ----------
28
+ adata
29
+ Annotated data matrix.
30
+ layer
31
+ Layer of feature to calculate clusters, by default fitted features
32
+ n_neighbors
33
+ Number of neighbors.
34
+ n_pcs
35
+ Number of PC to keep for PCA.
36
+ metric
37
+ distance metric to use for clustering.
38
+ resolution
39
+ Resolution parameter for leiden algorithm.
40
+ device
41
+ Run the analysis on 'cpu' (scanpy) or on 'gpu' (rapids-singlecell).
42
+ copy
43
+ Return a copy instead of writing to adata.
44
+
45
+ Returns
46
+ -------
47
+ adata : anndata.AnnData
48
+ if `copy=True` it returns or else add fields to `adata`:
49
+
50
+ `.var['clusters']`
51
+ module assignments for features.
52
+ """
53
+
54
+ adata = adata.copy() if copy else adata
55
+
56
+ layer = "X" if layer not in adata.layers else layer
57
+
58
+ logg.info(
59
+ f"Clustering features using {layer} layer",
60
+ reset=True,
61
+ end="\n",
62
+ )
63
+
64
+ X, _ = get_data(adata, layer, None)
65
+ adata_s = AnnData(
66
+ X.values.T,
67
+ obs=pd.DataFrame(index=adata.var_names),
68
+ var=pd.DataFrame(index=adata.obs_names),
69
+ )
70
+
71
+ if device == "gpu":
72
+ try:
73
+ import rapids_singlecell as rsc
74
+ except ModuleNotFoundError:
75
+ raise Exception(
76
+ "rapids-singlecell is required for device='gpu', use device='cpu' instead!"
77
+ )
78
+
79
+ rsc.pp.pca(adata_s, n_comps=n_pcs)
80
+ rsc.pp.neighbors(adata_s, n_neighbors=n_neighbors, metric=metric)
81
+ rsc.tl.leiden(adata_s, resolution=resolution)
82
+ else:
83
+ sc.pp.pca(adata_s, n_comps=n_pcs)
84
+ sc.pp.neighbors(adata_s, n_neighbors=n_neighbors, metric=metric)
85
+ sc.tl.leiden(adata_s, resolution=resolution, flavor="igraph", n_iterations=2)
86
+
87
+ adata.var["clusters"] = adata_s.obs.leiden.values
88
+
89
+ logg.info(" finished", time=True, end=" " if settings.verbosity > 2 else "\n")
90
+ logg.hint("added \n" " .var['clusters'] identified modules.")
91
+
92
+ return adata if copy else None
@@ -128,10 +128,17 @@ def test_covariate(
128
128
  logbase = np.log(adata.uns["log1p"]["base"])
129
129
  else:
130
130
  logbase = 1
131
- dfs = [
132
- pd.DataFrame(
133
- dict(t=adata.obs.t, exp=X, groups=adata.obs[group_key], logbase=logbase)
131
+ # pseudotime and groups restricted to the same cells as the expression values
132
+ obs = adata.obs.loc[segcells]
133
+ groups = obs[group_key].cat.remove_unused_categories()
134
+ if len(groups.cat.categories) < 2:
135
+ raise ValueError(
136
+ f"At least two groups of '{group_key}' are needed"
137
+ + (f" in segment {seg}" if seg is not None else "")
138
+ + f", found: {list(groups.cat.categories)}."
134
139
  )
140
+ dfs = [
141
+ pd.DataFrame(dict(t=obs.t, exp=X, groups=groups, logbase=logbase))
135
142
  for X in Xgenes
136
143
  ]
137
144
 
@@ -84,15 +84,12 @@ def root(
84
84
  )
85
85
  avgs.loc[nodes] = avgs_temp
86
86
  if tips_only:
87
+ # exclude non-tip nodes; NaN is skipped by idxmin/idxmax, unlike a
88
+ # placeholder value which could win when values are negative
87
89
  mask = np.ones(avgs.shape, bool)
88
90
  mask[adata.uns["graph"]["tips"]] = False
89
- avgs[mask] = 0
90
- if min_val:
91
- if tips_only:
92
- avgs[mask] = avgs.max()
93
- root = np.argmin(avgs)
94
- else:
95
- root = np.argmax(avgs)
91
+ avgs[mask] = np.nan
92
+ root = int(avgs.idxmin() if min_val else avgs.idxmax())
96
93
 
97
94
  if circle:
98
95
  d = 1e-6 + pairwise_distances(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scFates
3
- Version: 1.2.7
3
+ Version: 1.2.8
4
4
  Summary: scanpy compatible python suite for fast tree inference and advanced pseudotime downstream analysis
5
5
  Author: Louis Faure
6
6
  Project-URL: Repository, https://github.com/LouisFaure/scFates
@@ -1,121 +0,0 @@
1
- import scanpy as sc
2
- from anndata import AnnData
3
- from .. import logging as logg
4
- from .. import settings
5
- from .graph_fitting import get_data
6
-
7
-
8
- def cluster(
9
- adata: AnnData,
10
- layer="fitted",
11
- n_neighbors: int = 20,
12
- n_pcs: int = 50,
13
- metric: str = "cosine",
14
- resolution: float = 1,
15
- device: str = "cpu",
16
- copy: bool = False,
17
- ):
18
- """\
19
- Cluster features. Uses scanpy backend when using cpu, and cuml when using gpu.
20
- Dataset is transposed, PCA is calulcated and a nearest neighbor graph is generated
21
- from PC space. Leiden algorithm is used for community detection.
22
-
23
-
24
- Parameters
25
- ----------
26
- adata
27
- Annotated data matrix.
28
- layer
29
- Layer of feature to calculate clusters, by default fitted features
30
- n_neighbors
31
- Number of neighbors.
32
- n_pcs
33
- Number of PC to keep for PCA.
34
- metric
35
- distance metric to use for clustering.
36
- resolution
37
- Resolution parameter for leiden algorithm.
38
- device
39
- run the analysis on 'cpu' with phenograph, or on 'gpu' with grapheno.
40
-
41
- copy
42
- Return a copy instead of writing to adata.
43
- Returns
44
- -------
45
- adata : anndata.AnnData
46
- if `copy=True` it returns subsetted or else subset (keeping only
47
- significant features) and add fields to `adata`:
48
- `.var['cluters']`
49
- module assignments for features.
50
- """
51
-
52
- layer = "X" if layer not in adata.layers else layer
53
-
54
- logg.info(
55
- f"Clustering features using {layer} layer",
56
- reset=True,
57
- end="\n",
58
- )
59
-
60
- X, use_rep = get_data(adata, layer, None)
61
-
62
- adata_s = adata.copy().T
63
- if device == "gpu":
64
- from cuml.decomposition import PCA
65
-
66
- pca = PCA(n_components=n_pcs)
67
- adata_s.obsm["X_pca"] = pca.fit_transform(adata_s.X)
68
- else:
69
- sc.pp.pca(adata_s, n_comps=n_pcs)
70
-
71
- sc.pp.neighbors(
72
- adata_s,
73
- n_neighbors=n_neighbors,
74
- metric=metric,
75
- method="rapids" if device == "gpu" else "umap",
76
- )
77
-
78
- if device == "gpu":
79
- from scanpy.tools import _utils
80
-
81
- adjacency = _utils._choose_graph(adata_s, None, None)
82
- import cudf
83
- import cugraph
84
- from natsort import natsorted
85
- import numpy as np
86
- import pandas as pd
87
-
88
- offsets = cudf.Series(adjacency.indptr)
89
- indices = cudf.Series(adjacency.indices)
90
-
91
- sources, targets = adjacency.nonzero()
92
- weights = adjacency[sources, targets]
93
- if isinstance(weights, np.matrix):
94
- weights = np.asarray(weights).flatten()
95
- weights = cudf.Series(weights)
96
-
97
- g = cugraph.Graph()
98
-
99
- if hasattr(g, "add_adj_list"):
100
- g.add_adj_list(offsets, indices, weights)
101
- else:
102
- g.from_cudf_adjlist(offsets, indices, weights)
103
-
104
- leiden_parts, _ = cugraph.leiden(g, resolution=resolution)
105
- groups = (
106
- leiden_parts.to_pandas()
107
- .sort_values("vertex")[["partition"]]
108
- .to_numpy()
109
- .ravel()
110
- )
111
- adata_s.obs["leiden"] = pd.Categorical(
112
- values=groups.astype("U"),
113
- categories=natsorted(map(str, np.unique(groups))),
114
- )
115
- else:
116
- sc.tl.leiden(adata_s, resolution=resolution, flavor="igraph", n_iterations=2)
117
-
118
- adata.var["clusters"] = adata_s.obs.leiden
119
-
120
- logg.info(" finished", time=True, end=" " if settings.verbosity > 2 else "\n")
121
- logg.hint("added \n" " .var['clusters'] identified modules.")
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