Skip to content

Commit

Permalink
Fix StochKit export of certain PySB rxn Expressions (#521)
Browse files Browse the repository at this point in the history
Reactions with a rate as a PySB Expression object
(rather than merely containing one) are exported by string conversion,
which renders like "Expression(...)". This PR detects and fixes
this case.
  • Loading branch information
alubbock committed Dec 17, 2020
1 parent a6a61fc commit aa32fa6
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions pysb/export/stochkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,27 @@ def export(self, initials=None, param_values=None):
if rate is None:
# Custom propensity function needed

rxn_atoms = rxn["rate"].atoms()

# replace terms like __s**2 with __s*(__s-1)
rate = str(rxn["rate"])

matches = pattern.findall(rate)
for m in matches:
repl = m[0]
for i in range(1, int(m[1])):
repl += "*(%s-%d)" % (m[0], i)
rate = re.sub(pattern, repl, rate, count=1)

# expand only expressions used in the rate eqn
for e in {sym for sym in rxn_atoms
if isinstance(sym, Expression)}:
rate = re.sub(r'\b%s\b' % e.name,
expr_strings[e.name],
rate)
if isinstance(rxn['rate'], Expression):
rate = expr_strings[rxn['rate'].name]
else:
rxn_atoms = rxn["rate"].atoms()

# replace terms like __s**2 with __s*(__s-1)
rate = str(rxn["rate"])

matches = pattern.findall(rate)
for m in matches:
repl = m[0]
for i in range(1, int(m[1])):
repl += "*(%s-%d)" % (m[0], i)
rate = re.sub(pattern, repl, rate, count=1)

# expand only expressions used in the rate eqn
for e in {sym for sym in rxn_atoms
if isinstance(sym, Expression)}:
rate = re.sub(r'\b%s\b' % e.name,
expr_strings[e.name],
rate)

reacs.append(self._reaction_to_element(rxn_name,
rxn_desc,
Expand Down

0 comments on commit aa32fa6

Please sign in to comment.