ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/vu_module.py
Revision: 16
Committed: Sun Apr 18 05:34:10 2010 UTC (9 years, 5 months ago) by clausted
File size: 7919 byte(s)
Log Message:
Fix bug in "Next" button script.
Line File contents
1 """
2 vu: Viewer module for SPRI data in a ba class.
3 This uses the old Tk GUI toolkit.
4 Christopher Lausted, Institute for Systems Biology,
5 OSPRAI developers
6 Last modified on 100416 (yymmdd)
7
8 Example:
9 #import vu_module as vu
10 #io_module as io
11 #ba1 = io.readsprit("spritdata.txt")
12 #vu.dotgraph(ba1, "Title")
13 #vu.linegraph(ba1, "Title")
14 #vu.dualgraph(ba1, ba2, "Title")
15 #vu.scatterplot(ba1, t1, t2, t3, t4, title)
16 """
17 __version__ = "100416"
18
19
20 ## Import libraries
21 import ba_class as ba ## Our Biosensor Array class.
22 import numpy as np
23 from datetime import date
24 import Tkinter as Tk
25 import tkSimpleDialog as dialog
26 import matplotlib as mpl
27 mpl.use('TkAgg')
28 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
29 from matplotlib.figure import Figure
30
31
32 def linegraph(baLine, title=""):
33 """Graph data of baLine using lines."""
34 global root, fig, ax, canvas ## Tk/mpl globals.
35 global ba0, ba1, ba2 ## Ref to ba for roilist, dotgraph, linegraph
36 global gTitle ## Title for graph.
37 ba0, ba1, ba2 = baLine, 0, baLine
38 gTitle = title
39 createSensorgramTkWindow()
40 show()
41 root.mainloop()
42 return
43 ## End of linegraph().
44
45 def dotgraph(baDot, title=""):
46 """Graph data of baDot using dots."""
47 global root, fig, ax, canvas ## Tk/mpl globals..
48 global ba0, ba1, ba2 ## Ref to ba for roilist, dotgraph, linegraph
49 global gTitle ## Title for graph.
50 ba0, ba1, ba2 = baDot, baDot, 0
51 gTitle = title
52 createSensorgramTkWindow()
53 show()
54 root.mainloop()
55 return
56 ## End of dotgraph().
57
58 def dualgraph(baDot, baLine, title=""):
59 """Graph data using dots and lines."""
60 global root, fig, ax, canvas ## Tk/mpl globals.
61 global ba0, ba1, ba2 ## Ref to ba for roilist, dotgraph, linegraph
62 global gTitle ## Title for graph.
63 ba0, ba1, ba2 = baDot, baDot, baLine
64 gTitle = title
65 createSensorgramTkWindow()
66 show()
67 root.mainloop()
68 return
69 ## End of dualgraph().
70
71 def show():
72 """Graph data of ba using Tk window."""
73 global root, fig, ax, canvas
74 global ba1, ba2
75 global gTitle
76
77 ## Prepare graph labels.
78 ax.clear()
79 if (gTitle==""): gTitle='SPR Data plotted %s' % date.today().isoformat()
80 ax.set_title(gTitle)
81 ax.set_xlabel('Time (s)')
82 ax.set_ylabel('SPR Response')
83 ax.grid(True)
84
85 ## Plot traces of real data.
86 if (ba1 != 0):
87 ## Dotted traces.
88 marker = ['r,', 'g,', 'b,', 'c,', 'm,', 'y,'] + ['k,'] * 100
89 for i,j in enumerate(roiList()):
90 txt = str(j) + ':' + ba1.roi[j].name
91 t = ba1.roi[j].time
92 y = ba1.roi[j].value
93 ax.plot(t, y, marker[i], label=txt)
94 if (ba2 != 0):
95 ## Line traces.
96 marker = ['r', 'g', 'b', 'c', 'm', 'y'] + ['k'] * 100
97 for i,j in enumerate(roiList()):
98 txt = str(j) + ':' + ba2.roi[j].name
99 t = ba2.roi[j].time
100 y = ba2.roi[j].value
101 ax.plot(t, y, marker[i], label=txt)
102
103 ## Format legend.
104 leg = ax.legend(loc='upper right')
105 for t in leg.get_texts(): t.set_fontsize('x-small')
106 #for t in leg.get_lines(): t.set_linestyle('None')
107 #labels = [line.get_label() for line in ax.lines]
108 #fig.legend(ax.lines, labels, 'right')
109
110 canvas.draw()
111 return
112 ## End of show().
113
114 def btnNext():
115 ## Show next six traces on the graph.
116 global ba0
117 lo = max(roiList()) + 1
118 if (lo == len(ba0.roi)): return
119 lo = max(lo,0)
120 hi = lo + 6
121 hi = min(hi, len(ba0.roi))
122 ba0.set_plot_list(range(lo, hi))
123 show()
124 return
125
126 def btnPrev():
127 ## Show the previous six traces on the graph.
128 global ba0
129 lo = min(roiList()) - 6
130 lo = max(lo,0)
131 hi = lo + 6
132 hi = min(hi, len(ba0.roi))
133 ba0.set_plot_list(range(lo, hi))
134 show()
135 return
136
137 def btnTraces():
138 ## Ask for list of traces to show on graph.
139 global ba0
140 txt = "Which ROIs would you like to see? (e.g. 19,20,21)"
141 txt = dialog.askstring(title="Input",prompt=txt)
142 txt = txt + "," ## Be sure eval() produces a list.
143 ba0.set_plot_list(eval(txt))
144 show()
145 return
146
147
148 def roiList():
149 ## List of first six plottable ROIs.
150 global ba0
151 #x = [i for i in range(len(ba.roi)) if ba0.roi[i].plottable]
152 x = [i.index for i in ba0.roi if i.plottable]
153 return x[:6]
154
155
156 def createSensorgramTkWindow():
157 """
158 Create a Tk window for the Matplotlib graph.
159 This window has three buttons which will allow the user to
160 """
161 global root, fig, ax, canvas
162 root = Tk.Tk()
163 root.wm_title("TK matplotlib Osprai Sensorgram")
164 ## Prepare a matplotlib Figure/Axes.
165 fig = Figure(figsize=(6,4), dpi=100)
166 ax = fig.add_subplot(111)
167 ax.hold(True) # Hold data until ax.clear().
168 #ax.set_position([0.1, 0.1, 0.6, 0.8]) # Fraction of frame [left,bot,w,h]
169 ## Define tk.DrawingArea
170 canvas = FigureCanvasTkAgg(fig, master=root)
171 #canvas.show()
172 canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
173 ## Add a pylab-style toolbar.
174 toolbar = NavigationToolbar2TkAgg( canvas, root )
175 toolbar.update()
176 canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
177 ## Add buttons.
178 button1 = Tk.Button(master=root, text='Next6', command=btnNext)
179 button1.pack(side=Tk.RIGHT)
180 button2 = Tk.Button(master=root, text='Choose..', command=btnTraces)
181 button2.pack(side=Tk.RIGHT)
182 button3 = Tk.Button(master=root, text='Prev6', command=btnPrev)
183 button3.pack(side=Tk.RIGHT)
184 return
185 ## End of createSensorgramTkWindow().
186
187
188 ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
189
190
191 def scatterplot(ba0, t1, t2, t3, t4, title):
192 """
193 Plot binding changes between (t4-t3) versus (t2-t1).
194 Also print the results to the standard output.
195 """
196 ## Create a Tk window for the ccatterplot graph.
197 ## Variables root, fig, ax, canvas are local here, not global.
198 root = Tk.Tk()
199 root.wm_title("TK matplotlib Osprai Sensorgram")
200 ## Prepare a matplotlib Figure/Axes.
201 fig = Figure(figsize=(6,4), dpi=100)
202 ax = fig.add_subplot(111)
203 ax.hold(True) # Hold data until ax.clear().
204 ## Define tk.DrawingArea
205 canvas = FigureCanvasTkAgg(fig, master=root)
206 canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
207 ## Add a pylab-style toolbar.
208 toolbar = NavigationToolbar2TkAgg( canvas, root )
209 toolbar.update()
210 canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
211
212 ## Prepare graph labels.
213 ax.clear()
214 if (title==""): title='Scatterplot of %s' % date.today().isoformat()
215 ax.set_title(title)
216 ax.set_xlabel('Binding12')
217 ax.set_ylabel('Binding34')
218 ax.grid(True)
219
220 ## Get array of values at each of the four time points.
221 y1 = np.array([np.average(iroi.time2val(t1-15, t1+15)) for iroi in ba0.roi])
222 y2 = np.array([np.average(iroi.time2val(t2-15, t2+15)) for iroi in ba0.roi])
223 y3 = np.array([np.average(iroi.time2val(t3-15, t3+15)) for iroi in ba0.roi])
224 y4 = np.array([np.average(iroi.time2val(t4-15, t4+15)) for iroi in ba0.roi])
225 dx, dy = (y2-y1), (y4-y3)
226
227 ## Print to stdout
228 print "Name", "Binding12", "Binding34"
229 for i in range(len(dx)): print ba0.roi[i].name, dx[i], dy[i]
230
231 ## Plot
232 ax.plot(dx, dy, 'bo') ## Blue circles with 'bo'.
233 canvas.draw()
234 root.mainloop()
235 return
236 ## End of scatterplot()
237
238
239 ## Print a message to the users so they see this module is loaded.
240 print "Loaded vu_module.py version ", __version__
241
242 ################################# End of module #################################