-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdefault_inference.py
More file actions
263 lines (241 loc) · 8.76 KB
/
default_inference.py
File metadata and controls
263 lines (241 loc) · 8.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from typing import Literal
from typing import Optional
from typing import Tuple
import numpy as np
import pandas as pd
from joblib import Parallel # type: ignore
from joblib import delayed
from joblib import parallel_backend
from scipy.optimize import minimize # type: ignore
from pydeseq2 import inference
from pydeseq2 import utils
class DefaultInference(inference.Inference):
"""Default DESeq2-related inference methods, using scipy/sklearn/numpy.
This object contains the interface to the default inference routines and uses
joblib internally for parallelization. Inherit this class or its parent to write
custom inference routines.
Parameters
----------
joblib_verbosity : int
The verbosity level for joblib tasks. The higher the value, the more updates
are reported. (default: ``0``).
batch_size : int
Number of tasks to allocate to each joblib parallel worker. (default: ``128``).
n_cpus : int
Number of cpus to use. If None, all available cpus will be used.
(default: ``None``).
backend : str
Joblib backend.
"""
fit_rough_dispersions = staticmethod(utils.fit_rough_dispersions) # type: ignore
fit_moments_dispersions = staticmethod(utils.fit_moments_dispersions) # type: ignore
def __init__(
self,
joblib_verbosity: int = 0,
batch_size: int = 128,
n_cpus: Optional[int] = None,
backend: str = "loky",
):
self._joblib_verbosity = joblib_verbosity
self._batch_size = batch_size
self._n_cpus = utils.get_num_processes(n_cpus)
self._backend = backend
@property
def n_cpus(self) -> int: # noqa: D102
return self._n_cpus
@n_cpus.setter
def n_cpus(self, n_cpus: int) -> None:
self._n_cpus = utils.get_num_processes(n_cpus)
def lin_reg_mu( # noqa: D102
self,
counts: np.ndarray,
size_factors: np.ndarray,
design_matrix: np.ndarray,
min_mu: float,
) -> np.ndarray:
with parallel_backend(self._backend, inner_max_num_threads=1):
mu_hat_ = np.array(
Parallel(
n_jobs=self.n_cpus,
verbose=self._joblib_verbosity,
batch_size=self._batch_size,
)(
delayed(utils.fit_lin_mu)(
counts=counts[:, i],
size_factors=size_factors,
design_matrix=design_matrix,
min_mu=min_mu,
)
for i in range(counts.shape[1])
)
)
return mu_hat_.T
def irls( # noqa: D102
self,
counts: np.ndarray,
size_factors: np.ndarray,
design_matrix: np.ndarray,
disp: np.ndarray,
min_mu: float,
beta_tol: float,
min_beta: float = -30,
max_beta: float = 30,
optimizer: Literal["BFGS", "L-BFGS-B"] = "L-BFGS-B",
maxiter: int = 250,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
with parallel_backend(self._backend, inner_max_num_threads=1):
res = Parallel(
n_jobs=self.n_cpus,
verbose=self._joblib_verbosity,
batch_size=self._batch_size,
)(
delayed(utils.irls_solver)(
counts=counts[:, i],
size_factors=size_factors,
design_matrix=design_matrix,
disp=disp[i],
min_mu=min_mu,
beta_tol=beta_tol,
min_beta=min_beta,
max_beta=max_beta,
optimizer=optimizer,
maxiter=maxiter,
)
for i in range(counts.shape[1])
)
res = zip(*res)
MLE_lfcs_, mu_hat_, hat_diagonals_, converged_ = (np.array(m) for m in res)
return (
MLE_lfcs_,
mu_hat_.T,
hat_diagonals_.T,
converged_,
)
def alpha_mle( # noqa: D102
self,
counts: np.ndarray,
design_matrix: np.ndarray,
mu: np.ndarray,
alpha_hat: np.ndarray,
min_disp: float,
max_disp: float,
prior_disp_var: Optional[float] = None,
cr_reg: bool = True,
prior_reg: bool = False,
optimizer: Literal["BFGS", "L-BFGS-B"] = "L-BFGS-B",
) -> Tuple[np.ndarray, np.ndarray]:
with parallel_backend(self._backend, inner_max_num_threads=1):
res = Parallel(
n_jobs=self.n_cpus,
verbose=self._joblib_verbosity,
batch_size=self._batch_size,
)(
delayed(utils.fit_alpha_mle)(
counts=counts[:, i],
design_matrix=design_matrix,
mu=mu[:, i],
alpha_hat=alpha_hat[i],
min_disp=min_disp,
max_disp=max_disp,
prior_disp_var=prior_disp_var,
cr_reg=cr_reg,
prior_reg=prior_reg,
optimizer=optimizer,
)
for i in range(counts.shape[1])
)
res = zip(*res)
dispersions_, l_bfgs_b_converged_ = (np.array(m) for m in res)
return dispersions_, l_bfgs_b_converged_
def wald_test( # noqa: D102
self,
design_matrix: np.ndarray,
disp: np.ndarray,
lfc: np.ndarray,
mu: np.ndarray,
ridge_factor: np.ndarray,
contrast: np.ndarray,
lfc_null: np.ndarray,
alt_hypothesis: Optional[
Literal["greaterAbs", "lessAbs", "greater", "less"]
] = None,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
num_genes = mu.shape[1]
with parallel_backend(self._backend, inner_max_num_threads=1):
res = Parallel(
n_jobs=self.n_cpus,
verbose=self._joblib_verbosity,
batch_size=self._batch_size,
)(
delayed(utils.wald_test)(
design_matrix=design_matrix,
disp=disp[i],
lfc=lfc[i],
mu=mu[:, i],
ridge_factor=ridge_factor,
contrast=contrast,
lfc_null=lfc_null, # Convert log2 to natural log
alt_hypothesis=alt_hypothesis,
)
for i in range(num_genes)
)
res = zip(*res)
pvals, stats, se = (np.array(m) for m in res)
return pvals, stats, se
def dispersion_trend_gamma_glm( # noqa: D102
self, covariates: pd.Series, targets: pd.Series
) -> Tuple[np.ndarray, np.ndarray, bool]:
covariates_w_intercept = covariates.to_frame()
covariates_w_intercept.insert(0, "intercept", 1)
covariates_fit = covariates_w_intercept.values
targets_fit = targets.values
def loss(coeffs):
mu = covariates_fit @ coeffs
return np.nanmean(targets_fit / mu + np.log(mu), axis=0)
def grad(coeffs):
mu = covariates_fit @ coeffs
return -np.nanmean(
((targets_fit / mu - 1)[:, None] * covariates_fit) / mu[:, None], axis=0
)
res = minimize(
loss,
x0=np.array([1.0, 1.0]),
jac=grad,
method="L-BFGS-B",
bounds=[(0, np.inf)],
)
coeffs = res.x
return coeffs, covariates_fit @ coeffs, res.success
def lfc_shrink_nbinom_glm( # noqa: D102
self,
design_matrix: np.ndarray,
counts: np.ndarray,
size: np.ndarray,
offset: np.ndarray,
prior_no_shrink_scale: float,
prior_scale: float,
optimizer: str,
shrink_index: int,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
with parallel_backend(self._backend, inner_max_num_threads=1):
num_genes = counts.shape[1]
res = Parallel(
n_jobs=self.n_cpus,
verbose=self._joblib_verbosity,
batch_size=self._batch_size,
)(
delayed(utils.nbinomGLM)(
design_matrix=design_matrix,
counts=counts[:, i],
size=size[i],
offset=offset,
prior_no_shrink_scale=prior_no_shrink_scale,
prior_scale=prior_scale,
optimizer=optimizer,
shrink_index=shrink_index,
)
for i in range(num_genes)
)
res = zip(*res)
lfcs, inv_hessians, l_bfgs_b_converged_ = (np.array(m) for m in res)
return lfcs, inv_hessians, l_bfgs_b_converged_