Skip to content

Commit

Permalink
integrator python 2.7 and 3.x compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
bad-ants-fleet committed Jul 30, 2019
1 parent 1900973 commit 24d7c2f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,20 @@ However, here is a small example using the above oscillating CRN.
[['C', 'A'],['A','A'],0.9]]
>>> RG = ReactionGraph(crn)
>>> filename, odename = RG.write_ODE_lib(filename='ozzy.py')
>>> print 'Wrote ODE system file:', filename
>>> print('Wrote ODE system file:', filename)
Wrote ODE system file: ozzy.py
```

Then go ahead and execute `ozzy.py`
```sh
~$ python ./ozzy.py --p0 1=1e-6 2=2e-6 3=5e-6
~$ python ./ozzy.py --p0 1=1e-6 2=2e-6 3=5e-6 --t8 1e8 --pyplot ozzy.pdf
```

... or load it as python library.

```py
>>> import imp
>>> _temp = imp.load_source(odename, filename)
>>> integrate = getattr(_temp, 'integrate')
>>> from crnsimulator import get_integrator
>>> integrate = get_integrator(odename, filename)
>>> integrate(args) # args = <argparse.ArgumentParser()>
```

Expand All @@ -100,5 +99,5 @@ Then go ahead and execute `ozzy.py`
```

## Version
0.5
0.6

4 changes: 2 additions & 2 deletions crnsimulator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# Use at your own risk.
#
#
__version__ = "v0.5"
__version__ = "v0.6"

from crnsimulator.crn_parser import parse_crn_string, parse_crn_file
from crnsimulator.reactiongraph import ReactionGraph
from crnsimulator.solver import writeODElib
from crnsimulator.solver import writeODElib, get_integrator
from crnsimulator.odelib_template import ode_plotter
16 changes: 16 additions & 0 deletions crnsimulator/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
from builtins import range
import crnsimulator.odelib_template

def get_integrator(odename, filename):
"""Workaround to avoid deprecation warnings for the imp module.
"""
try: # Python 3.7
import types
import importlib.machinery

loader = importlib.machinery.SourceFileLoader(odename, filename)
mod = types.ModuleType(loader.name)
loader.exec_module(mod)

return getattr(mod, 'integrate')
except ImportError as err: # Python 2.7
import imp
_temp = imp.load_source(odename, filename)
return getattr(_temp, 'integrate')

def writeODElib(svars, odeM, jacobian=None, rdict=None, concvect=None,
odename='odesystem', filename='./odesystem', template=None):
Expand Down
10 changes: 4 additions & 6 deletions scripts/crnsimulator
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
# Use at your own risk.
#
#
from __future__ import absolute_import, division, print_function

from __future__ import print_function
import os
import sys
import imp
import argparse

from crnsimulator import ReactionGraph, __version__
from crnsimulator import ReactionGraph, get_integrator, __version__
from crnsimulator.odelib_template import add_integrator_args
from crnsimulator.crn_parser import parse_crn_string, ParseException

Expand Down Expand Up @@ -89,7 +88,7 @@ def main(args):
# **************** #
# WRITE ODE SYSTEM #
# ................ #
if not args.force and os.path.exists(filename):
if filename != 'odesystem.py' and not args.force and os.path.exists(filename):
print('# Reading ODE system from existing file:', filename)
else:
# ******************* #
Expand Down Expand Up @@ -119,8 +118,7 @@ def main(args):
print('# Simulating the ODE system, change parameters using:')
print("# python {} --help ".format(filename))

_temp = imp.load_source(odename, filename)
integrate = getattr(_temp, 'integrate')
integrate = get_integrator(odename, filename)

# ********************* #
# ARGUMENT PROCESSING 2 #
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='crnsimulator',
version="0.5",
version="0.6",
description='Simulate CRNs using ODEs.',
long_description=LONG_DESCRIPTION,
author='Stefan Badelt',
Expand Down
8 changes: 3 additions & 5 deletions tests/test_solver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import unicode_literals

import os
import imp
import unittest
from argparse import ArgumentParser

from crnsimulator import get_integrator
from crnsimulator.reactiongraph import ReactionGraph, ReactionNode
from crnsimulator.crn_parser import parse_crn_string
from crnsimulator.odelib_template import add_integrator_args
Expand Down Expand Up @@ -46,8 +46,7 @@ def test_crn(self):
RG = ReactionGraph(crn)

filename, odename = RG.write_ODE_lib(filename=self.filename)
_temp = imp.load_source(odename, filename)
integrate = getattr(_temp, 'integrate')
integrate = get_integrator(odename, filename)

self.args.p0 = ['1=0.5']
self.args.t_log = 10
Expand Down Expand Up @@ -86,8 +85,7 @@ def test_crn_sympy_imports(self):
RG = ReactionGraph(crn)

filename, odename = RG.write_ODE_lib(filename=self.filename)
_temp = imp.load_source(odename, filename)
integrate = getattr(_temp, 'integrate')
integrate = get_integrator(odename, filename)

self.args.p0 = ['S=0.5', 'cos=0.2']
self.args.t_log = 10
Expand Down

0 comments on commit 24d7c2f

Please sign in to comment.