ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/test.py
Revision: 60
Committed: Thu May 19 20:13:13 2011 UTC (8 years, 5 months ago) by clausted
File size: 7576 byte(s)
Log Message:
First, add a function to model multiple (up to eight) injections called simple1to1_series().  Second, add functions to return dictionaries with default parameters, called simple1to1_def_params(), simple1to1_mtl_def_params(), and simple1to1_series_def_params().  Also add doctest examples to mdl_module.py.
Line File contents
1 """
2 Christopher Lausted, Institute for Systems Biology
3 Last modified on 110519 (yymmdd)
4 Scripts to test various OSPRAI components.
5 """
6
7 ## Set the PYTHONPATH to your preferred OSPRAI installation.
8 import sys
9 sys.path.append("/home/clausted/Dropbox/ISB/subversion/osprai/trunk")
10
11 ## Import libaries
12 import ba_class
13 import vu_module
14 import io_module
15 import cal_module
16 import mdl_module
17 import fit_module
18 import osprai_one
19 ## Reload Osprai libraries in case code has changed.
20 reload(ba_class)
21 reload (vu_module)
22 reload(io_module)
23 reload(cal_module)
24 reload(mdl_module)
25 reload(fit_module)
26 reload(osprai_one)
27 ## Or just import this next one...
28 from osprai_one import *
29
30
31 def test_io_module():
32 print "Loading SPR data files..."
33 ba1 = readbiosensor("example-biosensor.txt")
34 ba1 = readicmtxt("example-icm.txt")
35 ba1 = readsprit("example-sprit.txt")
36 ba1 = applykey(ba1, "example-key.tsv")
37 print "Writing SPR data files..."
38 writesprit(ba1,"testwritesprit.txt")
39 writeclamp(ba1,"testwriteclamp.txt")
40 writebiosensor(ba1, "testwritebiosensor.txt")
41 print "Done with test_io_module."
42 return
43
44 def test_cal_module_vu_module():
45 print "Loading an SPR data file..."
46 ba1 = readicmtxt("example-icm.txt")
47
48 print "Plotting the raw data file..."
49 dotgraph(ba1)
50
51 print "Plotting the calibrated data..."
52 ba2 = calibrate(ba1, ba1, 2850, 3120)
53 linegraph(ba2, "Calibrated SPR Data")
54 dualgraph(ba1, ba2, "Uncalibrated and Calibrated SPR Data")
55
56 print "Use ROI #16 as background, subtract, and plot..."
57 bgset(ba2, 16)
58 ba3 = bgsubt(ba2)
59 linegraph(ba3, "Calibrated Referenced SPR Data")
60
61 print "Test of scatterplot using time points 500, 1000, 1500, 2000"
62 scatterplot(ba3, 3600, 4200, 3600, 4700, "Scatterplot")
63
64 print "Cutting out an interval..."
65 ba3 = copyinterval(ba3, 2800, 6200)
66 linegraph(ba3)
67 print "Done with test test_cal_module_vu_module."
68 return
69
70
71 def test_mdl_module_fit_module():
72 print "Test the model module..."
73 ## Set some default parameters.
74 dpar = {'t1': {'value':30.0, 'min':30.0, 'max':30.0, 'fixed':'fixed'} }
75 dpar['rmax'] = {'value':100.0, 'min':1.0, 'max':1000.0, 'fixed':'fixed'}
76 dpar['conc'] = {'value':1e-5, 'min':1e-12, 'max':1e1, 'fixed':'fixed'}
77 dpar['cofa'] = {'value':1.0, 'min':1e-10, 'max':1e10, 'fixed':'fixed'}
78 dpar['kon'] = {'value':2e4, 'min':1e1, 'max':1e8, 'fixed':'fixed'}
79 dpar['t2'] = {'value':150.0, 'min':10.0, 'max':1000.0, 'fixed':'fixed'}
80 dpar['koff'] = {'value':1.0e-2, 'min':1e-8, 'max':1e-1, 'fixed':'fixed'}
81 dpar['t3'] = {'value':270.0, 'min':270.0, 'max':270.0, 'fixed':'fixed'}
82 ## Create ba with three ROIs and 300 datapoints.
83 ba0 = BiosensorArray(3,300)
84 for roi in ba0.roi:
85 roi.time = arange(300, dtype=float) ## Samples every 1 second.
86 roi.value = zeros(300, dtype=float) + 20 ## Baseline signal is 20 units.
87 roi.params = deepcopy(dpar)
88 roi.model = simple1to1
89 ## Set the dilution factors
90 ba0.roi[0].params['cofa']['value'] = 1.0 / 27
91 ba0.roi[1].params['cofa']['value'] = 1.0 / 9
92 ba0.roi[2].params['cofa']['value'] = 1.0 / 3
93 ## Simulate the data and plot it.
94 for roi in ba0.roi:
95 roi.value = roi.model(roi.time, roi.value, roi.params)
96 linegraph(ba0, "Simulation")
97
98 ## Constrain rmax belowtrue value to show what happens.
99 print "Testing the fitting..."
100 ba1 = deepcopy(ba0)
101 ## Set one kon, koff, rmax to float.
102 ba1.roi[0].params['koff']['fixed'] = 'float'
103 ba1.roi[0].params['kon']['fixed'] = 'float'
104 ba1.roi[0].params['rmax'] = {'value':10.0, 'min':1.0, 'max':80.0, 'fixed':'float'}
105 ba1.roi[1].params['koff']['fixed'] = 0
106 ba1.roi[1].params['kon']['fixed'] = 0
107 ba1.roi[1].params['rmax']['fixed'] = 0
108 ba1.roi[2].params['koff']['fixed'] = 0
109 ba1.roi[2].params['kon']['fixed'] = 0
110 ba1.roi[2].params['rmax']['fixed'] = 0
111 ## Fit and then plot.
112 mclma(ba1.roi)
113 for roi in ba1.roi:
114 roi.value =roi.model(roi.time, roi.value, roi.params)
115 dualgraph(ba0, ba1, "Fitted Data with an incorrect rmax constraint")
116 print "Done with test test_mdl_module_fit_module."
117 return
118
119
120 def demo_mtl():
121 print "Test the model module..."
122 ba0 = BiosensorArray(1,300) ## Create ba1 with one ROI and 300 datapoints.
123 roi0 = ba0.roi[0]
124 roi0.time = arange(300, dtype=float) ## Samples every 1 second.
125 roi0.value = zeros(300, dtype=float) + 20 ## Baseline signal is 20 units.
126 roi0.params = {'t1': {'value':30.0, 'min':30.0, 'max':30.0, 'fixed':True} }
127 roi0.params['rmax'] = {'value': 100.0}
128 roi0.params['conc'] = {'value': 1e-6}
129 roi0.params['kon'] = {'value': 5e4}
130 roi0.params['t2'] = {'value': 150.0}
131 roi0.params['koff'] = {'value': 5e-3}
132 roi0.params['t3'] = {'value': 270.0}
133 roi0.params['kmtl'] = {'value': 2e6}
134 roi0.name = "kmtl=2e6"
135 roi0.model = simple1to1_mtl
136 roi0.value =roi0.model(roi0.time, roi0.value, roi0.params)
137
138 ba1 = deepcopy(ba0)
139 roi0 = ba1.roi[0]
140 roi0.name = "No MTL"
141 roi0.model = simple1to1
142 roi0.value =roi0.model(roi0.time, roi0.value, roi0.params)
143 dualgraph(ba0, ba1, "With and without MTL")
144 print "Done with test demo_mtl."
145
146
147 def test_mdl_module_series():
148 print "Test the model module..."
149
150 ## Multiple injection model (ba0)
151 ## Create ba with two ROIs and 950 datapoints.
152 ba0 = BiosensorArray(2,950)
153 for roi in ba0.roi:
154 roi.time = arange(950, dtype=float) ## Samples every 1 second.
155 roi.value = zeros(950, dtype=float) + 20 ## Baseline signal is 20.
156 roi.params = simple1to1_series_def_params()
157 roi.model = simple1to1_series
158 ## Define ROI0
159 roi = ba0.roi[0]
160 roi.params['kon']['value'] = 1e4
161 roi.params['t0']['value'] = 100
162 roi.params['c0']['value'] = 1e-6
163 roi.params['t1']['value'] = 400
164 roi.params['c1']['value'] = 2e-6
165 roi.params['t2']['value'] = 700
166 roi.params['c2']['value'] = 4e-6
167 roi.value = roi.model(roi.time, roi.value, roi.params)
168 ## Define ROI1
169 roi = ba0.roi[1]
170 roi.params['kon']['value'] = 2e3
171 roi.params['t0']['value'] = 100
172 roi.params['c0']['value'] = 1e-6
173 roi.params['t1']['value'] = 400
174 roi.params['c1']['value'] = 2e-6
175 roi.params['t2']['value'] = 700
176 roi.params['c2']['value'] = 4e-6
177 roi.value = roi.model(roi.time, roi.value, roi.params)
178
179 ## Single-injection model (ba1)
180 ba1 = BiosensorArray(2,950)
181 for roi in ba1.roi:
182 roi.time = arange(950, dtype=float) ## Samples every 1 second.
183 roi.value = zeros(950, dtype=float) + 15 ## Baseline signal is 19.
184 roi.params = simple1to1_def_params()
185 roi.model = simple1to1
186 ## Define ROI0
187 roi = ba1.roi[0]
188 roi.params['kon']['value'] = 1e4
189 roi.params['conc']['value'] = 1e-6
190 roi.value = roi.model(roi.time, roi.value, roi.params)
191 ## Define ROI1
192 roi = ba1.roi[1]
193 roi.params['kon']['value'] = 2e3
194 roi.params['conc']['value'] = 1e-6
195 roi.value = roi.model(roi.time, roi.value, roi.params)
196
197 ## Display graph.
198 dualgraph(ba0, ba1, "test_mdl_module_series")
199 return
200
201
202
203 ## Run selected test or tests.
204 #test_io_module()
205 #test_cal_module_vu_module()
206 #demo_mtl()
207 #test_mdl_module_fit_module()
208 test_mdl_module_series()
209