2
2
# dingo is part of GeomScale project
3
3
4
4
# Copyright (c) 2021 Apostolos Chalkis
5
+ # Copyright (c) 2024 Ke Shi
5
6
6
7
# Licensed under GNU LGPL.3, see LICENCE file
7
8
10
11
import warnings
11
12
import math
12
13
from dingo .MetabolicNetwork import MetabolicNetwork
13
- from dingo .fva import slow_fva
14
14
from dingo .utils import (
15
15
map_samples_to_steady_states ,
16
16
get_matrices_of_low_dim_polytope ,
17
17
get_matrices_of_full_dim_polytope ,
18
18
)
19
19
20
- try :
21
- import gurobipy
22
- from dingo .gurobi_based_implementations import (
23
- fast_fba ,
24
- fast_fva ,
25
- fast_inner_ball ,
26
- fast_remove_redundant_facets ,
27
- )
28
- except ImportError as e :
29
- pass
20
+ from dingo .pyoptinterface_based_impl import fba ,fva ,inner_ball ,remove_redundant_facets
30
21
31
22
from volestipy import HPolytope
32
23
@@ -53,14 +44,8 @@ def __init__(self, metabol_net):
53
44
self ._parameters ["first_run_of_mmcs" ] = True
54
45
self ._parameters ["remove_redundant_facets" ] = True
55
46
56
- try :
57
- import gurobipy
58
-
59
- self ._parameters ["fast_computations" ] = True
60
- self ._parameters ["tol" ] = 1e-06
61
- except ImportError as e :
62
- self ._parameters ["fast_computations" ] = False
63
- self ._parameters ["tol" ] = 1e-03
47
+ self ._parameters ["tol" ] = 1e-06
48
+ self ._parameters ["solver" ] = None
64
49
65
50
def get_polytope (self ):
66
51
"""A member function to derive the corresponding full dimensional polytope
@@ -82,24 +67,18 @@ def get_polytope(self):
82
67
) = self ._metabolic_network .fba ()
83
68
84
69
if (
85
- self ._parameters ["fast_computations" ]
86
- and self ._parameters ["remove_redundant_facets" ]
70
+ self ._parameters ["remove_redundant_facets" ]
87
71
):
88
72
89
- A , b , Aeq , beq = fast_remove_redundant_facets (
73
+ A , b , Aeq , beq = remove_redundant_facets (
90
74
self ._metabolic_network .lb ,
91
75
self ._metabolic_network .ub ,
92
76
self ._metabolic_network .S ,
93
77
self ._metabolic_network .objective_function ,
94
78
self ._parameters ["opt_percentage" ],
79
+ self ._parameters ["solver" ],
95
80
)
96
81
else :
97
- if (not self ._parameters ["fast_computations" ]) and self ._parameters [
98
- "remove_redundant_facets"
99
- ]:
100
- warnings .warn (
101
- "We continue without redundancy removal (slow mode is ON)"
102
- )
103
82
104
83
(
105
84
min_fluxes ,
@@ -162,14 +141,9 @@ def generate_steady_states(
162
141
163
142
P = HPolytope (self ._A , self ._b )
164
143
165
- if self ._parameters ["fast_computations" ]:
166
- self ._A , self ._b , Tr , Tr_shift , samples = P .fast_mmcs (
167
- ess , psrf , parallel_mmcs , num_threads
168
- )
169
- else :
170
- self ._A , self ._b , Tr , Tr_shift , samples = P .slow_mmcs (
171
- ess , psrf , parallel_mmcs , num_threads
172
- )
144
+ self ._A , self ._b , Tr , Tr_shift , samples = P .mmcs (
145
+ ess , psrf , parallel_mmcs , num_threads , self ._parameters ["solver" ]
146
+ )
173
147
174
148
if self ._parameters ["first_run_of_mmcs" ]:
175
149
steady_states = map_samples_to_steady_states (
@@ -207,7 +181,7 @@ def generate_steady_states_no_multiphase(
207
181
else :
208
182
bias_vector = bias_vector .astype ('float64' )
209
183
210
- samples = P .generate_samples (method , n , burn_in , thinning , self ._parameters ["fast_computations" ], variance , bias_vector )
184
+ samples = P .generate_samples (method , n , burn_in , thinning , variance , bias_vector , self ._parameters ["solver" ] )
211
185
samples_T = samples .T
212
186
213
187
steady_states = map_samples_to_steady_states (
@@ -218,7 +192,7 @@ def generate_steady_states_no_multiphase(
218
192
219
193
@staticmethod
220
194
def sample_from_polytope (
221
- A , b , ess = 1000 , psrf = False , parallel_mmcs = False , num_threads = 1
195
+ A , b , ess = 1000 , psrf = False , parallel_mmcs = False , num_threads = 1 , solver = None
222
196
):
223
197
"""A static function to sample from a full dimensional polytope.
224
198
@@ -233,22 +207,16 @@ def sample_from_polytope(
233
207
234
208
P = HPolytope (A , b )
235
209
236
- try :
237
- import gurobipy
210
+ A , b , Tr , Tr_shift , samples = P .mmcs (
211
+ ess , psrf , parallel_mmcs , num_threads , solver
212
+ )
238
213
239
- A , b , Tr , Tr_shift , samples = P .fast_mmcs (
240
- ess , psrf , parallel_mmcs , num_threads
241
- )
242
- except ImportError as e :
243
- A , b , Tr , Tr_shift , samples = P .slow_mmcs (
244
- ess , psrf , parallel_mmcs , num_threads
245
- )
246
214
247
215
return samples
248
216
249
217
@staticmethod
250
218
def sample_from_polytope_no_multiphase (
251
- A , b , method = 'billiard_walk' , n = 1000 , burn_in = 0 , thinning = 1 , variance = 1.0 , bias_vector = None
219
+ A , b , method = 'billiard_walk' , n = 1000 , burn_in = 0 , thinning = 1 , variance = 1.0 , bias_vector = None , solver = None
252
220
):
253
221
"""A static function to sample from a full dimensional polytope with an MCMC method.
254
222
@@ -267,25 +235,17 @@ def sample_from_polytope_no_multiphase(
267
235
268
236
P = HPolytope (A , b )
269
237
270
- try :
271
- import gurobipy
272
- samples = P .generate_samples (method , n , burn_in , thinning , True , variance , bias_vector )
273
- except ImportError as e :
274
- samples = P .generate_samples (method , n , burn_in , thinning , False , variance , bias_vector )
238
+ samples = P .generate_samples (method , n , burn_in , thinning , variance , bias_vector , solver )
275
239
276
240
samples_T = samples .T
277
241
return samples_T
278
242
279
243
@staticmethod
280
244
def round_polytope (
281
- A , b , method = "john_position"
245
+ A , b , method = "john_position" , solver = None
282
246
):
283
247
P = HPolytope (A , b )
284
- try :
285
- import gurobipy
286
- A , b , Tr , Tr_shift , round_value = P .rounding (method , True )
287
- except ImportError as e :
288
- A , b , Tr , Tr_shift , round_value = P .rounding (method , False )
248
+ A , b , Tr , Tr_shift , round_value = P .rounding (method , solver )
289
249
290
250
return A , b , Tr , Tr_shift
291
251
@@ -301,6 +261,7 @@ def sample_from_fva_output(
301
261
psrf = False ,
302
262
parallel_mmcs = False ,
303
263
num_threads = 1 ,
264
+ solver = None
304
265
):
305
266
"""A static function to sample steady states when the output of FVA is given.
306
267
@@ -335,16 +296,9 @@ def sample_from_fva_output(
335
296
336
297
P = HPolytope (A , b )
337
298
338
- try :
339
- import gurobipy
340
-
341
- A , b , Tr , Tr_shift , samples = P .fast_mmcs (
342
- ess , psrf , parallel_mmcs , num_threads
343
- )
344
- except ImportError as e :
345
- A , b , Tr , Tr_shift , samples = P .slow_mmcs (
346
- ess , psrf , parallel_mmcs , num_threads
347
- )
299
+ A , b , Tr , Tr_shift , samples = P .mmcs (
300
+ ess , psrf , parallel_mmcs , num_threads , solver
301
+ )
348
302
349
303
steady_states = map_samples_to_steady_states (samples , N , N_shift )
350
304
@@ -381,20 +335,8 @@ def metabolic_network(self):
381
335
def facet_redundancy_removal (self , value ):
382
336
self ._parameters ["remove_redundant_facets" ] = value
383
337
384
- if (not self ._parameters ["fast_computations" ]) and value :
385
- warnings .warn (
386
- "Since you are in slow mode the redundancy removal step is skipped (dingo does not currently support this functionality in slow mode)"
387
- )
388
-
389
- def set_fast_mode (self ):
390
-
391
- self ._parameters ["fast_computations" ] = True
392
- self ._parameters ["tol" ] = 1e-06
393
-
394
- def set_slow_mode (self ):
395
-
396
- self ._parameters ["fast_computations" ] = False
397
- self ._parameters ["tol" ] = 1e-03
338
+ def set_solver (self , solver ):
339
+ self ._parameters ["solver" ] = solver
398
340
399
341
def set_distribution (self , value ):
400
342
0 commit comments