ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/ba_class.py
Revision: 47
Committed: Fri Feb 4 19:06:37 2011 UTC (8 years, 4 months ago) by clausted
File size: 8820 byte(s)
Log Message:
Changes only to the docstrings of four modules so that Sphinx can use them.  Update references in mainapi.rst.  
Line User Rev File contents
1 clausted 7 """
2 clausted 47 ba_class
3     --------
4 clausted 7
5 clausted 47 Biosensor Array class for storing SPRI data.
6     This contains an array of SPR sensorgrams with their
7     associated models and parameters.
8     This can also contain the microarray geometery.
9    
10     .. moduleauthor:: Christopher Lausted,
11     Institute for Systems Biology,
12     Yuhang Wan,
13     OSPRAI developers.
14    
15     Examples::
16     >>> ba1 = BiosensorArray(800,1500) ## Allocate object with 800 spots of 1500 time points.
17     >>> ba1.roi[799].time[1499] = 7502.1 ## Set time in seconds.
18     >>> ba1.roi[799].value[1499] = 0.50 ## Set SPR reflectance signal.
19     >>> ba1.roi[799].name = "anti-IgG" ## Set name of microarray feature.
20 clausted 7 """
21 clausted 47 __version__ = "110204"
22 clausted 7
23 clausted 47
24 clausted 7 ## Import libraries.
25     import numpy as np
26 clausted 27 #import matplotlib.pyplot as plt
27 clausted 7 from datetime import date
28    
29    
30     class BiosensorArray(object):
31 clausted 27 """
32     The Biosensor Array class holds all ROIs (spots)
33     from an SPRI microarray run.
34    
35 clausted 47 ================ =============== =======================================
36     Property Type Description
37     ---------------- --------------- ---------------------------------------
38     roi list of object List of RegOfInterest objects.
39     rois integer Size of roi (Number of ROIs).
40     dpoints integer Number of datapoints in each ROI.
41     primarydatafiles list of string List of file names.
42     comments list of string List of comments regarding experiments.
43     ================ =============== =======================================
44 clausted 27 """
45    
46 clausted 7 def __init__(self, roi_size, datapoint_size):
47     """This object is dimensioned upon initialization.""" ## Might improve speed.
48     ## Use a list comprehension to dimension list of classes.
49     self.roi = [RegOfInterest(i,datapoint_size) for i in range(roi_size)]
50     ## Remember the dimensions for quick bounds-checking later.
51     self.rois = roi_size
52     self.dpoints = datapoint_size
53     ## Use these to keep track of laboratory notes.
54     self.primarydatafiles = [""] ## Like "spritdata.txt"
55     self.comments = [""] ## Like "Antibody data from 1 Jan 2010"
56 clausted 47 return
57 clausted 7
58     def xy2uid(self, gx, gy, x, y):
59     """Given roi coordinates, find corresponding uid (index)."""
60     coor1 = (gx, gy, x, y)
61     for ob in self.roi:
62     coor2 = (ob.gridx, ob.gridy, ob.spotx, ob.spoty)
63     if (coor1==coor2): return ob.uid ## Success.
64     return -1 ## Failure.
65    
66     def set_plot_all(self):
67     """Choose to plot every sensorgram."""
68     for ob in self.roi: ob.plottable = True
69 clausted 47 return
70 clausted 7
71     def set_plot_list(self, ilist):
72     """Choose a list of sensorgrams to plot"""
73     for ob in self.roi: ob.plottable = False
74     for i in ilist: self.roi[i].plottable = True
75 clausted 47 return
76    
77 clausted 7 ## A pyplot feature for testing purposes.
78     def plot(self):
79     """Show plot of the selected sensorgrams."""
80     plt.clf()
81     plt.title('SPR Data plotted %s' % date.today().isoformat())
82     plt.xlabel('Time (s)')
83     plt.ylabel('SPR Response')
84     plt.grid(True)
85     ## Plot traces.
86     for ob in self.roi:
87     if (ob.plottable==True):
88     mylabel = "%i:%s" % (ob.index,ob.name)
89     plt.plot(ob.time, ob.value, label=mylabel)
90     plt.legend(loc='best')
91     plt.show()
92 clausted 47 return
93    
94     """End of BiosensorArray class definition."""
95 clausted 7
96    
97     class RegOfInterest(object):
98 clausted 27 """
99     This Region Of Interest class can hold one SPR sensorgram.
100    
101 clausted 47 =========== =============== =============================================
102 clausted 27 Property Type Description
103 clausted 47 ----------- --------------- ---------------------------------------------
104 clausted 27 uid integer Unique ID integer. Never changes.
105     index integer Changes if ba size changes (add/remove rois).
106     time nparray Usually seconds.
107     value nparray Arbitrary units.
108     dpoints integer Can use for bounds checking.
109     name string Name of ROI.
110     desc string Description of ROI
111    
112     gridx integer Can be Block number from GAL file.
113     gridy integer Unused?
114     spotx integer ROI column number.
115     spoty integer ROI row number.
116     bgroi list of integer One or more background ROIs.
117     calibM float Slope used for calibration
118     calibB float Intercept used for calibration.
119     plottable boolean Whether to plot
120    
121     injconc list of float One or more analyte concentrations.
122     injstart list of float One or more injection start times.
123     injstop list of float One or more injection start times.
124     injrind list of float One or more expected refractive index jumps.
125    
126     flow float Flowrate of injection.
127     model ref to funct Model describing this ROI.
128     params dict of dict Parameters for this model.
129 clausted 47 =========== =============== =============================================
130 clausted 27 """
131    
132 clausted 7 def __init__(self, i, datapoint_size):
133     """
134     Each ROI in a list needs a unique ID number which will be i+1.
135     The time and value arrays must be allocated using datapoint_size.
136     """
137     self.uid = i+1 ## Unique ID integer. Never changes.
138     self.index = i ## Changes if ba size changes (add/remove rois).
139     self.time = np.zeros(datapoint_size, dtype=float) ## Usually seconds.
140     self.value = np.zeros(datapoint_size, dtype=float) ## Arbitrary units.
141     self.dpoints = datapoint_size ## Can use for bounds checking.
142     ## Useful optional information. Usually from GAL or Key files.
143     self.name = "Spot%i" % (i+1)
144     self.desc = "Description"
145     self.gridx = 0 ## Can be Block number from GAL file.
146     self.gridy = 0 ## Unused?
147     self.spotx = 0 ## ROI column number.
148     self.spoty = 0 ## ROI row number.
149     self.bgroi = [0] ## One or more background ROIs.
150     self.calibM = 1 ## Slope used for calibration
151     self.calibB = 0 ## Intercept used for calibration.
152     self.plottable = True ## Whether to plot
153     ## Optional injection information. Usually from Clamp or ICM Method files.
154     self.injconc = [0.0] ## One or more analyte concentrations.
155     self.injstart = [0.0] ## One or more injection start times.
156     self.injstop = [0.0] ## One or more injection start times.
157     self.injrind = [0.0] ## One or more expected refractive index jumps.
158     self.flow = 0 ## Flowrate of injection.
159 clausted 17 ## Curve fitting information. Model and model parameters.
160     ## Example model is reference to function like data=simple1to1(times,params)
161 clausted 28 ## Example params = {'Rmax': {'value':1, 'min':0, 'max':10, 'fixed':'float'} }
162 clausted 17 self.model = None ## Model describing this roi. Reference to a function.
163     self.params = [] ## Parameters for this model. A dictionary of dictionaries.
164 clausted 7
165     def time2dp(self, t):
166     """Find datapoint closest to given time."""
167 clausted 13 pos2 = self.time.searchsorted(t) ## Time point just after t.
168     pos2 = min(pos2, len(self.time)-1) ## Avoid indexing error.
169     pos1 = max(pos2-1,0) ## Time point just before t.
170 clausted 7 t2 = abs(self.time[pos2] - t)
171     t1 = abs(self.time[pos1] - t)
172 clausted 13 ## Decide if time point just before or just after is closer.
173 clausted 7 if (t2<t1):
174     return pos2
175     else:
176     return pos1
177 clausted 12
178     def time2val(self, t1, t2):
179     """Return list of SPR values between t1 and t2"""
180     dp1, dp2 = self.time2dp(t1), self.time2dp(t2)
181 clausted 13 return [self.value[i] for i in range(dp1, dp2)]
182 clausted 12
183 clausted 47 """End of RegOfInterest definition."""
184 clausted 7
185 clausted 27 '''
186 clausted 7 ## Here are a few lines to test this class.
187     if __name__ == '__main__':
188     print "Starting demo..."
189     print "Test plotting..."
190     x = BiosensorArray(10,10)
191     x.roi[0].time = np.arange(10)
192     x.roi[0].value = np.arange(10)**2
193     x.roi[1].time = np.arange(10)
194     x.roi[1].value = np.arange(10)**1.5
195     x.roi[9].time = np.arange(10)
196     x.roi[9].value = np.arange(10)**1
197     #x.set_plot_all()
198     x.set_plot_list([0,1,9])
199     x.plot()
200     print "Test xy2uid... 9 =",
201     x.roi[9].gridx = 2
202     print x.xy2uid(2,0,0,0)
203     print "Test time2dp... 5.6 =>",
204     print x.roi[9].time2dp(5.6)
205     print "Demo finished."
206 clausted 27 '''
207 clausted 7
208    
209 clausted 28 ################################# End of module #################################