Skip to content

Commit

Permalink
Backport #1592 (#1594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivirshup authored Jan 17, 2021
1 parent 6055f44 commit 12f4999
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/release-latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ Version 1.7
- Fixed bug where calling `set_figure_params` could block if IPython was installed, but not used. :pr:`1547` :smaller:`I Virshup`
- :func:`~scanpy.pl.violin` no longer fails if `.raw` not present :pr:`1548` :smaller:`I Virshup`
- :func:`~scanpy.pl.spatial` refactoring and better handling of spatial data :pr:`1512` :smaller:`G Palla`
- :func:`scanpy.pp.pca` works with `chunked=True` again :pr:`1592` :smaller:`I Virshup`
6 changes: 3 additions & 3 deletions scanpy/preprocessing/_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def pca(
Explained variance, equivalent to the eigenvalues of the
covariance matrix.
"""
start = logg.info(f'computing PCA')
logg_start = logg.info(f'computing PCA')

# chunked calculation is not randomized, anyways
if svd_solver in {'auto', 'randomized'} and not chunked:
Expand Down Expand Up @@ -239,7 +239,7 @@ def pca(
adata.varm['PCs'] = pca_.components_.T
adata.uns['pca']['variance'] = pca_.explained_variance_
adata.uns['pca']['variance_ratio'] = pca_.explained_variance_ratio_
logg.info(' finished', time=start)
logg.info(' finished', time=logg_start)
logg.debug(
'and added\n'
' \'X_pca\', the PCA coordinates (adata.obs)\n'
Expand All @@ -249,7 +249,7 @@ def pca(
)
return adata if copy else None
else:
logg.info(' finished', time=start)
logg.info(' finished', time=logg_start)
if return_info:
return (
X_pca,
Expand Down
24 changes: 24 additions & 0 deletions scanpy/tests/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,27 @@ def test_pca_reproducible(pbmc3k_normalized, array_type, float_dtype):
assert_equal(a, b)
# Test that changing random seed changes result
assert not np.array_equal(a.obsm["X_pca"], c.obsm["X_pca"])


def test_pca_chunked(pbmc3k_normalized):
# https://github.com/theislab/scanpy/issues/1590
# But also a more general test

# Subsetting for speed of test
pbmc = pbmc3k_normalized[::6].copy()
pbmc.X = pbmc.X.astype(np.float64)
chunked = sc.pp.pca(pbmc3k_normalized, chunked=True, copy=True)
default = sc.pp.pca(pbmc3k_normalized, copy=True)

# Taking absolute value since sometimes dimensions are flipped
np.testing.assert_allclose(
np.abs(chunked.obsm["X_pca"]), np.abs(default.obsm["X_pca"])
)
np.testing.assert_allclose(np.abs(chunked.varm["PCs"]), np.abs(default.varm["PCs"]))
np.testing.assert_allclose(
np.abs(chunked.uns["pca"]["variance"]), np.abs(default.uns["pca"]["variance"])
)
np.testing.assert_allclose(
np.abs(chunked.uns["pca"]["variance_ratio"]),
np.abs(default.uns["pca"]["variance_ratio"]),
)

0 comments on commit 12f4999

Please sign in to comment.