Skip to content

Commit

Permalink
Backport PR #1589: fix sc.tl.umap for umap-0.5 (#1598)
Browse files Browse the repository at this point in the history
Co-authored-by: Isaac Virshup <[email protected]>
  • Loading branch information
meeseeksmachine and ivirshup authored Jan 19, 2021
1 parent edc868c commit ac89e59
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
11 changes: 9 additions & 2 deletions scanpy/neighbors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from types import MappingProxyType
from typing import Union, Optional, Any, Mapping, Callable, NamedTuple, Generator, Tuple
import warnings

import numpy as np
import scipy
Expand Down Expand Up @@ -267,7 +268,10 @@ def compute_neighbors_umap(
-------
**knn_indices**, **knn_dists** : np.arrays of shape (n_observations, n_neighbors)
"""
from umap.umap_ import nearest_neighbors
with warnings.catch_warnings():
# umap 0.5.0
warnings.filterwarnings("ignore", message=r"Tensorflow not installed")
from umap.umap_ import nearest_neighbors

random_state = check_random_state(random_state)

Expand Down Expand Up @@ -344,7 +348,10 @@ def _compute_connectivities_umap(
simplicial set for each such point, and then combining all the local
fuzzy simplicial sets into a global one via a fuzzy union.
"""
from umap.umap_ import fuzzy_simplicial_set
with warnings.catch_warnings():
# umap 0.5.0
warnings.filterwarnings("ignore", message=r"Tensorflow not installed")
from umap.umap_ import fuzzy_simplicial_set

X = coo_matrix(([], ([], [])), shape=(n_obs, 1))
connectivities = fuzzy_simplicial_set(
Expand Down
25 changes: 24 additions & 1 deletion scanpy/tools/_umap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Optional, Union
import warnings

import numpy as np
from packaging import version
from anndata import AnnData
from sklearn.utils import check_random_state, check_array

Expand Down Expand Up @@ -129,7 +131,28 @@ def umap(
if ('params' not in neighbors
or neighbors['params']['method'] != 'umap'):
logg.warning(f'.obsp["{neighbors["connectivities_key"]}"] have not been computed using umap')
from umap.umap_ import find_ab_params, simplicial_set_embedding

# Compat for umap 0.4 -> 0.5
with warnings.catch_warnings():
# umap 0.5.0
warnings.filterwarnings("ignore", message=r"Tensorflow not installed")
import umap

if version.parse(umap.__version__) >= version.parse("0.5.0"):
def simplicial_set_embedding(*args, **kwargs):
from umap.umap_ import simplicial_set_embedding
X_umap, _ = simplicial_set_embedding(
*args,
densmap=False,
densmap_kwds={},
output_dens=False,
**kwargs,
)
return X_umap
else:
from umap.umap_ import simplicial_set_embedding
from umap.umap_ import find_ab_params

if a is None or b is None:
a, b = find_ab_params(spread, min_dist)
else:
Expand Down

0 comments on commit ac89e59

Please sign in to comment.