11import pandas as pd
22import numpy as np
3+ import scipy .sparse as sps
34import pytest
45
56import decoupler as dc
3839"""
3940
4041
42+ def test_erf (
43+ rng ,
44+ ):
45+ x = rng .normal (size = 10 )
46+ e = dc .mt ._gsva ._erf .py_func (x = x )
47+ assert isinstance (e , np .ndarray )
48+
49+ @pytest .mark .parametrize (
50+ 'k,lam' ,
51+ [
52+ [- 3 , 10 ],
53+ [0 , 5 ],
54+ [3 , 50 ],
55+ ]
56+ )
57+ def test_poisson_pmf (
58+ k ,
59+ lam ,
60+ ):
61+ p = dc .mt ._gsva ._poisson_pmf .py_func (k = k , lam = lam )
62+ assert isinstance (p , float )
63+
64+
65+ def test_ecdf (
66+ rng
67+ ):
68+ arr = rng .normal (size = 10 )
69+ e = dc .mt ._gsva ._ecdf .py_func (arr )
70+ assert isinstance (e , np .ndarray )
71+
72+
73+ def test_mat_ecdf (
74+ rng
75+ ):
76+ arr = rng .normal (size = (5 , 10 ))
77+ e = dc .mt ._gsva ._mat_ecdf .py_func (arr )
78+ assert isinstance (e , np .ndarray )
79+
80+
81+ @pytest .mark .parametrize ('gauss' , [True , False ])
82+ def test_col_d (
83+ rng ,
84+ gauss ,
85+ ):
86+ x = rng .normal (loc = 5 , size = 20 )
87+ pre_cdf = dc .mt ._gsva ._init_cdfs ()
88+ arr = dc .mt ._gsva ._col_d .py_func (
89+ x = x ,
90+ gauss = gauss ,
91+ pre_cdf = pre_cdf
92+ )
93+ assert isinstance (arr , np .ndarray )
94+
95+
96+ @pytest .mark .parametrize ('gauss' , [True , False ])
97+ def test_mat_d (
98+ rng ,
99+ gauss ,
100+ ):
101+ x = rng .normal (loc = 5 , size = (5 , 15 ))
102+ d = dc .mt ._gsva ._mat_d .py_func (mat = x , gauss = gauss )
103+ assert isinstance (d , np .ndarray )
104+
105+
106+ def test_dos_srs (
107+ rng ,
108+ ):
109+ r = np .array (15 )
110+ rng .shuffle (r )
111+ dos , srs = dc .mt ._gsva ._dos_srs .py_func (r = r )
112+ assert isinstance (dos , np .ndarray )
113+ assert isinstance (srs , np .ndarray )
114+
115+
116+ def test_rankmat (
117+ rng ,
118+ ):
119+ mat = rng .normal (size = (5 , 15 ))
120+ dos_mat , srs_mat = dc .mt ._gsva ._rankmat .py_func (mat = mat )
121+ assert isinstance (dos_mat , np .ndarray )
122+ assert isinstance (srs_mat , np .ndarray )
123+
124+
125+ @pytest .mark .parametrize (
126+ "gsetidx, decordstat, symrnkstat, n, tau" ,
127+ [
128+ (np .array ([1 , 3 ]), np .array ([3 , 1 , 2 , 4 ]), np .array ([0.9 , 0.1 , 0.8 , 0.2 ]), 4 , 1.0 ),
129+ (np .array ([2 , 4 ]), np .array ([1 , 3 , 2 , 4 ]), np .array ([0.5 , 0.4 , 0.6 , 0.3 ]), 4 , 2.0 ),
130+ (np .array ([1 ]), np .array ([2 , 1 , 3 ]), np .array ([1.0 , 0.5 , 0.2 ]), 3 , 0.5 ),
131+ ]
132+ )
133+ def test_rnd_walk (gsetidx , decordstat , symrnkstat , n , tau ):
134+ k = len (gsetidx )
135+ pos , neg = dc .mt ._gsva ._rnd_walk .py_func (
136+ gsetidx = gsetidx ,
137+ k = k ,
138+ decordstat = decordstat ,
139+ symrnkstat = symrnkstat ,
140+ n = n ,
141+ tau = tau ,
142+ )
143+ assert isinstance (pos , float )
144+ assert isinstance (neg , float )
145+ assert - 1.0 <= neg <= 1.0
146+ assert - 1.0 <= pos <= 1.0
147+
148+
149+ @pytest .mark .parametrize (
150+ "gsetidx, generanking, rankstat, maxdiff, absrnk, tau, expected_range" ,
151+ [
152+ (np .array ([1 , 3 ]), np .array ([3 , 1 , 2 , 4 ]), np .array ([0.9 , 0.1 , 0.8 , 0.2 ]), True , True , 1.0 , (0.0 , 2.0 )),
153+ (np .array ([2 , 4 ]), np .array ([1 , 3 , 2 , 4 ]), np .array ([0.5 , 0.4 , 0.6 , 0.3 ]), True , False , 2.0 , (- 2.0 , 2.0 )),
154+ (np .array ([1 ]), np .array ([2 , 1 , 3 ]), np .array ([1.0 , 0.5 , 0.2 ]), False , True , 0.5 , (- 1.0 , 1.0 )),
155+ ]
156+ )
157+ def test_score_geneset (gsetidx , generanking , rankstat , maxdiff , absrnk , tau , expected_range ):
158+ es = dc .mt ._gsva ._score_geneset .py_func (gsetidx , generanking , rankstat , maxdiff , absrnk , tau )
159+ assert isinstance (es , float )
160+ assert expected_range [0 ] <= es <= expected_range [1 ]
161+
162+
41163def test_init_cdfs ():
42164 cdfs = dc .mt ._gsva ._init_cdfs .py_func ()
43165 assert np .min (cdfs ) == 0.5
@@ -150,6 +272,7 @@ def test_func_gsva(
150272 X = np .vstack ((X [:2 , :], X [- 2 :, :]))
151273 if kcdf == 'poisson' :
152274 X = X .round ()
275+ X = sps .csr_matrix (X )
153276 dc_es , _ = dc .mt ._gsva ._func_gsva (
154277 mat = X ,
155278 cnct = cnct ,
@@ -159,6 +282,4 @@ def test_func_gsva(
159282 maxdiff = maxdiff ,
160283 absrnk = absrnk ,
161284 )
162- print (dc_es )
163- print (gv_es )
164285 assert np .isclose (dc_es , gv_es ).all ()
0 commit comments