1- from typing import Union
2-
31import numpy as np
42import numpy .linalg
3+ import scipy .sparse
54import scipy .stats
6- import xarray as xr
5+ from typing import Union
76
87
98def likelihood_ratio_test (
@@ -34,42 +33,40 @@ def likelihood_ratio_test(
3433 delta_df = df_full - df_reduced
3534 # Compute the deviance test statistic.
3635 delta_dev = 2 * (ll_full - ll_reduced )
37- # Compute the p-values based on the deviance and its expection based on the chi-square distribution.
36+ # Compute the p-values based on the deviance and its expectation based on the chi-square distribution.
3837 pvals = 1 - scipy .stats .chi2 (delta_df ).cdf (delta_dev )
3938 return pvals
4039
4140
4241def mann_whitney_u_test (
43- x0 : np .ndarray ,
44- x1 : np .ndarray ,
42+ x0 : Union [ np .ndarray , scipy . sparse . csr_matrix ] ,
43+ x1 : Union [ np .ndarray , scipy . sparse . csr_matrix ]
4544):
4645 """
47- Perform Wilcoxon rank sum test (Mann-Whitney U test) along second axis
48- (for each gene).
46+ Perform Wilcoxon rank sum test (Mann-Whitney U test) along second axis, ie. for each gene.
4947
5048 The Wilcoxon rank sum test is a non-parameteric test
5149 to compare two groups of observations.
5250
53- :param x0: np.array (observations x genes)
51+ :param x0: (observations x genes)
5452 Observations in first group by gene
55- :param x1: np.array (observations x genes)
53+ :param x1: (observations x genes)
5654 Observations in second group by gene.
5755 """
5856 axis = 1
5957 if np .any (np .ndim (x0 ) != np .ndim (x1 )):
60- raise ValueError ('stats.wilcoxon(): number of dimensions is not allowed to differ between x0 and x1! ' )
58+ raise ValueError ('number of dimensions is not allowed to differ between x0 and x1' )
6159 # Reshape into 2D array if only one test is performed.
6260 if np .ndim (x0 ) == 1 :
6361 x0 = x0 .reshape ([x0 .shape [0 ], 1 ])
6462 x1 = x1 .reshape ([x1 .shape [0 ], 1 ])
6563 if np .any (x0 .shape [axis ] != x1 .shape [axis ]):
66- raise ValueError (
67- 'stats.wilcoxon(): the first axis (number of tests) is not allowed to differ between x0 and x1!' )
64+ raise ValueError ('the first axis (number of tests) is not allowed to differ between x0 and x1' )
6865
6966 pvals = np .asarray ([
7067 scipy .stats .mannwhitneyu (
71- x = x0 [:, i ].flatten (),
72- y = x1 [:, i ].flatten (),
68+ x = np . asarray ( x0 [:, i ].todense ()). flatten () if isinstance ( x0 , scipy . sparse . csr_matrix ) else x0 [:, i ] ,
69+ y = np . asarray ( x1 [:, i ].todense ()). flatten () if isinstance ( x0 , scipy . sparse . csr_matrix ) else x1 [:, i ] ,
7370 use_continuity = True ,
7471 alternative = "two-sided"
7572 ).pvalue for i in range (x0 .shape [1 ])
@@ -252,6 +249,8 @@ def wald_test_chisq(
252249 if theta_mle .shape [0 ] != theta_covar .shape [1 ]:
253250 raise ValueError (
254251 'stats.wald_test(): theta_mle and theta_covar have to contain the same number of parameters' )
252+ if len (theta_covar .shape ) != 3 :
253+ raise ValueError ('stats.wald_test(): theta_covar should have 3 dimensions but has %i' % len (theta_covar .shape ))
255254 if theta_mle .shape [1 ] != theta_covar .shape [0 ]:
256255 raise ValueError ('stats.wald_test(): theta_mle and theta_covar have to contain the same number of genes' )
257256 if theta_covar .shape [1 ] != theta_covar .shape [2 ]:
@@ -264,8 +263,6 @@ def wald_test_chisq(
264263
265264 theta_diff = theta_mle - theta0
266265 # Convert to nd.array to avoid gufunc error.
267- if isinstance (theta_diff , xr .DataArray ):
268- theta_diff = theta_diff .values
269266 wald_statistic = np .array ([
270267 np .matmul (
271268 np .matmul (
0 commit comments