ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/vu_module.py
Revision: 11
Committed: Mon Apr 12 22:59:57 2010 UTC (9 years, 1 month ago) by clausted
File size: 5454 byte(s)
Log Message:
Introduce vu_module.py for viewing ba objects.  This GUI uses Tk and has buttons to allow users to move through the sensorgrams six-at-a-time.  The traces can be dotted or continuous lines or both.  Specify two ba objects and the first will be dotted while the second will be continuous.  This could be useful for comparing real and simulated data. --CGL 
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 100412 (yymmdd)
7
8 Example:
9 #import vu_module as vu
10 #io_module as io
11 #ba1 = io.readsprit("spritdata.txt")
12 #vu.show(ba1, "line")
13 #vu.dotgraph(ba1)
14 #vu.linegraph(ba1)
15 #vu.dualgraph(ba1, ba2)
16 """
17 __version__ = "100412"
18
19
20 ## Import libraries
21 import ba_class as ba ## Our Biosensor Array class.
22 from datetime import date
23 import Tkinter as Tk
24 import tkSimpleDialog as dialog
25 import matplotlib as mpl
26 mpl.use('TkAgg')
27 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
28 from matplotlib.figure import Figure
29
30
31 def linegraph(baLine):
32 """Graph data of baLine using lines."""
33 global root, fig, ax, canvas ## Tk/mpl globals.
34 global ba0, ba1, ba2 ## Ref to ba for roilist, dotgraph, linegraph
35 ba0, ba1, ba2 = baLine, 0, baLine
36 createTkWindow()
37 show()
38 root.mainloop()
39 return
40 ## End of linegraph().
41
42 def dotgraph(baDot):
43 """Graph data of baDot using dots."""
44 global root, fig, ax, canvas ## Tk/mpl globals..
45 global ba0, ba1, ba2 ## Ref to ba for roilist, dotgraph, linegraph
46 ba0, ba1, ba2 = baDot, baDot, 0
47 createTkWindow()
48 show()
49 root.mainloop()
50 return
51 ## End of dotgraph().
52
53 def dualgraph(baDot, baLine):
54 """Graph data using dots and lines."""
55 global root, fig, ax, canvas ## Tk/mpl globals.
56 global ba0, ba1, ba2 ## Ref to ba for roilist, dotgraph, linegraph
57 ba0, ba1, ba2 = baDot, baDot, baLine
58 createTkWindow()
59 show()
60 root.mainloop()
61 return
62 ## End of dualgraph().
63
64 def show():
65 """Graph data of ba using Tk window."""
66 global root, fig, ax, canvas
67 global ba1, ba2
68
69 ## Prepare graph labels.
70 ax.clear()
71 ax.set_title('SPR Data plotted %s' % date.today().isoformat())
72 ax.set_xlabel('Time (s)')
73 ax.set_ylabel('SPR Response')
74 ax.grid(True)
75
76 ## Plot traces of real data.
77 if (ba1 != 0):
78 ## Dotted traces.
79 marker = ['r,', 'g,', 'b,', 'c,', 'm,', 'y,'] + ['k,'] * 100
80 for i,j in enumerate(roiList()):
81 txt = str(j) + ':' + ba1.roi[j].name
82 t = ba1.roi[j].time
83 y = ba1.roi[j].value
84 ax.plot(t, y, marker[i], label=txt)
85 if (ba2 != 0):
86 ## Line traces.
87 marker = ['r', 'g', 'b', 'c', 'm', 'y'] + ['k'] * 100
88 for i,j in enumerate(roiList()):
89 txt = str(j) + ':' + ba2.roi[j].name
90 t = ba2.roi[j].time
91 y = ba2.roi[j].value
92 ax.plot(t, y, marker[i], label=txt)
93
94 ## Format legend.
95 leg = ax.legend(loc='upper right')
96 for t in leg.get_texts(): t.set_fontsize('x-small')
97 #for t in leg.get_lines(): t.set_linestyle('None')
98 #labels = [line.get_label() for line in ax.lines]
99 #fig.legend(ax.lines, labels, 'right')
100
101 canvas.draw()
102 return
103 ## End of show().
104
105 def btnNext():
106 ## Show next six traces on the graph.
107 global ba0
108 x = max(roiList())+1
109 if ((x+6) > len(ba0.roi)): return
110 ba0.set_plot_list(range(x,x+6))
111 show()
112 return
113
114 def btnPrev():
115 ## Show the previous six traces on the graph.
116 global ba0
117 x = min(roiList())+1
118 if ((x-6) < 1): return
119 ba0.set_plot_list(range(x-6,x))
120 show()
121 return
122
123 def btnTraces():
124 ## Ask for list of traces to show on graph.
125 global ba0
126 txt = "Which ROIs would you like to see? (e.g. 19,20,21)"
127 txt = dialog.askstring(title="Input",prompt=txt)
128 txt = txt + "," ## Be sure eval() produces a list.
129 ba0.set_plot_list(eval(txt))
130 show()
131 return
132
133
134 def roiList():
135 ## List of first six plottable ROIs.
136 global ba0
137 #x = [i for i in range(len(ba.roi)) if ba0.roi[i].plottable]
138 x = [i.index for i in ba0.roi if i.plottable]
139 return x[:6]
140
141
142 def createTkWindow():
143 ## Create a Tk window for the Matplotlib graph.
144 global root, fig, ax, canvas
145 root = Tk.Tk()
146 root.wm_title("TK matplotlib Osprai")
147 ## Prepare a matplotlib Figure/Axes.
148 fig = Figure(figsize=(6,4), dpi=100)
149 ax = fig.add_subplot(111)
150 ax.hold(True) # Hold data until ax.clear().
151 #ax.set_position([0.1, 0.1, 0.6, 0.8]) # Fraction of frame [left,bot,w,h]
152 ## Define tk.DrawingArea
153 canvas = FigureCanvasTkAgg(fig, master=root)
154 #canvas.show()
155 canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
156 ## Add a pylab-style toolbar.
157 toolbar = NavigationToolbar2TkAgg( canvas, root )
158 toolbar.update()
159 canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
160 ## Add buttons.
161 button1 = Tk.Button(master=root, text='Next6', command=btnNext)
162 button1.pack(side=Tk.RIGHT)
163 button2 = Tk.Button(master=root, text='Choose..', command=btnTraces)
164 button2.pack(side=Tk.RIGHT)
165 button3 = Tk.Button(master=root, text='Prev6', command=btnPrev)
166 button3.pack(side=Tk.RIGHT)
167 return
168 ## End of createTkWindow.
169
170
171 ## Run the following lines once upon loading. Setup Tk.
172 ## Print a message to the users so they see this module is loaded.
173 print "Loaded vu_module.py version ", __version__
174
175 ################################# End of module #################################