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