Skip to content

Commit

Permalink
fixed logging for representation choice in UMAP
Browse files Browse the repository at this point in the history
  • Loading branch information
falexwolf committed May 31, 2019
1 parent c319ac0 commit a5bd1ec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scanpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
del get_versions, check_versions

# the actual API
from ._settings import settings # start with settings as several tools are using it
from . import tools as tl
from . import preprocessing as pp
from . import plotting as pl
Expand All @@ -37,7 +38,6 @@
from anndata import read_h5ad, read_csv, read_excel, read_hdf, read_loom, read_mtx, read_text, read_umi_tools
from .readwrite import read, read_10x_h5, read_10x_mtx, write
from .neighbors import Neighbors
from ._settings import settings

set_figure_params = settings.set_figure_params

Expand Down
5 changes: 4 additions & 1 deletion scanpy/tools/_umap.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ def umap(
n_epochs = 0 if maxiter is None else maxiter
verbosity = _VERBOSITY_LEVELS_FROM_STRINGS.get(settings.verbosity, settings.verbosity)
neigh_params = adata.uns['neighbors']['params']
X = choose_representation(adata, neigh_params.get('use_rep', None), neigh_params.get('n_pcs', None))
X = choose_representation(
adata, neigh_params.get('use_rep', None), neigh_params.get('n_pcs', None), silent=True)
# the data matrix X is really only used for determining the number of connected components
# for the init condition in the UMAP embedding
X_umap = simplicial_set_embedding(
X,
adata.uns['neighbors']['connectivities'].tocoo(),
Expand Down
19 changes: 11 additions & 8 deletions scanpy/tools/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .. import logging as logg
from ._pca import pca
from ..preprocessing._simple import N_PCS
from .. import settings

doc_use_rep = """\
use_rep : {`None`, 'X'} or any key for `.obsm`, optional (default: `None`)
Expand All @@ -16,7 +17,10 @@
"""


def choose_representation(adata, use_rep=None, n_pcs=None):
def choose_representation(adata, use_rep=None, n_pcs=None, silent=False):
verbosity = settings.verbosity
if silent and settings.verbosity > 1:
settings.verbosity = 1
if use_rep is None and n_pcs == 0: # backwards compat for specifying `.X`
use_rep = 'X'
if use_rep is None:
Expand All @@ -26,9 +30,7 @@ def choose_representation(adata, use_rep=None, n_pcs=None):
raise ValueError(
'`X_pca` does not have enough PCs. Rerun `sc.pp.pca` with adjusted `n_comps`.')
X = adata.obsm['X_pca'][:, :n_pcs]
logg.info(' using \'X_pca\' with n_pcs = {}'
.format(X.shape[1]))
return X
logg.info(' using \'X_pca\' with n_pcs = {}'.format(X.shape[1]))
else:
logg.warn(
'You\'re trying to run this on {} dimensions of `.X`, '
Expand All @@ -37,19 +39,20 @@ def choose_representation(adata, use_rep=None, n_pcs=None):
.format(adata.n_vars))
X = pca(adata.X)
adata.obsm['X_pca'] = X[:, :n_pcs]
return X
else:
logg.info(' using data matrix X directly')
return adata.X
X = adata.X
else:
if use_rep in adata.obsm.keys():
return adata.obsm[use_rep]
X = adata.obsm[use_rep]
elif use_rep == 'X':
return adata.X
X = adata.X
else:
raise ValueError(
'Did not find {} in `.obsm.keys()`. '
'You need to compute it first.'.format(use_rep))
settings.verbosity = verbosity # resetting verbosity
return X


def preprocess_with_pca(adata, n_pcs=None, random_state=0):
Expand Down

0 comments on commit a5bd1ec

Please sign in to comment.