ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/cal_module.py
Revision: 57
Committed: Tue Apr 19 23:11:55 2011 UTC (8 years, 4 months ago) by clausted
File size: 5829 byte(s)
Log Message:
We are starting to implement automatic code testing using doctest.  The example code in the main docstring of cal_module was modified for compliance.
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 cal_module as cal
16 >>> import io_module as io
17 >>> baC = io.readicmtxt("./exampledata/example-icm.txt")
18 This ICM file has...
19 >>> ba1 = io.readicmtxt("./exampledata/example-icm.txt")
20 This ICM file has...
21 >>> t1, t2 = 1921, 2021
22 >>> n1, n2 = 1335700, 1336600
23 >>> ba2 = cal.calibrate(ba1, baC, t1, t2, n1, n2)
24 >>> bgroi = 2
25 >>> cal.bgset(ba2, bgroi)
26 >>> ba3 = cal.bgsubt(ba2)
27 """
28 __version__ = "110419"
29
30
31 ## Import libraries
32 import ba_class as ba
33 import numpy as np
34 from copy import deepcopy
35
36
37 def calibrate(ba0, baCal, t1, t2, n1=1335700, n2=1336600):
38 """
39 Use data in baCal to calibrate ba0.
40 A simple two-point linear calibration using timepoints t1 and t2 and
41 corresponding refractive indices n1 and n2.
42 Average 30 seconds of data (t-15 to 5+15 seconds).
43 The ba object returned will remember the slopes and intercepts used.
44
45 :param ba0: Object containing the data to be calibrated.
46 :type ba0: ba_class
47 :param baCal: Object containing the data to be calibrated.
48 :type baCal: ba_class
49 :param t1: Timepoint where refractive index = n1
50 :type t1: float
51 :param t2: Timepoint where refractive index = n2
52 :type t2: float
53 :param n1: First refractive index. Default=1335700 (~1x PBS).
54 :type n1: float
55 :param n2: Second refractive index. Default=1336600 (~2x PBS).
56 :type n2: float
57 :returns: nothing
58 """
59 ## Error checking
60 if (len(ba0.roi) != len(baCal.roi)):
61 print "Error: The ba objects are of different size."
62 return ba0
63 ## Continue.
64 ba2 = deepcopy(ba0)
65 for i, iroi in enumerate(baCal.roi):
66 ## Average values of camera units around t1 and t2.
67 cu1 = np.average(iroi.time2val(t1-15, t1+15))
68 cu2 = np.average(iroi.time2val(t2-15, t2+15))
69 ## Calculate and record slope and intercept parameters.
70 slope = (n2 - n1) / (cu2 - cu1)
71 intercept = n2 - (slope * cu2)
72 ## Apply the calibration to the new ba object.
73 ba2.roi[i].calibM = slope
74 ba2.roi[i].calibB = intercept
75 ba2.roi[i].value = ba0.roi[i].value * slope + intercept ## type np.array.
76 return ba2
77 """End of calibrate()"""
78
79
80 def bgsubt(ba0):
81 """
82 Apply the background subtraction and return a new ba object.
83
84 :param ba0: Object containing the source data.
85 :type ba0: ba_class
86 :returns: A new ba object
87 """
88 ba1 = deepcopy(ba0)
89 for i in range(len(ba0.roi)):
90 bg = ba0.roi[i].bgroi[0] ## TODO: Only uses first bgroi for now.
91 bg -= 1 ## Decide if index or id base 0 or 1.
92 ba1.roi[i].value = ba0.roi[i].value - ba0.roi[bg].value ## type np.array.
93 return ba1
94 """End of bgsubt()"""
95
96
97 def bgset(ba0, i):
98 """
99 Set i as the background ROI for all of the ROIs.
100
101 :param ba0: Object containing the data to be calibrated.
102 :type ba0: ba_class
103 :param i: Object containing the data to be calibrated.
104 :type i: ba_class
105 :returns: nothing
106 """
107 for iroi in ba0.roi: iroi.bgroi = [i]
108 return
109 """End of bgset()"""
110
111
112 def copyinterval(ba0, t1, t2):
113 """
114 Copy data from the interval between t1 and t2 and return a new ba object.
115
116 :param ba0: Object containing the source data.
117 :type ba0: ba_class
118 :param t1: Beginning timepoint.
119 :type t1: float
120 :param t2: Ending timepoint.
121 :type t2: float
122 :returns: A new ba object
123 """
124 ba1 = deepcopy(ba0)
125 for i in range(len(ba0.roi)):
126 dp1 = ba0.roi[i].time2dp(t1)
127 dp2 = ba0.roi[i].time2dp(t2)
128 ba1.roi[i].time = ba1.roi[i].time[dp1:dp2]
129 ba1.roi[i].value = ba1.roi[i].value[dp1:dp2]
130 return ba1
131 """End of copyinterval()"""
132
133
134 def zerotime(ba):
135 """
136 Adjust sensorgrams to start at time[0]=0.
137
138 :param ba: Object containing the data.
139 :type ba: ba_class
140 :returns: nothing
141 """
142 for roi in ba.roi:
143 if (len(roi.time) > 0):
144 roi.time -= roi.time[0]
145 return
146 """End of zerotime()"""
147
148
149 def zerovalue(ba):
150 """
151 Adjust sensorgrams to start at value[0]=0.
152
153 :param ba: Object containing the data.
154 :type ba: ba_class
155 :returns: nothing
156 """
157 for roi in ba.roi:
158 if (len(roi.value) > 0):
159 roi.value -= roi.value[0]
160 return
161 """End of zerovalue()"""
162
163
164 def flattenlist(lst, ltypes=(list, tuple)):
165 """
166 Flatten nested lists into one list.
167
168 :param lst: Non-list, list, or list of lists!
169 :returns: flat list
170 """
171 lst = [lst]
172 ltype = type(lst)
173 lst = list(lst)
174 i = 0
175 while (len(lst) > i):
176 while isinstance(lst[i], ltypes):
177 if not lst[i]:
178 lst.pop(i)
179 i -= 1
180 break
181 else:
182 lst[i:i + 1] = lst[i]
183 i += 1
184 return ltype(lst)
185 """End of flattenlist()"""
186
187
188 if __name__ == "__main__":
189 """
190 Code testing.
191 Simply execute 'python cal_module.py'.
192 Doctest runs the example code in the docstrings.
193 Can use ellipses as wildcard for returned text.
194 """
195 import doctest
196 doctest.testmod(optionflags=doctest.ELLIPSIS)
197
198
199 ################################# End of module #################################