Skip to content

Commit

Permalink
Fix Python and PySB flat exporters, add tests (pysb#366)
Browse files Browse the repository at this point in the history
* Python 3 compatibility in Python exporter
* Unit tests for Python and PySB flat exporters
* A few small fixes to get the tests running
  • Loading branch information
alubbock committed Jun 7, 2018
1 parent 833f1b3 commit ea1ae66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pysb/annotation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pysb.core import SelfExporter, Component
from pysb.core import SelfExporter, Component, Model

class Annotation(object):

Expand Down Expand Up @@ -33,6 +33,8 @@ def __init__(self, subject, object_, predicate="is"):
def __repr__(self):
if isinstance(self.subject, Component):
subject = self.subject.name
elif isinstance(self.subject, Model):
subject = 'model'
else:
subject = self.subject
return "%s(%s, %s, %s)" % (self.__class__.__name__, subject,
Expand Down
10 changes: 6 additions & 4 deletions pysb/examples/fricker_2010_apoptosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
for flip_m, kf, kr, reversible in (zip(flip_monomers, (kf31,kf32), (kr31,kr32), (False,True))):
rule = Rule('RLFADD_%s_Binding' % flip_m.name, FADD (rf=ANY, fe=None) + flip_m (fe=None, ee=None) | FADD (rf=ANY, fe=1) % flip_m (fe=1, ee=None), kf, kr)
if reversible is False:
rule.is_reversible = False;
rule.rate_reverse = None;
rule.is_reversible = False
rule.rule_expression.is_reversible = False
rule.rate_reverse = None

pC8_HomoD = pC8 (fe=ANY, ee=1, D384='U') % pC8 (fe=ANY, ee=1, D384='U')
pC8_HeteroD = pC8 (fe=ANY, ee=1, D384='U') % flipL (fe=ANY, ee=1, D384='U')
Expand All @@ -87,8 +88,9 @@
for flip_m, kf, kr, reversible in (zip(flip_monomers, (kf34,kf35), (kr34,kr35), (False,True))):
rule = Rule('RLFADD_C8_%s_Binding' % flip_m.name, pC8 (fe=ANY, ee=None, D384='U') + flip_m (fe=ANY, ee=None) | pC8 (fe=ANY, ee=1, D384='U') % flip_m (fe=ANY, ee=1), kf, kr)
if reversible is False:
rule.is_reversible = False;
rule.rate_reverse = None;
rule.is_reversible = False
rule.rule_expression.is_reversible = False
rule.rate_reverse = None

Parameter('kc36', 0.223046e-3)
#Homodimer catalyses Homodimer ?: no p18 is released. Only this "cleaved" p43 homoD is the product that will transform into a p18 + L:R:FADD in later reaction.
Expand Down
10 changes: 6 additions & 4 deletions pysb/export/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def export(self):
code_eqs = '\n'.join(['ydot[%d] = %s;' %
(i, sympy.ccode(self.model.odes[i]))
for i in range(len(self.model.odes))])
code_eqs = re.sub(r's(\d+)',
code_eqs = re.sub(r'__s(\d+)',
lambda m: 'y[%s]' % (int(m.group(1))), code_eqs)
for i, p in enumerate(self.model.parameters):
code_eqs = re.sub(r'\b(%s)\b' % p.name, 'p[%d]' % i, code_eqs)
Expand All @@ -118,7 +118,6 @@ def export(self):
output.write("# exported from PySB model '%s'\n" % self.model.name)
output.write(pad(r"""
import numpy
import weave
import scipy.integrate
import collections
import itertools
Expand All @@ -128,8 +127,11 @@ def export(self):
_use_inline = False
# try to inline a C statement to see if inline is functional
try:
import weave
weave.inline('int i;', force=1)
_use_inline = True
except ImportError:
pass
except distutils.errors.CompileError:
pass
Expand Down Expand Up @@ -214,8 +216,8 @@ def simulate(self, tspan, param_values=None, view=False):
self.y = numpy.empty((len(tspan), len(self.y0)))
if len(self.observables):
self.yobs = numpy.ndarray(len(tspan),
zip((obs.name for obs in self.observables),
itertools.repeat(float)))
list(zip((obs.name for obs in self.observables),
itertools.repeat(float))))
else:
self.yobs = numpy.ndarray((len(tspan), 0))
self.yobs_view = self.yobs.view(float).reshape(len(self.yobs),
Expand Down
10 changes: 9 additions & 1 deletion pysb/tests/test_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def test_export():

def check_convert(model, format):
""" Test exporters run without error """
exported_file = None
try:
export.export(model, format)
exported_file = export.export(model, format)
except export.ExpressionsNotSupported:
pass
except export.CompartmentsNotSupported:
Expand All @@ -34,3 +35,10 @@ def check_convert(model, format):
pass
else:
raise

if exported_file is not None:
if format == 'python':
# linspace arguments picked to avoid VODE warning
exec(exported_file + 'Model().simulate(tspan=numpy.linspace(0,1,501))\n', {'_use_inline': False})
elif format == 'pysb_flat':
exec(exported_file, {'__name__': model.name})

0 comments on commit ea1ae66

Please sign in to comment.