ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/vu_module.py
Revision: 14
Committed: Wed Apr 14 00:40:11 2010 UTC (9 years, 6 months ago) by clausted
File size: 7803 byte(s)
Log Message:
Added features to select time points for measuring binding responses and to output that data to a file or scatterplot.  Added the background subtraction feature to cal_module.  Also a few bug fixes. --CGL
Line User Rev File contents
1 clausted 11 """
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 clausted 14 Last modified on 100413 (yymmdd)
7 clausted 11
8     Example:
9     #import vu_module as vu
10     #io_module as io
11     #ba1 = io.readsprit("spritdata.txt")
12 clausted 14 #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 clausted 11 """
17 clausted 14 __version__ = "100413"
18 clausted 11
19    
20     ## Import libraries
21     import ba_class as ba ## Our Biosensor Array class.
22 clausted 14 import numpy as np
23 clausted 11 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 clausted 14 def linegraph(baLine, title=""):
33 clausted 11 """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 clausted 14 global gTitle ## Title for graph.
37 clausted 11 ba0, ba1, ba2 = baLine, 0, baLine
38 clausted 14 gTitle = title
39     createSensorgramTkWindow()
40 clausted 11 show()
41     root.mainloop()
42     return
43     ## End of linegraph().
44    
45 clausted 14 def dotgraph(baDot, title=""):
46 clausted 11 """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 clausted 14 global gTitle ## Title for graph.
50 clausted 11 ba0, ba1, ba2 = baDot, baDot, 0
51 clausted 14 gTitle = title
52     createSensorgramTkWindow()
53 clausted 11 show()
54     root.mainloop()
55     return
56     ## End of dotgraph().
57    
58 clausted 14 def dualgraph(baDot, baLine, title=""):
59 clausted 11 """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 clausted 14 global gTitle ## Title for graph.
63 clausted 11 ba0, ba1, ba2 = baDot, baDot, baLine
64 clausted 14 gTitle = title
65     createSensorgramTkWindow()
66 clausted 11 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 clausted 14 global gTitle
76 clausted 11
77     ## Prepare graph labels.
78     ax.clear()
79 clausted 14 if (gTitle==""): gTitle='SPR Data plotted %s' % date.today().isoformat()
80     ax.set_title(gTitle)
81 clausted 11 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     x = max(roiList())+1
118     if ((x+6) > len(ba0.roi)): return
119     ba0.set_plot_list(range(x,x+6))
120     show()
121     return
122    
123     def btnPrev():
124     ## Show the previous six traces on the graph.
125     global ba0
126     x = min(roiList())+1
127     if ((x-6) < 1): return
128     ba0.set_plot_list(range(x-6,x))
129     show()
130     return
131    
132     def btnTraces():
133     ## Ask for list of traces to show on graph.
134     global ba0
135     txt = "Which ROIs would you like to see? (e.g. 19,20,21)"
136     txt = dialog.askstring(title="Input",prompt=txt)
137     txt = txt + "," ## Be sure eval() produces a list.
138     ba0.set_plot_list(eval(txt))
139     show()
140     return
141    
142    
143     def roiList():
144     ## List of first six plottable ROIs.
145     global ba0
146     #x = [i for i in range(len(ba.roi)) if ba0.roi[i].plottable]
147     x = [i.index for i in ba0.roi if i.plottable]
148     return x[:6]
149    
150    
151 clausted 14 def createSensorgramTkWindow():
152     """
153     Create a Tk window for the Matplotlib graph.
154     This window has three buttons which will allow the user to
155     """
156 clausted 11 global root, fig, ax, canvas
157     root = Tk.Tk()
158 clausted 14 root.wm_title("TK matplotlib Osprai Sensorgram")
159 clausted 11 ## Prepare a matplotlib Figure/Axes.
160     fig = Figure(figsize=(6,4), dpi=100)
161     ax = fig.add_subplot(111)
162     ax.hold(True) # Hold data until ax.clear().
163     #ax.set_position([0.1, 0.1, 0.6, 0.8]) # Fraction of frame [left,bot,w,h]
164     ## Define tk.DrawingArea
165     canvas = FigureCanvasTkAgg(fig, master=root)
166     #canvas.show()
167     canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
168     ## Add a pylab-style toolbar.
169     toolbar = NavigationToolbar2TkAgg( canvas, root )
170     toolbar.update()
171     canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
172     ## Add buttons.
173     button1 = Tk.Button(master=root, text='Next6', command=btnNext)
174     button1.pack(side=Tk.RIGHT)
175     button2 = Tk.Button(master=root, text='Choose..', command=btnTraces)
176     button2.pack(side=Tk.RIGHT)
177     button3 = Tk.Button(master=root, text='Prev6', command=btnPrev)
178     button3.pack(side=Tk.RIGHT)
179     return
180 clausted 14 ## End of createSensorgramTkWindow().
181    
182    
183     ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
184 clausted 11
185    
186 clausted 14 def scatterplot(ba0, t1, t2, t3, t4, title):
187     """
188     Plot binding changes between (t4-t3) versus (t2-t1).
189     Also print the results to the standard output.
190     """
191     ## Create a Tk window for the ccatterplot graph.
192     ## Variables root, fig, ax, canvas are local here, not global.
193     root = Tk.Tk()
194     root.wm_title("TK matplotlib Osprai Sensorgram")
195     ## Prepare a matplotlib Figure/Axes.
196     fig = Figure(figsize=(6,4), dpi=100)
197     ax = fig.add_subplot(111)
198     ax.hold(True) # Hold data until ax.clear().
199     ## Define tk.DrawingArea
200     canvas = FigureCanvasTkAgg(fig, master=root)
201     canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
202     ## Add a pylab-style toolbar.
203     toolbar = NavigationToolbar2TkAgg( canvas, root )
204     toolbar.update()
205     canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
206    
207     ## Prepare graph labels.
208     ax.clear()
209     if (title==""): title='Scatterplot of %s' % date.today().isoformat()
210     ax.set_title(title)
211     ax.set_xlabel('Binding12')
212     ax.set_ylabel('Binding34')
213     ax.grid(True)
214    
215     ## Get array of values at each of the four time points.
216     y1 = np.array([np.average(iroi.time2val(t1-15, t1+15)) for iroi in ba0.roi])
217     y2 = np.array([np.average(iroi.time2val(t2-15, t2+15)) for iroi in ba0.roi])
218     y3 = np.array([np.average(iroi.time2val(t3-15, t3+15)) for iroi in ba0.roi])
219     y4 = np.array([np.average(iroi.time2val(t4-15, t4+15)) for iroi in ba0.roi])
220     dx, dy = (y2-y1), (y4-y3)
221    
222     ## Print to stdout
223     print "Name", "Binding12", "Binding34"
224     for i in range(len(dx)): print ba0.roi[i].name, dx[i], dy[i]
225    
226     ## Plot
227     ax.plot(dx, dy, 'bo') ## Blue circles with 'bo'.
228     canvas.draw()
229     root.mainloop()
230     return
231     ## End of scatterplot()
232    
233    
234 clausted 11 ## Print a message to the users so they see this module is loaded.
235     print "Loaded vu_module.py version ", __version__
236    
237     ################################# End of module #################################