Skip to content

Commit 10cbe3a

Browse files
committed
Only include the PySB rule in the influence map labels if needed to disambiguate
1 parent f7e5987 commit 10cbe3a

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

bioagents/mra/mra.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,23 @@ def get_statement_by_uuid(statements, uuid):
322322
return None
323323

324324

325-
def make_unique_label(labels_so_far, new_label):
326-
"""Make new_label a unique label by appeneding spaces."""
327-
while new_label in labels_so_far:
328-
new_label += ' '
329-
return new_label
330-
325+
def dict_value_appears_multiple_times(d, value):
326+
"""Returns True iff the given value is mapped to multiple times in this
327+
dictionary."""
328+
count = 0
329+
for k, v in d.items():
330+
if v == value:
331+
count += 1
332+
return count > 1
331333

332334
def make_influence_map_labels_natural_language(im, pysb_model, indra_model):
333335
"""Replaces the labels of the influence map with natural language labels.
334336
"""
335337
# Get the name of all the rules in the pysb model
336338
rule_names = [rule.name for rule in pysb_model.rules]
337339

338-
relabel_map = {}
340+
relabel_map_english = {}
341+
relabel_map_english_and_rule = {}
339342
for annotation in pysb_model.annotations:
340343
if annotation.subject in rule_names:
341344
name = annotation.subject
@@ -346,10 +349,24 @@ def make_influence_map_labels_natural_language(im, pysb_model, indra_model):
346349

347350
# Convert the statement into English
348351
assembler = EnglishAssembler([s])
349-
text = assembler.make_model()
350-
text = make_unique_label(relabel_map.values(), text)
351-
text += '\n' + name
352-
relabel_map[name] = text
352+
353+
english_text = assembler.make_model()
354+
english_and_rule = english_text + ' (' + name + ')'
355+
356+
relabel_map_english[name] = english_text
357+
relabel_map_english_and_rule[name] = english_and_rule
358+
359+
# Use only the English text if that is unambiguous, otherwise include
360+
# the rule name too
361+
relabel_map = {}
362+
for before in relabel_map_english:
363+
english_text = relabel_map_english[before]
364+
if dict_value_appears_multiple_times(relabel_map_english,
365+
english_text):
366+
relabel_map[before] = relabel_map_english_and_rule[before]
367+
else:
368+
relabel_map[before] = english_text
369+
353370
im = networkx.relabel_nodes(im, relabel_map)
354371
return im
355372

0 commit comments

Comments
 (0)