ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/cal_module.py
Revision: 58
Committed: Wed May 11 21:25:10 2011 UTC (8 years, 4 months ago) by clausted
File size: 6184 byte(s)
Log Message:
Updated docstring usage examples for doctest.
Line User Rev File contents
1 clausted 12 """
2 clausted 47 cal_module
3     ----------
4 clausted 12
5 clausted 47 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 clausted 54 OSPRAI developers.
12    
13 clausted 57 Example::
14 clausted 47
15 clausted 58 >>> 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 clausted 57 >>> n1, n2 = 1335700, 1336600
21 clausted 58 >>> 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 clausted 12 """
30 clausted 58 __version__ = "110425"
31 clausted 12
32    
33     ## Import libraries
34     import ba_class as ba
35     import numpy as np
36     from copy import deepcopy
37    
38    
39 clausted 14 def calibrate(ba0, baCal, t1, t2, n1=1335700, n2=1336600):
40 clausted 12 """
41 clausted 14 Use data in baCal to calibrate ba0.
42 clausted 12 A simple two-point linear calibration using timepoints t1 and t2 and
43 clausted 47 corresponding refractive indices n1 and n2.
44 clausted 12 Average 30 seconds of data (t-15 to 5+15 seconds).
45     The ba object returned will remember the slopes and intercepts used.
46 clausted 47
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 clausted 12 """
61     ## Error checking
62 clausted 14 if (len(ba0.roi) != len(baCal.roi)):
63 clausted 12 print "Error: The ba objects are of different size."
64     return ba0
65     ## Continue.
66     ba2 = deepcopy(ba0)
67 clausted 14 for i, iroi in enumerate(baCal.roi):
68 clausted 12 ## 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 clausted 14 ba2.roi[i].value = ba0.roi[i].value * slope + intercept ## type np.array.
78 clausted 12 return ba2
79 clausted 47 """End of calibrate()"""
80 clausted 14
81    
82     def bgsubt(ba0):
83 clausted 47 """
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 clausted 14 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 clausted 58 #bg -= 1 ## Decide if index or id base 0 or 1.
94 clausted 14 ba1.roi[i].value = ba0.roi[i].value - ba0.roi[bg].value ## type np.array.
95     return ba1
96 clausted 47 """End of bgsubt()"""
97 clausted 14
98    
99     def bgset(ba0, i):
100 clausted 47 """
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 clausted 15 for iroi in ba0.roi: iroi.bgroi = [i]
110 clausted 14 return
111 clausted 47 """End of bgset()"""
112 clausted 12
113 clausted 24
114     def copyinterval(ba0, t1, t2):
115 clausted 47 """
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 clausted 24 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 clausted 47 """End of copyinterval()"""
134 clausted 24
135    
136 clausted 36 def zerotime(ba):
137 clausted 47 """
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 clausted 36 for roi in ba.roi:
145     if (len(roi.time) > 0):
146     roi.time -= roi.time[0]
147     return
148 clausted 47 """End of zerotime()"""
149 clausted 36
150    
151     def zerovalue(ba):
152 clausted 47 """
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 clausted 36 for roi in ba.roi:
160     if (len(roi.value) > 0):
161     roi.value -= roi.value[0]
162     return
163 clausted 47 """End of zerovalue()"""
164 clausted 49
165 clausted 36
166 clausted 49 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 clausted 57
190 clausted 58 def _test():
191 clausted 57 """
192 clausted 58 Automatic Code testing with doctest.
193 clausted 57 Doctest runs the example code in the docstrings.
194 clausted 58 Enable use of ellipses as wildcard for returned text.
195 clausted 57 """
196     import doctest
197     doctest.testmod(optionflags=doctest.ELLIPSIS)
198    
199    
200 clausted 58 if __name__ == "__main__":
201     """
202     Code testing.
203     Simply execute 'python cal_module.py'.
204     """
205     _test()
206    
207    
208 clausted 47 ################################# End of module #################################