From c83eb8af8e3376969cf11a5efc59d4ef7459ad12 Mon Sep 17 00:00:00 2001 From: Ryan Spangler Date: Fri, 21 Dec 2018 14:26:04 -0800 Subject: [PATCH] removed support for forms, rearranging tests to allow for profiling --- .gitignore | 1 + arrow/arrow.py | 31 ++++++++---------- arrow/test/test_arrow.py | 69 +++++++++++++++++++++++----------------- setup.py | 2 +- 4 files changed, 54 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 514ad95..c1e13e2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ __pycache__ MANIFEST build/ dist/ +profile arrow.egg-info/ stochastic_arrow.egg-info/ diff --git a/arrow/arrow.py b/arrow/arrow.py index 00515cd..7c659aa 100644 --- a/arrow/arrow.py +++ b/arrow/arrow.py @@ -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) @@ -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] @@ -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) @@ -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) diff --git a/arrow/test/test_arrow.py b/arrow/test/test_arrow.py index c6dc5e4..2cba5f0 100644 --- a/arrow/test/test_arrow.py +++ b/arrow/test/test_arrow.py @@ -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) @@ -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) @@ -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') diff --git a/setup.py b/setup.py index eec26f1..06366e9 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='stochastic-arrow', - version='0.0.4', + version='0.0.8', packages=['arrow'], author='Ryan Spangler', author_email='spanglry@stanford.edu',