From 2d22a2f50d89a6bc4807e87ccfd13db0db1d6e0d Mon Sep 17 00:00:00 2001 From: Michal Klein <46717574+michalk8@users.noreply.github.com> Date: Tue, 23 May 2023 12:13:15 +0200 Subject: [PATCH] Wrap `scanpy.pp.log1p` --- scvelo/preprocessing/utils.py | 14 ++++---------- tests/preprocessing/test_moments.py | 4 +++- tests/preprocessing/test_utils.py | 4 ++-- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/scvelo/preprocessing/utils.py b/scvelo/preprocessing/utils.py index 4ff8dfd6..232a55e5 100644 --- a/scvelo/preprocessing/utils.py +++ b/scvelo/preprocessing/utils.py @@ -5,8 +5,6 @@ from scipy.sparse import issparse from sklearn.utils import sparsefuncs -from anndata import AnnData - from scvelo import logging as logg from scvelo.core import get_initial_size, get_size, multiply, set_initial_size, sum @@ -587,14 +585,10 @@ def log1p(data, copy=False): ------- Returns or updates `adata` depending on `copy`. """ - adata = data.copy() if copy else data - X = ( - (adata.X.data if issparse(adata.X) else adata.X) - if isinstance(adata, AnnData) - else adata - ) - np.log1p(X, out=X) - return adata if copy else None + from scanpy.pp import log1p as _log1p + + res = _log1p(data, copy=copy) + return res if copy else None def filter_and_normalize( diff --git a/tests/preprocessing/test_moments.py b/tests/preprocessing/test_moments.py index 46e3e0ae..b023e4c6 100644 --- a/tests/preprocessing/test_moments.py +++ b/tests/preprocessing/test_moments.py @@ -159,9 +159,11 @@ def test_analytic_example(self, sparse: bool): ) second_order_moment_centered = get_moments(adata=adata, second_order=True) - np.testing.assert_almost_equal( + np.testing.assert_allclose( second_order_moment_centered, second_order_moment_uncentered - first_order_moment_ground_truth**2, + rtol=1e-6, + atol=1e-6, ) diff --git a/tests/preprocessing/test_utils.py b/tests/preprocessing/test_utils.py index 43e4708d..718aa2bd 100644 --- a/tests/preprocessing/test_utils.py +++ b/tests/preprocessing/test_utils.py @@ -2321,7 +2321,7 @@ def test_pancreas50obs(self, capfd, pancreas_50obs): ) ) assert pancreas_50obs.var.columns.equals(pd.Index(["gene_count_corr"])) - assert [*pancreas_50obs.uns] == ["pca", "neighbors"] + assert [*pancreas_50obs.uns] == ["log1p", "pca", "neighbors"] assert [*pancreas_50obs.obsm] == ["X_pca"] assert [*pancreas_50obs.varm] == ["PCs"] assert [*pancreas_50obs.layers] == ["spliced", "unspliced", "Ms", "Mu"] @@ -2373,7 +2373,7 @@ def test_dentategyrus50obs(self, capfd, dentategyrus_50obs): ) ) assert dentategyrus_50obs.var.columns.equals(pd.Index([])) - assert [*dentategyrus_50obs.uns] == ["pca", "neighbors"] + assert [*dentategyrus_50obs.uns] == ["log1p", "pca", "neighbors"] assert [*dentategyrus_50obs.obsm] == ["X_pca"] assert [*dentategyrus_50obs.varm] == ["PCs"] assert [*dentategyrus_50obs.layers] == [