allanplot-gui.py 4.21 KB
#!/usr/bin/python
# -*- coding: utf-8 -*-

from PyQt4 import QtGui, QtCore
import sys, csv, numpy, allantools, pyqtgraph
import allanplotUI

class allanplot(QtGui.QMainWindow, allanplotUI.Ui_MainWindow):
    def __init__(self, parent=None):
        super(allanplot, self).__init__(parent)
        self.setupUi(self)
        self.connectActions()
        self.pqg_widget(self.frame)

    def pqg_widget(self, frame):
        pyqtgraph.setConfigOption('background', 'w')
        pyqtgraph.setConfigOption('foreground', 'k')
        self.plot_widget = pyqtgraph.PlotWidget()
        self.layout_pqg = QtGui.QVBoxLayout(frame)
        self.layout_pqg.addWidget(self.plot_widget)
        self.plot_widget.showGrid(True, True, 0.5)
        self.plot_widget.addLegend()
        self.plot_widget.getViewBox().setMouseMode(pyqtgraph.ViewBox.RectMode)

    def connectActions(self):
        self.pushQuit.clicked.connect(QtGui.qApp.quit)
        self.pushOpen.clicked.connect(self.openDat)
        self.pushTimePlot.clicked.connect(self.timePlot)
        self.pushAdevPlot.clicked.connect(self.adevPlot)
        self.pushRelativeTimePlot.clicked.connect(self.relativeTimePlot)
        self.pushRelativeAdevPlot.clicked.connect(self.relativeAdevPlot)

    def openDat(self):
        fileNames = QtGui.QFileDialog.getOpenFileNames(self, "Open datafile", QtCore.QDir.homePath(), "data files (*.dat)")
        if fileNames:
            fileList = sorted([str(f) for f in fileNames])
            self.data = []
            textList = ''
            for f in fileList:
                if textList=='':
                    textList = str(f)
                else:
                    textList = textList+'\n'+str(f)
                with open(f, 'r') as dest_f:
                    data_iter = csv.reader(dest_f, delimiter = '\t', quotechar = '"')
                    temp_data = [filter(None, value) for value in data_iter]
                    self.data.extend(temp_data)
            self.data = numpy.asarray(self.data, dtype = float)
            self.textFileList.setText(textList)
            self.textValue.setText(str(self.data[:]))

    def timePlot(self):
        self.plot_widget.clear()
        self.plot_widget.setTitle('time plot')
        self.plot_widget.setLabel('bottom', "Time", "s")
        self.plot_widget.setLabel('left', "y")
        self.plot_widget.setLogMode(False, False)
        for i in range(2, self.data.shape[1]):
            self.curve = self.plot_widget.plot()
            self.curve.setData(self.data[:,0], self.data[:,i], pen=5*i, name='#%s'%i)

    def relativeTimePlot(self):
        self.plot_widget.clear()
        self.plot_widget.setTitle('relative time plot')
        self.plot_widget.setLabel('bottom', "Time", "s")
        self.plot_widget.setLabel('left', "y")
        self.plot_widget.setLogMode(False, False)
        for i in range(2, self.data.shape[1]):
            self.curve = self.plot_widget.plot()
            self.curve.setData(self.data[:,0], self.data[:,i]/self.data[:,i].mean(), pen=5*i, name='#%s'%i)

    def adevPlot(self):
        self.plot_widget.clear()
        self.plot_widget.setTitle('adev plot')
        self.plot_widget.setLabel('bottom', "Tau", "s")
        self.plot_widget.setLabel('left', "adev")
        self.plot_widget.setLogMode(True, True)
        for i in range(2, self.data.shape[1]):
            (tau2, ad, ade, adn) = allantools.adev(self.data[:,i], rate=1, data_type="freq", taus='decade')
            self.curve = self.plot_widget.plot()
            self.curve.setData(tau2, ad, pen=5*i, name='#%s'%i)

    def relativeAdevPlot(self):
        self.plot_widget.clear()
        self.plot_widget.setTitle('relative adev plot')
        self.plot_widget.setLabel('bottom', "Tau", "s")
        self.plot_widget.setLabel('left', "relative adev")
        self.plot_widget.setLogMode(True, True)
        for i in range(2, self.data.shape[1]):
            (tau2, ad, ade, adn) = allantools.adev(self.data[:,i]/self.data[:,i].mean(), rate=1, data_type="freq", taus='decade')
            self.curve = self.plot_widget.plot()
            self.curve.setData(tau2, ad, pen=5*i, name='#%s'%i)

    def main(self):
        self.show()

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    allanplot = allanplot()
    allanplot.main()
    app.exec_()