ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/vu_module.py
Revision: 47
Committed: Fri Feb 4 19:06:37 2011 UTC (8 years, 7 months ago) by clausted
File size: 10019 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 11 """
2 clausted 44 vu_module
3     ---------
4 clausted 11
5 clausted 44 This is the viewer module for SPR data in a ba_class.
6     Traces can be displayed as dotted lines, continuous lines, or combinations.
7     Typically, up to six traces are displayed at a time.
8     Features include zooming, measurements, and save-to-PNG.
9     Graphics utilize the old Tk GUI toolkit.
10    
11     .. moduleauthor:: Christopher Lausted,
12     Institute for Systems Biology,
13     OSPRAI developers.
14 clausted 47 Examples::
15 clausted 44
16     >>> import vu_module as vu
17     >>> import io_module as io
18     >>> ba1 = io.readsprit("spritdata.txt")
19     >>> vu.dotgraph(ba1, "Title")
20     >>> vu.linegraph(ba1, "Title")
21     >>> vu.dualgraph(ba1, ba2, "Title")
22     >>> vu.scatterplot(ba1, t1, t2, t3, t4, title)
23 clausted 11 """
24 clausted 44 __version__ = "110202"
25 clausted 11
26    
27     ## Import libraries
28     import ba_class as ba ## Our Biosensor Array class.
29 clausted 14 import numpy as np
30 clausted 11 from datetime import date
31     import Tkinter as Tk
32     import tkSimpleDialog as dialog
33     import matplotlib as mpl
34     mpl.use('TkAgg')
35     from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
36     from matplotlib.figure import Figure
37    
38    
39 clausted 14 def linegraph(baLine, title=""):
40 clausted 44 """
41     Display signal vs time using continuous lines.
42    
43     :param baLine: Object containing the data.
44     :type baLine: ba_class
45     :param title: Title to place above the graph.
46     :type title: str
47     :returns: nothing
48     """
49 clausted 11 global root, fig, ax, canvas ## Tk/mpl globals.
50 clausted 44 global ba0, ba1, ba2 ## Ref to ba for __roiList, dotgraph, linegraph
51 clausted 14 global gTitle ## Title for graph.
52 clausted 11 ba0, ba1, ba2 = baLine, 0, baLine
53 clausted 14 gTitle = title
54 clausted 44 __createSensorgramTkWindow()
55     __show()
56 clausted 11 root.mainloop()
57     return
58 clausted 44 """
59     How does linegraph() work? It sets some global values, then calls __show().
60     """
61 clausted 11
62 clausted 14 def dotgraph(baDot, title=""):
63 clausted 44 """
64     Display signal vs time using dotted lines.
65    
66     :param baDot: Object containing the data.
67     :type baDot: ba_class
68     :param title: Title to place above the graph.
69     :type title: str
70     :returns: nothing
71     """
72 clausted 11 global root, fig, ax, canvas ## Tk/mpl globals..
73 clausted 44 global ba0, ba1, ba2 ## Ref to ba for __roiList, dotgraph, linegraph
74 clausted 14 global gTitle ## Title for graph.
75 clausted 11 ba0, ba1, ba2 = baDot, baDot, 0
76 clausted 14 gTitle = title
77 clausted 44 __createSensorgramTkWindow()
78     __show()
79 clausted 11 root.mainloop()
80     return
81     ## End of dotgraph().
82 clausted 44 """
83     How does dotgraph() work? It sets some global values, then calls __show().
84     """
85 clausted 11
86 clausted 14 def dualgraph(baDot, baLine, title=""):
87 clausted 44 """
88     Display signal vs time using dotted AND continuous lines.
89    
90     :param baDot: Object containing the dotted-line data.
91     :type baDot: ba_class
92     :param baLine: Object containing the continuous-line data.
93     :type baLine: ba_class
94     :param title: Title to place above the graph.
95     :type title: str
96     :returns: nothing
97     """
98 clausted 11 global root, fig, ax, canvas ## Tk/mpl globals.
99 clausted 44 global ba0, ba1, ba2 ## Ref to ba for __roiList, dotgraph, linegraph
100 clausted 14 global gTitle ## Title for graph.
101 clausted 11 ba0, ba1, ba2 = baDot, baDot, baLine
102 clausted 14 gTitle = title
103 clausted 44 __createSensorgramTkWindow()
104     __show()
105 clausted 11 root.mainloop()
106     return
107 clausted 44 """
108     How does dualgraph() work? It sets some global values, then calls __show().
109     """
110 clausted 11
111 clausted 44 def __show():
112     """
113     Graph data of ba using Tk window.
114     """
115 clausted 11 global root, fig, ax, canvas
116     global ba1, ba2
117 clausted 14 global gTitle
118 clausted 11
119     ## Prepare graph labels.
120     ax.clear()
121 clausted 14 if (gTitle==""): gTitle='SPR Data plotted %s' % date.today().isoformat()
122     ax.set_title(gTitle)
123 clausted 11 ax.set_xlabel('Time (s)')
124     ax.set_ylabel('SPR Response')
125     ax.grid(True)
126    
127     ## Plot traces of real data.
128     if (ba1 != 0):
129     ## Dotted traces.
130     marker = ['r,', 'g,', 'b,', 'c,', 'm,', 'y,'] + ['k,'] * 100
131 clausted 44 for i,j in enumerate(__roiList()):
132 clausted 11 txt = str(j) + ':' + ba1.roi[j].name
133     t = ba1.roi[j].time
134     y = ba1.roi[j].value
135     ax.plot(t, y, marker[i], label=txt)
136     if (ba2 != 0):
137     ## Line traces.
138     marker = ['r', 'g', 'b', 'c', 'm', 'y'] + ['k'] * 100
139 clausted 44 for i,j in enumerate(__roiList()):
140 clausted 11 txt = str(j) + ':' + ba2.roi[j].name
141     t = ba2.roi[j].time
142     y = ba2.roi[j].value
143     ax.plot(t, y, marker[i], label=txt)
144    
145     ## Format legend.
146     leg = ax.legend(loc='upper right')
147     for t in leg.get_texts(): t.set_fontsize('x-small')
148     #for t in leg.get_lines(): t.set_linestyle('None')
149     #labels = [line.get_label() for line in ax.lines]
150     #fig.legend(ax.lines, labels, 'right')
151    
152     canvas.draw()
153     return
154 clausted 44 """
155     End of __show().
156     """
157 clausted 11
158 clausted 44 def __btnNext():
159     """
160     Show next six traces on the graph.
161     """
162 clausted 11 global ba0
163 clausted 44 lo = max(__roiList()) + 1
164 clausted 16 if (lo == len(ba0.roi)): return
165     lo = max(lo,0)
166     hi = lo + 6
167     hi = min(hi, len(ba0.roi))
168     ba0.set_plot_list(range(lo, hi))
169 clausted 44 __show()
170 clausted 11 return
171    
172 clausted 44 def __btnPrev():
173     """
174     Show the previous six traces on the graph.
175     """
176 clausted 11 global ba0
177 clausted 44 lo = min(__roiList()) - 6
178 clausted 16 lo = max(lo,0)
179     hi = lo + 6
180     hi = min(hi, len(ba0.roi))
181     ba0.set_plot_list(range(lo, hi))
182 clausted 44 __show()
183 clausted 11 return
184    
185 clausted 44 def __btnTraces():
186     """
187     Ask for list of traces to show on graph.
188     """
189 clausted 11 global ba0
190     txt = "Which ROIs would you like to see? (e.g. 19,20,21)"
191     txt = dialog.askstring(title="Input",prompt=txt)
192     txt = txt + "," ## Be sure eval() produces a list.
193     ba0.set_plot_list(eval(txt))
194 clausted 44 __show()
195 clausted 11 return
196    
197    
198 clausted 44 def __roiList():
199     """
200     List of first six plottable ROIs.
201     """
202 clausted 11 global ba0
203     #x = [i for i in range(len(ba.roi)) if ba0.roi[i].plottable]
204     x = [i.index for i in ba0.roi if i.plottable]
205     return x[:6]
206    
207    
208 clausted 44 def __createSensorgramTkWindow():
209 clausted 14 """
210     Create a Tk window for the Matplotlib graph.
211 clausted 44 This window has three buttons which will allow the user to browse through many traces.
212 clausted 14 """
213 clausted 11 global root, fig, ax, canvas
214     root = Tk.Tk()
215 clausted 14 root.wm_title("TK matplotlib Osprai Sensorgram")
216 clausted 11 ## Prepare a matplotlib Figure/Axes.
217     fig = Figure(figsize=(6,4), dpi=100)
218     ax = fig.add_subplot(111)
219     ax.hold(True) # Hold data until ax.clear().
220     #ax.set_position([0.1, 0.1, 0.6, 0.8]) # Fraction of frame [left,bot,w,h]
221     ## Define tk.DrawingArea
222     canvas = FigureCanvasTkAgg(fig, master=root)
223 clausted 44 #canvas.__show()
224 clausted 11 canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
225     ## Add a pylab-style toolbar.
226     toolbar = NavigationToolbar2TkAgg( canvas, root )
227     toolbar.update()
228     canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
229     ## Add buttons.
230 clausted 44 button1 = Tk.Button(master=root, text='Next6', command=__btnNext)
231 clausted 11 button1.pack(side=Tk.RIGHT)
232 clausted 44 button2 = Tk.Button(master=root, text='Choose..', command=__btnTraces)
233 clausted 11 button2.pack(side=Tk.RIGHT)
234 clausted 44 button3 = Tk.Button(master=root, text='Prev6', command=__btnPrev)
235 clausted 11 button3.pack(side=Tk.RIGHT)
236     return
237 clausted 44 """
238     End of __createSensorgramTkWindow().
239     """
240 clausted 14
241     ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
242 clausted 11
243    
244 clausted 14 def scatterplot(ba0, t1, t2, t3, t4, title):
245     """
246     Plot binding changes between (t4-t3) versus (t2-t1).
247     Also print the results to the standard output.
248 clausted 44
249     :param ba0: Object containing the data.
250     :type ba0: ba_class
251     :param t1: Timepoint before binding, first axis.
252     :type t1: float
253     :param t2: Timepoint after binding, first axis.
254     :type t2: float
255     :param t3: Timepoint before binding, second axis.
256     :type t3: float
257     :param t4: Timepoint after binding, second axis.
258     :type t4: float
259     :param title: Title to place above the graph.
260     :type title: str
261     :returns: nothing
262 clausted 14 """
263     root = Tk.Tk()
264     root.wm_title("TK matplotlib Osprai Sensorgram")
265     ## Prepare a matplotlib Figure/Axes.
266     fig = Figure(figsize=(6,4), dpi=100)
267     ax = fig.add_subplot(111)
268     ax.hold(True) # Hold data until ax.clear().
269     ## Define tk.DrawingArea
270     canvas = FigureCanvasTkAgg(fig, master=root)
271     canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
272     ## Add a pylab-style toolbar.
273     toolbar = NavigationToolbar2TkAgg( canvas, root )
274     toolbar.update()
275     canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
276    
277     ## Prepare graph labels.
278     ax.clear()
279     if (title==""): title='Scatterplot of %s' % date.today().isoformat()
280     ax.set_title(title)
281     ax.set_xlabel('Binding12')
282     ax.set_ylabel('Binding34')
283     ax.grid(True)
284    
285     ## Get array of values at each of the four time points.
286     y1 = np.array([np.average(iroi.time2val(t1-15, t1+15)) for iroi in ba0.roi])
287     y2 = np.array([np.average(iroi.time2val(t2-15, t2+15)) for iroi in ba0.roi])
288     y3 = np.array([np.average(iroi.time2val(t3-15, t3+15)) for iroi in ba0.roi])
289     y4 = np.array([np.average(iroi.time2val(t4-15, t4+15)) for iroi in ba0.roi])
290     dx, dy = (y2-y1), (y4-y3)
291    
292     ## Print to stdout
293     print "Name", "Binding12", "Binding34"
294     for i in range(len(dx)): print ba0.roi[i].name, dx[i], dy[i]
295    
296     ## Plot
297     ax.plot(dx, dy, 'bo') ## Blue circles with 'bo'.
298     canvas.draw()
299     root.mainloop()
300     return
301 clausted 44 """
302     How does scatterplot() work?
303     First create a Tk window for the scatterplot graph.
304     Variables root, fig, ax, canvas are local here, not global.
305     Use list comprehensions to get list of average signal 30 sec around t1, t2, t3, t4.
306     Plot signal change between t1-t2 against t3-t4. Also print to std output.
307     """
308 clausted 14
309    
310 clausted 11 ## Print a message to the users so they see this module is loaded.
311     print "Loaded vu_module.py version ", __version__
312    
313 clausted 44 ################################# End of module #################################