Skip to content
This repository was archived by the owner on Oct 21, 2025. It is now read-only.

Commit 062161d

Browse files
Merge pull request #104 from theislab/dev
Dev
2 parents aad102e + 3eb7dea commit 062161d

27 files changed

+1670
-645
lines changed

diffxpy/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from . import test
55
from . import enrich
6+
from . import fit
67
from . import stats
78
from . import utils
89
from .. import pkg_constants

diffxpy/api/fit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from diffxpy.fit import model, residuals, partition

diffxpy/api/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from diffxpy.testing.utils import constraint_matrix_from_string, constraint_matrix_from_dict, \
22
constraint_system_from_star
3-
from diffxpy.testing.utils import design_matrix, design_matrix_from_xarray, design_matrix_from_anndata
3+
from diffxpy.testing.utils import design_matrix
44
from diffxpy.testing.utils import view_coef_names, preview_coef_names

diffxpy/fit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .fit import model, residuals, partition

diffxpy/fit/external.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from diffxpy.testing.tests import _fit
2+
from diffxpy.testing.utils import parse_gene_names, parse_sample_description, parse_size_factors, parse_grouping, \
3+
constraint_system_from_star

diffxpy/fit/fit.py

Lines changed: 811 additions & 0 deletions
Large diffs are not rendered by default.

diffxpy/models/batch_bfgs/optim.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
class Estim_BFGS_Model():
1717

1818
def __init__(self, Estim_BFGS, nproc):
19-
self._num_observations = Estim_BFGS.X.shape[0]
20-
self._num_features = Estim_BFGS.X.shape[1]
19+
self._num_observations = Estim_BFGS.x.shape[0]
20+
self._num_features = Estim_BFGS.x.shape[1]
2121
self._features = Estim_BFGS.feature_names
22-
self._observations = Estim_BFGS.X.shape[0]
22+
self._observations = Estim_BFGS.x.shape[0]
2323
self._design_loc = Estim_BFGS.design_loc
2424
self._design_scale = Estim_BFGS.design_scale
2525
self._loss = xr.DataArray(Estim_BFGS.full_loss(nproc))
2626
self._log_probs = -self._loss
2727
self._probs = np.exp(self._log_probs)
2828
self._mles = xr.DataArray(np.transpose(Estim_BFGS.mles()))
29-
self._gradient = xr.DataArray(np.zeros([Estim_BFGS.X.shape[1]]))
29+
self._gradient = xr.DataArray(np.zeros([Estim_BFGS.x.shape[1]]))
3030
self._fisher_inv = xr.DataArray(Estim_BFGS.fisher_inv)
3131
self._idx_loc = np.arange(0, Estim_BFGS.design_loc.shape[1])
3232
self._idx_scale = np.arange(Estim_BFGS.design_loc.shape[1],

diffxpy/pkg_constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
DE_TREAT_ZEROVAR_TT_AS_SIG = True
2-
31
BATCHGLM_OPTIM_GD = False
42
BATCHGLM_OPTIM_ADAM = False
53
BATCHGLM_OPTIM_ADAGRAD = False

diffxpy/stats/stats.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import Union
2-
31
import numpy as np
42
import numpy.linalg
3+
import scipy.sparse
54
import scipy.stats
6-
import xarray as xr
5+
from typing import Union
76

87

98
def 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

4241
def 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

Comments
 (0)