Changeset 441 for sans/utils/bt5/bt5view
- Timestamp:
- Nov 11, 2008 1:49:10 PM (14 years ago)
- Location:
- sans/utils/bt5/bt5view
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sans/utils/bt5/bt5view/bt5view
r440 r441 14 14 15 15 try: 16 17 18 16 import pygtk 17 pygtk.require("2.0") 18 19 19 except: 20 20 pass 21 21 22 22 try: 23 24 23 import gtk 24 import gtk.glade 25 25 except: 26 26 sys.exit(1) 27 27 28 28 29 29 30 30 class appGui: 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 self.AddFileListColumn("Filename",0)51 52 53 54 self.filelist.set_sort_column_id(0,gtk.SORT_ASCENDING)55 56 57 58 59 60 61 62 63 64 65 self.plotlist = gtk.TreeStore(str,'gboolean',object,object)66 67 self.plotlist.set_sort_column_id(0,gtk.SORT_ASCENDING)68 69 70 71 72 73 74 self.figure = Figure(figsize=(4,4), dpi=72)75 76 77 78 79 80 81 82 83 84 85 self.plotView.pack_start(self.canvas, True, True) 86 87 self.filelistview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK,31 32 TARGETS = [('STRING', gtk.TARGET_SAME_APP, 0)] 33 34 def __init__(self): 35 36 gladefile = "bt5view.glade" 37 self.windowname = "win_Main" 38 self.wTree = gtk.glade.XML(gladefile, self.windowname) 39 40 event_dic = {"on_win_Main_destroy" : gtk.main_quit, 41 "on_quit1_activate" : gtk.main_quit, 42 "on_set_data_dir1_activate" : self.setdatadir, 43 "on_tv_plotlist_key_press_event" : self.handle_plotlist_keypress} 44 45 46 self.wTree.signal_autoconnect(event_dic) 47 48 # Set up file list 49 self.filelistview = self.wTree.get_widget("tv_filelist") 50 self.AddFileListColumn("Filename", 0) 51 52 self.filelist = gtk.ListStore(str) 53 self.filelistview.set_model(self.filelist) 54 self.filelist.set_sort_column_id(0, gtk.SORT_ASCENDING) 55 56 # Set up plot group list 57 self.plotlistview = self.wTree.get_widget("tv_plotlist") 58 #self.AddPlotListColumn("Dataset",0) 59 #self.AddPlotListColumn("Plot",1) 60 self.cellrendertoggle = gtk.CellRendererToggle() 61 self.cellrendertoggle.set_property('activatable', True) 62 self.AddPlotListColumns("Dataset") 63 64 #Create TreeStore model - Name of dataset, is it plotted, loaded data for children, plottable data for parent 65 self.plotlist = gtk.TreeStore(str, 'gboolean', object, object) 66 self.plotlistview.set_model(self.plotlist) 67 self.plotlist.set_sort_column_id(0, gtk.SORT_ASCENDING) 68 69 #fill the file list 70 self.FillFileList(self.GetDirList()) 71 #self.plotlist.append(None,None) 72 73 # Set up graphing widget to display xpeek data 74 self.figure = Figure(figsize=(4, 4), dpi=72) 75 self.axis = self.figure.add_subplot(111) 76 self.axis.set_xlabel('Motor position') 77 self.axis.set_ylabel('Counts') 78 #self.axis.set_title('XPeek') 79 self.axis.grid(True) 80 81 self.canvas = FigureCanvasGTK(self.figure) 82 self.canvas.show() 83 84 self.plotView = self.wTree.get_widget("hbox1") 85 self.plotView.pack_start(self.canvas, True, True) 86 87 self.filelistview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, 88 88 self.TARGETS, 89 89 gtk.gdk.ACTION_COPY) 90 90 self.plotlistview.enable_model_drag_dest(self.TARGETS, 91 91 gtk.gdk.ACTION_COPY) 92 92 93 94 self.plotlistview.connect("drag_data_received",self.dnd_data_received)95 96 97 98 99 100 101 102 103 104 105 106 107 column.set_resizable(True) 108 109 110 111 112 113 114 115 #column.set_resizable(True) 116 117 118 119 120 121 122 column = gtk.TreeViewColumn('',self.cellrendertoggle, active=1)123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 def FillFileList(self,filenames):144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 titer = model.insert_after(parent,iter,[data,0,detdata,0])165 166 titer = model.insert_before(parent,iter,[data,0,detdata,0])167 168 169 170 titer = model.insert_after(iter, None, [data,0,detdata,0])171 172 173 iter = model.append(None,[data,0,0,0])174 titer = model.append(iter,[data,0,detdata,0])175 176 177 iter = model.append(None,[data[:5],0,0,0])178 titer = model.append(iter,[data,0,detdata,0])179 180 181 182 self.do_plot(model,titer, "total")183 184 185 186 187 188 189 #Always passing a child, but let's check anyway 190 191 parent = model.iter_parent(iter) 192 193 194 195 print "Parent plotted" 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 def make_plottable_dataset(self,model,path,type):218 #Will use select here, but for now ignore type and just do total 219 220 221 222 223 for iter in range(0,model.iter_n_children(piter)):224 225 cpath = model.get_path(model.iter_nth_child(piter,iter))226 227 228 229 230 231 ydata.append(data[mval][1]+data[mval][2]+data[mval][4]+data[mval][5]+data[mval][6])232 233 234 235 236 model[path][3] = [xdata,ydata] 237 238 239 240 241 242 243 244 245 246 247 chooser = gtk.FileChooserDialog(title="Select Data Directory",action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,248 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))249 250 251 252 253 254 255 256 257 def handle_plotlist_keypress(self,widget,event):258 259 260 261 262 263 (path,),focus = widget.get_cursor()264 widget.set_cursor(path+1)265 266 (path,),focus = widget.get_cursor()267 widget.set_cursor(path-1)268 269 270 271 272 273 274 275 93 self.filelistview.connect("drag_data_get", self.dnd_data_getdata) 94 self.plotlistview.connect("drag_data_received", self.dnd_data_received) 95 96 #Handle toggling of checkboxes in plot list 97 self.cellrendertoggle.connect("toggled", self.handle_toggled, (self.plotlistview.get_model(), 1)) 98 99 100 def AddFileListColumn(self, title, columnId): 101 """This function adds a column to the list view. 102 First it create the gtk.TreeViewColumn and then set 103 some needed properties""" 104 105 column = gtk.TreeViewColumn(title, gtk.CellRendererText() 106 , text=columnId) 107 column.set_resizable(True) 108 column.set_sort_column_id(columnId) 109 self.filelistview.append_column(column) 110 return 111 112 def AddPlotListColumns(self, title): 113 column = gtk.TreeViewColumn(title, gtk.CellRendererText() 114 , text=0) 115 #column.set_resizable(True) 116 column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) 117 column.set_fixed_width(230) 118 column.set_sort_column_id(0) 119 #column.set_min_width(200) 120 self.plotlistview.append_column(column) 121 122 column = gtk.TreeViewColumn('', self.cellrendertoggle, active=1) 123 column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) 124 #column.set_min_width(20) 125 #column.set_max_width(20) 126 #column.set_fixed_width(20) 127 #column.set_resizable(False) 128 self.plotlistview.append_column(column) 129 return 130 131 def handle_toggled(self, cell, path, user_data): 132 model, column = user_data 133 model[path][column] = not model[path][column] 134 135 return 136 137 def GetDirList(self): 138 dirlist = os.listdir(os.getcwd()) 139 140 return dirlist 141 142 143 def FillFileList(self, filenames): 144 self.filelist.clear() 145 for filename in filenames: 146 self.filelist.append([filename]) 147 return 148 149 150 def dnd_data_received(self, treeview, context, x, y, selection, info, etime): 151 model = treeview.get_model() 152 data = selection.data 153 #Get USANS data from file 154 detdata = usans.getBT5DataFromFile(data) 155 drop_info = treeview.get_dest_row_at_pos(x, y) 156 if drop_info: 157 #Other entries exist already 158 path, position = drop_info 159 iter = model.get_iter(path) 160 if model.iter_depth(iter) > 0: 161 #Adding to exisiting dataset 162 parent = model.iter_parent(iter) 163 if position == gtk.TREE_VIEW_DROP_INTO_OR_AFTER or position == gtk.TREE_VIEW_DROP_AFTER: 164 titer = model.insert_after(parent, iter, [data, 0, detdata, 0]) 165 elif position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE or position == gtk.TREE_VIEW_DROP_BEFORE: 166 titer = model.insert_before(parent, iter, [data, 0, detdata, 0]) 167 else: 168 #New top level entry 169 if position == gtk.TREE_VIEW_DROP_INTO_OR_AFTER or position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE: 170 titer = model.insert_after(iter, None, [data, 0, detdata, 0]) 171 else: 172 #Not sure that this should ever get called.... 173 iter = model.append(None, [data, 0, 0, 0]) 174 titer = model.append(iter, [data, 0, detdata, 0]) 175 else: 176 #First entry 177 iter = model.append(None, [data[:5], 0, 0, 0]) 178 titer = model.append(iter, [data, 0, detdata, 0]) 179 180 print titer 181 182 self.do_plot(model, titer, "total") 183 184 return 185 186 def do_plot(self, model, iter, type): 187 #Handle plotting - ignore type for the moment, only plot total counts 188 path = model.get_path(iter) 189 #Always passing a child, but let's check anyway 190 if model.iter_depth(iter) > 0: 191 parent = model.iter_parent(iter) 192 ppath = model.get_path(parent) 193 #Check to see if parent is already plotted 194 if (model[ppath][1]): 195 print "Parent plotted" 196 #If plotted, add new data to existing plotted dataset and set checkbox 197 self.make_plottable_dataset(model, ppath, type) 198 model[path][1] = 1 199 else: 200 print "Parent not plotted" 201 if(model.iter_n_children(iter) > 1): 202 print "has siblings" 203 #If not plotted and there are siblings, add new data to dataset and set checkbox but do not plot 204 self.make_plottable_dataset(model, ppath, type) 205 model[path][1] = 1 206 else: 207 #If parent not plotted and there are no siblings, create plottable dataset, plot and set checkbox 208 self.make_plottable_dataset(model, ppath, type) 209 print model[ppath][3] 210 self.axis.plot(model[ppath][3][0], model[ppath][3][1]) 211 self.figure.draw() 212 model[path][1] = 1 213 214 215 return 216 217 def make_plottable_dataset(self, model, path, type): 218 #Will use select here, but for now ignore type and just do total 219 piter = model.get_iter(path) 220 xdata = [] 221 ydata = [] 222 223 for iter in range(0, model.iter_n_children(piter)): 224 #for each child that has the plot flag set add keys to xdata and summed values to ydata 225 cpath = model.get_path(model.iter_nth_child(piter, iter)) 226 data = model[cpath][2] 227 mvals = data.keys() 228 mvals.sort() 229 for mval in mvals: 230 xdata.append(mval) 231 ydata.append(data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6]) 232 233 print xdata 234 print ydata 235 236 model[path][3] = [xdata, ydata] 237 return 238 239 def dnd_data_getdata(self, treeview, context, selection, target_id, etime): 240 treeselection = treeview.get_selection() 241 model, iter = treeselection.get_selected() 242 data = model.get_value(iter, 0) 243 selection.set('STRING', 8, data) 244 return 245 246 def setdatadir(self, widget): 247 chooser = gtk.FileChooserDialog(title="Select Data Directory", action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, 248 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) 249 chooser.set_default_response(gtk.RESPONSE_OK) 250 chooser.set_current_folder(os.getcwd()) 251 response = chooser.run() 252 if response == gtk.RESPONSE_OK: 253 os.chdir(chooser.get_filename()) 254 self.FillFileList(self.GetDirList()) 255 chooser.destroy() 256 257 def handle_plotlist_keypress(self, widget, event): 258 keyname = gtk.gdk.keyval_name(event.keyval) 259 print keyname 260 if keyname in ["Delete", "BackSpace"]: 261 self.deleteplotlistentry(widget) 262 if keyname in ["Down"]: 263 (path,), focus = widget.get_cursor() 264 widget.set_cursor(path + 1) 265 if keyname in ["Up"]: 266 (path,), focus = widget.get_cursor() 267 widget.set_cursor(path - 1) 268 return True 269 270 def deleteplotlistentry(self, treeview): 271 treeselection = treeview.get_selection() 272 model, iter = treeselection.get_selected() 273 model.remove(iter) 274 return 275 276 276 app = appGui() 277 277 gtk.main() -
sans/utils/bt5/bt5view/usans.py
r440 r441 3 3 #import Tkinter as Tk 4 4 5 def numeric_compare(x, y):6 7 y = float(y) 5 def numeric_compare(x, y): 6 x = float(x) 7 y = float(y) 8 8 9 10 return -111 elif x==y:12 13 14 9 if x < y: 10 return - 1 11 elif x == y: 12 return 0 13 else: # x>y 14 return 1 15 15 16 16 def getBT5DataFromFile(fileName): 17 ''' 18 Takes a filename and returns a dictionary of the detector values 19 keyed by varying value (ususally A2 or A5) 20 ''' 21 detdata = {} 17 ''' 18 Takes a filename and returns a dictionary of the detector values 19 keyed by varying value (ususally A2 or A5) 20 ''' 21 detdata = {} 22 metadata = {} 22 23 23 24 inputfile = open(fileName, "r") 24 25 25 26 inputdata = inputfile.readlines() 26 27 27 for index in range(13,len(inputdata),2): 28 detdata[inputdata[index].split()[0]] = inputdata[index+1].split(',') 28 mdtmp = inputdata[0].split(' ') 29 30 print mdtmp 29 31 30 for key in detdata.keys(): 31 for val in range(0,len(detdata[key])): 32 detdata[key][val] = int(detdata[key][val]) 32 for index in range(13, len(inputdata), 2): 33 detdata[inputdata[index].split()[0]] = inputdata[index + 1].split(',') 33 34 34 inputfile.close() 35 return detdata 35 for key in detdata.keys(): 36 for val in range(0, len(detdata[key])): 37 detdata[key][val] = int(detdata[key][val]) 38 39 inputfile.close() 40 return detdata 36 41 37 42 def printBT5DetData(detdata): 38 39 43 ''' 44 Print the contents of the file in a formatted fashion 40 45 41 42 43 44 45 46 Takes a dictionary of data as provided by getBT5DataFromFile() and prints out the contents 47 in a formatted fashion 48 ''' 49 motorvals = detdata.keys() 50 motorvals.sort(cmp=numeric_compare) 46 51 47 48 str = motorval+":"49 str += "\tMon: "+repr(detdata[motorval][0])50 str += "\tDet 1-5: "+repr(detdata[motorval][2])51 str += "\t"+repr(detdata[motorval][1])52 str += "\t"+repr(detdata[motorval][4])53 str += "\t"+repr(detdata[motorval][5])54 str += "\t"+repr(detdata[motorval][6])55 str += "\tTrans: "+repr(detdata[motorval][3])56 52 for motorval in motorvals: 53 str = motorval + ":" 54 str += "\tMon: " + repr(detdata[motorval][0]) 55 str += "\tDet 1-5: " + repr(detdata[motorval][2]) 56 str += "\t" + repr(detdata[motorval][1]) 57 str += "\t" + repr(detdata[motorval][4]) 58 str += "\t" + repr(detdata[motorval][5]) 59 str += "\t" + repr(detdata[motorval][6]) 60 str += "\tTrans: " + repr(detdata[motorval][3]) 61 print str 57 62 58 63 return 0 59 64 60 def getAlignVals(data, motorval):61 62 65 def getAlignVals(data, motorval): 66 ''' 67 Return the values we record in the logbook for a given motor position 63 68 64 65 66 67 69 Takes a dictionary as provided by getBT5DataFromFile and returns a dictionary with 70 keys Central, Trans and Sum 71 ''' 72 alignvals = {} 68 73 69 70 71 alignvals['Sum'] = data[motorval][1]+data[motorval][2]+data[motorval][4]+data[motorval][5]+data[motorval][6] 72 74 alignvals['Central'] = data[motorval][1] 75 alignvals['Trans'] = data[motorval][3] 76 alignvals['Sum'] = data[motorval][1] + data[motorval][2] + data[motorval][4] + data[motorval][5] + data[motorval][6] 77 return alignvals 73 78 74 def maxDetCount(data, detector):75 76 77 78 79 80 ''' 81 82 83 79 def maxDetCount(data, detector): 80 ''' 81 Return the maximum value and corresponding motor position for a given detector 82 83 Takes a dictionary as provided by getBT5DataFromFile() and returns a dictionary with 84 keys Position and Value 85 ''' 86 maxpos = '' 87 maxval = 0 88 result = {} 84 89 85 86 90 mvals = data.keys() 91 det = {'1':2, '2':1, '3':4, '4':5, '5':6}[repr(detector)] 87 92 88 89 90 maxval = data[mval][det] 91 92 93 94 93 for mval in mvals: 94 if data[mval][det] > maxval: 95 maxval = data[mval][det] 96 maxpos = mval 97 98 result['Position'] = maxpos 99 result['Value'] = maxval 95 100 96 return result 101 return result 97 102 98 99 103 104 100 105 101 106 if __name__ == '__main__': 102 103 104 107 import sys 108 data = getBT5DataFromFile(sys.argv[1]) 109 printBT5DetData(data) 105 110 106 maxinfo = maxDetCount(data,2)107 108 avals = getAlignVals(data,maxinfo['Position'])109 111 maxinfo = maxDetCount(data, 2) 112 print maxinfo 113 avals = getAlignVals(data, maxinfo['Position']) 114 print avals
Note: See TracChangeset
for help on using the changeset viewer.