Skip to content

Commit

Permalink
Fix eqn_mode='python' with local fxns (#495)
Browse files Browse the repository at this point in the history
* Fix eqn_mode='python' with local fxns

Fixes an issue when a model with local functions is used
with the ScipyOdeSimulator eqn_mode='python'. Lambdify would
show a NameError and wouldn't pick up model parameters, due to
those not being referenced when local functions are read back
in from BNG during network generation.

Includes a unit test as an example.

* Fix repr of expressions containing one expr/param

PySB currently prints expressions consisting of one
parameter or expression like so:

    Expression('e', Parameter('p', 1.0))

Whereas, for consistency, export etc., it should be:

    Expression('e', p)
  • Loading branch information
alubbock committed Mar 30, 2020
1 parent 918e86e commit d112d82
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pysb/bng.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,8 @@ def _parse_parameter(model, line):
if pname not in par_names:
# Need to parse the value even for constants, since BNG considers some
# expressions to be "Constant", e.g. "2^2"
parsed_expr = parse_bngl_expr(pval)
parsed_expr = parse_bngl_expr(pval, local_dict={
e.name: e for e in model.parameters | model.expressions})
if ptype == 'Constant' and pname not in model._derived_parameters.keys():
p = pysb.core.Parameter(pname, parsed_expr, _export=False)
model._derived_parameters.add(p)
Expand Down
6 changes: 5 additions & 1 deletion pysb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,12 @@ def tags(self):
return sorted(self.expr.atoms(Tag), key=lambda tag: tag.name)

def __repr__(self):
if isinstance(self.expr, (Parameter, Expression)):
expr_repr = self.expr.name
else:
expr_repr = repr(self.expr)
ret = '%s(%s, %s)' % (self.__class__.__name__, repr(self.name),
repr(self.expr))
expr_repr)
return ret

def __str__(self):
Expand Down
3 changes: 2 additions & 1 deletion pysb/export/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def encode_parameter(cls, par):
def encode_expression(cls, expr):
return {
'name': expr.name,
'expr': str(expr.expr)
'expr': expr.expr.name if isinstance(
expr.expr, (Parameter, Expression)) else str(expr.expr)
}

@classmethod
Expand Down
7 changes: 6 additions & 1 deletion pysb/tests/test_simulator_scipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from pysb import Monomer, Parameter, Initial, Observable, Rule, Expression
from pysb.simulator import ScipyOdeSimulator, InconsistentParameterError
from pysb.examples import robertson, earm_1_0, tyson_oscillator
from pysb.examples import robertson, earm_1_0, tyson_oscillator, localfunc
import unittest
import pandas as pd

Expand Down Expand Up @@ -507,3 +507,8 @@ def test_multiprocessing_lambdify():
model, tspan=tspan, compiler='python',
use_analytic_jacobian=True
).run(param_values=[pars, pars], num_processors=2)


def test_lambdify_localfunc():
model = localfunc.model
ScipyOdeSimulator(model, tspan=range(100), compiler='python').run()

0 comments on commit d112d82

Please sign in to comment.