Skip to content

Commit 174b060

Browse files
committed
refactored addFluxBound 2X speedup using fbexists
1 parent c5d680b commit 174b060

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/CBModel.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: [email protected]
22-
Last edit: $Author: bgoli $ ($Id: CBModel.py 690 2019-07-09 20:00:41Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBModel.py 691 2019-07-26 12:09:03Z bgoli $)
2323
2424
"""
2525
## gets rid of "invalid variable name" info
@@ -1020,6 +1020,17 @@ def addFluxBound(self, fluxbound, fbexists=None):
10201020
Add an instantiated FluxBound object to the FBA model
10211021
10221022
- *fluxbound* an instance of the FluxBound class
1023+
- *fbexists* [default=None] this is a list of strings which contains fluxbounds that have been added to the model, see sample code below.
1024+
The format of the string is 'reactionid_boundtype'
1025+
1026+
```python
1027+
fbexists = []
1028+
for fluxbound in list_of_fluxbounds:
1029+
model.addFluxBound(c_, fbexists=fbexists)
1030+
fbexists.append("{}_{}".format(fluxbound.getReactionId(), fluxbound.getType()))
1031+
```
1032+
1033+
Using the fbexists list drastically reduces the time it takes to add fluxbounds
10231034
10241035
"""
10251036
assert type(fluxbound) == FluxBound, '\nERROR: requires a FluxBound object, not something of type {}'.format(type(fluxbound))
@@ -1046,7 +1057,7 @@ def addFluxBound(self, fluxbound, fbexists=None):
10461057
else:
10471058
print('\"{}\" FluxBound for reaction {} exists, skipping'.format(fluxbound.is_bound, fluxbound.reaction))
10481059
else:
1049-
if not (fluxbound.getReactionId(), fluxbound.getType()) in fbexists:
1060+
if "{}_{}".format(fluxbound.getReactionId(), fluxbound.getType()) not in fbexists:
10501061
fluxbound.__objref__ = weakref.ref(self)
10511062
self.flux_bounds.append(fluxbound)
10521063
else:

src/CBXML.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: [email protected]
22-
Last edit: $Author: bgoli $ ($Id: CBXML.py 685 2019-06-17 15:54:33Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBXML.py 691 2019-07-26 12:09:03Z bgoli $)
2323
2424
"""
2525
## gets rid of "invalid variable name" info
@@ -3662,6 +3662,7 @@ def __init__(self, pid):
36623662
ubcntr += 1
36633663
# print 'Added new upper bound', newId
36643664
elif HAVE_FBC and FBCver == 2:
3665+
timeFBV2 = time.time()
36653666
for bnd in FB_data:
36663667
FB = CBModel.FluxBound(bnd['id'], bnd['reaction'], bnd['operation'], bnd['value'])
36673668
FB.annotation = bnd['annotation']
@@ -3672,6 +3673,7 @@ def __init__(self, pid):
36723673
if FB.name == None or FB.name == '':
36733674
FB.name = bnd['parameter']
36743675
CONSTR.append(FB)
3676+
print('FluxBounds process1: {}'.format(round(time.time() - timeFBV2, 3)))
36753677

36763678
if DEBUG: print('FluxBounds process: {}'.format(round(time.time() - time0, 3)))
36773679
time0 = time.time()
@@ -3801,9 +3803,11 @@ def __init__(self, pid):
38013803
if DEBUG: print('Reaction build: {}'.format(round(time.time() - time0, 3)))
38023804
time0 = time.time()
38033805
fbexists = []
3806+
timeFBV2 = time.time()
38043807
for c_ in CONSTR:
38053808
fm.addFluxBound(c_, fbexists=fbexists)
3806-
fbexists.append((c_.getReactionId(), c_.getType()))
3809+
fbexists.append("{}_{}".format(c_.getReactionId(), c_.getType()))
3810+
print('FluxBounds process2: {}'.format(round(time.time() - timeFBV2, 3)))
38073811
del fbexists
38083812
if DEBUG: print('FluxBound build: {}'.format(round(time.time() - time0, 3)))
38093813
time0 = time.time()

0 commit comments

Comments
 (0)