/
_umap.py
267 lines (241 loc) · 9.53 KB
/
_umap.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from __future__ import annotations
import warnings
from typing import TYPE_CHECKING, Literal
import numpy as np
from sklearn.utils import check_array, check_random_state
from .. import logging as logg
from .._compat import old_positionals
from .._settings import settings
from .._utils import AnyRandom, NeighborsView
from ._utils import _choose_representation, get_init_pos_from_paga
if TYPE_CHECKING:
from anndata import AnnData
_InitPos = Literal["paga", "spectral", "random"]
@old_positionals(
"min_dist",
"spread",
"n_components",
"maxiter",
"alpha",
"gamma",
"negative_sample_rate",
"init_pos",
"random_state",
"a",
"b",
"copy",
"method",
"neighbors_key",
)
def umap(
adata: AnnData,
*,
min_dist: float = 0.5,
spread: float = 1.0,
n_components: int = 2,
maxiter: int | None = None,
alpha: float = 1.0,
gamma: float = 1.0,
negative_sample_rate: int = 5,
init_pos: _InitPos | np.ndarray | None = "spectral",
random_state: AnyRandom = 0,
a: float | None = None,
b: float | None = None,
copy: bool = False,
method: Literal["umap", "rapids"] = "umap",
neighbors_key: str | None = None,
) -> AnnData | None:
"""\
Embed the neighborhood graph using UMAP :cite:p:`McInnes2018`.
UMAP (Uniform Manifold Approximation and Projection) is a manifold learning
technique suitable for visualizing high-dimensional data. Besides tending to
be faster than tSNE, it optimizes the embedding such that it best reflects
the topology of the data, which we represent throughout Scanpy using a
neighborhood graph. tSNE, by contrast, optimizes the distribution of
nearest-neighbor distances in the embedding such that these best match the
distribution of distances in the high-dimensional space.
We use the implementation of umap-learn_ :cite:p:`McInnes2018`.
For a few comparisons of UMAP with tSNE, see :cite:t:`Becht2018`.
.. _umap-learn: https://github.com/lmcinnes/umap
Parameters
----------
adata
Annotated data matrix.
min_dist
The effective minimum distance between embedded points. Smaller values
will result in a more clustered/clumped embedding where nearby points on
the manifold are drawn closer together, while larger values will result
on a more even dispersal of points. The value should be set relative to
the ``spread`` value, which determines the scale at which embedded
points will be spread out. The default of in the `umap-learn` package is
0.1.
spread
The effective scale of embedded points. In combination with `min_dist`
this determines how clustered/clumped the embedded points are.
n_components
The number of dimensions of the embedding.
maxiter
The number of iterations (epochs) of the optimization. Called `n_epochs`
in the original UMAP.
alpha
The initial learning rate for the embedding optimization.
gamma
Weighting applied to negative samples in low dimensional embedding
optimization. Values higher than one will result in greater weight
being given to negative samples.
negative_sample_rate
The number of negative edge/1-simplex samples to use per positive
edge/1-simplex sample in optimizing the low dimensional embedding.
init_pos
How to initialize the low dimensional embedding. Called `init` in the
original UMAP. Options are:
* Any key for `adata.obsm`.
* 'paga': positions from :func:`~scanpy.pl.paga`.
* 'spectral': use a spectral embedding of the graph.
* 'random': assign initial embedding positions at random.
* A numpy array of initial embedding positions.
random_state
If `int`, `random_state` is the seed used by the random number generator;
If `RandomState` or `Generator`, `random_state` is the random number generator;
If `None`, the random number generator is the `RandomState` instance used
by `np.random`.
a
More specific parameters controlling the embedding. If `None` these
values are set automatically as determined by `min_dist` and
`spread`.
b
More specific parameters controlling the embedding. If `None` these
values are set automatically as determined by `min_dist` and
`spread`.
copy
Return a copy instead of writing to adata.
method
Chosen implementation.
``'umap'``
Umap’s simplical set embedding.
``'rapids'``
GPU accelerated implementation.
.. deprecated:: 1.10.0
Use :func:`rapids_singlecell.tl.umap` instead.
neighbors_key
If not specified, umap looks .uns['neighbors'] for neighbors settings
and .obsp['connectivities'] for connectivities
(default storage places for pp.neighbors).
If specified, umap looks .uns[neighbors_key] for neighbors settings and
.obsp[.uns[neighbors_key]['connectivities_key']] for connectivities.
Returns
-------
Returns `None` if `copy=False`, else returns an `AnnData` object. Sets the following fields:
`adata.obsm['X_umap']` : :class:`numpy.ndarray` (dtype `float`)
UMAP coordinates of data.
`adata.uns['umap']` : :class:`dict`
UMAP parameters.
"""
adata = adata.copy() if copy else adata
if neighbors_key is None:
neighbors_key = "neighbors"
if neighbors_key not in adata.uns:
raise ValueError(
f"Did not find .uns[{neighbors_key!r}]. Run `sc.pp.neighbors` first."
)
start = logg.info("computing UMAP")
neighbors = NeighborsView(adata, neighbors_key)
if "params" not in neighbors or neighbors["params"]["method"] != "umap":
logg.warning(
f'.obsp["{neighbors["connectivities_key"]}"] have not been computed using umap'
)
with warnings.catch_warnings():
# umap 0.5.0
warnings.filterwarnings("ignore", message=r"Tensorflow not installed")
import umap
from umap.umap_ import find_ab_params, simplicial_set_embedding
if a is None or b is None:
a, b = find_ab_params(spread, min_dist)
else:
a = a
b = b
adata.uns["umap"] = {"params": {"a": a, "b": b}}
if isinstance(init_pos, str) and init_pos in adata.obsm.keys():
init_coords = adata.obsm[init_pos]
elif isinstance(init_pos, str) and init_pos == "paga":
init_coords = get_init_pos_from_paga(
adata, random_state=random_state, neighbors_key=neighbors_key
)
else:
init_coords = init_pos # Let umap handle it
if hasattr(init_coords, "dtype"):
init_coords = check_array(init_coords, dtype=np.float32, accept_sparse=False)
if random_state != 0:
adata.uns["umap"]["params"]["random_state"] = random_state
random_state = check_random_state(random_state)
neigh_params = neighbors["params"]
X = _choose_representation(
adata,
use_rep=neigh_params.get("use_rep", None),
n_pcs=neigh_params.get("n_pcs", None),
silent=True,
)
if method == "umap":
# the data matrix X is really only used for determining the number of connected components
# for the init condition in the UMAP embedding
default_epochs = 500 if neighbors["connectivities"].shape[0] <= 10000 else 200
n_epochs = default_epochs if maxiter is None else maxiter
X_umap, _ = simplicial_set_embedding(
data=X,
graph=neighbors["connectivities"].tocoo(),
n_components=n_components,
initial_alpha=alpha,
a=a,
b=b,
gamma=gamma,
negative_sample_rate=negative_sample_rate,
n_epochs=n_epochs,
init=init_coords,
random_state=random_state,
metric=neigh_params.get("metric", "euclidean"),
metric_kwds=neigh_params.get("metric_kwds", {}),
densmap=False,
densmap_kwds={},
output_dens=False,
verbose=settings.verbosity > 3,
)
elif method == "rapids":
msg = (
"`method='rapids'` is deprecated. "
"Use `rapids_singlecell.tl.louvain` instead."
)
warnings.warn(msg, FutureWarning)
metric = neigh_params.get("metric", "euclidean")
if metric != "euclidean":
raise ValueError(
f"`sc.pp.neighbors` was called with `metric` {metric!r}, "
"but umap `method` 'rapids' only supports the 'euclidean' metric."
)
from cuml import UMAP
n_neighbors = neighbors["params"]["n_neighbors"]
n_epochs = (
500 if maxiter is None else maxiter
) # 0 is not a valid value for rapids, unlike original umap
X_contiguous = np.ascontiguousarray(X, dtype=np.float32)
umap = UMAP(
n_neighbors=n_neighbors,
n_components=n_components,
n_epochs=n_epochs,
learning_rate=alpha,
init=init_pos,
min_dist=min_dist,
spread=spread,
negative_sample_rate=negative_sample_rate,
a=a,
b=b,
verbose=settings.verbosity > 3,
random_state=random_state,
)
X_umap = umap.fit_transform(X_contiguous)
adata.obsm["X_umap"] = X_umap # annotate samples with UMAP coordinates
logg.info(
" finished",
time=start,
deep=("added\n" " 'X_umap', UMAP coordinates (adata.obsm)"),
)
return adata if copy else None