Tellurium Plugins
Python ctypes wrapper documentation
 All Functions Variables Groups Pages
telEvents.py

This example shows

  1. How to define Python event functions and passing them to a plugin
1 import os
2 import roadrunner
3 import matplotlib.pyplot as plot
4 import numpy
5 import ctypes
6 from teplugins import *
7 
8 
9 #Create a plugin manager
11 
12 def pluginStarted():
13  print 'The plugin was started'
14 
15 def pluginIsProgressing(val):
16  pluginHandle = cast(val, ctypes.py_object).value
17  prop = getPluginProperty(pluginHandle, "Progress")
18  print '\nPlugin progress:' + `getPropertyValue(prop)` +' %'
19 
20 def pluginIsFinished():
21  print 'The plugin did finish'
22 
23 try:
24  #Create a roadrunner instance
25  rr = roadrunner.RoadRunner()
26 
27  #Retrieve a SBML model from plugin
28  modelPlugin = Plugin("tel_test_model")
29  sbmlModel= modelPlugin.Model
30 
31  #Check if model file exists
32  rr.load(sbmlModel)
33 
34  timeStart = 0
35  timeEnd = 10
36  numPoints = 500
37  data = rr.simulate(timeStart, timeEnd, numPoints)
38 
39  #Load the 'noise' plugin in order to add some noise to the data
40  pluginHandle = loadPlugin(pm, "tel_add_noise")
41 
42  #get parameter for noise 'size'
43  sigmaHandle = getPluginProperty(pluginHandle, "Sigma")
44  aSigma = getPropertyValueAsString(sigmaHandle)
45 
46  #set size of noise
47  setProperty(sigmaHandle, 1.e-5)
48 
49  cb_func1 = NotifyEvent(pluginStarted)
50  assignOnStartedEvent(pluginHandle, cb_func1)
51 
52  progressEvent = NotifyEventEx(pluginIsProgressing)
53 
54  #The ID of the plugin is passed as the last argument in the assignOnProgressEvent.
55  #The plugin ID is later on retrieved in the plugin Event handler, see above
56  theId = id(pluginHandle)
57  assignOnProgressEvent(pluginHandle, progressEvent, theId)
58 
59  cb_func3 = NotifyEvent(pluginIsFinished)
60  assignOnFinishedEvent(pluginHandle, cb_func3)
61 
62  # Get the dataseries from roadrunner
63  d = getDataSeries (data)
64 
65  #Pass data from roadrunner to the plugin
66  setPluginProperty(pluginHandle, "InputData", d._data) #Is _data OK here?
67 
68  #Execute the noise plugin which will add some noise to the (internal) data
69  executePluginEx(pluginHandle)
70 
71  #Retrieve data from plugin
72  pluginData = getPluginProperty(pluginHandle, "InputData")
73  rrData = getNumpyData(getProperty(pluginData))
74  colNames = getTelluriumDataColumnHeader(getProperty(pluginData))
75  plotTelluriumData(rrData, colNames)
76  unLoadPlugins(pm)
77 
78  print "done"
79 
80 except Exception as e:
81  print 'Exception: ' + `e`