Skip to content

Commit

Permalink
Merge pull request scverse#191 from jamestwebber/patch-1
Browse files Browse the repository at this point in the history
Speed up log1p
  • Loading branch information
falexwolf authored Jul 9, 2018
2 parents 1550326 + 438a60b commit 39c234c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions scanpy/preprocessing/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,27 @@ def log1p(data, copy=False, chunked=False, chunk_size=None):
-------
Returns or updates `data`, depending on `copy`.
"""
if copy:
data = data.copy()

def _log1p(X):
if issparse(X):
np.log1p(X.data, out=X.data)
else:
np.log1p(X, out=X)

return X

if isinstance(data, AnnData):
adata = data.copy() if copy else data
if chunked:
for chunk, start, end in adata.chunked_X(chunk_size):
adata.X[start:end] = log1p(chunk)
for chunk, start, end in data.chunked_X(chunk_size):
data.X[start:end] = _log1p(chunk)
else:
adata.X = log1p(data.X)
return adata if copy else None
X = data # proceed with data matrix
if not issparse(X):
return np.log1p(X)
_log1p(data.X)
else:
return X.log1p()
_log1p(data)

return data if copy else None


def pca(data, n_comps=None, zero_center=True, svd_solver='auto', random_state=0,
Expand Down

0 comments on commit 39c234c

Please sign in to comment.