Changeset 441 for sans/utils/bt5/bt5plot2/bt5plot2
- Timestamp:
- Nov 11, 2008 1:49:10 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sans/utils/bt5/bt5plot2/bt5plot2
r439 r441 13 13 14 14 try: 15 16 17 15 import pygtk 16 pygtk.require("2.0") 17 18 18 except: 19 19 pass 20 20 21 21 try: 22 23 22 import gtk 23 import gtk.glade 24 24 except: 25 25 sys.exit(1) 26 26 27 27 28 28 29 29 class appGui: 30 31 TARGETS = [('STRING', gtk.TARGET_SAME_APP, 0)] 32 33 def __init__(self): 34 35 gladefile = "bt5plot2.glade" 36 self.windowname = "win_Main" 37 self.wTree = gtk.glade.XML(gladefile, self.windowname) 38 39 event_dic = {"on_win_Main_destroy" : gtk.main_quit, 40 "on_quit1_activate" : gtk.main_quit, 41 "on_set_data_dir1_activate" : self.setdatadir, 42 "on_xaxis_loglin_activate" : self.handle_xaxis_loglin, 43 "on_yaxis_loglin_activate" : self.handle_yaxis_loglin} 44 # "on_tv_plotlist_key_press_event" : self.handle_plotlist_keypress} 45 46 47 self.wTree.signal_autoconnect(event_dic) 48 49 # Set up file list 50 self.filelistview = self.wTree.get_widget("tv_filelist") 51 52 self.filelist = gtk.ListStore(str,'gboolean',object,object,object) 53 self.filelist.set_sort_column_id(0,gtk.SORT_ASCENDING) 54 self.filelistview.set_model(self.filelist) 55 56 self.cellrenderertoggle = gtk.CellRendererToggle() 57 self.cellrenderertoggle.set_property('activatable', True) 58 self.cellrenderertoggle.connect("toggled", self.handle_plot_toggle, self.filelist) 59 60 self.AddFileListColumns() 61 62 #fill the file list 63 self.FillFileList(self.GetDirList()) 64 65 # Set up graphing widget to display xpeek data 66 self.figure = Figure(figsize=(4,4), dpi=72) 67 self.axis = self.figure.add_subplot(111) 68 self.axis.set_xlabel('Motor position') 69 self.axis.set_ylabel('Counts') 70 #self.axis.set_title('XPeek') 71 self.axis.grid(True) 72 73 self.canvas = FigureCanvasGTK(self.figure) 74 self.canvas.show() 75 76 self.plotView = self.wTree.get_widget("hbox1") 77 self.plotView.pack_start(self.canvas, True, True) 78 79 #self.filelistview.enable_model_drag_source( gtk.gdk.BUTTON1_MASK, 30 31 TARGETS = [('STRING', gtk.TARGET_SAME_APP, 0)] 32 33 def __init__(self): 34 35 gladefile = "bt5plot2.glade" 36 self.windowname = "win_Main" 37 self.wTree = gtk.glade.XML(gladefile, self.windowname) 38 39 event_dic = {"on_win_Main_destroy" : gtk.main_quit, 40 "on_quit1_activate" : gtk.main_quit, 41 "on_set_data_dir1_activate" : self.setdatadir, 42 "on_xaxis_loglin_activate" : self.handle_xaxis_loglin, 43 "on_yaxis_loglin_activate" : self.handle_yaxis_loglin, 44 "on_plot_type_activate" : self.handle_plot_type_change} 45 # "on_tv_plotlist_key_press_event" : self.handle_plotlist_keypress} 46 47 #This is a bit clunky, but never mind. 48 #Set default plottype to rate. Glade definition sets that as default active button in menu 49 self.plottype = 'rate' 50 51 self.wTree.signal_autoconnect(event_dic) 52 53 # Set up file list 54 self.filelistview = self.wTree.get_widget("tv_filelist") 55 56 self.filelist = gtk.ListStore(str, 'gboolean', object, object, object) 57 self.filelist.set_sort_column_id(0, gtk.SORT_ASCENDING) 58 self.filelistview.set_model(self.filelist) 59 60 self.cellrenderertoggle = gtk.CellRendererToggle() 61 self.cellrenderertoggle.set_property('activatable', True) 62 self.cellrenderertoggle.connect("toggled", self.handle_plot_toggle, self.filelist) 63 64 self.AddFileListColumns() 65 66 #fill the file list 67 self.FillFileList(self.GetDirList()) 68 69 # Set up graphing widget to display xpeek data 70 self.figure = Figure(figsize=(4, 4), dpi=72) 71 self.axis = self.figure.add_subplot(111) 72 self.axis.set_yscale('log') 73 self.axis.set_aspect('auto') 74 self.axis.set_autoscale_on('True') 75 self.axis.set_xlabel('Motor position') 76 self.axis.set_ylabel('Counts') 77 self.axis.grid(True) 78 79 self.canvas = FigureCanvasGTK(self.figure) 80 self.canvas.show() 81 82 self.plotView = self.wTree.get_widget("hbox1") 83 self.plotView.pack_start(self.canvas, True, True) 84 85 #self.filelistview.enable_model_drag_source( gtk.gdk.BUTTON1_MASK, 80 86 # self.TARGETS, 81 87 # gtk.gdk.ACTION_COPY) 82 88 83 #self.filelistview.connect("drag_data_get", self.dnd_data_getdata) 84 85 86 def AddFileListColumns(self): 87 """This function adds a column to the list view. 88 First it create the gtk.TreeViewColumn and then set 89 some needed properties""" 90 91 column = gtk.TreeViewColumn('Filename', gtk.CellRendererText() 92 , text=0) 93 column.set_resizable(True) 94 column.set_sort_column_id(0) 95 self.filelistview.append_column(column) 96 97 column = gtk.TreeViewColumn('', self.cellrenderertoggle, active=1) 98 self.filelistview.append_column(column) 99 return 100 101 def GetDirList(self): 102 dirlist = os.listdir(os.getcwd()) 103 104 bt5list = [ x for x in dirlist if x.find('.bt5') > 0] 105 106 return bt5list 107 108 109 def FillFileList(self,filenames): 110 self.filelist.clear() 111 for filename in filenames: 112 data = usans.getBT5DataFromFile(filename) 113 self.filelist.append([filename,0,data,0,0]) 114 return 115 116 117 def setdatadir(self, widget): 118 chooser = gtk.FileChooserDialog(title="Select Data Directory",action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, 119 buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) 120 chooser.set_default_response(gtk.RESPONSE_OK) 121 chooser.set_current_folder(os.getcwd()) 122 response = chooser.run() 123 if response == gtk.RESPONSE_OK: 124 os.chdir(chooser.get_filename()) 125 self.FillFileList(self.GetDirList()) 126 chooser.destroy() 127 128 def handle_plot_toggle(self, cell, path, model): 129 model[path][1] = not model[path][1] 130 131 if model[path][1]: 132 #add plot 133 self.add_plot(model, path) 134 else: 135 #remove plot 136 self.remove_plot(model, path) 137 return 138 139 def add_plot(self, model, path): 140 141 xdata = [] 142 ydata = [] 143 144 data = model[path][2] 145 mvals = data.keys() 146 mvals.sort(usans.numeric_compare) 147 for mval in mvals: 148 xdata.append(mval) 149 ydata.append(data[mval][1]+data[mval][2]+data[mval][4]+data[mval][5]+data[mval][6]) 150 151 model[path][3] = (xdata,ydata) 152 153 model[path][4], = self.axis.plot(xdata,ydata) 154 self.canvas.draw() 155 return 156 157 def remove_plot(self, model, path): 158 159 self.axis.lines.remove(model[path][4]) 160 self.canvas.draw() 161 return 162 163 def handle_xaxis_loglin(self,widget): 164 165 166 if (self.axis.get_xscale() == "log"): 167 self.axis.set_xscale('linear') 168 else: 169 self.axis.set_xscale('log') 170 171 self.canvas.draw() 172 return 173 174 def handle_yaxis_loglin(self,widget): 175 176 177 if (self.axis.get_yscale() == "log"): 178 self.axis.set_yscale('linear') 179 else: 180 self.axis.set_yscale('log') 181 182 self.canvas.draw() 183 return 184 185 #def handle_plotlist_keypress(self,widget,event): 186 # keyname = gtk.gdk.keyval_name(event.keyval) 187 # print keyname 188 # if keyname in ["Delete", "BackSpace"]: 189 # self.deleteplotlistentry(widget) 190 # 191 # return True 192 193 #def deleteplotlistentry(self, treeview): 194 # treeselection = treeview.get_selection() 195 # model, iter = treeselection.get_selected() 196 # model.remove(iter) 197 # return 198 89 #self.filelistview.connect("drag_data_get", self.dnd_data_getdata) 90 91 92 def AddFileListColumns(self): 93 """This function adds a column to the list view. 94 First it create the gtk.TreeViewColumn and then set 95 some needed properties""" 96 97 column = gtk.TreeViewColumn('Filename', gtk.CellRendererText() 98 , text=0) 99 column.set_resizable(True) 100 column.set_sort_column_id(0) 101 self.filelistview.append_column(column) 102 103 column = gtk.TreeViewColumn('', self.cellrenderertoggle, active=1) 104 self.filelistview.append_column(column) 105 return 106 107 def GetDirList(self): 108 dirlist = os.listdir(os.getcwd()) 109 110 bt5list = [ x for x in dirlist if x.find('.bt5') > 0] 111 112 return bt5list 113 114 115 def FillFileList(self, filenames): 116 self.filelist.clear() 117 for filename in filenames: 118 data,metadata = usans.getBT5DataFromFile(filename) 119 self.filelist.append([filename, 0, (data,metadata), 0, 0]) 120 return 121 122 123 def setdatadir(self, widget): 124 chooser = gtk.FileChooserDialog(title="Select Data Directory", action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, 125 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) 126 chooser.set_default_response(gtk.RESPONSE_OK) 127 chooser.set_current_folder(os.getcwd()) 128 response = chooser.run() 129 if response == gtk.RESPONSE_OK: 130 os.chdir(chooser.get_filename()) 131 self.FillFileList(self.GetDirList()) 132 chooser.destroy() 133 134 def handle_plot_toggle(self, cell, path, model): 135 model[path][1] = not model[path][1] 136 137 if model[path][1]: 138 #add plot 139 self.add_plot(model, path) 140 else: 141 #remove plot 142 self.remove_plot(model, path) 143 return 144 145 def add_plot(self, model, path): 146 147 self.make_plottable_dataset(model, path, self.plottype) 148 149 if self.plottype == 'split': 150 model[path][4] = self.axis.plot(model[path][3][0],model[path][3][1], 'o', 151 model[path][3][0],model[path][3][2], 'o', 152 model[path][3][0],model[path][3][3], 'o', 153 model[path][3][0],model[path][3][4], 'o', 154 model[path][3][0],model[path][3][5], 'o') 155 else: 156 model[path][4] = self.axis.plot(model[path][3][0],model[path][3][1], 'bo') 157 158 self.axis.autoscale_view() 159 self.canvas.draw() 160 return 161 162 def make_plottable_dataset(self, model, path, type): 163 164 data,metadata = model[path][2] 165 166 if type == 'total': 167 #generate totals 168 xdata = [] 169 ydata = [] 170 171 mvals = data.keys() 172 mvals.sort(usans.numeric_compare) 173 for mval in mvals: 174 xdata.append(mval) 175 ydata.append(data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6]) 176 177 model[path][3] = [xdata, ydata] 178 179 elif type == 'rate': 180 # generate countrate 181 xdata = [] 182 ydata = [] 183 184 mvals = data.keys() 185 mvals.sort(usans.numeric_compare) 186 for mval in mvals: 187 xdata.append(mval) 188 189 if metadata['base'] == 'TIME': 190 #Counting in TIME base, so normalize by seconds 191 cnttime = metadata['mon'] 192 for mval in mvals: 193 ydata.append((data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6])/cnttime) 194 else: 195 #Must be counting in monitor base so normalize by monitor 196 moncts = metadata['mon'] 197 for mval in mvals: 198 ydata.append((data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6])/cnttime) 199 200 model[path][3] = [xdata, ydata] 201 202 elif type == 'trans': 203 xdata = [] 204 ydata = [] 205 206 mvals = data.keys() 207 mvals.sort(usans.numeric_compare) 208 for mval in mvals: 209 xdata.append(mval) 210 ydata.append(data[mval][3]) 211 212 model[path][3] = [xdata, ydata] 213 214 elif type == 'mon': 215 xdata = [] 216 ydata = [] 217 218 mvals = data.keys() 219 mvals.sort(usans.numeric_compare) 220 for mval in mvals: 221 xdata.append(mval) 222 ydata.append(data[mval][0]) 223 224 model[path][3] = [xdata, ydata] 225 226 elif type == 'split': 227 xdata = [] 228 ydata1 = [] 229 ydata2 = [] 230 ydata3 = [] 231 ydata4 = [] 232 ydata5 = [] 233 234 mvals = data.keys() 235 mvals.sort(usans.numeric_compare) 236 for mval in mvals: 237 xdata.append(mval) 238 ydata1.append(data[mval][1]) 239 ydata2.append(data[mval][2]) 240 ydata3.append(data[mval][4]) 241 ydata4.append(data[mval][5]) 242 ydata5.append(data[mval][6]) 243 244 model[path][3] = [xdata,ydata1,ydata2,ydata3,ydata4,ydata5] 245 else: 246 pass 247 248 return 249 250 def remove_plot(self, model, path): 251 252 for line in model[path][4]: 253 self.axis.lines.remove(line) 254 255 self.axis.autoscale_view() 256 self.canvas.draw() 257 return 258 259 def handle_xaxis_loglin(self, widget): 260 261 262 if (self.axis.get_xscale() == "log"): 263 self.axis.set_xscale('linear') 264 else: 265 self.axis.set_xscale('log') 266 267 self.axis.autoscale_view() 268 269 self.canvas.draw() 270 271 return 272 273 def handle_yaxis_loglin(self, widget): 274 275 276 if (self.axis.get_yscale() == "log"): 277 self.axis.set_yscale('linear') 278 else: 279 self.axis.set_yscale('log') 280 281 self.axis.autoscale_view() 282 self.canvas.draw() 283 return 284 285 def handle_plot_type_change(self,widget): 286 287 if widget.get_active(): 288 self.plottype = widget.get_name().split('_')[1] 289 print self.plottype 290 291 return 292 199 293 200 294 app = appGui()
Note: See TracChangeset
for help on using the changeset viewer.