class Main(plugin.Plugin): " Main Class " def initialize(self, *args, **kwargs): " Init Main Class " super(Main, self).initialize(*args, **kwargs) self.scriptPath, self.scriptArgs = "", [] self.profilerPath, self.tempPath = profilerPath, tempPath self.output = " ERROR: FAIL: No output ! " self.process = QProcess() self.process.finished.connect(self.on_process_finished) self.process.error.connect(self.on_process_error) self.tabWidget, self.stat = QTabWidget(), QWidget() self.tabWidget.tabCloseRequested.connect( lambda: self.tabWidget.setTabPosition(1) if self.tabWidget. tabPosition() == 0 else self.tabWidget.setTabPosition(0)) self.tabWidget.setStyleSheet('QTabBar{font-weight:bold;}') self.tabWidget.setMovable(True) self.tabWidget.setTabsClosable(True) self.vboxlayout1 = QVBoxLayout(self.stat) self.hboxlayout1 = QHBoxLayout() self.filterTableLabel = QLabel("<b>Type to Search : </b>", self.stat) self.hboxlayout1.addWidget(self.filterTableLabel) self.filterTableLineEdit = QLineEdit(self.stat) self.filterTableLineEdit.setPlaceholderText(' Type to Search . . . ') self.hboxlayout1.addWidget(self.filterTableLineEdit) self.filterHintTableLabel = QLabel(" ? ", self.stat) self.hboxlayout1.addWidget(self.filterHintTableLabel) self.vboxlayout1.addLayout(self.hboxlayout1) self.tableWidget = QTableWidget(self.stat) self.tableWidget.setAlternatingRowColors(True) self.tableWidget.setColumnCount(8) self.tableWidget.setRowCount(2) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(3, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(4, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(5, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(6, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(7, item) self.tableWidget.itemDoubleClicked.connect( self.on_tableWidget_itemDoubleClicked) self.vboxlayout1.addWidget(self.tableWidget) self.tabWidget.addTab(self.stat, " ? ") self.source = QWidget() self.gridlayout = QGridLayout(self.source) self.scintillaWarningLabel = QLabel( "QScintilla is not installed!. Falling back to basic text edit!.", self.source) self.gridlayout.addWidget(self.scintillaWarningLabel, 1, 0, 1, 2) self.sourceTreeWidget = QTreeWidget(self.source) self.sourceTreeWidget.setAlternatingRowColors(True) self.sourceTreeWidget.itemActivated.connect( self.on_sourceTreeWidget_itemActivated) self.sourceTreeWidget.itemClicked.connect( self.on_sourceTreeWidget_itemClicked) self.sourceTreeWidget.itemDoubleClicked.connect( self.on_sourceTreeWidget_itemClicked) self.gridlayout.addWidget(self.sourceTreeWidget, 0, 0, 1, 1) self.sourceTextEdit = QTextEdit(self.source) self.sourceTextEdit.setReadOnly(True) self.gridlayout.addWidget(self.sourceTextEdit, 0, 1, 1, 1) self.tabWidget.addTab(self.source, " ? ") self.result = QWidget() self.vlayout = QVBoxLayout(self.result) self.globalStatGroupBox = QGroupBox(self.result) self.hboxlayout = QHBoxLayout(self.globalStatGroupBox) self.totalTimeLcdNumber = QLCDNumber(self.globalStatGroupBox) self.totalTimeLcdNumber.setSegmentStyle(QLCDNumber.Filled) self.totalTimeLcdNumber.setNumDigits(7) self.totalTimeLcdNumber.display(1000000) self.totalTimeLcdNumber.setFrameShape(QFrame.StyledPanel) self.totalTimeLcdNumber.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.hboxlayout.addWidget(self.totalTimeLcdNumber) self.tTimeLabel = QLabel("<b>Total Time (Sec)</b>", self.globalStatGroupBox) self.tTimeLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.hboxlayout.addWidget(self.tTimeLabel) self.numCallLcdNumber = QLCDNumber(self.globalStatGroupBox) self.numCallLcdNumber.setNumDigits(7) self.numCallLcdNumber.display(1000000) self.numCallLcdNumber.setSegmentStyle(QLCDNumber.Filled) self.numCallLcdNumber.setFrameShape(QFrame.StyledPanel) self.numCallLcdNumber.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.hboxlayout.addWidget(self.numCallLcdNumber) self.numCallLabel = QLabel("<b>Number of calls</b>", self.globalStatGroupBox) self.numCallLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.hboxlayout.addWidget(self.numCallLabel) self.primCallLcdNumber = QLCDNumber(self.globalStatGroupBox) self.primCallLcdNumber.setSegmentStyle(QLCDNumber.Filled) self.primCallLcdNumber.setFrameShape(QFrame.StyledPanel) self.primCallLcdNumber.setNumDigits(7) self.primCallLcdNumber.display(1000000) self.primCallLcdNumber.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.hboxlayout.addWidget(self.primCallLcdNumber) self.primCallLabel = QLabel("<b>Primitive calls (%)</b>", self.globalStatGroupBox) self.primCallLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.hboxlayout.addWidget(self.primCallLabel) self.vlayout.addWidget(self.globalStatGroupBox) try: from PyKDE4.kdeui import KRatingWidget self.rating = KRatingWidget(self.globalStatGroupBox) self.rating.setToolTip('Profiling Performance Rating') except ImportError: pass self.tabWidget.addTab(self.result, " Get Results ! ") self.resgraph = QWidget() self.vlayout2 = QVBoxLayout(self.result) self.graphz = QGroupBox(self.resgraph) self.hboxlayout2 = QHBoxLayout(self.graphz) try: from PyKDE4.kdeui import KLed KLed(self.graphz) except ImportError: pass self.hboxlayout2.addWidget( QLabel(''' Work in Progress :) Not Ready Yet''')) self.vlayout2.addWidget(self.graphz) self.tabWidget.addTab(self.resgraph, " Graphs and Charts ") self.pathz = QWidget() self.vlayout3 = QVBoxLayout(self.pathz) self.patz = QGroupBox(self.pathz) self.hboxlayout3 = QVBoxLayout(self.patz) self.profilepath = QLineEdit(profilerPath) self.getprofile = QPushButton(QIcon.fromTheme("document-open"), 'Open') self.getprofile.setToolTip( 'Dont touch if you dont know what are doing') self.getprofile.clicked.connect(lambda: self.profilepath.setText( str( QFileDialog.getOpenFileName( self.patz, ' Open the profile.py file ', path.expanduser("~"), ';;(profile.py)')))) self.hboxlayout3.addWidget( QLabel( '<center><b>Profile.py Python Library Full Path:</b></center>') ) self.hboxlayout3.addWidget(self.profilepath) self.hboxlayout3.addWidget(self.getprofile) self.argGroupBox = QGroupBox(self.pathz) self.hbxlayout = QHBoxLayout(self.argGroupBox) self.argLineEdit = QLineEdit(self.argGroupBox) self.argLineEdit.setToolTip( 'Not touch if you dont know what are doing') self.argLineEdit.setPlaceholderText( 'Dont touch if you dont know what are doing') self.hbxlayout.addWidget( QLabel('<b>Additional Profile Arguments:</b>')) self.hbxlayout.addWidget(self.argLineEdit) self.hboxlayout3.addWidget(self.argGroupBox) self.vlayout3.addWidget(self.patz) self.tabWidget.addTab(self.pathz, " Paths and Configs ") self.outp = QWidget() self.vlayout4 = QVBoxLayout(self.outp) self.outgro = QGroupBox(self.outp) self.outgro.setTitle(" MultiProcessing Output Logs ") self.hboxlayout4 = QVBoxLayout(self.outgro) self.outputlog = QTextEdit() self.outputlog.setText(''' I do not fear computers, I fear the lack of them. -Isaac Asimov ''') self.hboxlayout4.addWidget(self.outputlog) self.vlayout4.addWidget(self.outgro) self.tabWidget.addTab(self.outp, " Logs ") self.actionNew_profiling = QAction(QIcon.fromTheme("document-new"), 'New Profiling', self) self.actionLoad_profile = QAction(QIcon.fromTheme("document-open"), 'Open Profiling', self) self.actionClean = QAction(QIcon.fromTheme("edit-clear"), 'Clean', self) self.actionClean.triggered.connect(lambda: self.clearContent) self.actionAbout = QAction(QIcon.fromTheme("help-about"), 'About', self) self.actionAbout.triggered.connect(lambda: QMessageBox.about( self.dock, __doc__, ', '.join( (__doc__, __license__, __author__, __email__)))) self.actionSave_profile = QAction(QIcon.fromTheme("document-save"), 'Save Profiling', self) self.actionManual = QAction(QIcon.fromTheme("help-contents"), 'Help', self) self.actionManual.triggered.connect(lambda: open_new_tab( 'http://docs.python.org/library/profile.html')) self.tabWidget.setCurrentIndex(2) self.globalStatGroupBox.setTitle("Global Statistics") item = self.tableWidget.horizontalHeaderItem(0) item.setText("Number of Calls") item = self.tableWidget.horizontalHeaderItem(1) item.setText("Total Time") item = self.tableWidget.horizontalHeaderItem(2) item.setText("Per Call") item = self.tableWidget.horizontalHeaderItem(3) item.setText("Cumulative Time") item = self.tableWidget.horizontalHeaderItem(4) item.setText("Per Call") item = self.tableWidget.horizontalHeaderItem(5) item.setText("Filename") item = self.tableWidget.horizontalHeaderItem(6) item.setText("Line") item = self.tableWidget.horizontalHeaderItem(7) item.setText("Function") self.tabWidget.setTabText(self.tabWidget.indexOf(self.stat), "Statistics per Function") self.sourceTreeWidget.headerItem().setText(0, "Source files") self.tabWidget.setTabText(self.tabWidget.indexOf(self.source), "Sources Navigator") ####################################################################### self.scrollable, self.dock = QScrollArea(), QDockWidget() self.scrollable.setWidgetResizable(True) self.scrollable.setWidget(self.tabWidget) self.dock.setWindowTitle(__doc__) self.dock.setStyleSheet('QDockWidget::title{text-align: center;}') self.dock.setWidget(self.scrollable) QToolBar(self.dock).addActions( (self.actionNew_profiling, self.actionClean, self.actionSave_profile, self.actionLoad_profile, self.actionManual, self.actionAbout)) self.actionNew_profiling.triggered.connect( self.on_actionNew_profiling_triggered) self.actionLoad_profile.triggered.connect( self.on_actionLoad_profile_triggered) self.actionSave_profile.triggered.connect( self.on_actionSave_profile_triggered) self.locator.get_service('misc').add_widget( self.dock, QIcon.fromTheme("document-open-recent"), __doc__) if QSCI: # Scintilla source editor management self.scintillaWarningLabel.setText(' QScintilla is Ready ! ') layout = self.source.layout() layout.removeWidget(self.sourceTextEdit) self.sourceTextEdit = Qsci.QsciScintilla(self.source) layout.addWidget(self.sourceTextEdit, 0, 1) doc = self.sourceTextEdit doc.setLexer(Qsci.QsciLexerPython(self.sourceTextEdit)) doc.setReadOnly(True) doc.setEdgeMode(Qsci.QsciScintilla.EdgeLine) doc.setEdgeColumn(80) doc.setEdgeColor(QColor("#FF0000")) doc.setFolding(Qsci.QsciScintilla.BoxedTreeFoldStyle) doc.setBraceMatching(Qsci.QsciScintilla.SloppyBraceMatch) doc.setCaretLineVisible(True) doc.setMarginLineNumbers(1, True) doc.setMarginWidth(1, 25) doc.setTabWidth(4) doc.setEolMode(Qsci.QsciScintilla.EolUnix) self.marker = {} for color in COLORS: mnr = doc.markerDefine(Qsci.QsciScintilla.Background) doc.setMarkerBackgroundColor(color, mnr) self.marker[color] = mnr self.currentSourcePath = None # Connect table and tree filter edit signal to unique slot self.filterTableLineEdit.textEdited.connect( self.on_filterLineEdit_textEdited) # Timer to display filter hint message self.filterHintTimer = QTimer(self) self.filterHintTimer.setSingleShot(True) self.filterHintTimer.timeout.connect(self.on_filterHintTimer_timeout) # Timer to start search self.filterSearchTimer = QTimer(self) self.filterSearchTimer.setSingleShot(True) self.filterSearchTimer.timeout.connect( self.on_filterSearchTimer_timeout) self.tabLoaded = {} for i in range(10): self.tabLoaded[i] = False self.backgroundTreeMatchedItems = {} self.resizeWidgetToContent(self.tableWidget) def on_actionNew_profiling_triggered(self): self.clearContent() self.scriptPath = str( QFileDialog.getOpenFileName(self.dock, "Choose your script to profile", path.expanduser("~"), "Python (*.py *.pyw)")) commandLine = [ self.profilerPath, "-o", self.tempPath, self.scriptPath ] + self.scriptArgs commandLine = " ".join(commandLine) ##if self.termCheckBox.checkState() == Qt.Checked: #termList = ["xterm", "aterm"] #for term in termList: #termPath = which(term) #if termPath: #break #commandLine = """%s -e "%s ; echo 'Press ENTER Exit' ; read" """ \ #% (termPath, commandLine) self.process.start(commandLine) if not self.process.waitForStarted(): print((" ERROR: {} failed!".format(commandLine))) return def on_process_finished(self, exitStatus): ' whan the process end ' print((" INFO: OK: QProcess is %s" % self.process.exitCode())) self.output = self.process.readAll().data() if not self.output: self.output = " ERROR: FAIL: No output ! " self.outputlog.setText(self.output + str(self.process.exitCode())) if path.exists(self.tempPath): self.setStat(self.tempPath) remove(self.tempPath) else: self.outputlog.setText(" ERROR: QProcess FAIL: Profiling failed.") self.tabWidget.setCurrentIndex(2) def on_process_error(self, error): ' when the process fail, I hope you never see this ' print(" ERROR: QProcess FAIL: Profiler Dead, wheres your God now ? ") if error == QProcess.FailedToStart: self.outputlog.setText(" ERROR: FAIL: Profiler execution failed ") elif error == QProcess.Crashed: self.outputlog.setText(" ERROR: FAIL: Profiler execution crashed ") else: self.outputlog.setText(" ERROR: FAIL: Profiler unknown error ") def on_actionLoad_profile_triggered(self): """Load a previous profile sessions""" statPath = str( QFileDialog.getOpenFileName(self.dock, "Open profile dump", path.expanduser("~"), "Profile file (*)")) if statPath: self.clearContent() print(' INFO: OK: Loading profiling from ' + statPath) self.setStat(statPath) def on_actionSave_profile_triggered(self): """Save a profile sessions""" statPath = str( QFileDialog.getSaveFileName(self.dock, "Save profile dump", path.expanduser("~"), "Profile file (*)")) if statPath: #TODO: handle error case and give feelback to user print(' INFO: OK: Saving profiling to ' + statPath) self.stat.save(statPath) #=======================================================================# # Common parts # #=======================================================================# def on_tabWidget_currentChanged(self, index): """slot for tab change""" # Kill search and hint timer if running to avoid cross effect for timer in (self.filterHintTimer, self.filterSearchTimer): if timer.isActive(): timer.stop() if not self.stat: #No stat loaded, nothing to do return self.populateTable() self.populateSource() def on_filterLineEdit_textEdited(self, text): """slot for filter change (table or tree""" if self.filterSearchTimer.isActive(): # Already runnning, stop it self.filterSearchTimer.stop() # Start timer self.filterSearchTimer.start(300) def on_filterHintTimer_timeout(self): """Timeout to warn user about text length""" print("timeout") tab = self.tabWidget.currentIndex() if tab == TAB_FUNCTIONSTAT: label = self.filterHintTableLabel label.setText("Type > 2 characters to search") def on_filterSearchTimer_timeout(self): """timeout to start search""" tab = self.tabWidget.currentIndex() if tab == TAB_FUNCTIONSTAT: text = self.filterTableLineEdit.text() label = self.filterHintTableLabel edit = self.filterTableLineEdit widget = self.tableWidget else: print("Unknow tab for filterSearch timeout !") print(("do search for %s" % text)) if not len(text): # Empty keyword, just clean all if self.filterHintTimer.isActive(): self.filterHintTimer.stop() label.setText(" ? ") self.warnUSer(True, edit) self.clearSearch() return if len(text) < 2: # Don't filter if text is too short and tell it to user self.filterHintTimer.start(600) return else: if self.filterHintTimer.isActive(): self.filterHintTimer.stop() label.setText(" ? ") # Search self.clearSearch() matchedItems = [] if tab == TAB_FUNCTIONSTAT: # Find items matchedItems = widget.findItems(text, Qt.MatchContains) widget.setSortingEnabled(False) matchedRows = [item.row() for item in matchedItems] # Hide matched items header = widget.verticalHeader() for row in range(widget.rowCount()): if row not in matchedRows: header.hideSection(row) widget.setSortingEnabled(True) else: print(" Unknow tab for filterSearch timeout ! ") print(("got %s members" % len(matchedItems))) self.warnUSer(matchedItems, edit) self.resizeWidgetToContent(widget) def resizeWidgetToContent(self, widget): """Resize all columns according to content""" for i in range(widget.columnCount()): widget.resizeColumnToContents(i) def clearSearch(self): """Clean search result For table, show all items For tree, remove colored items""" tab = self.tabWidget.currentIndex() if tab == TAB_FUNCTIONSTAT: header = self.tableWidget.verticalHeader() if header.hiddenSectionCount(): for i in range(header.count()): if header.isSectionHidden(i): header.showSection(i) def clearContent(self): # Clear tabs self.tableWidget.clearContents() self.sourceTreeWidget.clear() # Reset LCD numbers for lcdNumber in (self.totalTimeLcdNumber, self.numCallLcdNumber, self.primCallLcdNumber): lcdNumber.display(1000000) # Reset stat self.pstat = None # Disable save as menu self.actionSave_profile.setEnabled(False) # Mark all tabs as unloaded for i in range(10): self.tabLoaded[i] = False def warnUSer(self, result, inputWidget): palette = inputWidget.palette() if result: palette.setColor(QPalette.Normal, QPalette.Base, QColor(255, 255, 255)) else: palette.setColor(QPalette.Normal, QPalette.Base, QColor(255, 136, 138)) inputWidget.setPalette(palette) inputWidget.update() def setStat(self, statPath): self.stat = Stat(path=statPath) # Global stat update self.totalTimeLcdNumber.display(self.stat.getTotalTime()) self.numCallLcdNumber.display(self.stat.getCallNumber()) self.primCallLcdNumber.display(self.stat.getPrimitiveCallRatio()) # Refresh current tab self.on_tabWidget_currentChanged(self.tabWidget.currentIndex()) # Activate save as menu self.actionSave_profile.setEnabled(True) try: self.rating.setMaxRating(10) self.rating.setRating( int(self.stat.getPrimitiveCallRatio()) / 10 - 1) except: pass #========================================================================# # Statistics table # #=======================================================================# def populateTable(self): row = 0 rowCount = self.stat.getStatNumber() progress = QProgressDialog("Populating statistics table...", "Abort", 0, 2 * rowCount) self.tableWidget.setSortingEnabled(False) self.tableWidget.setRowCount(rowCount) progress.setWindowModality(Qt.WindowModal) for (key, value) in self.stat.getStatItems(): #ncalls item = StatTableWidgetItem(str(value[0])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_NCALLS, item) colorTableItem(item, self.stat.getCallNumber(), value[0]) #total time item = StatTableWidgetItem(str(value[2])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_TTIME, item) colorTableItem(item, self.stat.getTotalTime(), value[2]) #per call (total time) if value[0] != 0: tPerCall = str(value[2] / value[0]) cPerCall = str(value[3] / value[0]) else: tPerCall = "" cPerCall = "" item = StatTableWidgetItem(tPerCall) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_TPERCALL, item) colorTableItem( item, 100.0 * self.stat.getTotalTime() / self.stat.getCallNumber(), tPerCall) #per call (cumulative time) item = StatTableWidgetItem(cPerCall) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_CPERCALL, item) colorTableItem( item, 100.0 * self.stat.getTotalTime() / self.stat.getCallNumber(), cPerCall) #cumulative time item = StatTableWidgetItem(str(value[3])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_CTIME, item) colorTableItem(item, self.stat.getTotalTime(), value[3]) #Filename self.tableWidget.setItem(row, STAT_FILENAME, StatTableWidgetItem(str(key[0]))) #Line item = StatTableWidgetItem(str(key[1])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_LINE, item) #Function name self.tableWidget.setItem(row, STAT_FUNCTION, StatTableWidgetItem(str(key[2]))) row += 1 # Store it in stat hash array self.stat.setStatLink(item, key, TAB_FUNCTIONSTAT) progress.setValue(row) if progress.wasCanceled(): return for i in range(self.tableWidget.rowCount()): progress.setValue(row + i) for j in range(self.tableWidget.columnCount()): item = self.tableWidget.item(i, j) if item: item.setFlags(Qt.ItemIsEnabled) self.tableWidget.setSortingEnabled(True) self.resizeWidgetToContent(self.tableWidget) progress.setValue(2 * rowCount) def on_tableWidget_itemDoubleClicked(self, item): matchedItems = [] filename = str(self.tableWidget.item(item.row(), STAT_FILENAME).text()) if not filename or filename.startswith("<"): # No source code associated, return immediatly return function = self.tableWidget.item(item.row(), STAT_FUNCTION).text() line = self.tableWidget.item(item.row(), STAT_LINE).text() self.on_tabWidget_currentChanged(TAB_SOURCE) # load source tab function = "%s (%s)" % (function, line) fathers = self.sourceTreeWidget.findItems(filename, Qt.MatchContains, SOURCE_FILENAME) print(("find %s father" % len(fathers))) for father in fathers: findItems(father, function, SOURCE_FILENAME, matchedItems) print(("find %s items" % len(matchedItems))) if matchedItems: self.tabWidget.setCurrentIndex(TAB_SOURCE) self.sourceTreeWidget.scrollToItem(matchedItems[0]) self.on_sourceTreeWidget_itemClicked(matchedItems[0], SOURCE_FILENAME) matchedItems[0].setSelected(True) else: print("oups, item found but cannot scroll to it !") #=======================================================================# # Source explorer # #=====================================================================# def populateSource(self): items = {} for stat in self.stat.getStatKeys(): source = stat[0] function = "%s (%s)" % (stat[2], stat[1]) if source in ("", "profile") or source.startswith("<"): continue # Create the function child child = QTreeWidgetItem([function]) # Store it in stat hash array self.stat.setStatLink(child, stat, TAB_SOURCE) if source in items: father = items[source] else: # Create the father father = QTreeWidgetItem([source]) items[source] = father father.addChild(child) self.sourceTreeWidget.setSortingEnabled(False) for value in list(items.values()): self.sourceTreeWidget.addTopLevelItem(value) self.sourceTreeWidget.setSortingEnabled(True) def on_sourceTreeWidget_itemActivated(self, item, column): self.on_sourceTreeWidget_itemClicked(item, column) def on_sourceTreeWidget_itemClicked(self, item, column): line = 0 parent = item.parent() if QSCI: doc = self.sourceTextEdit if parent: pathz = parent.text(column) result = match("(.*) \(([0-9]+)\)", item.text(column)) if result: try: function = str(result.group(1)) line = int(result.group(2)) except ValueError: # We got garbage... falling back to line 0 pass else: pathz = item.text(column) pathz = path.abspath(str(pathz)) if self.currentSourcePath != pathz: # Need to load source self.currentSourcePath == pathz try: if QSCI: doc.clear() doc.insert(file(pathz).read()) else: self.sourceTextEdit.setPlainText(file(pathz).read()) except IOError: QMessageBox.warning(self, "Error", "Source file could not be found", QMessageBox.Ok) return if QSCI: for function, line in [(i[2], i[1]) for i in self.stat.getStatKeys() if i[0] == pathz]: # expr, regexp, case sensitive, whole word, wrap, forward doc.findFirst("def", False, True, True, False, True, line, 0, True) end, foo = doc.getCursorPosition() time = self.stat.getStatTotalTime((pathz, line, function)) colorSource(doc, self.stat.getTotalTime(), time, line, end, self.marker) if QSCI: doc.ensureLineVisible(line)
class OWxsh_waviness(widget.OWWidget): name = "xsh_waviness" id = "orange.widgets.preprocessor.xsh_waviness" description = "xoppy application to compute..." icon = "icons/waviness.png" author = "Luca Rebuffi" maintainer_email = "[email protected]; [email protected]" priority = 10 category = "" keywords = ["xoppy", "xsh_waviness"] outputs = [{"name": "PreProcessor_Data", "type": ShadowPreProcessorData, "doc": "PreProcessor Data", "id": "PreProcessor_Data"}] want_main_area = 1 want_control_area = 1 WIDGET_WIDTH = 1100 WIDGET_HEIGHT = 650 xx = None yy = None zz = None number_of_points_x = Setting(10) number_of_points_y = Setting(100) dimension_x = Setting(20.1) dimension_y = Setting(113.1) estimated_slope_error = Setting(0.9) montecarlo_seed = Setting(2387427) waviness_file_name = Setting('waviness.dat') harmonic_maximum_index = Setting(60) data = Setting({'c': ['0.3', '0.1', '0.1', '0.0', '0.0', '0.0', '0.3', '0.0', '0.0', '0.3', '0.0', '0.0', '0.5', '0.0', '0.0', '0.2', '0.2', '0.2', '0.9', '0.0', '0.0', '0.0', '0.0', '0.0', '0.4', '0.0', '0.0', '0.4', '0.0', '0.0', '0.0', '0.6', '0.6', '0.0', '0.4', '0.4', '0.0', '0.4', '0.4', '0.1', '0.4', '0.4', '0.1', '0.2', '0.2', '0.0', '0.2', '0.2', '0.0', '0.3', '0.3', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0'], 'y': ['0.0', '-0.1', '-0.1', '0.0', '0.0', '0.0', '0.03', '0.0', '0.0', '0.2', '0.0', '0.0', '0.2', '0.0', '0.0', '0.1', '0.1', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.0', '0.01', '0.0', '0.0', '0.03', '0.0', '0.0', '0.0', '0.02', '0.02', '0.0', '0.1', '0.1', '0.0', '0.1', '0.1', '0.0', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.3', '0.3', '0.0', '0.2', '0.2', '0.0', '0.2', '0.2', '0.0', '0.2', '0.2', '0.0', '0.0', '0.0', '0.0'], 'g': ['0.0', '0.3', '0.3', '0.0', '0.0', '0.0', '0.05', '0.0', '0.0', '0.05', '0.0', '0.0', '0.1', '0.0', '0.0', '0.05', '0.05', '0.05', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.1', '0.0', '0.0', '0.1', '0.0', '0.0', '0.0', '0.2', '0.2', '0.0', '0.1', '0.1', '0.0', '0.1', '0.1', '0.0', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.1', '0.1', '0.0', '0.2', '0.2', '0.0', '0.1', '0.1', '0.0', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0']}) def __init__(self): super().__init__() geom = QApplication.desktop().availableGeometry() self.setGeometry(QRect(round(geom.width() * 0.05), round(geom.height() * 0.05), round(min(geom.width() * 0.98, self.WIDGET_WIDTH)), round(min(geom.height() * 0.95, self.WIDGET_HEIGHT)))) gen_box = ShadowGui.widgetBox(self.controlArea, "Waviness Parameters", addSpace=True, orientation="horizontal", width=500) tabs_setting = gui.tabWidget(gen_box) tab_input = ShadowGui.createTabPage(tabs_setting, "Input Parameter") tab_harmonics = ShadowGui.createTabPage(tabs_setting, "Harmonics") tab_out = ShadowGui.createTabPage(tabs_setting, "Output") self.input_box = ShadowGui.widgetBox(tab_input, "Inputs", addSpace=True, orientation="vertical", width=470) gui.button(self.input_box, self, "Load xsh_waviness input file ...", callback=self.load_inp_file) gui.separator(self.input_box) ShadowGui.lineEdit(self.input_box, self, "number_of_points_x", "Number of Points (<201) X (width)", labelWidth=300, valueType=int, orientation="horizontal") ShadowGui.lineEdit(self.input_box, self, "number_of_points_y", " Y (length)", labelWidth=300, valueType=int, orientation="horizontal") gui.separator(self.input_box) ShadowGui.lineEdit(self.input_box, self, "dimension_x", "Dimensions [cm] X (width)", labelWidth=300, valueType=float, orientation="horizontal") ShadowGui.lineEdit(self.input_box, self, "dimension_y", " Y (length)", labelWidth=300, valueType=float, orientation="horizontal") gui.separator(self.input_box) ShadowGui.lineEdit(self.input_box, self, "estimated_slope_error", "Estimated slope error [arcsec]", labelWidth=300, valueType=float, orientation="horizontal") ShadowGui.lineEdit(self.input_box, self, "montecarlo_seed", "Monte Carlo initial seed", labelWidth=300, valueType=int, orientation="horizontal") self.output_box = ShadowGui.widgetBox(tab_input, "Outputs", addSpace=True, orientation="vertical", width=470) self.select_file_box = ShadowGui.widgetBox(self.output_box, "", addSpace=True, orientation="horizontal") gui.separator(self.output_box) gui.button(self.output_box, self, "Write xsh_waviness input file (optional) ...", callback=self.write_inp_file) ShadowGui.lineEdit(self.select_file_box, self, "waviness_file_name", "Output File Name", labelWidth=120, valueType=str, orientation="horizontal") self.harmonics_box = ShadowGui.widgetBox(tab_harmonics, "Harmonics", addSpace=True, orientation="vertical", width=470, height=690) ShadowGui.lineEdit(self.harmonics_box, self, "harmonic_maximum_index", "Harmonic Maximum Index", labelWidth=300, valueType=int, orientation="horizontal", callback=self.set_harmonics) gui.separator(self.harmonics_box) self.scrollarea = QScrollArea() self.scrollarea.setMaximumWidth(400) self.harmonics_box.layout().addWidget(self.scrollarea, alignment=Qt.AlignHCenter) self.shadow_output = QTextEdit() self.shadow_output.setReadOnly(True) out_box = ShadowGui.widgetBox(tab_out, "System Output", addSpace=True, orientation="horizontal", height=600) out_box.layout().addWidget(self.shadow_output) button_box = ShadowGui.widgetBox(self.controlArea, "", addSpace=False, orientation="horizontal") button = gui.button(button_box, self, "Calculate Waviness", callback=self.calculate_waviness) button.setFixedHeight(45) button.setFixedWidth(170) button = gui.button(button_box, self, "Generate Waviness File", callback=self.generate_waviness_file) font = QFont(button.font()) font.setBold(True) button.setFont(font) palette = QPalette(button.palette()) # make a copy of the palette palette.setColor(QPalette.ButtonText, QColor('Dark Blue')) button.setPalette(palette) # assign new palette button.setFixedHeight(45) button.setFixedWidth(200) button = gui.button(button_box, self, "Reset Fields", callback=self.call_reset_settings) font = QFont(button.font()) font.setItalic(True) button.setFont(font) palette = QPalette(button.palette()) # make a copy of the palette palette.setColor(QPalette.ButtonText, QColor('Dark Red')) button.setPalette(palette) # assign new palette button.setFixedHeight(45) button.setFixedWidth(120) gui.rubber(self.controlArea) self.figure = Figure(figsize=(600, 600)) self.figure.patch.set_facecolor('white') self.axis = self.figure.add_subplot(111, projection='3d') self.axis.set_xlabel("X (cm)") self.axis.set_ylabel("Y (cm)") self.axis.set_zlabel("Z (µm)") self.figure_canvas = FigureCanvasQTAgg(self.figure) self.mainArea.layout().addWidget(self.figure_canvas) gui.rubber(self.mainArea) def restoreWidgetPosition(self): super().restoreWidgetPosition() self.table = QTableWidget(self.harmonic_maximum_index + 1, 3) self.table.setAlternatingRowColors(True) self.table.horizontalHeader().setResizeMode(QHeaderView.Fixed) for i in range(0, 3): self.table.setColumnWidth(i, 70) horHeaders = [] verHeaders = [] for n, key in enumerate(sorted(self.data.keys())): horHeaders.append(key) for m, item in enumerate(self.data[key]): table_item = QTableWidgetItem(str(item)) table_item.setTextAlignment(Qt.AlignRight) self.table.setItem(m, n, table_item) verHeaders.append(str(m)) self.table.setHorizontalHeaderLabels(horHeaders) self.table.setVerticalHeaderLabels(verHeaders) self.table.resizeRowsToContents() self.table.itemChanged.connect(self.table_item_changed) self.scrollarea.setWidget(self.table) self.scrollarea.setWidgetResizable(1) gui.rubber(self.controlArea) def reload_harmonics_table(self): horHeaders = [] verHeaders = [] self.table.itemChanged.disconnect(self.table_item_changed) self.table.clear() row_count = self.table.rowCount() for n in range(0, row_count): self.table.removeRow(0) for index in range(0, self.harmonic_maximum_index + 1): self.table.insertRow(0) for n, key in enumerate(sorted(self.data.keys())): horHeaders.append(key) for m, item in enumerate(self.data[key]): table_item = QTableWidgetItem(str(item)) table_item.setTextAlignment(Qt.AlignRight) self.table.setItem(m, n, table_item) verHeaders.append(str(m)) self.table.setHorizontalHeaderLabels(horHeaders) self.table.setVerticalHeaderLabels(verHeaders) self.table.resizeRowsToContents() for i in range(0, 3): self.table.setColumnWidth(i, 70) self.table.itemChanged.connect(self.table_item_changed) def table_item_changed(self): dict = {} message = "" error_row_index = -1 error_column_index = -1 previous_value = "" try: row_count = self.harmonic_maximum_index + 1 for column_index in range(0, self.table.columnCount()): column_name = self.table.horizontalHeaderItem(column_index).data(0) row_content = [] for row_index in range(0, row_count): if not self.table.item(row_index, column_index) is None: message = "Value at row " + str( row_index) + " and column \'" + column_name + "\' is not numeric" error_row_index = row_index error_column_index = column_index previous_value = self.data[column_name][row_index] value = float(self.table.item(row_index, column_index).data(0)) # to raise exception row_content.append(str(value)) dict[column_name] = row_content self.data = dict except ValueError: QMessageBox.critical(self, "QMessageBox.critical()", message + "\nValue is reset to previous value", QMessageBox.Ok) table_item = QTableWidgetItem(previous_value) table_item.setTextAlignment(Qt.AlignRight) self.table.setItem(error_row_index, error_column_index, table_item) self.table.setCurrentCell(error_row_index, error_column_index) except Exception as exception: QMessageBox.critical(self, "QMessageBox.critical()", exception.args[0], QMessageBox.Ok) def set_harmonics(self): if self.harmonic_maximum_index < 0: QMessageBox.critical(self, "QMessageBox.critical()", "Harmonic Maximum Index should be a positive integer number", QMessageBox.Ok) else: row_count = len(self.data["c"]) if self.harmonic_maximum_index + 1 > row_count: for n, key in enumerate(sorted(self.data.keys())): for m in range(row_count, self.harmonic_maximum_index + 1): self.data[key].append('0.0') else: for n, key in enumerate(sorted(self.data.keys())): self.data[key] = copy.deepcopy(self.data[key][0: self.harmonic_maximum_index + 1]) self.reload_harmonics_table() def load_inp_file(self): file_name = QFileDialog.getOpenFileName(self, "Select a input file for XSH_WAVINESS", ".", "*.inp") if not file_name is None: sys.stdout = EmittingStream(textWritten=self.writeStdOut) if not file_name.strip() == "": dict = ST.waviness_read(file=file_name) self.number_of_points_x = dict["npointx"] self.number_of_points_y = dict["npointy"] self.dimension_y = dict["xlength"] self.dimension_x = dict["width"] self.estimated_slope_error = dict["slp"] self.montecarlo_seed = dict["iseed"] self.waviness_file_name = dict["file"].strip('\n\r').strip() self.harmonic_maximum_index = dict["nharmonics"] self.data["c"] = self.to_str_array(dict["c"]) self.data["y"] = self.to_str_array(dict["y"]) self.data["g"] = self.to_str_array(dict["g"]) self.reload_harmonics_table() def write_inp_file(self): try: sys.stdout = EmittingStream(textWritten=self.writeStdOut) self.check_fields() file_name = self.waviness_file_name.strip().split(sep=".dat")[0] + ".inp" dict = {} dict["npointx"] = self.number_of_points_x dict["npointy"] = self.number_of_points_y dict["xlength"] = self.dimension_y dict["width"] = self.dimension_x dict["slp"] = self.estimated_slope_error dict["iseed"] = self.montecarlo_seed dict["file"] = self.waviness_file_name.strip('\n\r') dict["nharmonics"] = self.harmonic_maximum_index dict["c"] = self.to_float_array(self.data["c"]) dict["y"] = self.to_float_array(self.data["y"]) dict["g"] = self.to_float_array(self.data["g"]) ST.waviness_write(dict, file=file_name) QMessageBox.information(self, "QMessageBox.information()", "File \'" + file_name + "\' written to disk", QMessageBox.Ok) except Exception as exception: QMessageBox.critical(self, "QMessageBox.critical()", exception.args[0], QMessageBox.Ok) def calculate_waviness(self): try: sys.stdout = EmittingStream(textWritten=self.writeStdOut) self.check_fields() xx, yy, zz = ST.waviness_calc(npointx=self.number_of_points_x, npointy=self.number_of_points_y, width=self.dimension_x, xlength=self.dimension_y, slp=self.estimated_slope_error, nharmonics=self.harmonic_maximum_index, iseed=self.montecarlo_seed, c=self.to_float_array(self.data["c"]), y=self.to_float_array(self.data["y"]), g=self.to_float_array(self.data["g"])) self.xx = xx self.yy = yy self.zz = zz self.axis.clear() x_to_plot, y_to_plot = numpy.meshgrid(xx, yy) z_to_plot = [] for y_index in range(0, len(yy)): z_array = [] for x_index in range(0, len(xx)): z_array.append(1e4 * float(zz[x_index][y_index])) # to micron z_to_plot.append(z_array) z_to_plot = numpy.array(z_to_plot) self.axis.plot_surface(x_to_plot, y_to_plot, z_to_plot, rstride=1, cstride=1, cmap=cm.autumn, linewidth=0.5, antialiased=True) slope, sloperms = ST.slopes(zz, xx, yy) title = ' Slope error rms in X direction: %f arcsec' % (sloperms[0]) + '\n' + \ ' : %f urad' % (sloperms[2]) + '\n' + \ ' Slope error rms in Y direction: %f arcsec' % (sloperms[1]) + '\n' + \ ' : %f urad' % (sloperms[3]) self.axis.set_xlabel("X (cm)") self.axis.set_ylabel("Y (cm)") self.axis.set_zlabel("Z (µm)") self.axis.set_title(title) self.axis.mouse_init() self.figure_canvas.draw() QMessageBox.information(self, "QMessageBox.information()", "Waviness calculated: if the result is satisfactory,\nclick \'Generate Waviness File\' to complete the operation ", QMessageBox.Ok) except Exception as exception: QMessageBox.critical(self, "QMessageBox.critical()", exception.args[0], QMessageBox.Ok) def generate_waviness_file(self): if not self.zz is None and not self.yy is None and not self.xx is None: if not self.waviness_file_name is None: self.waviness_file_name = self.waviness_file_name.strip() if self.waviness_file_name == "": raise Exception("Output File Name missing") else: raise Exception("Output File Name missing") sys.stdout = EmittingStream(textWritten=self.writeStdOut) ST.write_shadow_surface(self.zz.T, self.xx, self.yy, outFile=self.waviness_file_name) QMessageBox.information(self, "QMessageBox.information()", "Waviness file " + self.waviness_file_name + " written on disk", QMessageBox.Ok) self.send("PreProcessor_Data", ShadowPreProcessorData(waviness_data_file=self.waviness_file_name)) def call_reset_settings(self): if ConfirmDialog.confirmed(parent=self, message="Confirm Reset of the Fields?"): try: self.resetSettings() self.reload_harmonics_table() except: pass def check_fields(self): self.number_of_points_x = ShadowGui.checkStrictlyPositiveNumber(self.number_of_points_x, "Number of Points X") self.number_of_points_y = ShadowGui.checkStrictlyPositiveNumber(self.number_of_points_y, "Number of Points Y") self.dimension_x = ShadowGui.checkStrictlyPositiveNumber(self.dimension_x, "Dimension X") self.dimension_y = ShadowGui.checkStrictlyPositiveNumber(self.dimension_y, "Dimension Y") self.estimated_slope_error = ShadowGui.checkPositiveNumber(self.estimated_slope_error, "Estimated slope error") self.montecarlo_seed = ShadowGui.checkPositiveNumber(self.montecarlo_seed, "Monte Carlo initial seed") self.harmonic_maximum_index = ShadowGui.checkPositiveNumber(self.harmonic_maximum_index, "Harmonic Maximum Index") if not self.waviness_file_name is None: self.waviness_file_name = self.waviness_file_name.strip() if self.waviness_file_name == "": raise Exception("Output File Name missing") else: raise Exception("Output File Name missing") def to_float_array(self, string_array): float_array = [] for index in range(len(string_array)): float_array.append(float(string_array[index])) return float_array def to_str_array(self, float_array): string_array = [] for index in range(len(float_array)): string_array.append(str(float_array[index])) return string_array def writeStdOut(self, text): cursor = self.shadow_output.textCursor() cursor.movePosition(QTextCursor.End) cursor.insertText(text) self.shadow_output.setTextCursor(cursor) self.shadow_output.ensureCursorVisible()
class CorrectorViewer(QtGui.QWidget): """ List all corrector and select part to lower table """ def __init__(self, cors, field, parent=None, nmax=4): super(CorrectorViewer, self).__init__(parent) self._nmax = nmax self._field = field self._cors = cors self._corlst1 = QtGui.QTreeWidget() self._header = dict([("Element", 0), ("Family", 1), ("s [m]", 2), ("Alpha X", 3), ("Alpha Y", 4), ("Beta X", 5), ("Beta Y", 6), ("Phi X", 7), ("Phi Y", 8), ("Eta X", 9)]) self._twiss = np.zeros((len(self._cors), 8), 'd') self._tunes = getTunes(source="database") self._corlst1.setColumnCount(len(self._header)) self._corlst1.setHeaderLabels( sorted(self._header, key=self._header.get)) self._corlst1.header().setStretchLastSection(False) prevcell = None for i,c in enumerate(self._cors): if c.cell and (prevcell is None or c.cell != prevcell.text(0)): # a new parent prevcell = QtGui.QTreeWidgetItem() prevcell.setText(0, c.cell) self._corlst1.addTopLevelItem(prevcell) it = QtGui.QTreeWidgetItem() it.setData(0, Qt.UserRole, i) it.setText(self._header["Element"], c.name) it.setText(self._header["Family"], c.family) it.setText(self._header["s [m]"], "%.3f" % c.sb) try: tw = getTwiss(c.name, ["s", "alphax", "alphay", "betax", "betay", "phix", "phiy", "etax"]) self._twiss[i,:] = tw[0,:] it.setText(self._header["Alpha X"], "%.4f" % self._twiss[i,1]) it.setText(self._header["Alpha Y"], "%.4f" % self._twiss[i,2]) it.setText(self._header["Beta X"], "%.4f" % self._twiss[i,3]) it.setText(self._header["Beta Y"], "%.4f" % self._twiss[i,4]) it.setText(self._header["Phi X"], "%.4f" % self._twiss[i,5]) it.setText(self._header["Phi Y"], "%.4f" % self._twiss[i,6]) it.setText(self._header["Eta X"], "%.4f" % self._twiss[i,7]) except: it.setDisabled(True) pass if c.cell: prevcell.addChild(it) else: self._corlst1.addTopLevelItem(it) prevcell = it for j in range(2, len(self._header)): it.setTextAlignment(j, Qt.AlignRight) self._corlst1.expandAll() for i in range(len(self._header)): self._corlst1.resizeColumnToContents(i) #self._corlst1.setColumnWidth(0, 150) #self.elemlst.setSelectionMode(QAbstractItemView.MultiSelection) columns = ['Corrector', 's', 'Alpha', 'Beta', 'Phi', "dPhi", "Initial Bump", "Cur. Sp", "dBump", "Final Rb"] self.table4 = QTableWidget(0, len(columns)) #self.table4.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) hdview = QHeaderView(Qt.Horizontal) self.table4.setHorizontalHeaderLabels(columns) #for i in range(4): # for j in range(len(columns)): # it = QTableWidgetItem() # if j > 0: it.setTextAlignment( # Qt.AlignRight | Qt.AlignVCenter) # if columns[j] != "dKick": # it.setFlags(it.flags() & (~Qt.ItemIsEditable)) # self.table4.setItem(i, j, it) #self.table4.resizeColumnsToContents() #self.table4.horizontalHeader().setStretchLastSection(True) #hrow = self.table4.rowHeight(0) #htbl = (hrow * 4) + self.table4.horizontalHeader().height() +\ # 2*self.table4.frameWidth() #self.table4.setMinimumHeight(htbl + 10) #self.table4.setMaximumHeight(htbl + 15) #self.table4.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) #print "Size:", htbl + 10 self.table4.resize(self.table4.width(), 150) splitter = QtGui.QSplitter(Qt.Vertical) splitter.addWidget(self._corlst1) splitter.addWidget(self.table4) vbox1 = QtGui.QVBoxLayout() vbox1.addWidget(splitter) self.setLayout(vbox1) self.connect(self._corlst1, SIGNAL("doubleClicked(QModelIndex)"), self.addCorrector) #self.connect(self.src, SIGNAL("returnPressed()"), # self._calc_source) #self.connect(self.table4, SIGNAL("cellChanged(int, int)"), # self.updateTable) #self.connect(self.table4, SIGNAL("doubleClicked(QModelIndex)"), # self.delCorrector) self._x0 = fget(self._cors, "x", handle="setpoint", unitsys=None) self._y0 = fget(self._cors, "y", handle="setpoint", unitsys=None) def addCorrector(self, idx): if not self._corlst1.selectedItems(): return #print self._corlst1.itemFromIndex(idx).text(0) nrow = self.table4.rowCount() if nrow >= self._nmax: QtGui.QMessageBox.critical( self, "Local Orbit Bump", "ERROR: We need only {0} correctors.".format(self._nmax), QtGui.QMessageBox.Ok) #self.progress.setValue(0) return self.table4.setRowCount(nrow+1) it0 = self._corlst1.selectedItems()[-1] icor, ok = it0.data(0, Qt.UserRole).toInt() if icor < 0: return newc = self._cors[icor] for j in range(self.table4.columnCount()): it = QTableWidgetItem() if j > 0: it.setTextAlignment( Qt.AlignRight | Qt.AlignVCenter) header = self.table4.horizontalHeaderItem(j) if header.text() != "dBump": it.setFlags(it.flags() & (~Qt.ItemIsEditable)) else: it.setData(Qt.DisplayRole, "0") it.setData(Qt.UserRole, 0.0) self.table4.setItem(nrow, j, it) self.table4.item(nrow,0).setData(Qt.UserRole, icor) for j,h in [(0, "Element"), (1, "s [m]")]: self.table4.item(nrow,j).setData(Qt.DisplayRole, it0.text(self._header[h])) for j in range(self._corlst1.columnCount()): it0.setForeground(j, Qt.red) it0.setDisabled(True) self.emit(SIGNAL("correctorAdded(PyQt_PyObject)"), newc) # use initial values self.updateTwiss() self.updateCorReadings() self.table4.resizeColumnsToContents() if self.table4.rowCount() == self._nmax: #print "All correctors are ready" self.emit(SIGNAL("correctorsComplete()")) def updateTwiss(self): if self._field == "x": jl = [self._header[h] for h in ["Alpha X", "Beta X", "Phi X"]] nu = self._tunes[0] elif self._field == "y": jl = [self._header[h] for h in ["Alpha Y", "Beta Y", "Phi Y"]] nu = self._tunes[1] else: raise RuntimeError("unknown cor field {0}".format(self._field)) #print "index:", jl # if rows provided use it, otherwise use all for i in range(self.table4.rowCount()): elemname = self.table4.item(i,0).data(Qt.DisplayRole).toString() it0 = self._corlst1.findItems( elemname, Qt.MatchExactly | Qt.MatchRecursive)[0] self.table4.item(i,2).setText(it0.text(jl[0])) self.table4.item(i,3).setText(it0.text(jl[1])) self.table4.item(i,4).setText(it0.text(jl[2])) self.table4.item(i,4).setData(Qt.UserRole, float(it0.text(jl[2]))) if i == 0: self.table4.item(i,5).setText("0.0") self.table4.item(i,5).setData(Qt.UserRole, 0.0) else: dph, ok = self.table4.item(i-1,5).data(Qt.UserRole).toFloat() ph0, ok = self.table4.item(i-1,4).data(Qt.UserRole).toFloat() ph1, ok = self.table4.item(i,4).data(Qt.UserRole).toFloat() dph = dph + ph1 - ph0 if ph1 < ph0: dph = dph + 2.0*np.pi*nu #print "Updating twiss:", i, dph self.table4.item(i,5).setData(Qt.UserRole, dph) self.table4.item(i,5).setText("%.5g" % dph) icor, ok = self.table4.item(i,0).data(Qt.UserRole).toInt() #c = self._cors[icor] #kick = self._cors[icor].get(self._field, unitsys=None) #self.table4.item(i,6).setData(Qt.UserRole, kick) #self.table4.item(i,6).setText("%.5g" % kick) #self.updateKickReadings(col=0) def clear(self): for i in range(self.table4.rowCount()): elemname = self.table4.item(i,0).data(Qt.DisplayRole).toString() it0 = self._corlst1.findItems( elemname, Qt.MatchExactly | Qt.MatchRecursive)[0] it0.setDisabled(False) for j in range(self._corlst1.columnCount()): it0.setForeground(j, Qt.black) self.table4.setRowCount(0) def updateCorReadings(self): for i in range(self.table4.rowCount()): icor, ok = self.table4.item(i,0).data(Qt.UserRole).toInt() cor = self._cors[icor] if self._field == "x": self.table4.item(i,6).setData(Qt.UserRole, self._x0[icor]) self.table4.item(i,6).setText("%.5g" % self._x0[icor]) elif self._field == "y": self.table4.item(i,6).setData(Qt.UserRole, self._y0[icor]) self.table4.item(i,6).setText("%.5g" % self._y0[icor]) kicksp = cor.get(self._field, handle="setpoint", unitsys=None) self.table4.item(i,7).setData(Qt.UserRole, float(kicksp)) self.table4.item(i,7).setText("%.5g" % kicksp) kickrb = cor.get(self._field, handle="readback", unitsys=None) self.table4.item(i,9).setData(Qt.UserRole, float(kickrb)) self.table4.item(i,9).setText("%.5g" % kickrb) def updateDbump(self, dkick): nrow = min(self.table4.rowCount(), len(dkick)) for i in range(nrow): # dbump column is 8 it = self.table4.item(i, 8) if dkick[i] is None: it.setData(Qt.DisplayRole, "") it.setData(Qt.UserRole, 0.0) else: it.setData(Qt.UserRole, float(dkick[i])) it.setData(Qt.DisplayRole, "{0}".format(dkick[i])) self.updateCorReadings() self.table4.resizeColumnsToContents() #print "(0,7)", self.table4.item(0, 7).data(Qt.UserRole).toFloat() #print "(0,7)", self.table4.item(0, 7).data(Qt.DisplayRole).toFloat() def applyKick(self): nrow = self.table4.rowCount() for i in range(nrow): icor, ok = self.table4.item(i,0).data(Qt.UserRole).toInt() cor = self._cors[icor] # Current SP: 7, dBump 8 k0, ok = self.table4.item(i, 7).data(Qt.UserRole).toFloat() dk, ok = self.table4.item(i, 8).data(Qt.UserRole).toFloat() #print "Setting {0} {1}+{2} [A]".format(cor.name, k0, dk) cor.put(self._field, k0+dk, unitsys=None) # update the final readings self.updateCorReadings() def getTwiss(self): tw = {"s": [], "Alpha": [], "Beta": [], "Phi": [], "dPhi": []} nrow = self.table4.rowCount() for j in range(self.table4.columnCount()): header = self.table4.horizontalHeaderItem(j) if header.text() not in tw.keys(): continue k = str(header.text()) for i in range(nrow): it = self.table4.item(i, j) v0, ok0 = it.data(Qt.UserRole).toFloat() v1, ok1 = it.data(Qt.DisplayRole).toFloat() if ok0: tw[k].append(v0) elif ok1: tw[k].append(v1) else: tw[k].append(np.nan) return tw def selectedCorrectors(self): ret = [] for i in range(self.table4.rowCount()): icor, ok = self.table4.item(i,0).data(Qt.UserRole).toInt() ret.append(self._cors[icor]) return ret def resetCorrectors(self): for i in range(self.table4.rowCount()): icor, ok = self.table4.item(i,0).data(Qt.UserRole).toInt() cor = self._cors[icor] if self._field == "x": kick = self._x0[icor] elif self._field == "y": kick = self._y0[icor] cor.put(self._field, kick, unitsys=None)
class daq_window(QMainWindow): """Window to control and visualise DAQ measurements. Set up the desired channels with a given sampling rate. Start an acquisition after a trigger. Display the acquired data on a trace, and accumulated data on a graph. Arguments: n -- run number for synchronisation rate -- max sample rate in samples / second dt -- desired acquisition period in seconds config_file -- path to file storing default settings port -- the port number to open for TCP connections """ def __init__(self, n=0, rate=250, dt=500, config_file='monitor\\daqconfig.dat', port=8622): super().__init__() self.types = OrderedDict([('n', int), ('config_file', str), ('trace_file', str), ('graph_file', str), ('save_dir', str), ('Sample Rate (kS/s)',float), ('Duration (ms)', float), ('Trigger Channel', str), ('Trigger Level (V)', float), ('Trigger Edge', str), ('channels',channel_stats)]) self.stats = OrderedDict([('n', n), ('config_file', config_file), ('trace_file', 'DAQtrace.csv'), ('graph_file', 'DAQgraph.csv'),('save_dir', '.'), ('Sample Rate (kS/s)', rate), ('Duration (ms)', dt), ('Trigger Channel', 'Dev2/ai1'), # /Dev2/PFI0 ('Trigger Level (V)', 1.0), ('Trigger Edge', 'rising'), ('channels', channel_stats("[['Dev2/ai0', '0', '1.0', '0.0', '5', '1', '1']]"))]) self.trigger_toggle = True # whether to trigger acquisition or just take a measurement self.slave = worker(rate*1e3, dt/1e3, self.stats['Trigger Channel'], self.stats['Trigger Level (V)'], self.stats['Trigger Edge'], list(self.stats['channels'].keys()), [ch['range'] for ch in self.stats['channels'].values()]) # this controls the DAQ self.dc = daqCollection(param=[], channels=list(self.stats['channels'].keys())) self.init_UI() self.load_config(config_file) # load default settings self.n_samples = int(self.stats['Duration (ms)'] * self.stats['Sample Rate (kS/s)']) # number of samples per acquisition self.last_path = './' self.x = [] # run numbers for graphing collections of acquired data self.y = [] # average voltages in slice of acquired trace self.slave.acquired.connect(self.update_graph) # take average of slices self.slave.acquired.connect(self.update_trace) # plot new data when it arrives self.tcp = PyClient(port=port) remove_slot(self.tcp.dxnum, self.set_n, True) remove_slot(self.tcp.textin, self.respond, True) self.tcp.start() def init_UI(self): """Produce the widgets and buttons.""" self.centre_widget = QWidget() self.tabs = QTabWidget() # make tabs for each main display self.centre_widget.layout = QVBoxLayout() self.centre_widget.layout.addWidget(self.tabs) self.centre_widget.setLayout(self.centre_widget.layout) self.setCentralWidget(self.centre_widget) # change font size font = QFont() font.setPixelSize(18) #### menubar at top gives options #### menubar = self.menuBar() # file menubar allows you to save/load data file_menu = menubar.addMenu('File') for label, function in [['Load Config', self.load_config], ['Save Config', self.save_config], ['Load Trace', self.load_trace], ['Save Trace', self.save_trace], ['Save Graph', self.save_graph]]: action = QAction(label, self) action.triggered.connect(function) file_menu.addAction(action) #### tab for settings #### settings_tab = QWidget() settings_grid = QGridLayout() settings_tab.setLayout(settings_grid) self.tabs.addTab(settings_tab, "Settings") self.settings = QTableWidget(1, 6) self.settings.setHorizontalHeaderLabels(['Duration (ms)', 'Sample Rate (kS/s)', 'Trigger Channel', 'Trigger Level (V)', 'Trigger Edge', 'Use Trigger?']) settings_grid.addWidget(self.settings, 0,0, 1,1) defaults = [str(self.stats['Duration (ms)']), str(self.stats['Sample Rate (kS/s)']), self.stats['Trigger Channel'], str(self.stats['Trigger Level (V)']), self.stats['Trigger Edge'], '1'] validators = [double_validator, double_validator, None, double_validator, None, bool_validator] for i in range(6): table_item = QLineEdit(defaults[i]) # user can edit text to change the setting if defaults[i] == 'Sample Rate (kS/s)': table_item.setEnabled(False) table_item.setValidator(validators[i]) # validator limits the values that can be entered self.settings.setCellWidget(0,i, table_item) self.settings.resizeColumnToContents(1) self.settings.setFixedHeight(70) # make it take up less space self.settings.cellWidget(0,0).textChanged.connect(self.check_slice_duration) # start/stop: start waiting for a trigger or taking an acquisition self.toggle = QPushButton('Start', self) self.toggle.setCheckable(True) self.toggle.clicked.connect(self.activate) settings_grid.addWidget(self.toggle, 1,0, 1,1) # channels self.channels = QTableWidget(8, 7) # make table self.channels.setHorizontalHeaderLabels(['Channel', 'Label', 'Scale (X/V)', 'Offset (V)', 'Range', 'Acquire?', 'Plot?']) settings_grid.addWidget(self.channels, 2,0, 1,1) validators = [None, double_validator, double_validator, None, bool_validator, bool_validator] for i in range(8): chan = 'Dev2/ai'+str(i) # name of virtual channel table_item = QLabel(chan) self.channels.setCellWidget(i,0, table_item) if chan in self.stats['channels']: # load values from previous defaults = self.stats['channels'][chan] else: # default values when none are loaded defaults = channel_stats("[dummy, "+str(i)+", 1.0, 0.0, 5.0, 0, 0]")['dummy'] for j, key in zip([0,1,2,4,5], ['label', 'scale', 'offset', 'acquire', 'plot']): table_item = QLineEdit(str(defaults[key])) if 'acquire' in key: table_item.textChanged.connect(self.check_slice_channels) elif 'plot' in key: table_item.textChanged.connect(self.set_acquire) table_item.setValidator(validators[j]) self.channels.setCellWidget(i,j+1, table_item) vrange = QComboBox() # only allow certain values for voltage range vrange.text = vrange.currentText # overload function so it's same as QLabel vrange.addItems(['%.1f'%x for x in self.slave.vrs]) try: vrange.setCurrentIndex(self.slave.vrs.index(defaults['range'])) except Exception as e: logger.error('Invalid channel voltage range\n'+str(e)) self.channels.setCellWidget(i,4, vrange) #### Plot for most recently acquired trace #### trace_tab = QWidget() trace_grid = QGridLayout() trace_tab.setLayout(trace_grid) self.tabs.addTab(trace_tab, "Trace") # button activates horizontal line self.hline_toggle = QPushButton('Horizontal line', self, checkable=True) self.hline_toggle.clicked.connect(self.add_horizontal) trace_grid.addWidget(self.hline_toggle, 0,0, 1,1) self.hline_label = QLabel() trace_grid.addWidget(self.hline_label, 0,1, 1,1) fadeline_button = QPushButton('Persist', self) fadeline_button.clicked.connect(self.set_fadelines) trace_grid.addWidget(fadeline_button, 0,2, 1,1) # plot the trace self.trace_canvas = pg.PlotWidget() self.trace_legend = self.trace_canvas.addLegend() self.trace_canvas.getAxis('bottom').tickFont = font self.trace_canvas.getAxis('left').tickFont = font self.trace_canvas.setLabel('bottom', 'Time', 's', **{'font-size':'18pt'}) self.trace_canvas.setLabel('left', 'Voltage', 'V', **{'font-size':'18pt'}) self.lines = [] # handles for lines plotting the last measurement self.fadelines = [] # handles for previous measurement lines for i in range(8): chan = self.channels.cellWidget(i,1).text() self.lines.append(self.trace_canvas.plot([1], name=chan, pen=pg.mkPen(pg.intColor(i), width=3))) self.lines[i].hide() self.fadelines.append(self.trace_canvas.plot([1], pen=pg.mkPen(pg.intColor(i, alpha=50), width=2))) self.fadelines[i].hide() self.hline = pg.InfiniteLine(1., angle=0, pen='k', movable=True) self.trace_canvas.addItem(self.hline) self.hline.sigPositionChanged.connect(self.update_hline) self.hline.hide() trace_grid.addWidget(self.trace_canvas, 1,0, 1,3) #### Settings for slices of the trace accumulating into the graph #### slice_tab = QWidget() slice_grid = QGridLayout() slice_tab.setLayout(slice_grid) self.tabs.addTab(slice_tab, "Slice") # Buttons to add/remove slices and reset graph for i, (label, func) in enumerate([['Add slice', self.add_slice], ['Remove slice', self.del_slice], ['Reset graph', self.reset_graph]]): button = QPushButton(label, self) button.clicked.connect(func) slice_grid.addWidget(button, 0,i, 1,1) # parameters for slices self.slices = QTableWidget(0, 4) # make table self.slices.setHorizontalHeaderLabels(['Slice name', 'Start (ms)', 'End (ms)', 'Channels']) slice_grid.addWidget(self.slices, 1,0, 1,3) #### Plot for graph of accumulated data #### graph_tab = QWidget() graph_grid = QGridLayout() graph_tab.setLayout(graph_grid) self.tabs.addTab(graph_tab, "Graph") self.mean_graph = pg.PlotWidget() # for plotting means self.stdv_graph = pg.PlotWidget() # for plotting standard deviations self.graph_legends = [] for i, g in enumerate([self.mean_graph, self.stdv_graph]): g.getAxis('bottom').tickFont = font g.getAxis('bottom').setFont(font) g.getAxis('left').tickFont = font g.getAxis('left').setFont(font) graph_grid.addWidget(g, i,0, 1,1) self.reset_lines() # make a line for every slice channel self.stdv_graph.setLabel('bottom', 'Shot', '', **{'font-size':'18pt'}) self.stdv_graph.setLabel('left', 'Standard Deviation', 'V', **{'font-size':'18pt'}) self.mean_graph.setLabel('left', 'Mean', 'V', **{'font-size':'18pt'}) #### tab for TCP message settings #### tcp_tab = QWidget() tcp_grid = QGridLayout() tcp_tab.setLayout(tcp_grid) self.tabs.addTab(tcp_tab, "Sync") label = QLabel('Run number: ') tcp_grid.addWidget(label, 0,0, 1,1) self.n_edit = QLineEdit(str(self.stats['n'])) self.n_edit.setValidator(int_validator) self.n_edit.textEdited[str].connect(self.set_n) tcp_grid.addWidget(self.n_edit, 0,1, 1,1) label = QLabel('Save directory: ') tcp_grid.addWidget(label, 1,0, 1,1) self.save_edit = QLineEdit(self.stats['save_dir']) self.save_edit.textEdited[str].connect(self.set_save_dir) tcp_grid.addWidget(self.save_edit, 1,1, 1,1) label = QLabel('Trace file name: ') tcp_grid.addWidget(label, 2,0, 1,1) self.trace_edit = QLineEdit(self.stats['trace_file']) self.trace_edit.textEdited[str].connect(self.set_trace_file) tcp_grid.addWidget(self.trace_edit, 2,1, 1,1) label = QLabel('Graph file name: ') tcp_grid.addWidget(label, 3,0, 1,1) self.graph_edit = QLineEdit(self.stats['graph_file']) self.graph_edit.textEdited[str].connect(self.set_graph_file) tcp_grid.addWidget(self.graph_edit, 3,1, 1,1) reset = QPushButton('Reset TCP client', self) reset.clicked.connect(self.reset_client) tcp_grid.addWidget(reset, 4,0, 1,1) #### Title and icon #### self.setWindowTitle('- NI DAQ Controller -') self.setWindowIcon(QIcon('docs/daqicon.png')) self.setGeometry(200, 200, 800, 600) #### user input functions #### def set_acquire(self): """If the user chooses to plot, set the same channel to acquire.""" for i in range(self.channels.rowCount()): if BOOL(self.channels.cellWidget(i,6).text()): # plot self.channels.cellWidget(i,5).setText('1') # only plot if acquiring def check_settings(self): """Coerce the settings into allowed values.""" statstr = "[[" # dictionary of channel names and properties for i in range(self.channels.rowCount()): self.trace_legend.items[i][1].setText(self.channels.cellWidget(i,1).text()) # label if BOOL(self.channels.cellWidget(i,5).text()): # acquire statstr += ', '.join([self.channels.cellWidget(i,j).text() for j in range(self.channels.columnCount())]) + '],[' self.stats['channels'] = channel_stats(statstr[:-2] + ']') self.dc.channels = self.stats['channels'].keys() # acquisition settings self.stats['Duration (ms)'] = float(self.settings.cellWidget(0,0).text()) # check that the requested rate is valid rate = float(self.settings.cellWidget(0,1).text()) if len(self.stats['channels']) > 1 and rate > 245 / len(self.stats['channels']): rate = 245 / len(self.stats['channels']) elif len(self.stats['channels']) < 2 and rate > 250: rate = 250 self.stats['Sample Rate (kS/s)'] = rate self.settings.cellWidget(0,1).setText('%.2f'%(rate)) self.n_samples = int(self.stats['Duration (ms)'] * self.stats['Sample Rate (kS/s)']) # check the trigger channel is valid trig_chan = self.settings.cellWidget(0,2).text() if 'Dev2/PFI' in trig_chan or 'Dev2/ai' in trig_chan: self.stats['Trigger Channel'] = trig_chan else: self.stats['Trigger Channel'] = 'Dev2/ai0' self.settings.cellWidget(0,2).setText(str(self.stats['Trigger Channel'])) self.stats['Trigger Level (V)'] = float(self.settings.cellWidget(0,3).text()) self.stats['Trigger Edge'] = self.settings.cellWidget(0,4).text() self.trigger_toggle = BOOL(self.settings.cellWidget(0,5).text()) def set_table(self): """Display the acquisition and channel settings in the table.""" x = self.stats.copy() # prevent it getting overwritten for i in range(5): self.settings.cellWidget(0,i).setText(str(x[ self.settings.horizontalHeaderItem(i).text()])) for i in range(8): ch = self.channels.cellWidget(i,0).text() if ch in x['channels']: for j, key in zip([0,1,2,4,5], ['label', 'scale', 'offset', 'acquire', 'plot']): self.channels.cellWidget(i,j+1).setText(str(x['channels'][ch][key])) self.channels.cellWidget(i,4).setCurrentText('%.1f'%x['channels'][ch]['range']) else: self.channels.cellWidget(i,5).setText('0') # don't acquire self.channels.cellWidget(i,6).setText('0') # don't plot #### slice settings functions #### def add_slice(self, param=[]): """Add a row to the slice table and append it to the daqCollection instance for analysis. param -- [name, start (ms), end (ms), channels]""" try: name, start, end, channels = param except TypeError: name = 'Slice' + str(self.slices.rowCount()) start = 0 end = self.stats['Duration (ms)'] channels = list(self.stats['channels'].keys()) i = self.slices.rowCount() # index to add row at self.slices.insertRow(i) # add row to table validator = QDoubleValidator(0.,float(self.stats['Duration (ms)']),3) for j, text in enumerate([name, str(start), str(end)]): item = QLineEdit(text) item.pos = (i, j) if j > 0: item.setValidator(validator) item.textChanged.connect(self.update_slices) self.slices.setCellWidget(i, j, item) chanbox = QListWidget(self) chanbox.setSelectionMode(3) # extended selection, allows multiple selection chanbox.itemSelectionChanged.connect(self.update_slices) chanbox.pos = (i, 3) chanbox.text = chanbox.objectName chanlist = list(self.stats['channels'].keys()) chanbox.addItems(chanlist) self.slices.setCellWidget(i, j+1, chanbox) self.slices.resizeRowToContents(i) # add to the dc list of slices t = np.linspace(0, self.stats['Duration (ms)']/1000, self.n_samples) self.dc.add_slice(name, np.argmin(np.abs(t-start)), np.argmin(np.abs(t-end)), OrderedDict([(chan, chanlist.index(chan)) for chan in channels])) self.reset_lines() def del_slice(self, toggle=True): """Remove the slice at the selected row of the slice table.""" index = self.slices.currentRow() self.slices.removeRow(index) if index >= 0: self.dc.slices.pop(index) def check_slice_duration(self, newtxt): """If the acquisition duration is changed, make sure the slices can't use times beyond this.""" self.check_settings() # update DAQ acquisition settings first for i in range(self.slices.rowCount()): for j in [1,2]: # force slice to be within max duration validator = QDoubleValidator(0.,float(self.stats['Duration (ms)']),3) self.slices.cellWidget(i, j).setValidator(validator) def check_slice_channels(self, newtxt): """If the channels that can be used for the acquisition are changed, change the list widgets in the slice settings to match.""" self.check_settings() # update DAQ acquisition settings first for i in range(self.slices.rowCount()): w = self.slices.cellWidget(i, 3) # widget shorthand selected = [w.row(x) for x in w.selectedItems()] # make record of selected channels w.clear() w.addItems(list(self.stats['channels'].keys())) for r in selected: try: w.setCurrentRow(r, QItemSelectionModel.SelectCurrent) except Exception as e: pass def update_slices(self, newtxt=''): """Use the current item from the table to update the parameter for the slices.""" try: w = self.sender() i, j = w.pos t = np.linspace(0, self.stats['Duration (ms)'], self.n_samples) x = self.dc.slices[i] # shorthand if j == 0: # name x.name = w.text() elif j == 1: # start (ms) x.i0 = np.argmin(np.abs(t-float(w.text()))) elif j == 2: # end (ms) x.i1 = np.argmin(np.abs(t-float(w.text()))) elif j == 3: x.channels = OrderedDict([(x.text(), w.row(x)) for x in w.selectedItems()]) x.stats = OrderedDict([(chan, OrderedDict([ ('mean',[]), ('stdv',[])])) for chan in x.channels.keys()]) self.reset_lines() self.reset_graph() x.inds = slice(x.i0, x.i1+1) x.size = x.i1 - x.i0 except IndexError as e: pass # logger.error("Couldn't update slice.\n"+str(e)) #### TCP functions #### def set_n(self, num): """Receive the new run number to update to""" self.stats['n'] = int(num) self.n_edit.setText(str(num)) def set_save_dir(self, directory): """Set the directory to save results to""" self.stats['save_dir'] = directory self.save_edit.setText(directory) def set_trace_file(self, fname): """Set the default name for trace files when they're saved.""" self.stats['trace_file'] = fname self.trace_edit.setText(fname) def set_graph_file(self, fname): """Set the default name for graph files when they're saved.""" self.stats['graph_file'] = fname self.graph_edit.setText(fname) def reset_client(self, toggle=True): """Stop the TCP client thread then restart it.""" self.tcp.stop = True for i in range(100): # wait til it's stopped if not self.tcp.isRunning(): break else: time.sleep(0.001) self.tcp.start() # restart def respond(self, msg=''): """Interpret a TCP message. For setting properties, the syntax is: value=property. E.g. 'Z:\Tweezer=save_dir'.""" if 'save_dir' in msg: self.set_save_dir(msg.split('=')[0]) elif 'trace_file' in msg: self.set_trace_file(msg.split('=')[0]) elif 'graph_file' in msg: self.set_graph_file(msg.split('=')[0]) elif 'start' in msg and not self.toggle.isChecked(): self.toggle.setChecked(True) self.activate() elif 'stop' in msg and self.toggle.isChecked(): self.toggle.setChecked(False) self.activate() elif 'save trace' in msg: self.save_trace(os.path.join(self.stats['save_dir'], self.stats['trace_file'])) elif 'save graph' in msg: self.save_graph(os.path.join(self.stats['save_dir'], self.stats['graph_file'])) elif 'set fadelines' in msg: self.set_fadelines() #### acquisition functions #### def activate(self, toggle=0): """Prime the DAQ task for acquisition if it isn't already running. Otherwise, stop the task running.""" if self.toggle.isChecked(): self.check_settings() self.slave = worker(self.stats['Sample Rate (kS/s)']*1e3, self.stats['Duration (ms)']/1e3, self.stats['Trigger Channel'], self.stats['Trigger Level (V)'], self.stats['Trigger Edge'], list(self.stats['channels'].keys()), [ch['range'] for ch in self.stats['channels'].values()]) remove_slot(self.slave.acquired, self.update_trace, True) remove_slot(self.slave.acquired, self.update_graph, True) if self.trigger_toggle: # remove_slot(self.slave.finished, self.activate, True) self.slave.start() self.toggle.setText('Stop') else: self.toggle.setChecked(False) self.slave.analogue_acquisition() else: # remove_slot(self.slave.finished, self.activate, False) self.slave.stop = True self.slave.quit() self.toggle.setText('Start') #### plotting functions #### def update_trace(self, data): """Plot the supplied data with labels on the trace canvas.""" t = np.linspace(0, self.stats['Duration (ms)']/1000, self.n_samples) i = 0 # index to keep track of which channels have been plotted for j in range(8): ch = self.channels.cellWidget(j,0).text() l = self.lines[j] # shorthand if ch in self.stats['channels'] and self.stats['channels'][ch]['plot']: try: l.setData(t, data[i]) except Exception as e: logger.error('DAQ trace could not be plotted.\n'+str(e)) self.fadelines[j].show() l.show() self.trace_legend.items[j][0].show() self.trace_legend.items[j][1].show() i += 1 else: l.hide() self.fadelines[j].hide() self.trace_legend.items[j][0].hide() self.trace_legend.items[j][1].hide() self.trace_legend.resize(0,0) def set_fadelines(self): """Take the data from the current lines and sets it to the fadelines.""" for j in range(8): ch = self.channels.cellWidget(j,0).text() l = self.lines[j] # shorthand if ch in self.stats['channels'] and self.stats['channels'][ch]['plot']: try: self.fadelines[j].setData(l.xData, l.yData) except Exception as e: logger.error('DAQ trace could not be plotted.\n'+str(e)) self.fadelines[j].show() else: self.fadelines[j].hide() def reset_lines(self): """Clear the mean and stdv graphs, reset the legends, then make new lines for each of the slice channels.""" for legend in self.graph_legends: # reset the legends try: legend.scene().removeItem(legend) except AttributeError: pass for g in [self.mean_graph, self.stdv_graph]: g.clear() g.lines = OrderedDict([]) self.graph_legends.append(g.addLegend()) i = 0 for s in self.dc.slices: for chan, val in s.stats.items(): g.lines[s.name+'/'+chan] = g.plot([1], name=s.name+'/'+chan, pen=None, symbol='o', symbolPen=pg.mkPen(pg.intColor(i)), symbolBrush=pg.intColor(i)) i += 1 def reset_graph(self): """Reset the collection of slice data, then replot the graph.""" self.dc.reset_arrays() self.update_graph() def update_graph(self, data=[]): """Extract averages from slices of the data. Replot the stored data accumulated from averages in slices of the measurements.""" if np.size(data): self.dc.process(data, self.stats['n']) for s in self.dc.slices: for chan, val in s.stats.items(): self.mean_graph.lines[s.name+'/'+chan].setData(self.dc.runs, val['mean']) self.stdv_graph.lines[s.name+'/'+chan].setData(self.dc.runs, val['stdv']) def add_horizontal(self, toggle=True): """Display a horizontal line on the trace""" if toggle: self.hline.show() else: self.hline.hide() self.hline_label.setText('') def update_hline(self): """Display the value of the horizontal line in the label""" self.hline_label.setText(str(self.hline.value())) #### save/load functions #### def try_browse(self, title='Select a File', file_type='all (*)', open_func=QFileDialog.getOpenFileName, default_path=''): """Open a file dialog and retrieve a file name from the browser. title: String to display at the top of the file browser window default_path: directory to open first file_type: types of files that can be selected open_func: the function to use to open the file browser""" default_path = default_path if default_path else os.path.dirname(self.last_path) try: if 'PyQt4' in sys.modules: file_name = open_func(self, title, default_path, file_type) elif 'PyQt5' in sys.modules: file_name, _ = open_func(self, title, default_path, file_type) if type(file_name) == str: self.last_path = file_name return file_name except OSError: return '' # probably user cancelled def save_config(self, file_name='daqconfig.dat'): """Save the current acquisition settings to the config file.""" self.stats['config_file'] = file_name if file_name else self.try_browse( 'Save Config File', 'dat (*.dat);;all (*)', QFileDialog.getSaveFileName) try: with open(self.stats['config_file'], 'w+') as f: for key, val in self.stats.items(): if key == 'channels': f.write(key+'='+channel_str(val)+'\n') else: f.write(key+'='+str(val)+'\n') logger.info('DAQ config saved to '+self.stats['config_file']) except Exception as e: logger.error('DAQ settings could not be saved to config file.\n'+str(e)) def load_config(self, file_name='daqconfig.dat'): """Load the acquisition settings from the config file.""" self.stats['config_file'] = file_name if file_name else self.try_browse(file_type='dat (*.dat);;all (*)') try: with open(self.stats['config_file'], 'r') as f: for line in f: if len(line.split('=')) == 2: key, val = line.replace('\n','').split('=') # there should only be one = per line try: self.stats[key] = self.types[key](val) except KeyError as e: logger.warning('Failed to load DAQ default config line: '+line+'\n'+str(e)) self.set_table() # make sure the updates are displayed self.set_n(self.stats['n']) self.set_save_dir(self.stats['save_dir']) self.set_trace_file(self.stats['trace_file']) self.set_graph_file(self.stats['graph_file']) self.dc.channels = list(self.stats['channels'].keys()) logger.info('DAQ config loaded from '+self.stats['config_file']) except FileNotFoundError as e: logger.warning('DAQ settings could not find the config file.\n'+str(e)) def save_trace(self, file_name=''): """Save the data currently displayed on the trace to a csv file.""" file_name = file_name if file_name else self.try_browse( 'Save File', 'csv (*.csv);;all (*)', QFileDialog.getSaveFileName) if file_name: # metadata header = ', '.join(list(self.stats.keys())) + '\n' header += ', '.join(list(map(str, self.stats.values()))[:-1] ) + ', ' + channel_str(self.stats['channels']) + '\n' # determine which channels are in the plot header += 'Time (s)' data = [] for key, d in self.stats['channels'].items(): if d['plot']: header += ', ' + key # column headings if len(data) == 0: # time (s) data.append(self.lines[int(key[-1])].xData) data.append(self.lines[int(key[-1])].yData) # voltage # data converted to the correct type out_arr = np.array(data).T try: np.savetxt(file_name, out_arr, fmt='%s', delimiter=',', header=header) logger.info('DAQ trace saved to '+file_name) except (PermissionError, FileNotFoundError) as e: logger.error('DAQ controller denied permission to save file: \n'+str(e)) def load_trace(self, file_name=''): """Load data for the current trace from a csv file.""" file_name = file_name if file_name else self.try_browse(file_type='csv(*.csv);;all (*)') if file_name: head = [[],[],[]] # get metadata with open(file_name, 'r') as f: for i in range(3): row = f.readline() if row[:2] == '# ': head[i] = row[2:].replace('\n','').split(', ') # apply the acquisition settings from the file labels = [self.settings.horizontalHeaderItem(i).text() for i in range(self.settings.columnCount())] for i in range(len(head[0])): try: j = labels.index(head[0][i]) self.settings.cellWidget(0,j).setText(head[1][i]) except ValueError: pass self.stats['channels'] = channel_stats(', '.join(head[1][7:])) for i in range(8): # whether to plot or not ch = self.channels.cellWidget(i,0).text() if ch in head[2]: self.channels.cellWidget(i,6).setText('1') self.channels.cellWidget(i,1).setText(self.stats['channels'][ch]['label']) else: self.channels.cellWidget(i,6).setText('0') self.check_settings() # plot the data data = np.genfromtxt(file_name, delimiter=',', dtype=float) if np.size(data) < 2: return 0 # insufficient data to load self.update_trace(data.T[1:]) def save_graph(self, file_name=''): """Save the data accumulated from several runs that's displayed in the graph into a csv file.""" file_name = file_name if file_name else self.try_browse( 'Save File', 'csv (*.csv);;all (*)', QFileDialog.getSaveFileName) if file_name: self.dc.save(file_name, list(self.stats.keys()), list(map(str, self.stats.values()))[:-1] + [channel_str(self.stats['channels'])]) logger.info('DAQ graph saved to '+file_name) def closeEvent(self, event): """Before closing, try to save the config settings to file.""" statstr = "[[" # dictionary of channel names and properties for i in range(self.channels.rowCount()): statstr += ', '.join([self.channels.cellWidget(i,j).text() for j in range(self.channels.columnCount())]) + '],[' self.stats['channels'] = channel_stats(statstr[:-2] + ']') # add all channels to stats self.save_config(self.stats['config_file']) event.accept()
class Main(plugin.Plugin): " Main Class " def initialize(self, *args, **kwargs): " Init Main Class " super(Main, self).initialize(*args, **kwargs) self.scriptPath, self.scriptArgs = "", [] self.profilerPath, self.tempPath = profilerPath, tempPath self.output = " ERROR: FAIL: No output ! " self.process = QProcess() self.process.finished.connect(self.on_process_finished) self.process.error.connect(self.on_process_error) self.tabWidget, self.stat = QTabWidget(), QWidget() self.tabWidget.tabCloseRequested.connect(lambda: self.tabWidget.setTabPosition(1) if self.tabWidget.tabPosition() == 0 else self.tabWidget.setTabPosition(0)) self.tabWidget.setStyleSheet('QTabBar{font-weight:bold;}') self.tabWidget.setMovable(True) self.tabWidget.setTabsClosable(True) self.vboxlayout1 = QVBoxLayout(self.stat) self.hboxlayout1 = QHBoxLayout() self.filterTableLabel = QLabel("<b>Type to Search : </b>", self.stat) self.hboxlayout1.addWidget(self.filterTableLabel) self.filterTableLineEdit = QLineEdit(self.stat) self.filterTableLineEdit.setPlaceholderText(' Type to Search . . . ') self.hboxlayout1.addWidget(self.filterTableLineEdit) self.filterHintTableLabel = QLabel(" ? ", self.stat) self.hboxlayout1.addWidget(self.filterHintTableLabel) self.vboxlayout1.addLayout(self.hboxlayout1) self.tableWidget = QTableWidget(self.stat) self.tableWidget.setAlternatingRowColors(True) self.tableWidget.setColumnCount(8) self.tableWidget.setRowCount(2) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(3, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(4, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(5, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(6, item) item = QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(7, item) self.tableWidget.itemDoubleClicked.connect( self.on_tableWidget_itemDoubleClicked) self.vboxlayout1.addWidget(self.tableWidget) self.tabWidget.addTab(self.stat, " ? ") self.source = QWidget() self.gridlayout = QGridLayout(self.source) self.scintillaWarningLabel = QLabel( "QScintilla is not installed!. Falling back to basic text edit!.", self.source) self.gridlayout.addWidget(self.scintillaWarningLabel, 1, 0, 1, 2) self.sourceTreeWidget = QTreeWidget(self.source) self.sourceTreeWidget.setAlternatingRowColors(True) self.sourceTreeWidget.itemActivated.connect( self.on_sourceTreeWidget_itemActivated) self.sourceTreeWidget.itemClicked.connect( self.on_sourceTreeWidget_itemClicked) self.sourceTreeWidget.itemDoubleClicked.connect( self.on_sourceTreeWidget_itemClicked) self.gridlayout.addWidget(self.sourceTreeWidget, 0, 0, 1, 1) self.sourceTextEdit = QTextEdit(self.source) self.sourceTextEdit.setReadOnly(True) self.gridlayout.addWidget(self.sourceTextEdit, 0, 1, 1, 1) self.tabWidget.addTab(self.source, " ? ") self.result = QWidget() self.vlayout = QVBoxLayout(self.result) self.globalStatGroupBox = QGroupBox(self.result) self.hboxlayout = QHBoxLayout(self.globalStatGroupBox) self.totalTimeLcdNumber = QLCDNumber(self.globalStatGroupBox) self.totalTimeLcdNumber.setSegmentStyle(QLCDNumber.Filled) self.totalTimeLcdNumber.setNumDigits(7) self.totalTimeLcdNumber.display(1000000) self.totalTimeLcdNumber.setFrameShape(QFrame.StyledPanel) self.totalTimeLcdNumber.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.hboxlayout.addWidget(self.totalTimeLcdNumber) self.tTimeLabel = QLabel("<b>Total Time (Sec)</b>", self.globalStatGroupBox) self.tTimeLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.hboxlayout.addWidget(self.tTimeLabel) self.numCallLcdNumber = QLCDNumber(self.globalStatGroupBox) self.numCallLcdNumber.setNumDigits(7) self.numCallLcdNumber.display(1000000) self.numCallLcdNumber.setSegmentStyle(QLCDNumber.Filled) self.numCallLcdNumber.setFrameShape(QFrame.StyledPanel) self.numCallLcdNumber.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.hboxlayout.addWidget(self.numCallLcdNumber) self.numCallLabel = QLabel("<b>Number of calls</b>", self.globalStatGroupBox) self.numCallLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.hboxlayout.addWidget(self.numCallLabel) self.primCallLcdNumber = QLCDNumber(self.globalStatGroupBox) self.primCallLcdNumber.setSegmentStyle(QLCDNumber.Filled) self.primCallLcdNumber.setFrameShape(QFrame.StyledPanel) self.primCallLcdNumber.setNumDigits(7) self.primCallLcdNumber.display(1000000) self.primCallLcdNumber.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.hboxlayout.addWidget(self.primCallLcdNumber) self.primCallLabel = QLabel("<b>Primitive calls (%)</b>", self.globalStatGroupBox) self.primCallLabel.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.hboxlayout.addWidget(self.primCallLabel) self.vlayout.addWidget(self.globalStatGroupBox) try: from PyKDE4.kdeui import KRatingWidget self.rating = KRatingWidget(self.globalStatGroupBox) self.rating.setToolTip('Profiling Performance Rating') except ImportError: pass self.tabWidget.addTab(self.result, " Get Results ! ") self.resgraph = QWidget() self.vlayout2 = QVBoxLayout(self.result) self.graphz = QGroupBox(self.resgraph) self.hboxlayout2 = QHBoxLayout(self.graphz) try: from PyKDE4.kdeui import KLed KLed(self.graphz) except ImportError: pass self.hboxlayout2.addWidget(QLabel(''' Work in Progress :) Not Ready Yet''')) self.vlayout2.addWidget(self.graphz) self.tabWidget.addTab(self.resgraph, " Graphs and Charts ") self.pathz = QWidget() self.vlayout3 = QVBoxLayout(self.pathz) self.patz = QGroupBox(self.pathz) self.hboxlayout3 = QVBoxLayout(self.patz) self.profilepath = QLineEdit(profilerPath) self.getprofile = QPushButton(QIcon.fromTheme("document-open"), 'Open') self.getprofile.setToolTip('Dont touch if you dont know what are doing') self.getprofile.clicked.connect(lambda: self.profilepath.setText(str( QFileDialog.getOpenFileName(self.patz, ' Open the profile.py file ', path.expanduser("~"), ';;(profile.py)')))) self.hboxlayout3.addWidget(QLabel( '<center><b>Profile.py Python Library Full Path:</b></center>')) self.hboxlayout3.addWidget(self.profilepath) self.hboxlayout3.addWidget(self.getprofile) self.argGroupBox = QGroupBox(self.pathz) self.hbxlayout = QHBoxLayout(self.argGroupBox) self.argLineEdit = QLineEdit(self.argGroupBox) self.argLineEdit.setToolTip('Not touch if you dont know what are doing') self.argLineEdit.setPlaceholderText( 'Dont touch if you dont know what are doing') self.hbxlayout.addWidget(QLabel('<b>Additional Profile Arguments:</b>')) self.hbxlayout.addWidget(self.argLineEdit) self.hboxlayout3.addWidget(self.argGroupBox) self.vlayout3.addWidget(self.patz) self.tabWidget.addTab(self.pathz, " Paths and Configs ") self.outp = QWidget() self.vlayout4 = QVBoxLayout(self.outp) self.outgro = QGroupBox(self.outp) self.outgro.setTitle(" MultiProcessing Output Logs ") self.hboxlayout4 = QVBoxLayout(self.outgro) self.outputlog = QTextEdit() self.outputlog.setText(''' I do not fear computers, I fear the lack of them. -Isaac Asimov ''') self.hboxlayout4.addWidget(self.outputlog) self.vlayout4.addWidget(self.outgro) self.tabWidget.addTab(self.outp, " Logs ") self.actionNew_profiling = QAction(QIcon.fromTheme("document-new"), 'New Profiling', self) self.actionLoad_profile = QAction(QIcon.fromTheme("document-open"), 'Open Profiling', self) self.actionClean = QAction(QIcon.fromTheme("edit-clear"), 'Clean', self) self.actionClean.triggered.connect(lambda: self.clearContent) self.actionAbout = QAction(QIcon.fromTheme("help-about"), 'About', self) self.actionAbout.triggered.connect(lambda: QMessageBox.about(self.dock, __doc__, ', '.join((__doc__, __license__, __author__, __email__)))) self.actionSave_profile = QAction(QIcon.fromTheme("document-save"), 'Save Profiling', self) self.actionManual = QAction(QIcon.fromTheme("help-contents"), 'Help', self) self.actionManual.triggered.connect(lambda: open_new_tab('http://docs.python.org/library/profile.html')) self.tabWidget.setCurrentIndex(2) self.globalStatGroupBox.setTitle("Global Statistics") item = self.tableWidget.horizontalHeaderItem(0) item.setText("Number of Calls") item = self.tableWidget.horizontalHeaderItem(1) item.setText("Total Time") item = self.tableWidget.horizontalHeaderItem(2) item.setText("Per Call") item = self.tableWidget.horizontalHeaderItem(3) item.setText("Cumulative Time") item = self.tableWidget.horizontalHeaderItem(4) item.setText("Per Call") item = self.tableWidget.horizontalHeaderItem(5) item.setText("Filename") item = self.tableWidget.horizontalHeaderItem(6) item.setText("Line") item = self.tableWidget.horizontalHeaderItem(7) item.setText("Function") self.tabWidget.setTabText(self.tabWidget.indexOf(self.stat), "Statistics per Function") self.sourceTreeWidget.headerItem().setText(0, "Source files") self.tabWidget.setTabText(self.tabWidget.indexOf(self.source), "Sources Navigator") ####################################################################### self.scrollable, self.dock = QScrollArea(), QDockWidget() self.scrollable.setWidgetResizable(True) self.scrollable.setWidget(self.tabWidget) self.dock.setWindowTitle(__doc__) self.dock.setStyleSheet('QDockWidget::title{text-align: center;}') self.dock.setWidget(self.scrollable) QToolBar(self.dock).addActions((self.actionNew_profiling, self.actionClean, self.actionSave_profile, self.actionLoad_profile, self.actionManual, self.actionAbout)) self.actionNew_profiling.triggered.connect( self.on_actionNew_profiling_triggered) self.actionLoad_profile.triggered.connect( self.on_actionLoad_profile_triggered) self.actionSave_profile.triggered.connect( self.on_actionSave_profile_triggered) self.locator.get_service('misc').add_widget(self.dock, QIcon.fromTheme("document-open-recent"), __doc__) if QSCI: # Scintilla source editor management self.scintillaWarningLabel.setText(' QScintilla is Ready ! ') layout = self.source.layout() layout.removeWidget(self.sourceTextEdit) self.sourceTextEdit = Qsci.QsciScintilla(self.source) layout.addWidget(self.sourceTextEdit, 0, 1) doc = self.sourceTextEdit doc.setLexer(Qsci.QsciLexerPython(self.sourceTextEdit)) doc.setReadOnly(True) doc.setEdgeMode(Qsci.QsciScintilla.EdgeLine) doc.setEdgeColumn(80) doc.setEdgeColor(QColor("#FF0000")) doc.setFolding(Qsci.QsciScintilla.BoxedTreeFoldStyle) doc.setBraceMatching(Qsci.QsciScintilla.SloppyBraceMatch) doc.setCaretLineVisible(True) doc.setMarginLineNumbers(1, True) doc.setMarginWidth(1, 25) doc.setTabWidth(4) doc.setEolMode(Qsci.QsciScintilla.EolUnix) self.marker = {} for color in COLORS: mnr = doc.markerDefine(Qsci.QsciScintilla.Background) doc.setMarkerBackgroundColor(color, mnr) self.marker[color] = mnr self.currentSourcePath = None # Connect table and tree filter edit signal to unique slot self.filterTableLineEdit.textEdited.connect( self.on_filterLineEdit_textEdited) # Timer to display filter hint message self.filterHintTimer = QTimer(self) self.filterHintTimer.setSingleShot(True) self.filterHintTimer.timeout.connect(self.on_filterHintTimer_timeout) # Timer to start search self.filterSearchTimer = QTimer(self) self.filterSearchTimer.setSingleShot(True) self.filterSearchTimer.timeout.connect( self.on_filterSearchTimer_timeout) self.tabLoaded = {} for i in range(10): self.tabLoaded[i] = False self.backgroundTreeMatchedItems = {} self.resizeWidgetToContent(self.tableWidget) def on_actionNew_profiling_triggered(self): self.clearContent() self.scriptPath = str(QFileDialog.getOpenFileName(self.dock, "Choose your script to profile", path.expanduser("~"), "Python (*.py *.pyw)")) commandLine = [self.profilerPath, "-o", self.tempPath, self.scriptPath] + self.scriptArgs commandLine = " ".join(commandLine) ##if self.termCheckBox.checkState() == Qt.Checked: #termList = ["xterm", "aterm"] #for term in termList: #termPath = which(term) #if termPath: #break #commandLine = """%s -e "%s ; echo 'Press ENTER Exit' ; read" """ \ #% (termPath, commandLine) self.process.start(commandLine) if not self.process.waitForStarted(): print((" ERROR: {} failed!".format(commandLine))) return def on_process_finished(self, exitStatus): ' whan the process end ' print((" INFO: OK: QProcess is %s" % self.process.exitCode())) self.output = self.process.readAll().data() if not self.output: self.output = " ERROR: FAIL: No output ! " self.outputlog.setText(self.output + str(self.process.exitCode())) if path.exists(self.tempPath): self.setStat(self.tempPath) remove(self.tempPath) else: self.outputlog.setText(" ERROR: QProcess FAIL: Profiling failed.") self.tabWidget.setCurrentIndex(2) def on_process_error(self, error): ' when the process fail, I hope you never see this ' print(" ERROR: QProcess FAIL: Profiler Dead, wheres your God now ? ") if error == QProcess.FailedToStart: self.outputlog.setText(" ERROR: FAIL: Profiler execution failed ") elif error == QProcess.Crashed: self.outputlog.setText(" ERROR: FAIL: Profiler execution crashed ") else: self.outputlog.setText(" ERROR: FAIL: Profiler unknown error ") def on_actionLoad_profile_triggered(self): """Load a previous profile sessions""" statPath = str(QFileDialog.getOpenFileName(self.dock, "Open profile dump", path.expanduser("~"), "Profile file (*)")) if statPath: self.clearContent() print(' INFO: OK: Loading profiling from ' + statPath) self.setStat(statPath) def on_actionSave_profile_triggered(self): """Save a profile sessions""" statPath = str(QFileDialog.getSaveFileName(self.dock, "Save profile dump", path.expanduser("~"), "Profile file (*)")) if statPath: #TODO: handle error case and give feelback to user print(' INFO: OK: Saving profiling to ' + statPath) self.stat.save(statPath) #=======================================================================# # Common parts # #=======================================================================# def on_tabWidget_currentChanged(self, index): """slot for tab change""" # Kill search and hint timer if running to avoid cross effect for timer in (self.filterHintTimer, self.filterSearchTimer): if timer.isActive(): timer.stop() if not self.stat: #No stat loaded, nothing to do return self.populateTable() self.populateSource() def on_filterLineEdit_textEdited(self, text): """slot for filter change (table or tree""" if self.filterSearchTimer.isActive(): # Already runnning, stop it self.filterSearchTimer.stop() # Start timer self.filterSearchTimer.start(300) def on_filterHintTimer_timeout(self): """Timeout to warn user about text length""" print("timeout") tab = self.tabWidget.currentIndex() if tab == TAB_FUNCTIONSTAT: label = self.filterHintTableLabel label.setText("Type > 2 characters to search") def on_filterSearchTimer_timeout(self): """timeout to start search""" tab = self.tabWidget.currentIndex() if tab == TAB_FUNCTIONSTAT: text = self.filterTableLineEdit.text() label = self.filterHintTableLabel edit = self.filterTableLineEdit widget = self.tableWidget else: print("Unknow tab for filterSearch timeout !") print(("do search for %s" % text)) if not len(text): # Empty keyword, just clean all if self.filterHintTimer.isActive(): self.filterHintTimer.stop() label.setText(" ? ") self.warnUSer(True, edit) self.clearSearch() return if len(text) < 2: # Don't filter if text is too short and tell it to user self.filterHintTimer.start(600) return else: if self.filterHintTimer.isActive(): self.filterHintTimer.stop() label.setText(" ? ") # Search self.clearSearch() matchedItems = [] if tab == TAB_FUNCTIONSTAT: # Find items matchedItems = widget.findItems(text, Qt.MatchContains) widget.setSortingEnabled(False) matchedRows = [item.row() for item in matchedItems] # Hide matched items header = widget.verticalHeader() for row in range(widget.rowCount()): if row not in matchedRows: header.hideSection(row) widget.setSortingEnabled(True) else: print(" Unknow tab for filterSearch timeout ! ") print(("got %s members" % len(matchedItems))) self.warnUSer(matchedItems, edit) self.resizeWidgetToContent(widget) def resizeWidgetToContent(self, widget): """Resize all columns according to content""" for i in range(widget.columnCount()): widget.resizeColumnToContents(i) def clearSearch(self): """Clean search result For table, show all items For tree, remove colored items""" tab = self.tabWidget.currentIndex() if tab == TAB_FUNCTIONSTAT: header = self.tableWidget.verticalHeader() if header.hiddenSectionCount(): for i in range(header.count()): if header.isSectionHidden(i): header.showSection(i) def clearContent(self): # Clear tabs self.tableWidget.clearContents() self.sourceTreeWidget.clear() # Reset LCD numbers for lcdNumber in (self.totalTimeLcdNumber, self.numCallLcdNumber, self.primCallLcdNumber): lcdNumber.display(1000000) # Reset stat self.pstat = None # Disable save as menu self.actionSave_profile.setEnabled(False) # Mark all tabs as unloaded for i in range(10): self.tabLoaded[i] = False def warnUSer(self, result, inputWidget): palette = inputWidget.palette() if result: palette.setColor(QPalette.Normal, QPalette.Base, QColor(255, 255, 255)) else: palette.setColor(QPalette.Normal, QPalette.Base, QColor(255, 136, 138)) inputWidget.setPalette(palette) inputWidget.update() def setStat(self, statPath): self.stat = Stat(path=statPath) # Global stat update self.totalTimeLcdNumber.display(self.stat.getTotalTime()) self.numCallLcdNumber.display(self.stat.getCallNumber()) self.primCallLcdNumber.display(self.stat.getPrimitiveCallRatio()) # Refresh current tab self.on_tabWidget_currentChanged(self.tabWidget.currentIndex()) # Activate save as menu self.actionSave_profile.setEnabled(True) try: self.rating.setMaxRating(10) self.rating.setRating( int(self.stat.getPrimitiveCallRatio()) / 10 - 1) except: pass #========================================================================# # Statistics table # #=======================================================================# def populateTable(self): row = 0 rowCount = self.stat.getStatNumber() progress = QProgressDialog("Populating statistics table...", "Abort", 0, 2 * rowCount) self.tableWidget.setSortingEnabled(False) self.tableWidget.setRowCount(rowCount) progress.setWindowModality(Qt.WindowModal) for (key, value) in self.stat.getStatItems(): #ncalls item = StatTableWidgetItem(str(value[0])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_NCALLS, item) colorTableItem(item, self.stat.getCallNumber(), value[0]) #total time item = StatTableWidgetItem(str(value[2])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_TTIME, item) colorTableItem(item, self.stat.getTotalTime(), value[2]) #per call (total time) if value[0] != 0: tPerCall = str(value[2] / value[0]) cPerCall = str(value[3] / value[0]) else: tPerCall = "" cPerCall = "" item = StatTableWidgetItem(tPerCall) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_TPERCALL, item) colorTableItem(item, 100.0 * self.stat.getTotalTime() / self.stat.getCallNumber(), tPerCall) #per call (cumulative time) item = StatTableWidgetItem(cPerCall) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_CPERCALL, item) colorTableItem(item, 100.0 * self.stat.getTotalTime() / self.stat.getCallNumber(), cPerCall) #cumulative time item = StatTableWidgetItem(str(value[3])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_CTIME, item) colorTableItem(item, self.stat.getTotalTime(), value[3]) #Filename self.tableWidget.setItem(row, STAT_FILENAME, StatTableWidgetItem(str(key[0]))) #Line item = StatTableWidgetItem(str(key[1])) item.setTextAlignment(Qt.AlignRight) self.tableWidget.setItem(row, STAT_LINE, item) #Function name self.tableWidget.setItem(row, STAT_FUNCTION, StatTableWidgetItem(str(key[2]))) row += 1 # Store it in stat hash array self.stat.setStatLink(item, key, TAB_FUNCTIONSTAT) progress.setValue(row) if progress.wasCanceled(): return for i in range(self.tableWidget.rowCount()): progress.setValue(row + i) for j in range(self.tableWidget.columnCount()): item = self.tableWidget.item(i, j) if item: item.setFlags(Qt.ItemIsEnabled) self.tableWidget.setSortingEnabled(True) self.resizeWidgetToContent(self.tableWidget) progress.setValue(2 * rowCount) def on_tableWidget_itemDoubleClicked(self, item): matchedItems = [] filename = str(self.tableWidget.item(item.row(), STAT_FILENAME).text()) if not filename or filename.startswith("<"): # No source code associated, return immediatly return function = self.tableWidget.item(item.row(), STAT_FUNCTION).text() line = self.tableWidget.item(item.row(), STAT_LINE).text() self.on_tabWidget_currentChanged(TAB_SOURCE) # load source tab function = "%s (%s)" % (function, line) fathers = self.sourceTreeWidget.findItems(filename, Qt.MatchContains, SOURCE_FILENAME) print(("find %s father" % len(fathers))) for father in fathers: findItems(father, function, SOURCE_FILENAME, matchedItems) print(("find %s items" % len(matchedItems))) if matchedItems: self.tabWidget.setCurrentIndex(TAB_SOURCE) self.sourceTreeWidget.scrollToItem(matchedItems[0]) self.on_sourceTreeWidget_itemClicked(matchedItems[0], SOURCE_FILENAME) matchedItems[0].setSelected(True) else: print("oups, item found but cannot scroll to it !") #=======================================================================# # Source explorer # #=====================================================================# def populateSource(self): items = {} for stat in self.stat.getStatKeys(): source = stat[0] function = "%s (%s)" % (stat[2], stat[1]) if source in ("", "profile") or source.startswith("<"): continue # Create the function child child = QTreeWidgetItem([function]) # Store it in stat hash array self.stat.setStatLink(child, stat, TAB_SOURCE) if source in items: father = items[source] else: # Create the father father = QTreeWidgetItem([source]) items[source] = father father.addChild(child) self.sourceTreeWidget.setSortingEnabled(False) for value in list(items.values()): self.sourceTreeWidget.addTopLevelItem(value) self.sourceTreeWidget.setSortingEnabled(True) def on_sourceTreeWidget_itemActivated(self, item, column): self.on_sourceTreeWidget_itemClicked(item, column) def on_sourceTreeWidget_itemClicked(self, item, column): line = 0 parent = item.parent() if QSCI: doc = self.sourceTextEdit if parent: pathz = parent.text(column) result = match("(.*) \(([0-9]+)\)", item.text(column)) if result: try: function = str(result.group(1)) line = int(result.group(2)) except ValueError: # We got garbage... falling back to line 0 pass else: pathz = item.text(column) pathz = path.abspath(str(pathz)) if self.currentSourcePath != pathz: # Need to load source self.currentSourcePath == pathz try: if QSCI: doc.clear() doc.insert(file(pathz).read()) else: self.sourceTextEdit.setPlainText(file(pathz).read()) except IOError: QMessageBox.warning(self, "Error", "Source file could not be found", QMessageBox.Ok) return if QSCI: for function, line in [(i[2], i[1] ) for i in self.stat.getStatKeys() if i[0] == pathz]: # expr, regexp, case sensitive, whole word, wrap, forward doc.findFirst("def", False, True, True, False, True, line, 0, True) end, foo = doc.getCursorPosition() time = self.stat.getStatTotalTime((pathz, line, function)) colorSource(doc, self.stat.getTotalTime(), time, line, end, self.marker) if QSCI: doc.ensureLineVisible(line)