This repository was archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
93 lines (76 loc) · 2.74 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations
from dataclasses import dataclass, field
import typing as tp
import pandas as pd
import pymde
import scanpy as sc
from anndata import AnnData
from src.types import DataFrame, Series, Path, Array
__all__ = ["DataSet", "PyMDE", "DiffMap", "AnnData"]
@dataclass
class DataSet:
x: DataFrame
obs: tp.Optional[DataFrame] = None
var: tp.Optional[DataFrame] = None
name: tp.Optional[str] = None
data_type: tp.Optional[str] = None
attributes: tp.Sequence[str] = ()
cmaps: tp.Dict[str, str] = field(default_factory=dict)
palettes: tp.Dict[str, tp.Sequence[tp.Tuple[float, float, float, float]]] = field(
default_factory=dict
)
metadata_dir: Path = Path("metadata").resolve()
data_dir: Path = Path("data").resolve()
results_dir: Path = Path("results").resolve()
def __repr__(self):
return f"DataSet with {self.n_obs} observations and {self.n_vars} variables."
@property
def n_obs(self):
return self.x.shape[0]
@property
def n_vars(self):
return self.x.shape[1]
@property
def shape(self):
return self.x.shape
# @x.setter
# def x(self, df: DataFrame) -> None:
# self.x = df
# if self.obs is not None:
# self.obs = self.obs.reindex(index=self.x.index)
# if self.var is not None:
# self.var = self.var.reindex(index=self.x.columns)
class PyMDE:
def fit_anndata(self, anndata, config="default") -> PyMDE:
if config == "default":
anndata.obsm["X_pymde"] = self.fit_transform(anndata.X)
elif config == "alternate":
anndata.obsm["X_pymde_alt"] = self.fit_transform(
anndata.X,
embedding_dim=2,
attractive_penalty=pymde.penalties.Quadratic,
repulsive_penalty=None,
)
return self
def fit_transform(self, x, embedding_dim: int = 2, **kwargs) -> Array:
if isinstance(x, pd.DataFrame):
x = x.values
embedding = (
pymde.preserve_neighbors(x, embedding_dim=embedding_dim, **kwargs).embed().numpy()
)
return embedding
class DiffMap:
def fit_transform(self, x, embedding_dim: int = 2, **kwargs) -> Array:
a = AnnData(x)
sc.pp.neighbors(a, use_rep="X")
sc.tl.pca(a, n_comps=1)
return a.obsm["X_diffmap"][:, 1 : 1 + embedding_dim]
class DPT:
def fit_transform(
self, x, root_obs: str, embedding_dim: int = 2, **kwargs
) -> tp.Tuple[Array, Series]:
a = AnnData(x)
sc.pp.neighbors(a, use_rep="X")
a.var["xroot"] = a[root_obs, :].X.squeeze()
sc.tl.dpt(a)
return a.obsm["X_diffmap"][:, 1 : 1 + embedding_dim], a.obs["dpt_pseudotime"]