ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/cal_module.py
Revision: 59
Committed: Wed May 11 21:27:16 2011 UTC (8 years, 2 months ago) by clausted
File size: 6184 byte(s)
Log Message:
Work on doctest material for ba_class and cal_module.
Now ba_class has built-in 1:1 interaction model simulator.
This is used for testing functions and methods in this and other modules.
Also change bgset() function so zero refers to first ROI.
This is necessary for consistency--but old user scripts might need updating.
Line File contents
1 """
2 cal_module
3 ----------
4
5 Calibration module for SPRI data in a ba_class.
6 This uses a known jump in index to turn arbitrary units into RIU.
7 Other routines provide background subtraction and data rearrangement.
8
9 .. moduleauthor:: Christopher Lausted,
10 Institute for Systems Biology,
11 OSPRAI developers.
12
13 Example::
14
15 >>> import ba_class as ba
16 >>> ba1 = ba.BiosensorArray(2, 300) ## Create ba simulated data.
17 >>> ba1.roi[0].simulate(bulkRI=900, kon=1e4, koff=1e-2, conc=0.0)
18 >>> ba1.roi[1].simulate(bulkRI=900, kon=1e4, koff=1e-2, conc=1e-5)
19 >>> t1, t2 = 15, 50
20 >>> n1, n2 = 1335700, 1336600
21 >>> baCal = ba1 ## Calibration injection is in same ba.
22 >>> ba2 = calibrate(ba1, baCal, t1, t2, n1, n2)
23 >>> bgset(ba2, 0) ## The first ROI is the background ref.
24 >>> ba3 = bgsubt(ba2)
25 >>> zerotime(ba3) ## Optional, time already starts at zero.
26 >>> zerovalue(ba3) ## Optional, value already starts at zero.
27 >>> print round(ba3.roi[1].value[299], 2)
28 337.79
29 """
30 __version__ = "110426"
31
32
33 ## Import libraries
34 import ba_class as ba
35 import numpy as np
36 from copy import deepcopy
37
38
39 def calibrate(ba0, baCal, t1, t2, n1=1335700, n2=1336600):
40 """
41 Use data in baCal to calibrate ba0.
42 A simple two-point linear calibration using timepoints t1 and t2 and
43 corresponding refractive indices n1 and n2.
44 Average 30 seconds of data (t-15 to 5+15 seconds).
45 The ba object returned will remember the slopes and intercepts used.
46
47 :param ba0: Object containing the data to be calibrated.
48 :type ba0: ba_class
49 :param baCal: Object containing the data to be calibrated.
50 :type baCal: ba_class
51 :param t1: Timepoint where refractive index = n1
52 :type t1: float
53 :param t2: Timepoint where refractive index = n2
54 :type t2: float
55 :param n1: First refractive index. Default=1335700 (~1x PBS).
56 :type n1: float
57 :param n2: Second refractive index. Default=1336600 (~2x PBS).
58 :type n2: float
59 :returns: nothing
60 """
61 ## Error checking
62 if (len(ba0.roi) != len(baCal.roi)):
63 print "Error: The ba objects are of different size."
64 return ba0
65 ## Continue.
66 ba2 = deepcopy(ba0)
67 for i, iroi in enumerate(baCal.roi):
68 ## Average values of camera units around t1 and t2.
69 cu1 = np.average(iroi.time2val(t1-15, t1+15))
70 cu2 = np.average(iroi.time2val(t2-15, t2+15))
71 ## Calculate and record slope and intercept parameters.
72 slope = (n2 - n1) / (cu2 - cu1)
73 intercept = n2 - (slope * cu2)
74 ## Apply the calibration to the new ba object.
75 ba2.roi[i].calibM = slope
76 ba2.roi[i].calibB = intercept
77 ba2.roi[i].value = ba0.roi[i].value * slope + intercept ## type np.array.
78 return ba2
79 """End of calibrate()"""
80
81
82 def bgsubt(ba0):
83 """
84 Apply the background subtraction and return a new ba object.
85
86 :param ba0: Object containing the source data.
87 :type ba0: ba_class
88 :returns: A new ba object
89 """
90 ba1 = deepcopy(ba0)
91 for i in range(len(ba0.roi)):
92 bg = ba0.roi[i].bgroi[0] ## TODO: Only uses first bgroi for now.
93 #bg -= 1 ## Decide if index or id base 0 or 1.
94 ba1.roi[i].value = ba0.roi[i].value - ba0.roi[bg].value ## type np.array.
95 return ba1
96 """End of bgsubt()"""
97
98
99 def bgset(ba0, i):
100 """
101 Set i as the background ROI for all of the ROIs.
102
103 :param ba0: Object containing the data to be calibrated.
104 :type ba0: ba_class
105 :param i: Object containing the data to be calibrated.
106 :type i: ba_class
107 :returns: nothing
108 """
109 for iroi in ba0.roi: iroi.bgroi = [i]
110 return
111 """End of bgset()"""
112
113
114 def copyinterval(ba0, t1, t2):
115 """
116 Copy data from the interval between t1 and t2 and return a new ba object.
117
118 :param ba0: Object containing the source data.
119 :type ba0: ba_class
120 :param t1: Beginning timepoint.
121 :type t1: float
122 :param t2: Ending timepoint.
123 :type t2: float
124 :returns: A new ba object
125 """
126 ba1 = deepcopy(ba0)
127 for i in range(len(ba0.roi)):
128 dp1 = ba0.roi[i].time2dp(t1)
129 dp2 = ba0.roi[i].time2dp(t2)
130 ba1.roi[i].time = ba1.roi[i].time[dp1:dp2]
131 ba1.roi[i].value = ba1.roi[i].value[dp1:dp2]
132 return ba1
133 """End of copyinterval()"""
134
135
136 def zerotime(ba):
137 """
138 Adjust sensorgrams to start at time[0]=0.
139
140 :param ba: Object containing the data.
141 :type ba: ba_class
142 :returns: nothing
143 """
144 for roi in ba.roi:
145 if (len(roi.time) > 0):
146 roi.time -= roi.time[0]
147 return
148 """End of zerotime()"""
149
150
151 def zerovalue(ba):
152 """
153 Adjust sensorgrams to start at value[0]=0.
154
155 :param ba: Object containing the data.
156 :type ba: ba_class
157 :returns: nothing
158 """
159 for roi in ba.roi:
160 if (len(roi.value) > 0):
161 roi.value -= roi.value[0]
162 return
163 """End of zerovalue()"""
164
165
166 def flattenlist(lst, ltypes=(list, tuple)):
167 """
168 Flatten nested lists into one list.
169
170 :param lst: Non-list, list, or list of lists!
171 :returns: flat list
172 """
173 lst = [lst]
174 ltype = type(lst)
175 lst = list(lst)
176 i = 0
177 while (len(lst) > i):
178 while isinstance(lst[i], ltypes):
179 if not lst[i]:
180 lst.pop(i)
181 i -= 1
182 break
183 else:
184 lst[i:i + 1] = lst[i]
185 i += 1
186 return ltype(lst)
187 """End of flattenlist()"""
188
189
190 def _test():
191 """
192 Automatic Code testing with doctest.
193 Doctest runs the example code in the docstrings.
194 Enable use of ellipses as wildcard for returned text.
195 """
196 import doctest
197 doctest.testmod(optionflags=doctest.ELLIPSIS)
198
199
200 if __name__ == "__main__":
201 """
202 Code testing.
203 Simply execute 'python cal_module.py'.
204 """
205 _test()
206
207
208 ################################# End of module #################################