Skip to content

Commit

Permalink
style cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jlause committed Mar 10, 2021
1 parent e23ea6c commit fc49c25
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
15 changes: 12 additions & 3 deletions scanpy/preprocessing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from ._recipes import recipe_zheng17, recipe_weinreb17, recipe_seurat, recipe_pearson_residuals
from ._recipes import (
recipe_zheng17,
recipe_weinreb17,
recipe_seurat,
recipe_pearson_residuals,
)
from ._simple import filter_cells, filter_genes
from ._deprecated.highly_variable_genes import filter_genes_dispersion
from ._highly_variable_genes import highly_variable_genes
Expand All @@ -7,6 +12,10 @@
from ._pca import pca
from ._qc import calculate_qc_metrics
from ._combat import combat
from ._normalization import normalize_total, normalize_pearson_residuals, normalize_pearson_residuals_pca
from ._normalization import (
normalize_total,
normalize_pearson_residuals,
normalize_pearson_residuals_pca,
)

from ..neighbors import neighbors
from ..neighbors import neighbors
38 changes: 18 additions & 20 deletions scanpy/preprocessing/_highly_variable_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def _highly_variable_genes_seurat_v3(
df = df.drop(['highly_variable_nbatches'], axis=1)
return df


def _highly_variable_pearson_residuals(
adata: AnnData,
layer: Optional[str] = None,
Expand All @@ -184,7 +185,7 @@ def _highly_variable_pearson_residuals(
clip: Union[Literal['auto', 'none'], float] = 'auto',
chunksize: int = 100,
subset: bool = False,
inplace: bool = True
inplace: bool = True,
) -> Optional[pd.DataFrame]:
"""\
See `highly_variable_genes`.
Expand Down Expand Up @@ -214,7 +215,7 @@ def _highly_variable_pearson_residuals(
in all batches
"""

view_to_actual(adata)
view_to_actual(adata)
X = _get_obs_rep(adata, layer=layer)
computed_on = layer if layer else 'adata.X'

Expand Down Expand Up @@ -330,8 +331,7 @@ def _highly_variable_pearson_residuals(
df = df.loc[adata.var_names]

if inplace or subset:
adata.uns['hvg'] = {'flavor': 'pearson_residuals',
'computed_on':computed_on}
adata.uns['hvg'] = {'flavor': 'pearson_residuals', 'computed_on': computed_on}
logg.hint(
'added\n'
' \'highly_variable\', boolean vector (adata.var)\n'
Expand Down Expand Up @@ -364,9 +364,7 @@ def _highly_variable_pearson_residuals(
['highly_variable_nbatches', 'highly_variable_intersection'], axis=1
)
return df





def _highly_variable_genes_single_batch(
adata: AnnData,
Expand Down Expand Up @@ -481,7 +479,6 @@ def _highly_variable_genes_single_batch(
return df



def highly_variable_genes(
adata: AnnData,
layer: Optional[str] = None,
Expand All @@ -495,7 +492,9 @@ def highly_variable_genes(
theta: float = 100,
clip: Union[Literal['auto', 'none'], float] = 'auto',
chunksize: int = 1000,
flavor: Literal['seurat', 'cell_ranger', 'seurat_v3', 'pearson_residuals'] = 'seurat',
flavor: Literal[
'seurat', 'cell_ranger', 'seurat_v3', 'pearson_residuals'
] = 'seurat',
subset: bool = False,
inplace: bool = True,
batch_key: Optional[str] = None,
Expand Down Expand Up @@ -658,21 +657,20 @@ def highly_variable_genes(
if flavor == 'pearson_residuals':
if n_top_genes is None:
raise ValueError(
"`pp.highly_variable_genes` requires the argument `n_top_genes`"
" for `flavor='pearson_residuals'`"
"`pp.highly_variable_genes` requires the argument `n_top_genes`"
" for `flavor='pearson_residuals'`"
)
return _highly_variable_pearson_residuals(
adata,
layer = layer,
n_top_genes = n_top_genes,
batch_key = batch_key,
theta = theta,
clip = clip,
chunksize= chunksize,
subset = subset,
inplace = inplace,
layer=layer,
n_top_genes=n_top_genes,
batch_key=batch_key,
theta=theta,
clip=clip,
chunksize=chunksize,
subset=subset,
inplace=inplace,
)


if batch_key is None:
df = _highly_variable_genes_single_batch(
Expand Down
25 changes: 10 additions & 15 deletions scanpy/preprocessing/_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
from scanpy.get import _get_obs_rep, _set_obs_rep





def _normalize_data(X, counts, after=None, copy=False):
X = X.copy() if copy else X
if issubclass(X.dtype.type, (int, np.integer)):
Expand Down Expand Up @@ -78,7 +75,7 @@ def normalize_pearson_residuals(
theta: float = 100,
clip: Union[Literal['auto', 'none'], float] = 'auto',
layer: Optional[str] = None,
copy: bool=False,
copy: bool = False,
inplace: bool = True,
) -> Optional[Dict[str, np.ndarray]]:
"""\
Expand Down Expand Up @@ -120,29 +117,27 @@ def normalize_pearson_residuals(
`adata.X` and `adata.layers`, depending on `inplace`.
"""

if copy:
if not inplace:
raise ValueError(
"`copy=True` cannot be used with `inplace=False`."
)
raise ValueError("`copy=True` cannot be used with `inplace=False`.")
adata = adata.copy()

view_to_actual(adata)
view_to_actual(adata)
X = _get_obs_rep(adata, layer=layer)
computed_on = layer if layer else 'adata.X'

msg = 'computing analytic Pearson residuals on %s' % computed_on
start = logg.info(msg)
residuals = _pearson_residuals(X, theta, clip, copy = ~inplace)

residuals = _pearson_residuals(X, theta, clip, copy=~inplace)
settings_dict = dict(theta=theta, clip=clip, computed_on=computed_on)

if inplace:
_set_obs_rep(adata,residuals,layer=layer)
_set_obs_rep(adata, residuals, layer=layer)
adata.uns['pearson_residuals_normalization'] = settings_dict
else:
results_dict = dict(X=residuals,**settings_dict)
results_dict = dict(X=residuals, **settings_dict)

logg.info(' finished ({time_passed})', time=start)

Expand Down Expand Up @@ -246,7 +241,7 @@ def normalize_pearson_residuals_pca(
return None
else:
return adata_pca


def normalize_total(
adata: AnnData,
Expand Down

0 comments on commit fc49c25

Please sign in to comment.