Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getting log1p base #2546

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scanpy/preprocessing/_highly_variable_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _highly_variable_genes_single_batch(
"""
X = adata.layers[layer] if layer is not None else adata.X
if flavor == 'seurat':
if 'log1p' in adata.uns_keys() and adata.uns['log1p']['base'] is not None:
if 'log1p' in adata.uns_keys() and adata.uns['log1p'].get('base') is not None:
X *= np.log(adata.uns['log1p']['base'])
X = np.expm1(X)

Expand Down
15 changes: 15 additions & 0 deletions scanpy/tests/test_rank_genes_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ def test_emptycat():
rank_genes_groups(pbmc, groupby='louvain')


def test_log1p_save_restore(tmp_path):
"""tests the sequence log1p→save→load→rank_genes_groups"""
from anndata import read

pbmc = pbmc68k_reduced()
sc.pp.log1p(pbmc)

path = tmp_path / 'test.h5ad'
pbmc.write(path)

pbmc = read(path)

sc.tl.rank_genes_groups(pbmc, groupby='bulk_labels', use_raw=True)


def test_wilcoxon_symmetry():
pbmc = pbmc68k_reduced()

Expand Down
4 changes: 2 additions & 2 deletions scanpy/tools/_rank_genes_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(
layer=None,
comp_pts=False,
):
if 'log1p' in adata.uns_keys() and adata.uns['log1p']['base'] is not None:
if 'log1p' in adata.uns_keys() and adata.uns['log1p'].get('base') is not None:
self.expm1_func = lambda x: np.expm1(x * np.log(adata.uns['log1p']['base']))
else:
self.expm1_func = np.expm1
Expand Down Expand Up @@ -751,7 +751,7 @@ def filter_rank_genes_groups(
index=gene_names.index,
)

if 'log1p' in adata.uns_keys() and adata.uns['log1p']['base'] is not None:
if 'log1p' in adata.uns_keys() and adata.uns['log1p'].get('base') is not None:
expm1_func = lambda x: np.expm1(x * np.log(adata.uns['log1p']['base']))
else:
expm1_func = np.expm1
Expand Down