ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/cal_module.py
Revision: 49
Committed: Tue Feb 8 01:38:34 2011 UTC (8 years, 5 months ago) by clausted
File size: 5393 byte(s)
Log Message:
Add readcsv() function to io_module since sometimes we might have data in a spreadsheet with the left column containing time data and the rest of the columns containing sensorgram data.  
Add a flattenlist() function to cal_module.  It might be useful when using the ba.roi.bgroi property. Sometimes it is a single number, but sometimes we'd like it to be a list of roi numbers.  
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 Examples::
13
14 >>> import cal_module as cal
15 >>> io_module as io
16 >>> baC = io.readsprit("calibrationdata.txt")
17 >>> ba1 = io.readsprit("spritdata.txt")
18 >>> ba2 = cal.calibrate(ba1, baCal, t1, t2, n1, n2)
19 >>> cal.bgset(ba2, bgroi)
20 >>> ba3 = cal.bgsubt(ba2)
21 """
22 __version__ = "110204"
23
24
25 ## Import libraries
26 import ba_class as ba
27 import numpy as np
28 from copy import deepcopy
29
30
31 def calibrate(ba0, baCal, t1, t2, n1=1335700, n2=1336600):
32 """
33 Use data in baCal to calibrate ba0.
34 A simple two-point linear calibration using timepoints t1 and t2 and
35 corresponding refractive indices n1 and n2.
36 Average 30 seconds of data (t-15 to 5+15 seconds).
37 The ba object returned will remember the slopes and intercepts used.
38
39 :param ba0: Object containing the data to be calibrated.
40 :type ba0: ba_class
41 :param baCal: Object containing the data to be calibrated.
42 :type baCal: ba_class
43 :param t1: Timepoint where refractive index = n1
44 :type t1: float
45 :param t2: Timepoint where refractive index = n2
46 :type t2: float
47 :param n1: First refractive index. Default=1335700 (~1x PBS).
48 :type n1: float
49 :param n2: Second refractive index. Default=1336600 (~2x PBS).
50 :type n2: float
51 :returns: nothing
52 """
53 ## Error checking
54 if (len(ba0.roi) != len(baCal.roi)):
55 print "Error: The ba objects are of different size."
56 return ba0
57 ## Continue.
58 ba2 = deepcopy(ba0)
59 for i, iroi in enumerate(baCal.roi):
60 ## Average values of camera units around t1 and t2.
61 cu1 = np.average(iroi.time2val(t1-15, t1+15))
62 cu2 = np.average(iroi.time2val(t2-15, t2+15))
63 ## Calculate and record slope and intercept parameters.
64 slope = (n2 - n1) / (cu2 - cu1)
65 intercept = n2 - (slope * cu2)
66 ## Apply the calibration to the new ba object.
67 ba2.roi[i].calibM = slope
68 ba2.roi[i].calibB = intercept
69 ba2.roi[i].value = ba0.roi[i].value * slope + intercept ## type np.array.
70 return ba2
71 """End of calibrate()"""
72
73
74 def bgsubt(ba0):
75 """
76 Apply the background subtraction and return a new ba object.
77
78 :param ba0: Object containing the source data.
79 :type ba0: ba_class
80 :returns: A new ba object
81 """
82 ba1 = deepcopy(ba0)
83 for i in range(len(ba0.roi)):
84 bg = ba0.roi[i].bgroi[0] ## TODO: Only uses first bgroi for now.
85 bg -= 1 ## Decide if index or id base 0 or 1.
86 ba1.roi[i].value = ba0.roi[i].value - ba0.roi[bg].value ## type np.array.
87 return ba1
88 """End of bgsubt()"""
89
90
91 def bgset(ba0, i):
92 """
93 Set i as the background ROI for all of the ROIs.
94
95 :param ba0: Object containing the data to be calibrated.
96 :type ba0: ba_class
97 :param i: Object containing the data to be calibrated.
98 :type i: ba_class
99 :returns: nothing
100 """
101 for iroi in ba0.roi: iroi.bgroi = [i]
102 return
103 """End of bgset()"""
104
105
106 def copyinterval(ba0, t1, t2):
107 """
108 Copy data from the interval between t1 and t2 and return a new ba object.
109
110 :param ba0: Object containing the source data.
111 :type ba0: ba_class
112 :param t1: Beginning timepoint.
113 :type t1: float
114 :param t2: Ending timepoint.
115 :type t2: float
116 :returns: A new ba object
117 """
118 ba1 = deepcopy(ba0)
119 for i in range(len(ba0.roi)):
120 dp1 = ba0.roi[i].time2dp(t1)
121 dp2 = ba0.roi[i].time2dp(t2)
122 ba1.roi[i].time = ba1.roi[i].time[dp1:dp2]
123 ba1.roi[i].value = ba1.roi[i].value[dp1:dp2]
124 return ba1
125 """End of copyinterval()"""
126
127
128 def zerotime(ba):
129 """
130 Adjust sensorgrams to start at time[0]=0.
131
132 :param ba: Object containing the data.
133 :type ba: ba_class
134 :returns: nothing
135 """
136 for roi in ba.roi:
137 if (len(roi.time) > 0):
138 roi.time -= roi.time[0]
139 return
140 """End of zerotime()"""
141
142
143 def zerovalue(ba):
144 """
145 Adjust sensorgrams to start at value[0]=0.
146
147 :param ba: Object containing the data.
148 :type ba: ba_class
149 :returns: nothing
150 """
151 for roi in ba.roi:
152 if (len(roi.value) > 0):
153 roi.value -= roi.value[0]
154 return
155 """End of zerovalue()"""
156
157
158 def flattenlist(lst, ltypes=(list, tuple)):
159 """
160 Flatten nested lists into one list.
161
162 :param lst: Non-list, list, or list of lists!
163 :returns: flat list
164 """
165 lst = [lst]
166 ltype = type(lst)
167 lst = list(lst)
168 i = 0
169 while (len(lst) > i):
170 while isinstance(lst[i], ltypes):
171 if not lst[i]:
172 lst.pop(i)
173 i -= 1
174 break
175 else:
176 lst[i:i + 1] = lst[i]
177 i += 1
178 return ltype(lst)
179 """End of flattenlist()"""
180
181 ################################# End of module #################################