Skip to content

Commit

Permalink
Merge pull request #28 from CovertLab/noform
Browse files Browse the repository at this point in the history
Removed support for alternate forms, rearranging tests to allow for profiling
  • Loading branch information
prismofeverything authored Dec 21, 2018
2 parents edf69e8 + c83eb8a commit d419c37
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ __pycache__
MANIFEST
build/
dist/
profile

arrow.egg-info/
stochastic_arrow.egg-info/
Expand Down
31 changes: 13 additions & 18 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ def choose(n, k):
return np.rint(product)


def propensity(stoichiometric_matrix, state, form):
reactants = np.where(stoichiometric_matrix < 0)
def propensity(stoichiometry, state):
reactants = np.where(stoichiometry < 0)
terms = [
form(state[reactant], -stoichiometric_matrix[reactant])
choose(state[reactant], -stoichiometry[reactant])
for reactant in reactants[0]]

return np.array(terms).prod()

def step(stoichiometric_matrix, rates, state, forms, propensities=[], update_reactions=()):

def step(stoichiometric_matrix, rates, state, propensities=[], update_reactions=()):
if len(update_reactions):
for update in update_reactions:
stoichiometry = stoichiometric_matrix[:, update]
form = forms if callable(forms) else forms[update]
propensities[update] = propensity(stoichiometry, state, form)
propensities[update] = propensity(stoichiometry, state)
else:
propensities = np.array([
propensity(stoichiometry, state, forms if callable(forms) else forms[index])
propensity(stoichiometry, state)
for index, stoichiometry in enumerate(stoichiometric_matrix.T)])

distribution = (rates * propensities)
Expand All @@ -52,7 +52,7 @@ def step(stoichiometric_matrix, rates, state, forms, propensities=[], update_rea
return time_to_next, outcome, choice, propensities


def evolve(stoichiometric_matrix, rates, state, duration, forms=choose):
def evolve(stoichiometric_matrix, rates, state, duration):
time_current = 0
time = [0]
counts = [state]
Expand All @@ -61,18 +61,14 @@ def evolve(stoichiometric_matrix, rates, state, duration, forms=choose):
events = np.zeros(rates.shape)

dependencies = [
np.where(np.any(stoichiometric_matrix[
stoichiometry != 0
] < 0, 0))[0]
for stoichiometry in stoichiometric_matrix.T
]
np.where(np.any(stoichiometric_matrix[stoichiometry != 0] < 0, 0))[0]
for stoichiometry in stoichiometric_matrix.T]

while True:
time_to_next, state, choice, propensities = step(
stoichiometric_matrix,
rates,
state,
forms,
propensities,
update_reactions)

Expand All @@ -94,13 +90,12 @@ def evolve(stoichiometric_matrix, rates, state, duration, forms=choose):


class StochasticSystem(object):
def __init__(self, stoichiometric_matrix, rates, forms=None):
def __init__(self, stoichiometric_matrix, rates):
self.stoichiometric_matrix = stoichiometric_matrix
self.rates = rates
self.forms = forms or choose

def step(self, state):
return step(self.stoichiometric_matrix, self.rates, state, forms=self.forms)
return step(self.stoichiometric_matrix, self.rates, state)

def evolve(self, state, duration):
return evolve(self.stoichiometric_matrix, self.rates, state, duration, forms=self.forms)
return evolve(self.stoichiometric_matrix, self.rates, state, duration)
69 changes: 39 additions & 30 deletions arrow/test/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import numpy as np
import json
import argparse

from arrow import evolve, StochasticSystem

def test_equilibration():
stoichiometric_matrix = np.array([
[-1, +1, 0],
[+1, -1, -1],
])
[+1, -1, -1]])

rates = np.array([10, 10, 0.1])
system = StochasticSystem(stoichiometric_matrix, rates)
Expand All @@ -42,8 +42,7 @@ def test_dimerization():
[-1, -2, +1],
[-1, 0, +1],
[+1, 0, -1],
[ 0, +1, 0]
])
[ 0, +1, 0]])

rates = np.array([3, 1, 1]) * 0.01
system = StochasticSystem(stoichiometric_matrix, rates)
Expand Down Expand Up @@ -110,43 +109,53 @@ def load_state(filename):
return (time, counts, events)

if __name__ == '__main__':
from itertools import izip

import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument('--plot', action='store_true')
parser.add_argument('--complexation', action='store_true')
parser.add_argument('--runs', type=int, default=1)
args = parser.parse_args()

from arrow.analysis.plotting import plot_full_history
from itertools import izip

systems = (
test_equilibration,
test_dimerization,
test_complexation,
)
test_complexation)

if not args.plot:
if args.complexation:
for run in xrange(args.runs):
test_complexation()
else:
for system in systems:
system()
else:
import matplotlib.pyplot as plt
from arrow.analysis.plotting import plot_full_history

n_systems = len(systems)
n_systems = len(systems)

ncols = int(np.ceil(np.sqrt(n_systems)))
nrows = int(np.ceil(n_systems / ncols))
ncols = int(np.ceil(np.sqrt(n_systems)))
nrows = int(np.ceil(n_systems / ncols))

margins = 1
axes_size = 3
margins = 1
axes_size = 3

figsize = (
margins + axes_size*ncols,
margins + axes_size*nrows
)
figsize = (
margins + axes_size*ncols,
margins + axes_size*nrows)

(fig, all_axes) = plt.subplots(
figsize = figsize,
nrows = nrows, ncols = ncols,
constrained_layout = True
)
(fig, all_axes) = plt.subplots(
figsize = figsize,
nrows = nrows, ncols = ncols,
constrained_layout = True)

all_axes = np.asarray(all_axes)
all_axes = np.asarray(all_axes)

for (axes, system) in izip(all_axes.flatten(), systems):
axes.set_title(system.func_name)
for (axes, system) in izip(all_axes.flatten(), systems):
axes.set_title(system.func_name)

time, counts, events = system()
plot_full_history(axes, time, counts)
time, counts, events = system()
plot_full_history(axes, time, counts)

fig.savefig('test_systems.png')
fig.savefig('test_systems.png')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='stochastic-arrow',
version='0.0.4',
version='0.0.8',
packages=['arrow'],
author='Ryan Spangler',
author_email='[email protected]',
Expand Down

0 comments on commit d419c37

Please sign in to comment.