Tellurium Plugins
Python Wrapper for Tellurium Plugins
 All Data Structures Functions Properties Groups Pages
telLevenbergMarquardt.py

This example demonstrates parameter minimization, using the Levenberg-Marquardt algorithm. Output is shown in the image. To run the example, make sure you have some 'Experimental' data available.

lmFit1_output.jpg
Image output using the example below.
1 import ctypes
2 import teplugins as tel
3 
4 #Get a lmfit plugin object
5 chiPlugin = tel.Plugin("tel_chisquare")
6 lm = tel.Plugin("tel_levenberg_marquardt")
7 
8 #========== EVENT FUNCTION SETUP ===========================
9 def pluginIsProgressing(lmP):
10  # The plugin don't know what a python object is.
11  # We need to cast it here, to a proper python object
12  lmObject = ctypes.cast(lmP, ctypes.py_object).value
13  print 'Iterations = ' + `lmObject.getProperty("NrOfIter")` \
14  + '\tNorm = ' + `lmObject.getProperty("Norm")`
15 
16 try:
17  progressEvent = tel.NotifyEventEx(pluginIsProgressing)
18 
19  #The ID of the plugin is passed as the last argument in the assignOnProgressEvent.
20  #The plugin ID is later on retrieved in the plugin Event handler, see above
21  theId = id(lm)
22  tel.assignOnProgressEvent(lm.plugin, progressEvent, theId)
23  #============================================================
24  #Retrieve a SBML model from plugin
25 
26  test_model = tel.readAllText('two_parameters.xml')
27 
28  #Setup lmfit properties.
29  lm.SBML = test_model
30  experimentalData = tel.DataSeries.readDataSeries ('ExperimentalData.dat')
31  lm.ExperimentalData = experimentalData
32 
33  # Add the parameters that we're going to fit and a initial 'start' value
34  lm.setProperty("InputParameterList", ["k1", 0.8])
35  lm.setProperty("InputParameterList", ["k2", 2.3])
36 
37  lm.setProperty("FittedDataSelectionList", "[S1] [S2]")
38  lm.setProperty("ExperimentalDataSelectionList", "[S1] [S2]")
39 
40  # Start minimization
41  lm.execute()
42 
43  hessian = lm.getProperty("Hessian")
44  print 'Hessian: \n' + `hessian`
45 
46  cov = lm.getProperty("CovarianceMatrix")
47  print 'CovarianceMatrix: \n' + `cov`
48 
49  print 'Minimization finished. \n==== Result ===='
50  print tel.getPluginResult(lm.plugin)
51 
52  # Get the experimental data as a numpy array
53  experimentalData = experimentalData.toNumpy
54 
55  # Get the fitted and residual data
56  fittedData = lm.getProperty ("FittedData").toNumpy
57  residuals = lm.getProperty ("Residuals").toNumpy
58 
59  tel.telplugins.plot(fittedData [:,[0,1]], "blue", "-", "", "S1 Fitted")
60  tel.telplugins.plot(fittedData [:,[0,2]], "blue", "-", "", "S2 Fitted")
61  tel.telplugins.plot(residuals [:,[0,1]], "blue", "None", "x", "S1 Residual")
62  tel.telplugins.plot(residuals [:,[0,2]], "red", "None", "x", "S2 Residual")
63  tel.telplugins.plot(experimentalData [:,[0,1]], "red", "", "*", "S1 Data")
64  tel.telplugins.plot(experimentalData [:,[0,2]], "blue", "", "*", "S2 Data")
65  tel.telplugins.plt.show()
66 
67 except Exception as e:
68  print 'Problem.. ' + `e`