Skip to content

Commit

Permalink
Merge pull request scverse#439 from tomwhite/generalize-distributed
Browse files Browse the repository at this point in the history
Use np.asarray() rather than Zappy-specific code.
  • Loading branch information
falexwolf authored Jan 23, 2019
2 parents 32c3412 + 4f62c9e commit 2e867c4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
19 changes: 4 additions & 15 deletions scanpy/preprocessing/_distributed.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import numpy as np

# install dask if available
try:
import dask.array as da
except ImportError:
da = None

# install zappy (which wraps numpy), or fall back to plain numpy
try:
import zappy.base as np
except ImportError:
import numpy as np


def materialize_as_ndarray(a):
"""Convert distributed arrays to ndarrays."""
if type(a) in (list, tuple):
if da is not None and any(isinstance(arr, da.Array) for arr in a):
return da.compute(*a, sync=True)
elif hasattr(np, 'asarray'): # zappy case
return tuple(np.asarray(arr) for arr in a)
else:
if da is not None and isinstance(a, da.Array):
return a.compute()
elif hasattr(np, 'asarray'): # zappy case
return np.asarray(a)
return a
return tuple(np.asarray(arr) for arr in a)
return np.asarray(a)
15 changes: 5 additions & 10 deletions scanpy/preprocessing/_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Union, Optional, Tuple

import numba
import numpy as np
import scipy as sp
from scipy.sparse import issparse, isspmatrix_csr, csr_matrix, spmatrix
from sklearn.utils import sparsefuncs
Expand All @@ -24,12 +25,6 @@
except ImportError:
da = None

# install zappy (which wraps numpy), or fall back to plain numpy
try:
import zappy.base as np
except ImportError:
import numpy as np

N_PCS = 50 # default number of PCs

# backwards compat
Expand Down Expand Up @@ -204,7 +199,7 @@ def filter_genes(
`n_counts` or `n_cells` per gene.
"""
if copy:
logg.warn('`copy` is deprecated, use `inplace` instead.')
logg.warn('`copy` is deprecated, use `inplace` instead.')
n_given_options = sum(
option is not None for option in
[min_cells, min_counts, max_cells, max_counts])
Expand Down Expand Up @@ -667,7 +662,7 @@ def normalize_per_cell_weinreb16_deprecated(X, max_fraction=1,
"""
if max_fraction < 0 or max_fraction > 1:
raise ValueError('Choose max_fraction between 0 and 1.')

counts_per_cell = X.sum(1).A1 if issparse(X) else X.sum(1)
gene_subset = np.all(X <= counts_per_cell[:, None] * max_fraction, axis=0)
if issparse(X): gene_subset = gene_subset.A1
Expand Down Expand Up @@ -960,9 +955,9 @@ def downsample_cell(col: np.array, target: int, random_state: int=0,
replace: bool=True, inplace: bool=False):
"""
Evenly reduce counts in cell to target amount.
This is an internal function and has some restrictions:
* `dtype` of col must be an integer (i.e. satisfy issubclass(col.dtype.type, np.integer))
* total counts in cell must be less than target
"""
Expand Down

0 comments on commit 2e867c4

Please sign in to comment.