Skip to content

Commit

Permalink
Make louvain optional for tests (scverse#2063)
Browse files Browse the repository at this point in the history
* Make louvain dep optional for CI

* Make clustering tests relying on louvain optional

* Make louvain optional in test_neighbors_key_added.py

* Make logreg rank_genes_groups tests not require louvain

* Skip paul15 paga tests if louvain not installed

* Replace louvain with leiden in pbmc3k test

* Fix doc builds by updating mpl.colorbar.ColorBarBase to mpl.colorbar.ColorBar
  • Loading branch information
ivirshup authored Dec 1, 2021
1 parent 48cc7b3 commit 4cb8a61
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- script: |
python -m pip install --upgrade pip
pip install pytest-cov wheel
pip install .[dev,test,louvain,leiden,magic,harmony,scrublet,scanorama,skmisc]
pip install .[dev,test,leiden,magic,harmony,scrublet,scanorama,skmisc]
displayName: 'Install dependencies'
- script: |
Expand Down
2 changes: 1 addition & 1 deletion scanpy/plotting/_baseplot_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def _plot_colorbar(self, color_legend_ax: Axes, normalize):
cmap = pl.get_cmap(self.cmap)
import matplotlib.colorbar

matplotlib.colorbar.ColorbarBase(
matplotlib.colorbar.Colorbar(
color_legend_ax, orientation='horizontal', cmap=cmap, norm=normalize
)

Expand Down
2 changes: 1 addition & 1 deletion scanpy/plotting/_tools/paga.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def paga(
cax
A matplotlib axes object for a potential colorbar.
cb_kwds
Keyword arguments for :class:`~matplotlib.colorbar.ColorbarBase`,
Keyword arguments for :class:`~matplotlib.colorbar.Colorbar`,
for instance, `ticks`.
add_pos
Add the positions to `adata.uns['paga']`.
Expand Down
Binary file modified scanpy/tests/notebooks/pbmc3k_images/violin_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions scanpy/tests/notebooks/test_paga_paul15_subsampled.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
from matplotlib.testing import setup
import pytest

setup()

Expand Down Expand Up @@ -40,6 +41,9 @@ def test_paga_paul15_subsampled(image_comparer, plt):

sc.pl.draw_graph(adata, color='paul15_clusters', legend_loc='on data')

# TODO: skip if louvain isn't installed, needs major rework
pytest.importorskip("louvain")

# Clustering and PAGA
sc.tl.louvain(adata, resolution=1.0)
sc.tl.paga(adata, groups='louvain')
Expand Down
18 changes: 9 additions & 9 deletions scanpy/tests/notebooks/test_pbmc3k.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,23 @@ def test_pbmc3k(image_comparer):

# Clustering the graph

sc.tl.louvain(adata)
# sc.pl.umap(adata, color=['louvain', 'CST3', 'NKG7'], show=False)
sc.tl.leiden(adata)
# sc.pl.umap(adata, color=['leiden', 'CST3', 'NKG7'], show=False)
# save_and_compare_images('umap_2')
sc.pl.scatter(adata, 'CST3', 'NKG7', color='louvain', show=False)
sc.pl.scatter(adata, 'CST3', 'NKG7', color='leiden', show=False)
save_and_compare_images('scatter_3')

# Finding marker genes

sc.tl.rank_genes_groups(adata, 'louvain')
sc.tl.rank_genes_groups(adata, 'leiden')
sc.pl.rank_genes_groups(adata, n_genes=20, sharey=False, show=False)
save_and_compare_images('rank_genes_groups_1')

sc.tl.rank_genes_groups(adata, 'louvain', method='logreg')
sc.tl.rank_genes_groups(adata, 'leiden', method='logreg')
sc.pl.rank_genes_groups(adata, n_genes=20, sharey=False, show=False)
save_and_compare_images('rank_genes_groups_2')

sc.tl.rank_genes_groups(adata, 'louvain', groups=['0'], reference='1')
sc.tl.rank_genes_groups(adata, 'leiden', groups=['0'], reference='1')
sc.pl.rank_genes_groups(adata, groups='0', n_genes=20, show=False)
save_and_compare_images('rank_genes_groups_3')

Expand All @@ -139,12 +139,12 @@ def test_pbmc3k(image_comparer):
'Dendritic cells',
'Megakaryocytes',
]
adata.rename_categories('louvain', new_cluster_names)
adata.rename_categories('leiden', new_cluster_names)

# sc.pl.umap(adata, color='louvain', legend_loc='on data', title='', frameon=False, show=False)
# sc.pl.umap(adata, color='leiden', legend_loc='on data', title='', frameon=False, show=False)
# save_and_compare_images('umap_3')

sc.pl.violin(
adata, ['CST3', 'NKG7', 'PPBP'], groupby='louvain', rotation=90, show=False
adata, ['CST3', 'NKG7', 'PPBP'], groupby='leiden', rotation=90, show=False
)
save_and_compare_images('violin_2')
7 changes: 6 additions & 1 deletion scanpy/tests/test_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def test_leiden_basic(adata_neighbors):
],
)
def test_clustering_subset(adata_neighbors, clustering, key):
if clustering == sc.tl.louvain:
pytest.importorskip("louvain")

clustering(adata_neighbors, key_added=key)

for c in adata_neighbors.obs[key].unique():
Expand Down Expand Up @@ -46,14 +49,16 @@ def test_clustering_subset(adata_neighbors, clustering, key):


def test_louvain_basic(adata_neighbors):
pytest.importorskip("louvain")

sc.tl.louvain(adata_neighbors)
sc.tl.louvain(adata_neighbors, use_weights=True)
sc.tl.louvain(adata_neighbors, use_weights=True, flavor="igraph")
sc.tl.louvain(adata_neighbors, flavor="igraph")


def test_partition_type(adata_neighbors):
import louvain
louvain = pytest.importorskip("louvain")

sc.tl.louvain(adata_neighbors, partition_type=louvain.RBERVertexPartition)
sc.tl.louvain(adata_neighbors, partition_type=louvain.SurpriseVertexPartition)
26 changes: 20 additions & 6 deletions scanpy/tests/test_neighbors_key_added.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ def test_neighbors_key_obsp(adata, field):
assert adata.uns['leiden']['params'] == adata1.uns['leiden']['params']
assert np.all(adata.obs['leiden'] == adata1.obs['leiden'])

sc.tl.louvain(adata, random_state=0)
sc.tl.louvain(adata1, random_state=0, **arg)

assert adata.uns['louvain']['params'] == adata1.uns['louvain']['params']
assert np.all(adata.obs['louvain'] == adata1.obs['louvain'])

# no obsp in umap, paga
if field == 'neighbors_key':
sc.tl.umap(adata, random_state=0)
Expand All @@ -77,3 +71,23 @@ def test_neighbors_key_obsp(adata, field):
adata.uns['paga']['connectivities_tree'].toarray(),
adata1.uns['paga']['connectivities_tree'].toarray(),
)


@pytest.mark.parametrize('field', ['neighbors_key', 'obsp'])
def test_neighbors_key_obsp_louvain(adata, field):
pytest.importorskip("louvain")
adata1 = adata.copy()

sc.pp.neighbors(adata, n_neighbors=n_neighbors, random_state=0)
sc.pp.neighbors(adata1, n_neighbors=n_neighbors, random_state=0, key_added=key)

if field == 'neighbors_key':
arg = {field: key}
else:
arg = {field: adata1.uns[key]['connectivities_key']}

sc.tl.louvain(adata, random_state=0)
sc.tl.louvain(adata1, random_state=0, **arg)

assert adata.uns['louvain']['params'] == adata1.uns['louvain']['params']
assert np.all(adata.obs['louvain'] == adata1.obs['louvain'])
39 changes: 19 additions & 20 deletions scanpy/tests/test_rank_genes_groups_logreg.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
import pytest

import numpy as np
import scanpy as sc


def test_rank_genes_groups_with_renamed_categories():
@pytest.mark.parametrize(
"method",
["t-test", "logreg"],
)
def test_rank_genes_groups_with_renamed_categories(method):
adata = sc.datasets.blobs(n_variables=4, n_centers=3, n_observations=200)
assert np.allclose(adata.X[1], [9.214668, -2.6487126, 4.2020774, 0.51076424])

sc.tl.pca(adata)
sc.pp.neighbors(adata)

for method in ['logreg', 't-test']:
sc.tl.louvain(adata)
# for method in ['logreg', 't-test']:

sc.tl.rank_genes_groups(adata, 'louvain', method=method)
assert adata.uns['rank_genes_groups']['names'].dtype.names == ('0', '1', '2')
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('3', '1', '0')
sc.tl.rank_genes_groups(adata, 'blobs', method=method)
assert adata.uns['rank_genes_groups']['names'].dtype.names == ('0', '1', '2')
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('1', '3', '0')

adata.rename_categories('louvain', ['Zero', 'One', 'Two'])
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('3', '1', '0')
adata.rename_categories('blobs', ['Zero', 'One', 'Two'])
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('1', '3', '0')

sc.tl.rank_genes_groups(adata, 'louvain', method=method)
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('3', '1', '0')
sc.tl.rank_genes_groups(adata, 'blobs', method=method)
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('1', '3', '0')
assert adata.uns['rank_genes_groups']['names'].dtype.names == ('Zero', 'One', 'Two')


def test_rank_genes_groups_with_renamed_categories_use_rep():
adata = sc.datasets.blobs(n_variables=4, n_centers=3, n_observations=200)
assert np.allclose(adata.X[1], [9.214668, -2.6487126, 4.2020774, 0.51076424])

sc.tl.pca(adata)
sc.pp.neighbors(adata)

adata.layers["to_test"] = adata.X.copy()
adata.X = adata.X[::-1, :]

sc.tl.louvain(adata)
sc.tl.rank_genes_groups(
adata, 'louvain', method='logreg', layer="to_test", use_raw=False
adata, 'blobs', method='logreg', layer="to_test", use_raw=False
)
assert adata.uns['rank_genes_groups']['names'].dtype.names == ('0', '1', '2')
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('3', '1', '0')
assert adata.uns['rank_genes_groups']['names'][0].tolist() == ('1', '3', '0')

sc.tl.rank_genes_groups(adata, 'louvain', method="logreg")
sc.tl.rank_genes_groups(adata, 'blobs', method="logreg")
assert not adata.uns['rank_genes_groups']['names'][0].tolist() == ('3', '1', '0')

0 comments on commit 4cb8a61

Please sign in to comment.