Skip to content

Commit

Permalink
update get_integrator function, documentation, ode_plotter
Browse files Browse the repository at this point in the history
  • Loading branch information
bad-ants-fleet committed Feb 13, 2020
1 parent b687644 commit f89a208
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 33 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Stefan Badelt <badelt@dna.caltech.edu>
Copyright (c) 2017-2020 Stefan Badelt <stefan.badelt@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
48 changes: 31 additions & 17 deletions README.md
Expand Up @@ -8,17 +8,18 @@ equations (ODEs).

Create a test file with your CRN:

File: ozzy.crn
File: [oscillator.crn]
```
# Oscillator Test
# Rock-Paper-Scissors Oscillator
A + B -> B + B [k = 0.2]
B + C -> C + C [k = 0.3]
C + A -> A + A [k = 1]
B + C -> C + C [k = 0.4]
C + A -> A + A [k = 0.7]
```

And pipe it into the crnsimulator:
```sh
~$ crnsimulator -o ozzy < ozzy.crn
~$ crnsimulator -o ozzy < oscillator.crn
```
This writes the ODE system to an executable python script: `ozzy.py`

Expand All @@ -35,7 +36,7 @@ You can pass the command line options for ozzy.py directly to `crnsimulator`.
This will automatically simulate your ODE system. Use --force to overwrite an
existing `ozzy.py` script.
```sh
~$ crnsimulator --p0 A=0.1 B=1e-2 C=1e-3 --t8 10000 -o ozzy --pyplot ozzy.pdf < ozzy.crn
~$ crnsimulator --p0 A=0.1 B=1e-2 C=1e-3 --t8 10000 -o ozzy --pyplot ozzy.pdf < oscillator.crn
```

You can specify the CRN in a single line:
Expand Down Expand Up @@ -69,21 +70,38 @@ However, here is a small example using the above oscillating CRN.
[['B', 'C'],['C','C'],0.8],
[['C', 'A'],['A','A'],0.9]]
>>> RG = ReactionGraph(crn)
>>> filename, odename = RG.write_ODE_lib(filename='ozzy.py')
>>> svars = ['B', 'C', 'A'] # let's enforce the order of species, because we can!
>>> filename, odename = RG.write_ODE_lib(filename='ozzy.py', sorted_vars = svars)
>>> print('Wrote ODE system file:', filename)
Wrote ODE system file: ozzy.py
```

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

... or load it as python library.
... or load its functions by treating it as a python library:

```py
# Import
>>> import numpy as np
>>> from scipy.integrate import odeint
>>> from crnsimulator import get_integrator
>>> odesys = get_integrator(filename, function = odename)
>>> odeplt = get_integrator(filename, function = 'ode_plotter')
# Simulate
>>> p0 = [1e-6, 2e-6, 5e-6] # order of svars
>>> time = np.linspace(0, 1e8, num = 10_000)
>>> ny = odeint(odesys, p0, time, (None,), atol = 1e-10, rtol = 1e-10).T
# Plot
>>> odeplt(`ozzy.pdf`, time, ny, svars)
```

... or include the prebuilt integrator in you own script (like the crnsimulator exectuable):
```py
>>> from crnsimulator import get_integrator
>>> integrate = get_integrator(odename, filename)
>>> integrate = get_integrator(filename)
>>> integrate(args) # args = <argparse.ArgumentParser()>
```

Expand All @@ -92,12 +110,8 @@ Then go ahead and execute `ozzy.py`
```sh
~$ python setup.py install
```

### local installation
```sh
~$ python setup.py install --user
```

## Version
0.7
0.7.1

[oscillator.crn]: <https://github.com/bad-ants-fleet/crnsimulator/blob/master/tests/crns/oscillator.crn>
2 changes: 1 addition & 1 deletion crnsimulator/__init__.py
Expand Up @@ -6,7 +6,7 @@
# Use at your own risk.
#
#
__version__ = "v0.7"
__version__ = "v0.7.1"

from crnsimulator.crn_parser import parse_crn_string, parse_crn_file
from crnsimulator.reactiongraph import ReactionGraph
Expand Down
9 changes: 6 additions & 3 deletions crnsimulator/odelib_template.py
Expand Up @@ -26,8 +26,8 @@
sns.set(style="darkgrid", font_scale=1, rc={"lines.linewidth": 2.0})


def ode_plotter(name, t, ny, svars, log=False, labels=None,
xlim=None, ylim=None):
def ode_plotter(name, t, ny, svars, log = False, labels = None,
xlim = None, ylim = None, plim = None):
""" Plots the ODE trajectories.
Args:
Expand All @@ -37,6 +37,9 @@ def ode_plotter(name, t, ny, svars, log=False, labels=None,
svars (list[str]): A list of names for every trajectory in ny
log (bool,optional): Plot data on a logarithmic time scale
labels (set(),optional): Define species that appear labelled in the plot
xlim ((float,float), optional): matplotlib xlim.
ylim ((float,float), optional): matplotlib ylim.
plim (float, optional): Minimal occupancy to plot a trajectory. Defaults to None.
Prints:
A file containing the plot (Format *.pdf, *.png, etc.)
Expand Down Expand Up @@ -74,7 +77,7 @@ def ode_plotter(name, t, ny, svars, log=False, labels=None,
ax.plot(t, y, '--', lw=0.1, color='gray', zorder=1)
else:
for e, y in enumerate(ny):
if max(y) > 0.05:
if plim is None or max(y) > plim:
ax.plot(t, y, '-', label=svars[e])
else:
ax.plot(t, y, '--', lw=0.1, color='gray', zorder=1)
Expand Down
30 changes: 24 additions & 6 deletions crnsimulator/solver.py
Expand Up @@ -11,13 +11,31 @@

import crnsimulator.odelib_template

def get_integrator(odename, filename):
"""Workaround to avoid deprecation warnings for the imp module.
def get_integrator(filename, function = 'integrate'):
""" Wrapper for the jit import of a function from a python script.
Note: The intended usage is to import the integrate function from the
executable produced by crnsimulator. However, it can be used to import
arbitrary functions from arbitrary python scripts.
Args:
filename (str): The autogenerated python executable containing the odesystem.
function (str, optional): The name of the function to be imported. Defaults
to 'integrate', which is the main function of the file, but it can also
be used to import low-level functions such as 'odesystem' or 'ode_plotter'.
Returns:
A jit import of the requested function.
"""
loader = importlib.machinery.SourceFileLoader(odename, filename)
mod = types.ModuleType(loader.name)
loader.exec_module(mod)
return getattr(mod, 'integrate')
try:
loader = importlib.machinery.SourceFileLoader('my_loader', filename)
mod = types.ModuleType(loader.name)
loader.exec_module(mod)
except FileNotFoundError as err:
print(Warning('DEPRECATED - crnsimulator: Please note that the crnsimulator.solver.get_integrator function interface changed.'))
raise err

return getattr(mod, function)

def writeODElib(svars, odeM, jacobian=None, rdict=None, concvect=None,
odename='odesystem', filename='./odesystem', template=None):
Expand Down
2 changes: 1 addition & 1 deletion scripts/crnsimulator
Expand Up @@ -130,7 +130,7 @@ def main(args):
print('# Simulating the ODE system, change parameters using:')
print("# python {} --help ".format(filename))

integrate = get_integrator(odename, filename)
integrate = get_integrator(filename)

# ********************* #
# ARGUMENT PROCESSING 2 #
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -9,13 +9,13 @@

setup(
name = 'crnsimulator',
version = "0.7",
version = "0.7.1",
description = 'Simulate CRNs using ODEs.',
long_description = LONG_DESCRIPTION,
author = 'Stefan Badelt',
author_email = 'badelt@caltech.edu',
url = 'https://github.com/bad-ants-fleet/crnsimulator',
download_url = 'https://github.com/bad-ants-fleet/crnsimulator/archive/v0.7.tar.gz',
download_url = 'https://github.com/bad-ants-fleet/crnsimulator/archive/v0.7.1.tar.gz',
license = 'MIT',
classifiers = [
'Development Status :: 3 - Alpha',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_solver.py
Expand Up @@ -46,7 +46,7 @@ def test_crn(self):
RG = ReactionGraph(crn)

filename, odename = RG.write_ODE_lib(filename=self.filename)
integrate = get_integrator(odename, filename)
integrate = get_integrator(filename)

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

filename, odename = RG.write_ODE_lib(filename=self.filename)
integrate = get_integrator(odename, filename)
integrate = get_integrator(filename)

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

0 comments on commit f89a208

Please sign in to comment.