Skip to content

Commit

Permalink
Updated tests for previous additions
Browse files Browse the repository at this point in the history
  • Loading branch information
PauBadiaM committed Dec 12, 2023
1 parent 135519a commit 89a9325
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
21 changes: 18 additions & 3 deletions decoupler/tests/test_decouple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import numpy as np
import numpy.testing as npt
import pandas as pd
from anndata import AnnData
from ..decouple import get_wrappers, run_methods, parse_methods, decouple, run_consensus
Expand All @@ -10,13 +11,27 @@ def test_get_wrappers():


def test_run_methods():
m = np.array([[7., 1., 1., 1.], [4., 2., 1., 2.], [1., 2., 5., 1.], [1., 1., 6., 2.]])
m = np.array([[7., 1., 1., 1., 0.1], [4., 2., 1., 2., 0.1], [1., 2., 5., 1., 0.1], [1., 1., 6., 2., 0.1]])
r = np.array(['S1', 'S2', 'S3', 'S4'])
c = np.array(['G1', 'G2', 'G3', 'G4'])
c = np.array(['G1', 'G2', 'G3', 'G4', 'G5'])
df = pd.DataFrame(m, index=r, columns=c)
net = pd.DataFrame([['T1', 'G1', 1], ['T1', 'G2', 2], ['T2', 'G3', -3], ['T2', 'G4', 4]],
columns=['source', 'target', 'weight'])
run_methods(df, net, 'source', 'target', 'weight', ['mlm', 'ulm'], {}, 0, True, False)
run_methods(AnnData(df), net, 'source', 'target', 'weight', ['gsva'], {}, 0, False, False, False)
run_methods(df, net, 'source', 'target', 'weight', ['gsva'], {}, 0, False, False, False)
wo_args = run_methods(df, net, 'source', 'target', 'weight', ['ulm', 'aucell'], {}, 0, False, False, False)
wt_args = run_methods(df, net, 'source', 'target', 'weight', ['ulm', 'aucell'],
{'aucell': {'n_up': 3}}, 0, False, False, False)
assert np.all(wo_args['aucell_estimate'].values != wt_args['aucell_estimate'].values)
# Run dense
df.loc['S1', ['G3', 'G4']] = 0
dens = run_methods(df, net, 'source', 'target', 'weight', ['ulm', 'aucell'], {}, 0, False, False, True)
assert wt_args['ulm_estimate'].loc['S1', 'T2'] != dens['ulm_estimate'].loc['S1', 'T2']
npt.assert_allclose(wt_args['ulm_estimate'].values[1:, :], dens['ulm_estimate'].values[1:, :], rtol=1e-6)
# Check that estimate_loc works
res = run_methods(df, net, 'source', 'target', 'weight', ['ulm', 'wsum'],
{'wsum': {'estimate_loc': 3}}, 0, False, False, True)
npt.assert_allclose(res['wsum_estimate'], res['wsum_pvals'])


def test_parse_methods():
Expand Down
2 changes: 2 additions & 0 deletions decoupler/tests/test_pre.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def test_check_mat():
assert nr.size == 2
assert nc.size == 2
assert type(nm) is not csr_matrix and isinstance(nm, np.ndarray)
with pytest.raises(ValueError):
check_mat(m, r, np.array(['G1', 'G2', 'G1']))
m = csr_matrix(np.array([[1, 0, 2], [np.nan, 0, 3], [0, 0, 0]]))
with pytest.raises(ValueError):
check_mat(m, r, c)
Expand Down
12 changes: 11 additions & 1 deletion decoupler/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import pytest
import numpy as np
from scipy.sparse import csr_matrix
import numpy.testing as npt
import pandas as pd
import os
from anndata import AnnData
from ..utils import m_rename, melt, show_methods, check_corr, get_toy_data, summarize_acts
from ..utils import assign_groups, p_adjust_fdr, dense_run, shuffle_net, read_gmt
from ..method_mlm import run_mlm
from ..method_ora import run_ora
from ..method_gsva import run_gsva


def test_m_rename():
Expand All @@ -32,6 +35,7 @@ def test_melt():
melt(pvals)
melt(res_dict)
melt(res_list)
melt({estimate.name: estimate})
with pytest.raises(ValueError):
melt({0, 1, 2, 3})

Expand Down Expand Up @@ -85,20 +89,26 @@ def test_p_adjust_fdr():


def test_denserun():
mat = pd.DataFrame([[0, 2, 3, 4, 5, 6], [1, 0, 0, 0, 0, 0]], columns=['G01', 'G02', 'G03', 'G06', 'G07', 'G08'])
mat = pd.DataFrame([[0, 2, 3, 4, 5, 6], [1, 0, 0, 0, 0, 0]], index=['S1', 'S2'],
columns=['G01', 'G02', 'G03', 'G06', 'G07', 'G08'])
net = pd.DataFrame([['T1', 'G01', 1], ['T1', 'G02', 1], ['T2', 'G06', 1], ['T2', 'G07', 0.5],
['T3', 'G06', -0.5], ['T3', 'G07', -3]], columns=['source', 'target', 'weight'])
acts, _ = dense_run(run_mlm, mat, net, min_n=2, verbose=True)
assert acts.shape[1] == 2
assert not np.all(np.isnan(acts.values[0]))
assert np.all(np.isnan(acts.values[1]))
spr = AnnData(mat.astype(np.float32))
spr.X = csr_matrix(spr.X).copy()
dense_run(run_mlm, spr, net, min_n=2, verbose=True, use_raw=False)
npt.assert_allclose(spr.obsm['mlm_estimate'], acts)

mat = AnnData(mat.astype(np.float32))
dense_run(run_ora, mat, net, min_n=2, verbose=True, use_raw=False)
acts = mat.obsm['ora_estimate']
assert acts.shape[1] == 2
assert not np.all(np.isnan(acts.values[0]))
assert np.all(np.isnan(acts.values[1]))
dense_run(run_gsva, mat, net, min_n=2, verbose=True, use_raw=False)


def test_shuffle_net():
Expand Down

0 comments on commit 89a9325

Please sign in to comment.