Revision: 15 Committed: Wed Apr 14 07:14:32 2010 UTC (9 years, 6 months ago) by clausted File size: 2496 byte(s) Log Message: ```Bug fix. ```
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__ = "100413"
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 ################################# End of module #################################