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