def __init__(self, **kwargs): self.id = '.' + os.environ['HOME'].split('/')[-1] self.id += str(int(time.time()) % 1000000) print 'id', self.id self.advanced = False if 'advanced' in kwargs: self.advanced = True self.tempPath = '/home/mota/public/gui/sessionimage%s.png' % self.id Gtk.Window.__init__(self, title="Image Viewer [session%s]" % self.id) self.maximize() self.set_border_width(3) self.image = None self.foot = None self.all_runIDs = ConniePaths.runID() self.update_runID = True self.runIDs = None self.add(self.build_window()) self.paths = [] self.run = -1 self.no_file = 'file not found' self.fast_render = False if 'fast_render' in kwargs: self.fast_render = True if 'run' in kwargs: self.run = int(kwargs['run']) else: self.run = ConniePaths.run()[-1] if 'runID' in kwargs: self.set_runID(int(kwargs['runID'])) print 'runID', self.get_runID() print 'self.run', self.run if 'ohdu' in kwargs: self.set_ohdu(int(kwargs['ohdu'])) else: self.set_ohdu(2) self.runIDcolumn = None if 'imageType' in kwargs: self.set_imageType(kwargs['imageType']) elif 'imagetype' in kwargs: self.set_imageType(kwargs['imagetype']) else: self.set_imageType('raw') print 'run', self.run if self.run is not None: self.runEntry.get_child().set_text(str(self.run)) self.connect('destroy', Gtk.main_quit) self.maximize() self.show_all()
def set_runID(self, runID): #try: print 'set_runID', runID, type(runID) runID = int(runID) print 'set_runID datapath.run', ConniePaths.run(runID=runID) self.run = ConniePaths.run(runID=runID) print 'set_runID run', self.run #self.runButtons[self.run].set_active( True ) self.runIDEntry.get_child().set_text(str(runID)) #self.runIDEntry.set_active( self.runIDs.index(runID) ) #self.runIDEntry.set_text( str(runID) ) self.refresh_pathsLabel()
def getPath(self): runID = self.get_runID() imageType = self.get_imageType() print 'getPath', runID, imageType if runID is None or imageType is None: return [self.no_file] if imageType in ['raw', 'osi', 'vosi', 'vhosi', 'conv', 'convRaw']: return ConniePaths.runIDPath(int(runID)) #elif imageType == 'osi': #return get_osi_path( int(runID) ) elif imageType in ['MB', 'mbs']: return ConniePaths.masterBiasPath(int(runID)) elif imageType == 'scn': return ConniePaths.runIDPathProcessed(int(runID), image='scn') #return get_scn_path( int(runID) ) return [self.no_file]
def refresh_runIDList(self, run=None, runID=None): self.runIDEntry.remove_all() if run is None: self.runIDEntry.get_child().set_text('') return runIDs = ConniePaths.runID(run=run)[::-1] for runID_ in runIDs: self.runIDEntry.append_text(str(runID_)) self.runIDs = runIDs if runID is None: runID = runIDs[0] print 'setting runID to', runID self.runIDEntry.get_child().set_text(str(runID)) self.runIDEntry.set_active(self.runIDs.index(runID)) return
def on_run_changed(self, widget, *args): if not self.update_runID: return self.update_runID = False if widget is self.runEntry: run = self.get_run() print 'on_run_changed', run if not str(run) in self.runList: print self.get_run(), 'is not present' self.change_color(self.runEntry.get_child(), 'red') self.refresh_runIDList() else: widget.set_active(self.runList.index(str(run))) self.change_color(self.runEntry.get_child()) print 'run', run self.refresh_runIDList(run) elif widget is self.runIDEntry: runID = self.get_runID() print 'on_runID_changed', runID if not runID in self.all_runIDs: print self.get_runID(), 'is not present' self.change_color(self.runIDEntry.get_child(), 'red') self.runEntry.get_child().set_text('') else: self.change_color(self.runIDEntry.get_child()) if runID not in self.runIDs: print 'not present in self.runIDs' run = ConniePaths.run(runID=runID) self.runEntry.get_child().set_text(str(run)) self.runEntry.set_active(self.runList.index(str(run))) self.runEntry.get_child().override_color( 0, Gdk.RGBA(red=0, green=0, blue=0)) print 'run', run self.refresh_runIDList(run, runID) self.runIDEntry.set_active(self.runIDs.index(runID)) self.update_runID = True return
def plot_table(self, quantity, max_per_subplot=4): ''' generate image and associate it with the proper imageCanvas object. The range is set by to rangeEntry ''' print 'plot table', quantity self.plotting[quantity] = True startTime = time.time() self.maximize() print 'plotting image' allocation = self.imageCanvas.get_allocation() print 'allocated size', allocation.width, allocation.height self.fig = plt.figure(figsize=(allocation.width / 100., allocation.height / 100.), dpi=100) self.imageCanvas.set_from_pixbuf( GdkPixbuf.Pixbuf.new_from_file( '/home/mota/public/gui/loading.gif')) self.fig.suptitle(quantity) if not os.path.exists(self.tablePaths[quantity]): return data = np.genfromtxt(self.tablePaths[quantity], names=True) ohdus = self.get_active_ohdus() runIDRange_str = self.runIDRangeEntry.get_text() try: runIDMin_str, runIDMax_str = runIDRange_str.split(':') except: runIDMin_str, runIDMax_str = None, None runIDMin = utils.try_convertion(runIDMin_str, int) runIDMax = utils.try_convertion(runIDMax_str, int) if runIDMin is None: if self.global_runIDMin is not None: runIDMin = min(self.global_runIDMin, data['runID'].min()) else: runIDMin = data['runID'].min() self.global_runIDMin = runIDMin self.update = True if runIDMax is None: #runIDMax = max( self.global_runIDMax, data['runID'].max() ) runIDMax = data['runID'].max() self.global_runIDMax = runIDMax self.update = True if runIDMax < 0: runIDMax = data['runID'].max() + runIDMax if runIDMin < 0: runIDMin = runIDMax + runIDMin if runIDMin > runIDMax: print 'wrong zoom %s %s %s' % (quantity, runIDMin, runIDMax) return runIDMask = np.all( [data['runID'] >= runIDMin, data['runID'] <= runIDMax], axis=0) all_runs = ConniePaths.run() all_runBoxes = [{ 'run': run, 'range': (lambda x: [x[0] if x[0] > 1 else x[1], x[-1]])( ConniePaths.runID(run=run)) } for run in all_runs] runBoxes = [ runBox for runBox in all_runBoxes if runBox['range'][1] > runIDMin and runBox['range'][0] < runIDMax ] runBoxes[0]['range'][0] = runIDMin runBoxes[-1]['range'][1] = runIDMax m = int(np.ceil(np.float(len(ohdus)) / max_per_subplot)) n = float(len(ohdus)) / m if m == 0: return self.grid = [] ohdus_ranges = [] for i in range(m): share = None if self.grid == [] else self.grid[0] self.grid.append( self.fig.add_subplot(m, 1, i + 1, sharex=share, sharey=share)) plt.setp(self.grid[i].get_xticklabels(), visible=False) ohdus_ranges.append(ohdus[int(i * n):int((i + 1) * n)]) ycum = [] self.lines = {} for i, ohdus_range in enumerate(ohdus_ranges): for ohdu in ohdus_range: for runBox in runBoxes[::-2]: self.grid[i].axvspan(runBox['range'][0], runBox['range'][1], alpha=.02, color='blue') ohduMask = np.all([runIDMask, data['ohdu'] == ohdu], axis=0) y = data[quantity][ohduMask] if len(y) == 0: continue ycum.append(y) x = data['runID'][ohduMask] self.lines[ohdu], = self.grid[i].plot( x, y, '.', ms=3., label=r'${\bf %02d}$ $\mu%.2f$ $\sigma%.2f$' % (ohdu, np.mean(y), np.std(y))) #, rasterized=True ) self.grid[i].hlines(np.mean(y), min(x), max(x), color=self.lines[ohdu].get_color(), linewidth=.5) #print 'lines data', lines[2][0].get_xdata() ax = self.grid[0].twiny() ax.set_xlim(self.grid[0].get_xlim()) ax.set_xticks( map(lambda runBox: .5 * (runBox['range'][0] + runBox['range'][1]), runBoxes)) ax.set_xticklabels(map(lambda runBox: runBox['run'], runBoxes)) ax.set_zorder(-100) val_max = 1 yrange = self.get_active_yrange() if yrange == self.yrangeBox.get_children()[0].get_label(): val_max = 1.05 * np.nanmax(ycum) elif yrange == self.yrangeBox.get_children()[1].get_label(): medians = np.nanmedian(ycum, axis=1) val_max = 2 * np.nanmax(medians) self.grid[-1].set_xlabel('runID') plt.setp(self.grid[-1].get_xticklabels(), visible=True) for i in range(m): self.grid[i].grid(True) if quantity == 'darkCurrent': self.grid[i].set_ylabel(r'm-m$_{os}$') elif quantity == 'readoutNoise': self.grid[i].set_ylabel(r'MAD$_{os}$') elif quantity == 'diffMADSqr': self.grid[i].set_ylabel(r'MAD$^2$-MAD$_{os}^2$') self.grid[i].legend(fancybox=True, framealpha=0, bbox_to_anchor=(1., 1.), loc='upper left') if m > 1: self.grid[i].set_ylim(((1 - .05) * np.nanmin(ycum), val_max)) try: self.fig.tight_layout(rect=(0, 0, .875, .95)) except: print 'could not adjust, please refreh' self.fig.subplots_adjust(hspace=0.05) print 'plotting path', self.imagePaths[quantity] self.fig.savefig(self.imagePaths[quantity]) plt.close() #self.updateLabel(quantity, ' <span color="green">plot done (%ds)</span>'%(time.time()-startTime) ) def callback(): print 'plotting image callback' self.imageCanvas.set_from_pixbuf( GdkPixbuf.Pixbuf.new_from_file(self.imagePaths[quantity])) self.imageCanvas.show() return False GLib.idle_add(callback) self.plotting[quantity] = False
def update_table(self, quantity): ''' computes an entry for the type specified, reads the previous table and appends to it. During its execution the table file is locked to avoid other instances of the code to modify the file ''' if self.is_locked(quantity): self.wait[quantity] = 5 return False startTime = time.time() self.create_lock(quantity) if quantity in [ 'sigmaOS', 'sigmaOSbin', 'sigmaOSMAD', 'sigmaOSMAD2', 'gainCu' ]: all_runIDs_reversed = ConniePaths.runIDProcessed_v3()[::-1] elif quantity in ['lambdaBGbin']: all_runIDs_reversed = self.read_from_table_runIDs( quantity='gainCu')[::-1] else: all_runIDs_reversed = ConniePaths.runID()[::-1] if os.path.exists(self.tablePaths[quantity]): data = np.genfromtxt(self.tablePaths[quantity], names=True) data = data.tolist() else: print '*** path does not exist***' lastrunID = all_runIDs_reversed[0] data = self.make_entry(lastrunID, quantity) runIDs_done = [] if len(data) > 0: runIDs_done = zip(*data)[0] runIDs_done = list(set(runIDs_done)) #print '(update_table)runIDs_done=', runIDs_done self.resetLabel(quantity) runIDs_todo = set(all_runIDs_reversed) - set(runIDs_done) if len(runIDs_todo) == 0: if self.firstAllDoneMsg[quantity]: print '%s all done in thread %s' % ( threading.currentThread().getName(), quantity) self.firstAllDoneMsg[quantity] = False self.resetLabel(quantity) self.remove_lock(quantity) self.updateLabel(quantity, 'all done; sleeping for a minute') self.wait[quantity] = 60 return False self.firstAllDoneMsg[quantity] = False runIDs_todo = set(all_runIDs_reversed) - set(runIDs_done) print '(update_table)[%s]len_todo=' % quantity, len(runIDs_todo) self.percentage_done[quantity] = len(runIDs_todo) for runID in list(runIDs_todo)[::-1]: #if runID not in runIDs_done: print '%s runID %s in thread %s' % ( quantity, runID, threading.currentThread().getName()) self.resetLabel(quantity) self.updateLabel(quantity, 'calculating runID <b>%s</b>' % runID) entry = self.make_entry(runID, quantity) if len(entry) > 0: data.extend(entry) break else: print '(update_table)empty entry at quantity', quantity, 'runID', runID break if len(data) > 0: with open(self.tablePaths[quantity], 'wb') as f: np.savetxt(f, data, header='#runID ohdu %s' % quantity, fmt='%d %d %.6f') self.remove_lock(quantity) self.resetLabel(quantity) self.updateLabel( quantity, 'concluded <b>%s</b> (%ds)' % (runID, time.time() - startTime)) if len(self.eta) > 0: estimateEnd = np.mean(self.eta) * len(runIDs_todo) self.updateLabel( quantity, 'ETA <b>%s</b>' % (time.ctime(estimateEnd + time.time()))) if self.get_active_quantity() == quantity: self.plot_table(quantity) print 'finished %s runID %s in thread %s' % ( quantity, runID, threading.currentThread().getName()) return False
def build_optionsPanel(self): optionsPanel = Gtk.VBox() optionsPanel.pack_start(Gtk.HBox(), False, False, 1) optionsPanel.get_children()[-1].pack_start(Gtk.Label(label='run'), False, True, 1) self.runEntry = Gtk.ComboBoxText(has_entry=True) optionsPanel.get_children()[-1].pack_end(self.runEntry, False, True, 1) self.runEntry.get_child().set_width_chars(4) self.runList = [] for run in ConniePaths.run()[::-1]: self.runEntry.append_text(str(run)) self.runList.append(str(run)) self.runEntry.connect('changed', self.on_run_changed) self.runEntry.get_child().connect('activate', self.on_plotButton_click) optionsPanel.pack_start(Gtk.HBox(), False, False, 1) optionsPanel.get_children()[-1].pack_start(Gtk.Label(label='runID'), False, False, 1) self.runIDEntry = Gtk.ComboBoxText(has_entry=True) self.runIDEntry.get_child().set_width_chars(5) self.runIDEntry.connect('changed', self.on_run_changed) self.runIDEntry.get_child().connect('activate', self.on_plotButton_click) optionsPanel.get_children()[-1].pack_end(self.runIDEntry, False, False, 1) optionsPanel.pack_start(Gtk.HBox(), False, False, 1) optionsPanel.get_children()[-1].pack_start(Gtk.Label(label='image'), False, False, 1) self.imageTypeEntry = Gtk.ComboBoxText() self.imageTypeOptions = [ 'raw', 'vosi', 'vhosi', 'osi', 'MB', 'mbs', 'scn', 'conv', 'convRaw' ] for key in self.imageTypeOptions: self.imageTypeEntry.append_text(key) optionsPanel.get_children()[-1].pack_end(self.imageTypeEntry, False, False, 1) self.imageTypeEntry.connect('changed', self.on_plotButton_click) optionsPanel.pack_start(Gtk.HBox(), False, False, 1) optionsPanel.get_children()[-1].pack_start(Gtk.Label(label='ohdu'), False, False, 1) self.ohduEntry = Gtk.ComboBoxText() self.ohdus = [2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14] for ohdu in self.ohdus: self.ohduEntry.append_text(str(ohdu)) optionsPanel.get_children()[-1].pack_end(self.ohduEntry, False, False, 1) self.ohduEntry.connect('changed', self.on_plotButton_click) optionsPanel.pack_start(Gtk.HBox(), False, False, 1) self.leftButton = Gtk.ToggleButton(label='left') optionsPanel.get_children()[-1].pack_start(self.leftButton, True, True, 0) self.leftButton.set_active(True) self.rightButton = Gtk.ToggleButton(label='right') optionsPanel.get_children()[-1].pack_end(self.rightButton, True, True, 0) self.leftButton.connect('toggled', self.on_side_toggled) self.rightButton.connect('toggled', self.on_side_toggled) self.plotOptions = ['E', 'x', 'y'] self.optionEntry = {} for key in self.plotOptions: optionsPanel.pack_start(Gtk.HBox(), False, False, 1) optionsPanel.get_children()[-1].pack_start(Gtk.Label(label=key), False, False, 1) if key == 'x' or key == 'y': for s in ['Max', 'Min']: self.optionEntry['%s%s' % (key, s)] = Gtk.Entry() self.optionEntry['%s%s' % (key, s)].set_text('auto') self.optionEntry['%s%s' % (key, s)].set_width_chars(5) optionsPanel.get_children()[-1].pack_end( self.optionEntry['%s%s' % (key, s)], False, False, 1) self.optionEntry['%s%s' % (key, s)].connect( 'activate', self.on_plotButton_click) else: self.optionEntry[key] = Gtk.Entry() optionsPanel.get_children()[-1].pack_end( self.optionEntry[key], False, False, 1) self.optionEntry[key].connect('activate', self.on_plotButton_click) self.optionEntry['E'].set_text('200') self.optionEntry['E'].set_width_chars(7) self.plotButton = Gtk.Button(label='plot', relief=Gtk.ReliefStyle.NONE) #self.plotButton.set_relief() self.plotButton.connect('clicked', self.on_plotButton_click) optionsPanel.pack_end(self.plotButton, False, False, 1) self.consoleButton = Gtk.ToggleButton(label='console') self.consoleButton.set_relief(Gtk.ReliefStyle.NONE) optionsPanel.pack_end(self.consoleButton, False, False, 1) self.consolePopover = Gtk.Popover(modal=False) self.consolePopover.set_position(Gtk.PositionType.RIGHT) self.consolePopover.add(Gtk.VBox()) self.consolePopover.get_child().pack_start( Gtk.Label(label='Console', margin=10), False, False, 1) self.consolePopover.get_child().pack_start(Gtk.HSeparator(), False, False, 0) self.Console = Gtk.Label(margin=10) self.consolePopover.get_child().pack_start(self.Console, True, True, 1) def toggled(widget): if widget.get_active(): self.consolePopover.set_relative_to(widget) self.consolePopover.show_all() else: self.consolePopover.hide() self.consoleButton.connect('toggled', toggled) self.pathsLabel = Gtk.Label() self.pathsLabel.set_label('None') self.pathsLabel.set_selectable(True) self.pathsLabel.set_property('margin', 10) self.pathsPopover = Gtk.Popover() self.pathsPopover.set_modal(False) self.pathsPopover.add(self.pathsLabel) pathsButton = Gtk.ToggleButton(label='paths') pathsButton.set_relief(Gtk.ReliefStyle.NONE) def toggled(widget): if widget.get_active(): self.pathsPopover.set_relative_to(widget) self.pathsPopover.show_all() else: self.pathsPopover.hide() pathsButton.connect('toggled', toggled) optionsPanel.pack_end(pathsButton, False, False, 1) return optionsPanel