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

Allow csc_matrix input for rank and ttest #175

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions diffxpy/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def mann_whitney_u_test(

pvals = np.asarray([
scipy.stats.mannwhitneyu(
x=np.asarray(x0[:, i].todense()).flatten() if isinstance(x0, scipy.sparse.csr_matrix) else x0[:, i],
y=np.asarray(x1[:, i].todense()).flatten() if isinstance(x0, scipy.sparse.csr_matrix) else x1[:, i],
x=x0[:, i].toarray().flatten() if isinstance(x0, scipy.sparse.spmatrix) else x0[:, i],
y=x1[:, i].toarray().flatten() if isinstance(x0, scipy.sparse.spmatrix) else x1[:, i],
use_continuity=True,
alternative="two-sided"
).pvalue for i in range(x0.shape[1])
Expand Down
20 changes: 10 additions & 10 deletions diffxpy/testing/det.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,8 +1564,8 @@ def __init__(
x0, x1 = split_x(data, grouping)

# Only compute p-values for genes with non-zero observations and non-zero group-wise variance.
mean_x0 = np.asarray(np.mean(x0, axis=0)).flatten().astype(dtype=np.float)
mean_x1 = np.asarray(np.mean(x1, axis=0)).flatten().astype(dtype=np.float)
mean_x0 = np.asarray(np.mean(x0, axis=0, dtype=np.float)).flatten()
mean_x1 = np.asarray(np.mean(x1, axis=0, dtype=np.float)).flatten()
# Avoid unnecessary mean computation:
self._mean = np.asarray(np.average(
a=np.vstack([mean_x0, mean_x1]),
Expand All @@ -1575,13 +1575,13 @@ def __init__(
returned=False
)).flatten()
self._ave_nonzero = self._mean != 0 # omit all-zero features
if isinstance(x0, scipy.sparse.csr_matrix):
if isinstance(x0, scipy.sparse.spmatrix):
# Efficient analytic expression of variance without densification.
var_x0 = np.asarray(np.mean(x0.power(2), axis=0)).flatten().astype(dtype=np.float) - np.square(mean_x0)
var_x1 = np.asarray(np.mean(x1.power(2), axis=0)).flatten().astype(dtype=np.float) - np.square(mean_x1)
var_x0 = np.asarray(np.mean(x0.power(2, dtype=np.float), axis=0)).flatten() - np.square(mean_x0)
var_x1 = np.asarray(np.mean(x1.power(2, dtype=np.float), axis=0)).flatten() - np.square(mean_x1)
else:
var_x0 = np.asarray(np.var(x0, axis=0)).flatten().astype(dtype=np.float)
var_x1 = np.asarray(np.var(x1, axis=0)).flatten().astype(dtype=np.float)
var_x0 = np.asarray(np.var(x0, axis=0, dtype=np.float)).flatten()
var_x1 = np.asarray(np.var(x1, axis=0, dtype=np.float)).flatten()
self._var_geq_zero = np.logical_or(
var_x0 > 0,
var_x1 > 0
Expand Down Expand Up @@ -1690,8 +1690,8 @@ def __init__(

x0, x1 = split_x(data, grouping)

mean_x0 = np.asarray(np.mean(x0, axis=0)).flatten().astype(dtype=np.float)
mean_x1 = np.asarray(np.mean(x1, axis=0)).flatten().astype(dtype=np.float)
mean_x0 = np.asarray(np.mean(x0, axis=0, dtype=np.float)).flatten()
mean_x1 = np.asarray(np.mean(x1, axis=0, dtype=np.float)).flatten()
# Avoid unnecessary mean computation:
self._mean = np.asarray(np.average(
a=np.vstack([mean_x0, mean_x1]),
Expand All @@ -1700,7 +1700,7 @@ def __init__(
axis=0,
returned=False
)).flatten()
if isinstance(x0, scipy.sparse.csr_matrix):
if isinstance(x0, scipy.sparse.spmatrix):
# Efficient analytic expression of variance without densification.
var_x0 = np.asarray(np.mean(x0.power(2), axis=0)).flatten().astype(dtype=np.float) - np.square(mean_x0)
var_x1 = np.asarray(np.mean(x1.power(2), axis=0)).flatten().astype(dtype=np.float) - np.square(mean_x1)
Expand Down
30 changes: 15 additions & 15 deletions diffxpy/unit_test/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,20 @@ def simulate(self, n_cells: int = 200, n_genes: int = 2):
})
return sim.x, random_sample_description

def _test_numpy(self, sparse):
def _test_numpy(self, fmt=np.asarray):
data, sample_description = self.simulate()
gene_names = ["gene" + str(i) for i in range(data.shape[1])]
if sparse:
data = scipy.sparse.csr_matrix(data)
data = fmt(data)

self._test_wald(data=data, sample_description=sample_description, gene_names=gene_names)
self._test_lrt(data=data, sample_description=sample_description, gene_names=gene_names)
self._test_t_test(data=data, sample_description=sample_description, gene_names=gene_names)
self._test_rank(data=data, sample_description=sample_description, gene_names=gene_names)

def _test_anndata(self, sparse):
def _test_anndata(self, fmt=np.asarray):
data, sample_description = self.simulate()
gene_names = ["gene" + str(i) for i in range(data.shape[1])]
if sparse:
data = scipy.sparse.csr_matrix(data)
data = fmt(data)

data = anndata.AnnData(data)
data.var_names = gene_names
Expand All @@ -87,11 +85,10 @@ def _test_anndata(self, sparse):
self._test_t_test(data=data, sample_description=sample_description)
self._test_rank(data=data, sample_description=sample_description)

def _test_anndata_raw(self, sparse):
def _test_anndata_raw(self, fmt=np.asarray):
data, sample_description = self.simulate()
gene_names = ["gene" + str(i) for i in range(data.shape[1])]
if sparse:
data = scipy.sparse.csr_matrix(data)
data = fmt(data)

data = anndata.AnnData(data)
data.var_names = gene_names
Expand All @@ -106,8 +103,9 @@ def test_numpy(self):
logging.getLogger("batchglm").setLevel(logging.WARNING)
logging.getLogger("diffxpy").setLevel(logging.WARNING)

self._test_numpy(sparse=False)
self._test_numpy(sparse=True)
self._test_numpy(fmt=np.asarray)
self._test_numpy(fmt=scipy.sparse.csr_matrix)
self._test_numpy(fmt=scipy.sparse.csc_matrix)

return True

Expand All @@ -116,10 +114,12 @@ def test_anndata(self):
logging.getLogger("batchglm").setLevel(logging.WARNING)
logging.getLogger("diffxpy").setLevel(logging.WARNING)

self._test_anndata(sparse=False)
self._test_anndata(sparse=True)
self._test_anndata_raw(sparse=False)
self._test_anndata_raw(sparse=True)
self._test_anndata(fmt=np.asarray)
self._test_anndata(fmt=scipy.sparse.csr_matrix)
self._test_anndata(fmt=scipy.sparse.csc_matrix)
self._test_anndata_raw(fmt=np.asarray)
self._test_anndata_raw(fmt=scipy.sparse.csr_matrix)
self._test_anndata_raw(fmt=scipy.sparse.csc_matrix)

return True

Expand Down