1 |
clausted |
12 |
"""
|
2 |
|
|
cal: Calibration module for SPRI data in a ba class.
|
3 |
|
|
Christopher Lausted, Institute for Systems Biology,
|
4 |
|
|
OSPRAI developers
|
5 |
clausted |
24 |
Last modified on 100426 (yymmdd)
|
6 |
clausted |
12 |
|
7 |
|
|
Example:
|
8 |
|
|
#import cal_module as cal
|
9 |
|
|
#io_module as io
|
10 |
clausted |
14 |
#baC = io.readsprit("calibrationdata.txt")
|
11 |
clausted |
12 |
#ba1 = io.readsprit("spritdata.txt")
|
12 |
clausted |
14 |
#ba2 = cal.calibrate(ba1, baCal, t1, t2, n1, n2)
|
13 |
|
|
#cal.bgset(ba2, bgroi)
|
14 |
|
|
#ba3 = cal.bgsubt(ba2)
|
15 |
clausted |
12 |
"""
|
16 |
clausted |
24 |
__version__ = "100426"
|
17 |
clausted |
12 |
|
18 |
|
|
|
19 |
|
|
## Import libraries
|
20 |
|
|
import ba_class as ba
|
21 |
|
|
import numpy as np
|
22 |
|
|
from copy import deepcopy
|
23 |
|
|
|
24 |
|
|
|
25 |
clausted |
14 |
def calibrate(ba0, baCal, t1, t2, n1=1335700, n2=1336600):
|
26 |
clausted |
12 |
"""
|
27 |
clausted |
14 |
Use data in baCal to calibrate ba0.
|
28 |
clausted |
12 |
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 |
clausted |
14 |
if (len(ba0.roi) != len(baCal.roi)):
|
36 |
clausted |
12 |
print "Error: The ba objects are of different size."
|
37 |
|
|
return ba0
|
38 |
|
|
## Continue.
|
39 |
|
|
ba2 = deepcopy(ba0)
|
40 |
clausted |
14 |
for i, iroi in enumerate(baCal.roi):
|
41 |
clausted |
12 |
## 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 |
clausted |
14 |
ba2.roi[i].value = ba0.roi[i].value * slope + intercept ## type np.array.
|
51 |
clausted |
12 |
return ba2
|
52 |
|
|
## End of calibrate().
|
53 |
clausted |
14 |
|
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 |
clausted |
15 |
for iroi in ba0.roi: iroi.bgroi = [i]
|
69 |
clausted |
14 |
return
|
70 |
|
|
## End of bgset().
|
71 |
clausted |
12 |
|
72 |
clausted |
24 |
|
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 |
clausted |
12 |
################################# End of module ################################# |