class ApplicationWindow(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.initUI() def initUI(self): self.main_frame = QtGui.QWidget() self.setWindowTitle("Matplotlib Figure in a Qt4 Window with Navigation Toolbar") self.fig=Figure() self.axes = self.fig.add_subplot(111) self.x = np.arange(0.0, 1.0, 0.01) self.y = np.cos(2*np.pi*self.x + 5) + 2 self.axes.plot(self.x, self.y) self.canvas=FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus) self.canvas.setFocus() self.ntb = NavigationToolbar(self.canvas, self.main_frame) self.canvas.mpl_connect('key_press_event', self.on_key_press) vbox = QtGui.QVBoxLayout() vbox.addWidget(self.canvas) # the matplotlib canvas vbox.addWidget(self.ntb) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def on_key_press(self, event): print('you pressed', event.key) key_press_handler(event, self.canvas, self.ntb)
class PlotWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Online plot') self.create_main_frame() self.on_draw() def save_plot(self): pass def on_about(self): pass def on_pick(self, event): pass def on_draw(self): self.axes.clear() self.axes.grid(True) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() self.dpi = 100 self.fig = Figure((20.0, 6.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.axes = self.fig.add_subplot(111) self.canvas.mpl_connect('pick_event', self.on_pick) self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame)
class LsstCatalogPlot(QDialog): def __init__(self, main, ind, dep, color): QDialog.__init__(self, main) self.main = main self.setPalette(main.palette()) self.ind = ind self.dep = dep self.color = color self.info = QLabel('Click on a point to highlight the corresponding' ' record') self.figure = mpl.figure.Figure() self.canvas = FigureCanvasQTAgg(self.figure) self.canvas.setParent(self) self.canvas.setFocus() self.toolbar = NavigationToolbar2QT(self.canvas, self) self.ax = self.figure.add_subplot(111) self.scatterPlot = self.ax.scatter(self.ind, self.dep, c=self.color, picker=5, cmap=mpl.cm.YlGnBu_r) if self.color is not None: self.colorbar = self.figure.colorbar(self.scatterPlot) self.canvas.mpl_connect('pick_event', self.main.handleClick) vbox = QVBoxLayout() vbox.addWidget(self.info) vbox.addWidget(self.canvas) vbox.addWidget(self.toolbar) self.setLayout(vbox) self.show()
def peak_plot(self): """ This method creates a plot that allows the user to see the x location of all major peaks found in the integrated data Parameters ---------- self Returns ------- None """ window = QtGui.QDialog(self) window.setWindowTitle('Peak Locations') window.setWindowModality(QtCore.Qt.NonModal) fig = plt.figure() canvas = FigureCanvas(fig) toolbar = NavigationToolBar(canvas, self) canvas.mpl_connect('button_press_event', self.click_handling) peak_graph = PeakPlot(fig, canvas, self.int_data_dict, self.int_key_list) self.peak_plots.append(peak_graph) peak_graph.get_plot() layout = QtGui.QVBoxLayout() layout.addWidget(toolbar) layout.addWidget(canvas) window.setLayout(layout) window.show() window.exec_()
def r_rep_widget(self): """ This class creates the figure and instance of the ReducedRepPlot that is used to create the plot in the top right corner Parameters ---------- self Returns ------- None """ figure = plt.figure() canvas = FigureCanvas(figure) canvas.mpl_connect('button_press_event', self.click_handling) FigureCanvas.setSizePolicy(canvas, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) FigureCanvas.updateGeometry(canvas) canvas.setMinimumWidth(800) self.rpp = ReducedRepPlot(self.data_dict, self.key_list, 0, 100, 0, 100, "min", figure, canvas) toolbar = NavigationToolBar(canvas, self) layout = QtGui.QVBoxLayout() layout.addWidget(toolbar) layout.addWidget(canvas) self.display_box_1.addStretch() self.display_box_1.addLayout(layout)
def r_rep_widget(self): """ This class creates the figure and instance of the ReducedRepPlot that is used to create the plot in the top right corner Parameters ---------- self Returns ------- None """ fig = plt.figure() canvas = FigureCanvas(fig) canvas.mpl_connect('button_press_event', self.click_handling) FigureCanvas.setSizePolicy(canvas, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) FigureCanvas.updateGeometry(canvas) self.rpp_list.append(ReducedRepPlot(self.data_dict, self.key_list, fig, canvas, self.func_dict)) toolbar = NavigationToolBar(canvas, self) layout = QtGui.QVBoxLayout() layout.addWidget(toolbar) layout.addWidget(canvas) multi = QtGui.QWidget() multi.setLayout(layout) self.plot_dock.setWidget(multi)
class ClusteringConfig(QDialog): def __init__(self, parent=None): QDialog.__init__(self) self.parent = parent self.g = self.parent.matplot_frame.g self.setWindowTitle('Clustering Configuration') #MPL figure self.dpi = 100 self.fig = Figure((5.0, 4.0), dpi=self.dpi) self.fig.subplots_adjust(left=0,right=1,top=1,bottom=0) #QT canvas self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self) self.canvas.mpl_connect('pick_event', self.on_pick) #used when selecting canvas objects self.canvas.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) self.axes = self.fig.add_subplot(111) self.axes.hold(False) #clear the axes every time plot() is called #MPL Toolbar self.mpl_toolbar = NavigationToolbar(self.canvas, self) vbox=QVBoxLayout(self) vbox.addWidget(self.mpl_toolbar) vbox.addWidget(self.canvas) self.setLayout(vbox) self.resize(650,350) def on_pick(self): print 'picked in clustering config' def _show(self): self.axes.clear() self.g = self.parent.matplot_frame.g self.axes.plot([x[1] for x in self.g.mag], 'o-') i = 0 v = 45 for x in self.g.mag: self.axes.annotate(str(self.g.ind[i]), xy=(i, x[1]), xycoords='data', xytext=(0, v), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="angle3,angleA=0,angleB=-90"), ) if v == 15: v = 45 else: v -= 15 i += 1 self.show()
class Main(QMainWindow, Ui_MainWindow) : def __init__(self, ) : super(Main, self).__init__() self.setupUi(self) self.thisDir = os.path.dirname(os.path.abspath(__file__)) self.btOpenImage.clicked.connect(self.openImage) fig = Figure() self.canvas = FigureCanvas(fig) self.canvasGraph.addWidget(self.canvas) self.canvas.draw() return def openImage(self) : texto = 'Escolha uma imagem' path = QtGui.QFileDialog.getOpenFileNameAndFilter(self, texto, self.thisDir, "Images (*.png *.jpg)") datafile = cbook.get_sample_data(str(path[0])) img = imread(datafile) self.updateCanvas(self.initializeCanvas(img)) return def initializeCanvas(self, img) : fig = Figure() fig.clear() Graph = fig.add_subplot(111) Graph.imshow(img, zorder=0, extent=[0.0, 1.0, 0.0, 1.0]) return fig def updateCanvas(self, fig) : self.canvasGraph.removeWidget(self.canvas) self.canvas.close() self.canvas = FigureCanvas(fig) self.canvasGraph.addWidget(self.canvas) self.canvas.draw() def onclick(event): x, y = event.xdata, event.ydata print x, y self.canvas.mpl_connect('button_press_event', onclick) return
def getWindow(pressfun = pressed): aw = MyWindow() vbox = QtGui.QVBoxLayout() figc = FigureCanvas(Figure()) vbox.addWidget(figc) f = figc.figure ax = f.add_axes([0,0,1,1],xlim = [0,10],ylim=[0,10]) ax.plot(range(10)) aw.main_widget.setLayout(vbox) print 'setting pressfun' figc.mpl_connect('button_press_event',pressfun) return aw
def crearWidget(self, filtro, ep): fig10, fig11 = self.creaFiguras(filtro.tiempo, filtro.temp, filtro.flujo) canvas1 = FigureCanvas(fig10) canvas2 = FigureCanvas(fig11) vbox = QtGui.QGridLayout() vbox.addWidget(QtGui.QLabel("<b>Episodio:</b> " + filtro.nombre)) vbox.addWidget(QtGui.QLabel("<b>Inicio:</b> " + str(filtro.tiempo[0]))) vbox.addWidget(QtGui.QLabel("<b>Final:</b> " + str(filtro.tiempo[-1]))) vbox.addWidget(QtGui.QLabel("<b>Duración:</b> %i min" % (len(filtro.tiempo)))) vbox.addWidget(QtGui.QLabel("<b>Coeficiente de correlación:</b> " + str(filtro.correlacion)[:5])) vbox.addWidget(QtGui.QLabel("<b>Calorías consumidas:</b> " + str(filtro.numCalorias)[:6] + " (" + str(filtro.numCalorias/self.selep.totalCal*100)[:4] + "%)")) vbox.addWidget(canvas1) vbox.addWidget(canvas2) canvas2.mpl_connect('pick_event', lambda event: self.onpick(event, ep)) return vbox
def _get_new_canvas_layout(self, figure, beam): layout = QtGui.QVBoxLayout() canvas = FigureCanvas(figure) canvas.mpl_connect( 'motion_notify_event', lambda event: self._mouse_moved_on_figure(event, figure, beam) ) canvas.mpl_connect( 'button_press_event', lambda event: self._mouse_clicked_on_figure(event, figure, beam) ) toolbar = SbSGuiMatchResultView.CustomNavigationBar(canvas, figure, self) layout.addWidget(toolbar) layout.addWidget(canvas) return layout
def new_r_rep(self, selection, x_min, x_max, y_min, y_max): """This method will make a reduced representation graph in a new window Parameters ---------- selection : str The name of the desired analysis function x_min : int the starting x value defined by the ROI x_max : int the stopping x value defined by the ROI y_min : int the starting y value defined by the ROI y_max : int the stopping y value defined by the ROI Returns ------- None """ try: popup_window = QtGui.QDialog(self) popup_window.setWindowTitle(selection) popup_window.setWindowModality(QtCore.Qt.NonModal) fig = plt.figure() canvas = FigureCanvas(fig) canvas.mpl_connect('button_press_event', self.click_handling) toolbar = NavigationToolBar(canvas, self) self.rpp_list.append(ReducedRepPlot(self.data_dict, self.key_list, fig, canvas, self.func_dict, selection)) idx = len(self.rpp_list) - 1 vbox = QtGui.QVBoxLayout() vbox.addStretch() vbox.addWidget(toolbar) vbox.addStretch() vbox.addWidget(canvas) popup_window.setLayout(vbox) popup_window.show() self.rpp_list[idx].x_start = x_min self.rpp_list[idx].x_stop = x_max self.rpp_list[idx].y_start = y_min self.rpp_list[idx].y_stop = y_max self.rpp_list[idx].show() popup_window.exec_() except Exception: print("error creating a new rpp window") finally: self.rpp_list.__delitem__(idx)
class GraphViewer(Graphics): signalShowTitle = QtCore.pyqtSignal(str) signalGraphUpdate = pyqtSignal() waitEvent = threading.Event() signalPrint = pyqtSignal() signalPrintEnd = threading.Event() def __init__(self, parent = None): Graphics.__init__(self,parent) self.parent = parent self.create_main_frame() def create_main_frame(self): self.canvas = FigureCanvas(self.fig) self.canvas2 = FigureCanvas(self.fig2) self.canvas.setParent(self.parent.ui.frame) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() # self.mpl_toolbar = CustomToolbar(self.canvas, self.parent.ui.mpl,self) self.canvas.mpl_connect('pick_event',self.onPick) self.canvas.mpl_connect('key_press_event', self.on_key_press) # self.vbox = QVBoxLayout() self.vbox.addWidget(self.canvas) # the matplotlib canvas self.vbox.addWidget(self.canvas2) # the matplotlib canvas self.vbox.addWidget(self.mpl_toolbar) self.parent.ui.frame.setLayout(self.vbox) # # # self.fig.clear() # self.genPlotPage() self.genTextPage() self.canvas.setVisible(True) self.canvas2.setVisible(False) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() self.page = 1 self.signalGraphUpdate.emit() def genImage(self): self.fig.savefig('../WorkingDir/Page1.png', format='png')
class MatplotlibWidget(QWidget): def __init__(self,parent=None,grid=True): QWidget.__init__(self,parent) self.grid = grid self.fig = Figure() self.canvas =FigureCanvas(self.fig) self.canvas.setParent(self) self.canvas.mpl_connect('button_press_event', self.onPick) # bind pick event #self.axes = self.fig.add_subplot(111) margins = [0.05,0.1,0.9,0.8] self.axes = self.fig.add_axes(margins) self.toolbar = NavigationToolbar(self.canvas,self) #self.initFigure() layout = QVBoxLayout() layout.addWidget(self.toolbar) layout.addWidget(self.canvas) self.setLayout(layout) def onPick(self,event): print 'Pick event' print 'you pressed', event.button, event.xdata, event.ydata def update(self): self.canvas.draw() def plot(self,*args,**kwargs): self.axes.plot(*args,**kwargs) self.axes.grid(self.grid) self.update() def clear(self): self.axes.clear() def initFigure(self): self.axes.grid(True) x = np.linspace(-1,1) y = x**2 self.axes.plot(x,y,'o-')
def __crearBarWidget__(self, means): def onpick(event): rect = event.artist for i in range(len(self.bar)): # MEJORAR!! if self.bar[i] == rect: print "Barra", self.selep.epFiltro[i].nombre, self.selep.epFiltro[i].numCalorias, "calorías", len( self.selep.epFiltro[i].tiempo ), "minutos" lbl = ( "(" + self.selep.epFiltro[i].nombre + ") " + str(self.selep.epFiltro[i].numCalorias)[:6] + " calorías " + str(len(self.selep.epFiltro[i].tiempo)) + " minutos" ) self.lblDetalles.setText(lbl) return fig = plt.figure(facecolor="white") ax = fig.add_subplot(111) colors = [] for i in self.selep.epFiltro: if i.tipo == cachitos.tipoSueno: c = colores.sueno elif i.tipo == cachitos.tipoSedentario: c = colores.sedentario elif i.tipo == cachitos.tipoLigera: c = colores.ligero elif i.tipo == cachitos.tipoModerado: c = colores.moderado colors.append(c) ind = np.linspace(10, len(means), num=len(means)) self.bar = ax.bar(ind, means, color=colors, picker=1, align="center") ax.set_xticklabels(np.arange(len(means))) fig.tight_layout() canvas = FigureCanvas(fig) vbox = QtGui.QGridLayout() vbox.addWidget(canvas) canvas.mpl_connect("pick_event", onpick) # return FigureCanvas(fig) return vbox
class Plot(): def __init__(self, parent=None, width=5, height=5, dpi=100): #pl.ion() self.fig = Figure(figsize=(width, height), dpi=dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(parent) self.canvas.mpl_connect('button_press_event', self.on_click) self.rho_area = self.fig.add_subplot(111) #プロットエリアがクリックされた時 def on_click(self, event): print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%( event.button, event.x, event.y, event.xdata, event.ydata) row = int(event.ydata) col = int(event.xdata) print 'cca r:',self.r_m[row][col] def on_draw(self, r): self.r_m = r self.rho_area = self.fig.add_subplot(111) fs = 10 dr, dc = self.r_m.shape Y, X = np.mgrid[slice(0, dr+1, 1),slice(0, dc+1, 1)] img = self.rho_area.pcolor(X, Y, self.r_m, vmin=0.0, vmax=1.0, cmap=cm.gray) """ if self.cbar == None: self.cbar = self.fig.colorbar(img) self.cbar.ax.tick_params(labelsize=fs-1) """ self.rho_area.set_xlim(0, dc) self.rho_area.set_ylim(0, dr) wid = 10 #とりあえず決め打ちで10ずつ目盛表示 ticks = [i*wid for i in range(dr/wid+1)] labels = [(dr-1)/2-i*wid for i in range(dr/wid+1)] self.rho_area.set_yticks(ticks=ticks) self.rho_area.set_yticklabels(labels=labels) self.rho_area.set_xlabel("user 1") self.rho_area.set_ylabel("user 2") self.rho_area.tick_params(labelsize=fs) self.rho_area.set_title("rho", fontsize=fs+1) self.fig.canvas.draw()
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) #self.x, self.y = self.get_data() self.data = self.get_data2() self.create_main_frame() self.on_draw() def create_main_frame(self): self.main_frame = QWidget() self.fig = Figure((5.0, 4.0), dpi=100) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) self.canvas.mpl_connect('key_press_event', self.on_key_press) vbox = QVBoxLayout() vbox.addWidget(self.canvas) # the matplotlib canvas vbox.addWidget(self.mpl_toolbar) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def get_data2(self): return np.arange(20).reshape([4, 5]).copy() def on_draw(self): self.fig.clear() self.axes = self.fig.add_subplot(111) #self.axes.plot(self.x, self.y, 'ro') self.axes.imshow(self.data, interpolation='nearest') #self.axes.plot([1,2,3]) self.canvas.draw() def on_key_press(self, event): print('you pressed', event.key) # implement the default mpl key press events described at # http://matplotlib.org/users/navigation_toolbar.html#navigation-keyboard-shortcuts key_press_handler(event, self.canvas, self.mpl_toolbar)
def __createMatplotlibCanvas(self, plt_extent): fig = Figure((1, 1), #tight_layout=True, linewidth=0.0, subplotpars=matplotlib.figure.SubplotParams(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0 ) ) #fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None) #fig = Figure((24, 24), tight_layout=True) # try: # fig = Figure(tight_layout=True) # except: # #tight layout not available # fig = Figure() #fig = plt.figure() #fig.set_tight_layout(True) #font = {'family': 'arial', 'weight': 'normal', 'size': 12} #rc('font', **font) rect = fig.patch rect.set_facecolor((0.9, 0.9, 0.9)) # self.subplot = fig.add_axes( # #(0.08, 0.15, 0.92, 0.82), # (0.0, 0.0, 1.0, 1.0), # anchor='SW', # adjustable='box-forced' # ) #left bottom right top self.subplot = fig.add_axes( (LEFT_MARGIN, BOTTOM_MARGIN, RIGHT_MARGIN, TOP_MARGIN), adjustable='datalim', aspect=1 ) #self.subplot.plot.tight_layout(True) self.subplot.set_xbound(plt_extent.xmin, plt_extent.xmax) self.subplot.set_ybound(plt_extent.ymin, plt_extent.ymax) self.__setupAxes(self.subplot) #fig.tight_layout() canvas = FigureCanvasQTAgg(fig) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) canvas.setSizePolicy(sizePolicy) canvas.mpl_connect('pick_event', self.__plotPicked) canvas.mpl_connect('draw_event', self.__figureDrawn) canvas.mpl_connect('button_press_event', self.__buttonPressed) canvas.mpl_connect('motion_notify_event', self.__mouse_move) return canvas
def crearWidget(self, ep, derecho): """ ep: Episodio a visualizar derecho: 0/1 episodio izquierdo o derecho """ fig10, fig11 = self.creaFiguras(ep) canvas1 = FigureCanvas(fig10) canvas2 = FigureCanvas(fig11) vbox = QtGui.QGridLayout() vbox.addWidget(QtGui.QLabel("<b>Episodio:</b> " + ep.nombre)) vbox.addWidget(QtGui.QLabel("<b>Inicio:</b> " + str(ep.tiempo[0]))) vbox.addWidget(QtGui.QLabel("<b>Final:</b> " + str(ep.tiempo[-1]))) vbox.addWidget(QtGui.QLabel("<b>Duración:</b> %s min" % (ep.tiempo[-1] - ep.tiempo[0]))) vbox.addWidget(QtGui.QLabel("<b>Coeficiente de correlación:</b> " + str(ep.correlacion)[:5])) vbox.addWidget(QtGui.QLabel("<b>Calorías consumidas:</b> " + str(ep.numCalorias)[:6] + " (" + str(ep.numCalorias/self.selep.totalCal*100)[:4] + "%)")) vbox.addWidget(canvas1) vbox.addWidget(canvas2) canvas2.mpl_connect('pick_event', lambda event: self.onpick(event, derecho)) return vbox
def _refresh_mpl_widget(self): """ Create the mpl widget and update the underlying control. """ # Delete the old widgets in the layout, it's just shenanigans # to try to reuse the old widgets when the figure changes. widget = self.widget layout = widget.layout() while layout.count(): layout_item = layout.takeAt(0) layout_item.widget().deleteLater() # Create the new figure and toolbar widgets. It seems that key # events will not be processed without an mpl figure manager. # However, a figure manager will create a new toplevel window, # which is certainly not desired in this case. This appears to # be a limitation of matplotlib. The canvas is manually set to # visible, or QVBoxLayout will ignore it for size hinting. figure = self.declaration.figure if figure: canvas = FigureCanvasQTAgg(figure) canvas.setParent(widget) canvas.setFocusPolicy(Qt.ClickFocus) canvas.setVisible(True) canvas.setFocus() toolbar = NavigationToolbar2QT(canvas, widget) toolbar.setVisible(self.declaration.toolbar_visible) if self.declaration.toolbar_location == 'top': layout.addWidget(toolbar) layout.addWidget(canvas) else: layout.addWidget(canvas) layout.addWidget(toolbar) self.last_location = self.declaration.toolbar_location if self.declaration.event_actions: for event_name, event_action in self.declaration.event_actions: try: if event_name in VALID_EVENTS: canvas.mpl_connect(event_name, event_action) except: pass
class SLM_Dialog(QtGui.QDialog): def __init__(self, parent=None): super(SLM_Dialog, self).__init__(parent) self.fig = Figure() self.fc = FigureCanvas(self.fig) layout = QtGui.QHBoxLayout() layout.addWidget(self.fc) layout.setMargin(0) self.setLayout(layout) self.setGeometry(1681, 1050-768, 768, 768) self.fc.mpl_connect('button_press_event', self.close_on_click) def close_on_click(self, event): self.close() def imshow(self, img): axes = self.fig.add_axes([0,0,1,1]) axes.axis('off') axes.imshow(img, cmap=get_cmap('binary'))
class AppForm(QMainWindow): def __init__(self, fig): parent = None QMainWindow.__init__(self, parent) self.create_main_frame(fig) self.on_draw() def create_main_frame(self, fig): from matplotlib.backends.backend_qt4agg import ( FigureCanvasQTAgg as FigureCanvas, NavigationToolbar2QT as NavigationToolbar, ) self.main_frame = QWidget() self.fig = fig self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) self.canvas.mpl_connect("key_press_event", self.on_key_press) vbox = QVBoxLayout() vbox.addWidget(self.canvas) # the matplotlib canvas vbox.addWidget(self.mpl_toolbar) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def on_draw(self): self.canvas.draw() def on_key_press(self, event): from matplotlib.backend_bases import key_press_handler print("you pressed", event.key) # implement the default mpl key press events described at # http://matplotlib.org/users/navigation_toolbar.html key_press_handler(event, self.canvas, self.mpl_toolbar)
class PlotWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('CrazyFlie Magnetometer Calibration') self.create_main_frame() self.on_draw() def save_plot(self): pass def on_about(self): pass def on_pick(self, event): pass def on_draw(self): self.axes2D.clear() self.axes2D.grid(True) self.axes3D.clear() self.axes3D.grid(True) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() self.dpi = 100 self.fig = Figure((8.0, 4.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.axes3D = self.fig.add_subplot(121, projection='3d') self.axes2D = self.fig.add_subplot(122) self.canvas.mpl_connect('pick_event', self.on_pick) self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame)
def get_canva(self, edge=False, show_all_labels=False) : if not self.data : return None fig, axes = plt.subplots(len(self)) try : _ = (ax for ax in axes) except TypeError : axes = [axes] for index, ax in enumerate(axes) : if index == len(self)-1 or show_all_labels : self[index].plot(ax, edge=edge) else : self[index].plot(ax, show_labels=False, edge=edge) plt.tight_layout() sns.despine(left=True, bottom=True) canva = FigureCanvas(fig) canva.mpl_connect('button_press_event', self.click_pressed) canva.mpl_connect('button_release_event', self.click_released) return canva
class QFigureWidget(QtGui.QWidget): """Widget to layout the actual figure and toolbar. Further it forwards. the key press events from the widget to the figure.""" def __init__(self, fig, *args, **kw): super(QFigureWidget, self).__init__(*args, **kw) self.fig = fig self.canvas = FigureCanvasQTAgg(self.fig) self.canvas.setParent(self) self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus) self.canvas.setFocus() color = fig.get_facecolor() self.toolbar = QNavigationToolbar(self.canvas, self) self.toolbar.setStyleSheet("QNavigationToolbar { background-color: %s }" %rgb2hex(color)) self.toolbar.setIconSize(QtCore.QSize(16, 16)) self.canvas.mpl_connect('key_press_event', self.on_key_press) vbox = QtGui.QVBoxLayout(self) vbox.addWidget(self.canvas) vbox.addWidget(self.toolbar) vbox.setContentsMargins(0, 0, 0, 0) vbox.setSpacing(0) def on_key_press(self, event): # sometimes mpl has a weird ideas what oo-programing is. # any could overwrite method by my self # no fullscreen unless self is a window! if event.key == "t": self.toolbar.toggle() elif event.key not in rcParams["keymap.fullscreen"]: key_press_handler(event, self.canvas, self.toolbar)
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Beammap Chooser') self.create_main_frame() self.create_status_bar() def lookupPoint(self,event): x = event.xdata y = event.ydata if y != None and x != None and self.mpl_toolbar.mode == '': iClosestLeft = np.argmin((x-self.left[:,1])**2+(y-self.left[:,2])**2) print 'Closest Pixel:',self.leftNames[iClosestLeft],self.left[iClosestLeft] self.axes0.scatter(self.left[iClosestLeft,1],self.left[iClosestLeft,2],alpha=0.9,s=100,label='left',marker='o',color='c') self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() # Create the mpl Figure and FigCanvas objects. self.dpi = 100 self.fig = Figure((15.0, 15.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.axes0 = self.fig.add_subplot(111) cid=self.canvas.mpl_connect('button_press_event', self.lookupPoint) # Create the navigation toolbar, tied to the canvas self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def create_status_bar(self): self.status_text = QLabel("Awaiting orders.") self.statusBar().addWidget(self.status_text, 1) def plot_beammap(self): self.left,self.leftNames = getList('freq_atten_x_y_swap.txt') self.axes0.scatter(self.left[:,1],self.left[:,2],marker='o',alpha=0.5,s=100,label='left',color='b') self.canvas.draw()
class MPLWidget(FigureCanvas): """ A widget to contain a matplotlib figure. """ def __init__(self, parent=None, toolbar=False, tight_layout=True, autofocus=False, background_hack=True, **kwargs): """ A widget to contain a matplotlib figure. :param autofocus: [optional] If set to `True`, the figure will be in focus when the mouse hovers over it so that keyboard shortcuts/matplotlib events can be used. """ super(MPLWidget, self).__init__(Figure()) self.figure = Figure(tight_layout=tight_layout) self.canvas = FigureCanvas(self.figure) self.canvas.setParent(parent) # Focus the canvas initially. self.canvas.setFocusPolicy(QtCore.Qt.WheelFocus) self.canvas.setFocus() self.toolbar = None if autofocus: self._autofocus_cid = self.canvas.mpl_connect( "figure_enter_event", self._focus) self.figure.patch.set_facecolor([v/255. for v in self.palette().color(QtGui.QPalette.Window).getRgb()[:3]]) return None def _focus(self, event): """ Set the focus of the canvas. """ self.canvas.setFocus()
class QMPLWidget(QtGui.QWidget): """ Qt4 Widget container for matplotlib artists & canvas. """ def __init__(self, parent=None, axes=111, fig_facecolor="none"): """ Create a new QMPLWidget. """ # Parent constructor super(QMPLWidget, self).__init__(parent) # Set up figure, axes, figure canvas, and navigation bar self.fig = Figure() self.axes = self.fig.add_subplot(axes) self.canvas = FigureCanvas(self.fig) self.mpl_toolbar = NavigationToolbar(self.canvas, self, coordinates=False) # Enforce that the figure canvas expands/contracts as window is # resized self.canvas.setSizePolicy( QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)) self.canvas.updateGeometry() # Add a label for current mouse position self.loc_label = QtGui.QLabel("", self.canvas) # Location label should expand horizontally with window, but not # vertically self.loc_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom) self.loc_label.setSizePolicy( QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred)) # Get rid of ugly white/gray border around figure object in widget self.fig.patch.set_facecolor(fig_facecolor) # Setup and apply layout self.layout = QtGui.QVBoxLayout(self) self.layout.addWidget(self.mpl_toolbar) self.layout.addWidget(self.canvas) self.layout.addWidget(self.loc_label) self.setLayout(self.layout) # Link up events to callbacks self.canvas.mpl_connect('motion_notify_event', self.mouse_motion_callback) # Initial render # TODO: is this necessary? Check for both standalone and embedded use # cases self.canvas.draw() self.show() def mouse_motion_callback(self, mouse_event): """ Callback for mouse motion over canvas. Report the current x, y position of cursor in data coordinates. """ if mouse_event.inaxes: x, y = mouse_event.xdata, mouse_event.ydata self.loc_label.setText("x = %.3f\ty = %.3f" % (x, y)) else: self.loc_label.setText("")
class mGraph(QtGui.QWidget): def __init__(self, device, parent=None): QtGui.QWidget.__init__(self, parent) # Create a matplotlib figure. self.figure = plt.figure() self.figure.set_facecolor('r') # Create a QFrame to house the plot. This is not necessary, # just makes it look nice. self.matframe = QtGui.QFrame() self.matLayout = QtGui.QVBoxLayout() self.matLayout.setSpacing(0) self.matframe.setLayout(self.matLayout) self.matframe.setFrameShape(QtGui.QFrame.Panel) self.matframe.setFrameShadow(QtGui.QFrame.Plain) self.matframe.setStyleSheet( "background-color: rgb(70,80,88); " "margin:0px; border:2px solid rgb(0, 0, 0); ") self.canvas = FigureCanvas(self.figure) self.canvas.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred) # This is the device we want to use. self.device = device # This sets up axis on which to plot. color = (189. / 255, 195. / 255, 199. / 255) self.ax = self.figure.add_subplot(111, axisbg=color) ax = self.ax # Add the matplotlib canvas to the QFrame. self.matLayout.addWidget(self.canvas) # The following lines set up all the colors, makes it look nice. # The code to do it is far from pretty and I am planning # on cleaning this up a bit. self.figure.patch.set_color((70. / 255, 80. / 255, 88. / 255)) self.figure.patch.set_edgecolor((70. / 255, 80. / 255, 88. / 255)) ax.spines['bottom'].set_color(color) ax.spines['top'].set_color(color) ax.spines['right'].set_color(color) ax.spines['left'].set_color(color) ax.tick_params(axis='x', colors=color) ax.tick_params(axis='y', colors=color) ax.title.set_color(color) ax.yaxis.label.set_color(color) ax.xaxis.label.set_color(color) ax.xaxis.get_offset_text().set_color(color) ax.yaxis.get_offset_text().set_color(color) # This is an array of all the lines on the plot. A line for # every parameter. self.line = [] self.mins = 0 self.maxes = 1 # Each element of line holds a plot, to be combined onto # the same graph. self.line.append(ax.plot(1, 1, label="Getting Data...")[0]) # In order to handle interactivity, I had to do some odd stuff # with the toolbar buttons: self.home holds the original # function called when the home button on the toolbar # is clicked. self.home = NavigationToolbar.home # We now change the function that is called when the toolbar is # clicked. NavigationToolbar.home = self.enableAutoScaling self.toolbar = NavigationToolbar(self.canvas, self) self.cid = self.canvas.mpl_connect('button_press_event', self.disableAutoScaling) self.setStyleSheet("QPushButton{\ color:rgb(189,195, 199); \ background:rgb(70, 80, 88)};") self.fullGraphBtn = QtGui.QPushButton("Show Interactive Graph") self.fullGraphBtn.clicked.connect(self.openFullGraphGui) self.toolbarFrame = QtGui.QFrame() toolbarFrameLayout = QtGui.QVBoxLayout() toolbarFrameLayout.addWidget(self.toolbar) self.toolbar.setParent(None) self.toolbarFrame.setLayout(toolbarFrameLayout) self.toolbarFrame.setStyleSheet("\ border:2px solid rgb(0,0,0);\ color:rgb(189,195,199); \ background:rgb(70, 80, 88);") self.toolbar.setStyleSheet("\ border:0px solid rgb(0,0,0);\ QDialog{background:rgb(70, 80, 88)}") self.matPlotInfo = QtGui.QLabel() self.alertFont = QtGui.QFont() self.alertFont.setPointSize(12) self.matPlotInfo.setStyleSheet("color:rgb(200, 69, 50);") self.matPlotInfo.setText("Auto refresh disabled, " "click HOME button to enable.") self.matPlotInfo.setFont(self.alertFont) #self.refreshRateSec = device.getFrame().getPlotRefreshRate() self.refreshRateSec = device.getFrame().getPlotRefreshRate() self.timer = QtCore.QTimer(self) self.hidden = True self.home = True self.initialized = False self.currTimeRange = 120 self.lineSelect = MCheckableComboBox() self.lineSelect.setSizeAdjustPolicy(0) self.lineSelect.setStyleSheet("\ background-color:rgb(70, 80, 88);\ color:rgb(189,195, 199);") self.plot(self.currTimeRange) self.timer.timeout.connect(partial(self.plot, self.currTimeRange)) self.timer.start(self.refreshRateSec * 1000) # Did it store data? self.dataOk = True self.hideButton = QtGui.QPushButton("Show Plot") self.hideButton.clicked.connect(self.togglePlot) self.oneMinButton = QtGui.QPushButton("1 min") self.oneMinButton.clicked.connect(partial(self.plot, 60)) self.tenMinButton = QtGui.QPushButton("10 min") self.tenMinButton.clicked.connect(partial(self.plot, 600)) self.twoHrButton = QtGui.QPushButton("2 hr") self.twoHrButton.clicked.connect(partial(self.plot, 7200)) self.twelveHrButton = QtGui.QPushButton("12 hr") self.twelveHrButton.clicked.connect(partial(self.plot, 43200)) self.threeDayButton = QtGui.QPushButton("3 day") self.threeDayButton.clicked.connect(partial(self.plot, 259200)) self.oneWkButton = QtGui.QPushButton("1 week") self.oneWkButton.clicked.connect(partial(self.plot, 604800)) self.allButton = QtGui.QPushButton("All Time") self.allButton.clicked.connect(partial(self.plot, None)) self.canvas.hide() self.toolbar.hide() # Set the layout. buttonLayout1 = QtGui.QHBoxLayout() buttonLayout1.addWidget(self.hideButton) buttonLayout1.addWidget(self.fullGraphBtn) buttonLayout1.addStretch(0) buttonLayout2 = QtGui.QHBoxLayout() settingsbuttons1 = QtGui.QHBoxLayout() buttonLayout2.addWidget(self.oneMinButton) buttonLayout2.addWidget(self.tenMinButton) buttonLayout2.addWidget(self.twoHrButton) buttonLayout2.addWidget(self.twelveHrButton) buttonLayout2.addWidget(self.threeDayButton) buttonLayout2.addWidget(self.oneWkButton) buttonLayout2.addWidget(self.allButton) buttonLayout2.addStretch(0) self.oneMinButton.hide() self.tenMinButton.hide() self.twoHrButton.hide() self.twelveHrButton.hide() self.threeDayButton.hide() self.oneWkButton.hide() self.allButton.hide() self.lineSelect.hide() self.matframe.hide() self.matPlotInfo.hide() self.toolbarFrame.hide() settingsbuttons1.addWidget(self.lineSelect) layout = QtGui.QVBoxLayout() allButtonsLayout = QtGui.QHBoxLayout() timeButtonsLayout = QtGui.QVBoxLayout() allButtonsLayout.addLayout(timeButtonsLayout) layout.addLayout(allButtonsLayout) allButtonsLayout.addLayout(settingsbuttons1) timeButtonsLayout.addLayout(buttonLayout1) timeButtonsLayout.addLayout(buttonLayout2) timeButtonsLayout.addWidget(self.matPlotInfo) layout.addWidget(self.matframe) layout.addWidget(self.toolbarFrame) self.setLayout(layout) def enableAutoScaling(self): self.timer.start(self.refreshRateSec * 1000) self.home = True self.matPlotInfo.hide() self.plot(self.currTimeRange) def disableAutoScaling(self, event): self.home = False self.matPlotInfo.show() self.canvas.update() self.timer.stop() def togglePlot(self): if not self.hidden: self.canvas.hide() self.toolbar.hide() self.oneMinButton.hide() self.tenMinButton.hide() self.twoHrButton.hide() self.twelveHrButton.hide() self.threeDayButton.hide() self.oneWkButton.hide() self.allButton.hide() self.matPlotInfo.hide() self.matframe.hide() self.lineSelect.hide() self.toolbarFrame.hide() self.timer.stop() self.hideButton.setText("Show Plot") self.hidden = True elif self.hidden: self.canvas.show() self.toolbar.show() self.oneMinButton.show() self.tenMinButton.show() self.twoHrButton.show() self.twelveHrButton.show() self.threeDayButton.show() self.oneWkButton.show() self.allButton.show() self.plot(self.currTimeRange) self.matframe.show() self.lineSelect.show() self.toolbarFrame.show() self.timer.start(self.refreshRateSec * 1000) self.hideButton.setText("Hide Plot") self.enableAutoScaling() self.hidden = False def initializePlot(self, dataSet): if dataSet: varNames = dataSet.getVariables() varNames = [varNames[1][i][0] for i in range(len(varNames[1]))] self.dropdownFont = QtGui.QFont() self.dropdownFont.setPointSize(12) if dataSet is not None: self.initialized = True self.line[0].remove() self.line = [] for i in range(len(varNames)): self.line.append(self.ax.plot(1, 1, label=varNames[i])[0]) text = QtCore.QString(varNames[i]) self.lineSelect.addItem(text) self.lineSelect.setFont(self.dropdownFont) self.lineSelect.setChecked(i, True) def changeIndependenVarRange(self, timeRange): if not self.hidden: if timeRange != self.currTimeRange: self.timer.stop() self.timer.timeout.disconnect() self.currTimeRange = timeRange self.timer.timeout.connect( lambda: self.plot(self.currTimeRange)) self.timer.start(self.refreshRateSec * 1000) plotRefreshRate = self.device.getFrame().getPlotRefreshRate() if self.refreshRateSec != plotRefreshRate: self.refreshRateSec = plotRefreshRate self.timer.stop() self.timer.timeout.disconnect() self.currTimeRange = timeRange self.timer.timeout.connect( lambda: self.plot(self.currTimeRange)) self.timer.start(self.refreshRateSec * 1000) def getDataRangeFromDataSet(self, dataSet, time): if dataSet: data = dataSet.getData() i = len(data) - 1 if time: while data[i][0] > (data[-1][0] - time): i -= 1 if -1 * i > len(data): return data data = data[i:-1] return data else: return None def openFullGraphGui(self): # print "opening full graph gui." dataSet = self.device.getFrame().getDataSet().getData() # print dataSet times = [dt.datetime.fromtimestamp(elem[0]) for elem in dataSet] vars = self.device.getFrame().getDataSet().getVariables() self.fullgraphcont = fullGraphContainer(times, vars, dataSet) self.fullgraphcont.show() def plot(self, time): times = None self.changeIndependenVarRange(time) dataSet = self.device.getFrame().getDataSet() if not self.initialized: self.initializePlot(dataSet) self.legend = self.ax.legend(loc='upper left') # This is the ONLY time canvas.draw is called. It should # NOT be called anywhere else if the graphing speed is # to be fast. self.canvas.draw() else: data = self.getDataRangeFromDataSet(dataSet, time) for i in range(len(data[0]) - 1): if self.lineSelect.isChecked(i): times = [dt.datetime.fromtimestamp(row[0]) for row in data] column = [row[i + 1] for row in data] if not self.line[i].get_visible(): self.line[i].set_visible(True) self.line[i].set_data(times, column) self.legend = self.ax.legend(loc='upper left') self.ax.grid(True) self.ax.hold(True) else: self.line[i].set_visible(False) pass self.ax.set_title(self.device.getFrame().getTitle(), color=(189. / 255, 195. / 255, 199. / 255)) if self.home and times: self.ax.set_xlim(times[0], times[-1]) self.ax.relim(visible_only=True) self.ax.autoscale(axis='y') frame = self.device.getFrame() yLabel = frame.getYLabel() if yLabel is not None: if frame.getCustomUnits(): self.ax.set_ylabel("%s (%s)" % (yLabel, frame.getCustomUnits())) elif frame.getUnits()[i - 1]: self.ax.set_ylabel("%s (%s)" % (yLabel, frame.getUnits()[i - 1])) locator = AutoDateLocator() self.ax.xaxis.set_major_locator(locator) self.ax.xaxis.set_major_formatter(DateFormatter('%m/%d %H:%M:%S')) self.figure.autofmt_xdate() self.ax.draw_artist(self.figure) self.ax.draw_artist(self.ax.patch) self.ax.draw_artist(self.ax.yaxis) self.ax.draw_artist(self.ax.xaxis) for i, line in enumerate(self.line): self.ax.draw_artist(line) self.ax.set_xlabel("Time") self.ax.draw_artist(self.legend) self.canvas.update() self.canvas.flush_events()
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Demo: PyQt with matplotlib') self.create_menu() self.create_main_frame() self.create_status_bar() ## self.textbox.setText('1 2 3 4') self.on_draw() def save_plot(self): file_choices = "PNG (*.png)|*.png" path = unicode( QFileDialog.getSaveFileName(self, 'Save file', '', file_choices)) if path: self.canvas.print_figure(path, dpi=self.dpi) self.statusBar().showMessage('Saved to %s' % path, 2000) def on_about(self): msg = """ A demo of using PyQt with matplotlib: * Use the matplotlib navigation bar * Add values to the text box and press Enter (or click "Draw") * Show or hide the grid * Drag the slider to modify the width of the bars * Save the plot to a file using the File menu * Click on a bar to receive an informative message """ QMessageBox.about(self, "About the demo", msg.strip()) def on_pick(self, event): # The event received here is of the type # matplotlib.backend_bases.PickEvent # # It carries lots of information, of which we're using # only a small amount here. # def onpick1(event): if isinstance(event.artist, Text): text = event.artist value = text.get_prop_tup() text_to_pass = value[2] ## print('Hai selezionato l\'US:', text.get_text()) ## box_points = event.artist.get_bbox().get_points() msg = "'Hai selezionato l\'US:' %s" % text_to_pass #str(dir(text.get_label)) QMessageBox.information(self, "Click!", msg) def on_draw(self): """ Redraws the figure """ A = pgv.AGraph(directed=True) A.add_edge(1, 2) A.add_edge(2, 3) A.add_edge(3, 4) A.add_edge(3, 5) A.add_edge(2, 5) A.add_edge(2, 6) A.add_edge(2, 16) A.add_edge(2, 22) A.add_edge(2, 33) A.add_edge(33, 45) A.add_edge(45, 7) A.add_edge(45, 9) A.add_edge(45, 123) A.add_edge(45, 46) A.add_edge(45, 47) A.add_edge(3, 4) A.add_edge(3, 5) A.add_edge(2, 5) A.add_edge(2, 6) A.add_edge(2, 16) A.add_edge(2, 7) A.add_edge(2, 7) A.add_edge(2, 7) A.add_edge(2, 7) A.add_edge(2, 7) A.add_edge(2, 7) A.tred() G1 = nx.DiGraph(A) pos = nx.graphviz_layout(G1, prog='dot') #self.fig = plt.figure() ax = self.fig.add_subplot(111) ax.set_title('Clicca su una US', picker=True) ax.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red')) points = [] key = [] for k, v in pos.items(): key.append(k) points.append(v) for i in range(len(key)): ax.text(points[i][0], points[i][1], key[i], picker=True, ha='center', alpha=0) ax.plot(nx.draw(G1, pos, with_labels=True, arrows=True, node_color='w', node_shape='s', node_size=400), 'o', picker=10000) ## str = unicode(self.textbox.text()) ## self.data = map(int, str.split()) ## ## x = range(len(self.data)) ## ## # clear the axes and redraw the plot anew ## # ## self.axes.clear() ## self.axes.grid(self.grid_cb.isChecked()) ## ## self.axes.bar( ## left=x, ## height=self.data, ## width=self.slider.value() / 100.0, ## align='center', ## alpha=0.44, ## picker=5) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch ## # Since we have only one plot, we can use add_axes ## # instead of add_subplot, but then the subplot ## # configuration tool in the navigation toolbar wouldn't ## # work. ## # #self.axes = self.fig.add_subplot(111) # Bind the 'pick' event for clicking on one of the bars # self.dpi = 100 self.fig = plt.figure() self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.canvas.mpl_connect('pick_event', self.on_pick) # Create the navigation toolbar, tied to the canvas # self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # # Layout with box sizers # hbox = QHBoxLayout() vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def create_status_bar(self): self.status_text = QLabel("This is a demo") self.statusBar().addWidget(self.status_text, 1) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") load_file_action = self.create_action("&Save plot", shortcut="Ctrl+S", slot=self.save_plot, tip="Save the plot") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (load_file_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action, )) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
class smileiQtPlot(QWidget): scalarDict = dict() fieldDict = dict() phaseDict = dict() fieldFile = None phaseFile = None nplots = 0 ax = {} dirName = None someCheckBoxChanged = False pauseSignal = pyqtSignal() shiftPressed = False title = '' def __init__(self, parent, dirName): super(smileiQtPlot, self).__init__() self.setParent(parent) uiFile = os.path.dirname( os.path.realpath(__file__)) + '/smileiQtPlot.ui' self.ui = uic.loadUi(uiFile, self) self.dirName = dirName self.parent = parent self.parent.timer.timeout.connect(self.next) self.parent.ui.next.released.connect(self.next) self.parent.ui.previous.released.connect(self.previous) self.parent.ui.first.released.connect(self.first) self.parent.ui.last.released.connect(self.last) self.pauseSignal.connect(self.parent.pause) self.setWindowFlags(Qt.Window) self.setWindowTitle(dirName) self.step = 0 self.ui.savePoints.setIcon(self.ui.style().standardIcon( QStyle.SP_DialogSaveButton)) self.ui.savePoints.released.connect(self.doSavePoints) self.ui.tabWidget.currentChanged.connect(self.changeTab) self.ui.autoScale.stateChanged.connect(self.doPlots) self.fig = Figure() self.canvas = FigureCanvas(self.fig) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() self.canvas.mpl_connect('motion_notify_event', self.on_movement) self.canvas.mpl_connect('key_press_event', self.on_key_press) self.canvas.mpl_connect('key_release_event', self.on_key_release) self.canvas.mpl_connect('button_press_event', self.on_button_press) self.toolbar = NavigationToolbar(self.canvas, self) self.toolbar.setFixedHeight(18) self.canvas.setCursor(Qt.CrossCursor) self.ui.plotLayout.addWidget(self.canvas) self.ui.plotLayout.addWidget(self.toolbar) fname = os.path.join(dirName, "scalars.txt") if os.path.isfile(fname): self.scalarData = np.loadtxt(fname) names = [] for line in open(fname): li = line.strip() if li.startswith("#"): list = line.split() names.append(list[-1]) scalars_names = names[1:-2] for i in range(len(scalars_names)): my_button = QCheckBox(scalars_names[i]) my_button.stateChanged.connect(self.checkBoxChanged) self.ui.layoutScalars.addWidget(my_button) self.ui.layoutScalars.addStretch() else: print "Problem reading ", fname # self.deleteLater() self.fieldSteps = [] fname = os.path.join(dirName, "Fields.h5") if os.path.isfile(fname): self.fieldFile = tb.openFile(fname) self.res_space = self.fieldFile.root._v_attrs.res_space[0] self.res_time = self.fieldFile.root._v_attrs.res_time self.sim_length = self.fieldFile.root._v_attrs.sim_length self.fieldEvery = self.fieldFile.root._v_attrs.every first = True for group in self.fieldFile.listNodes("/", classname='Group'): self.fieldSteps.append(group._v_name) if first: first = False for array in group: my_button = QCheckBox(array._v_name) my_button.stateChanged.connect(self.checkBoxChanged) self.ui.layoutFields.addWidget(my_button) self.ui.layoutFields.addStretch() self.ui.slider.setRange(0, len(self.fieldSteps) - 1) else: print "Problem reading ", fname # self.deleteLater() self.ui.spinStep.setSuffix("/" + str(len(self.fieldSteps) - 1)) self.ui.spinStep.setMaximum(len(self.fieldSteps) - 1) fname = os.path.join(dirName, "PhaseSpace.h5") if os.path.isfile(fname): self.phaseFile = tb.openFile(fname) for phaseData in self.phaseFile.walkNodes("/", classname='Array'): my_button = QCheckBox(phaseData._v_pathname) my_button.stateChanged.connect(self.checkBoxChanged) self.ui.layoutPhase.addWidget(my_button) self.ui.layoutPhase.addStretch() else: print "Problem reading ", fname # self.deleteLater() self.load_settings() if sys.platform == "darwin": self.raise_() self.show() def doSavePoints(self): fname = QFileDialog.getSaveFileName(self, 'Save Point logger', self.dirName, selectedFilter='*.txt') if fname: f = open(fname, 'w') f.write(self.ui.logger.toPlainText()) f.close() def checkBoxChanged(self): self.someCheckBoxChanged = True def load_settings(self): settings = QSettings(QFileInfo(__file__).fileName(), "") settings.beginGroup(QDir(self.dirName).dirName()) frames = [self.ui.scalars, self.ui.fields, self.ui.phase] for frame in [self.ui.scalars, self.ui.fields, self.ui.phase]: settings.beginGroup(frame.objectName()) for chkbox in frame.findChildren(QCheckBox): chkbox.setChecked(settings.value(chkbox.text()).toBool()) settings.endGroup() settings.endGroup() self.ui.tabWidget.setCurrentIndex(0) def save_settings(self): settings = QSettings(QFileInfo(__file__).fileName(), "") settings.beginGroup(QDir(self.dirName).dirName()) frames = [self.ui.scalars, self.ui.fields, self.ui.phase] for frame in [self.ui.scalars, self.ui.fields, self.ui.phase]: settings.beginGroup(frame.objectName()) for chkbox in frame.findChildren(QCheckBox): settings.setValue(chkbox.text(), chkbox.isChecked()) settings.endGroup() settings.endGroup() def next(self): self.ui.slider.setValue(self.step + 1) def previous(self): self.ui.slider.setValue(self.step - 1) def first(self): self.ui.slider.setValue(0) def last(self): self.ui.slider.setValue(len(self.fieldSteps) - 1) def on_slider_valueChanged(self, step): self.step = step self.doPlots() @pyqtSignature("int") def on_spinStep_valueChanged(self, my_step): self.ui.slider.setValue(my_step) @pyqtSignature("int") def changeTab(self, tabNum): if self.ui.tabWidget.currentIndex() == 0: self.doPlots() else: self.pauseSignal.emit() def preparePlots(self): self.someCheckBoxChanged = False self.scalarDict = dict() self.fieldDict = dict() self.phaseDict = dict() self.nplots = 0 frames = [self.ui.scalars, self.ui.fields, self.ui.phase] for frame in [self.ui.scalars, self.ui.fields, self.ui.phase]: for chkbox in frame.findChildren(QCheckBox): if chkbox.isChecked(): self.nplots += 1 if self.nplots > 0: self.fig.clear() self.title = self.fig.suptitle('') self.ax = {} plot = 0 col = 0 print "preparing scalars" for i in self.ui.scalars.findChildren(QCheckBox): col += 1 if i.isChecked(): name = str(i.text()) if not (name in self.fieldDict): x = self.scalarData[:, 0] y = self.scalarData[:, col] self.scalarDict[name] = (x, y) ax = self.fig.add_subplot(self.nplots, 1, plot + 1) ax.xaxis.grid(True) ax.yaxis.grid(True) ax.plot(x, y) ax.set_xlim(x.min(), x.max()) ax.set_ylabel(name) ax.axvline(x=0, c="red", linewidth=2, zorder=0, clip_on=False) self.ax[name] = ax plot += 1 print "preparing fields" for i in self.ui.fields.findChildren(QCheckBox): if i.isChecked(): ax = self.fig.add_subplot(self.nplots, 1, plot + 1) ax.xaxis.grid(True) ax.yaxis.grid(True) data = [] name = str(i.text()) for d in self.fieldFile.root: data.append(d._f_getChild(name)) self.fieldDict[name] = data if len(self.sim_length) == 1: ax.set_xlim(0, self.sim_length) ax.set_ylabel(name) x = np.array(range(len(data[0]))) / self.res_space y = data[0] ax.plot(x, y) self.ax[name] = ax elif len(self.sim_length) == 2: divider = make_axes_locatable(ax) cax = divider.new_horizontal(size="2%", pad=0.05) self.fig.add_axes(cax) ax.set_ylabel(name) im = ax.imshow([[0]], extent=(0, self.sim_length[0], 0, self.sim_length[1]), aspect='auto', origin='lower') cb = plt.colorbar(im, cax=cax) self.ax[name] = ax plot += 1 print "preparing phase" for i in self.ui.phase.findChildren(QCheckBox): if i.isChecked(): name = str(i.text()) node = self.phaseFile.getNode(name) self.phaseDict[name] = node ax = self.fig.add_subplot(self.nplots, 1, plot + 1) ax.xaxis.grid(True) ax.yaxis.grid(True) ax.set_ylabel(name) divider = make_axes_locatable(ax) cax = divider.new_horizontal(size="2%", pad=0.05) self.fig.add_axes(cax) im = ax.imshow( [[0]], extent=node._v_parent._v_attrs.extents.reshape( 4).tolist(), aspect='auto', origin='lower') cb = plt.colorbar(im, cax=cax) self.ax[name] = ax plot += 1 print "done" self.doPlots() def on_movement(self, event): if not event.inaxes: return msg = "%G %G" % (event.xdata, event.ydata) self.ui.pos.setText(msg) def on_key_press(self, event): if not event.inaxes: return if event.key == 'a': for line in event.inaxes.lines: if line.get_xdata().size > 1: data = line.get_ydata() nonzero = data[np.nonzero(data)] if len(nonzero): mini = np.min(nonzero) maxi = np.max(nonzero) if mini != maxi: event.inaxes.set_ylim(mini, maxi) for image in event.inaxes.images: data = np.array(image.get_array()) nonzero = data[np.nonzero(data)] if len(nonzero): mini = np.min(nonzero) maxi = np.max(nonzero) if mini != maxi: image.set_clim(mini, maxi) self.canvas.draw() elif event.key == 'l': if event.inaxes.lines: scale = 'log' if event.inaxes.get_yaxis().get_scale( ) == 'linear' else 'linear' try: event.inaxes.set_yscale(scale) self.canvas.draw() except ValueError: scale = 'log' if scale == 'linear' else 'linear' event.inaxes.set_yscale(scale) self.canvas.draw() elif event.inaxes.images: for image in event.inaxes.images: mini = np.array(image.get_array()).clip(0).min() if mini > 0: maxi = np.array(image.get_array()).clip(0).max() print ">>>>>>>>", mini, maxi pprint(vars(event.inaxes.images[0].norm)) try: event.inaxes.images[0].set_norm(LogNorm( mini, maxi)) self.canvas.draw() except ValueError: self.canvas.draw() elif event.key == 'shift': self.shiftPressed = True def on_key_release(self, event): if not event.inaxes: return if event.key == 'shift': self.shiftPressed = False def on_button_press(self, event): if not event.inaxes: return if self.shiftPressed == True: self.ui.logger.moveCursor(QTextCursor.End) for i in range(len(self.fig.axes)): if self.fig.axes[i] == event.inaxes: txt = "%d %G %G %G\n" % (i, self.step / self.res_time * self.fieldEvery, event.xdata, event.ydata) QApplication.clipboard().setText(txt) self.ui.logger.insertPlainText(txt) self.ui.logger.moveCursor(QTextCursor.End) def doPlots(self): if len(self.fieldSteps) == 0: return if self.someCheckBoxChanged == True: self.preparePlots() self.step %= len(self.fieldSteps) self.slider.setValue(self.step) self.ui.spinStep.setValue(self.step) time = float(self.step) / self.res_time * self.fieldEvery for name in self.scalarDict: self.ax[name].lines[-1].set_xdata(time) for name in self.fieldDict: data = self.fieldDict[name][self.step] if len(self.sim_length) == 1: self.ax[name].lines[-1].set_ydata(data) if self.ui.autoScale.isChecked(): self.ax[name].set_ylim(min(data), max(data)) elif len(self.sim_length) == 2: im = self.ax[name].images[-1] npData = np.array(data) im.set_data(npData.T) if self.ui.autoScale.isChecked(): im.set_clim(npData.min(), npData.max()) for name in self.phaseDict: data = self.phaseDict[name][self.step].T im = self.ax[name].images[-1] im.set_data(data) if self.ui.autoScale.isChecked(): im.set_clim(data.min(), data.max()) self.title.set_text('Time: %.3f' % time) self.canvas.draw() if self.ui.saveImages.isChecked(): self.fig.savefig(self.dirName + '-%06d.png' % self.step) def closeEvent(self, event): self.save_settings() if self.fieldFile is not None: self.fieldFile.close() if self.phaseFile is not None: self.phaseFile.close() self.parent.plots.remove(self) QApplication.processEvents() self.deleteLater()
class PlotResponses(QtGui.QWidget): """ the plot and list of stations """ def __init__(self, data_fn=None, resp_fn=None): super(PlotResponses, self).__init__() self.file_watcher = QtCore.QFileSystemWatcher() self.file_watcher.fileChanged.connect(self.file_changed) self.modem_data = None self.modem_resp = None self._modem_data_copy = None self.station = None self._plot_z = False self.plot_settings = PlotSettings() self._ax = None self._ax2 = None self._key = 'z' self._ax_index = 0 self.ax_list = None self.setup_ui() self._data_fn = data_fn self._resp_fn = resp_fn #------------------------------------------------ # make the data_fn and resp_fn properties so that if they are reset # they will read in the data to a new modem.Data object # trying to use decorators for syntactical sugar @property def data_fn(self): self._data_fn @data_fn.getter def data_fn(self): return self._data_fn @data_fn.setter def data_fn(self, data_fn): self._data_fn = data_fn self.file_watcher.addPath(self._data_fn) # create new modem data object self.modem_data = mtpy.modeling.modem.Data() self.modem_data.read_data_file(self._data_fn) # make a back up copy that will be unchanged # that way we can revert back self._modem_data_copy = copy.deepcopy(self.modem_data) self.dirpath = os.path.dirname(self._data_fn) # fill list of stations station_list = sorted(self.modem_data.mt_dict.keys()) self.list_widget.clear() for station in station_list: self.list_widget.addItem(station) if self.station is None: self.station = station_list[0] self.plot() @property def resp_fn(self): self._resp_fn @resp_fn.getter def resp_fn(self): return self._resp_fn @resp_fn.setter def resp_fn(self, resp_fn): self._resp_fn = resp_fn self.modem_resp = mtpy.modeling.modem.Data() self.modem_resp.read_data_file(self._resp_fn) self.plot() @property def plot_z(self): self._plot_z @plot_z.getter def plot_z(self): return self._plot_z @plot_z.setter def plot_z(self, value): self._plot_z = value self.plot() #---------------------------- def setup_ui(self): """ setup the user interface with list of stations on the left and the plot on the right. There will be a button for save edits. """ #make a widget that will be the station list self.list_widget = QtGui.QListWidget() self.list_widget.itemClicked.connect(self.get_station) self.list_widget.setMaximumWidth(150) self.save_edits_button = QtGui.QPushButton() self.save_edits_button.setText("Save Edits") self.save_edits_button.setStyleSheet("background-color: #42f489") self.save_edits_button.pressed.connect(self.save_edits) self.apply_edits_button = QtGui.QPushButton() self.apply_edits_button.setText('Apply Edits') self.apply_edits_button.setStyleSheet("background-color: #c6dcff") self.apply_edits_button.pressed.connect(self.apply_edits) # self.undo_edit_button = QtGui.QPushButton() # this is the Canvas Widget that displays the `figure` # it takes the `figure` instance as a parameter to __init__ self.figure = Figure(dpi=150) self.mpl_widget = FigureCanvas(self.figure) self.mpl_widget.setFocusPolicy(QtCore.Qt.ClickFocus) self.mpl_widget.setFocus() # be able to edit the data self.mpl_widget.mpl_connect('pick_event', self.on_pick) self.mpl_widget.mpl_connect('axes_enter_event', self.in_axes) #make sure the figure takes up the entire plottable space self.mpl_widget.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) # this is the Navigation widget # it takes the Canvas widget and a parent self.mpl_toolbar = NavigationToolbar(self.mpl_widget, self) # set the layout for the plot mpl_vbox = QtGui.QVBoxLayout() mpl_vbox.addWidget(self.mpl_toolbar) mpl_vbox.addWidget(self.mpl_widget) left_layout = QtGui.QVBoxLayout() left_layout.addWidget(self.list_widget) left_layout.addWidget(self.apply_edits_button) left_layout.addWidget(self.save_edits_button) # set the layout the main window layout = QtGui.QHBoxLayout() layout.addLayout(left_layout) layout.addLayout(mpl_vbox) self.setLayout(layout) def get_station(self, widget_item): """ get the station name from the clicked station """ self.station = str(widget_item.text()) self.plot() def file_changed(self): """ data file changed outside the program reload it """ print '{0} changed'.format(self.data_fn) self.data_fn = self._data_fn def save_edits(self): """ save edits to another file """ fn_dialog = QtGui.QFileDialog() save_fn = str(fn_dialog.getSaveFileName(caption='Choose File to save', filter='*.dat')) self.modem_data.write_data_file(save_path=os.path.dirname(save_fn), fn_basename=os.path.basename(save_fn), compute_error=False, fill=False, elevation=True) def apply_edits(self): self.plot() def plot(self): """ plot the data """ if self.station is None: return z_obj = self.modem_data.mt_dict[self.station].Z t_obj = self.modem_data.mt_dict[self.station].Tipper period = self.modem_data.period_list # need to make sure that resistivity and phase is computed z_obj.compute_resistivity_phase() z_obj.compute_resistivity_phase() plt.rcParams['font.size'] = self.plot_settings.fs fontdict = {'size':self.plot_settings.fs+2, 'weight':'bold'} #--> make key word dictionaries for plotting kw_xx = {'color':self.plot_settings.cted, 'marker':self.plot_settings.mted, 'ms':self.plot_settings.ms, 'ls':':', 'lw':self.plot_settings.lw, 'e_capsize':self.plot_settings.e_capsize, 'e_capthick':self.plot_settings.e_capthick, 'picker':3} kw_yy = {'color':self.plot_settings.ctmd, 'marker':self.plot_settings.mtmd, 'ms':self.plot_settings.ms, 'ls':':', 'lw':self.plot_settings.lw, 'e_capsize':self.plot_settings.e_capsize, 'e_capthick':self.plot_settings.e_capthick, 'picker':3} #convert to apparent resistivity and phase if self.plot_z == True: scaling = np.zeros_like(z_obj.z) for ii in range(2): for jj in range(2): scaling[:, ii, jj] = 1./np.sqrt(z_obj.freq) plot_res = abs(z_obj.z.real*scaling) plot_res_err = abs(z_obj.z_err*scaling) plot_phase = abs(z_obj.z.imag*scaling) plot_phase_err = abs(z_obj.z_err*scaling) h_ratio = [1, 1, .5] elif self.plot_z == False: plot_res = z_obj.resistivity plot_res_err = z_obj.resistivity_err plot_phase = z_obj.phase plot_phase_err = z_obj.phase_err h_ratio = [1.5, 1, .5] #find locations where points have been masked nzxx = np.nonzero(z_obj.z[:, 0, 0])[0] nzxy = np.nonzero(z_obj.z[:, 0, 1])[0] nzyx = np.nonzero(z_obj.z[:, 1, 0])[0] nzyy = np.nonzero(z_obj.z[:, 1, 1])[0] ntx = np.nonzero(t_obj.tipper[:, 0, 0])[0] nty = np.nonzero(t_obj.tipper[:, 0, 1])[0] self.figure.clf() self.figure.suptitle(str(self.station), fontdict=fontdict) #set the grid of subplots if np.all(t_obj.tipper == 0.0) == True: self.plot_tipper = False else: self.plot_tipper = True gs = gridspec.GridSpec(3, 4, height_ratios=h_ratio) gs.update(wspace=self.plot_settings.subplot_wspace, left=self.plot_settings.subplot_left, top=self.plot_settings.subplot_top, bottom=self.plot_settings.subplot_bottom, right=self.plot_settings.subplot_right, hspace=self.plot_settings.subplot_hspace) axrxx = self.figure.add_subplot(gs[0, 0]) axrxy = self.figure.add_subplot(gs[0, 1], sharex=axrxx) axryx = self.figure.add_subplot(gs[0, 2], sharex=axrxx) axryy = self.figure.add_subplot(gs[0, 3], sharex=axrxx) axpxx = self.figure.add_subplot(gs[1, 0]) axpxy = self.figure.add_subplot(gs[1, 1], sharex=axrxx) axpyx = self.figure.add_subplot(gs[1, 2], sharex=axrxx) axpyy = self.figure.add_subplot(gs[1, 3], sharex=axrxx) axtxr = self.figure.add_subplot(gs[2, 0], sharex=axrxx) axtxi = self.figure.add_subplot(gs[2, 1], sharex=axrxx) axtyr = self.figure.add_subplot(gs[2, 2], sharex=axrxx) axtyi = self.figure.add_subplot(gs[2, 3], sharex=axrxx) self.ax_list = [axrxx, axrxy, axryx, axryy, axpxx, axpxy, axpyx, axpyy, axtxr, axtxi, axtyr, axtyi] # plot data response erxx = mtplottools.plot_errorbar(axrxx, period[nzxx], plot_res[nzxx, 0, 0], plot_res_err[nzxx, 0, 0], **kw_xx) erxy = mtplottools.plot_errorbar(axrxy, period[nzxy], plot_res[nzxy, 0, 1], plot_res_err[nzxy, 0, 1], **kw_xx) eryx = mtplottools.plot_errorbar(axryx, period[nzyx], plot_res[nzyx, 1, 0], plot_res_err[nzyx, 1, 0], **kw_yy) eryy = mtplottools.plot_errorbar(axryy, period[nzyy], plot_res[nzyy, 1, 1], plot_res_err[nzyy, 1, 1], **kw_yy) #plot phase epxx = mtplottools.plot_errorbar(axpxx, period[nzxx], plot_phase[nzxx, 0, 0], plot_phase_err[nzxx, 0, 0], **kw_xx) epxy = mtplottools.plot_errorbar(axpxy, period[nzxy], plot_phase[nzxy, 0, 1], plot_phase_err[nzxy, 0, 1], **kw_xx) epyx = mtplottools.plot_errorbar(axpyx, period[nzyx], plot_phase[nzyx, 1, 0], plot_phase_err[nzyx, 1, 0], **kw_yy) epyy = mtplottools.plot_errorbar(axpyy, period[nzyy], plot_phase[nzyy, 1, 1], plot_phase_err[nzyy, 1, 1], **kw_yy) #plot tipper if self.plot_tipper == True: ertx = mtplottools.plot_errorbar(axtxr, period[ntx], t_obj.tipper[ntx, 0, 0].real, t_obj.tipper_err[ntx, 0, 0], **kw_xx) erty = mtplottools.plot_errorbar(axtyr, period[nty], t_obj.tipper[nty, 0, 1].real, t_obj.tipper_err[nty, 0, 1], **kw_yy) eptx = mtplottools.plot_errorbar(axtxi, period[ntx], t_obj.tipper[ntx, 0, 0].imag, t_obj.tipper_err[ntx, 0, 0], **kw_xx) epty = mtplottools.plot_errorbar(axtyi, period[nty], t_obj.tipper[nty, 0, 1].imag, t_obj.tipper_err[nty, 0, 1], **kw_yy) #---------------------------------------------- # get error bar list for editing later if self.plot_tipper == False: try: self._err_list = [[erxx[1][0], erxx[1][1], erxx[2][0]], [erxy[1][0], erxy[1][1], erxy[2][0]], [eryx[1][0], eryx[1][1], eryx[2][0]], [eryy[1][0], eryy[1][1], eryy[2][0]], [epxx[1][0], epxx[1][1], epxx[2][0]], [epxy[1][0], epxy[1][1], epxy[2][0]], [epyx[1][0], epyx[1][1], epyx[2][0]], [epyy[1][0], epyy[1][1], epyy[2][0]]] line_list = [[erxx[0]], [erxy[0]], [eryx[0]], [eryy[0]]] except IndexError: print 'Found no Z components for {0}'.format(self.station) line_list = [[None], [None], [None], [None]] self._err_list = [[None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None]] else: try: line_list = [[erxx[0]], [erxy[0]], [eryx[0]], [eryy[0]], [ertx[0]], [erty[0]]] self._err_list = [[erxx[1][0], erxx[1][1], erxx[2][0]], [erxy[1][0], erxy[1][1], erxy[2][0]], [eryx[1][0], eryx[1][1], eryx[2][0]], [eryy[1][0], eryy[1][1], eryy[2][0]], [epxx[1][0], epxx[1][1], epxx[2][0]], [epxy[1][0], epxy[1][1], epxy[2][0]], [epyx[1][0], epyx[1][1], epyx[2][0]], [epyy[1][0], epyy[1][1], epyy[2][0]], [ertx[1][0], ertx[1][1], ertx[2][0]], [eptx[1][0], eptx[1][1], eptx[2][0]], [erty[1][0], erty[1][1], erty[2][0]], [epty[1][0], epty[1][1], epty[2][0]]] except IndexError: print 'Found no Z components for {0}'.format(self.station) line_list = [[None], [None], [None], [None], [None], [None]] self._err_list = [[None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None]] #------------------------------------------ # make things look nice # set titles of the Z components label_list = [['$Z_{xx}$'], ['$Z_{xy}$'], ['$Z_{yx}$'], ['$Z_{yy}$']] for ax, label in zip(self.ax_list[0:4], label_list): ax.set_title(label[0],fontdict={'size':self.plot_settings.fs+2, 'weight':'bold'}) # set legends for tipper components # fake a line l1 = plt.Line2D([0], [0], linewidth=0, color='w', linestyle='None', marker='.') t_label_list = ['Re{$T_x$}', 'Im{$T_x$}', 'Re{$T_y$}', 'Im{$T_y$}'] label_list += [['$T_{x}$'], ['$T_{y}$']] for ax, label in zip(self.ax_list[-4:], t_label_list): ax.legend([l1], [label], loc='upper left', markerscale=.01, borderaxespad=.05, labelspacing=.01, handletextpad=.05, borderpad=.05, prop={'size':max([self.plot_settings.fs, 5])}) #--> set limits if input if self.plot_settings.res_xx_limits is not None: axrxx.set_ylim(self.plot_settings.res_xx_limits) if self.plot_settings.res_xy_limits is not None: axrxy.set_ylim(self.plot_settings.res_xy_limits) if self.plot_settings.res_yx_limits is not None: axryx.set_ylim(self.plot_settings.res_yx_limits) if self.plot_settings.res_yy_limits is not None: axryy.set_ylim(self.plot_settings.res_yy_limits) if self.plot_settings.phase_xx_limits is not None: axpxx.set_ylim(self.plot_settings.phase_xx_limits) if self.plot_settings.phase_xy_limits is not None: axpxy.set_ylim(self.plot_settings.phase_xy_limits) if self.plot_settings.phase_yx_limits is not None: axpyx.set_ylim(self.plot_settings.phase_yx_limits) if self.plot_settings.phase_yy_limits is not None: axpyy.set_ylim(self.plot_settings.phase_yy_limits) #set axis properties for aa, ax in enumerate(self.ax_list): ax.tick_params(axis='y', pad=self.plot_settings.ylabel_pad) ylabels = ax.get_yticks().tolist() if aa < 8: ylabels[-1] = '' ylabels[0] = '' ax.set_yticklabels(ylabels) plt.setp(ax.get_xticklabels(), visible=False) if self.plot_z == True: ax.set_yscale('log', nonposy='clip') else: ax.set_xlabel('Period (s)', fontdict=fontdict) if aa < 4 and self.plot_z is False: ax.set_yscale('log', nonposy='clip') #set axes labels if aa == 0: if self.plot_z == False: ax.set_ylabel('App. Res. ($\mathbf{\Omega \cdot m}$)', fontdict=fontdict) elif self.plot_z == True: ax.set_ylabel('Re[Z (mV/km nT)]', fontdict=fontdict) elif aa == 4: if self.plot_z == False: ax.set_ylabel('Phase (deg)', fontdict=fontdict) elif self.plot_z == True: ax.set_ylabel('Im[Z (mV/km nT)]', fontdict=fontdict) elif aa == 8: ax.set_ylabel('Tipper', fontdict=fontdict) if aa > 7: if self.plot_settings.tipper_limits is not None: ax.set_ylim(self.plot_settings.tipper_limits) else: pass ax.set_xscale('log', nonposx='clip') ax.set_xlim(xmin=10**(np.floor(np.log10(period[0])))*1.01, xmax=10**(np.ceil(np.log10(period[-1])))*.99) ax.grid(True, alpha=.25) ##---------------------------------------------- #plot model response if self.modem_resp is not None: resp_z_obj = self.modem_resp.mt_dict[self.station].Z resp_z_err = np.nan_to_num((z_obj.z-resp_z_obj.z)/z_obj.z_err) resp_z_obj.compute_resistivity_phase() resp_t_obj = self.modem_resp.mt_dict[self.station].Tipper resp_t_err = np.nan_to_num((t_obj.tipper-resp_t_obj.tipper)/t_obj.tipper_err) #convert to apparent resistivity and phase if self.plot_z == True: scaling = np.zeros_like(resp_z_obj.z) for ii in range(2): for jj in range(2): scaling[:, ii, jj] = 1./np.sqrt(resp_z_obj.freq) r_plot_res = abs(resp_z_obj.z.real*scaling) r_plot_phase = abs(resp_z_obj.z.imag*scaling) elif self.plot_z == False: r_plot_res = resp_z_obj.resistivity r_plot_phase = resp_z_obj.phase rms_xx = resp_z_err[nzxx, 0, 0].std() rms_xy = resp_z_err[nzxy, 0, 1].std() rms_yx = resp_z_err[nzyx, 1, 0].std() rms_yy = resp_z_err[nzyy, 1, 1].std() #--> make key word dictionaries for plotting kw_xx = {'color':self.plot_settings.ctem, 'marker':self.plot_settings.mtem, 'ms':self.plot_settings.ms, 'ls':':', 'lw':self.plot_settings.lw, 'e_capsize':self.plot_settings.e_capsize, 'e_capthick':self.plot_settings.e_capthick} kw_yy = {'color':self.plot_settings.ctmm, 'marker':self.plot_settings.mtmm, 'ms':self.plot_settings.ms, 'ls':':', 'lw':self.plot_settings.lw, 'e_capsize':self.plot_settings.e_capsize, 'e_capthick':self.plot_settings.e_capthick} # plot data response rerxx = mtplottools.plot_errorbar(axrxx, period[nzxx], r_plot_res[nzxx, 0, 0], None, **kw_xx) rerxy = mtplottools.plot_errorbar(axrxy, period[nzxy], r_plot_res[nzxy, 0, 1], None, **kw_xx) reryx = mtplottools.plot_errorbar(axryx, period[nzyx], r_plot_res[nzyx, 1, 0], None, **kw_yy) reryy = mtplottools.plot_errorbar(axryy, period[nzyy], r_plot_res[nzyy, 1, 1], None, **kw_yy) #plot phase repxx = mtplottools.plot_errorbar(axpxx, period[nzxx], r_plot_phase[nzxx, 0, 0], None, **kw_xx) repxy = mtplottools.plot_errorbar(axpxy, period[nzxy], r_plot_phase[nzxy, 0, 1], None, **kw_xx) repyx = mtplottools.plot_errorbar(axpyx, period[nzyx], r_plot_phase[nzyx, 1, 0], None, **kw_yy) repyy = mtplottools.plot_errorbar(axpyy, period[nzyy], r_plot_phase[nzyy, 1, 1], None, **kw_yy) #plot tipper if self.plot_tipper == True: rertx = mtplottools.plot_errorbar(axtxr, period[ntx], resp_t_obj.tipper[ntx, 0, 0].real, None, **kw_xx) rerty = mtplottools.plot_errorbar(axtyr, period[nty], resp_t_obj.tipper[nty, 0, 1].real, None, **kw_yy) reptx = mtplottools.plot_errorbar(axtxi, period[ntx], resp_t_obj.tipper[ntx, 0, 0].imag, None, **kw_xx) repty = mtplottools.plot_errorbar(axtyi, period[nty], resp_t_obj.tipper[nty, 0, 1].imag, None, **kw_yy) if self.plot_tipper == False: line_list[0] += [rerxx[0]] line_list[1] += [rerxy[0]] line_list[2] += [reryx[0]] line_list[3] += [reryy[0]] label_list[0] += ['$Z^m_{xx}$ '+ 'rms={0:.2f}'.format(rms_xx)] label_list[1] += ['$Z^m_{xy}$ '+ 'rms={0:.2f}'.format(rms_xy)] label_list[2] += ['$Z^m_{yx}$ '+ 'rms={0:.2f}'.format(rms_yx)] label_list[3] += ['$Z^m_{yy}$ '+ 'rms={0:.2f}'.format(rms_yy)] else: line_list[0] += [rerxx[0]] line_list[1] += [rerxy[0]] line_list[2] += [reryx[0]] line_list[3] += [reryy[0]] line_list[4] += [rertx[0]] line_list[5] += [rerty[0]] label_list[0] += ['$Z^m_{xx}$ '+ 'rms={0:.2f}'.format(rms_xx)] label_list[1] += ['$Z^m_{xy}$ '+ 'rms={0:.2f}'.format(rms_xy)] label_list[2] += ['$Z^m_{yx}$ '+ 'rms={0:.2f}'.format(rms_yx)] label_list[3] += ['$Z^m_{yy}$ '+ 'rms={0:.2f}'.format(rms_yy)] label_list[4] += ['$T^m_{x}$ '+ 'rms={0:.2f}'.format(resp_t_err[ntx, 0, 0].std())] label_list[5] += ['$T^m_{y}$'+ 'rms={0:.2f}'.format(resp_t_err[nty, 0, 1].std())] legend_ax_list = self.ax_list[0:4] if self.plot_tipper == True: legend_ax_list += [self.ax_list[-4], self.ax_list[-2]] for aa, ax in enumerate(legend_ax_list): ax.legend(line_list[aa], label_list[aa], loc=self.plot_settings.legend_loc, bbox_to_anchor=self.plot_settings.legend_pos, markerscale=self.plot_settings.legend_marker_scale, borderaxespad=self.plot_settings.legend_border_axes_pad, labelspacing=self.plot_settings.legend_label_spacing, handletextpad=self.plot_settings.legend_handle_text_pad, borderpad=self.plot_settings.legend_border_pad, prop={'size':max([self.plot_settings.fs, 5])}) self.mpl_widget.draw() def on_pick(self, event): """ mask a data point when it is clicked on. """ data_point = event.artist data_period = data_point.get_xdata()[event.ind] data_value = data_point.get_ydata()[event.ind] # get the indicies where the data point has been edited p_index = np.where(self.modem_data.period_list==data_period)[0][0] s_index = np.where(self.modem_data.data_array['station']==self.station)[0][0] if self._key == 'tip': data_value_2 = self.modem_data.mt_dict[self.station].Tipper.tipper[p_index, self._comp_index_x, self._comp_index_y] if self._ax_index%2 == 0: data_value_2 = data_value_2.imag else: data_value_2 = data_value_2.real elif self._key == 'z': if self.plot_z == True: data_value_2 = self.modem_data.mt_dict[self.station].Z.z[p_index, self._comp_index_x, self._comp_index_y] if self._ax_index % 2 == 0: data_value_2 = data_value_2.imag else: data_value_2 = data_value_2.real elif self.plot_z == False and self._ax_index < 4: data_value_2 = self.modem_data.mt_dict[self.station].Z.phase[p_index, self._comp_index_x, self._comp_index_y] elif self.plot_z == False and self._ax_index >= 4: data_value_2 = self.modem_data.mt_dict[self.station].Z.resistivity[p_index, self._comp_index_x, self._comp_index_y] if event.mouseevent.button == 1: # mask the point in the data mt_dict self.modem_data.data_array[s_index][self._key][p_index, self._comp_index_x, self._comp_index_y] = 0+0j if self._key == 'tip': self.modem_data.mt_dict[self.station].Tipper.tipper[p_index, self._comp_index_x, self._comp_index_y] = 0+0j elif self._key == 'z': self.modem_data.mt_dict[self.station].Z.z[p_index, self._comp_index_x, self._comp_index_y] = 0+0j # plot the points as masked self._ax.plot(data_period, data_value, color=(0, 0, 0), marker='x', ms=self.plot_settings.ms*2, mew=4) self._ax2.plot(data_period, data_value_2, color=(0, 0, 0), marker='x', ms=self.plot_settings.ms*2, mew=4) self._ax.figure.canvas.draw() self._ax2.figure.canvas.draw() # Increase error bars if event.mouseevent.button == 3: # make sure just checking the top plots #put the new error into the error array if self._key == 'tip': err = self.modem_data.mt_dict[self.station].Tipper.tipper_err[p_index, self._comp_index_x, self._comp_index_y] err = err+abs(err)*self.plot_settings.t_err_increase self.modem_data.mt_dict[self.station].Tipper.tipper_err[p_index, self._comp_index_x, self._comp_index_y] = err if self._key == 'z': err = self.modem_data.mt_dict[self.station].Z.z_err[p_index, self._comp_index_x, self._comp_index_y] err = err+abs(err)*self.plot_settings.z_err_increase self.modem_data.mt_dict[self.station].Z.z_err[p_index, self._comp_index_x, self._comp_index_y] = err self.modem_data.data_array[s_index][self._key+'_err'][p_index, self._comp_index_x, self._comp_index_y] = err # make error bar array eb = self._err_list[self._ax_index][2].get_paths()[p_index].vertices # make ecap array ecap_l = self._err_list[self._ax_index][0].get_data()[1][p_index] ecap_u = self._err_list[self._ax_index][1].get_data()[1][p_index] # change apparent resistivity error if self._key == 'tip': neb_u = eb[0, 1]-self.plot_settings.t_err_increase*abs(eb[0,1]) neb_l = eb[1, 1]+self.plot_settings.t_err_increase*abs(eb[1,1]) ecap_l = ecap_l-self.plot_settings.t_err_increase*abs(ecap_l) ecap_u = ecap_u+self.plot_settings.t_err_increase*abs(ecap_u) elif self._key == 'z': neb_u = eb[0, 1]-self.plot_settings.z_err_increase*abs(eb[0,1]) neb_l = eb[1, 1]+self.plot_settings.z_err_increase*abs(eb[1,1]) ecap_l = ecap_l-self.plot_settings.z_err_increase*abs(ecap_l) ecap_u = ecap_u+self.plot_settings.z_err_increase*abs(ecap_u) #set the new error bar values eb[0,1] = neb_u eb[1,1] = neb_l #reset the error bars and caps ncap_l = self._err_list[self._ax_index][0].get_data() ncap_u = self._err_list[self._ax_index][1].get_data() ncap_l[1][p_index] = ecap_l ncap_u[1][p_index] = ecap_u #set the values self._err_list[self._ax_index][0].set_data(ncap_l) self._err_list[self._ax_index][1].set_data(ncap_u) self._err_list[self._ax_index][2].get_paths()[p_index].vertices = eb # need to redraw the figure self._ax.figure.canvas.draw() def in_axes(self, event): """ figure out which axes you just chose the point from """ ax_index_dict = {0:(0, 0), 1:(0, 1), 2:(1, 0), 3:(1, 1), 4:(0, 0), 5:(0, 1), 6:(1, 0), 7:(1, 1), 8:(0, 0), 9:(0, 0), 10:(0, 1), 11:(0, 1)} ax_pairs = {0:4, 1:5, 2:6, 3:7, 4:0, 5:1, 6:2, 7:3, 8:9, 9:8, 10:11, 11:10} # make the axis an attribute self._ax = event.inaxes # find the component index so that it can be masked for ax_index, ax in enumerate(self.ax_list): if ax == event.inaxes: self._comp_index_x, self._comp_index_y = ax_index_dict[ax_index] self._ax_index = ax_index self._ax2 = self.ax_list[ax_pairs[ax_index]] if ax_index < 8: self._key = 'z' else: self._key = 'tip'
class PlotWidget(QWidget): def __init__(self, parent=None, image=None, toolbar=True): QWidget.__init__(self, parent) self.data = image self.dpi = 100 self.cmap = 'gray' self.toolbar = toolbar self.create_main_widget() if self.data is not None: self.on_draw() def create_main_widget(self): print(self.data) self.fig = Figure((10.0, 8.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() vbox = QVBoxLayout() vbox.addWidget(self.canvas) # the matplotlib canvas if self.toolbar: self.mpl_toolbar = NavigationToolbar(self.canvas, self) vbox.addWidget(self.mpl_toolbar) self.canvas.mpl_connect('key_press_event', self.on_key_press) self.setLayout(vbox) def set_image(self, image): self.data = image self.on_draw() def on_key_press(self, event): if event.key == 'i': print('* image infos:') print('shape is (%dx%d)' % (np.shape(self.data)[0], np.shape(self.data)[1])) print('min in image= %d, max in image=%d' % (np.min(self.data), np.max(self.data))) elif event.key == 'right': # load next image self.parent().parent().on_next_image() elif event.key == 'left': # load previous image self.parent().parent().on_prev_image() elif event.key == 'h': # plot the image histogram hist(self.data, data_range=(np.min(self.data), np.max(self.data)), show=True) # implement the default mpl key press events described at # http://matplotlib.org/users/navigation_toolbar.html#navigation-keyboard-shortcuts key_press_handler(event, self.canvas, self.mpl_toolbar) def on_draw(self): if not hasattr(self.fig, 'subplot'): self.axes = self.fig.add_subplot(111) self.axes.imshow(self.data, cmap=self.cmap, origin='upper', interpolation='nearest', clim=[np.min(self.data), np.max(self.data)]) self.canvas.draw()
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.create_menu() self.suOrbit = SuperOrbit() self.create_main_frame() self.lastFile = '' self.track = 0 if os.path.exists("file.txt"): f = open("file.txt", 'r') self.lastFile = cPickle.load(f) self.suOrbit.open_file(self.lastFile) f.close() self.create_points() else: self.errors_button.setEnabled(False) #self.suOrbit = SuperOrbit() def create_main_frame(self): self.main_frame = QWidget() self.create_graphics() self.errors_button = QPushButton("seed errors") self.cor_button = QPushButton("correction") self.cor_button.setEnabled(False) self.resp_button = QPushButton("response matrix calculation") self.resp_button_meas = QPushButton("response matrix measurement") self.resp_button.setEnabled(False) self.resp_button_meas.setEnabled(False) self.cb_track = QCheckBox('Show trajectory') #cb.toggle() self.cb_track.stateChanged.connect(self.show_track) QObject.connect(self.errors_button, SIGNAL("clicked()"), self.suOrbit.seed_and_read) QObject.connect(self.suOrbit, SIGNAL("plot()"), self.draw_plot) QObject.connect(self.resp_button, SIGNAL("clicked()"), self.suOrbit.find_resp) QObject.connect(self.suOrbit, SIGNAL("resp_ok()"), self.switch_correction) QObject.connect(self.suOrbit, SIGNAL("resp_off()"), self.switch_off) QObject.connect(self.cor_button, SIGNAL("clicked()"), self.suOrbit.correction) # Layout with box sizers # grid = QGridLayout() grid.setSpacing(5) grid.addWidget(self.canvas, 0, 0, 3, 3) grid.addWidget(self.mpl_toolbar, 4, 0, 1, 3) grid.addWidget(self.resp_button, 5, 0, 1, 1) grid.addWidget(self.resp_button_meas, 6, 0, 1, 1) grid.addWidget(self.cor_button, 5, 1, 1, 1) grid.addWidget(self.errors_button, 5, 2, 1, 1) grid.addWidget(self.cb_track, 6, 1) self.main_frame.setLayout(grid) self.setCentralWidget(self.main_frame) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") load_file_action = self.create_action("&Open file", shortcut="Ctrl+S", slot=self.open_file, tip="Open input file") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (load_file_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action, )) def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def open_file(self): file_choices = "*.py;*.inp" path = QFileDialog.getOpenFileNames(self, 'Open file', self.lastFile, file_choices) if len(path) != 0: self.path = unicode(path[0]) f = open("file.txt", 'w') cPickle.dump(self.path, f) f.close() self.analysis() def on_about(self): msg = """ Orbit correction """ QMessageBox.about(self, "About the demo", msg.strip()) def analysis(self): self.suOrbit.open_file(self.path) self.create_points() def create_points(self): self.points_x = [] self.points_y = [] for i, bpm in enumerate(self.suOrbit.orbit.bpms): point_x, = self.axes.plot([], 'ro') point_y, = self.axes2.plot([], 'ro') self.points_x.append(point_x) self.points_y.append(point_y) self.errors_button.setEnabled(True) def create_graphics(self): self.setWindowTitle('Orbit correction') self.resize(800, 600) self.points_with_annotation = [] self.fig = Figure() self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self) self.fig.clear() self.fig.canvas.set_window_title('Horizontal orbit [m]') self.axes = self.fig.add_subplot(211) self.axes.set_title("Horizontal orbit [m]") self.axes.set_ylabel("X, [m]") self.axes.grid(True) self.fig.canvas.set_window_title('Vertical orbit [m]') self.axes2 = self.fig.add_subplot(212) self.axes2.set_title("Vertical orbit [m]") self.axes2.set_xlabel("S, [m]") self.axes2.set_ylabel("Y, [m]") self.axes2.grid(True) self.p1, = self.axes.plot([], 'ro-', lw=2.0) self.p2, = self.axes.plot([], 'bo-', lw=2.0) self.show_x, = self.axes.plot([], 'black', lw=1.0) self.axes.legend([r'$old$', r'$new$']) self.p3, = self.axes2.plot([], 'ro-', lw=2.0) self.p4, = self.axes2.plot([], 'bo-', lw=2.0) self.show_y, = self.axes2.plot([], 'black', lw=1.0) self.axes2.legend([r'$old$', r'$new$']) self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) #self.canvas.mpl_connect('pick_event', self.line_picker) self.canvas.mpl_connect('motion_notify_event', self.on_move) def show_track(self): #print self.cb_track.checkState(), self.track if self.track and self.cb_track.checkState(): self.suOrbit.orbit.calc_track(self.suOrbit.lat_errors) self.show_x.set_data(self.suOrbit.orbit.s_track, self.suOrbit.orbit.x_track) self.show_y.set_data(self.suOrbit.orbit.s_track, self.suOrbit.orbit.y_track) else: self.show_x.set_data([], []) self.show_y.set_data([], []) self.canvas.draw() def draw_plot(self): self.track = 1 s = map(lambda b: b.s, self.suOrbit.orbit.bpms) x = map(lambda b: b.x, self.suOrbit.orbit.bpms) y = map(lambda b: b.y, self.suOrbit.orbit.bpms) if len(self.s) == 0: self.s = s self.x = x self.y = y self.p1.set_data(self.s, self.x) self.p3.set_data(self.s, self.y) self.p2.set_data(s, x) self.p4.set_data(s, y) self.axes.grid(True) self.axes2.grid(True) self.points_with_annotation = [] max_x = max(self.x) for i, bpm in enumerate(self.suOrbit.orbit.bpms): point = self.points_x[i] point.set_data(self.s[i], self.x[i]) annotation = self.axes.annotate( bpm.id, xy=(self.s[i], self.x[i]), xycoords='data', xytext=(self.s[i], self.x[i] + max_x / 5), textcoords='data', horizontalalignment="upper", arrowprops=dict(arrowstyle="simple", connectionstyle="arc3,rad=-0.2"), bbox=dict(boxstyle="round", facecolor="w", edgecolor="0.5", alpha=0.9)) # by default, disable the annotation visibility annotation.set_visible(False) self.points_with_annotation.append([point, annotation]) max_y = max(self.y) for i, bpm in enumerate(self.suOrbit.orbit.bpms): point = self.points_y[i] point.set_data(self.s[i], self.y[i]) annotation = self.axes2.annotate( bpm.id, xy=(self.s[i], self.y[i]), xycoords='data', xytext=(self.s[i], self.y[i] + max_y / 5), textcoords='data', horizontalalignment="upper", arrowprops=dict(arrowstyle="simple", connectionstyle="arc3,rad=-0.2"), bbox=dict(boxstyle="round", facecolor="w", edgecolor="0.5", alpha=0.9)) # by default, disable the annotation visibility annotation.set_visible(False) self.points_with_annotation.append([point, annotation]) self.show_track() self.s = s self.x = x self.y = y self.axes.relim() self.axes.autoscale_view(True, True, True) self.axes2.relim() self.axes2.autoscale_view(True, True, True) self.canvas.draw() #self.s = copy.deepcopy(s) #self.x = copy.deepcopy(x) #self.y = copy.deepcopy(y) self.resp_button.setEnabled(True) def switch_correction(self): self.cor_button.setEnabled(True) def switch_off(self): self.cor_button.setEnabled(False) self.track = 0 self.s = [] self.x = [] self.y = [] def on_move(self, event): visibility_changed = False for point, annotation in self.points_with_annotation: should_be_visible = (point.contains(event)[0] == True) if should_be_visible != annotation.get_visible(): visibility_changed = True annotation.set_visible(should_be_visible) if visibility_changed: self.canvas.draw() """
class MPLWidget(qw.QWidget): labels_changed = QtCore.pyqtSignal(np.ndarray, np.ndarray) def __init__(self, parent, timestamps=None, n_states=10, state_min=0, width=5, height=8, dpi=100, static_data=None, overlay_data=None): super(MPLWidget, self).__init__() self.parent = parent self.timestamps = timestamps self.n_states = n_states self.state_min = state_min self.static_data = static_data self.overlay_data = overlay_data self.fig = Figure((width, height), dpi=dpi) self.ax = self.fig.add_subplot(111) self.canvas = FigureCanvasQTAgg(self.fig) self.canvas.setParent(self) self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus) self.canvas.setFocus() self.toolbar = NavigationToolbar(self.canvas, self) self.zoom_active = False for child in self.toolbar.children(): if isinstance(child, qw.QToolButton) and child.text() == 'Zoom': child.toggled.connect(self.zoom_button_toggled) self.init_figure() self.canvas.mpl_connect('button_press_event', self.button_pressed) self.canvas.mpl_connect('button_release_event', self.button_released) self.canvas.mpl_connect('motion_notify_event', self.mouse_moved) vbox = qw.QVBoxLayout() vbox.addWidget(self.toolbar) vbox.addWidget(self.canvas) self.setLayout(vbox) def init_figure(self): ax = self.ax x = self.timestamps y = np.zeros_like(x) self.lines = {} for i in range(self.n_states): self.lines[i] = ax.plot(x, y, 'ko-')[0] self.pos_line = ax.plot([0, 0], [0, self.n_states], '-', color=3 * [.5])[0] if self.overlay_data is not None and \ self.overlay_data.data is not None: ax.plot(self.overlay_data.timestamps, self.overlay_data.data, '-', color=3 * [.74], lw=.5) y_min = -1 if self.static_data is not None: if not isinstance(self.static_data, list): static_data = [self.static_data] else: static_data = self.static_data colors = ['r', 'b', 'o'] for i, dd in enumerate(static_data): x = dd.timestamps y = dd.data ax.plot(x, -2 - i * 2 + y / (4 * y.std()), '-', color=colors[i], alpha=.25) y_min -= 1.5 ax.set_xlim(x.min() - 1, x.max() + 1) ax.set_xlabel('Time (s)') ax.set_ylabel('Assigned state') ax.set_ylim(y_min, self.n_states) ax.set_yticks(np.arange(self.state_min, self.n_states)) ax.xaxis.set_major_locator(plt.MaxNLocator(5)) # Hide the right and top spines ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) # Only show ticks on the left and bottom spines ax.yaxis.set_ticks_position('left') ax.xaxis.set_ticks_position('bottom') ax.tick_params(axis='both', which='major', labelsize=8) ax.xaxis.label.set_fontsize(10) ax.xaxis.label.set_fontname('Arial') ax.yaxis.label.set_fontsize(10) ax.xaxis.label.set_fontname('Arial') for at in ax.texts: at.set_fontsize(10) at.set_fontname('Arial') self.fig.canvas.draw() def plot_labels(self, y, pos=-1): x = self.timestamps for i in range(self.n_states): yi = np.ma.masked_where(y != i, y) self.lines[i].set_data(x, yi) self.ax.draw_artist(self.lines[i]) if pos >= 0: self.pos_line.set_data(2 * [x[pos]], [0, self.n_states]) self.ax.draw_artist(self.pos_line) self.fig.canvas.draw() def update_plot(self): for i in range(self.n_states): self.ax.draw_artist(self.lines[i]) self.ax.draw_artist(self.pos_line) self.fig.canvas.draw() def update_figure(self): self.fig.canvas.draw() def button_pressed(self, event): self.x_start = event.xdata self.y_start = event.ydata def zoom_button_toggled(self, event): self.zoom_active = event def button_released(self, event): if not self.zoom_active: x1 = self.x_start y1 = self.y_start x2 = event.xdata y2 = event.ydata if x1 is not None and x2 is not None and x1 != x2: if x1 > x2: x1, x2 = x2, x1 y1, y2 = y2, y1 slope = (y2 - y1) / (x2 - x1) intercept = y1 - slope * x1 ts = self.timestamps ind = np.where(np.logical_and(ts >= x1, ts <= x2))[0] yy = ts[ind] * slope + intercept labels = np.asarray(np.round(yy), np.int) self.labels_changed.emit(ind, labels) def mouse_moved(self, event): pass
class BinaryWidget(QtGui.QWidget): """ The binary-widget window. @param parent: Parent window """ def __init__(self, parent=None, **kwargs): super(BinaryWidget, self).__init__(parent, **kwargs) self.initBin() self.initBinLayout() self.showVisibleWidgets() self.psr = None self.parent = parent def initBin(self): self.setMinimumSize(650, 550) self.binarybox = QtGui.QVBoxLayout() # whole widget self.modelbox = QtGui.QHBoxLayout() # model widget self.modelWidget = QtGui.QWidget() # model widget #self.actionsWidget = BinActionsWidget(parent=self) self.binaryModelCB = QtGui.QComboBox() self.binaryModelCB.addItem('DD') self.binaryModelCB.addItem('T2') self.binaryModelCB.addItem('ELL') # We are creating the Figure here, so set the color scheme appropriately self.setColorScheme(True) # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.binDpi = 100 self.binFig = Figure((5.0, 4.0), dpi=self.binDpi) self.binCanvas = FigureCanvas(self.binFig) self.binCanvas.setParent(self) # Since we have only one plot, we can use add_axes # instead of add_subplot, but then the subplot # configuration tool in the navigation toolbar wouldn't # work. # self.binAxes = self.binFig.add_subplot(111) # Done creating the Figure. Restore color scheme to defaults self.setColorScheme(False) # Call-back functions for clicking and key-press. self.binCanvas.mpl_connect('button_press_event', self.canvasClickEvent) self.binCanvas.mpl_connect('key_press_event', self.canvasKeyEvent) # Create the navigation toolbar, tied to the canvas # #self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # Draw an empty graph self.drawSomething() # Create the XY choice widget #self.xyChoiceWidget = BinXYPlotWidget(parent=self) # At startup, all the widgets are visible self.xyChoiceVisible = True self.fitboxVisible = True self.actionsVisible = True self.layoutMode = 1 # (0 = none, 1 = all, 2 = only fitboxes, 3 = fit & action) def initBinLayout(self): """ Initialise the basic layout of this plk emulator emulator """ # Initialise the plk box #self.plkbox.addWidget(self.fitboxesWidget) self.modelbox.addWidget(self.binaryModelCB) self.modelbox.addStretch(1) self.modelWidget.setLayout(self.modelbox) self.binarybox.addWidget(self.modelWidget) #self.xyplotbox.addWidget(self.xyChoiceWidget) self.binarybox.addWidget(self.binCanvas) #self.plkbox.addLayout(self.xyplotbox) #self.plkbox.addWidget(self.actionsWidget) self.setLayout(self.binarybox) def setColorScheme(self, start=True): """ Set the color scheme @param start: When true, save the original scheme, and set to white When False, restore the original scheme """ # Obtain the Widget background color color = self.palette().color(QtGui.QPalette.Window) r, g, b = color.red(), color.green(), color.blue() rgbcolor = (r / 255.0, g / 255.0, b / 255.0) if start: # Copy of 'white', because of bug in matplotlib that does not allow # deep copies of rcParams. Store values of matplotlib.rcParams self.orig_rcParams = copy.deepcopy(constants.mpl_rcParams_white) for key, value in self.orig_rcParams.iteritems(): self.orig_rcParams[key] = matplotlib.rcParams[key] rcP = copy.deepcopy(constants.mpl_rcParams_white) rcP['axes.facecolor'] = rgbcolor rcP['figure.facecolor'] = rgbcolor rcP['figure.edgecolor'] = rgbcolor rcP['savefig.facecolor'] = rgbcolor rcP['savefig.edgecolor'] = rgbcolor for key, value in rcP.iteritems(): matplotlib.rcParams[key] = value else: for key, value in constants.mpl_rcParams_black.iteritems(): matplotlib.rcParams[key] = value def drawSomething(self): """ When we don't have a pulsar yet, but we have to display something, just draw an empty figure """ self.setColorScheme(True) #self.binAxes.clear() #self.binAxes.grid(True) #self.binAxes.set_xlabel('MJD') #self.binAxes.set_ylabel('Residual ($\mu$s)') #self.binCanvas.draw() self.setColorScheme(False) def setPulsar(self, psr): """ We've got a new pulsar! """ self.psr = psr # Update the fitting checkboxes #self.fitboxesWidget.setPulsar(psr) #self.xyChoiceWidget.setPulsar(psr, self.updatePlot) #self.actionsWidget.setPulsar(psr, self.updatePlot, self.reFit) # Draw the residuals #self.xyChoiceWidget.updateChoice() # This screws up the show/hide logistics #self.show() def reFit(self): """ We need to re-do the fit for this pulsar """ if not self.psr is None: self.psr.fit() self.updatePlot() def newFitParameters(self): """ This function is called when we have new fitparameters TODO: callback not used right now """ pass def showVisibleWidgets(self): """ Show the correct widgets in the plk Window """ #self.xyChoiceWidget.setVisible(self.xyChoiceVisible) #self.fitboxesWidget.setVisible(self.fitboxVisible) #self.actionsWidget.setVisible(self.actionsVisible) pass def updatePlot(self): """ Update the plot/figure """ if self.psr is not None: # Get a mask for the plotting points msk = self.psr.mask('plot') #print("Mask has {0} toas".format(np.sum(msk))) # Get the IDs of the X and Y axis #xid, yid = self.xyChoiceWidget.plotids() xid, yid = 'MJD', 'post-fit' # Retrieve the data x, xerr, xlabel = self.psr.data_from_label(xid) y, yerr, ylabel = self.psr.data_from_label(yid) if x is not None and y is not None and np.sum(msk) > 0: xp = x[msk] yp = y[msk] if yerr is not None: yerrp = yerr[msk] else: yerrp = None self.updatePlotL(xp, yp, yerrp, xlabel, ylabel, self.psr.name) else: raise ValueError("Nothing to plot!") def updatePlotL(self, x, y, yerr, xlabel, ylabel, title): """ Update the plot, given all the plotting info """ self.setColorScheme(True) self.binAxes.clear() self.binAxes.grid(True) xave = 0.5 * (np.max(x) + np.min(x)) xmin = xave - 1.05 * (xave - np.min(x)) xmax = xave + 1.05 * (np.max(x) - xave) if yerr is None: yave = 0.5 * (np.max(y) + np.min(y)) ymin = yave - 1.05 * (yave - np.min(y)) ymax = yave + 1.05 * (np.max(y) - yave) self.binAxes.scatter(x, y, marker='.', c='g') else: yave = 0.5 * (np.max(y + yerr) + np.min(y - yerr)) ymin = yave - 1.05 * (yave - np.min(y - yerr)) ymax = yave + 1.05 * (np.max(y + yerr) - yave) self.binAxes.errorbar(x, y, yerr=yerr, fmt='.', color='green') self.binAxes.axis([xmin, xmax, ymin, ymax]) self.binAxes.get_xaxis().get_major_formatter().set_useOffset(False) self.binAxes.set_xlabel(xlabel) self.binAxes.set_ylabel(ylabel) self.binAxes.set_title(title) self.binCanvas.draw() self.setColorScheme(False) def setFocusToCanvas(self): """ Set the focus to the plk Canvas """ self.binCanvas.setFocus() def coord2point(self, cx, cy): """ Given data coordinates x and y, obtain the index of the observations that is closest to it @param cx: x-value of the coordinates @param cy: y-value of the coordinates @return: Index of observation """ ind = None if self.psr is not None: # Get a mask for the plotting points msk = self.psr.mask('plot') # Get the IDs of the X and Y axis #xid, yid = self.xyChoiceWidget.plotids() xid, yid = 'MJD', 'post-fit' # Retrieve the data x, xerr, xlabel = self.psr.data_from_label(xid) y, yerr, ylabel = self.psr.data_from_label(yid) if np.sum(msk) > 0 and x is not None and y is not None: # Obtain the limits xmin, xmax, ymin, ymax = self.binAxes.axis() dist = ((x[msk] - cx) / (xmax - xmin))**2 + ((y[msk] - cy) / (ymax - ymin))**2 ind = np.arange(len(x))[msk][np.argmin(dist)] return ind def keyPressEvent(self, event, **kwargs): """ A key is pressed. Handle all the shortcuts here. This function can be called as a callback from the Canvas, or as a callback from Qt. So first some parsing must be done """ if hasattr(event.key, '__call__'): ukey = event.key() modifiers = int(event.modifiers()) from_canvas = False print( "WARNING: call-back key-press, canvas location not available") xpos, ypos = None, None else: # Modifiers are noted as: key = 'ctrl+alt+F', or 'alt+control', or # 'shift+g'. Do some parsing fkey = event.key from_canvas = True xpos, ypos = event.xdata, event.ydata ukey = ord(fkey[-1]) modifiers = QtCore.Qt.NoModifier if 'ctrl' in fkey: modifiers += QtCore.Qt.ControlModifier if 'shift' in fkey: modifiers += QtCore.Qt.ShiftModifier if 'alt' in fkey: modifiers += QtCore.Qt.ShiftModifier if 'meta' in fkey: modifiers += QtCore.Qt.MetaModifier #if int(e.modifiers()) == (QtCore.Qt.ControlModifier+QtCore.Qt.AltModifier) if ukey == QtCore.Qt.Key_Escape: if self.parent is None: self.close() else: self.parent.close() elif (ukey == ord('M') or ukey == ord('m')) and \ modifiers == QtCore.Qt.ControlModifier: # Change the window self.layoutMode = (1 + self.layoutMode) % 4 if self.layoutMode == 0: self.xyChoiceVisible = False self.fitboxVisible = False self.actionsVisible = False elif self.layoutMode == 1: self.xyChoiceVisible = True self.fitboxVisible = True self.actionsVisible = True elif self.layoutMode == 2: self.xyChoiceVisible = False self.fitboxVisible = True self.actionsVisible = True elif self.layoutMode == 3: self.xyChoiceVisible = False self.fitboxVisible = True self.actionsVisible = False self.showVisibleWidgets() elif ukey == ord('s'): # Set START flag at xpos # TODO: propagate back to the IPython shell self.psr['START'].set = True self.psr['START'].fit = True self.psr['START'].val = xpos self.updatePlot() elif ukey == ord('f'): # Set FINISH flag as xpos # TODO: propagate back to the IPython shell self.psr['FINISH'].set = True self.psr['FINISH'].fit = True self.psr['FINISH'].val = xpos self.updatePlot() elif ukey == ord('u'): # Unzoom # TODO: propagate back to the IPython shell self.psr['START'].set = True self.psr['START'].fit = False self.psr['START'].val = np.min(self.psr.toas) self.psr['FINISH'].set = True self.psr['FINISH'].fit = False self.psr['FINISH'].val = np.max(self.psr.toas) self.updatePlot() elif ukey == ord('d'): # Delete data point # TODO: propagate back to the IPython shell # TODO: Fix libstempo! ind = self.coord2point(xpos, ypos) #print("Deleted:", self.psr._psr.deleted) # TODO: fix this hack properly in libstempo tempdel = self.psr.deleted tempdel[ind] = True self.psr.deleted = tempdel self.updatePlot() #print("Index deleted = ", ind) #print("Deleted:", self.psr.deleted[ind]) elif ukey == ord('x'): # Re-do the fit, using post-fit values of the parameters self.reFit() elif ukey == QtCore.Qt.Key_Left: # print("Left pressed") pass else: #print("Other key: {0} {1} {2} {3}".format(ukey, # modifiers, ord('M'), QtCore.Qt.ControlModifier)) pass #print("BinaryWidget: key press: ", ukey, xpos, ypos) if not from_canvas: if self.parent is not None: print("Propagating key press") self.parent.keyPressEvent(event) super(BinaryWidget, self).keyPressEvent(event, **kwargs) def canvasClickEvent(self, event): """ When one clicks on the Figure/Canvas, this function is called. The coordinates of the click are stored in event.xdata, event.ydata """ #print('Canvas click, you pressed', event.button, event.xdata, event.ydata) pass def canvasKeyEvent(self, event): """ When one presses a button on the Figure/Canvas, this function is called. The coordinates of the click are stored in event.xdata, event.ydata """ # Callback to the binaryWidget self.keyPressEvent(event)
class Diagnosis(QtGui.QDialog): def __init__(self, mainWindow): super(Diagnosis, self).__init__() #self.program_path = os.path.dirname(sys.argv[0]) self.program_path = os.getcwd() # GUI self.setWindowIcon(QIcon(self.program_path +'/logo/refcurv_logo.png')) # figure self.figure = Figure() self.canvas = FigureCanvas(self.figure) self.canvas.mpl_connect('pick_event', self.onpick) self.ax1 = self.figure.add_subplot(221) self.ax2 = self.figure.add_subplot(222) self.ax3 = self.figure.add_subplot(223) self.ax4 = self.figure.add_subplot(224) self.canvas.draw() self.nav = NavigationToolbar(self.canvas,self.canvas, coordinates=False) mainLayout = QtGui.QVBoxLayout() mainLayout.addWidget(self.canvas) self.setLayout(mainLayout) self.lms_diagnosis() def BCCG(self, params,x): M = params[0] S = params[1] L = params[2] Phi = 0.5*(1 + erf((1/(S*np.abs(L)))/(np.sqrt(2)))) if L == 0: z = (1/S)*np.log(x/M) else: z = (1/(S*L))*(((x/M)**L)-1) f = (x**(L-1)*np.exp(-0.5*z**2))/((M**L)*S*Phi*np.sqrt(2*np.pi)) return f def LL(self, params, x): #if (params[0]>0 and params[1]>0): try: prob = 0 prob_i = self.BCCG(params, x) prob = np.sum(np.log(prob_i)) return -prob except: return np.inf def lms_diagnosis(self): self.LMS_diagnosis_window = QtGui.QDialog() self.figure_lms = Figure() self.canvas_lms = FigureCanvas(self.figure_lms) self.ax1_lms = self.figure_lms.add_subplot(221) self.ax2_lms = self.figure_lms.add_subplot(222) self.ax3_lms = self.figure_lms.add_subplot(223) self.ax4_lms = self.figure_lms.add_subplot(224) self.canvas_lms.draw() mainLayout_lms = QtGui.QVBoxLayout() mainLayout_lms.addWidget(self.canvas_lms) self.LMS_diagnosis_window.setLayout(mainLayout_lms) def update_lms_diagnosis(self): self.ax1_lms.clear() self.ax2_lms.clear() self.ax3_lms.clear() variation_vector = np.arange(0.5, 1.5, 0.01) cur_L = self.cur_data["nu"].values[self.index_data] cur_M = self.cur_data["mu"].values[self.index_data] cur_S = self.cur_data["sigma"].values[self.index_data] cur_x = self.cur_data.iloc[:,1].values[self.index_data] L_variation = variation_vector * cur_L M_variation = variation_vector * cur_M S_variation = variation_vector * cur_S likelihood_matrix = np.zeros((len(variation_vector), len(variation_vector))) print(cur_x) ######### fix L for l in [cur_L]: for i in range(0,len(M_variation)): for j in range(0,len(S_variation)): parameter = [M_variation[i], S_variation[j], l[0]] try: likelihood_matrix[i,j] = self.LL(parameter, cur_x) except: likelihood_matrix[i,j] = np.inf m_variation, s_variation = np.meshgrid(M_variation, S_variation) print(likelihood_matrix) test = self.ax1_lms.contourf(m_variation, s_variation, likelihood_matrix) self.ax1_lms.set_title("L = " + str(l)) self.ax1_lms.set_ylabel("S") self.ax1_lms.set_xlabel("M") self.ax1_lms.plot(cur_M, cur_S,"ro") cbar = self.figure_lms.colorbar(test) #self.colorbar = self.figure_lms.colorbar(self.ax1_lms, ax=self.ax1_lms) ########### fix S for s in [cur_S]: for i in range(0,len(M_variation)): for j in range(0,len(L_variation)): parameter = [M_variation[i], s[0], L_variation[j]] try: likelihood_matrix[i,j] = self.LL(parameter, cur_x) except: likelihood_matrix[i,j] = np.inf m_variation, l_variation = np.meshgrid(M_variation, L_variation) self.ax2_lms.contourf(m_variation, l_variation, likelihood_matrix) self.ax2_lms.set_title("S = " + str(s)) self.ax2_lms.set_ylabel("L") self.ax2_lms.set_xlabel("M") self.ax2_lms.plot(cur_M, cur_L,"ro") ########### fix M for m in [cur_M]: for i in range(0,len(S_variation)): for j in range(0,len(L_variation)): parameter = [m[0], S_variation[i], L_variation[j]] try: likelihood_matrix[i,j] = self.LL(parameter, cur_x) except: likelihood_matrix[i,j] = np.inf s_variation, l_variation = np.meshgrid(S_variation, L_variation) self.ax3_lms.contourf(s_variation, l_variation, likelihood_matrix) self.ax3_lms.set_title("M = " + str(m)) self.ax3_lms.set_ylabel("L") self.ax3_lms.set_xlabel("S") self.ax3_lms.plot(cur_S, cur_L,"ro") self.canvas_lms.draw() self.LMS_diagnosis_window.show() def onpick(self, event): try: thisdata = event.artist #xdata = thisdata.get_xdata() #ydata = thisdata.get_ydata() self.index_data = event.ind print(self.index_data) print(self.cur_data.iloc[:,0].values[self.index_data], self.cur_data.iloc[:,1].values[self.index_data]) self.update_lms_diagnosis() except: print("error picking") #print('index: %d\nobjective 1: %0.2f\nobjective 2: %0.2f\nobjective' % (event.ind[0], self.cur_data[ind,0], self.cur_data[ind,1])) #print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % # ('double' if event.dblclick else 'single', event.button, # event.x, event.y, event.xdata, event.ydata)) #self.ax4.plot(event.xdata, event.ydata, color="r", marker="o", markersize = 5) #self.canvas.draw() def plot_residuals(self): try: self.cur_data = pd.read_csv(self.program_path + "/tmp/cur_data.csv",sep =',', encoding = "ISO-8859-1") lms_datapoint_chart = pd.read_csv(self.program_path + "/tmp/lms_datapoint_chart.csv",sep =',', encoding = "ISO-8859-1") res = pd.read_csv(self.program_path + "/tmp/res_chart.csv",sep =',', encoding = "ISO-8859-1") self.cur_data = self.cur_data.join(lms_datapoint_chart) self.cur_data = self.cur_data.join(res["resid_m1"]) #print(cur_data) residuals = np.sort(res["resid_m1"].values) residuals_index = np.arange(len(residuals)) residuals_theoretical = st.norm.ppf(residuals_index/len(residuals)) # 1. plot self.ax1.plot(residuals_theoretical, residuals, '.', picker = 5) self.ax1.set_title("Normal Q-Q plot") self.ax1.set_ylabel("Sample quantiles") self.ax1.set_xlabel("Theoretical quantiles") # 2. plot self.ax2.plot(residuals_theoretical, residuals-residuals_theoretical, '.', picker = 5) self.ax2.set_title("Worm plot") self.ax2.set_ylabel("Deviation") self.ax2.set_xlabel("Unit normal quantile") # 3. plot num_bins = 20 self.ax3.hist(residuals, num_bins, normed=1, picker = 5) self.ax3.set_title("Density estimate") self.ax3.set_ylabel("Density") self.ax3.set_xlabel("Quantile residuals") k2, p = st.normaltest(residuals) alpha = 1e-3 print("p = {:g}".format(p)) # 4. plot self.ax4.plot(self.cur_data.iloc[:,0].values, residuals,'.', picker = 5) self.ax4.set_title("Against fitted values") self.ax4.set_ylabel("Quantile residuals") self.ax4.set_xlabel("Fitted values") self.canvas.draw() except: print("error")
class MiniMap(QtGui.QWidget): """Shows the entire signal and allows the user to navigate through it. Provides an scrollable selector over the entire signal. Attributes: xmin: Selector lower limit (measured in h-axis units). xmax: Selector upper limit (measured in h-axis units). step: Selector length (measured in h-axis units). """ def __init__(self, parent, ax, record=None): super(MiniMap, self).__init__(parent) self.ax = ax self.xmin = 0.0 self.xmax = 0.0 self.step = 10.0 self.xrange = np.array([]) self.minimapFig = plt.figure() self.minimapFig.set_figheight(0.75) self.minimapFig.add_axes((0, 0, 1, 1)) self.minimapCanvas = FigureCanvas(self.minimapFig) self.minimapCanvas.setFixedHeight(64) self.minimapSelector = self.minimapFig.axes[0].axvspan(0, self.step, color='gray', alpha=0.5, animated=True) self.minimapSelection = self.minimapFig.axes[0].axvspan( 0, self.step, color='LightCoral', alpha=0.5, animated=True) self.minimapSelection.set_visible(False) self.minimapBackground = [] self.minimapSize = (self.minimapFig.bbox.width, self.minimapFig.bbox.height) self.press_selector = None self.playback_marker = None self.minimapCanvas.mpl_connect('button_press_event', self.onpress) self.minimapCanvas.mpl_connect('button_release_event', self.onrelease) self.minimapCanvas.mpl_connect('motion_notify_event', self.onmove) # Animation related attrs. self.background = None self.animated = False # Set the layout self.layout = QtGui.QVBoxLayout(self) self.layout.addWidget(self.minimapCanvas) # Animation related attributes self.parentViewer = parent # Set Markers dict self.markers = {} self.record = None if record is not None: self.set_record(record) def set_record(self, record, step): self.record = record self.step = step self.xrange = np.linspace(0, len(self.record.signal) / self.record.fs, num=len(self.record.signal), endpoint=False) self.xmin = self.xrange[0] self.xmax = self.xrange[-1] self.markers = {} ax = self.minimapFig.axes[0] ax.lines = [] formatter = FuncFormatter( lambda x, pos: str(datetime.timedelta(seconds=x))) ax.xaxis.set_major_formatter(formatter) ax.grid(True, which='both') # Set dataseries to plot xmin = self.xmin * self.record.fs xmax = self.xmax * self.record.fs pixel_width = np.ceil(self.minimapFig.get_figwidth() * self.minimapFig.get_dpi()) x_data, y_data = plotting.reduce_data(self.xrange, self.record.signal, pixel_width, xmin, xmax) # self._plot_data.set_xdata(x_data) # self._plot_data.set_ydata(y_data) ax.plot(x_data, y_data, color='black', rasterized=True) ax.set_xlim(self.xmin, self.xmax) plotting.adjust_axes_height(ax) # Set the playback marker self.playback_marker = PlayBackMarker(self.minimapFig, self) self.playback_marker.markers[0].set_animated(True) # Draw canvas self.minimapCanvas.draw() self.minimapBackground = self.minimapCanvas.copy_from_bbox( self.minimapFig.bbox) self.draw_animate() def onpress(self, event): self.press_selector = event xdata = round(self.get_xdata(event), 2) xmin = round(xdata - (self.step / 2.0), 2) xmax = round(xdata + (self.step / 2.0), 2) self.parentViewer._set_animated(True) self.set_selector_limits(xmin, xmax) def onrelease(self, event): self.press_selector = None # Finish parent animation self.parentViewer._set_animated(False) def onmove(self, event): if self.press_selector is not None: xdata = round(self.get_xdata(event), 2) xmin = round(xdata - (self.step / 2.0), 2) xmax = round(xdata + (self.step / 2.0), 2) self.set_selector_limits(xmin, xmax) def get_xdata(self, event): inv = self.minimapFig.axes[0].transData.inverted() xdata, _ = inv.transform((event.x, event.y)) return xdata def set_selector_limits(self, xmin, xmax): step = xmax - xmin if step >= self.xmax - self.xmin: xleft = self.xmin xright = self.xmax if xmin < self.xmin: xleft = self.xmin xright = self.step elif xmax > self.xmax: xleft = self.xmax - step xright = self.xmax else: xleft = xmin xright = xmax if (xleft, xright) != (self.minimapSelector.xy[1, 0], self.minimapSelector.xy[2, 0]): self.step = step self.minimapSelector.xy[:2, 0] = xleft self.minimapSelector.xy[2:4, 0] = xright self.ax.set_xlim(xleft, xright) self.draw_animate() else: self.parentViewer.draw() def get_selector_limits(self): return self.minimapSelector.xy[0, 0], self.minimapSelector.xy[2, 0] def draw(self): self.draw_animate() def draw_animate(self): size = self.minimapFig.bbox.width, self.minimapFig.bbox.height if size != self.minimapSize: self.minimapSize = size self.minimapCanvas.draw() self.minimapBackground = self.minimapCanvas.copy_from_bbox( self.minimapFig.bbox) self.minimapCanvas.restore_region(self.minimapBackground) self.minimapFig.draw_artist(self.minimapSelection) self.minimapFig.draw_artist(self.minimapSelector) self.minimapFig.draw_artist(self.playback_marker.markers[0]) for marker in self.markers.values(): self.minimapFig.draw_artist(marker) self.minimapCanvas.blit(self.minimapFig.bbox) def set_visible(self, value): self.minimapCanvas.setVisible(value) def get_visible(self): return self.minimapCanvas.isVisible() def set_selection_limits(self, xleft, xright): self.minimapSelection.xy[:2, 0] = xleft self.minimapSelection.xy[2:4, 0] = xright self.draw_animate() def set_selection_visible(self, value): self.minimapSelection.set_visible(value) self.draw_animate() def create_marker(self, key, position, **kwargs): if self.xmin <= position <= self.xmax: marker = self.minimapFig.axes[0].axvline(position, animated=True) self.markers[key] = marker self.markers[key].set(**kwargs) def set_marker_position(self, key, value): marker = self.markers.get(key) if marker is not None: if self.xmin <= value <= self.xmax: marker.set_xdata(value) def set_marker(self, key, **kwargs): marker = self.markers.get(key) if marker is not None: kwargs.pop( "animated", None ) # marker's animated property must be always true to be drawn properly marker.set(**kwargs) def delete_marker(self, key): marker = self.markers.get(key) if marker is not None: self.minimapFig.axes[0].lines.remove(marker) self.markers.pop(key)
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Demo: PyQt with matplotlib') self.create_main_frame() self.on_draw() def on_pick(self, event): # The event received here is of the type # matplotlib.backend_bases.PickEvent # # It carries lots of information, of which we're using # only a small amount here. # box_points = event.artist.get_bbox().get_points() msg = "You've clicked on a bar with coords:\n %s" % box_points QMessageBox.information(self, "Click!", msg) def on_draw(self): """ Redraws the figure """ self.data = [random.randint(1, 10) for i in range(10)] x = range(len(self.data)) # clear the axes and redraw the plot anew # self.axes.clear() self.axes.grid(self.grid_cb.isChecked()) self.axes.bar(left=x, height=self.data, width=self.slider.value() / 100.0, align='center', alpha=0.44, picker=5) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.dpi = 100 self.fig = Figure((5.0, 4.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) # Since we have only one plot, we can use add_axes # instead of add_subplot, but then the subplot # configuration tool in the navigation toolbar wouldn't # work. # self.axes = self.fig.add_subplot(111) # Bind the 'pick' event for clicking on one of the bars # self.canvas.mpl_connect('pick_event', self.on_pick) # Create the navigation toolbar, tied to the canvas # self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # Other GUI controls # self.draw_button = QPushButton("&Draw") self.connect(self.draw_button, SIGNAL('clicked()'), self.on_draw) self.grid_cb = QCheckBox("Show &Grid") self.grid_cb.setChecked(False) self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.on_draw) slider_label = QLabel('Slider value (%):') self.slider = QSlider(Qt.Horizontal) self.slider.setRange(1, 100) self.slider.setValue(20) self.slider.setTracking(True) self.slider.setTickPosition(QSlider.TicksBothSides) self.connect(self.slider, SIGNAL('valueChanged(int)'), self.on_draw) # # Layout with box sizers # hbox = QHBoxLayout() for w in [self.draw_button, self.grid_cb, slider_label, self.slider]: hbox.addWidget(w) hbox.setAlignment(w, Qt.AlignVCenter) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame)
class AppForm(QMainWindow): def __init__(self): QMainWindow.__init__(self, None) self.setWindowTitle('LaserScan Labeler') self.points = None self.dragging = None self.play_timer = None self.circles = CircleRegionManager() self.path = None # Save file path self.data = None self.create_menu() self.create_main_frame() self.setChildrenFocusPolicy(Qt.NoFocus) self.on_draw() def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") save_file_action = self.create_action("&Save", shortcut="Ctrl+S", slot=self.save, tip="Save a label file") save_as_action = self.create_action( "&Save As...", shortcut="Ctrl+Shift+S", slot=self.save, tip="Save a label file to another path") load_file_action = self.create_action("&Open...", shortcut="Ctrl+O", slot=self.open, tip="Open a label file") export_action = self.create_action("&Export", shortcut="Ctrl+E", slot=self.export, tip="Export labeled data") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions( self.file_menu, (load_file_action, None, save_file_action, save_as_action, None, export_action, None, quit_action)) def save(self): print "Save!" if self.path is None: self.save_as() else: self.save_file(self.path) def save_as(self): print "Save as!" file_choices = "LSL (*.lsl)|*.lsl" path = unicode( QFileDialog.getSaveFileName(self, 'Save file', '', file_choices)) if not path.endswith(".lsl"): path = path + ".lsl" self.path = path self.save_file(path) print path def save_file(self, path): with gzip.open(path, 'wb') as f: pickle.dump([path, self.data, self.circles], f) def open(self): print "Open!" file_choices = "LSL or BAG (*.lsl *.bag);; LSL (*.lsl);; BAG (*.bag)" path = unicode( QFileDialog.getOpenFileName(self, 'Open bag or lsl file', '', file_choices)) if path.endswith(".lsl"): self.path = path with gzip.open(path, 'rb') as f: self.circles.cleanup() path, self.data, self.circles = pickle.load(f) else: self.data = BagLoader(path, None) self.circles.cleanup() self.circles = CircleRegionManager() # Set the UI elements that depend on data or need resetting self.spinbox.setValue(0) self.spinbox.setMaximum(len(self.data.data) - 1) self.ax_p.set_rmax(self.data.range_max) self.ax_p.set_rticks(np.arange(0, self.data.range_max + 1, 1.0)) # less radial ticks # Open window self.show() # Re-render everything self.on_draw() def export(self): """ Export labeled data as a mat file """ print "export start" # Get the save path file_choices = "mat (*.mat)" path = unicode( QFileDialog.getSaveFileName(self, 'Export mat', '', file_choices)) if not path.endswith(".mat"): path = path + ".mat" # Get all data into a dict data = {'range_max': self.data.range_max, 'theta': self.data.theta} data['scans'] = self.data.data data['classes'] = [] for i in range(len(self.data.data)): data['classes'].append(self.circles.get_classes(self.data, i)) # Save data dict sio.savemat(path, data) print "export done" def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action def press(self, event): print event, event.inaxes print "press", event.xdata, event.ydata, event self.dragging = self.circles.get_patch_index(event.xdata, event.ydata) if self.dragging is not None: if event.button == 3: self.circles.delete(self.dragging) self.dragging = None elif event.button == 2: self.circles.current[self.dragging].end = None self.dragging = None else: index = self.spinbox.value() self.circles.current[self.dragging].move( event.xdata, event.ydata, index) else: # Create new region! self.circles.create(event.xdata, event.ydata) self.on_draw() def scroll(self, event): delta = 0.1 / self.data.range_max if event.button == "down": delta = -delta # invert print delta target = self.circles.get_patch_index(event.xdata, event.ydata) if target is not None: index = self.spinbox.value() self.circles.current[target].resize(delta, index) self.on_draw() def motion(self, event): if self.dragging is not None: index = self.spinbox.value() self.circles.current[self.dragging].move(event.xdata, event.ydata, index) self.on_draw() def release(self, event): print "release", event.xdata, event.ydata self.dragging = None def setChildrenFocusPolicy(self, policy): def recursiveSetChildFocusPolicy(parentQWidget): for childQWidget in parentQWidget.findChildren(QWidget): childQWidget.setFocusPolicy(policy) recursiveSetChildFocusPolicy(childQWidget) recursiveSetChildFocusPolicy(self) def on_draw(self): """ Redraws the figure """ if self.data is None: # Don't get ahead of ourselves return index = self.spinbox.value() self.circles.set_index(index) # Filter out max range points of "no return" data_filtered = [ r if r < self.data.range_max else None for r in self.data.data[index] ] colors = self.circles.get_colors(self.data) idx = np.array(self.data.data[index]) < self.data.range_max self.lines.set_data(self.data.theta, data_filtered) if self.points is not None: self.points.remove() self.points = self.ax_p.scatter(self.data.theta[idx], self.data.data[index][idx], 3, colors[idx], zorder=5) self.circles.render(self.ax_c) self.canvas.draw() def prev(self, event): mods = QApplication.keyboardModifiers() if bool(mods & Qt.ShiftModifier): self.spinbox.setValue(self.spinbox.value() - 10) else: self.spinbox.setValue(self.spinbox.value() - 1) def play(self, event): if self.play_timer is None: self.play_timer = QTimer() self.play_timer.timeout.connect(self.next) self.play_timer.start(100) else: if self.play_timer.isActive(): self.play_timer.stop() else: self.play_timer.start() if self.play_timer.isActive(): self.play_button.setText(u"⏸") else: self.play_button.setText(u"▶") print "Play" def keyPressEvent(self, event): if event.key() == Qt.Key_Left: self.prev(event) elif event.key() == Qt.Key_Right: self.next(event) elif event.key() == Qt.Key_Space: self.play(event) def next(self, *args): mods = QApplication.keyboardModifiers() if bool(mods & Qt.ShiftModifier): self.spinbox.setValue(self.spinbox.value() + 10) else: self.spinbox.setValue(self.spinbox.value() + 1) def valueChanged(self, value): self.on_draw() print value def create_main_frame(self): self.main_frame = QWidget() # Create the figure self.fig = Figure() self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) # Create axes # the polar axis: rect = [0, 0, 1, 1] self.ax_p = self.fig.add_axes(rect, polar=True, frameon=False, aspect=1) self.ax_c = self.fig.add_axes(rect, aspect=1, frameon=False) # Set up the cartesian plot self.ax_c.get_xaxis().set_visible(False) self.ax_c.get_yaxis().set_visible(False) # Set up the polar polot self.ax_p.set_rlabel_position( 0) # get radial labels away from plotted line self.ax_p.grid(True) self.ax_p.autoscale(False) # Patch self.circles.render(self.ax_c) # Render initial values self.lines, = self.ax_p.plot([0], [0], 'r-') # Bind the 'pick' event for clicking on one of the bars # # self.canvas.mpl_connect('pick_event', self.on_pick) self.canvas.mpl_connect('button_press_event', self.press) self.canvas.mpl_connect('motion_notify_event', self.motion) self.canvas.mpl_connect('button_release_event', self.release) self.canvas.mpl_connect('scroll_event', self.scroll) # GUI controls # self.prev_button = QPushButton(u"🡰") self.prev_button.clicked.connect(self.prev) self.play_button = QPushButton(u"▶") self.play_button.clicked.connect(self.play) self.next_button = QPushButton(u"🡲") self.next_button.clicked.connect(self.next) spinbox_label = QLabel('Scan #') self.spinbox = QSpinBox() self.spinbox.setRange(0, 0) self.spinbox.setValue(0) self.spinbox.valueChanged.connect(self.valueChanged) self.spinbox.setFocusPolicy(Qt.NoFocus) # # Button layout # hbox = QHBoxLayout() for w in [ self.prev_button, self.play_button, self.next_button, spinbox_label, self.spinbox ]: hbox.addWidget(w) hbox.setAlignment(w, Qt.AlignVCenter) vbox = QVBoxLayout() vbox.addWidget(self.canvas) # vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame)
class RoachSweepWindow(QMainWindow): sweepClicked = QtCore.pyqtSignal() #fitClicked = QtCore.pyqtSignal() rotateClicked = QtCore.pyqtSignal() translateClicked = QtCore.pyqtSignal() adcAttenChanged = QtCore.pyqtSignal() dacAttenChanged = QtCore.pyqtSignal() resetRoach = QtCore.pyqtSignal(int) def __init__(self,roach,config,parent=None): """ Window for showing IQ plot of resonators INPUTS: roach - A RoachStateMachine Object. We need this to access all the relevent settings config - ConfigParser object parent - Leave as default """ QMainWindow.__init__(self,parent=parent) self._want_to_close = False self.roach=roach self.roachNum = self.roach.num self.config = config self.setWindowTitle('r'+str(self.roachNum)+': IQ Plot') self.channelsModified=set() # Remember which channels we've already modified but haven't reloaded into roach self.dataList = [] # Save data from sweeps and translates in memory self.rotatedList = [] # Save data from rotate corresponding to data in dataList self.numData2Show = 4 # number of previous sweeps to show self.maxDataListLength = 10 # maximum number of sweeps to save in memory self.create_main_frame() #self.counter = 0 def plotData(self,data=None,rotated=False,**kwargs): #print 'Plotting Data: ',data if data is not None and not rotated: self.appendData(data.copy()) if rotated: self.appendRotated(data.copy()) if self.isVisible(): self.makePlot(**kwargs) self.draw() def appendData(self,data): if len(self.dataList)>0: if len(data['I']) != len(self.dataList[0]['I']): #Changed the frequncy list self.dataList=[] self.rotatedList=[] self.dataList.append(data) self.rotatedList.append(None) if len(self.dataList) > self.maxDataListLength: self.dataList = self.dataList[-1*self.maxDataListLength:] self.rotatedList = self.rotatedList[-1*self.maxDataListLength:] def appendRotated(self,data): self.rotatedList[-1] = data def makePlot(self, **kwargs): print "Making sweep plot" self.ax.clear() self.ax2.clear() numData2Show = min(self.numData2Show,len(self.dataList)) ch = self.spinbox_channel.value() c, s = self.roach.roachController.getStreamChannelFromFreqChannel(ch) print 'ch, freq[ch]:',ch, ', ',self.roach.roachController.freqList[ch] print 'ch/stream, freq[ch,stream]:',c,'/',s,', ',self.roach.roachController.freqChannels[c,s] #for i in range(numData2Show): for i in range(len(self.dataList) - numData2Show, len(self.dataList)): #print 'i:',i data = self.dataList[i] I=data['I'] Q=data['Q'] #kwargs['alpha']=1. if i==0 else .6 - 0.5*(i-1)/(numData2Show-1) kwargs['alpha'] = 1. if i==len(self.dataList)-1 else .6 - .5*(len(self.dataList)-i-1)/numData2Show fmt = 'b.-' if i==len(self.dataList)-1 else 'c.-' self.ax.plot(I[ch], Q[ch], fmt,**kwargs) center = data['centers'][ch] #print 'center1 ',center self.ax.plot(center[0],center[1],'gx',alpha=kwargs['alpha']) iOnRes = data['IonRes'] qOnRes = data['QonRes'] self.ax.plot(iOnRes[ch],qOnRes[ch],'g.',alpha=kwargs['alpha']) #resFreq = self.roach.roachController.freqList[ch] #loSpan = self.config.getfloat('Roach '+str(self.roachNum),'sweeplospan') #nSteps = len(I[ch]) #loStep = self.config.getfloat('Roach '+str(self.roachNum),'sweeplostep') #freqs = np.linspace(resFreq-loSpan/2., resFreq+loSpan/2., nSteps) try: #freqs = data['freqOffsets'] + self.roach.roachController.freqList[ch] freqs = data['freqOffsets'] + data['freqList'][ch] vel = np.sqrt((I[ch][1:] - I[ch][:-1])**2 + (Q[ch][1:] - Q[ch][:-1])**2) freqs = freqs[:-1]+(freqs[1]-freqs[0])/2. self.ax2.plot(freqs,vel,fmt,alpha=kwargs['alpha']) #self.ax2.semilogy(freqs, np.sqrt(I[ch]**2 + Q[ch]**2),fmt,alpha=kwargs['alpha']) except: print 'Couldn\'t make IQ velocity plot' traceback.print_exc() self.ax2.axvline(x=self.roach.roachController.freqList[ch],color='r') if self.rotatedList[i] is not None: iOnRes2 = self.rotatedList[i]['IonRes'][ch] qOnRes2 = self.rotatedList[i]['QonRes'][ch] center2 = np.copy(center) # needs local copy, not pointer #print 'center2 ',center2 avgI = np.average(iOnRes2) avgQ = np.average(qOnRes2) rotation = self.rotatedList[i]['rotation'] print 'Rotated ch'+str(ch)+' '+str(-1*rotation[ch]*180./np.pi)+' deg' self.ax.plot(iOnRes2,qOnRes2,'r.',alpha=kwargs['alpha']) self.ax.plot([center2[0],avgI],[center2[1],avgQ],'r--',alpha=kwargs['alpha']) self.ax.plot([center2[0],avgI],[center2[1],center2[1]],'r--',alpha=kwargs['alpha']) #self.ax.plot(centers[ch][0],centers[ch][1],'rx',**kwargs) #self.ax.plot(iqOnRes[ch][0]+centers[ch][0],iqOnRes[ch][1]+centers[ch][1],'ro',**kwargs) #print 'channel center plot',ch,centers[ch][0],centers[ch][1] self.ax.set_xlabel('I') self.ax.set_ylabel('Q') self.ax2.set_xlabel('Freqs [Hz]') self.ax2.set_ylabel('IQ velocity') def draw(self): #print 'r'+str(self.roachNum)+' drawing data - '+str(self.counter) self.canvas.draw() self.canvas.flush_events() def initFreqs(self, fromRoach=True): """ After we've loaded the frequency file in RoachStateMachine object then we can initialize some GUI elements Also called whenever we change the current channel INPUTS: fromRoach - True if we've just finished a LoadFreq command - False if we call it otherwise. Like from switching channels """ ch=self.spinbox_channel.value() if fromRoach: self.channelsModified=set() if ch in self.channelsModified: self.label_modifyFlag.show() else: self.label_modifyFlag.hide() freqs = self.roach.roachController.freqList attens = self.roach.roachController.attenList try: lofreq = self.roach.roachController.LOFreq except AttributeError: lofreq=0 try: fList = np.copy(self.roach.roachController.dacQuantizedFreqList) fList[np.where(fList>(self.roach.roachController.params['dacSampleRate']/2.))] -= self.roach.roachController.params['dacSampleRate'] fList+=self.roach.roachController.LOFreq freqs=np.copy(fList) self.label_freq.setText('Quantized Freq: '+str(freqs[ch]/1.e9)+' GHz') except AttributeError: self.label_freq.setText('Freq: '+str(freqs[ch]/1.e9)+' GHz') self.spinbox_channel.setRange(0,len(freqs)-1) self.label_atten.setText('Atten: '+str(attens[ch])+' dB') self.label_lofreq.setText('LO Freq: '+str(lofreq/1.e9)+' GHz') self.textbox_modifyFreq.setText("%.9e" % freqs[ch]) self.spinbox_modifyAtten.setValue(int(attens[ch])) def saveResonator(self): freqs = self.roach.roachController.freqList attens = self.roach.roachController.attenList ch=self.spinbox_channel.value() print 'P:Old freq: ', freqs[ch] newFreq = float(self.textbox_modifyFreq.text()) print 'P:New freq: ', newFreq newAtten = self.spinbox_modifyAtten.value() if newFreq!=freqs[ch]: self.resetRoach.emit(RoachStateMachine.LOADFREQ) elif newAtten!= attens[ch]: self.resetRoach.emit(RoachStateMachine.DEFINEDACLUT) freqs[ch] = newFreq # This changes it in the Roach2Control object as well. That's what we want attens[ch] = newAtten self.channelsModified = self.channelsModified | set([ch]) self.writeNewFreqFile(np.copy(freqs), np.copy(attens)) self.label_modifyFlag.show() def writeNewFreqFile(self, freqs=None, attens=None): if freqs is None: freqs = np.copy(self.roach.roachController.freqList) if attens is None: attens = np.copy(self.roach.roachController.attenList) keepRes = np.where(attens < 99) # remove any resonators with atten=99 nFreqs = len(freqs) freqs = freqs[keepRes] attens=attens[keepRes] if len(freqs) != nFreqs: self.resetRoach.emit(RoachStateMachine.LOADFREQ) freqFile = self.config.get('Roach '+str(self.roachNum),'freqfile') newFreqFile=freqFile.rsplit('.',1)[0]+str('_NEW.')+freqFile.rsplit('.',1)[1] data=np.transpose([freqs,attens]) print "Saving "+newFreqFile np.savetxt(newFreqFile,data,['%15.9e', '%4i']) def changedSetting(self,settingID,setting): """ When a setting is changed, reflect the change in the config object which is shared across all GUI elements. INPUTS: settingID - the key in the configparser setting - the value """ self.config.set('Roach '+str(self.roachNum),settingID,str(setting)) #If we don't force the setting value to be a string then the configparser has trouble grabbing the value later on for some unknown reason newSetting = self.config.get('Roach '+str(self.roachNum),settingID) print 'setting ',settingID,' to ',newSetting def keyPressed(self, event): """ This function is called when a key is pressed on the central widget Only works when in focus :-/ INPUTS: event - QKeyEvent """ if event.key() == Qt.Key_Left or event.key() == Qt.Key_A: print "decrease channel from keyboard" elif event.key() == Qt.Key_Right or event.key() == Qt.Key_D: print "increase channel from keyboard" raise NotImplementedError def figureClicked(self, event): """ INPUTS: event - matplotlib.backend_bases.MouseEvent """ if self.mpl_toolbar._active is None: if event.inaxes == self.ax2: clickFreq = event.xdata self.textbox_modifyFreq.setText("%.9e" % clickFreq) try: self.clickLine.remove() except: pass self.clickLine = self.ax2.axvline(x=clickFreq,color='g') #plot closest point in IQ plane ch=self.spinbox_channel.value() data = self.dataList[-1] I=data['I'][ch] Q=data['Q'][ch] freqs = data['freqOffsets'] + self.roach.roachController.freqList[ch] arg = np.abs(np.atleast_1d(freqs) - clickFreq).argmin() i_click = I[arg] q_click = Q[arg] try: self.clickPoint.remove() except: pass self.clickPoint, = self.ax.plot(i_click, q_click, 'ro') self.draw() #print event.inaxes #print self.ax #print self.ax2 #print event.xdata def changeADCAtten(self, adcAtten): """ This function executes when change the adc attenuation spinbox It tells the ADC attenuator to change Works similiarly to phaseSnapShot() """ QtCore.QMetaObject.invokeMethod(self.roach, 'loadADCAtten', Qt.QueuedConnection, QtCore.Q_ARG(float, adcAtten)) self.adcAttenChanged.emit() self.resetRoach.emit(RoachStateMachine.SWEEP) def changeDACAtten(self, dacAtten): """ This function executes when we modify the dac atten start spinbox If we're keeping the resonators at a fixed atten, then we need to redefine the LUTs (which sets the DAC atten) Otherwise, we need to directly change the DAC atten INPUTS: dacAtten - the dac attenuation """ if self.checkbox_resAttenFixed.isChecked(): self.resetRoach.emit(RoachStateMachine.DEFINEDACLUT) else: QtCore.QMetaObject.invokeMethod(self.roach, 'loadDACAtten', Qt.QueuedConnection, QtCore.Q_ARG(float, dacAtten)) self.dacAttenChanged.emit() attens = self.roach.roachController.attenList attens=attens+(dacAtten-self.dacAtten) self.roach.roachController.attenList=attens # force this to update self.writeNewFreqFile(attens=attens) self.resetRoach.emit(RoachStateMachine.SWEEP) self.initFreqs(False) self.dacAtten=dacAtten def toggleResAttenFixed(self): ''' This function executes when you check/uncheck the keep res atten fixed box ''' if self.checkbox_resAttenFixed.isChecked(): self.checkbox_resAttenFixed.setText('Keep Res Atten Fixed') else: dacAtten = self.config.getfloat('Roach '+str(self.roachNum),'dacatten_start') self.checkbox_resAttenFixed.setText('Keep Res Atten Fixed (was '+str(dacAtten)+' dB)') def create_main_frame(self): """ Makes GUI elements on the window """ self.main_frame = QWidget() #self.main_frame.keyPressEvent = self.keyPressed self.dpi = 100 self.fig = Figure((9.0, 5.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.canvas.mpl_connect('button_press_event', self.figureClicked) self.ax = self.fig.add_subplot(121) self.ax.set_xlabel('I') self.ax.set_ylabel('Q') self.ax2 = self.fig.add_subplot(122) self.ax2.set_xlabel('Freq') self.ax2.set_ylabel('IQ velocity') # Create the navigation toolbar, tied to the canvas self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) label_channel = QLabel('Channel:') self.spinbox_channel = QSpinBox() #initializes to 0 self.spinbox_channel.setRange(0,0) #set the range after we read the freq file self.spinbox_channel.setWrapping(True) #self.spinbox_channel.valueChanged.connect(self.plotData) self.spinbox_channel.valueChanged.connect(lambda x: self.plotData()) self.spinbox_channel.valueChanged.connect(lambda x: self.initFreqs(False)) self.label_freq = QLabel('Freq: 0 GHz') self.label_freq.setMinimumWidth(170) self.label_freq.setMaximumWidth(170) self.label_atten = QLabel('Atten: 0 dB') self.label_lofreq = QLabel('LO Freq: 0 GHz') label_num2Plot = QLabel('Num Plots:') spinbox_num2Plot = QSpinBox() spinbox_num2Plot.setWrapping(False) spinbox_num2Plot.setCorrectionMode(QAbstractSpinBox.CorrectToNearestValue) spinbox_num2Plot.setRange(1,self.maxDataListLength) spinbox_num2Plot.setValue(self.numData2Show) def changeNum2Plot(x): self.numData2Show=x spinbox_num2Plot.valueChanged.connect(changeNum2Plot) label_modify = QLabel('Modify Resonator - ') label_modifyFreq = QLabel('Freq [Hz]: ') self.textbox_modifyFreq = QLineEdit('') self.textbox_modifyFreq.setMaximumWidth(150) self.textbox_modifyFreq.setMinimumWidth(150) label_modifyAtten = QLabel('Atten: ') self.spinbox_modifyAtten = QSpinBox() self.spinbox_modifyAtten.setSuffix(' dB') self.spinbox_modifyAtten.setWrapping(False) self.spinbox_modifyAtten.setCorrectionMode(QAbstractSpinBox.CorrectToNearestValue) self.button_modifyRes = QPushButton('Save Resonators') self.button_modifyRes.setEnabled(True) self.button_modifyRes.clicked.connect(self.saveResonator) #self.button_modifyRes.clicked.connect(lambda x: self.resetRoach.emit(RoachStateMachine.LOADFREQ)) self.label_modifyFlag = QLabel('MODIFIED') self.label_modifyFlag.setStyleSheet('color: red') self.label_modifyFlag.hide() dacAttenStart = self.config.getfloat('Roach '+str(self.roachNum),'dacatten_start') self.dacAtten=dacAttenStart label_dacAttenStart = QLabel('DAC atten:') spinbox_dacAttenStart = QDoubleSpinBox() spinbox_dacAttenStart.setValue(dacAttenStart) spinbox_dacAttenStart.setSuffix(' dB') spinbox_dacAttenStart.setRange(0,31.75*2) # There are 2 DAC attenuators spinbox_dacAttenStart.setToolTip("Rounded to the nearest 1/4 dB") spinbox_dacAttenStart.setSingleStep(1.) spinbox_dacAttenStart.setWrapping(False) spinbox_dacAttenStart.setCorrectionMode(QAbstractSpinBox.CorrectToNearestValue) dacAttenStop = self.config.getfloat('Roach '+str(self.roachNum),'dacatten_stop') label_dacAttenStop = QLabel(' to ') spinbox_dacAttenStop = QDoubleSpinBox() spinbox_dacAttenStop.setValue(dacAttenStop) spinbox_dacAttenStop.setSuffix(' dB') spinbox_dacAttenStop.setRange(0,31.75*2) spinbox_dacAttenStop.setToolTip("Rounded to the nearest 1/4 dB") spinbox_dacAttenStop.setSingleStep(1.) spinbox_dacAttenStop.setWrapping(False) spinbox_dacAttenStop.setCorrectionMode(QAbstractSpinBox.CorrectToNearestValue) spinbox_dacAttenStart.valueChanged.connect(partial(self.changedSetting,'dacatten_start')) spinbox_dacAttenStart.valueChanged.connect(spinbox_dacAttenStop.setValue) #Automatically change value of dac atten stop when start value changes spinbox_dacAttenStop.valueChanged.connect(partial(self.changedSetting,'dacatten_stop')) spinbox_dacAttenStop.valueChanged.connect(lambda x: spinbox_dacAttenStop.setValue(max(spinbox_dacAttenStart.value(),x))) #Force stop value to be larger than start value #spinbox_dacAttenStart.valueChanged.connect(lambda x: self.resetRoach.emit(RoachStateMachine.DEFINEDACLUT)) # reset roach to so that the new dac atten is loaded in next time we sweep spinbox_dacAttenStart.valueChanged.connect(self.changeDACAtten) adcAtten = self.config.getfloat('Roach '+str(self.roachNum),'adcatten') label_adcAtten = QLabel('ADC Atten:') spinbox_adcAtten = QDoubleSpinBox() spinbox_adcAtten.setValue(adcAtten) spinbox_adcAtten.setSuffix(' dB') spinbox_adcAtten.setRange(0,31.75) spinbox_adcAtten.setToolTip("Rounded to the nearest 1/4 dB") spinbox_adcAtten.setSingleStep(1.) spinbox_adcAtten.setWrapping(False) spinbox_adcAtten.setCorrectionMode(QAbstractSpinBox.CorrectToNearestValue) spinbox_adcAtten.valueChanged.connect(partial(self.changedSetting,'adcatten')) #spinbox_adcAtten.valueChanged.connect(lambda x: self.resetRoach.emit(RoachStateMachine.SWEEP)) # reset state of roach spinbox_adcAtten.valueChanged.connect(self.changeADCAtten) self.checkbox_resAttenFixed = QCheckBox('Keep Res Atten Fixed') self.checkbox_resAttenFixed.setChecked(True) self.checkbox_resAttenFixed.stateChanged.connect(lambda x: self.toggleResAttenFixed()) psFile = self.config.get('Roach '+str(self.roachNum),'powersweepfile') label_psFile = QLabel('Powersweep File:') textbox_psFile = QLineEdit(psFile) textbox_psFile.setMaximumWidth(300) textbox_psFile.setMinimumWidth(300) textbox_psFile.textChanged.connect(partial(self.changedSetting,'powersweepfile')) loSpan = self.config.getfloat('Roach '+str(self.roachNum),'sweeplospan') label_loSpan = QLabel('LO Span [Hz]:') loSpan_str = "%.3e" % loSpan textbox_loSpan = QLineEdit(loSpan_str) textbox_loSpan.setMaximumWidth(90) textbox_loSpan.setMinimumWidth(90) #textbox_loSpan.textChanged.connect(partial(self.changedSetting,'sweeplospan')) # This just saves whatever string you type in textbox_loSpan.textChanged.connect(lambda x: self.changedSetting('sweeplospan',"%.9e" % float(x))) loStep = self.config.getfloat('Roach '+str(self.roachNum),'sweeplostep') label_loStep = QLabel('LO Step [Hz]:') loStep_str = "%.3e" % loStep textbox_loStep = QLineEdit(loStep_str) textbox_loStep.setMaximumWidth(90) textbox_loStep.setMinimumWidth(90) textbox_loStep.textChanged.connect(lambda x: self.changedSetting('sweeplostep',"%.9e" % float(x))) button_sweep = QPushButton("Sweep Freqs") button_sweep.setEnabled(True) button_sweep.clicked.connect(self.sweepClicked) #You can connect signals to more signals! #button_fit = QPushButton("Fit Loops") #button_fit.setEnabled(True) #button_fit.clicked.connect(self.fitClicked) button_rotate = QPushButton("Rotate Loops") button_rotate.setEnabled(True) button_rotate.clicked.connect(self.rotateClicked) button_translate = QPushButton("Translate Loops") button_translate.setEnabled(True) button_translate.clicked.connect(self.translateClicked) ''' centerBool = self.config.getboolean('Roach '+str(self.roachNum),'centerbool') checkbox_center = QCheckBox('Recenter') checkbox_center.setChecked(centerBool) #checkbox_center.stateChanged.connect(partial(self.changedSetting,'centerbool')) # This has some weird tristate int checkbox_center.stateChanged.connect(lambda x: self.changedSetting('centerbool',checkbox_center.isChecked())) ''' vbox_plot = QVBoxLayout() vbox_plot.addWidget(self.canvas) vbox_plot.addWidget(self.mpl_toolbar) hbox_res = QHBoxLayout() hbox_res.addWidget(label_channel) hbox_res.addWidget(self.spinbox_channel) hbox_res.addSpacing(20) hbox_res.addWidget(self.label_freq) hbox_res.addWidget(self.label_atten) hbox_res.addWidget(self.label_lofreq) hbox_res.addSpacing(20) hbox_res.addWidget(label_num2Plot) hbox_res.addWidget(spinbox_num2Plot) hbox_res.addStretch() hbox_modifyRes = QHBoxLayout() hbox_modifyRes.addWidget(label_modify) hbox_modifyRes.addWidget(label_modifyFreq) hbox_modifyRes.addWidget(self.textbox_modifyFreq) hbox_modifyRes.addWidget(label_modifyAtten) hbox_modifyRes.addWidget(self.spinbox_modifyAtten) hbox_modifyRes.addWidget(self.button_modifyRes) hbox_modifyRes.addWidget(self.label_modifyFlag) hbox_modifyRes.addStretch() hbox_atten = QHBoxLayout() hbox_atten.addWidget(label_dacAttenStart) hbox_atten.addWidget(spinbox_dacAttenStart) hbox_atten.addWidget(label_dacAttenStop) hbox_atten.addWidget(spinbox_dacAttenStop) hbox_atten.addSpacing(30) hbox_atten.addWidget(label_adcAtten) hbox_atten.addWidget(spinbox_adcAtten) hbox_atten.addSpacing(30) hbox_atten.addWidget(self.checkbox_resAttenFixed) hbox_atten.addStretch() hbox_powersweep = QHBoxLayout() hbox_powersweep.addWidget(label_psFile) hbox_powersweep.addWidget(textbox_psFile) hbox_powersweep.addStretch() hbox_sweep = QHBoxLayout() hbox_sweep.addWidget(label_loSpan) hbox_sweep.addWidget(textbox_loSpan) hbox_sweep.addWidget(label_loStep) hbox_sweep.addWidget(textbox_loStep) hbox_sweep.addWidget(button_sweep) hbox_sweep.addSpacing(50) #hbox_sweep.addWidget(button_fit) hbox_sweep.addWidget(button_rotate) hbox_sweep.addWidget(button_translate) #hbox_sweep.addWidget(checkbox_center) hbox_sweep.addStretch() box = QVBoxLayout() box.addLayout(vbox_plot) box.addLayout(hbox_res) box.addLayout(hbox_modifyRes) box.addLayout(hbox_atten) box.addLayout(hbox_powersweep) box.addLayout(hbox_sweep) label_note = QLabel("NOTE: Changing Settings won't take effect until you reload them into the ROACH2") label_note.setWordWrap(True) box.addSpacing(20) box.addWidget(label_note) self.main_frame.setLayout(box) self.setCentralWidget(self.main_frame) def closeEvent(self, event): if self._want_to_close: event.accept() self.close() else: event.ignore() self.hide()
class ProfileDockWidget(QDockWidget): """ DockWidget class to display the profile """ closeSignal = pyqtSignal() def __init__(self, iface): """ Constructor :param iface: interface """ QDockWidget.__init__(self) self.setWindowTitle(QCoreApplication.translate("VDLTools", "Profile Tool")) self.resize(1024, 400) self.__iface = iface self.__canvas = self.__iface.mapCanvas() self.__types = ['PDF', 'PNG'] # ], 'SVG', 'PS'] self.__libs = [] if Qwt5_loaded: self.__lib = 'Qwt5' self.__libs.append('Qwt5') if matplotlib_loaded: self.__libs.append('Matplotlib') elif matplotlib_loaded: self.__lib = 'Matplotlib' self.__libs.append('Matplotlib') else: self.__lib = None self.__iface.messageBar().pushMessage( QCoreApplication.translate("VDLTools", "No graph lib available (qwt5 or matplotlib)"), level=QgsMessageBar.CRITICAL, duration=0) self.__doTracking = False self.__vline = None self.__profiles = None self.__numLines = None self.__mntPoints = None self.__marker = None self.__tabmouseevent = None self.__contentWidget = QWidget() self.setWidget(self.__contentWidget) self.__boxLayout = QHBoxLayout() self.__contentWidget.setLayout(self.__boxLayout) self.__plotFrame = QFrame() self.__frameLayout = QHBoxLayout() self.__plotFrame.setLayout(self.__frameLayout) self.__printLayout = QHBoxLayout() self.__printLayout.addWidget(self.__plotFrame) self.__legendLayout = QVBoxLayout() self.__printLayout.addLayout(self.__legendLayout) self.__printWdg = QWidget() self.__printWdg.setLayout(self.__printLayout) self.__plotWdg = None self.__changePlotWidget() size = QSize(150, 20) self.__boxLayout.addWidget(self.__printWdg) self.__vertLayout = QVBoxLayout() self.__libCombo = QComboBox() self.__libCombo.setFixedSize(size) self.__libCombo.addItems(self.__libs) self.__vertLayout.addWidget(self.__libCombo) self.__libCombo.currentIndexChanged.connect(self.__setLib) self.__maxLabel = QLabel("y max") self.__maxLabel.setFixedSize(size) self.__vertLayout.addWidget(self.__maxLabel) self.__maxSpin = QSpinBox() self.__maxSpin.setFixedSize(size) self.__maxSpin.setRange(-10000, 10000) self.__maxSpin.valueChanged.connect(self.__reScalePlot) self.__vertLayout.addWidget(self.__maxSpin) self.__vertLayout.insertSpacing(10, 20) self.__minLabel = QLabel("y min") self.__minLabel.setFixedSize(size) self.__vertLayout.addWidget(self.__minLabel) self.__minSpin = QSpinBox() self.__minSpin.setFixedSize(size) self.__minSpin.setRange(-10000, 10000) self.__minSpin.valueChanged.connect(self.__reScalePlot) self.__vertLayout.addWidget(self.__minSpin) self.__vertLayout.insertSpacing(10, 40) self.__typeCombo = QComboBox() self.__typeCombo.setFixedSize(size) self.__typeCombo.addItems(self.__types) self.__vertLayout.addWidget(self.__typeCombo) self.__saveButton = QPushButton(QCoreApplication.translate("VDLTools", "Save")) self.__saveButton.setFixedSize(size) self.__saveButton.clicked.connect(self.__save) self.__vertLayout.addWidget(self.__saveButton) self.__boxLayout.addLayout(self.__vertLayout) self.__maxSpin.setEnabled(False) self.__minSpin.setEnabled(False) self.__colors = [] for cn in QColor.colorNames(): qc = QColor(cn) val = qc.red() + qc.green() + qc.blue() if 0 < val < 450: self.__colors.append(cn) def __changePlotWidget(self): """ When plot widget is change (qwt <-> matplotlib) """ self.__activateMouseTracking(False) while self.__frameLayout.count(): child = self.__frameLayout.takeAt(0) child.widget().deleteLater() self.__plotWdg = None if self.__lib == 'Qwt5': self.__plotWdg = QwtPlot(self.__plotFrame) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(10) sizePolicy.setVerticalStretch(0) self.__plotWdg.setSizePolicy(sizePolicy) self.__plotWdg.setAutoFillBackground(False) # Decoration self.__plotWdg.setCanvasBackground(Qt.white) self.__plotWdg.plotLayout().setAlignCanvasToScales(False) self.__plotWdg.plotLayout().setSpacing(100) self.__plotWdg.plotLayout().setCanvasMargin(10, QwtPlot.xBottom) self.__plotWdg.plotLayout().setCanvasMargin(10, QwtPlot.yLeft) title = QwtText(QCoreApplication.translate("VDLTools", "Distance [m]")) title.setFont(QFont("Helvetica", 10)) self.__plotWdg.setAxisTitle(QwtPlot.xBottom, title) title.setText(QCoreApplication.translate("VDLTools", "Elevation [m]")) title.setFont(QFont("Helvetica", 10)) self.__plotWdg.setAxisTitle(QwtPlot.yLeft, title) self.__zoomer = QwtPlotZoomer(QwtPlot.xBottom, QwtPlot.yLeft, QwtPicker.DragSelection, QwtPicker.AlwaysOff, self.__plotWdg.canvas()) self.__zoomer.setRubberBandPen(QPen(Qt.blue)) grid = QwtPlotGrid() grid.setPen(QPen(QColor('grey'), 0, Qt.DotLine)) grid.attach(self.__plotWdg) self.__frameLayout.addWidget(self.__plotWdg) elif self.__lib == 'Matplotlib': # __plotWdg.figure : matplotlib.figure.Figure fig = Figure((1.0, 1.0), linewidth=0.0, subplotpars=SubplotParams(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)) font = {'family': 'arial', 'weight': 'normal', 'size': 12} rc('font', **font) rect = fig.patch rect.set_facecolor((0.9, 0.9, 0.9)) self.__axes = fig.add_axes((0.07, 0.16, 0.92, 0.82)) self.__axes.set_xbound(0, 1000) self.__axes.set_ybound(0, 1000) self.__manageMatplotlibAxe(self.__axes) self.__plotWdg = FigureCanvasQTAgg(fig) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) self.__plotWdg.setSizePolicy(sizePolicy) self.__frameLayout.addWidget(self.__plotWdg) def setProfiles(self, profiles, numLines): """ To set the profiles :param profiles: profiles : positions with elevations (for line and points) :param numLines: number of selected connected lines """ self.__numLines = numLines self.__profiles = profiles if self.__lib == 'Matplotlib': self.__prepare_points() def __getLinearPoints(self): """ To extract the linear points of the profile """ profileLen = 0 self.__profiles[0]['l'] = profileLen for i in range(0, len(self.__profiles)-1): x1 = float(self.__profiles[i]['x']) y1 = float(self.__profiles[i]['y']) x2 = float(self.__profiles[i+1]['x']) y2 = float(self.__profiles[i+1]['y']) profileLen += sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))) self.__profiles[i+1]['l'] = profileLen def __getMnt(self, settings): """ To get the MN data for the profile :param settings: settings containing MN url """ if settings is None or settings.mntUrl is None or settings.mntUrl == "None": url = 'http://map.lausanne.ch/main/wsgi/profile.json' elif settings.mntUrl == "": return else: url = settings.mntUrl names = ['mnt', 'mns', 'toit_rocher'] url += '?layers=' pos = 0 for name in names: if pos > 0: url += ',' pos += 1 url += name url += '&geom={"type":"LineString","coordinates":[' pos = 0 for i in range(len(self.__profiles)): if pos > 0: url += ',' pos += 1 url += '[' + str(self.__profiles[i]['x']) + ',' + str(self.__profiles[i]['y']) + ']' url = url + ']}&nbPoints=' + str(int(self.__profiles[len(self.__profiles)-1]['l'])) try: response = urlopen(url) j = response.read() j_obj = json.loads(j) profile = j_obj['profile'] self.__mntPoints = [] self.__mntPoints.append(names) mnt_l = [] mnt_z = [] for p in range(len(names)): z = [] mnt_z.append(z) for pt in profile: mnt_l.append(float(pt['dist'])) values = pt['values'] for p in range(len(names)): if names[p] in values: mnt_z[p].append(float(values[names[p]])) else: mnt_z[p].append(None) self.__mntPoints.append(mnt_l) self.__mntPoints.append(mnt_z) except HTTPError as e: self.__iface.messageBar().pushMessage( QCoreApplication.translate("VDLTools", "HTTP Error"), QCoreApplication.translate("VDLTools", "status error [" + str(e.code) + "] : " + e.reason), level=QgsMessageBar.CRITICAL, duration=0) except URLError as e: self.__iface.messageBar().pushMessage( QCoreApplication.translate("VDLTools", "URL Error"), e.reason, level=QgsMessageBar.CRITICAL, duration=0) def attachCurves(self, names, settings, usedMnts): """ To attach the curves for the layers to the profile :param names: layers names """ if (self.__profiles is None) or (self.__profiles == 0): return self.__getLinearPoints() if usedMnts is not None and (usedMnts[0] or usedMnts[1] or usedMnts[2]): self.__getMnt(settings) c = 0 if self.__mntPoints is not None: for p in range(len(self.__mntPoints[0])): if usedMnts[p]: legend = QLabel("<font color='" + self.__colors[c] + "'>" + self.__mntPoints[0][p] + "</font>") self.__legendLayout.addWidget(legend) if self.__lib == 'Qwt5': xx = [list(g) for k, g in itertools.groupby(self.__mntPoints[1], lambda x: x is None) if not k] yy = [list(g) for k, g in itertools.groupby(self.__mntPoints[2][p], lambda x: x is None) if not k] for j in range(len(xx)): curve = QwtPlotCurve(self.__mntPoints[0][p]) curve.setData(xx[j], yy[j]) curve.setPen(QPen(QColor(self.__colors[c]), 3)) curve.attach(self.__plotWdg) elif self.__lib == 'Matplotlib': qcol = QColor(self.__colors[c]) self.__plotWdg.figure.get_axes()[0].plot(self.__mntPoints[1], self.__mntPoints[2][p], gid=self.__mntPoints[0][p], linewidth=3) tmp = self.__plotWdg.figure.get_axes()[0].get_lines() for t in range(len(tmp)): if self.__mntPoints[0][p] == tmp[t].get_gid(): tmp[c].set_color((old_div(qcol.red(), 255.0), old_div(qcol.green(), 255.0), old_div(qcol.blue(), 255.0), old_div(qcol.alpha(), 255.0))) self.__plotWdg.draw() break c += 1 if 'z' in self.__profiles[0]: for i in range(len(self.__profiles[0]['z'])): if i < self.__numLines: v = 0 else: v = i - self.__numLines + 1 name = names[v] xx = [] yy = [] for prof in self.__profiles: xx.append(prof['l']) yy.append(prof['z'][i]) for j in range(len(yy)): if yy[j] is None: xx[j] = None if i == 0 or i > (self.__numLines-1): legend = QLabel("<font color='" + self.__colors[c] + "'>" + name + "</font>") self.__legendLayout.addWidget(legend) if self.__lib == 'Qwt5': # Split xx and yy into single lines at None values xx = [list(g) for k, g in itertools.groupby(xx, lambda x: x is None) if not k] yy = [list(g) for k, g in itertools.groupby(yy, lambda x: x is None) if not k] # Create & attach one QwtPlotCurve per one single line for j in range(len(xx)): curve = QwtPlotCurve(name) curve.setData(xx[j], yy[j]) curve.setPen(QPen(QColor(self.__colors[c]), 3)) if i > (self.__numLines-1): curve.setStyle(QwtPlotCurve.Dots) pen = QPen(QColor(self.__colors[c]), 8) pen.setCapStyle(Qt.RoundCap) curve.setPen(pen) curve.attach(self.__plotWdg) elif self.__lib == 'Matplotlib': qcol = QColor(self.__colors[c]) if i < self.__numLines: self.__plotWdg.figure.get_axes()[0].plot(xx, yy, gid=name, linewidth=3) else: self.__plotWdg.figure.get_axes()[0].plot(xx, yy, gid=name, linewidth=5, marker='o', linestyle='None') tmp = self.__plotWdg.figure.get_axes()[0].get_lines() for t in range(len(tmp)): if name == tmp[t].get_gid(): tmp[c].set_color((old_div(qcol.red(), 255.0), old_div(qcol.green(), 255.0), old_div(qcol.blue(), 255.0), old_div(qcol.alpha(), 255.0))) self.__plotWdg.draw() break c += 1 # scaling this try: self.__reScalePlot(None, True) except: self.__iface.messageBar().pushMessage( QCoreApplication.translate("VDLTools", "Rescale problem... (trace printed)"), level=QgsMessageBar.CRITICAL, duration=0) print( QCoreApplication.translate("VDLTools", "rescale problem : "), sys.exc_info()[0], traceback.format_exc()) if self.__lib == 'Qwt5': self.__plotWdg.replot() elif self.__lib == 'Matplotlib': self.__plotWdg.figure.get_axes()[0].redraw_in_frame() self.__plotWdg.draw() self.__activateMouseTracking(True) self.__marker.show() def __reScalePlot(self, value=None, auto=False): """ To rescale the profile plot depending to the bounds """ if (self.__profiles is None) or (self.__profiles == 0): self.__plotWdg.replot() return maxi = 0 for i in range(len(self.__profiles)): if (int(self.__profiles[i]['l'])) > maxi: maxi = int(self.__profiles[i]['l']) + 1 if self.__lib == 'Qwt5': self.__plotWdg.setAxisScale(2, 0, maxi, 0) elif self.__lib == 'Matplotlib': self.__plotWdg.figure.get_axes()[0].set_xbound(0, maxi) minimumValue = self.__minSpin.value() maximumValue = self.__maxSpin.value() # to set max y and min y displayed if auto: minimumValue = 1000000000 maximumValue = -1000000000 for i in range(len(self.__profiles)): if 'z' in self.__profiles[i]: mini = self.__minTab(self.__profiles[i]['z']) if int(mini) < minimumValue: minimumValue = int(mini) - 1 maxi = self.__maxTab(self.__profiles[i]['z']) if int(maxi) > maximumValue: maximumValue = int(maxi) + 1 if self.__mntPoints is not None: for pts in self.__mntPoints[2]: miniMnt = self.__minTab(pts) if int(miniMnt) < minimumValue: minimumValue = int(miniMnt) - 1 maxiMnt = self.__maxTab(pts) if int(maxiMnt) > maximumValue: maximumValue = int(maxiMnt) + 1 self.__maxSpin.setValue(maximumValue) self.__minSpin.setValue(minimumValue) self.__maxSpin.setEnabled(True) self.__minSpin.setEnabled(True) if self.__lib == 'Qwt5': rect = QRectF(0, minimumValue, maxi, maximumValue-minimumValue) self.__zoomer.setZoomBase(rect) # to draw vertical lines for i in range(len(self.__profiles)): zz = [] for j in range(self.__numLines): if self.__profiles[i]['z'][j] is not None: zz.append(j) color = None if len(zz) == 2: width = 3 color = QColor('red') else: width = 1 if self.__lib == 'Qwt5': vertLine = QwtPlotMarker() vertLine.setLineStyle(QwtPlotMarker.VLine) pen = vertLine.linePen() pen.setWidth(width) if color is not None: pen.setColor(color) vertLine.setLinePen(pen) vertLine.setXValue(self.__profiles[i]['l']) label = vertLine.label() label.setText(str(i)) vertLine.setLabel(label) vertLine.setLabelAlignment(Qt.AlignLeft) vertLine.attach(self.__plotWdg) elif self.__lib == 'Matplotlib': self.__plotWdg.figure.get_axes()[0].vlines(self.__profiles[i]['l'], minimumValue, maximumValue, linewidth=width) if minimumValue < maximumValue: if self.__lib == 'Qwt5': self.__plotWdg.setAxisScale(0, minimumValue, maximumValue, 0) self.__plotWdg.replot() elif self.__lib == 'Matplotlib': self.__plotWdg.figure.get_axes()[0].set_ybound(minimumValue, maximumValue) self.__plotWdg.figure.get_axes()[0].redraw_in_frame() self.__plotWdg.draw() @staticmethod def __minTab(tab): """ To get the minimum value in a table :param tab: table to scan :return: minimum value """ mini = 1000000000 for t in tab: if t is None: continue if t < mini: mini = t return mini @staticmethod def __maxTab(tab): """ To get the maximum value in a table :param tab: table to scan :return: maximum value """ maxi = -1000000000 for t in tab: if t is None: continue if t > maxi: maxi = t return maxi def __setLib(self): """ To set the new widget library (qwt <-> matplotlib) """ self.__lib = self.__libs[self.__libCombo.currentIndex()] self.__changePlotWidget() def __save(self): """ To save the profile in a file, on selected format """ idx = self.__typeCombo.currentIndex() if idx == 0: self.__outPDF() elif idx == 1: self.__outPNG() else: self.__iface.messageBar().pushMessage( QCoreApplication.translate("VDLTools", "Invalid index ") + str(idx), level=QgsMessageBar.CRITICAL, duration=0) def __outPDF(self): """ To save the profile as pdf file """ fileName = QFileDialog.getSaveFileName( self.__iface.mainWindow(), QCoreApplication.translate("VDLTools", "Save As"), QCoreApplication.translate("VDLTools", "Profile.pdf"),"Portable Document Format (*.pdf)") if fileName is not None: if self.__lib == 'Qwt5': printer = QPrinter() printer.setCreator(QCoreApplication.translate("VDLTools", "QGIS Profile Plugin")) printer.setOutputFileName(fileName) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOrientation(QPrinter.Landscape) self.__plotWdg.print_(printer) elif self.__lib == 'Matplotlib': self.__plotWdg.figure.savefig(str(fileName)) # printer = QPrinter() # printer.setCreator(QCoreApplication.translate("VDLTools", "QGIS Profile Plugin")) # printer.setOutputFileName(fileName) # printer.setOutputFormat(QPrinter.PdfFormat) # printer.setOrientation(QPrinter.Landscape) # printer.setPaperSize(QSizeF(self.__printWdg.size()), QPrinter.Millimeter) # printer.setFullPage(True) # self.__printWdg.render(printer) def __outPNG(self): """ To save the profile as png file """ fileName = QFileDialog.getSaveFileName( self.__iface.mainWindow(), QCoreApplication.translate("VDLTools", "Save As"), QCoreApplication.translate("VDLTools", "Profile.png"),"Portable Network Graphics (*.png)") if fileName is not None: QPixmap.grabWidget(self.__printWdg).save(fileName, "PNG") def clearData(self): """ To clear the displayed data """ if self.__profiles is None: return if self.__lib == 'Qwt5': self.__plotWdg.clear() self.__profiles = None temp1 = self.__plotWdg.itemList() for j in range(len(temp1)): if temp1[j].rtti() == QwtPlotItem.Rtti_PlotCurve: temp1[j].detach() elif self.__lib == 'Matplotlib': self.__plotWdg.figure.get_axes()[0].cla() self.__manageMatplotlibAxe(self.__plotWdg.figure.get_axes()[0]) self.__maxSpin.setEnabled(False) self.__minSpin.setEnabled(False) self.__maxSpin.setValue(0) self.__minSpin.setValue(0) # clear legend while self.__legendLayout.count(): child = self.__legendLayout.takeAt(0) child.widget().deleteLater() def __manageMatplotlibAxe(self, axe): """ To manage the axes for matplotlib library :param axe: the axes element """ axe.grid() axe.tick_params(axis="both", which="major", direction="out", length=10, width=1, bottom=True, top=False, left=True, right=False) axe.minorticks_on() axe.tick_params(axis="both", which="minor", direction="out", length=5, width=1, bottom=True, top=False, left=True, right=False) axe.set_xlabel(QCoreApplication.translate("VDLTools", "Distance [m]")) axe.set_ylabel(QCoreApplication.translate("VDLTools", "Elevation [m]")) def __activateMouseTracking(self, activate): """ To (de)activate the mouse tracking on the profile for matplotlib library :param activate: true to activate, false to deactivate """ if activate: self.__doTracking = True self.__loadRubber() self.cid = self.__plotWdg.mpl_connect('motion_notify_event', self.__mouseevent_mpl) elif self.__doTracking: self.__doTracking = False self.__plotWdg.mpl_disconnect(self.cid) if self.__marker is not None: self.__canvas.scene().removeItem(self.__marker) try: if self.__vline is not None: self.__plotWdg.figure.get_axes()[0].lines.remove(self.__vline) self.__plotWdg.draw() except Exception as e: self.__iface.messageBar().pushMessage( QCoreApplication.translate("VDLTools", "Tracking exception : ") + str(e), level=QgsMessageBar.CRITICAL, duration=0) def __mouseevent_mpl(self, event): """ To manage matplotlib mouse tracking event :param event: mouse tracking event """ if event.xdata is not None: try: if self.__vline is not None: self.__plotWdg.figure.get_axes()[0].lines.remove(self.__vline) except Exception as e: self.__iface.messageBar().pushMessage( QCoreApplication.translate("VDLTools", "Mouse event exception : ") + str(e), level=QgsMessageBar.CRITICAL, duration=0) xdata = float(event.xdata) self.__vline = self.__plotWdg.figure.get_axes()[0].axvline(xdata, linewidth=2, color='k') self.__plotWdg.draw() i = 1 while i < len(self.__tabmouseevent)-1 and xdata > self.__tabmouseevent[i][0]: i += 1 i -= 1 x = self.__tabmouseevent[i][1] + (self.__tabmouseevent[i + 1][1] - self.__tabmouseevent[i][1]) / ( self.__tabmouseevent[i + 1][0] - self.__tabmouseevent[i][0]) * (xdata - self.__tabmouseevent[i][0]) y = self.__tabmouseevent[i][2] + (self.__tabmouseevent[i + 1][2] - self.__tabmouseevent[i][2]) / ( self.__tabmouseevent[i + 1][0] - self.__tabmouseevent[i][0]) * (xdata - self.__tabmouseevent[i][0]) self.__marker.show() self.__marker.setCenter(QgsPoint(x, y)) def __loadRubber(self): """ To load te rubber band for mouse tracking on map """ self.__marker = QgsVertexMarker(self.__canvas) self.__marker.setIconSize(5) self.__marker.setIconType(QgsVertexMarker.ICON_BOX) self.__marker.setPenWidth(3) def __prepare_points(self): """ To prepare the points on map for mouse tracking on profile """ self.__tabmouseevent = [] length = 0 for i, point in enumerate(self.__profiles): if i == 0: self.__tabmouseevent.append([0, point['x'], point['y']]) else: length += ((self.__profiles[i]['x'] - self.__profiles[i-1]['x']) ** 2 + (self.__profiles[i]['y'] - self.__profiles[i-1]['y']) ** 2) ** 0.5 self.__tabmouseevent.append([float(length), float(point['x']), float(point['y'])]) def closeEvent(self, event): """ When the dock widget is closed :param event: close event """ if self.__maxSpin is not None: Signal.safelyDisconnect(self.__maxSpin.valueChanged, self.__reScalePlot) self.__maxSpin = None if self.__minSpin is not None: Signal.safelyDisconnect(self.__minSpin.valueChanged, self.__reScalePlot) self.__minSpin = None if self.__saveButton is not None: Signal.safelyDisconnect(self.__saveButton.clicked, self.__save) self.__saveButton = None if self.__libCombo is not None: Signal.safelyDisconnect(self.__libCombo.currentIndexChanged, self.__setLib) self.__libCombo = None self.closeSignal.emit() if self.__marker is not None: self.__marker.hide() QDockWidget.closeEvent(self, event)
class Plot(): def __init__(self, parent=None, width=5, height=5, dpi=100): #pl.ion() self.fig = Figure(figsize=(width, height), dpi=dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(parent) self.canvas.mpl_connect('button_press_event', self.on_click) self.rho_area = self.fig.add_subplot(111) #self.rho_area = self.fig.add_subplot(211) self.rho_area.set_title("rho", fontsize=11) self.fig.tight_layout() #ROS rospy.init_node('cor_joints', anonymous=True) self.mpub = rospy.Publisher('visualization_marker_array', MarkerArray, queue_size=10) #rvizのカラー設定(未) self.carray = [] clist = [[1, 0, 0, 1], [0, 1, 0, 1], [1, 1, 0, 1], [1, 0.5, 0, 1]] for c in clist: color = ColorRGBA() color.r = c[0] color.g = c[1] color.b = c[2] color.a = c[3] self.carray.append(color) self.jidx = [ [11, 3, 2, 10, 1, 0], [10, 4, 5, 6], [10, 7, 8, 9], ] #プロットエリアがクリックされた時 def on_click(self, event): print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % ( event.button, event.x, event.y, event.xdata, event.ydata) row = int(event.ydata) col = int(event.xdata) print "---(", row, ", ", col, ")---" print 'cca r:', self.r_m[row][col] self.pubViz(row, col) def rviz_obj(self, obj_id, obj_ns, obj_type, obj_size, obj_color=0, obj_life=0): obj = Marker() obj.header.frame_id, obj.header.stamp = "camera_link", rospy.Time.now() obj.ns, obj.action, obj.type = str(obj_ns), 0, obj_type obj.scale.x, obj.scale.y, obj.scale.z = obj_size[0], obj_size[ 1], obj_size[2] obj.color = self.carray[obj_color] obj.lifetime = rospy.Duration.from_sec(obj_life) obj.pose.orientation.w = 1.0 return obj def set_point(self, pos, addx=0, addy=0, addz=0, rotate=False): pt = Point() if rotate == True: pt.x, pt.y, pt.z = -1 * pos[0] + addx, -1 * pos[1] + addy, pos[ 2] + addz else: pt.x, pt.y, pt.z = pos[0] + addx, pos[1] + addy, pos[2] + addz return pt #def pubViz(self, t_user1, t_user2): #def pubViz(self, r, c, cor, wts, poses, wins, orgs): def pubViz(self, row, col): #print "r, c", row, col rate = rospy.Rate(17) #offset = c if r>c else r #datasize = len(self.data1) window = self.window offset = self.offset d1_start = col d1_end = d1_start + window viz_data1 = self.data1[d1_start:d1_end, :] #(34, 36) d2_start = col - (row - offset - 1) d2_end = d2_start + window viz_data2 = self.data2[d2_start:d2_end, :] vel1 = self.vel1[d1_start:d1_end] vel2 = self.vel2[d2_start:d2_end] self.draw_raw_data(vel1, vel2) #plt.plot(vel1) #plt.plot(vel2) #plt.show() #plt.scatter(vel1, vel2) #plt.show() viz_poses = [viz_data1, viz_data2] viz_rotates = [True, False] viz_offsets = [0, 1] for i in range(len(viz_data1)): msgs = MarkerArray() """ sq = i+offset print "frame:",sq tmsg = self.rviz_obj(10, 'f10', 9, [0.1, 0.1, 0.1], 0) tmsg.pose.position.x,tmsg.pose.position.y,tmsg.pose.position.z=0,0,0 tmsg.text = "c:"+str(round(cor, 3))+", f:"+str(sq) msgs.markers.append(tmsg) """ #print i for u in range(2): # points pmsg = self.rviz_obj(u, 'p' + str(u), 7, [0.03, 0.03, 0.03], 0) #print np.array(viz_poses[u][i,:]).shape vpos = np.array(viz_poses[u][i, :]).reshape(12, 3) #決めうち pmsg.points = [ self.set_point(p, rotate=viz_rotates[u], addx=viz_offsets[u]) for p in vpos ] msgs.markers.append(pmsg) # lines lmsg = self.rviz_obj(u, 'l' + str(u), 5, [0.005, 0.005, 0.005], 2) for jid in self.jidx: for pi in range(len(jid) - 1): for add in range(2): lmsg.points.append( self.set_point( vpos[jid[pi + add]], rotate=viz_rotates[u], addx=viz_offsets[u])) #vpos(12,3) msgs.markers.append(lmsg) """ # text tjs = 0.1 tmsg = self.rviz_obj(u, 't'+str(u), 9, [tjs, tjs, tjs], 0) tmsg.pose.position = self.set_point(pos[i][3], addy=tjs, addz=tjs) tmsg.text = "user_"+str(u+1) msgs.markers.append(tmsg) """ self.mpub.publish(msgs) rate.sleep() def draw_raw_data(self, data1, data2): #data1 = signal.savgol_filter(data1, self.window/4, 5) #data2 = signal.savgol_filter(data2, self.window/4, 5) self.tmp_x_area.cla() self.tmp_x_area.plot(data1) self.tmp_x_area.plot(data2) #self.tmp_x_area.plot(data1) self.tmp_x_area.set_title("raw_data", fontsize=11) #self.tmp_x_area.set_ylim(-0.5, 0.5) #self.tmp_x_area.set_ylim(-0.6, 0.6) self.tmp_y_area.cla() self.tmp_y_area.scatter(data1, data2) #self.tmp_y_area.plot(data2) self.tmp_y_area.set_title("scatter", fontsize=11) self.tmp_y_area.set_xlim(-0.3, 0.3) self.tmp_x_area.set_ylim(-0.3, 0.3) self.fig.canvas.draw() self.fig.tight_layout() def scale(self, X): data = (X - np.mean(X, axis=0)) / np.std(X, axis=0) return data def on_draw(self, r, data1, data2, vel1, vel2, annos, times, window, offset): self.r_m = r self.data1 = data1 self.data2 = data2 self.vel1 = vel1 self.vel2 = vel2 self.window = window self.offset = offset #(offset*2+1, datasize-window)のr_mを(offset*2+1,)となるよう合計をとる print "r_m", self.r_m.shape #plt.show() #データの可視化 #fig = plt.figure() #ax1 = plt.subplot2grid((3,4), (0,0), rowspan=2, colspan=3) #plt.subplot(2,1,1) ax1 = plt.subplot2grid((1, 4), (0, 0), colspan=3) fs = 10 dr, dc = self.r_m.shape Y, X = np.mgrid[slice(0, dr + 1, 1), slice(0, dc + 1, 1)] #print "min rho", np.min(self.r_m) #print "data shape",self.data1.shape,self.data2.shape #fig = plt.figure() #ax = fig.add_subplot(111) #axp = ax.imshow(np.random.randint(0, 100, (100, 100))) # Adding the colorbar #cbaxes = fig.add_axes([0.1, 0.1, 0.03, 0.8]) # This is the position for the colorbar #cb = plt.colorbar(axp, cax = cbaxes) img = plt.pcolor(X, Y, self.r_m, vmin=-1.0, vmax=1.0, cmap=cm.bwr) #plt.colorbar(img) #plt.tick_params(labelsize=5) plt.xlim(0, dc) plt.ylim(0, dr) diff_times = [] for i in range(len(times)): diff_times.append(times[i] - times[0]) diff_times = np.round(diff_times, 1) plt.xticks(np.arange(dc)[::500], diff_times[::500]) #print len(np.arange(dc)[::500]), len(diff_times[::500]) esp_times = [] for i in range(len(times) - 1): if i == 0: esp_times.append(0) esp_times.append(times[i + 1] - times[i]) esp_times = np.round(esp_times, 1) ave_esp_time = np.average(esp_times) print ave_esp_time, " fps:", 1 / ave_esp_time wid = 5 ticks = np.arange(dr)[::wid] use_time = diff_times[:offset] labels = np.hstack((use_time[::-1], 0, -use_time))[::wid] #print labels plt.yticks(ticks, labels) plt.xlabel("time[sec]") plt.ylabel("offset[sec]") title = "moving cross correlation" plt.title(title) """ #plt.subplot(2,1,2) ax2 = plt.subplot2grid((3,4), (2,0), colspan=3) #ax2 = plt.subplot2grid((3,1), (2,0)) data1 = annos[:,0] data2 = annos[:,1] start_1, start_2 = 0, 0 width = 0.9 data1_y, data2_y = 1, 0 for i in range(len(data1)-1): if data1[i]==0 and data1[i+1]==1: start_1 = i if data2[i]==0 and data2[i+1]==1: start_2 = i if data1[i]==1 and data1[i+1]==0: plt.barh(data1_y, i-start_1, width, left=start_1, color='r', edgecolor='k', align="center") if data2[i]==1 and data2[i+1]==0: plt.barh(data2_y, i-start_2, width, left=start_2, color='g', edgecolor='k', align="center") plt.yticks([data2_y, data1_y],["robot","person"]) plt.xticks(np.arange(dc)[::500], diff_times[::500]) plt.xlim(0, dc) plt.ylim(-1, 2) plt.xlabel("time[sec]") plt.title("speaker/listener result") """ #ax3 = plt.subplot2grid((3,4), (0,3), rowspan=2) ax3 = plt.subplot2grid((1, 4), (0, 3)) abs_rm = np.fabs(self.r_m) sum_rm = np.sum(abs_rm, axis=1) datasize = self.r_m.shape[1] dev = np.ones(self.offset) * datasize dev = dev - np.arange(1, self.offset + 1) dev_rm = np.hstack((dev[::-1], datasize, dev)) ave_rm = sum_rm / dev_rm #locate = np.arange(self.offset*2+1)[::5] #locate = np.arange() #labels = np.hstack((np.arange(1,self.offset+1)[::-1],0,-np.arange(1,self.offset+1)))[::5] plt.plot(ave_rm, np.arange(len(ave_rm))) #print ','.join(map(str, ave_rm)) #plt.yticks(locate, labels) plt.yticks(ticks, labels) print ','.join(map(str, ticks)) print ','.join(map(str, labels)) #timeline = np.hstack((use_time[::-1],0,-use_time)) #print ','.join(map(str, timeline)) plt.xlim(0, 0.5) #plt.ylim(0, dr) #print dr, plt.ylabel("offset[sec]") plt.xlabel("abs correlation") plt.title("abs correlation average") plt.grid() plt.rcParams["font.size"] = 10 plt.tight_layout() plt.show() def on_draw_tmp(self, r, data1, data2, vel1, vel2, window, offset): self.r_m = r self.data1 = data1 self.data2 = data2 self.vel1 = vel1 self.vel2 = vel2 self.window = window self.offset = offset #(offset*2+1, datasize-window)のr_mを(offset*2+1,)となるよう合計をとる print "r_m", self.r_m.shape """ for rows in range(self.r_m.shape[0]): for cols in range(self.r_m.shape[1]): self.r_m[rows][cols] = 0 if self.r_m[rows][cols]<0 else self.r_m[rows][cols] """ abs_rm = np.fabs(self.r_m) sum_rm = np.sum(abs_rm, axis=1) datasize = self.r_m.shape[1] dev = np.ones(self.offset) * datasize dev = dev - np.arange(1, self.offset + 1) dev_rm = np.hstack((dev[::-1], datasize, dev)) ave_rm = sum_rm / dev_rm locate = np.arange(self.offset * 2 + 1)[::5] #locate = np.arange() labels = np.hstack((np.arange(1, self.offset + 1)[::-1], 0, -np.arange(1, self.offset + 1)))[::5] plt.plot(ave_rm) plt.xticks(locate, labels) plt.ylim(0, 0.3) plt.show() self.rho_area = self.fig.add_subplot(111) #self.rho_area = self.fig.add_subplot(211) #データの可視化 fs = 10 dr, dc = self.r_m.shape Y, X = np.mgrid[slice(0, dr + 1, 1), slice(0, dc + 1, 1)] print "min rho", np.min(self.r_m) print "data shape", self.data1.shape, self.data2.shape if np.min(self.r_m) < 0: img = self.rho_area.pcolor(X, Y, self.r_m, vmin=-1.0, vmax=1.0, cmap=cm.bwr) #gray #bwr else: img = self.rho_area.pcolor(X, Y, self.r_m, vmin=0.0, vmax=1.0, cmap=cm.gray) #gray #bwr """ if self.cbar == None: self.cbar = self.fig.colorbar(img) self.cbar.ax.tick_params(labelsize=fs-1) """ self.rho_area.set_xlim(0, dc) self.rho_area.set_ylim(0, dr) wid = 10 #とりあえず決め打ちで10ずつ目盛表示 ticks = [i * wid for i in range(dr / wid + 1)] labels = [(dr - 1) / 2 - i * wid for i in range(dr / wid + 1)] self.rho_area.set_yticks(ticks=ticks) self.rho_area.set_yticklabels(labels=labels) self.rho_area.set_xlabel("user 1") self.rho_area.set_ylabel("user 2") self.rho_area.tick_params(labelsize=fs) self.rho_area.set_title("rho", fontsize=fs + 1) self.fig.canvas.draw()
class Form(QMainWindow): def __init__(self, parent=None): super(Form, self).__init__(parent) self.setWindowTitle('SHOGUN interactive demo') self.series_list_model = QStandardItemModel() self.create_menu() self.create_main_frame() self.create_status_bar() self.create_toy_data() self.on_show() def on_show(self): self.axes.clear() self.axes.plot(self.x, self.y, 'ro') self.axes.set_xlim((self.xmin, self.xmax)) self.axes.set_ylim((self.ymin, self.ymax)) self.axes.grid(True) self.canvas.draw() self.fill_series_list(self.get_stats()) def on_about(self): msg = __doc__ QMessageBox.about(self, "About the demo", msg.strip()) def fill_series_list(self, names): self.series_list_model.clear() for name in names: item = QStandardItem(name) item.setCheckState(Qt.Unchecked) item.setCheckable(False) self.series_list_model.appendRow(item) def onclick(self, event): print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % ( event.button, event.x, event.y, event.xdata, event.ydata) x = SP.append(self.x, event.xdata) self.y = SP.append(self.y, event.ydata) self.x = x[:, SP.newaxis] self.on_show() self.status_text.setText("New data point: x=%f, y=%f" % (event.xdata, event.ydata)) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") #load_action = self.create_action("&Load file", # shortcut="Ctrl+L", slot=self.load_file, tip="Load a file") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") #self.add_actions(self.file_menu, # (load_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action, )) def clear_data(self): self.x = SP.array([]) self.y = SP.array([]) self.xmin = -5 self.xmax = 5 self.ymin = -5 self.ymax = 5 self.on_show() self.status_text.setText("Data cleared") def enable_widgets(self): kernel_name = self.kernel_combo.currentText() if kernel_name == "Linear": self.sigma.setDisabled(True) self.degree.setDisabled(True) elif kernel_name == "Polynomial": self.sigma.setDisabled(True) self.degree.setEnabled(True) elif kernel_name == "Gaussian": self.sigma.setEnabled(True) self.degree.setDisabled(True) def get_stats(self): num_train = len(self.x) str_train = "num training points: %i" % num_train str_test = "num training points: %s" % self.nTest.text() return (str_train, str_test) def create_toy_data(self): #0. generate Toy-Data; just samples from a superposition of a sin + linear trend x = SP.arange(self.xmin, self.xmax, (self.xmax - self.xmin) / 100.0) C = 2 #offset b = 0 y = b * x + C + float(self.sine_amplitude.text()) * SP.sin( float(self.sine_freq.text()) * x) # dy = b + 1*SP.cos(x) y += float(self.noise_level.text()) * random.randn(y.shape[0]) self.y = y - y.mean() self.x = x[:, SP.newaxis] self.on_show() def learn_kernel_width(self): root = ModelSelectionParameters() c1 = ModelSelectionParameters("inference_method", inf) root.append_child(c1) c2 = ModelSelectionParameters("scale") c1.append_child(c2) c2.build_values(0.01, 4.0, R_LINEAR) c3 = ModelSelectionParameters("likelihood_model", likelihood) c1.append_child(c3) c4 = ModelSelectionParameters("sigma") c3.append_child(c4) c4.build_values(0.001, 4.0, R_LINEAR) c5 = ModelSelectionParameters("kernel", SECF) c1.append_child(c5) c6 = ModelSelectionParameters("width") c5.append_child(c6) c6.build_values(0.001, 4.0, R_LINEAR) crit = GradientCriterion() grad = GradientEvaluation(gp, feat_train, labels, crit) grad.set_function(inf) gp.print_modsel_params() root.print_tree() grad_search = GradientModelSelection(root, grad) grad.set_autolock(0) best_combination = grad_search.select_model(1) self.sigma.setText("1.0") self.plot_gp() def plot_gp(self): feat_train = RealFeatures(self.x.T) labels = RegressionLabels(self.y) #[x,y]=self.data.get_data() #feat_train=RealFeatures(x.T) #labels=RegressionLabels(y) n_dimensions = 1 kernel_name = self.kernel_combo.currentText() print "current kernel is %s" % (kernel_name) #new interface with likelihood parametres being decoupled from the covaraince function likelihood = GaussianLikelihood() #covar_parms = SP.log([2]) #hyperparams = {'covar':covar_parms,'lik':SP.log([1])} # construct covariance function width = float(self.sigma.text()) degree = int(self.degree.text()) if kernel_name == "Linear": gk = LinearKernel(feat_train, feat_train) gk.set_normalizer(IdentityKernelNormalizer()) elif kernel_name == "Polynomial": gk = PolyKernel(feat_train, feat_train, degree, True) gk.set_normalizer(IdentityKernelNormalizer()) elif kernel_name == "Gaussian": gk = GaussianKernel(feat_train, feat_train, width) #SECF = GaussianKernel(feat_train, feat_train, width) #covar = SECF zmean = ZeroMean() inf = ExactInferenceMethod(gk, feat_train, zmean, labels, likelihood) inf.get_negative_marginal_likelihood() # location of unispaced predictions x_test = array([linspace(self.xmin, self.xmax, self.nTest.text())]) feat_test = RealFeatures(x_test) gp = GaussianProcessRegression(inf) gp.train() covariance = gp.get_variance_vector(feat_test) predictions = gp.get_mean_vector(feat_test) #print "x_test" #print feat_test.get_feature_matrix() #print "mean predictions" #print predictions.get_labels() #print "covariances" #print covariance.get_labels() self.status_text.setText("Negative Log Marginal Likelihood = %f" % (inf.get_negative_marginal_likelihood())) self.axes.clear() self.axes.grid(True) self.axes.set_xlim((self.xmin, self.xmax)) self.axes.set_ylim((self.ymin, self.ymax)) self.axes.hold(True) x_test = feat_test.get_feature_matrix()[0] self.axes.plot(x_test, predictions, 'b-x') #self.axes.plot(x_test, labels.get_labels(), 'ro') self.axes.plot(self.x, self.y, 'ro') #self.axes.plot(feat_test.get_feature_matrix()[0], predictions.get_labels()-3*sqrt(covariance.get_labels())) #self.axes.plot(feat_test.get_feature_matrix()[0], predictions.get_labels()+3*sqrt(covariance.get_labels())) upper = predictions + 3 * sqrt(covariance) lower = predictions - 3 * sqrt(covariance) self.axes.fill_between(x_test, lower, upper, color='grey') self.axes.hold(False) self.canvas.draw() self.fill_series_list(self.get_stats()) def create_main_frame(self): self.xmin = -5 self.xmax = 5 self.ymin = -5 self.ymax = 5 self.main_frame = QWidget() plot_frame = QWidget() self.dpi = 100 self.fig = Figure((6.0, 6.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) cid = self.canvas.mpl_connect('button_press_event', self.onclick) self.axes = self.fig.add_subplot(111) self.cax = None #self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) self.kernel_combo = QComboBox() self.kernel_combo.insertItem(-1, "Gaussian") self.kernel_combo.insertItem(-1, "Polynomial") self.kernel_combo.insertItem(-1, "Linear") self.kernel_combo.maximumSize = QSize(300, 50) self.connect(self.kernel_combo, SIGNAL("currentIndexChanged(QString)"), self.enable_widgets) log_label = QLabel("Data points") self.series_list_view = QListView() self.series_list_view.setModel(self.series_list_model) self.sine_freq = QLineEdit() self.sine_freq.setText("1.0") self.sine_amplitude = QLineEdit() self.sine_amplitude.setText("1.0") self.sigma = QLineEdit() self.sigma.setText("1.2") self.degree = QLineEdit() self.degree.setText("2") self.noise_level = QLineEdit() self.noise_level.setText("1") self.nTest = QLineEdit() self.nTest.setText("100") spins_hbox = QHBoxLayout() spins_hbox.addWidget(QLabel('Sine data setting: ')) spins_hbox.addWidget(QLabel('Sine Freq.')) spins_hbox.addWidget(self.sine_freq) spins_hbox.addWidget(QLabel('Sine Amplitude')) spins_hbox.addWidget(self.sine_amplitude) spins_hbox.addWidget(QLabel('Noise Level')) spins_hbox.addWidget(self.noise_level) spins_hbox.addStretch(1) spins_hbox2 = QHBoxLayout() spins_hbox2.addWidget(QLabel('Kernel Setting: ')) spins_hbox2.addWidget(QLabel('Type')) spins_hbox2.addWidget(self.kernel_combo) spins_hbox2.addWidget(QLabel("Width")) spins_hbox2.addWidget(self.sigma) spins_hbox2.addWidget(QLabel("Degree")) spins_hbox2.addWidget(self.degree) spins_hbox2.addStretch(1) spins_hbox3 = QHBoxLayout() spins_hbox3.addWidget(QLabel('Test Setting: ')) spins_hbox3.addWidget(QLabel('Number of test points')) spins_hbox3.addWidget(self.nTest) spins_hbox3.addStretch(1) self.show_button = QPushButton("&Train GP") self.connect(self.show_button, SIGNAL('clicked()'), self.plot_gp) self.gen_sine_data_button = QPushButton("&Generate Sine Data") self.connect(self.gen_sine_data_button, SIGNAL('clicked()'), self.create_toy_data) self.clear_data_button = QPushButton("&Clear") self.connect(self.clear_data_button, SIGNAL('clicked()'), self.clear_data) self.learn_kernel_button = QPushButton( "&Learn Kernel Width and train GP") self.connect(self.learn_kernel_button, SIGNAL('clicked()'), self.learn_kernel_width) left_vbox = QVBoxLayout() left_vbox.addWidget(self.canvas) #left_vbox.addWidget(self.mpl_toolbar) right0_vbox = QVBoxLayout() right0_vbox.addWidget(QLabel("Data Points")) right0_vbox.addWidget(self.series_list_view) #right0_vbox.addWidget(self.legend_cb) right0_vbox.addStretch(1) right2_vbox = QVBoxLayout() right2_vbox.addWidget(QLabel("Settings")) right2_vbox.addWidget(self.gen_sine_data_button) right2_vbox.addWidget(self.clear_data_button) right2_vbox.addWidget(self.show_button) #right2_vbox.addWidget(self.learn_kernel_button) right2_vbox.addLayout(spins_hbox) right2_vbox.addLayout(spins_hbox2) right2_vbox.addLayout(spins_hbox3) right2_vbox.addStretch(1) right_vbox = QHBoxLayout() right_vbox.addLayout(right0_vbox) right_vbox.addLayout(right2_vbox) hbox = QVBoxLayout() hbox.addLayout(left_vbox) hbox.addLayout(right_vbox) self.main_frame.setLayout(hbox) self.setCentralWidget(self.main_frame) self.enable_widgets() def create_status_bar(self): self.status_text = QLabel("") self.statusBar().addWidget(self.status_text, 1) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
class readout_histogram(QtGui.QWidget): def __init__(self, reactor, cxn = None, parent=None): QtGui.QWidget.__init__(self, parent) self.reactor = reactor self.cxn = cxn self.thresholdVal = 10 self.last_data = None self.last_hist = None self.last_fid = None self.current_data_set = 0 self.subscribed = [False,False] self.create_layout() self.connect_labrad() def create_layout(self): layout = QtGui.QVBoxLayout() plot_layout = self.create_plot_layout() layout.addLayout(plot_layout) self.setLayout(layout) def create_plot_layout(self): layout = QtGui.QVBoxLayout() self.fig = Figure() self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self) self.axes = self.fig.add_subplot(211) self.axes.set_xlim(left = 0, right = 100) self.axes.set_ylim(bottom = 0, top = 50) self.thresholdLine = self.axes.axvline(self.thresholdVal, linewidth=3.0, color = 'r', label = 'Threshold') self.axes.legend(loc = 'best') self.mpl_toolbar = NavigationToolbar(self.canvas, self) self.axes.set_title('State Readout', fontsize = 22) self.axes1 = self.fig.add_subplot(212) self.axes1.set_xlim(left = 0, right = 10) self.axes1.set_ylim(bottom = 0, top = 1.1) self.fig.tight_layout() layout.addWidget(self.mpl_toolbar) layout.addWidget(self.canvas) return layout def connect_layout(self): self.canvas.mpl_connect('button_press_event', self.on_key_press) @inlineCallbacks def on_key_press(self, event): if event.button == 2: xval = int(round(event.xdata)) yield self.thresholdChange(xval) def on_new_data(self, readout): self.last_data = readout self.current_data_set = self.current_data_set + 1 self.update_histogram(readout) self.plot_fidelity() def update_histogram(self, data): #remove old histogram if self.last_hist is not None: self.last_hist.remove() #explicitly delete the reference although not necessary #el self.last_hist print numpy.max([data[:,1].max()-data[:,1].min(),1]) y = numpy.histogram(data[:,1],int(numpy.max([data[:,1].max()-data[:,1].min(),1]))) print y counts = y[0] bins = y[1][:-1] if bins[0] < 0: bins = bins + .5 print bins, counts self.last_hist = self.axes.bar(bins, counts, width = 1) print 'plot' x_maximum = bins.max() x_min = bins.min() self.axes.set_xlim(left = int(x_min - 2)) self.axes.set_xlim(right = int(x_maximum+2)) self.axes.set_ylim(bottom = 0) y_maximum = counts.max() self.axes.set_ylim(top = y_maximum+1) self.canvas.draw() def plot_fidelity(self): # check xaxis x_max = self.axes1.get_xlim()[1] if self.current_data_set == x_max-2: self.axes1.set_xlim(right = x_max+1) bright = numpy.where(self.last_data[:,1] >= self.thresholdVal) fid = float(len(bright[0]))/len(self.last_data[:,1]) self.last_fid = self.axes1.plot(self.current_data_set,fid,'o') self.canvas.draw() def update_fidelity(self): if self.last_fid is not None: self.axes1.lines[self.current_data_set - 1].remove() if self.last_data is not None: bright = numpy.where(self.last_data[:,1] >= self.thresholdVal) fid = float(len(bright[0]))/len(self.last_data[:,1]) self.last_fid = self.axes1.plot(self.current_data_set,fid,'o') self.canvas.draw() @inlineCallbacks def thresholdChange(self, threshold): #update canvas self.update_canvas_line(threshold) self.thresholdVal = threshold self.update_fidelity() try: server = yield self.cxn.get_server('ParameterVault') yield server.set_parameter(config_hist.readout_threshold_dir[0], config_hist.readout_threshold_dir[1], threshold, context = self.context) except Exception, e: print e yield None
class acplot(QWidget): """docstring for acplot based on QWidget""" def __init__(self, parent=None): # as main window widget # basic init, title-size e.t.c. # super(acplot,QWidget.__init__(self, parent)) check it! QWidget.__init__(self, parent) self.setWindowTitle('Spectrum density plot') self.resize(800, 600) # widgets & layout: self.flbl = QLabel('Filename: will be here, once something opened') self.apnum = QLabel('Current AP: change value with up/down keys') # we'll access this later so self should do the trick opnbtn = QPushButton("Open") # svbtn = QPushButton("Save") # not needed if using mpl, got one in bar exbtn = QPushButton("Exit") # matplotlib widget: self.create_mpl_frame() # connects here: self.connect(opnbtn, SIGNAL('clicked()'), self.opendlg) # below should work as qApp, SLOT('quit()') -> yep, that worked on debug # quit() hangs the entire ipython gui loop, that's not acceptable. self.connect(exbtn, SIGNAL('clicked()'), SLOT('close()')) # vbox = QVBoxLayout() vbox and hbox works the same way # vbox.addWidget(opnbtn) grid = QGridLayout() # assuming 6 by 3 grid grid.setSpacing(10) # test it! grid.addWidget(self.flbl, 1, 0) grid.addWidget(self.apnum, 1, 2) grid.addWidget(self.mpl_frame, 2, 0, 4, 3) grid.addWidget(opnbtn, 6, 0) # grid.addWidget(svbtn, 6, 1) grid.addWidget(exbtn, 6, 2) # clearing figure: # self.fig.clf() - we shouldn't remove axis # no need to self for child objects, as vbox will be pinned to self, nice! self.setLayout(grid) # internal vars self.lastdir = '/' self.fname = '' self.current = 0 self.bd = None def create_mpl_frame(self): self.mpl_frame = QWidget() self.fig = Figure((5.0, 4.0), dpi=100) self.axes = self.fig.add_subplot(111) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.mpl_frame) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() self.mpl_toolbar = NavigationToolbar(self.canvas, self.mpl_frame) # self.canvas.mpl_connect('key_press_event', self.on_key_press) - debug key values # we overload default QWidget keyPressEvent self.canvas.mpl_connect('key_press_event', self.keyPressEvent) vbox = QVBoxLayout() vbox.addWidget(self.canvas) # the matplotlib canvas vbox.addWidget(self.mpl_toolbar) self.mpl_frame.setLayout(vbox) # self.setCentralWidget(self.mpl_frame) def on_draw(self): pass # placeholder, use on plot & resize '''self.fig.clear() self.axes = self.fig.add_subplot(111) #self.axes.plot(self.x, self.y, 'ro') self.axes.imshow(self.data, interpolation='nearest') #self.axes.plot([1,2,3]) self.canvas.draw()''' def opendlg(self): newname = QFileDialog.getOpenFileName(self, 'Open file', self.lastdir, 'Base files (*.base)') if newname == '': self.canvas.setFocus() return self.fname = newname # do the stuff here # update last dir # ONCE USED BY QT str becomes QString!!!! # convert back to use python string functions and such. self.lastdir = os.path.dirname(str(self.fname)) print ' Now using directory:', self.lastdir self.flbl.setText(self.fname) self.apnum.setText('Current AP: 0') # print 'Label updated to:', self.flbl.text() self.bd = basefile.loadMulti(str(self.fname), True) # will disable it once done self.current = 0 self.maxap = 0 self.plot() self.canvas.setFocus() # not to loose focus on mpl plot def plot(self): chan = 1 data, chans, res, periods, sess, ascan, ast1, ast2 = self.bd[ 0:8] # initial data bandwidth = 16.0 self.maxap = periods step = bandwidth / res x = np.arange(0, bandwidth, step) # xt = np.arange(0, bandwidth, 0.5) fix xticks may be? rep = abs(data[chan, self.current]) # self.fig.clf() - entyre fig # self.fig.cla() - axis clean up self.axes.cla() self.axes.plot(x, rep) # self.axes.xticks(xt, rotation=90) xticks on subplot??? self.canvas.draw() # some layout glitches here # placeholder for keyPressEvent on mpl toolbar or graph, we'll not use it for now def on_key_press(self, event): print('you clicked', event.key) # just to see if we can access here # debugging, later on we'll update keyPressEvent! # implement the default mpl key press events described at # http://matplotlib.org/users/navigation_toolbar.html#navigation-keyboard-shortcuts # key_press_handler(event, self.canvas, self.mpl_toolbar) # MPL intercept key handling with different key values 😒 def keyPressEvent(self, event): # if event.key() == Qt.Key_Up or event.key == u'up': # first one works with pure PyQt, second one from mpl keys handler!!! print('you pressed', event.key) # We'll use backup numpad keys, as older matplotlib can't handle arrow keys :( # if event.key == Qt.Key_Up or event.key == u'up' or event.key == '8': if event.key == u'up' or event.key == '8': print 'Key_Up pressed - next AP' if self.bd is None: return self.current += 10 if self.current > self.maxap: self.current = self.maxap self.apnum.setText('Current AP: %d' % self.current) self.plot() # if event.key == Qt.Key_Down or event.key == u'down' or event.key == '2': if event.key == u'down' or event.key == '2': print 'Key_Down pressed - prev AP' if self.bd is None: return self.current -= 10 if self.current < 0: self.current = 0 self.apnum.setText('Current AP: %d' % self.current) self.plot() if event.key == Qt.Key_O or event.key == u'o': # no register check, be warned! print 'Key_o pressed - openDialog here' self.opendlg() if event.key == Qt.Key_Escape or event.key == u'escape': print 'Key_Escape pressed - exit here' if event.key == Qt.Key_Enter or event.key == u'enter': print 'Key_Enter pressed - update plot' # not to re-plot on any change in lineEdit
class Watcher(QMainWindow): """""" def __init__(self, freezer, parent=None): """Constructor for Viewer""" self.freezer = freezer self.numTrials = 0 self.currTrialNum = 0 self.allTrials = None self.allAlignedTrials = None self.allOnsets = None self.idList = None # # Useful stuff self.onsetLine1 = None self.onsetLine2 = None self.isDragging = False QMainWindow.__init__(self, parent) # self.showMaximized() self.createMainFrame() # self.drawTrial() def queryData(self, queryStr): """Query some data from freezer """ self.idList = [] self.allTrials = [] self.allAlignedTrials = [] self.allOnsets = [] self.allQueryResults = { } # {'idxxx': {'var1':value1, 'var2':value2...}} self.queryStr = queryStr # allDocs = self.freezer.processed.find(eval(self.queryStr)) allDocs = self.freezer.posts.find(eval(self.queryStr)) for doc in allDocs: s = StringIO.StringIO(doc['trialData']) tTrialData = pd.read_csv(s) self.allTrials.append(tTrialData) t = 0 #doc['timeOnset'] self.allOnsets.append(t) tId = doc['_id'] self.idList.append(tId) self.allQueryResults[tId] = { 'isAccepted': doc['isAccepted'], 'trialData': tTrialData, 'timeOnset': int(0.0) } self.numTrials = len(self.allTrials) # self.allAlignedTrials = [t for t in self.allTrials] print "Found", self.numTrials, "trials." def freezeAllOnsets(self): """Freeze timeOnset field in Freezer """ allDocs = self.freezer.processed.find(eval(self.queryStr)) try: for onset, doc in zip(self.allOnsets, allDocs): self.freezer.processed.update({'_id': doc['_id']}, {'$set': { 'timeOnset': onset }}) print("Froze %d onsets" % len(self.allOnsets)) except: print("Error updating") def freezeAllIsAccepted(self): """Freeze timeOnset field in Freezer """ allDocs = self.freezer.processed.find(eval(self.queryStr)) try: for isAccepted, doc in zip( self.allQueryResults[self.idList[self.currTrialNum]] ['isAccepted'], allDocs): print isAccepted, doc['_id'] self.freezer.processed.update( {'_id': doc['_id']}, {'$set': { 'isAccepted': isAccepted }}) print("Froze %d isAccepted flags" % len(self.allIsAccepted)) except: print("Error updating") def freezeAllQueryResults(self): """Freeze timeOnset field in Freezer """ try: for id in self.idList: print id, { 'isAccepted': self.allQueryResults[id]['isAccepted'] } self.freezer.processed.update({'_id': id}, { '$set': { 'isAccepted': self.allQueryResults[id]['isAccepted'], 'timeOnset': int(self.allQueryResults[id]['timeOnset']) } }) print("Froze %d isAccepted flags" % len(self.idList)) except: print("Error freezing") def createMainFrame(self): self.main_frame = QWidget() self.fig = Figure((5.0, 4.0), dpi=100) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) self.canvas.setFocusPolicy(Qt.StrongFocus) self.canvas.setFocus() self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) ### Linking some events self.canvas.mpl_connect('key_press_event', self.onKey) self.canvas.mpl_connect('pick_event', self.onPick) self.canvas.mpl_connect('button_press_event', self.onMouseDown) self.canvas.mpl_connect('button_release_event', self.onMouseUp) self.canvas.mpl_connect('motion_notify_event', self.onMouseMotion) self.textbox = QTextEdit("""{"analystName": "zcwaxs"} """) self.textbox.selectAll() self.textbox.setMinimumWidth(200) self.queryButton = QPushButton("&Query") self.connect(self.queryButton, SIGNAL('clicked()'), self.onSubmitQuery) self.fwdButton = QPushButton("&>>") self.connect(self.fwdButton, SIGNAL('clicked()'), self.onFwd) self.bwdButton = QPushButton("&<<") self.connect(self.bwdButton, SIGNAL('clicked()'), self.onBwd) self.saveButton = QPushButton("&Save") self.connect(self.saveButton, SIGNAL('clicked()'), self.onSave) self.alignButton = QPushButton("&Close") self.connect(self.alignButton, SIGNAL('clicked()'), self.onFinish) self.grid_cb = QCheckBox("Show &Grid") self.grid_cb.setChecked(False) # self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.onGrid) self.isAcceptedCB = QCheckBox("Accept?") self.isAcceptedCB.setChecked(False) self.connect(self.isAcceptedCB, SIGNAL('stateChanged(int)'), self.onChangeIsAccepted) slider_label = QLabel('Bar width (%):') self.slider = QSlider(Qt.Horizontal) self.slider.setRange(1, 100) self.slider.setValue(20) self.slider.setTracking(True) self.slider.setTickPosition(QSlider.TicksBothSides) # self.connect(self.slider, SIGNAL('valueChanged(int)'), self.onSlider) # # Layout with box sizers # hbox = QHBoxLayout() for w in [ self.textbox, self.queryButton, self.isAcceptedCB, self.bwdButton, self.fwdButton, self.saveButton, self.alignButton, self.grid_cb, slider_label, self.slider ]: hbox.addWidget(w) hbox.setAlignment(w, Qt.AlignVCenter) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def drawCurrTrial(self): self.fig.clear() self.fig.hold(True) self.ax1 = self.fig.add_subplot(211) self.ax2 = self.fig.add_subplot(212) self.ax1.plot(self.currTrial['Left Elbow Flex / Time']) self.ax1.set_ylim([10, 130]) self.ax2.plot(self.currTrial['Biceps']) self.ax2.set_ylim([-1.0, 1.0]) ### Draw timeOnset lines self.onsetLine1 = self.ax1.axvline(x=self.currOnset(), ymin=0, ymax=100, color='b', linewidth=5) self.onsetLine2 = self.ax2.axvline(x=self.currOnset(), ymin=0, ymax=100, color='r', linewidth=5) self.canvas.draw() def currOnset(self): return self.allQueryResults[self.idList[ self.currTrialNum]]['timeOnset'] def setOnset(self): """Add the field 'onset' to all documents""" l = self.currTrial['Left Elbow Flex / Time'][0:800] base = sum(l) / float(len(l)) th = base * 0.98 f = lambda x: x <= th possible = indices(f, self.currTrial['Left Elbow Flex / Time']) tOnset = possible[0] self.allOnsets[self.currTrialNum] = tOnset self.allQueryResults[self.idList[ self.currTrialNum]]['timeOnset'] = int(tOnset) # self.allAlignedTrials[self.currTrialNum] = self.currTrial.drop(xrange(self.currOnset - 100)) def setPickedOnsetLine(self, artist): self.onsetLine1 = artist self.onsetLine1.set_color('g') def setCurrTrial(self, n=0): self.currTrialNum = n # print(len(self.allTrials)) # self.currTrial = self.allTrials[self.currTrialNum] self.currTrial = self.allQueryResults[self.idList[n]]['trialData'] # print(self.currTrialNum, len(self.currTrial)) self.isAcceptedCB.setChecked( self.allQueryResults[self.idList[n]]['isAccepted']) self.setOnset() def setOnsetLine(self, new_x): xs, ys = self.onsetLine1.get_data() #new_xs = [min(rbound, max(lbound, new_x)) for xx in xs] self.onsetLine1.set_data(new_x, ys) self.onsetLine2.set_data(new_x, ys) self.allQueryResults[self.idList[ self.currTrialNum]]['timeOnset'] = new_x self.canvas.draw() def onPick(self, event): self.setPickedOnsetLine(event.artist) self.canvas.draw() def onMouseDown(self, event): self.isDragging = True def onMouseUp(self, event): self.isDragging = False def onMouseMotion(self, event): if self.isDragging: self.setOnsetLine(event.xdata) def onKey(self, event): if event.key in '[': xs, ys = self.onsetLine1.get_data() new_xs = [xx - 20 for xx in xs] self.onsetLine1.set_data(new_xs, ys) elif event.key in ']': xs, ys = self.onsetLine1.get_data() new_xs = [xx + 20 for xx in xs] self.onsetLine1.set_data(new_xs, ys) elif event.key in '{': xs, ys = self.onsetLine1.get_data() new_xs = [xx - 100 for xx in xs] self.onsetLine1.set_data(new_xs, ys) elif event.key in '}': xs, ys = self.onsetLine1.get_data() new_xs = [xx + 100 for xx in xs] self.onsetLine1.set_data(new_xs, ys) self.canvas.draw() def onFwd(self): """Go forward 1 trial""" self.setCurrTrial(min(self.currTrialNum + 1, self.numTrials - 1)) self.drawCurrTrial() # self.setOnset() # self.setOnsetLine() def onBwd(self): """Go backward 1 trial""" self.setCurrTrial(max(self.currTrialNum - 1, 0)) self.drawCurrTrial() # self.setOnset() # self.setOnsetLine() def onChangeIsAccepted(self, value): self.allQueryResults[self.idList[self.currTrialNum]]['isAccepted'] = \ True if value == 2 else False def onFinish(self): # self.freezeAllOnsets() self.freezeAllQueryResults() self.close() def onSave(self): # self.freezeAllOnsets() print self.currTrialNum print self.currOnset() tDataFrame = self.allQueryResults[self.idList[ self.currTrialNum]]['trialData'] tDataFrame = tDataFrame.drop(xrange(self.currOnset() - 1500)) tDataFrame.to_csv( 'D:\Code\local_timegrinder\ProcessedData\S2\FES_S02_L_FR_T%d.csv' % (self.currTrialNum + 1)) def onSubmitQuery(self): self.queryData(str(self.textbox.toPlainText())) self.setCurrTrial() self.drawCurrTrial()
class mplchart(chart): colours = dict( zip('red green blue yellow magenta black'.split(), 'r g b y m k'.split())) def __init__(self, spurset, fef, parent): chart.__init__(self, spurset, fef, parent) # make the figure blend in with the native application look bgcol = parent.palette().window().color().toRgb() bgcol = [bgcol.redF(), bgcol.greenF(), bgcol.blueF()] self.fig = Figure() self.fig.set_facecolor(bgcol) # a FigureCanvas can be added as a QWidget, so we use that self.plot = FigureCanvas(self.fig) self.plot.setParent(parent) self.ax = self.fig.add_subplot(111) # TODO skip this, just do a redraw() after initialization? self.ax.set_xlim(self.spurset.RFmin, self.spurset.RFmax) self.ax.set_ylim(-0.5 * self.spurset.dspan, 0.5 * self.spurset.dspan) self.ax.grid(True) self.fig.tight_layout() # a second figure to hold the legend self.legendFig = Figure() self.legendFig.set_facecolor(bgcol) self.legendCanvas = FigureCanvas(self.legendFig) self.legendFig.legend(*self.ax.get_legend_handles_labels(), loc='upper left') # connect up the picker watching self.picked_obj = None self._pick = self.plot.mpl_connect('pick_event', self.onpick) self._drag = self.plot.mpl_connect('motion_notify_event', self.ondrag) self._drop = self.plot.mpl_connect('button_release_event', self.ondrop) def legend(self): return self.legendCanvas def mkline(self, xdata, ydata, style, title): return mpl.lines.Line2D(xdata, ydata, label=title, color=self.colours[style[0]], ls=style[1]) def add_line(self, line): self.ax.add_line(line) def del_line(self, line): self.ax.lines.remove(line) def draw_spurs(self, obj): chart.draw_spurs(self, obj) def redraw(self): self.ax.set_ylim(-0.5 * self.spurset.dspan, 0.5 * self.spurset.dspan) self.ax.set_xlim(self.spurset.RFmin, self.spurset.RFmax) # WHO WANTS TO BET THIS IS UNSUPPORTED? # but damn, it works :/ self.legendFig.legends = [] self.legendFig.legend(*self.ax.get_legend_handles_labels(), loc='upper left') self.legendCanvas.draw() self.plot.draw() def draw_fef(self, obj): chart.draw_fef(self, obj) self.feflines[0].set_picker(10) self.feflines[1].set_picker(10) def onpick(self, event): if event.mouseevent.button != 1: # only picking on left-click atm return obj, x, y = event.artist, event.mouseevent.xdata, event.mouseevent.ydata self.picked_obj = obj self.pick(obj, x, y) self.drag(obj, x, y) def ondrag(self, event): self.drag(self.picked_obj, event.xdata, event.ydata) def ondrop(self, event): if event.button != 1: # only picking on left-click atm return self.drop(self.picked_obj, event.xdata, event.ydata) self.picked_obj = None
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Demo: PyQt with matplotlib') self.create_menu() self.create_main_frame() self.create_status_bar() self.textbox.setText('1 2 3 4') self.on_draw() def save_plot(self): file_choices = "PNG (*.png)|*.png" path = unicode( QFileDialog.getSaveFileName(self, 'Save file', '', file_choices)) if path: self.canvas.print_figure(path, dpi=self.dpi) self.statusBar().showMessage('Saved to %s' % path, 2000) def on_about(self): msg = """ A demo of using PyQt with matplotlib: * Use the matplotlib navigation bar * Add values to the text box and press Enter (or click "Draw") * Show or hide the grid * Drag the slider to modify the width of the bars * Save the plot to a file using the File menu * Click on a bar to receive an informative message """ QMessageBox.about(self, "About the demo", msg.strip()) def on_pick(self, event): # The event received here is of the type # matplotlib.backend_bases.PickEvent # # It carries lots of information, of which we're using # only a small amount here. # box_points = event.artist.get_bbox().get_points() msg = "You've clicked on a bar with coords:\n %s" % box_points QMessageBox.information(self, "Click!", msg) def on_draw(self): """ Redraws the figure """ str = unicode(self.textbox.text()) self.data = map(int, str.split()) x = range(len(self.data)) # clear the axes and redraw the plot anew # self.axes.clear() self.axes.grid(self.grid_cb.isChecked()) self.axes.bar(left=x, height=self.data, width=self.slider.value() / 100.0, align='center', alpha=0.44, picker=5) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.dpi = 100 self.fig = Figure((5.0, 4.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) # Since we have only one plot, we can use add_axes # instead of add_subplot, but then the subplot # configuration tool in the navigation toolbar wouldn't # work. # self.axes = self.fig.add_subplot(111) # Bind the 'pick' event for clicking on one of the bars # self.canvas.mpl_connect('pick_event', self.on_pick) # Create the navigation toolbar, tied to the canvas # self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # Other GUI controls # self.textbox = QLineEdit() self.textbox.setMinimumWidth(200) self.connect(self.textbox, SIGNAL('editingFinished ()'), self.on_draw) self.draw_button = QPushButton("&Draw") self.connect(self.draw_button, SIGNAL('clicked()'), self.on_draw) self.grid_cb = QCheckBox("Show &Grid") self.grid_cb.setChecked(False) self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.on_draw) slider_label = QLabel('Bar width (%):') self.slider = QSlider(Qt.Horizontal) self.slider.setRange(1, 100) self.slider.setValue(20) self.slider.setTracking(True) self.slider.setTickPosition(QSlider.TicksBothSides) self.connect(self.slider, SIGNAL('valueChanged(int)'), self.on_draw) # # Layout with box sizers # hbox = QHBoxLayout() for w in [ self.textbox, self.draw_button, self.grid_cb, slider_label, self.slider ]: hbox.addWidget(w) hbox.setAlignment(w, Qt.AlignVCenter) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def create_status_bar(self): self.status_text = QLabel("This is a demo") self.statusBar().addWidget(self.status_text, 1) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") load_file_action = self.create_action("&Save plot", shortcut="Ctrl+S", slot=self.save_plot, tip="Save the plot") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (load_file_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action, )) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
class PlotWidget(QtGui.QWidget): COLORS = 'rbgcmyk' def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.history_s = 20.0 self.next_color = 0 self.paused = False self.figure = matplotlib.figure.Figure() self.canvas = FigureCanvas(self.figure) self.canvas.mpl_connect('key_press_event', self.handle_key_press) self.canvas.mpl_connect('key_release_event', self.handle_key_release) self.left_axis = self.figure.add_subplot(111) self.left_axis.grid() self.left_axis.fmt_xdata = lambda x: '%.3f' % x self.left_axis.legend_loc = LEFT_LEGEND_LOC self.right_axis = None def draw(): # NOTE jpieper: For some reason, on the first repaint # event, the height is negative, which throws spurious # errors. Paper over that here. l, b, w, h = self.figure.bbox.bounds if h < 0: return FigureCanvas.draw(self.canvas) self.canvas.repaint() self.canvas.draw = draw self.toolbar = backend_qt4agg.NavigationToolbar2QT(self.canvas, self) self.pause_action = QtGui.QAction(u'Pause', self) self.pause_action.setCheckable(True) self.pause_action.toggled.connect(self._handle_pause) self.toolbar.addAction(self.pause_action) layout = QtGui.QVBoxLayout(self) layout.addWidget(self.toolbar, 0) layout.addWidget(self.canvas, 1) self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus) def _handle_pause(self, value): self.paused = value def add_plot(self, name, signal, axis_number): axis = self.left_axis if axis_number == 1: if self.right_axis is None: self.right_axis = self.left_axis.twinx() self.right_axis.legend_loc = RIGHT_LEGEND_LOC axis = self.right_axis item = PlotItem(axis, self, name, signal) return item def remove_plot(self, item): item.remove() def _get_axes_keys(self): result = [] result.append(('1', self.left_axis)) if self.right_axis: result.append(('2', self.right_axis)) return result def handle_key_press(self, event): if event.key not in ['1', '2']: return for key, axis in self._get_axes_keys(): if key == event.key: axis.set_navigate(True) else: axis.set_navigate(False) def handle_key_release(self, event): if event.key not in ['1', '2']: return for key, axis in self._get_axes_keys(): axis.set_navigate(True)
class Form(QMainWindow): def __init__(self, parent=None): super(Form, self).__init__(parent) self.setWindowTitle('SHOGUN interactive demo') self.data = DataHolder() self.series_list_model = QStandardItemModel() self.create_menu() self.create_main_frame() self.create_status_bar() self.on_show() def load_file(self, filename=None): filename = QFileDialog.getOpenFileName(self, 'Open a data file', '.', 'CSV files (*.csv);;All Files (*.*)') if filename: self.data.load_from_file(filename) self.fill_series_list(self.data.series_names()) self.status_text.setText("Loaded " + filename) def on_show(self): self.axes.clear() self.axes.grid(True) self.axes.plot(self.data.x1, self.data.x2, 'bo') self.axes.set_xlim((-5,5)) self.axes.set_ylim((-5,5)) self.canvas.draw() self.fill_series_list(self.data.get_stats()) def on_about(self): msg = __doc__ QMessageBox.about(self, "About the demo", msg.strip()) def fill_series_list(self, names): self.series_list_model.clear() for name in names: item = QStandardItem(name) item.setCheckState(Qt.Unchecked) item.setCheckable(False) self.series_list_model.appendRow(item) def onclick(self, event): print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(event.button, event.x, event.y, event.xdata, event.ydata) self.data.add_example(event.xdata, event.ydata) self.on_show() def clear(self): self.data.clear() self.on_show() def enable_widgets(self): kernel_name = self.kernel_combo.currentText() if kernel_name == "LinearKernel": self.sigma.setDisabled(True) self.degree.setDisabled(True) elif kernel_name == "PolynomialKernel": self.sigma.setDisabled(True) self.degree.setEnabled(True) elif kernel_name == "GaussianKernel": self.sigma.setEnabled(True) self.degree.setDisabled(True) def train_svm(self): width = float(self.sigma.text()) degree = int(self.degree.text()) self.axes.clear() self.axes.grid(True) self.axes.plot(self.data.x1, self.data.x2, 'bo') # train svm labels = self.data.get_labels() print type(labels) lab = RegressionLabels(labels) features = self.data.get_examples() train = RealFeatures(features) kernel_name = self.kernel_combo.currentText() print "current kernel is %s" % (kernel_name) if kernel_name == "LinearKernel": gk = LinearKernel(train, train) gk.set_normalizer(IdentityKernelNormalizer()) elif kernel_name == "PolynomialKernel": gk = PolyKernel(train, train, degree, True) gk.set_normalizer(IdentityKernelNormalizer()) elif kernel_name == "GaussianKernel": gk = GaussianKernel(train, train, width) cost = float(self.cost.text()) tubeeps = float(self.tubeeps.text()) print "cost", cost svm = LibSVR(cost, tubeeps, gk, lab) svm.train() svm.set_epsilon(1e-2) x=numpy.linspace(-5.0,5.0,100) y=svm.apply(RealFeatures(numpy.array([x]))).get_labels() self.axes.plot(x,y,'r-') self.axes.set_xlim((-5,5)) self.axes.set_ylim((-5,5)) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() plot_frame = QWidget() self.dpi = 100 self.fig = Figure((6.0, 6.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) cid = self.canvas.mpl_connect('button_press_event', self.onclick) self.axes = self.fig.add_subplot(111) self.cax = None #self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) log_label = QLabel("Number of examples:") self.series_list_view = QListView() self.series_list_view.setModel(self.series_list_model) cost_label = QLabel('C') #self.cost = QSpinBox()#QLineEdit() self.cost = QLineEdit() self.cost.setText("1.0") #self.cost.setMinimum(1) spin_label2 = QLabel('tube') self.tubeeps = QLineEdit() self.tubeeps.setText("0.1") spin_label3 = QLabel('sigma') self.sigma = QLineEdit() self.sigma.setText("1.2") #self.sigma.setMinimum(1) spin_label4 = QLabel('d') self.degree = QLineEdit() self.degree.setText("2") #self.sigma.setMinimum(1) spins_hbox = QHBoxLayout() spins_hbox.addWidget(cost_label) spins_hbox.addWidget(self.cost) spins_hbox.addWidget(spin_label2) spins_hbox.addWidget(self.tubeeps) spins_hbox.addWidget(spin_label3) spins_hbox.addWidget(self.sigma) spins_hbox.addWidget(spin_label4) spins_hbox.addWidget(self.degree) spins_hbox.addStretch(1) self.legend_cb = QCheckBox("Show Support Vectors") self.legend_cb.setChecked(False) self.show_button = QPushButton("&Train SVR") self.connect(self.show_button, SIGNAL('clicked()'), self.train_svm) self.clear_button = QPushButton("&Clear") self.connect(self.clear_button, SIGNAL('clicked()'), self.clear) self.kernel_combo = QComboBox() self.kernel_combo.insertItem(-1, "GaussianKernel") self.kernel_combo.insertItem(-1, "PolynomialKernel") self.kernel_combo.insertItem(-1, "LinearKernel") self.kernel_combo.maximumSize = QSize(300, 50) self.connect(self.kernel_combo, SIGNAL("currentIndexChanged(QString)"), self.enable_widgets) left_vbox = QVBoxLayout() left_vbox.addWidget(self.canvas) #left_vbox.addWidget(self.mpl_toolbar) right0_vbox = QVBoxLayout() right0_vbox.addWidget(log_label) right0_vbox.addWidget(self.series_list_view) #right0_vbox.addWidget(self.legend_cb) right0_vbox.addStretch(1) right2_vbox = QVBoxLayout() right2_label = QLabel("Settings") right2_vbox.addWidget(right2_label) right2_vbox.addWidget(self.show_button) right2_vbox.addWidget(self.kernel_combo) right2_vbox.addLayout(spins_hbox) right2_clearlabel = QLabel("Remove Data") right2_vbox.addWidget(right2_clearlabel) right2_vbox.addWidget(self.clear_button) right2_vbox.addStretch(1) right_vbox = QHBoxLayout() right_vbox.addLayout(right0_vbox) right_vbox.addLayout(right2_vbox) hbox = QVBoxLayout() hbox.addLayout(left_vbox) hbox.addLayout(right_vbox) self.main_frame.setLayout(hbox) self.setCentralWidget(self.main_frame) self.enable_widgets() def create_status_bar(self): self.status_text = QLabel("") self.statusBar().addWidget(self.status_text, 1) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") load_action = self.create_action("&Load file", shortcut="Ctrl+L", slot=self.load_file, tip="Load a file") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (load_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action,)) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action( self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
class AppForm(QMainWindow): ## making a new signal new_signal = pyqtSignal() def __init__(self, parent=None): self.cxn = labrad.connect() self.dv = self.cxn.data_vault self.path = [''] # self.path = ['','Ted','Paramp','UCSB4.2','dev4.2.4','131010'] QMainWindow.__init__(self, parent) self.setWindowTitle('QtPyPlot') #Stuff for importing from datavault self.plotShow = 1 self.plotEnd = 5 self.subMem = False #Do I subtract memory trace from current trace? self.showMem = False #Do I show the memory trace? self.buffTrace = np.array([]) #array to store trace self.listDep = [] self.plots = np.arange(self.plotShow, self.plotEnd, 1) self.myData = DataVaultWrapper(self.path, self.cxn) # self.populate_browserList() self.dirList = self.myData.dir() # self.dirList.insert(0,'...') self.dep = 0 # self.plotList = ['...']+self.myData.keys() self.plotList = self.myData.keys() # print self.plotList # for ps in self.plots: # print "enqueue " + str(ps) # self.myData.enqueueId(int(ps)) # while not self.myData.cache.cache: # print "Loading..." # time.sleep(1) ### end of stuff for importing form datavault self.create_menu() self.create_main_frame() # self.create_status_bar() self.textbox.setText(str(self.path)) self.on_draw() def save_plot(self): file_choices = "PNG (*.png)|*.png" path = unicode( QFileDialog.getSaveFileName(self, 'Save file', '', file_choices)) if path: self.canvas.print_figure(path, dpi=self.dpi) self.statusBar().showMessage('Saved to %s' % path, 2000) def print_plot(self): dialog = QPrintDialog() # if dialog.exec_() == QDialog.Accepted: self.goPrinter() # self.canvas.print_figure(os.getcwd()+'temp.png',dpi=self.dpi).print_(dialog.printer()) # self.canvas.document().print_(dialog.printer()) def goPrinter(self): printer = QPrinter() printer.Letter printer.HighResolution printer.Color anotherWidget = QPrintDialog(printer, self) if (anotherWidget.exec_() != QDialog.Accepted): return p = QPixmap.grabWidget(self.canvas) printLabel = QLabel() printLabel.setPixmap(p) painter = QPainter(printer) printLabel.render(painter) painter.end() show() def on_about(self): msg = """ A demo of using PyQt with matplotlib: * Use the matplotlib navigation bar * Add values to the text box and press Enter (or click "Draw") * Show or hide the grid * Drag the slider to modify the width of the bars * Save the plot to a file using the File menu * Click on a bar to receive an informative message """ QMessageBox.about(self, "About the demo", msg.strip()) def on_pick(self, event): # The event received here is of the type # matplotlib.backend_bases.PickEvent # # It carries lots of information, of which we're using # only a small amount here. # box_points = event.artist.get_bbox().get_points() msg = "You've clicked on a bar with coords:\n %s" % box_points QMessageBox.information(self, "Click!", msg) # def add_plot(self): # arr = self.myData[self.plotShow] # self.axes.plot(arr[:,0],arr[:,2+self.dep]) # print "add plot" def on_draw(self): """ Redraws the figure """ # str = unicode(self.textbox.text()) # self.data = map(int, str.split()) arr = self.myData[self.plotShow] # x = range(len(self.data)) self.fig.clf() self.axes = self.fig.add_subplot(111) # self.axes.clear() self.axes.set_title(arr.name) self.setWindowTitle('QtPyPlot: ' + str(self.plotShow)) if len(arr.indep) == 2: indep0First = np.unique(arr[:, 0])[0] indep0Last = np.unique(arr[:, 0])[-1] indep1First = np.unique(arr[:, 1])[0] indep1Last = np.unique(arr[:, 1])[-1] ratio = (indep0Last - indep0First) / (indep1Last - indep1First) x = convertForImshow(self.myData[self.plotShow], sec=self.dep) # clear the axes and redraw the plot anew # self.axes.grid(self.grid_cb.isChecked()) implt = self.axes.imshow( x, aspect=ratio, extent=[indep0First, indep0Last, indep1First, indep1Last]) # implt.set_clim(0,30) self.axes.set_xlabel(arr.indep[0][0] + ' (' + arr.indep[0][1] + ')') self.axes.set_ylabel(arr.indep[1][0] + ' (' + arr.indep[0][1] + ')') self.cbar = self.fig.colorbar(implt) else: if self.subMem: arr[:, 1] = arr[:, 1] - self.buffTrace[:, 1] for ent in self.listDep: indep0First = np.unique(arr[:, 0])[0] indep0Last = np.unique(arr[:, 0])[-1] dep0First = np.unique(arr[:, 1 + ent])[0] dep0Last = np.unique(arr[:, 1 + ent])[-1] self.axes.set_xlabel(arr.indep[0][0] + ' (' + arr.indep[0][1] + ')') self.axes.set_ylabel(arr.dep[0 + ent][0] + ' (' + arr.dep[0 + ent][1] + ')') # if not self.subMem: # self.axes.plot(arr[:,0],arr[:,1+ent]) self.axes.plot(arr[:, 0], arr[:, 1 + ent]) if self.showMem: self.axes.plot(self.buffTrace[:, 0], self.buffTrace[:, 1]) # if self.subMem: # self.axes.plot(arr[:,0],arr[:,1+ent]-self.buffTrace[:,1]) # self.axes.axis([indep0First, indep0Last,dep0First,dep0Last]) self.canvas.draw() def on_next(self): self.plotShow = self.plotShow + 1 print self.plotShow self.on_draw() self.update_dep_list() def on_prev(self): if self.plotShow > 1: self.plotShow = self.plotShow - 1 self.on_draw() print self.plotShow self.update_dep_list() def on_save_trace(self): self.buffTrace = np.array([]) #re-initialize to empty, then add trace self.buffTrace = np.vstack( (self.myData[self.plotShow][:, 0].copy(), self.myData[self.plotShow][:, 1 + self.listDep[0]].copy())) self.buffTrace = self.buffTrace.T # print "Trace buffered: ", self.buffTrace[:,0], self.buffTrace[:,1] def on_sub_trace(self): self.subMem = True self.on_draw() self.subMem = False def on_plot_mem(self): self.showMem = True self.on_draw() self.showMem = False def on_browser_list(self, item): print str(item.text()) # strItem = str(item.text()) # numItem = [int(s) for s in strItem.split() if s.isdigit()] # print numItem # if item.text()=='[...]' and len(self.path)>1: if item.text() == '...' and len(self.path) > 1: self.path.pop() print self.path self.update_browse_list() self.depList.clear() # elif len(self.path)==1: # pass # else elif item.text() in self.plotList: # self.plotShow = self.myData.keys().index(item.text())+1 self.plotShow = int(str.split(str(item.text()))[0]) print self.plotShow self.update_dep_list() self.dep = 0 self.listDep = [] #re-initialize list of deps self.on_draw() elif str(item.text()) in self.dirList: print "It's a DEER" self.path = self.path + [str(item.text())] print self.path self.update_browse_list() # print self.plotShow def on_dep_list(self, item): # print item.checkState() # If item is checked, that means it was changed from unchecked, which means is has to be # added to the array prop1, prop2 = str(item.text()).split(' vs. ')[0].split(';')[0], str( item.text()).split(' vs. ')[0].split(';')[1] # print prop1,prop2 for ind, ent in enumerate(self.myData[self.plotShow].dep): if prop1 in ent: if prop2 in ent: self.dep = ind # print "the dep is: ", ind if item.checkState() == 2: self.listDep.append(ind) if item.checkState() == 0: self.listDep.remove(ind) print "list of deps ", self.listDep self.on_draw() # Item has gone to unchecked, need to be removed from # print item(index) # dep = str.split(str(item.text()))[0] def on_dep_active(self, item): print "item selection changed " + item.text() def new_func(self): print "new signal rec'd" def create_main_frame(self): self.main_frame = QWidget() # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.dpi = 100 self.fig = Figure((5.0, 4.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) # Since we have only one plot, we can use add_axes # instead of add_subplot, but then the subplot # configuration tool in the navigation toolbar wouldn't # work. # # self.axes = self.fig.add_subplot(111) # Bind the 'pick' event for clicking on one of the bars # self.canvas.mpl_connect('pick_event', self.on_pick) # Create the navigation toolbar, tied to the canvas # self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # Other GUI controls # self.textbox = QLineEdit() self.textbox.setMinimumWidth(200) self.connect(self.textbox, SIGNAL('editingFinished ()'), self.on_draw) self.prev_button = QPushButton("&Prev") self.connect(self.prev_button, SIGNAL('clicked()'), self.on_prev) self.next_button = QPushButton("&Next") self.connect(self.next_button, SIGNAL('clicked()'), self.on_next) # Trace memory operations # self.save_button = QPushButton("Trace->Mem") self.connect(self.save_button, SIGNAL('clicked()'), self.on_save_trace) self.plot_mem_button = QPushButton("&Show Mem") self.connect(self.plot_mem_button, SIGNAL('clicked()'), self.on_plot_mem) self.sub_button = QPushButton("&Trace - Mem") self.connect(self.sub_button, SIGNAL('clicked()'), self.on_sub_trace) self.grid_cb = QCheckBox("Show &Grid") self.grid_cb.setChecked(False) self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.on_draw) # slider_label = QLabel('Bar width (%):') # self.slider = QSlider(Qt.Horizontal) # self.slider.setRange(1, 100) # self.slider.setValue(20) # self.slider.setTracking(True) # self.slider.setTickPosition(QSlider.TicksBothSides) # self.connect(self.slider, SIGNAL('valueChanged(int)'), self.on_draw) # # Make the list of files in the dataVault directory # self.browserList = QListWidget() self.browserList.setMinimumWidth(350) self.depList = QListWidget() self.depList.setMinimumWidth(100) # self.browserList.setMinimumWidth(300) self.populate_browserList() # for entry in self.plotList: # self.browserList.addItem(entry) self.connect(self.browserList, SIGNAL("itemDoubleClicked(QListWidgetItem*)"), self.on_browser_list) # self.connect(self.depList,SIGNAL("itemClicked(QListWidgetItem*)"),self.on_dep_list) self.connect(self.depList, SIGNAL("itemChanged(QListWidgetItem*)"), self.on_dep_list) # self.connect(self.depList,SIGNAL("itemClicked(QListWidgetItem*)"),self.on_dep_active) # # Layout with box sizers # hbox = QHBoxLayout() hboxmem = QHBoxLayout() hboxPrime = QHBoxLayout() for w in [self.textbox, self.prev_button, self.next_button]: hbox.addWidget(w) hbox.setAlignment(w, Qt.AlignVCenter) for w in [self.save_button, self.sub_button, self.plot_mem_button]: hboxmem.addWidget(w) hboxmem.setAlignment(w, Qt.AlignVCenter) vbox = QVBoxLayout() # vbox.addWidget(self.browserList) vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) vbox.addLayout(hboxmem) hboxPrime.addLayout(vbox) hboxPrime.addWidget(self.browserList) hboxPrime.addWidget(self.depList) self.main_frame.setLayout(hboxPrime) self.setCentralWidget(self.main_frame) # Defining some new signals self.new_signal.connect(self.new_func) def mousePressEvent(self, event): self.new_signal.emit() if event.button() == Qt.RightButton: print "the right mouse button was pressed.. somewhere" # def create_status_bar(self): # self.status_text = QLabel("We can write some status here") # self.statusBar().addWidget(self.status_text, 1) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") load_file_action = self.create_action("&Save plot", shortcut="Ctrl+S", slot=self.save_plot, tip="Save the plot") print_action = self.create_action("&Print plot", slot=self.print_plot, tip="Print the plot") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (load_file_action, print_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action, )) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def update_browse_list(self): self.textbox.setText(str(self.path)) self.myData = DataVaultWrapper(self.path, self.cxn) self.dirList = self.myData.dir() if len(self.path) > 1: self.dirList.insert(0, '...') self.plotList = self.myData.keys() self.populate_browserList() print "dirList =", self.dirList def update_dep_list(self): self.depList.clear() for entry in self.myData[self.plotShow].dep: title = entry[0] + ';' + entry[1] + ' vs. ' + self.myData[ self.plotShow].indep[0][0] itm = QListWidgetItem(title) itm.setCheckState(False) # print title self.depList.addItem(itm) def populate_browserList(self): self.browserList.clear() for entry in self.dirList: itm = QListWidgetItem(entry) itm.setIcon(QIcon('tick.png')) self.browserList.addItem(itm) for entry in self.plotList: self.browserList.addItem(entry) self.browserList.update() def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
class PerceptronDemo(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) self.setWindowTitle('Perceptron Demo') self.frame = QtGui.QWidget() self.create_plot() self.layout_window() self.create_status_bar() self.setGeometry(50, 50, 680, 600) self.data = [] self.data = [] self.total_epochs = 0 self.net = SingleNeuronPerceptron() def create_plot(self): self.plot_figure = Figure((6.0, 6.0), dpi=100) self.plot_canvas = FigureCanvas(self.plot_figure) self.plot_canvas.setParent(self.frame) # Add a plot self.axes = self.plot_figure.add_subplot(111) self.plot_figure.subplots_adjust(bottom=0.2, left=0.1) self.axes.set_xlim(0, 10) self.axes.set_ylim(0, 10) self.axes.tick_params(labelsize=8) self.axes.set_xlabel("$p^1$", fontsize=10) self.axes.set_ylabel("$p^2$", fontsize=10) self.pos_line, = self.axes.plot([], 'mo', label="Positive Class") self.neg_line, = self.axes.plot([], 'cs', label="Negative Class") self.decision, = self.axes.plot([], 'r-', label="Decision Boundary") self.axes.legend(loc='lower center', fontsize=8, framealpha=0.9, numpoints=1, ncol=3, bbox_to_anchor=(0, -.24, 1, -.280), mode='expand') self.axes.set_title("Single Neuron Perceptron") self.plot_canvas.draw() # Add event handler for a mouseclick in the plot self.plot_canvas.mpl_connect('button_press_event', self.on_mouseclick) def create_status_bar(self): self.current_status = QtGui.QLabel("Starting") self.statusBar().addWidget(self.current_status, 1) def layout_window(self): explain = QtGui.QLabel( "On the plot, click the: <ul><li><b>Primary mouse button</b> to add a positive class observation</li>" + "<li><b>Secondary mouse button</b> to add a negative class observation</li></ul>\n" + "Then click <b>Run</b> to start a training run. The training run will stop when the error goes to 0 or when the epoch limit is reached." ) epoch_font = QtGui.QFont("Courier", 14, QtGui.QFont.Bold) epoch_label = QtGui.QLabel("Epochs so far:") epoch_label.setFixedHeight(20) self.epoch_display = QtGui.QLabel("---") self.epoch_display.setFixedHeight(25) self.epoch_display.setFont(epoch_font) error_label = QtGui.QLabel("Error:") error_label.setFixedHeight(20) self.error_display = QtGui.QLabel("---") self.error_display.setFixedHeight(25) self.error_display.setFont(epoch_font) epr_label = QtGui.QLabel("Max Epochs per run:") self.epochs_per_run = QtGui.QComboBox() self.epochs_per_run.addItems(["1", "10", "100", "1000"]) self.epochs_per_run.setCurrentIndex(2) self.run_button = QtGui.QPushButton("Run") self.connect(self.run_button, QtCore.SIGNAL('clicked()'), self.on_run) self.rerun_button = QtGui.QPushButton("Reset to Start") self.connect(self.rerun_button, QtCore.SIGNAL('clicked()'), self.on_reset) self.undo_click_button = QtGui.QPushButton("Undo Last Mouse Click") self.connect(self.undo_click_button, QtCore.SIGNAL('clicked()'), self.on_undo_mouseclick) self.clear_button = QtGui.QPushButton("Clear Data") self.connect(self.clear_button, QtCore.SIGNAL('clicked()'), self.on_clear) # Control control_panel = QtGui.QVBoxLayout() for w in (epoch_label, self.epoch_display, error_label, self.error_display, epr_label, \ self.epochs_per_run, self.run_button, self.rerun_button, self.undo_click_button, \ self.clear_button): control_panel.addWidget(w) #control_panel.setAlignment(w, QtCore.Qt.AlignHCenter) control_panel.addStretch(10) # Plot plot_panel = QtGui.QVBoxLayout() #plot_panel.addWidget(explain) plot_panel.addWidget(self.plot_canvas) # Main window hbox = QtGui.QHBoxLayout() hbox.addLayout(plot_panel) hbox.addLayout(control_panel) vbox = QtGui.QVBoxLayout() vbox.addLayout(hbox) vbox.addWidget(explain) self.frame.setLayout(vbox) self.setCentralWidget(self.frame) def draw_data(self): self.pos_line.set_data([x[0] for x in self.data if x[2] == POS], [y[1] for y in self.data if y[2] == POS]) self.neg_line.set_data([x[0] for x in self.data if x[2] == NEG], [y[1] for y in self.data if y[2] == NEG]) self.plot_canvas.draw() def draw_decision_boundary(self): lim = self.axes.get_xlim() X = np.linspace(lim[0], lim[1], 101) Y = self.net.find_decision_boundary(X) self.decision.set_data(X, Y) self.plot_canvas.draw() def clear_decision_boundary(self): self.decision.set_data([], []) self.plot_canvas.draw() def on_mouseclick(self, event): """Add an item to the plot""" if event.xdata != None and event.xdata != None: self.data.append( (event.xdata, event.ydata, POS if event.button == 1 else NEG)) self.draw_data() self.current_status.setText("x={0:0.2f} y={1:0.2f}".format( event.xdata, event.ydata)) def on_clear(self): self.data = [] self.clear_decision_boundary() self.net.initialize_weights() self.total_epochs = 0 self.update_run_status() self.draw_data() def update_run_status(self): if self.total_epochs == 0: self.epoch_display.setText("---") self.error_display.setText("---") else: self.epoch_display.setText(str(self.total_epochs)) self.error_display.setText(str(self.total_error)) def on_run(self): # Do 10 epochs for epoch in range(int(self.epochs_per_run.currentText())): self.total_epochs += 1 training = self.data.copy() np.random.shuffle(training) for d in training: self.net.train_one_iteration(np.array(d[0:2]), d[2]) # Calculate the error for the epoch self.all_t_hat = np.array( [self.net.run_forward(np.array(xy[0:2])) for xy in self.data]) self.total_error = abs( np.array([t[2] for t in self.data]) - self.all_t_hat).sum() if self.total_error == 0: break # print("Epoch:", self.total_epochs, "Error is:", total_error) self.current_status.setText("Epoch: {0} Error: {1}".format( self.total_epochs, self.total_error)) self.update_run_status() self.draw_decision_boundary() def on_reset(self): self.net.initialize_weights() self.total_epochs = 0 self.update_run_status() self.clear_decision_boundary() def on_undo_mouseclick(self): if len(self.data) > 1: self.data.pop() self.draw_data()
class MainWindow(QtWidgets.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.central_widget = QtWidgets.QWidget(self) self.setCentralWidget(self.central_widget) self.setWindowTitle('Spectral Expander') self.file_src = "" # self.freqs = None self.spectra = [] self.fft_size = 512 self.fft_hop = self.fft_size // 8 self.marker_freqs = [] self.marker_dBs = [] self.ratios = [] self.vol_curves = [] self.cfg = config.read_config("config.ini") self.cb = QtWidgets.QApplication.clipboard() # a figure instance to plot on self.fig, self.ax = plt.subplots(nrows=1, ncols=1) self.ax.set_xlabel('Frequency (Hz)') self.ax.set_ylabel('Volume (dB)') # the range is not automatically fixed self.fig.patch.set_facecolor((53/255, 53/255, 53/255)) self.ax.set_facecolor((35/255, 35/255, 35/255)) # this is the Canvas Widget that displays the `figure` # it takes the `fig` instance as a parameter to __init__ self.canvas = FigureCanvas(self.fig) self.canvas.mpl_connect('button_press_event', self.onclick) # this is the Navigation widget # it takes the Canvas widget and a parent self.toolbar = NavigationToolbar(self.canvas, self) # Just some button connected to `plot` method self.file_widget = widgets.FilesWidget(self, 1, self.cfg) self.file_widget.on_load_file = self.open_file self.b_expand = QtWidgets.QPushButton('Expand') self.b_expand.setToolTip("Write expanded audio to a new file.") self.b_expand.clicked.connect(self.expand) self.s_band_lower = QtWidgets.QSpinBox() self.s_band_lower.valueChanged.connect(self.plot) self.s_band_lower.setRange(0, 22000) self.s_band_lower.setSingleStep(1000) self.s_band_lower.setValue(13000) self.s_band_lower.setToolTip("Lower frequency boundary of noise floor") self.s_band_upper = QtWidgets.QSpinBox() self.s_band_upper.valueChanged.connect(self.plot) self.s_band_upper.setRange(1000, 22000) self.s_band_upper.setSingleStep(1000) self.s_band_upper.setValue(17000) self.s_band_upper.setToolTip("Upper frequency boundary of noise floor") self.s_clip_lower = QtWidgets.QSpinBox() self.s_clip_lower.valueChanged.connect(self.plot) self.s_clip_lower.setRange(-200, 0) self.s_clip_lower.setSingleStep(1) self.s_clip_lower.setValue(-120) self.s_clip_lower.setToolTip("Lower gain boundary of noise floor") self.s_clip_upper = QtWidgets.QSpinBox() self.s_clip_upper.valueChanged.connect(self.plot) self.s_clip_upper.setRange(-200, 0) self.s_clip_upper.setSingleStep(1) self.s_clip_upper.setValue(-85) self.s_clip_upper.setToolTip("Upper gain boundary of noise floor") self.c_channels = QtWidgets.QComboBox(self) self.c_channels.addItems(list(("L,R","L","R","Mean"))) self.c_channels.setToolTip("Which channels should be analyzed?") tolerance_l = QtWidgets.QLabel("Tolerance") self.s_smoothing = QtWidgets.QDoubleSpinBox() self.s_smoothing.setRange(.001, 5) self.s_smoothing.setSingleStep(.01) self.s_smoothing.setValue(.11) self.s_smoothing.setToolTip("Smoothing in s.") self.l_result = QtWidgets.QLabel("Result: ") self.qgrid = QtWidgets.QGridLayout() # self.qgrid.setHorizontalSpacing(0) self.qgrid.setVerticalSpacing(0) self.qgrid.addWidget(self.toolbar, 0, 0, 1, 8) self.qgrid.addWidget(self.canvas, 1, 0, 1, 8) self.qgrid.addWidget(self.file_widget, 2, 0) self.qgrid.addWidget(self.b_expand, 2, 1) self.qgrid.addWidget(self.c_channels, 2, 2) self.qgrid.addWidget(self.s_band_lower, 2, 3) self.qgrid.addWidget(self.s_band_upper, 2, 4) self.qgrid.addWidget(self.s_clip_lower, 2, 5) self.qgrid.addWidget(self.s_clip_upper, 2, 6) self.qgrid.addWidget(self.s_smoothing, 2, 7) self.central_widget.setLayout(self.qgrid) self.s_band_lower.valueChanged.connect(self.on_param_changed) self.s_band_upper.valueChanged.connect(self.on_param_changed) self.s_clip_lower.valueChanged.connect(self.on_param_changed) self.s_clip_upper.valueChanged.connect(self.on_param_changed) self.s_smoothing.valueChanged.connect(self.on_param_changed) self.c_channels.currentIndexChanged.connect(self.update_spectrum) def on_param_changed(self,): self.vol_curves = [] band_lower = self.s_band_lower.value() band_upper = self.s_band_upper.value() # clip_lower = self.s_clip_lower.value() # clip_upper = self.s_clip_upper.value() # sample over an uneven number of points in volume curve smoothing = make_odd( int(self.s_smoothing.value() * self.sr / self.fft_hop) ) # update volume curve if self.spectra: num_bins, last_fft_i = self.spectra[0].shape def freq2bin(f): return max(1, min(num_bins-3, int(round(f * self.fft_size / self.sr))) ) bL = freq2bin(band_lower) bU = freq2bin(band_upper) for i, spectrum in enumerate(self.spectra): dBs = np.nanmean(spectrum[bL:bU,: ], axis=0) dBs = savgol_filter(dBs, smoothing, 2) self.vol_curves.append(dBs) self.plot() def open_file(self, filepaths): for filepath in filepaths: self.file_src = filepath self.update_spectrum() break def update_spectrum(self,): if self.file_src: self.spectra, self.sr = spectrum_from_audio(self.file_src, self.fft_size, self.fft_hop, self.c_channels.currentText()) # get the time stamp at which each fft is taken self.t = np.arange(0, self.fft_hop * len(self.spectra[0][0]), self.fft_hop) / self.sr self.on_param_changed() def onclick(self, event): """ Update dB bounds on right click""" if event.xdata and event.ydata: #right click if event.button == 3: clip_new = round(event.ydata) # print(clip_new) clip_lower = self.s_clip_lower.value() clip_upper = self.s_clip_upper.value() middle = (clip_lower+ clip_upper) / 2 if clip_new > middle: self.s_clip_upper.setValue(clip_new) else: self.s_clip_lower.setValue(clip_new) def expand(self,): if self.file_src: print("Resampling...") # get input clip_lower = self.s_clip_lower.value() clip_upper = self.s_clip_upper.value() signal, sr, channels = io_ops.read_file(self.file_src) for channel_i in range(channels): # map curve to channel output if channel_i < len(self.vol_curves): dBs = self.vol_curves[channel_i] else: dBs = self.vol_curves[-1] # clip dB curve clipped = np.clip(dBs, clip_lower, clip_upper) dB_diff = clip_upper - clipped fac = units.to_fac(dB_diff) # create factor for each sample final_fac = np.interp( np.arange(len(signal)), self.t*sr, fac) signal[:,channel_i] *= final_fac signal = units.normalize(signal) io_ops.write_file(self.file_src, signal, sr, channels, "decompressed") def plot(self): # discards the old graph self.ax.clear() if self.spectra: # draw clipped curves for clipped in self.vol_curves: self.ax.plot(self.t, clipped, linewidth=0.5, alpha=0.85) # draw bounds for bt in (self.s_clip_lower, self.s_clip_upper): v = bt.value() self.ax.plot((self.t[0], self.t[-1]), (v,v), linestyle="--", color="red", linewidth=0.5, alpha=0.85) self.ax.legend(self.c_channels.currentText().split(","), loc='upper left') self.ax.set_xlabel('Time [s]') self.ax.set_ylabel('Input [dB]') # refresh canvas self.canvas.draw()
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Mandelbrot Set') self.create_menu() self.create_main_frame() self.create_status_bar() # # Initialize textbox values # self.textbox_re_min.setText(str(re_min)) self.textbox_re_max.setText(str(re_max)) self.textbox_im_min.setText(str(im_min)) self.textbox_im_max.setText(str(im_max)) self.textbox_max_iter.setText(str(max_iter)) # # Render mandelbrot set # self.setMinimumWidth(620) self.resize(620, 460) self.draw() def save_plot(self): file_choices = "PNG (*.png)|*.png" path = unicode(QFileDialog.getSaveFileName(self, 'Save file', '', file_choices)) if path: self.canvas.print_figure(path) self.statusBar().showMessage('Saved to %s' % path, 2000) # # Display infos about application # def on_about(self): msg = """Mandelbrot Set Generator: ### Features ### * Click left mouse button and drag to zoom * Enter custom values for ReMin, ReMin, ImMin and ImMax * Show or hide the grid * Save the plot to a file using the File menu * De-/activate continuous color spectrum * De-/activate normalized values ### Used Libraries ### * PyQt4 * Matplotlib ### Author ### Made by Philip Wiese [email protected] 16. Oktober 2016 """ QMessageBox.about(self, "About the demo", msg.strip()) # # Show mouse position in statusbar # def statusbar_coord(self, event): # Show coordinates time in statusbar if event.inaxes is not None: text = "Re(c): % .5f, Im(c) % .5f" % (event.xdata, event.ydata) self.coord_text.setText(text) # # Calculates mandelbrot set and updates mpl plot # def draw(self): """ Redraws the figure """ # Grap values from textboxes re_min = float(unicode(self.textbox_re_min.text())) re_max = float(unicode(self.textbox_re_max.text())) im_min = float(unicode(self.textbox_im_min.text())) im_max = float(unicode(self.textbox_im_max.text())) max_iter = int(unicode(self.textbox_max_iter.text())) # Grap values from checkboxes self.axes.grid(self.grid_cb.isChecked()) cont = self.cont_cb.isChecked() norm = self.norm_cb.isChecked() # Calculate mandelbrot set self.fractal = mandelbrot(re_min, re_max, im_min, im_max, max_betr, max_iter, res, cont) # Normalize Values if norm: self.fractal.data[self.fractal.data > 0] -= self.fractal.min # Show calculation time in statusbar self.status_text.setText("Calculation Time: %0.3fs" % self.fractal.calc_time) # Load data to mpl plot self.axes.imshow(self.fractal.data.T, origin="lower left", cmap='jet', extent=[re_min, re_max, im_min, im_max]) self.axes.set_xlabel("Re(c)", labelpad=20) self.axes.set_ylabel("Im(c)") # Show/hide grid if self.grid_cb.isChecked(): self.axes.grid(linewidth=1, linestyle='-') # Align layout and redraw plot self.canvas.draw_idle() #self.fig.tight_layout() def line_select_callback(self, eclick, erelease): # eclick and erelease are the press and release events x1, y1 = eclick.xdata, eclick.ydata x2, y2 = erelease.xdata, erelease.ydata # Zoom with left mouse click if eclick.button == 1: # Check for valid coordinates if (x1 != None and y2 != None and x1 != None and y1 != None): self.xmin = min(x1, x2) self.xmax = max(x1, x2) self.ymin = min(y1, y2) self.ymax = max(y1, y2) # Save array with relative values self.xy = [self.xmax - self.xmin, self.ymax - self.ymin] # Calculate precision in decimal digits for v in self.xy: if v <= 1: self.decimals = round(log10(1 / v)) + 2 # Round values with calculated precision re_min = round(self.xmin, int(self.decimals)) re_max = round(self.xmax, int(self.decimals)) im_min = round(self.ymin, int(self.decimals)) im_max = round(self.ymax, int(self.decimals)) # Update textbos values self.textbox_re_min.setText(str(re_min)) self.textbox_re_max.setText(str(re_max)) self.textbox_im_min.setText(str(im_min)) self.textbox_im_max.setText(str(im_max)) # Calculate and draw new mandelbrot set self.draw() # Zoom with right mouse click if eclick.button == 3: # Grap values from textboxes re_min = float(unicode(self.textbox_re_min.text())) re_max = float(unicode(self.textbox_re_max.text())) im_min = float(unicode(self.textbox_im_min.text())) im_max = float(unicode(self.textbox_im_max.text())) self.xy = [ re_max - re_min, im_max - im_min] # Calculate new values re_min = re_min - self.xy[0] / 2 re_max = re_max + self.xy[0] / 2 im_min = im_min - self.xy[1] / 2 im_max = im_max + self.xy[1] / 2 # Calculate precision in decimal digits for v in self.xy: if v <= 1: self.decimals = round(log10(1 / v)) + 2 # Round values with calculated precision re_min = round(re_min, int(self.decimals)) re_max = round(re_max, int(self.decimals)) im_min = round(im_min, int(self.decimals)) im_max = round(im_max, int(self.decimals)) # Update textbos values self.textbox_re_min.setText(str(re_min)) self.textbox_re_max.setText(str(re_max)) self.textbox_im_min.setText(str(im_min)) self.textbox_im_max.setText(str(im_max)) # Calculate and draw new mandelbrot set self.draw() def create_main_frame(self): self.main_frame = QWidget() self.main_frame.setMinimumHeight(280) # Create the Figure and FigCanvas objects self.fig = Figure((5,10), tight_layout=True) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) # Add sublot to figure do formatting self.axes = self.fig.add_subplot(111) self.axes.ticklabel_format(style='sci', scilimits=(0,0), axis='both') # Create zoom event handler self.RS = RectangleSelector(self.axes, self.line_select_callback, drawtype='box', useblit=True, button=[1, 3], # don't use middle button spancoords='data') # Other GUI controls self.textbox_re_min = QLineEdit() self.textbox_re_min_text = QLabel("ReMin: ") self.textbox_re_min.setMinimumWidth(55) self.textbox_re_max = QLineEdit() self.textbox_re_max_text = QLabel("ReMax: ") self.textbox_re_max.setMinimumWidth(55) self.textbox_im_min = QLineEdit() self.textbox_im_min_text = QLabel("ImMin: ") self.textbox_im_min.setMinimumWidth(55) self.textbox_im_max = QLineEdit() self.textbox_im_max_text = QLabel("ImMax: ") self.textbox_im_max.setMinimumWidth(55) self.textbox_max_iter = QLineEdit() self.textbox_max_iter_text = QLabel("Max Iterration: ") self.textbox_max_iter.setMinimumWidth(55) self.grid_cb = QCheckBox("Show Grid") self.grid_cb.setChecked(False) self.cont_cb = QCheckBox("Continuous Coloring") self.cont_cb.setChecked(True) self.norm_cb = QCheckBox("Normalize Values") self.norm_cb.setChecked(True) self.draw_button = QPushButton("Calculate && Draw") self.connect(self.draw_button, SIGNAL('clicked()'), self.draw) # # Layout with box sizers # hbox = QHBoxLayout() grid = QGridLayout() hbox.addWidget(self.canvas, 3) self.canvas.setCursor(Qt.CrossCursor) hbox.addLayout(grid,1) grid.setRowStretch(1,1) grid.addWidget(self.textbox_re_min , 0,1) grid.addWidget(self.textbox_re_min_text , 0,0) grid.addWidget(self.textbox_re_max , 1,1) grid.addWidget(self.textbox_re_max_text , 1,0) grid.addWidget(self.textbox_im_min , 2,1) grid.addWidget(self.textbox_im_min_text , 2,0) grid.addWidget(self.textbox_im_max , 3,1) grid.addWidget(self.textbox_im_max_text , 3,0) grid.addWidget(self.textbox_max_iter , 5,1) grid.addWidget(self.textbox_max_iter_text , 5,0) grid.addWidget(self.grid_cb , 6,0,1,2) grid.addWidget(self.cont_cb , 7,0,1,2) grid.addWidget(self.norm_cb , 8,0,1,2) grid.addWidget(self.draw_button , 9,0,1,2) grid.addWidget(QLabel(""), 10,0,2,2) self.main_frame.setLayout(hbox) self.setCentralWidget(self.main_frame) def create_status_bar(self): self.status_text = QLabel("Ready") self.coord_text = QLabel("Re(c): % 7f, Im(c) % 7f" % (0, 0)) self.canvas.mpl_connect("motion_notify_event", self.statusbar_coord) self.statusBar().addWidget(self.status_text, 1) self.statusBar().addWidget(self.coord_text, -1) def create_menu(self): # -- Menu Structure -- # File # Save plot (Ctrl+S) # Quit (Ctrl+Q) # Help # About (F1) # self.file_menu = self.menuBar().addMenu("&File") load_file_action = self.create_action("&Save plot", shortcut="Ctrl+S", slot=self.save_plot, tip="Save the plot") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (load_file_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the application') self.add_actions(self.help_menu, (about_action,)) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
class MatplotlibExample(QtGui.QMainWindow): """HOW TO EMBED MATPLOTLIB WITH PYSIDE""" here = os.path.abspath(os.path.join( os.path.dirname(__file__))) # to be overloaded media = os.path.abspath(os.path.join(here, "media")) def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) self.x = None self.y = None self.last_idx = None # matplotlib stuff self.dpi = 200 self.figure = None self.canvas = None self.axes = None self.mpl_toolbar = None # PySide stuff self.main_frame = None self.file_menu = None self.help_menu = None self.textbox = None self.draw_button = None self.grid_ck = None self.slider = None # create ui self.setWindowTitle('PySide with matplotlib') self.center() self.create_menu() self.create_main_frame() self.on_draw() def create_menu(self): """Menu creation""" # file menu self.file_menu = self.menuBar().addMenu("&File") load_file_action = self.create_action("&Save plot", shortcut="Ctrl+S", slot=self.save_plot, tip="Save the plot") self.file_menu.addAction(load_file_action) quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the app") self.file_menu.addAction(quit_action) # help menu self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About this app') self.help_menu.addAction(about_action) def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): """Helper function to create an action""" action = QtGui.QAction(text, self) if icon is not None: action.setIcon( QtGui.QIcon(os.path.join(self.media, "%s.png" % icon))) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) if slot is not None: action.triggered.connect(slot) if checkable: action.setCheckable(True) return action def create_main_frame(self): self.main_frame = QtGui.QWidget() # Create Matplotlib figure and canvas self.figure = Figure((6.0, 4.0), dpi=self.dpi) # inches, dots-per-inch self.canvas = FigureCanvas(self.figure) self.canvas.setParent(self.main_frame) self.canvas.setFocusPolicy( QtCore.Qt.ClickFocus) # key for press events!!! self.canvas.setFocus() # we use add_subplot (so that the subplot configuration tool in the navigation toolbar works) self.axes = self.figure.add_subplot(111) # Bind events self.canvas.mpl_connect('pick_event', self.on_pick) self.canvas.mpl_connect('button_press_event', self.on_mouse_press) self.canvas.mpl_connect('key_press_event', self.on_key_press) # Create the navigation toolbar, tied to the canvas self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # other GUI controls self.textbox = QtGui.QLineEdit() self.textbox.setMinimumWidth(200) self.textbox.editingFinished.connect(self.on_draw) self.textbox.setText('1 3 2 6 3 2 4') self.draw_button = QtGui.QPushButton("&Draw") self.draw_button.clicked.connect(self.on_draw) # grid self.grid_ck = QtGui.QCheckBox("Show &Grid") self.grid_ck.setChecked(False) self.grid_ck.stateChanged.connect(self.on_draw) # slider slider_label = QtGui.QLabel('Plot width (%):') self.slider = QtGui.QSlider(QtCore.Qt.Horizontal) self.slider.setRange(1, 100) self.slider.setValue(20) self.slider.setTracking(True) self.slider.setTickPosition(QtGui.QSlider.TicksBothSides) self.slider.valueChanged.connect(self.on_draw) # layouts hbox = QtGui.QHBoxLayout() for w in [ self.textbox, self.draw_button, self.grid_ck, slider_label, self.slider ]: hbox.addWidget(w) hbox.setAlignment(w, QtCore.Qt.AlignVCenter) vbox = QtGui.QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) def center(self): qr = self.frameGeometry() cp = QtGui.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def save_plot(self): flt = "PNG (*.png)|*.png" path = QtGui.QFileDialog.getSaveFileName(self, 'Save file', '', flt) if path: self.canvas.print_figure(path, dpi=self.dpi) def on_about(self): msg = """PySide with matplotlib: - navigation bar - grid toggle - interactivity ('Draw' button, slider, click on bar) - plot saving """ QtGui.QMessageBox.about(self, "About the demo", msg.strip()) def on_pick(self, event): """Manage pick event""" if event.ind is None: return m_x = event.mouseevent.xdata m_y = event.mouseevent.ydata msg = "Click event:\n" msg += "ind: %s\n" % event.ind msg += "mouse.xdata/ydata: %s, %s\n" % (m_x, m_y) # click location msg += "xydata:%s\n" % event.artist.get_xydata() print(msg) # in case of multiple selection distances = np.hypot(m_x - self.x[event.ind], m_y - self.y[event.ind]) idx_min = distances.argmin() self.last_idx = event.ind[idx_min] self.update_plot() def on_key_press(self, event): """Manage press event""" print("pressed key: %s" % event.key) def on_mouse_press(self, event): """Manage press event""" print("pressed mouse: %s" % event.key) def on_draw(self): """Redraws the figure""" self.y = [int(d) for d in self.textbox.text().split()] self.x = range(len(self.y)) # clear the axes and redraw self.axes.clear() self.axes.grid(self.grid_ck.isChecked()) self.axes.plot(self.x, self.y, linewidth=self.slider.value() / 100.0, alpha=0.5, picker=3) self.canvas.draw() def update_plot(self): if self.last_idx is None: return print("update") self.on_draw() self.axes.text(self.x[self.last_idx], self.y[self.last_idx], 'x=%1.3f\ny=%1.3f' % (self.x[self.last_idx], self.y[self.last_idx]), va='top') self.canvas.draw()
class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Epi') self.create_menu() self.create_main_frame() #self.create_status_bar() #self.Figura = Figura(input_from_file('test'), 'test') self.Figura = Figura(epi_examples.BATMAN_LIST, 'Batman') #self.textbox.setText('1 2 3 4') self.on_draw() def open_file(self): file_choices = "" self.file_dialog = QFileDialog() filename = self.file_dialog.getOpenFileName(self,'Open file', '', file_choices) self.Figura = Figura(input_from_file(filename), "Test") self.connect(self.file_dialog, SIGNAL("fileSelected(QString)"), self.on_draw) #filename = unicode(QFileDialog.getOpenFileName(self, #'Open file', '', #file_choices)) #self.connect(QFileDialog, SIGNAL("currentChanged(QString)"), self.on_draw) self.on_draw() def save_plot(self): file_choices = "PNG (*.png)|*.png" path = unicode(QFileDialog.getSaveFileName(self, 'Save file', '', file_choices)) if path: self.canvas.print_figure(path, dpi=self.dpi) self.statusBar().showMessage('Saved to %s' % path, 2000) def on_about(self): msg = """ A demo of using PyQt with matplotlib: * Use the matplotlib navigation bar * Add values to the text box and press Enter (or click "Draw") * Show or hide the grid * Drag the slider to modify the width of the bars * Save the plot to a file using the File menu * Click on a bar to receive an informative message """ QMessageBox.about(self, "About the demo", msg.strip()) def on_pick(self, event): # The event received here is of the type # matplotlib.backend_bases.PickEvent # # It carries lots of information, of which we're using # only a small amount here. # box_points = event.artist.get_bbox().get_points() msg = "You've clicked on a bar with coords:\n %s" % box_points QMessageBox.information(self, "Click!", msg) def on_draw(self): """ Redraws the figure """ #str = unicode(self.textbox.text()) #self.data = map(int, str.split()) #x = range(len(self.data)) # clear the axes and redraw the plot anew # #if (self.slider.value() == 20): #self.Figura = Figura(input_from_file('test'), 'test') #elif (self.slider.value() == 1): #self.Figura = Figura(epi_examples.BATMAN_LIST, 'Batman') self.fig_plot.clear() self.fig_plot.grid(self.grid_cb.isChecked()) #self.fig_plot.bar( #left=x, #height=self.data, #width=self.slider.value() / 100.0, #align='center', #alpha=0.44, #picker=5) self.fig_plot.plot( numpy.real(self.Figura.sampled_f), numpy.imag(self.Figura.sampled_f), numpy.real(self.Figura.lista), numpy.imag(self.Figura.lista), 'ro') xbounds = self.fig_plot.get_xbound() ybounds = self.fig_plot.get_ybound() self.fig_plot.axis([xbounds[0], xbounds[1], ybounds[0]*1.1, ybounds[1]*1.5]) self.fig_plot.legend((r'$f(z)$', 'Points'), 'upper center', shadow=True) self.fig_plot.set_title(self.Figura.title) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.dpi = 100 self.fig = Figure((8.0, 8.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) # Since we have only one plot, we can use add_axes # instead of add_subplot, but then the subplot # configuration tool in the navigation toolbar wouldn't # work. # #self.fig_plot = self.fig.add_subplot(111) self.fig_plot = self.fig.add_subplot(111, aspect='equal') # Bind the 'pick' event for clicking on one of the bars # self.canvas.mpl_connect('pick_event', self.on_pick) # Create the navigation toolbar, tied to the canvas # self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # Other GUI controls # #self.textbox = QLineEdit() #self.textbox.setMinimumWidth(200) #self.connect(self.textbox, SIGNAL('editingFinished ()'), self.on_draw) #self.draw_button = QPushButton("&Draw") #self.connect(self.draw_button, SIGNAL('clicked()'), self.on_draw) self.grid_cb = QCheckBox("Show &Grid") self.grid_cb.setChecked(False) self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.on_draw) #slider_label = QLabel('Bar width (%):') #self.slider = QSlider(Qt.Horizontal) #self.slider.setRange(1, 100) #self.slider.setValue(20) #self.slider.setTracking(True) #self.slider.setTickPosition(QSlider.TicksBothSides) #self.connect(self.slider, SIGNAL('valueChanged(int)'), self.on_draw) # # Layout with box sizers # hbox = QHBoxLayout() #for w in [ self.textbox, self.draw_button, self.grid_cb, #slider_label, self.slider]: #hbox.addWidget(w) #hbox.setAlignment(w, Qt.AlignVCenter) hbox.addWidget(self.grid_cb) hbox.setAlignment(self.grid_cb, Qt.AlignVCenter) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.addLayout(hbox) self.main_frame.setLayout(vbox) self.setCentralWidget(self.main_frame) #def create_status_bar(self): #self.status_text = QLabel("This is a demo") #self.statusBar().addWidget(self.status_text, 1) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") load_file_action = self.create_action("&Save plot", shortcut="Ctrl+S", slot=self.save_plot, tip="Save the plot") open_file_action = self.create_action("&Open file", shortcut="Ctrl+O", slot = self.open_file, tip="Open a data file") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (open_file_action, load_file_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action,)) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action( self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
class PlkWidget(QtGui.QWidget): """ The plk-emulator window. @param parent: Parent window """ def __init__(self, parent=None, **kwargs): super(PlkWidget, self).__init__(parent, **kwargs) self.initPlk() self.initPlkLayout() self.showVisibleWidgets() self.psr = None self.parent = parent def initPlk(self): self.setMinimumSize(650, 550) self.plkbox = QtGui.QVBoxLayout( ) # plkbox contains the whole plk widget self.xyplotbox = QtGui.QHBoxLayout( ) # plkbox contains the whole plk widget self.fitboxesWidget = PlkFitboxesWidget( parent=self) # Contains all the checkboxes self.actionsWidget = PlkActionsWidget(parent=self) # We are creating the Figure here, so set the color scheme appropriately self.setColorScheme(True) # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.plkDpi = 100 self.plkFig = Figure((5.0, 4.0), dpi=self.plkDpi) self.plkCanvas = FigureCanvas(self.plkFig) self.plkCanvas.setParent(self) # Since we have only one plot, we can use add_axes # instead of add_subplot, but then the subplot # configuration tool in the navigation toolbar wouldn't # work. # self.plkAxes = self.plkFig.add_subplot(111) # Done creating the Figure. Restore color scheme to defaults self.setColorScheme(False) # Call-back functions for clicking and key-press. self.plkCanvas.mpl_connect('button_press_event', self.canvasClickEvent) self.plkCanvas.mpl_connect('key_press_event', self.canvasKeyEvent) # Create the navigation toolbar, tied to the canvas # #self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # Draw an empty graph self.drawSomething() # Create the XY choice widget self.xyChoiceWidget = PlkXYPlotWidget(parent=self) # At startup, all the widgets are visible self.xyChoiceVisible = True self.fitboxVisible = True self.actionsVisible = True self.layoutMode = 1 # (0 = none, 1 = all, 2 = only fitboxes, 3 = fit & action) def setColorScheme(self, start=True): """ Set the color scheme @param start: When true, save the original scheme, and set to white When False, restore the original scheme """ # Obtain the Widget background color color = self.palette().color(QtGui.QPalette.Window) r, g, b = color.red(), color.green(), color.blue() rgbcolor = (r / 255.0, g / 255.0, b / 255.0) if start: # Copy of 'white', because of bug in matplotlib that does not allow # deep copies of rcParams. Store values of matplotlib.rcParams self.orig_rcParams = copy.deepcopy(constants.mpl_rcParams_white) for key, value in self.orig_rcParams.iteritems(): self.orig_rcParams[key] = matplotlib.rcParams[key] rcP = copy.deepcopy(constants.mpl_rcParams_white) rcP['axes.facecolor'] = rgbcolor rcP['figure.facecolor'] = rgbcolor rcP['figure.edgecolor'] = rgbcolor rcP['savefig.facecolor'] = rgbcolor rcP['savefig.edgecolor'] = rgbcolor for key, value in rcP.iteritems(): matplotlib.rcParams[key] = value else: for key, value in constants.mpl_rcParams_black.iteritems(): matplotlib.rcParams[key] = value def drawSomething(self): """ When we don't have a pulsar yet, but we have to display something, just draw an empty figure """ self.setColorScheme(True) self.plkAxes.clear() self.plkAxes.grid(True) self.plkAxes.set_xlabel('MJD') self.plkAxes.set_ylabel('Residual ($\mu$s)') self.plkCanvas.draw() self.setColorScheme(False) def setPulsar(self, psr): """ We've got a new pulsar! """ self.psr = psr # Update the fitting checkboxes self.fitboxesWidget.setPulsar(psr) self.xyChoiceWidget.setPulsar(psr, self.updatePlot) self.actionsWidget.setPulsar(psr, self.updatePlot, self.reFit) # Draw the residuals self.xyChoiceWidget.updateChoice() # This screws up the show/hide logistics #self.show() def reFit(self): """ We need to re-do the fit for this pulsar """ if not self.psr is None: self.psr.fit() self.updatePlot() def newFitParameters(self): """ This function is called when we have new fitparameters TODO: callback not used right now """ pass def initPlkLayout(self): """ Initialise the basic layout of this plk emulator emulator """ # Initialise the plk box self.plkbox.addWidget(self.fitboxesWidget) self.xyplotbox.addWidget(self.xyChoiceWidget) self.xyplotbox.addWidget(self.plkCanvas) self.plkbox.addLayout(self.xyplotbox) self.plkbox.addWidget(self.actionsWidget) self.setLayout(self.plkbox) def showVisibleWidgets(self): """ Show the correct widgets in the plk Window """ self.xyChoiceWidget.setVisible(self.xyChoiceVisible) self.fitboxesWidget.setVisible(self.fitboxVisible) self.actionsWidget.setVisible(self.actionsVisible) def updatePlot(self): """ Update the plot/figure """ self.setColorScheme(True) self.plkAxes.clear() self.plkAxes.grid(True) if self.psr is not None: # Get a mask for the plotting points msk = self.psr.mask('plot') #print("Mask has {0} toas".format(np.sum(msk))) # Get the IDs of the X and Y axis xid, yid = self.xyChoiceWidget.plotids() # Retrieve the data x, xerr, xlabel = self.psr.data_from_label(xid) y, yerr, ylabel = self.psr.data_from_label(yid) if x is not None and y is not None and np.sum(msk) > 0: xp = x[msk] yp = y[msk] if yerr is not None: yerrp = yerr[msk] else: yerrp = None self.plotResiduals(xp, yp, yerrp, xlabel, ylabel, self.psr.name) if xid in ['mjd', 'year', 'rounded MJD']: self.plotPhaseJumps(self.psr.phasejumps()) else: raise ValueError("Nothing to plot!") self.plkCanvas.draw() self.setColorScheme(False) def plotResiduals(self, x, y, yerr, xlabel, ylabel, title): """ Update the plot, given all the plotting info """ xave = 0.5 * (np.max(x) + np.min(x)) xmin = xave - 1.05 * (xave - np.min(x)) xmax = xave + 1.05 * (np.max(x) - xave) if yerr is None: yave = 0.5 * (np.max(y) + np.min(y)) ymin = yave - 1.05 * (yave - np.min(y)) ymax = yave + 1.05 * (np.max(y) - yave) self.plkAxes.scatter(x, y, marker='.', c='g') else: yave = 0.5 * (np.max(y + yerr) + np.min(y - yerr)) ymin = yave - 1.05 * (yave - np.min(y - yerr)) ymax = yave + 1.05 * (np.max(y + yerr) - yave) self.plkAxes.errorbar(x, y, yerr=yerr, fmt='.', color='green') self.plkAxes.axis([xmin, xmax, ymin, ymax]) self.plkAxes.get_xaxis().get_major_formatter().set_useOffset(False) self.plkAxes.set_xlabel(xlabel) self.plkAxes.set_ylabel(ylabel) self.plkAxes.set_title(title) def plotPhaseJumps(self, phasejumps): """ Plot the phase jump lines, if we have any """ xmin, xmax, ymin, ymax = self.plkAxes.axis() if len(phasejumps) > 0: phasejumps = np.array(phasejumps) for ii in range(len(phasejumps)): if phasejumps[ii, 1] != 0: # TODO: Add the jump size on top of the plot self.plkAxes.vlines(phasejumps[ii, 0], ymin, ymax, color='darkred', linestyle='--', linewidth=0.5) def setFocusToCanvas(self): """ Set the focus to the plk Canvas """ self.plkCanvas.setFocus() def coord2point(self, cx, cy, which='xy'): """ Given data coordinates x and y, obtain the index of the observations that is closest to it @param cx: x-value of the coordinates @param cy: y-value of the coordinates @param which: which axis to include in distance measure [xy/x/y] @return: Index of observation """ ind = None if self.psr is not None: # Get a mask for the plotting points msk = self.psr.mask('plot') # Get the IDs of the X and Y axis xid, yid = self.xyChoiceWidget.plotids() # Retrieve the data x, xerr, xlabel = self.psr.data_from_label(xid) y, yerr, ylabel = self.psr.data_from_label(yid) if np.sum(msk) > 0 and x is not None and y is not None: # Obtain the limits xmin, xmax, ymin, ymax = self.plkAxes.axis() if which == 'xy': dist = ((x[msk] - cx) / (xmax - xmin))**2 + ((y[msk] - cy) / (ymax - ymin))**2 elif which == 'x': dist = ((x[msk] - cx) / (xmax - xmin))**2 elif which == 'y': dist = ((y[msk] - cy) / (ymax - ymin))**2 else: raise ValueError( "Value {0} not a valid option for coord2point".format( which)) ind = np.arange(len(x))[msk][np.argmin(dist)] return ind def keyPressEvent(self, event, **kwargs): """ A key is pressed. Handle all the shortcuts here. This function can be called as a callback from the Canvas, or as a callback from Qt. So first some parsing must be done """ if hasattr(event.key, '__call__'): ukey = event.key() modifiers = int(event.modifiers()) from_canvas = False print( "WARNING: call-back key-press, canvas location not available") xpos, ypos = None, None else: # Modifiers are noted as: key = 'ctrl+alt+F', or 'alt+control', or # 'shift+g'. Do some parsing fkey = event.key from_canvas = True xpos, ypos = event.xdata, event.ydata ukey = ord(fkey[-1]) modifiers = QtCore.Qt.NoModifier if 'ctrl' in fkey: modifiers += QtCore.Qt.ControlModifier if 'shift' in fkey: modifiers += QtCore.Qt.ShiftModifier if 'alt' in fkey: modifiers += QtCore.Qt.ShiftModifier if 'meta' in fkey: modifiers += QtCore.Qt.MetaModifier if 'backspace' in fkey: ukey = QtCore.Qt.Key_Backspace #if int(e.modifiers()) == (QtCore.Qt.ControlModifier+QtCore.Qt.AltModifier) if ukey == QtCore.Qt.Key_Escape: if self.parent is None: self.close() else: self.parent.close() elif (ukey == ord('M') or ukey == ord('m')) and \ modifiers == QtCore.Qt.ControlModifier: # Change the window self.layoutMode = (1 + self.layoutMode) % 4 if self.layoutMode == 0: self.xyChoiceVisible = False self.fitboxVisible = False self.actionsVisible = False elif self.layoutMode == 1: self.xyChoiceVisible = True self.fitboxVisible = True self.actionsVisible = True elif self.layoutMode == 2: self.xyChoiceVisible = False self.fitboxVisible = True self.actionsVisible = True elif self.layoutMode == 3: self.xyChoiceVisible = False self.fitboxVisible = True self.actionsVisible = False self.showVisibleWidgets() elif ukey == ord('s'): # Set START flag at xpos # TODO: propagate back to the IPython shell self.psr['START'].set = True self.psr['START'].fit = True self.psr['START'].val = xpos self.updatePlot() elif ukey == ord('f'): # Set FINISH flag as xpos # TODO: propagate back to the IPython shell self.psr['FINISH'].set = True self.psr['FINISH'].fit = True self.psr['FINISH'].val = xpos self.updatePlot() elif ukey == ord('u'): # Unzoom # TODO: propagate back to the IPython shell self.psr['START'].set = True self.psr['START'].fit = False self.psr['START'].val = np.min(self.psr.toas) self.psr['FINISH'].set = True self.psr['FINISH'].fit = False self.psr['FINISH'].val = np.max(self.psr.toas) self.updatePlot() elif ukey == ord('d'): # Delete data point # TODO: propagate back to the IPython shell # TODO: Fix libstempo! ind = self.coord2point(xpos, ypos) #print("Deleted:", self.psr._psr.deleted) # TODO: fix this hack properly in libstempo tempdel = self.psr.deleted tempdel[ind] = True self.psr.deleted = tempdel self.updatePlot() #print("Index deleted = ", ind) #print("Deleted:", self.psr.deleted[ind]) elif ukey == ord('+') or ukey == ord('-'): # Add/delete a phase jump jump = 1 if ukey == ord('-'): jump = -1 ind = self.coord2point(xpos, ypos, which='x') self.psr.add_phasejump(self.psr.stoas[ind], jump) self.updatePlot() elif ukey == QtCore.Qt.Key_Backspace: # Remove all phase jumps self.psr.remove_phasejumps() self.updatePlot() elif ukey == ord('<'): # Add a data point to the view on the left # TODO: Make this more Pythonic! if self.psr['START'].set and self.psr['START'].fit: start = self.psr['START'].val ltmask = self.psr.stoas < start if np.sum(ltmask) > 2: ltind = np.arange(len(self.psr.stoas))[ltmask] lttoas = self.psr.stoas[ltmask] max_ltind = np.argmax(lttoas) # Get maximum of selected TOAs ltmax = ltind[max_ltind] start_max = self.psr.stoas[ltmax] # Get second-highest TOA value ltmask[ltmax] = False ltind = np.arange(len(self.psr.stoas))[ltmask] lttoas = self.psr.stoas[ltmask] max_ltind = np.argmax(lttoas) ltmax = ltind[max_ltind] start_max2 = self.psr.stoas[ltmax] # Set the new START value self.psr['START'].val = 0.5 * (start_max + start_max2) elif np.sum(ltmask) == 2: idmin = np.argmin(self.psr.stoas) stmin = self.psr.stoas[idmin] mask = np.ones(len(self.psr.stoas), dtype=np.bool) mask[idmin] = False self.psr['START'].val = 0.5 * \ (np.min(self.psr.stoas[mask]) + stmin) elif np.sum(ltmask) == 1: self.psr['START'].val = np.min(self.psr.stoas) - 1 elif np.sum(ltmask) == 0: pass self.updatePlot() elif ukey == ord('>'): # Add a data point to the view on the left # TODO: Make this more Pythonic! if self.psr['FINISH'].set and self.psr['FINISH'].fit: start = self.psr['FINISH'].val gtmask = self.psr.stoas > start if np.sum(gtmask) > 2: gtind = np.arange(len(self.psr.stoas))[gtmask] gttoas = self.psr.stoas[gtmask] min_gtind = np.argmin(gttoas) # Get maximum of selected TOAs gtmin = gtind[min_gtind] start_min = self.psr.stoas[gtmin] # Get second-highest TOA value gtmask[gtmin] = False gtind = np.arange(len(self.psr.stoas))[gtmask] gttoas = self.psr.stoas[gtmask] min_gtind = np.argmin(gttoas) gtmin = gtind[min_gtind] start_min2 = self.psr.stoas[gtmin] # Set the new FINISH value self.psr['FINISH'].val = 0.5 * (start_min + start_min2) elif np.sum(gtmask) == 2: idmax = np.argmax(self.psr.stoas) stmax = self.psr.stoas[idmax] mask = np.ones(len(self.psr.stoas), dtype=np.bool) mask[idmax] = False self.psr['FINISH'].val = 0.5 * \ (np.max(self.psr.stoas[mask]) + stmax) elif np.sum(gtmask) == 1: self.psr['FINISH'].val = np.max(self.psr.stoas) + 1 elif np.sum(gtmask) == 0: pass self.updatePlot() elif ukey == ord('x'): # Re-do the fit, using post-fit values of the parameters self.reFit() elif ukey == QtCore.Qt.Key_Left: # print("Left pressed") pass else: #print("Other key: {0} {1} {2} {3}".format(ukey, # modifiers, ord('M'), QtCore.Qt.ControlModifier)) pass #print("PlkWidget: key press: ", ukey, xpos, ypos) if not from_canvas: if self.parent is not None: print("Propagating key press") self.parent.keyPressEvent(event) super(PlkWidget, self).keyPressEvent(event, **kwargs) def canvasClickEvent(self, event): """ When one clicks on the Figure/Canvas, this function is called. The coordinates of the click are stored in event.xdata, event.ydata """ #print('Canvas click, you pressed', event.button, event.xdata, event.ydata) pass def canvasKeyEvent(self, event): """ When one presses a button on the Figure/Canvas, this function is called. The coordinates of the click are stored in event.xdata, event.ydata """ # Callback to the plkWidget self.keyPressEvent(event)
class AppForm(QMainWindow): def __init__(self, fileIn, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('PyProf') self.currentFile = fileIn self.whichShowing = 0 self.posMouse = [0,0] self.createMainFrame() def readParameters(self, rootFile): # Read inverted parameters ff = nf(rootFile+'.parameters', 'r') pars = ff.variables['map'][:] ff.close() # Read errors ff = nf(rootFile+'.errors', 'r') errors = ff.variables['map'][:] ff.close() return pars, errors def readProfiles(self, rootFile): # Read inverted profiles ff = nf(rootFile+'.inversion', 'r') synthProf = ff.variables['map'][:] ff.close() return synthProf def readObservations(self, rootFile): # Read inverted profiles ff = nf(rootFile+'.nc', 'r') sizeMask = ff.variables['mask'].shape sizeMap = ff.variables['map'].shape obsProf = ff.variables['map'][:].reshape((sizeMask[0],sizeMask[1],sizeMap[-2],sizeMap[-1])) ff.close() return obsProf def readData(self): # Parameters self.obs = self.readObservations(self.currentFile) obsShape = self.obs.shape self.pars, self.errors = self.readParameters(self.currentFile) parsShape = self.pars.shape self.pars = self.pars.reshape((obsShape[0],obsShape[1],parsShape[-1])) self.errors = self.errors.reshape((obsShape[0],obsShape[1],parsShape[-1])) self.syn = self.readProfiles(self.currentFile) synShape = self.syn.shape self.syn = self.syn.reshape((obsShape[0],obsShape[1],synShape[-2],synShape[-1])) self.nx, self.ny, self.nLambda, _ = self.syn.shape self.maps = [None] * 4 self.maps = [None] * 4 for i in range(4): self.maps[i] = np.sum(self.obs[:,:,:,0], axis=(2)) def updateProfiles(self): # Blit animation. We only redraw the lines loop = 0 for j in range(6): for i in range(4): self.rightCanvas.restore_region(self.background[loop]) self.obs[loop].set_ydata(self.profiles[self.rangeFrom[j]:self.rangeTo[j],i]) self.axes[loop].draw_artist(self.obs[loop]) self.rightCanvas.blit(self.axes[loop].bbox) loop += 1 def onMouseMove(self, event): print(event.xdata, event.ydata) if (event.xdata != None and event.ydata != None): newPos = np.asarray([event.xdata, event.ydata]) newPos = newPos.astype(int) if (newPos[0] != self.posMouse[0] or newPos[1] != self.posMouse[1]): self.posMouse = newPos self.profiles = self.getProfiles(newPos[0], newPos[1]) self.updateProfiles() def onScrollMove(self,event): pass #if (event.button == 'up'): #self.newTime += 1 #if (event.button == 'down'): #self.newTime -= 1 #self.newTime = self.newTime % 35 #self.profiles = self.getProfiles(self.posMouse[0], self.posMouse[1]) #self.updateProfiles() def getProfiles(self, x, y): return self.obs[x,y,:,0:5], self.syn[x,y,:,:] def getMaps(self, x): return self.obs[:,:,0,0] #-------------------------------------- def create_main_frame(self): self.mainFrame = QWidget() gridLayout = QGridLayout() self.dpi = 80 self.xpos = 0 self.ypos = 0 self.titles = [r'I',r'Q',r'U',r'V'] self.readData() self.resize(1500,900) # Left window self.leftPlot = QWidget() self.leftFig = Figure((2*self.ny / self.dpi, 4*self.nx / self.dpi), dpi=self.dpi) self.leftCanvas = FigureCanvas(self.leftFig) self.leftCanvas.setParent(self.leftPlot) self.leftAxes = [None]*4 self.drawnMap = [None]*4 for i in range(4): self.leftAxes[i] = self.leftFig.add_subplot(2,2,i+1) self.drawnMap[i] = self.leftAxes[i].imshow(self.maps[i], aspect='equal') # self.leftAxes[i].set_axis_off() self.leftCanvas.mpl_connect('motion_notify_event', self.onMouseMove) self.leftCanvas.mpl_connect('scroll_event', self.onScrollMove) gridLayout.addWidget(self.leftPlot, 0, 0) gridLayout.setSpacing(10) # Central window # self.centralPlot = QWidget() # self.centralFig = Figure((10,8), dpi=self.dpi) # self.centralCanvas = FigureCanvas(self.centralFig) # self.centralCanvas.setParent(self.centralPlot) # self.slitMaps = self.getMaps(0) # self.centralAxes = [None]*4 # self.drawnSlitMap = [None]*4 # # for i in range(4): # # self.centralAxes[i] = self.centralFig.add_subplot(4,1,i+1) # # self.drawnSlitMap[i] = self.centralAxes[i].imshow(self.slitMaps[i,:,:]) # #self.centralCanvas.draw() # gridLayout.addWidget(self.centralPlot, 0, 1) # Right window self.rightPlot = QWidget() self.rightFig = Figure((8,18), dpi=self.dpi) self.rightCanvas = FigureCanvas(self.rightFig) self.rightCanvas.setParent(self.rightPlot) self.stokesObs, self.stokesSyn = self.getProfiles(0,0) # Draw the axes and the first profiles nCols = 2 nRows = 2 self.axes = [None] * 4 self.obsLine = [None] * 4 self.synLine = [None] * 4 loop = 0 for j in range(2): for i in range(2): self.axes[loop] = self.rightFig.add_subplot(nCols, nRows, loop+1) self.obsLine[loop], = self.axes[loop].plot(self.stokesObs[:,loop]) self.synLine[loop], = self.axes[loop].plot(self.stokesSyn[:,loop]) loop += 1 gridLayout.addWidget(self.rightPlot, 0, 2) ## Tight layout and redraw # self.rightFig.tight_layout() self.rightCanvas.draw() ## We are using blit animation to do it fast, so we need to recover the axes modified by tight_layout self.newAxes = self.rightFig.get_axes() ## Save the backgrounds # loop = 0 # for j in range(6): # for i in range(4): # self.axes[loop] = self.newAxes[loop] # self.background[loop] = self.canvasRight.copy_from_bbox(self.axes[loop].bbox) # loop += 1 gridLayout.addWidget(self.rightPlot) # fullLayout.addLayout(gridLayout) self.mainFrame.setLayout(gridLayout) self.setCentralWidget(self.mainFrame)
class LinePlotter(SimulationDataConsumer, QWidget): def __init__(self, visualizables, colors=None, parent=None, figure_width=5.0, figure_height=4.0, dpi=100, facecolor="", legend_alpha=0.5): QWidget.__init__(self, parent) SimulationDataConsumer.__init__(self) self.figure_width = figure_width self.figure_height = figure_height self.dpi = dpi self.grid_visible = False self.legend_visible = True self.facecolor = facecolor self.legend_alpha = legend_alpha self.visualizables = visualizables self._setup_prelude() self._setup_plot_canvas() self._setup_navigation_toolbar() self._setup_actions() self._setup_context_menu() self._setup_signal_slot_connections() self._setup_postlude() """ def lineplot(self, visids, *args, **kwargs): axes = self.figure.add_subplot(args, kwargs) for visid in visids: line = axes.plot(numpy.array([]), numpy.array([]), label=visid, gid=visid)[0] self._create_legend() return axes def eventplot(): pass def eventplot(self, nrows, ncols, plot_number, ids, **kwargs, ): pass def add_subplot(self, nrows, ncols, plot_number, plot_type, ids, **kwargs): axes = self.add_subplot(rows, cols, plot_number, kwargs) if plot_type == moogli.constants.EVENT_PLOT: axes.eventplot elif: return axes def get_subplot(plot_number): pass """ def set_title(self, title): self.axes.set_title(title) def get_field(self): return self.field def _create_line(self, label, color=constants.DEFAULT, zorder=0): line = self.axes.plot(numpy.array([]), numpy.array([]), label=label, gid=label)[0] if color is not constants.DEFAULT: line.set_color(color) self._create_legend() def _setup_prelude(self): self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.setLayout(QGridLayout()) def _setup_postlude(self): map(self._create_line, self.visualizables) def _setup_plot_canvas(self): self.figure = Figure((self.figure_width, self.figure_height), dpi=self.dpi, facecolor='#F3EFEE') self.canvas = FigureCanvas(self.figure) self.layout().addWidget(self.canvas, 0, 0) self.axes = self.figure.add_subplot(1, 1, 1) self.legend = None def _create_legend(self): self.legend = self.axes.legend( loc='upper right', prop={'size': 10}, # bbox_to_anchor=(1.0, 0.5), fancybox=True, shadow=False, ncol=1) self.legend.draggable() self.legend.get_frame().set_alpha(self.legend_alpha) if self.legend_visible: self.show_legend_slot() else: self.hide_legend_slot() def _setup_navigation_toolbar(self): self.navigation_toolbar = NavigationToolbar(self.canvas, self) self.layout().addWidget(self.navigation_toolbar, 1, 0) def _setup_actions(self): self.toggle_grid_action = QAction(self) self.toggle_grid_action.setText("Show Grid") self.toggle_autoscale_action = QAction(self) self.toggle_autoscale_action.setText("Enable Autoscaling") self.toggle_axis_hold_action = QAction(self) self.toggle_axis_hold_action.setText("Hold Axes") self.toggle_legend_action = QAction(self) self.toggle_legend_action.setText("Hide Legend") self.export_to_csv_action = QAction(self) self.export_to_csv_action.setText("CSV") def _setup_signal_slot_connections(self): self.canvas.mpl_connect('pick_event', self.pick_event_slot) self.toggle_grid_action.triggered.connect(self.toggle_grid_slot) self.toggle_autoscale_action.triggered.connect( self.toggle_autoscale_slot) self.toggle_axis_hold_action.triggered.connect( self.toggle_axis_hold_slot) self.toggle_legend_action.triggered.connect(self.toggle_legend_slot) self.export_to_csv_action.triggered.connect(self.export_to_csv_slot) self.connect(self, SIGNAL("customContextMenuRequested(QPoint)"), self, SLOT("show_context_menu_slot(QPoint)")) @QtCore.pyqtSlot() def draw_slot(self): self.canvas.draw() def pick_event_slot(self, event): pass def show_grid_slot(self): self.axes.grid(True) self.toggle_grid_action.setText("Hide Grid") self.draw_slot() def hide_grid_slot(self): self.axes.grid(False) self.toggle_grid_action.setText("Show Grid") self.draw_slot() def toggle_grid_slot(self): self.hide_grid_slot() if self.grid_visible else self.show_grid_slot() self.grid_visible = not self.grid_visible def toggle_autoscale_slot(self): pass def toggle_axis_hold_slot(self): pass def show_legend_slot(self): if self.legend is None: return self.legend.set_visible(True) self.toggle_legend_action.setText("Hide Legend") self.draw_slot() def hide_legend_slot(self): if self.legend is None: return self.legend.set_visible(False) self.toggle_legend_action.setText("Show Legend") self.draw_slot() def toggle_legend_slot(self): if self.legend_visible: self.hide_legend_slot() else: self.show_legend_slot() self.legend_visible = not self.legend_visible def set_xlabel(self, xlabel): self.axes.set_xlabel(xlabel) def set_ylabel(self, ylable): self.axes.set_ylabel(ylable) @pyqtSlot(QtCore.QPoint) def show_context_menu_slot(self, point): self.context_menu.exec_(self.mapToGlobal(point)) def _setup_context_menu(self): self.context_menu = QMenu() self.context_menu.addAction(self.toggle_grid_action) self.context_menu.addAction(self.toggle_autoscale_action) self.context_menu.addAction(self.toggle_axis_hold_action) self.context_menu.addAction(self.toggle_legend_action) export_menu = self.context_menu.addMenu("Export") export_menu.addAction(self.export_to_csv_action) def toggle_line_visibility_slot(self, line): line.set_visible(not line.get_visible()) @QtCore.pyqtSlot(str) def export_to_csv_slot(self, filepath): with open(filepath, "wb") as csv_file: writer = csv.writer(csv_file, delimiter=' ', quotechar='"', fieldnames=["Time"] + self._tables.keys(), quoting=csv.QUOTE_NONNUMERIC) writer.writerow([ "#MODEL : {model}".format( model=self.instance["model"]) ]) writer.writerow([ "#TIMESTAMP : {timestamp}".format( timestamp=int(time.time())) ]) writer.writerow([ "#MOOSE VERSION : {moose_version}".format( moose_version="123") ]) writer.writerow([ "#GOOSE VERSION : {goose_version}".format( goose_version="456") ]) writer.writeheader() units = ["s"] + [self._unit] * len(self._tables) writer.writerow(units) data = [value.get_ydata() for value in self._tables.values()] data = data + data[0].get_xdata() data = transpose(data) writer.writerows(data) def consume(self): self.axes.set_xlim([self.clock.begin, self.clock.end]) for line in self.axes.lines: for mediator in self.mediators: try: print "gid", line.get_gid() yvalue = mediator.output[line.get_gid()] ydata = numpy.append(line.get_ydata(), yvalue) print("y_data", ydata) line.set_ydata(ydata) line.set_xdata( numpy.linspace(self.clock.begin, self.clock.now(), len(ydata))) break except: continue self.draw_slot()