Revision: 36 Committed: Sat Nov 20 01:12:26 2010 UTC (9 years, 2 months ago) by clausted File size: 3338 byte(s) Log Message: ```Add zerotime() and zerovalue() to cal_module for zeroing the initial values of sensorgrams. ```
Line File contents
1 """
2 cal: Calibration module for SPRI data in a ba class.
3 Christopher Lausted, Institute for Systems Biology,
4 OSPRAI developers
6
7 Example:
8 #import cal_module as cal
9 #io_module as io
12 #ba2 = cal.calibrate(ba1, baCal, t1, t2, n1, n2)
13 #cal.bgset(ba2, bgroi)
14 #ba3 = cal.bgsubt(ba2)
15 """
16 __version__ = "100426"
17
18
19 ## Import libraries
20 import ba_class as ba
21 import numpy as np
22 from copy import deepcopy
23
24
25 def calibrate(ba0, baCal, t1, t2, n1=1335700, n2=1336600):
26 """
27 Use data in baCal to calibrate ba0.
28 A simple two-point linear calibration using timepoints t1 and t2 and
29 corresponding refractive indices n1 and n2. If n1 and n2 are not given,
30 assume 1x PBS (n1=1335700uRIU) and 2xPBS (n2=1336600uRIU).
31 Average 30 seconds of data (t-15 to 5+15 seconds).
32 The ba object returned will remember the slopes and intercepts used.
33 """
34 ## Error checking
35 if (len(ba0.roi) != len(baCal.roi)):
36 print "Error: The ba objects are of different size."
37 return ba0
38 ## Continue.
39 ba2 = deepcopy(ba0)
40 for i, iroi in enumerate(baCal.roi):
41 ## Average values of camera units around t1 and t2.
42 cu1 = np.average(iroi.time2val(t1-15, t1+15))
43 cu2 = np.average(iroi.time2val(t2-15, t2+15))
44 ## Calculate and record slope and intercept parameters.
45 slope = (n2 - n1) / (cu2 - cu1)
46 intercept = n2 - (slope * cu2)
47 ## Apply the calibration to the new ba object.
48 ba2.roi[i].calibM = slope
49 ba2.roi[i].calibB = intercept
50 ba2.roi[i].value = ba0.roi[i].value * slope + intercept ## type np.array.
51 return ba2
52 ## End of calibrate().
53
54
55 def bgsubt(ba0):
56 """Apply the background subtraction and return a new ba object."""
57 ba1 = deepcopy(ba0)
58 for i in range(len(ba0.roi)):
59 bg = ba0.roi[i].bgroi[0] ## TODO: Only uses first bgroi for now.
60 bg -= 1 ## Decide if index or id base 0 or 1.
61 ba1.roi[i].value = ba0.roi[i].value - ba0.roi[bg].value ## type np.array.
62 return ba1
63 ## End of bgsubt().
64
65
66 def bgset(ba0, i):
67 """Set i as the background ROI for all of the ROIs."""
68 for iroi in ba0.roi: iroi.bgroi = [i]
69 return
70 ## End of bgset().
71
72
73 def copyinterval(ba0, t1, t2):
74 """Copy data from the interval between t1 and t2 and return a new ba object."""
75 ba1 = deepcopy(ba0)
76 for i in range(len(ba0.roi)):
77 dp1 = ba0.roi[i].time2dp(t1)
78 dp2 = ba0.roi[i].time2dp(t2)
79 ba1.roi[i].time = ba1.roi[i].time[dp1:dp2]
80 ba1.roi[i].value = ba1.roi[i].value[dp1:dp2]
81 return ba1
82 ## End of copyinterval().
83
84
85 def zerotime(ba):
86 """Adjust sensorgrams to start at time[0]=0."""
87 for roi in ba.roi:
88 if (len(roi.time) > 0):
89 roi.time -= roi.time[0]
90 return
91 ## End of zerotime().
92
93
94 def zerovalue(ba):
95 """Adjust sensorgrams to start at value[0]=0."""
96 for roi in ba.roi:
97 if (len(roi.value) > 0):
98 roi.value -= roi.value[0]
99 return
100 ## End of zerovalue().
101
102 ################################# End of module #################################