Skip to content

Commit

Permalink
Stability updates to calculate_qc_metrics
Browse files Browse the repository at this point in the history
* More useful error message if requested proportions are out of bounds
* Type stability for input to jitted function
  • Loading branch information
ivirshup committed Jan 14, 2019
1 parent 5f97c5f commit 72ae5bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions scanpy/preprocessing/_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ def top_segment_proportions(mtx, ns):
the 50th most expressed gene.
"""
# Pretty much just does dispatch
assert max(ns) <= mtx.shape[1], "Cumulative proportions can only be calculated to the number of given features"
if not (max(ns) <= mtx.shape[1] and min(ns) > 0):
raise IndexError("Positions outside range of features.")
if issparse(mtx):
if not isspmatrix_csr(mtx):
mtx = csr_matrix(mtx)
return top_segment_proportions_sparse_csr(mtx.data, mtx.indptr, ns)
return top_segment_proportions_sparse_csr(mtx.data, mtx.indptr,
np.array(ns, dtype=np.int))
else:
return top_segment_proportions_dense(mtx, ns)

Expand Down
5 changes: 5 additions & 0 deletions scanpy/tests/test_qc_metrics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pandas as pd
import pytest
import scanpy as sc
import scanpy
from scipy import sparse
Expand Down Expand Up @@ -126,3 +127,7 @@ def test_qc_metrics_percentage(): # In response to #421
sc.pp.calculate_qc_metrics(adata_dense, percent_top=(None))
sc.pp.calculate_qc_metrics(adata_dense, percent_top=[1,2,3,10])
sc.pp.calculate_qc_metrics(adata_dense, percent_top=[1])
with pytest.raises(IndexError):
sc.pp.calculate_qc_metrics(adata_dense, percent_top=[1, 2, 3, -5])
with pytest.raises(IndexError):
sc.pp.calculate_qc_metrics(adata_dense, percent_top=[20, 30, 1001])

0 comments on commit 72ae5bb

Please sign in to comment.