Stella: Compiling a subset of Python

 

More info coming soon!

So what would a Stella program look like? All Stella programs are valid and work Python programs!

#!/usr/bin/env python3
"""
Semi-infinite 1D strip with a single spider.
"""

import mtpy  # cython wrapper around mtwist
from math import log, exp
import pdb
import sys
import time
from copy import deepcopy
import virtnet_utils
from virtnet_utils import dsl
from numpy import zeros

class Settings(virtnet_utils.Settings):
  def setDefaults(self):
    self.settings = {
                'seed'      : [int(time.time()), int],
                'r'         : [0.1, float],
                'koffp'     : [1.0, float],
                'K'         : [10, int],
                'rununtiltime' : [1e3, float],
                'elapsedTime':[self.elapsedTime, lambda x:x],
                }

class Simulation(object):
  EXPSTART = 0.2
  def __init__(self, params):
    self.K = params['K']
    self.rununtiltime = params['rununtiltime']
    mtpy.seed(params['seed'])
    self.koffp = params['koffp']
    self.kcat = params['r']

    self.delta = (log(self.rununtiltime)-log(self.EXPSTART))/float(self.K-1)
    self.leg = 0
    self.substrate = 0
    self.obs_i = 0
    self.observations = zeros(shape=self.K, dtype=int)

  def makeObservation(self):
    """Called from run()"""
    self.observations[self.obs_i] = self.leg
    self.obs_i += 1

    self.next_obs_time = self.getNextObsTime()

  def getNextObsTime(self):
    """Called from run()"""
    if self.obs_i == 0:
      return self.EXPSTART
    if self.obs_i==self.K-1:
      return self.rununtiltime;

    return exp(log(self.EXPSTART)+self.delta*self.obs_i);

  def step(self):
    """Called from run()"""
    if self.leg == 0:
      self.leg += 1
    else:
      u1 = mtpy.uniform()
      if u1 < 0.5:
        self.leg -= 1
      else:
        self.leg += 1
    if self.leg == self.substrate:
      self.substrate += 1

  def isNextObservation(self):
    return self.t > self.next_obs_time and self.obs_i < self.K

  @dsl
  def run(self):
    self.t = 0.0
    self.next_obs_time = self.getNextObsTime()

    while self.obs_i < self.K and self.t < self.rununtiltime:
      if self.leg < self.substrate:
        R = self.koffp
      else:
        R = self.kcat
      self.t += mtpy.exp(R)

      while self.isNextObservation():
        self.makeObservation()

      self.step()
    return self.observations

def test():
  settings = Settings()
  settings['seed'] = 1368223681
  expected = [0, 0, 1, 1, 3, 2, 7, 21, 32, 9]
  actual = Simulation(settings).run()
  # convert back to python list for easier comparison
  assert(list(actual) == expected)

def main(argv):
  settings = Settings(argv)
  print ("#", settings)
  results = Simulation(settings).run()
  print (results)

if __name__ == '__main__':
  main(sys.argv[1:])

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>