Skip to content

Commit

Permalink
Additional summary test improvements (#278)
Browse files Browse the repository at this point in the history
This commit adds tabulate as a requirement for summary methods tests. It
also uses a pickled pFBA solution for the textbook model to avoid
raising errors on different solvers.
  • Loading branch information
pstjohn authored and hredestig committed Sep 23, 2016
1 parent 1d1ab7f commit 64d7674
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Binary file added cobra/test/data/textbook_solution.pickle
Binary file not shown.
5 changes: 5 additions & 0 deletions cobra/test/data/update_pickles.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@
clean_result[key] = {k: round(v, 5) for k, v in fva_result[key].items()}
with open("textbook_fva.json", "w") as outfile:
json_dump(clean_result, outfile)

# textbook solution
cobra.flux_analysis.parsimonious.optimize_minimal_flux(textbook)
with open('textbook_solution.pickle', 'wb') as f:
dump(textbook.solution, f, protocol=2)
36 changes: 26 additions & 10 deletions cobra/test/flux_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from os import name
from json import load
from contextlib import contextmanager
import pickle
import re

from six import iteritems, StringIO
Expand All @@ -22,6 +23,10 @@
import pandas
except:
pandas = None
try:
import tabulate
except:
tabulate = None

if __name__ == "__main__":
sys.path.insert(0, "../..")
Expand Down Expand Up @@ -335,14 +340,20 @@ def check_entries(self, out, desired_entries):
for item in desired_entries:
self.assertIn(re.sub('\s', '', item), output_set)

@skipIf(pandas is None, "summary methods require pandas")
@skipIf((pandas is None) or (tabulate is None),
"summary methods require pandas and tabulate")
def test_summary_methods(self):

# Test model summary methods
model = create_test_model("textbook")
with self.assertRaises(Exception):
model.summary()
model.optimize()

# Load model solution
with open(join(data_directory, "textbook_solution.pickle"),
"rb") as infile:
model.solution = pickle.load(infile)

desired_entries = [
'idFluxRangeidFluxRangeBiomass_Ecol...0.874',
'o2_e 21.8 [19.9, 23.7]'
Expand Down Expand Up @@ -380,11 +391,13 @@ def test_summary_methods(self):
]
# Need to use a different method here because
# there are multiple entries per line.
with captured_output() as (out, err):
model.summary()
s = out.getvalue()
for i in desired_entries:
self.assertIn(i, s)
for solver in solver_dict:
with captured_output() as (out, err):
model.summary()

s = out.getvalue()
for i in desired_entries:
self.assertIn(i, s)

# Test metabolite summary methods
desired_entries = [
Expand All @@ -398,9 +411,11 @@ def test_summary_methods(self):
'4.0 h_c + nadh_c + q8_c --> 3.0 h_e + nad_c + q...',
'12% 5.06 SUCDi q8_c + succ_c --> fum_c + q8h2_c',
]
with captured_output() as (out, err):
model.metabolites.q8_c.summary()
self.check_entries(out, desired_entries)

for solver in solver_dict:
with captured_output() as (out, err):
model.metabolites.q8_c.summary()
self.check_entries(out, desired_entries)

desired_entries = [
'PRODUCING REACTIONS -- D-Fructose 1,6-bisphosphate (fdp_c)',
Expand All @@ -415,6 +430,7 @@ def test_summary_methods(self):
'0% 0 [0, 1.72] FBP '
'fdp_c + h2o_c --> f6p_c + pi_c',
]

for solver in solver_dict:
with captured_output() as (out, err):
model.metabolites.fdp_c.summary(fva=0.99, solver=solver)
Expand Down

0 comments on commit 64d7674

Please sign in to comment.