ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/vu_module.py
(Generate patch)
# Line 3 | Line 3
3   This uses the old Tk GUI toolkit.
4   Christopher Lausted, Institute for Systems Biology,
5   OSPRAI developers
6 < Last modified on 100412 (yymmdd)
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.show(ba1, "line")
13 < #vu.dotgraph(ba1)
14 < #vu.linegraph(ba1)
15 < #vu.dualgraph(ba1, ba2)
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__ = "100412"
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
# Line 28 | Line 29
29   from matplotlib.figure import Figure
30  
31  
32 < def linegraph(baLine):
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 <    createTkWindow()
38 >    gTitle = title
39 >    createSensorgramTkWindow()
40      show()
41      root.mainloop()
42      return
43      ## End of linegraph().
44      
45 < def dotgraph(baDot):
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 <    createTkWindow()
51 >    gTitle = title
52 >    createSensorgramTkWindow()
53      show()
54      root.mainloop()
55      return
56      ## End of dotgraph().
57      
58 < def dualgraph(baDot, baLine):
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 <    createTkWindow()
64 >    gTitle = title
65 >    createSensorgramTkWindow()
66      show()
67      root.mainloop()
68      return
# Line 65 | Line 72
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 <    ax.set_title('SPR Data plotted %s' % date.today().isoformat())
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)
# Line 105 | Line 114
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))
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 <    x = min(roiList())+1
130 <    if ((x-6) < 1): return
131 <    ba0.set_plot_list(range(x-6,x))
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  
# Line 139 | Line 153
153      return x[:6]
154  
155  
156 < def createTkWindow():
157 <    ## Create a Tk window for the Matplotlib graph.
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")
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)
# Line 165 | Line 182
182      button3 = Tk.Button(master=root, text='Prev6', command=btnPrev)
183      button3.pack(side=Tk.RIGHT)
184      return
185 <    ## End of createTkWindow.
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  
171 ## Run the following lines once upon loading.  Setup Tk.
239   ## Print a message to the users so they see this module is loaded.
240   print "Loaded vu_module.py version ", __version__
241  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines