1
+ import numpy as np
2
+ import pandas as pd
3
+ from anndata import AnnData
4
+ from ..method_zscore import zscore , run_zscore
5
+
6
+ def test_zscore ():
7
+ m = np .array ([[- 7. , - 1. , 1. , 1. ], [- 4. , - 2. , 1. , 2. ], [1. , 2. , 5. , 1. ], [1. , 1. , 6. , 2. ], [- 8. , - 7. , 1. , 1. ]], dtype = np .float32 )
8
+ net = np .array ([[1. , 0. ], [1 , 0. ], [0. , - 1. ], [0. , - 1. ]], dtype = np .float32 )
9
+ act , pvl = zscore (m , net )
10
+ assert act [0 , 0 ] < 0
11
+ assert act [1 , 0 ] < 0
12
+ assert act [2 , 0 ] > 0
13
+ assert act [3 , 0 ] > 0
14
+ assert act [4 , 0 ] < 0
15
+ assert np .all ((0. <= pvl ) * (pvl <= 1. ))
16
+
17
+ act2 , pvl2 = zscore (m , net , flavor = 'KSEA' )
18
+ assert act2 [0 , 0 ] < 0
19
+ assert act2 [1 , 0 ] < 0
20
+ assert act2 [2 , 0 ] < 0
21
+ assert act2 [3 , 0 ] < 0
22
+ assert act2 [4 , 0 ] < 0
23
+ assert np .all ((0. <= pvl2 ) * (pvl2 <= 1. ))
24
+
25
+ def test_run_zscore ():
26
+ m = np .array ([[- 7. , - 1. , 1. , 1. ], [- 4. , - 2. , 1. , 2. ], [1. , 2. , 5. , 1. ], [1. , 1. , - 6. , - 8. ], [- 8. , - 7. , 1. , 1. ]])
27
+ r = np .array (['S1' , 'S2' , 'S3' , 'S4' , 'S5' ])
28
+ c = np .array (['G1' , 'G2' , 'G3' , 'G4' ])
29
+ df = pd .DataFrame (m , index = r , columns = c )
30
+ net = pd .DataFrame ([['T1' , 'G1' , 1 ], ['T1' , 'G2' , 1 ], ['T2' , 'G3' , - 1 ], ['T2' , 'G4' , - 1 ]],
31
+ columns = ['source' , 'target' , 'weight' ])
32
+ res = run_zscore (df , net , verbose = True , use_raw = False , min_n = 0 )
33
+ assert res [0 ].loc ['S1' , 'T2' ] < 0
34
+ assert res [0 ].loc ['S2' , 'T2' ] < 0
35
+ assert res [0 ].loc ['S3' , 'T2' ] < 0
36
+ assert res [0 ].loc ['S4' , 'T2' ] > 0
37
+ assert res [0 ].loc ['S5' , 'T2' ] < 0
38
+ assert res [1 ].map (lambda x : 0 <= x <= 1 ).all ().all ()
39
+
40
+ res2 = run_zscore (df , net , verbose = True , use_raw = False , min_n = 0 , flavor = 'KSEA' )
41
+ assert res2 [0 ].loc ['S1' , 'T2' ] > 0
42
+ assert res2 [0 ].loc ['S2' , 'T2' ] < 0
43
+ assert res2 [0 ].loc ['S3' , 'T2' ] < 0
44
+ assert res2 [0 ].loc ['S4' , 'T2' ] > 0
45
+ assert res2 [0 ].loc ['S5' , 'T2' ] > 0
46
+ assert res2 [1 ].map (lambda x : 0 <= x <= 1 ).all ().all ()
47
+
48
+ adata = AnnData (df .astype (np .float32 ))
49
+ run_zscore (adata , net , verbose = True , use_raw = False , min_n = 0 )
0 commit comments