def generate_graph(self, csv_file, save_path):
        ''' Generate a graph

        This function generates a graph with data from csv_file.

         Each line (PlotData) in the graph have some settings (parameters). For example:
            - x values - [List] X values on the graph
            - y values - [List] Y values on the graph
            - color - [String] What color the line is going to have
            - label - [String] Label/name of the line. Used for "plt.legend"

         Even the graph (gh) have some settings (parameters). For example:
            - gh(line (plot) 1, line (plot) 2, Title on graph, x label, y label, ticks settings (more information
            about this further down), save path
            plot1 - [PlotData obj] Obj containing line data
            plot2 - [PlotData obj] Obj containing line data
            title,- [String] Title on the graph
            xlabel - [String] X-label
            ylabel - [String] Y-label
            validation - [Boolean] A flag that determines how the y-values are going to be generated (plt.yticks). So right
            now there are two different settings (true or false). (If you need more settings this could be changed to a
            string or something.)
            save_path - [String] Where the graph is going to be saved.

        :args:
            :param csv_file: The location of the csv file where the data are collected
            :param save_path: The location where the graph is going to be saved at
        '''

        # Read the csv file
        df = pd.read_csv(csv_file)

        # Epochs
        epochs = []
        for x in range(len(df['loss'])):
            epochs.append(x)

        # Accuracy
        plot1 = PlotData(epochs, df['val_accuracy'], "red",
                         "Validation accuracy")
        plot2 = PlotData(epochs, df['accuracy'], "blue", "Training accuracy")

        graph = gh(plot1, plot2, "TITLE", "Training Epoch",
                   "Training Accuracy", False, save_path)
        graph.generate_graph()
        del plot1, plot2, graph

        # Loss
        plot1 = PlotData(epochs, df['val_loss'], "red", "Validation loss")
        plot2 = PlotData(epochs, df['loss'], "blue", "Training loss")

        graph = gh(plot1, plot2, "TITLE", "Training Epoch", "Training Loss",
                   True, save_path)
        graph.generate_graph()
        del plot1, plot2, graph
Example #2
0
    def __init__(self):
        """Create a PlotView instance"""
        QFrame.__init__(self)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # setup some default data values
        self.data = PlotData()
        self.data.x_data_type = "number"
        self.data.setValid(False)

        self.plot_figure = PlotFigure()
        self.plot_settings = PlotSettings()

        self.canvas = FigureCanvas(self.plot_figure.getFigure())
        self.canvas.setParent(self)
        self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        self.mouse_handler = MouseHandler(self)
    def start(self):
        """ Initialise audio, make DetectorBank, open PlotData window and 
            start audio 
        """
        
        print('Initializing audio...')
        deviceIdx = self.deviceBox.currentIndex()
        device = self.availableDevices[deviceIdx]
        self.initializeAudio(device)
        
        print('Making DetectorBank...')
        pitchOffset = self.makeDetectorBank()
        
        print('Making PlotData object...')
        self.pd = PlotData(self.db.getChans(), pitchOffset)
#        self.pd.show()
        
        print('Starting audio...')
        self.startAudio()
def plotDelayDistribution(q):

    ATBM_data = []
    SQ_data = []
    ATBM_file = "F:\data\experiment/Seperate_Delay_ATBM_q" + str(q) + ".xlsx"
    SQ_file = "F:\data\experiment/Seperate_Delay_SQ_q" + str(q) + ".xlsx"
    #获取各类延迟的数据
    for j in range(4, 10):
        ATBM_data.append(caculateSumOfSeperateDelay(ATBM_file, j))
        SQ_data.append(caculateSumOfSeperateDelay(SQ_file, j))

#数据进行归于话处理
    ATBM_data = np.array(ATBM_data)
    SQ_data = np.array(SQ_data)
    ATBM_data = np.round(ATBM_data / np.sum(ATBM_data), 2)
    SQ_data = np.round(SQ_data / np.sum(SQ_data), 2)
    data = [ATBM_data, SQ_data]
    colors = ("k", "w")
    labels = ("ATBM", "SQ")
    title = u"队列级数为" + str(q) + u"时ATBM和SQ下不同延迟的分布"
    xticks = []
    for i in range(5):
        xticks.append("d=" + str(i))
    xticks.append("d>=" + str(4))
    plot = PlotData()
    plot.plotBar(data,
                 colors,
                 labels,
                 title,
                 xlabel=u"请求延迟",
                 ylabel=u"延迟对应的比例",
                 ylimit=1,
                 xticks=xticks)

    print("ATBM各级延迟", ATBM_data)
    print("SQ各级延迟", SQ_data)
    '''
def analyzeCompareResult(q):
    wrFile = WRFile()
    #fileKind = "F:\\data\\experiment/Delay_SQ_q"
    fileKind = "F:\\data\\experiment/Delay_q"
    x = wrFile.readDataFromExcel(filePath=fileKind + str(q) + ".xlsx",
                                 min_cols=1,
                                 max_cols=1)
    y = wrFile.readDataFromExcel(filePath=fileKind + str(q) + ".xlsx",
                                 min_cols=2,
                                 max_cols=2)
    z = wrFile.readDataFromExcel(filePath=fileKind + str(q) + ".xlsx",
                                 min_cols=3,
                                 max_cols=3)
    a = wrFile.readDataFromExcel(filePath=fileKind + str(q) + ".xlsx",
                                 min_cols=5,
                                 max_cols=5)
    #plot4DSeperate(x,y,z,a,q)
    PlotData().plot4D(x, y, z, a, q)
Example #6
0
    def __init__(self):
        """Create a PlotView instance"""
        QFrame.__init__(self)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # setup some default data values
        self.data = PlotData()
        self.data.x_data_type = "number"
        self.data.setValid(False)

        self.plot_figure = PlotFigure()
        self.plot_settings = PlotSettings()

        self.canvas = FigureCanvas(self.plot_figure.getFigure())
        self.canvas.setParent(self)
        self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        self.mouse_handler = MouseHandler(self)
class Visualizer(QMainWindow):
    
    def __init__(self):
        
        super().__init__()
        self.initUI()
        self._buflen = 1440 # 4096


    def initUI(self):
        
        # main window/layout
        window = QWidget()
        layout = QVBoxLayout()
        
        # layout for audio device and sample rate selection
        deviceLayout = QHBoxLayout()
        
        # make audio device selection box and list of available devices
        self.deviceBox = QComboBox()
        defaultDeviceInfo = QAudioDeviceInfo.defaultInputDevice()
        self.availableDevices = [defaultDeviceInfo]
        self.availableDevices += QAudioDeviceInfo.availableDevices(
                                                             QAudio.AudioInput)
        for device in self.availableDevices:
            self.deviceBox.addItem(device.deviceName())
            
        # make sample rate label and combobox
        sRateLabel = QLabel("Sample rate:")
        sRateLabel.setAlignment(Qt.AlignRight)
        
        # user can choose between 44.1 and 48kHz (valid DetectorBank rates)
        self.sRateBox = QComboBox()
        self.sRateBox.addItem("44100")
        self.sRateBox.addItem("48000")
        self.sRateBox.setCurrentIndex(1)
        
        # add device and sr widgets to device layout
        deviceLayout.addWidget(self.deviceBox)
        deviceLayout.addWidget(sRateLabel)
        deviceLayout.addWidget(self.sRateBox)
        
        # add device layout to main layout
        layout.addLayout(deviceLayout)
        
        # DetectorBank parameters layout
        # two rows of three parameters
        # each param needs label and edit, 
        # and a 'Start' button will be added at the bottom
        # so grid should be 3x6
        detBankParamLayout = QGridLayout()
        
        # label and lineedit for each
        bandwidthLabel = QLabel("Bandwidth (cents):")
        dampingLabel = QLabel("Damping:")
        gainLabel = QLabel("Gain:")
        edoLabel = QLabel("EDO:")
        lwrLabel = QLabel("Lower note:")
        uprLabel = QLabel("Upper note:")
        
        self.bandwidthEdit = QLineEdit("0")
        self.dampingEdit = QLineEdit("0.0001")
        self.gainEdit = QLineEdit("25")
        self.edoEdit = QLineEdit("12")
        self.lwrEdit = QLineEdit("A1")
        self.uprEdit = QLineEdit("A7")
        
        # store all in lists
        detBankParamLabels = [bandwidthLabel, dampingLabel, gainLabel, 
                              edoLabel, lwrLabel, uprLabel]
        
        detBankParamEdits = [self.bandwidthEdit, self.dampingEdit, 
                             self.gainEdit, self.edoEdit, self.lwrEdit, 
                             self.uprEdit]
         
        # fill first two rows of grid with labels and edits
        row = 0

        for row in range(2):
            widgetNum = 0
        
            for i in range((row*3), (row*3)+3):
                detBankParamLayout.addWidget(detBankParamLabels[i], row, 
                                             widgetNum)
                widgetNum += 1
                detBankParamLayout.addWidget(detBankParamEdits[i], row, 
                                             widgetNum)
                widgetNum += 1
            
        # align labels to the right (next to the edit)
        for i in range(len(detBankParamLabels)):
            detBankParamLabels[i].setAlignment(Qt.AlignRight)
                
        # button to make DetectorBank and start visualisation
        row += 1
        startButton = QPushButton("&Start!")
        detBankParamLayout.addWidget(startButton, row, 5)
        startButton.clicked.connect(self.start)
        
        # add grid of detbank params (and start button) to main layout
        layout.addLayout(detBankParamLayout)
        
        window.setLayout(layout)
        self.setCentralWidget(window)
        self.show()
        
        
    def initializeAudio(self, deviceInfo):
        """ Make a QAudioInput from the given device """
                
        # make buffers of 40ms of samples
        self.refRate = 0.04
        
        # mono, 32-bit float audio
        fmt = QAudioFormat()
        fmt.setSampleRate(self.getSampleRate())
        fmt.setChannelCount(1)
        fmt.setSampleSize(32)
        fmt.setSampleType(QAudioFormat.Float)
        fmt.setByteOrder(QAudioFormat.LittleEndian)
        fmt.setCodec("audio/pcm")
        
        if not deviceInfo.isFormatSupported(fmt):
            fmt = deviceInfo.nearestFormat(fmt)
            
        self.audioInput = QAudioInput(deviceInfo, fmt)
        self.audioInput.setBufferSize(4*self.buflen) # set size in bytes

        
    def startAudio(self):
        self.audioDevice = self.audioInput.start()
        self.audioDevice.readyRead.connect(self.updatePlot)
        
        
    def start(self):
        """ Initialise audio, make DetectorBank, open PlotData window and 
            start audio 
        """
        
        print('Initializing audio...')
        deviceIdx = self.deviceBox.currentIndex()
        device = self.availableDevices[deviceIdx]
        self.initializeAudio(device)
        
        print('Making DetectorBank...')
        pitchOffset = self.makeDetectorBank()
        
        print('Making PlotData object...')
        self.pd = PlotData(self.db.getChans(), pitchOffset)
#        self.pd.show()
        
        print('Starting audio...')
        self.startAudio()
        
    
    def updatePlot(self):
        
        # get data as float32
        # 4*buflen is number of bytes
        data = self.audioDevice.read(4*self.buflen)
        data = np.frombuffer(data, dtype=np.int16)
        data = np.array(data/2**15, dtype=np.dtype('float32'))
        
        # set DetectorBank input
        self.db.setInputBuffer(data)
          
        # fill z with detector output
        self.db.getZ(self.z)
        
#        self.db.absZ(self.r, self.z)
        
        self.pd.update(self.z)
        
#        self.close()
        
        
    def makeDetectorBank(self):
        """ Make DetectorBank from given parameters """
        
        sr = self.getSampleRate()
        bandwidth_cents = float(self.bandwidthEdit.text())
        dmp = float(self.dampingEdit.text())
        gain = float(self.gainEdit.text())
        edo = float(self.edoEdit.text())
        lwr = self.lwrEdit.text()
        upr = self.uprEdit.text()
        
        lwr, pitchOffset = getNoteNum(lwr, edo)
        upr, _ = getNoteNum(upr, edo)
        upr += 1 # include upr note in DetectorBank
        
        # make and fill frequency and bandwidth arrays
        freq = np.zeros(int(upr-lwr))
        bw = np.zeros(len(freq))
        for i in range(len(freq)):
            k = lwr+i
            freq[i] = 440*2**(k/edo)
            # if non-minimum bandwidth detectors requested, find B in Hz
            if bandwidth_cents != 0:
                bw[i] = centsToHz(freq[i], bandwidth_cents, edo)
                
        # combine into stacked array
        det_char = np.stack((freq,bw), axis=1)
        
        # (almost) empty input buffer
        buffer = np.zeros(1, dtype=np.float32)
        
        # DetectorBank features
        method = DetectorBank.runge_kutta
        f_norm = DetectorBank.freq_unnormalized
        a_norm = DetectorBank.amp_unnormalized
        
        self.db = DetectorBank(sr, buffer, 4, det_char, method|f_norm|a_norm, 
                                dmp, gain)
        
        # create empty output array
        self.z = np.zeros((int(self.db.getChans()),self.buflen), 
                          dtype=np.complex128)
        
        self.r = np.zeros(self.z.shape)
        
        print("Made DetectorBank with {} channels, with a sample rate of {}Hz"
              .format(self.db.getChans(), self.db.getSR()))
        
        return pitchOffset
        
    ## get and/or set various values
    def getSampleRate(self, returnType=int):
        return returnType(self.sRateBox.currentText())
    
    @property
    def refreshRate(self):
        return self._refRate
    
    @refreshRate.setter
    def refreshRate(self, value):
        self._refRate = value
        self.buflen = self._refRate * self.getSampleRate()
    
    @property
    def buflen(self):
        return self._buflen
        
    @buflen.setter
    def buflen(self, value):
        self._buflen = int(value)
Example #8
0
class PlotView(QFrame):
    """PlotView presents a matplotlib canvas with interaction possibilities. (picking and tooltip)"""

    def __init__(self):
        """Create a PlotView instance"""
        QFrame.__init__(self)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # setup some default data values
        self.data = PlotData()
        self.data.x_data_type = "number"
        self.data.setValid(False)

        self.plot_figure = PlotFigure()
        self.plot_settings = PlotSettings()

        self.canvas = FigureCanvas(self.plot_figure.getFigure())
        self.canvas.setParent(self)
        self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        self.mouse_handler = MouseHandler(self)

    def toggleMember(self, line):
        gid = int(line.get_gid())
        if gid in self.plot_settings.getSelectedMembers():
            self.plot_settings.unselectMember(gid)
        else:
            self.plot_settings.selectMember(gid)

    @ert_gui.widgets.util.may_take_a_long_time
    def drawPlot(self):
        self.plot_figure.drawPlot(self.data, self.plot_settings)        
        self.canvas.draw()

    def resizeEvent(self, event):
        QFrame.resizeEvent(self, event)
        self.canvas.resize(event.size().width(), event.size().height())

    def loadSettings(self, name):
        if self.data.isValid():
            plot_config_loader = PlotSettingsLoader()
            if not plot_config_loader.load(name, self.plot_settings):
                self.drawPlot()

    def saveSettings(self):
        if self.data.isValid():
            plot_config_saver = PlotSettingsSaver()
            plot_config_saver.save(self.data.getSaveName(), self.plot_settings)

    def setData(self, data):
        self.saveSettings()

        self.data = data

        self.loadSettings(self.data.getSaveName())


    def setXZoomFactors(self, xminf, xmaxf):
        self.plot_settings.setMinXZoom(xminf)
        self.plot_settings.setMaxXZoom(xmaxf)

    def setYZoomFactors(self, yminf, ymaxf):
        self.plot_settings.setMinYZoom(yminf)
        self.plot_settings.setMaxYZoom(ymaxf)

    def save(self):
        self.saveSettings()

        plot_generator = PlotGenerator(self.plot_settings.getPlotPath(), self.plot_settings.getPlotConfigPath())
        plot_generator.save(self.data)

    def saveAll(self):
        self.saveSettings()

        plot_generator = PlotGenerator(self.plot_settings.getPlotPath(), self.plot_settings.getPlotConfigPath())
        plot_generator.saveAll()
        
    def copyPlotSettings(self):
        plot_config_loader = PlotSettingsLoader()
        plot_config_loader.copy(self.plot_settings)

    def setPlotPath(self, plot_path):
        self.plot_settings.setPlotPath(plot_path)

    def setPlotConfigPath(self, path):
        self.plot_settings.setPlotConfigPath(path)

    def _selectedMemberIdentifier(self, artist):
        return artist.get_gid() in self.plot_settings.getSelectedMembers()

    def clearSelection(self):
        selected_lines = self.plot_figure.fig.findobj(self._selectedMemberIdentifier)
        for line in selected_lines:
            self.plot_settings.unselectMember(line.get_gid())


    def displayToolTip(self, event):
        if not self.data is None and not event.xdata is None and not event.ydata is None:
            if self.data.getXDataType() == "time":
                date = matplotlib.dates.num2date(event.xdata)
                self.setToolTip("x: %s y: %04f" % (date.strftime("%d/%m-%Y"), event.ydata))
            else:
                self.setToolTip("x: %04f y: %04f" % (event.xdata, event.ydata))
        else:
            self.setToolTip("")

    def annotate(self, label, x, y, xt=None, yt=None):
        self.plot_settings.addAnnotation(label, x, y, xt, yt)

    def removeAnnotation(self, annotation_artist):
        annotations = self.plot_settings.getAnnotations()
        for annotation in annotations:
            if annotation.getUserData() == annotation_artist:
                self.plot_settings.removeAnnotation(annotation)

    def moveAnnotation(self, annotation_artist, xt, yt):
        annotations = self.plot_settings.getAnnotations()
        for annotation in annotations:
            if annotation.getUserData() == annotation_artist:
                annotation.xt = xt
                annotation.yt = yt

        annotation_artist.xytext = (xt, yt)

    def draw(self):
        self.canvas.draw()

    def setMinYLimit(self, value):
        self.plot_settings.setMinYLimit(value)

    def setMaxYLimit(self, value):
        self.plot_settings.setMaxYLimit(value)

    def setMinXLimit(self, value):
        self.plot_settings.setMinXLimit(value)

    def setMaxXLimit(self, value):
        self.plot_settings.setMaxXLimit(value)

    def getPlotConfigList(self):
        return self.plot_settings.getPlotConfigList()
Example #9
0
class PlotView(QFrame):
    """PlotView presents a matplotlib canvas with interaction possibilities. (picking and tooltip)"""
    def __init__(self):
        """Create a PlotView instance"""
        QFrame.__init__(self)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # setup some default data values
        self.data = PlotData()
        self.data.x_data_type = "number"
        self.data.setValid(False)

        self.plot_figure = PlotFigure()
        self.plot_settings = PlotSettings()

        self.canvas = FigureCanvas(self.plot_figure.getFigure())
        self.canvas.setParent(self)
        self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        self.mouse_handler = MouseHandler(self)

    def toggleMember(self, line):
        gid = int(line.get_gid())
        if gid in self.plot_settings.getSelectedMembers():
            self.plot_settings.unselectMember(gid)
        else:
            self.plot_settings.selectMember(gid)

    @ert_gui.widgets.util.may_take_a_long_time
    def drawPlot(self):
        self.plot_figure.drawPlot(self.data, self.plot_settings)
        self.canvas.draw()

    def resizeEvent(self, event):
        QFrame.resizeEvent(self, event)
        self.canvas.resize(event.size().width(), event.size().height())

    def loadSettings(self, name):
        if self.data.isValid():
            plot_config_loader = PlotSettingsLoader()
            if not plot_config_loader.load(name, self.plot_settings):
                self.drawPlot()

    def saveSettings(self):
        if self.data.isValid():
            plot_config_saver = PlotSettingsSaver()
            plot_config_saver.save(self.data.getSaveName(), self.plot_settings)

    def setData(self, data):
        self.saveSettings()

        self.data = data

        self.loadSettings(self.data.getSaveName())

    def setXZoomFactors(self, xminf, xmaxf):
        self.plot_settings.setMinXZoom(xminf)
        self.plot_settings.setMaxXZoom(xmaxf)

    def setYZoomFactors(self, yminf, ymaxf):
        self.plot_settings.setMinYZoom(yminf)
        self.plot_settings.setMaxYZoom(ymaxf)

    def save(self):
        self.saveSettings()

        plot_generator = PlotGenerator(self.plot_settings.getPlotPath(),
                                       self.plot_settings.getPlotConfigPath())
        plot_generator.save(self.data)

    def saveAll(self):
        self.saveSettings()

        plot_generator = PlotGenerator(self.plot_settings.getPlotPath(),
                                       self.plot_settings.getPlotConfigPath())
        plot_generator.saveAll()

    def copyPlotSettings(self):
        plot_config_loader = PlotSettingsLoader()
        plot_config_loader.copy(self.plot_settings)

    def setPlotPath(self, plot_path):
        self.plot_settings.setPlotPath(plot_path)

    def setPlotConfigPath(self, path):
        self.plot_settings.setPlotConfigPath(path)

    def _selectedMemberIdentifier(self, artist):
        return artist.get_gid() in self.plot_settings.getSelectedMembers()

    def clearSelection(self):
        selected_lines = self.plot_figure.fig.findobj(
            self._selectedMemberIdentifier)
        for line in selected_lines:
            self.plot_settings.unselectMember(line.get_gid())

    def displayToolTip(self, event):
        if not self.data is None and not event.xdata is None and not event.ydata is None:
            if self.data.getXDataType() == "time":
                date = matplotlib.dates.num2date(event.xdata)
                self.setToolTip("x: %s y: %04f" %
                                (date.strftime("%d/%m-%Y"), event.ydata))
            else:
                self.setToolTip("x: %04f y: %04f" % (event.xdata, event.ydata))
        else:
            self.setToolTip("")

    def annotate(self, label, x, y, xt=None, yt=None):
        self.plot_settings.addAnnotation(label, x, y, xt, yt)

    def removeAnnotation(self, annotation_artist):
        annotations = self.plot_settings.getAnnotations()
        for annotation in annotations:
            if annotation.getUserData() == annotation_artist:
                self.plot_settings.removeAnnotation(annotation)

    def moveAnnotation(self, annotation_artist, xt, yt):
        annotations = self.plot_settings.getAnnotations()
        for annotation in annotations:
            if annotation.getUserData() == annotation_artist:
                annotation.xt = xt
                annotation.yt = yt

        annotation_artist.xytext = (xt, yt)

    def draw(self):
        self.canvas.draw()

    def setMinYLimit(self, value):
        self.plot_settings.setMinYLimit(value)

    def setMaxYLimit(self, value):
        self.plot_settings.setMaxYLimit(value)

    def setMinXLimit(self, value):
        self.plot_settings.setMinXLimit(value)

    def setMaxXLimit(self, value):
        self.plot_settings.setMaxXLimit(value)

    def getPlotConfigList(self):
        return self.plot_settings.getPlotConfigList()