Skip to content

Commit

Permalink
Performance metric now work for reversible model. Implements #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdiener committed Jun 7, 2016
1 parent 3f9b753 commit 392fcce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
34 changes: 28 additions & 6 deletions corda/corda.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,35 +249,57 @@ def build(self):
self.impossible = np.unique(self.impossible).tolist()
self.built = True

def __str__(self):
old_counts = Counter(self.__conf_old.values())
def info(self, reversible=True):
"""Gives basic performance infos about the reconstruction.
Generates an acceptably nice output describing which reactions
from each confidence level were included in the reconstruction.
Args:
reversible (Optional[boolean]): Whether the statistics should be
given for the model allwoing for reversible reactions or for the
model where each reaction is split into its forward and backward
reactions.
Returns:
A formatted output string.
"""

if reversible:
m = deepcopy(self.model)
revert_to_reversible(m)
rids = [r.id for r in m.reactions]
else: rids = [r.id for r in self.model.reactions]

old_counts = Counter([self.__conf_old[k] for k in self.__conf_old
if k in rids])
if not self.built:
out = "build status: not built\n" + \
"#reactions (including mock): {}\n".\
format(len(self.__conf_old)) + \
"#reactions (including mock): {}\n".format(len(rids)) + \
"Reaction confidence:\n" + \
" - unclear: {}\n".format(old_counts[0]) + \
" - exclude: {}\n".format(old_counts[-1]) + \
" - low and medium: {}\n".format(old_counts[1] + \
old_counts[2]) + \
" - high: {}\n".format(old_counts[3])
else:
rids = self.conf.keys()
old = np.array([self.__conf_old[k] for k in rids])
new = np.array([self.conf[k] for k in rids])
med_inc = np.sum(((old == 1) | (old == 2)) & (new == 3))
noc_inc = np.sum((old == -1) & (new == 3))
free_inc = np.sum((old == 0) & (new == 3))
high_inc = np.sum((old == 3) & (new == 3))
out = "build status: reconstruction complete\n" + \
"Inc. reactions: {}/{}\n".format(np.sum(new == 3), len(old)) +\
"Inc. reactions: {}/{}\n".format(np.sum(new == 3), len(old)) + \
" - unclear: {}/{}\n".format(free_inc, old_counts[0]) + \
" - exclude: {}/{}\n".format(noc_inc, old_counts[-1]) + \
" - low and medium: {}/{}\n".format(med_inc, old_counts[1] + \
old_counts[2]) + \
" - high: {}/{}\n".format(high_inc, old_counts[3])
return out

def __str__(self):
return self.info(reversible=True)

def cobra_model(self, name, reversible=True, bound=1000):
"""Constructs a cobra model for the reconstruction.
Expand Down
2 changes: 2 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def test_performance_metrics(self):
opt = CORDA(self.model, self.conf)
opt.build()
self.assertTrue("reconstruction complete" in str(opt))
self.assertTrue("/60" in opt.info(reversible=True))
self.assertTrue("/101" in opt.info(reversible=False))

def test_build_works(self):
opt = CORDA(self.model, self.conf)
Expand Down

0 comments on commit 392fcce

Please sign in to comment.