예제 #1
0
class Main(QMainWindow, Ui_MainWindow):
	def __init__(self, ):
		super(Main, self).__init__()
		self.setupUi(self)
		self.fig_dict = {}

		self.mplfigs.itemClicked.connect(self.change_fig)


	def change_fig(self, item):
		text = item.text()
		self.rm_mpl()
		self.add_mpl(self.fig_dict[text])


	def add_mpl(self, fig):
		self.canvas = FigureCanvas(fig)
		self.mplvl.addWidget(self.canvas)
		self.canvas.draw()

		self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
		self.addToolBar(self.toolbar)


	def rm_mpl(self, ):
		self.mplvl.removeWidget(self.canvas)
		self.canvas.close()
		self.mplvl.removeWidget(self.toolbar)
		self.toolbar.close()


	def add_fig(self, name, fig):
		self.fig_dict[name] = fig
		self.mplfigs.addItem(name)
예제 #2
0
class Main(Ui_MainWindow, QMainWindow):
    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)
        self.fig_dict = {}

        self.L1.itemClicked.connect(self.changefig)

        # self.chk1.setVisible(False)
        # self.chk1.setChecked(True)
        self.setWindowIcon(QtGui.QIcon('ipco-logo.png'))

    def changefig(self, item):
        text = item.text()
        self.rmmpl()
        self.addmpl(self.fig_dict[text])

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.mplvl.addWidget(self.toolbar)
        self.rmmpl()

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def addfig(self, name, fig):
        self.fig_dict[name] = fig
        self.L1.addItem(name)
예제 #3
0
class EDXmain(QtWidgets.QWidget):
    ''' Interface to get a list of points from an existing plot '''

    # http://blog.rcnelson.com/building-a-matplotlib-gui-with-qt-designer-part-1/
    def __init__(self, ):
        super(EDXmain, self).__init__()  # gets inherited methods of this class
        self.initUI()
        self.fig_dict = {}  # list of active figures
        # click on item in Qlistwidget triggers change of figures
        self.mplfigs.itemClicked.connect(self.changefig)
        # add select data subset button here

    def initUI(self):
        self.setGeometry(600, 300, 400, 200)
        self.setWindowTitle('EDX main window')
        self.show()

    def addmpl(self, fig):
        ''' adds matplotlib plot to qt container'''
        self.canvas = FigureCanvas(fig)  # fig canvas widget
        # mplvl is a QVBoxLayout instance with addWidget method
        self.mplvl.addWidget(
            self.canvas)  # mplvl is layout name within container in .ui window
        # could also be directly added to mplwindow container but then scales differently
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)  # toolbar added below passed plot
        self.canvas.mpl_connect('key_press_event', self.on_key_press)

    def rmmpl(self, ):
        ''' remove existing plots  '''
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def addfig(self, name, fig):
        ''' Adds name, underlying fig to dictionary and displays in window '''
        self.fig_dict[name] = fig
        self.mplfigs.addItem(name)  # adds name to QlistWidget

    def changefig(self, item):
        text = item.text()
        self.rmmpl()  # removes existing
        self.addmpl(self.fig_dict[text])

    def on_key_press(self, event):
        ''' get coords on plot '''
        ix, iy = event.xdata, event.ydata
        print('Coords are: ', str(ix), str(iy))
class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.plotField.clicked.connect(self.plotfield)

        #Notice that an empty figure instance was added to our plotting window in the initialization method.
        #This is necessary because our first call to changefig will try to remove a previously displayed figure,
        #which will throw an error if one is not displayed.
        #The empty figure serves as a placeholder so the changefig method functions properly.
        fig = Figure()
        self.addmpl(fig)

    def plotfield(self):
        wavelen = float(self.wavelen.toPlainText())
        modenum = int(self.modeNum.toPlainText())
        Zin = float(self.z.toPlainText())

        x, ET = genAM.doubleSlit(wavelen, modenum, Zin)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)
        ax1f1.set_title('Beam Pattern')

        ax1f1.plot(x, np.abs(ET)**2, label='Beam Pattern at %s m' % Zin)
        ax1f1.set_xlabel('x [m]')
        ax1f1.set_ylabel('Intensity')
        ax1f1.legend(loc='best')
        ax1f1.grid()

        self.rmmpl()
        self.addmpl(figx)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl_0.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl_0.addWidget(self.toolbar)

    def rmmpl(self):
        self.mplvl_0.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl_0.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #5
0
파일: test_plot.py 프로젝트: roesel/k237
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #6
0
class MplWidget(QWidget):
    '''
    This class creates figures in the UI
    '''
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.canvas = FigureCanvas(Figure())
        self.vertical_layout = QVBoxLayout()
        self.vertical_layout.addWidget(self.canvas)
        self.toolbar = NavigationToolbar(self.canvas, self)
        self.vertical_layout.addWidget(self.toolbar)
        self.setLayout(self.vertical_layout)
        self.canvas.draw()

    def removePlot(self, ):
        self.vertical_layout.removeWidget(self.canvas)
        self.canvas.close()
        self.vertical_layout.removeWidget(self.toolbar)
        self.toolbar.close()
        self.setLayout(self.vertical_layout)

    def addPlot(self, fig):
        self.canvas = FigureCanvas(fig)
        self.vertical_layout.addWidget(self.canvas)
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.vertical_layout.addWidget(self.toolbar)
        self.canvas.draw()
        self.setLayout(self.vertical_layout)

    def addDynamicPlot(self, fig):
        self.canvas = FigureCanvas(fig)
        self.vertical_layout.addWidget(self.canvas)
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.vertical_layout.addWidget(self.toolbar)
        self.setLayout(self.vertical_layout)
        self.cursor = SnaptoCursor(fig)
        self.cid = self.canvas.mpl_connect('motion_notify_event',
                                           self.cursor.mouse_move)

    def selectPointPlot(self, fig, QListWidget):
        self.cursor = SnaptoCursor(fig, QListWidget)
        self.canvas.mpl_connect('button_press_event', self.cursor.onclick)
        self.canvas.mpl_connect('motion_notify_event', self.cursor.mouse_move)
예제 #7
0
class PlotMaker(FigureCanvas):
    def __init__(self, plotVLayout, parent=None, title: str = None):
        self.toolbar = None
        self.fig = Figure()  # подаем на вход рисунок
        matplotlib.pyplot.close(self.fig)
        self.ax = self.fig.add_subplot(111)
        self.ax.set_title(title)
        self.plotVLayout = plotVLayout  # подаем на вход слой элементов виджета
        self.visualizeData(self.fig)

    def drawPlot(self, fig):
        self.removePlot()
        self.visualizeData(fig)

    def visualizeData(self, fig):
        self.geomForMpl = self.plotVLayout
        self.canvas = TsneMplForWidget(self.fig)
        # self.checkWidget(self.geomForMpl)
        self.geomForMpl.addWidget(self.canvas)

        # self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        # self.geomForMpl.addWidget(self.toolbar)

    def add_toolbar(self, parent):
        self.toolbar = NavigationToolbar(self.canvas, parent, coordinates=True)
        self.plotVLayout.addWidget(self.toolbar)

    # Попытка очистки графика одновременно с рисованием
    def checkWidget(self, plotVLayout):
        lcount = plotVLayout.count()
        if (lcount > 1):
            for i in reversed(range(1, lcount)):
                plotVLayout.itemAt(i).widget().deleteLater()
            # plotVLayout.removeItem(plotVLayout.itemAt(1))

    def removePlot(self):
        self.geomForMpl.removeWidget(self.canvas)
        self.canvas.close()
        self.geomForMpl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #8
0
파일: VPmeasure.py 프로젝트: ntejos/joebvp
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self,specfilename,parfilename=None,wave1=None,wave2=None,numchunks=8,parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        #super(Main,self).__init__()
        self.setupUi(self)

        ### Initialize stuff
        self.line_dict = {}
        self.fitpars = None
        self.parinfo = None
        self.linecmts = None
        self.wave1 = wave1
        self.wave2 = wave2
        self.numchunks = numchunks
        self.spls = []
        self.labeltog = 1
        self.pixtog = 0
        self.restog = 1
        self.fitconvtog = 0
        self.lastclick=1334.

        ### Read in spectrum and list of lines to fit
        self.specfilename=specfilename
        self.spectrum = readspec(specfilename)
        self.wave=self.spectrum.wavelength.value
        self.normflux=self.spectrum.flux/self.spectrum.co
        self.normsig=self.spectrum.sig/self.spectrum.co
        cfg.spectrum = self.spectrum
        cfg.wave=self.wave
        cfg.normflux=self.normflux
        cfg.filename=self.specfilename

        if not parfilename==None:
            self.initialpars(parfilename)


        ### Connect signals to slots
        self.fitButton.clicked.connect(self.fitlines)
        self.fitConvBox.clicked.connect(self.togfitconv)
        self.boxLineLabel.clicked.connect(self.toglabels)
        self.boxFitpix.clicked.connect(self.togfitpix)
        self.boxResiduals.clicked.connect(self.togresiduals)
        self.loadParsButton.clicked.connect(self.openParFileDialog)
        self.addLineButton.clicked.connect(self.addLineDialog)
        self.writeParsButton.clicked.connect(self.writeParFileDialog)
        self.writeModelButton.clicked.connect(self.writeModelFileDialog)
        self.writeModelCompButton.clicked.connect(self.writeModelCompFileDialog)
        self.quitButton.clicked.connect(self.quitGui)

        ### Initialize spectral plots
        fig=Figure()
        self.fig=fig
        self.initplot(fig)

        ### Initialize side plot
        sidefig=Figure(figsize=(5.25,2))
        self.sidefig = sidefig
        self.addsidempl(self.sidefig)
        self.sideplot(self.lastclick)  #Dummy initial cenwave setting


    def initplot(self,fig,numchunks=8):
        wlen=len(self.spectrum.wavelength)/numchunks
        self.spls=[]
        if self.wave1==None:  waveidx1=0  # Default to plotting entire spectrum for now
        else: waveidx1=jbg.closest(self.wave,self.wave1)
        if self.fitpars!=None:
                model=joebvpfit.voigtfunc(self.wave,self.datamodel.fitpars)
        sg=jbg.subplotgrid(numchunks)
        for i in range(numchunks):
            self.spls.append(fig.add_subplot(sg[i][0],sg[i][1],sg[i][2]))
            pixs=range(waveidx1+i*wlen,waveidx1+(i+1)*wlen)
            self.spls[i].plot(self.wave[pixs],self.normflux[pixs],linestyle='steps-mid')
            if self.fitpars!=None:
                self.spls[i].plot(self.wave,model,'r')
            self.spls[i].set_xlim(self.wave[pixs[0]],self.wave[pixs[-1]])
            self.spls[i].set_ylim(cfg.ylim)
            self.spls[i].set_xlabel('wavelength',labelpad=0)
            self.spls[i].set_ylabel('relative flux',labelpad=-5)
            self.spls[i].get_xaxis().get_major_formatter().set_scientific(False)
        fig.subplots_adjust(top=0.98,bottom=0.05,left=0.08,right=0.97,wspace=0.15,hspace=0.25)
        self.addmpl(fig)

    def initialpars(self,parfilename):
        ### Deal with initial parameters from line input file
        self.fitpars,self.fiterrors,self.parinfo,self.linecmts = joebvpfit.readpars(parfilename)
        cfg.fitidx=joebvpfit.fitpix(self.wave, self.fitpars) #Set pixels for fit
        cfg.wavegroups=[]
        self.datamodel = LineParTableModel(self.fitpars,self.fiterrors,self.parinfo,linecmts=self.linecmts)
        self.tableView.setModel(self.datamodel)
        self.datamodel.updatedata(self.fitpars,self.fitpars,self.parinfo,self.linecmts)

    def sideplot(self,cenwave,wavebuf=3):
        if len(self.sidefig.axes)==0:
            self.sideax=self.sidefig.add_subplot(111)
        self.sideax.clear()
        self.sideax.plot(self.wave, self.normflux, linestyle='steps-mid')
        if self.pixtog == 1:
            self.sideax.plot(self.wave[cfg.fitidx], self.normflux[cfg.fitidx], 'gs', markersize=4, mec='green')
        model = joebvpfit.voigtfunc(self.wave, self.fitpars)
        res = self.normflux - model
        self.sideax.plot(self.wave, model, 'r')
        if self.restog == 1:
            self.sideax.plot(self.wave, -res, '.', color='black', ms=2)
        self.sideax.plot(self.wave, [0] * len(self.wave), color='gray')

        ### label lines we are trying to fit
        if self.labeltog == 1:
            for j in range(len(self.fitpars[0])):
                labelloc = self.fitpars[0][j] * (1. + self.fitpars[3][j]) + self.fitpars[4][j] / c * \
                                                                            self.fitpars[0][j] * (
                                                                            1. + self.fitpars[3][j])
                label = ' {:.1f}_\nz{:.4f}'.format(self.fitpars[0][j], self.fitpars[3][j])
                self.sideax.text(labelloc, cfg.label_ypos, label, rotation=90, withdash=True, ha='center', va='bottom',
                                 clip_on=True, fontsize=cfg.label_fontsize)

        self.sideax.plot(self.wave, self.normsig, linestyle='steps-mid', color='red', lw=0.5)
        self.sideax.plot(self.wave, -self.normsig, linestyle='steps-mid', color='red', lw=0.5)
        self.sideax.get_xaxis().get_major_formatter().set_scientific(False)
        self.sideax.get_xaxis().get_major_formatter().set_useOffset(False)
        try:
            self.sideax.set_xlim(cenwave-wavebuf,cenwave+wavebuf)
            self.sideax.set_ylim(cfg.ylim)
            self.changesidefig(self.sidefig)
        except TypeError:
            pass
    def fitlines(self):
        print('VPmeasure: Fitting line profile(s)...')
        print(len(self.fitpars[0]),'lines loaded for fitting.')
        if self.fitconvtog:
            self.fitpars, self.fiterrors = joebvpfit.fit_to_convergence(self.wave, self.normflux, self.normsig,
                                                               self.datamodel.fitpars, self.datamodel.parinfo)
        else:
            self.fitpars, self.fiterrors = joebvpfit.joebvpfit(self.wave, self.normflux,self.normsig, self.datamodel.fitpars,self.datamodel.parinfo)
        self.datamodel.updatedata(self.fitpars,self.fiterrors,self.parinfo,self.linecmts)
        self.tableView.resizeColumnsToContents()
        self.updateplot()
        self.sideplot(self.lastclick)

    def togfitconv(self):
        if self.fitconvtog==1: self.fitconvtog=0
        else: self.fitconvtog=1

    def quitGui(self):
        self.deleteLater()
        self.close()

    def toglabels(self):
        if self.labeltog==1: self.labeltog=0
        else: self.labeltog=1
        self.updateplot()

    def togfitpix(self):
        if self.pixtog == 1:
            self.pixtog = 0
        else:
            self.pixtog = 1
        self.updateplot()

    def togresiduals(self):
        if self.restog == 1:
            self.restog = 0
        else:
            self.restog = 1
        self.updateplot()

    def openParFileDialog(self):
        fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Open line parameter file','.')
        fname = str(fname[0])
        if fname != '':
            self.initialpars(fname)

        self.updateplot()

    def writeParFileDialog(self):
        fname = QtWidgets.QFileDialog.getSaveFileName(self, 'Save line parameter file', cfg.VPparoutfile)
        fname = str(fname[0])
        if fname != '':
            joebvpfit.writelinepars(self.datamodel.fitpars, self.datamodel.fiterrors, self.datamodel.parinfo, self.specfilename, fname, self.datamodel.linecmts)

    def writeModelFileDialog(self):
        fname = QtWidgets.QFileDialog.getSaveFileName(self, 'Save model to file', cfg.VPmodeloutfile)
        fname = str(fname[0])
        if fname != '':
            joebvpfit.writeVPmodel(fname, self.wave, self.fitpars, self.normflux, self.normsig)

    def writeModelCompFileDialog(self):
        dirDialog = QtWidgets.QFileDialog(self)
        dirDialog.setFileMode(dirDialog.Directory)
        dirDialog.setOption(dirDialog.ShowDirsOnly, True)
        defDirName = cfg.VPmodeloutfile[:-5]
        dname = dirDialog.getSaveFileName(self, 'Save model to files split by components',defDirName)
        dname = str(dname[0])
        if dname != '':
            joebvpfit.writeVPmodelByComp(dname, self.spectrum,self.fitpars)

    def addLineDialog(self):
        dlgOutput=newLineDialog.get_newline()
        if (dlgOutput != 0):
            if '' not in dlgOutput:
                newlam,newz,newcol,newb,newvel,newvel1,newvel2 = dlgOutput
                self.datamodel.addLine(self.wave,float(newlam), float(newz), float(newcol), float(newb), float(newvel), float(newvel1), float(newvel2))        #dialog=newLineDialog(parent=None)
                self.fitpars = self.datamodel.fitpars
                self.fiterrors = self.datamodel.fiterrors
                self.parinfo = self.datamodel.parinfo
                self.tableView.setModel(self.datamodel)


    def updateplot(self):
        if self.wave1==None:  waveidx1=0  # Default to plotting entire spectrum for now
        else: waveidx1=jbg.closest(self.wave,self.wave1)
        wlen=len(self.spectrum.wavelength)/self.numchunks
        for i,sp in enumerate(self.spls):
                sp.clear()
                prange=range(waveidx1+i*wlen,waveidx1+(i+1)*wlen)
    
                if ((len(self.fitpars[0])>0)):
    
                    sp.plot(self.wave,self.normflux,linestyle='steps-mid')
                    if self.pixtog==1:
                        sp.plot(self.wave[cfg.fitidx], self.normflux[cfg.fitidx], 'gs', markersize=4, mec='green')
                    model=joebvpfit.voigtfunc(self.wave,self.fitpars)
                    res=self.normflux-model
                    sp.plot(self.wave,model,'r')
                    if self.restog==1:
                        sp.plot(self.wave,-res,'.',color='black', ms=2)
                    sp.plot(self.wave,[0]*len(self.wave),color='gray')
    
                    ### label lines we are trying to fit
                    if self.labeltog==1:
                        for j in range(len(self.fitpars[0])):
                            labelloc=self.fitpars[0][j]*(1.+self.fitpars[3][j])+self.fitpars[4][j]/c*self.fitpars[0][j]*(1.+self.fitpars[3][j])
                            label = ' {:.1f}_\nz{:.4f}'.format(self.fitpars[0][j], self.fitpars[3][j])
                            sp.text(labelloc, cfg.label_ypos, label, rotation=90, withdash=True, ha='center', va='bottom', clip_on=True, fontsize=cfg.label_fontsize)
                
    
                sp.plot(self.wave,self.normsig,linestyle='steps-mid',color='red', lw=0.5)
                sp.plot(self.wave,-self.normsig,linestyle='steps-mid',color='red', lw=0.5)
                sp.set_ylim(cfg.ylim)
                sp.set_xlim(self.wave[prange[0]],self.wave[prange[-1]])
                sp.set_xlabel('wavelength (A)', fontsize=cfg.xy_fontsize, labelpad=cfg.x_labelpad)
                sp.set_ylabel('normalized flux', fontsize=cfg.xy_fontsize, labelpad=cfg.y_labelpad)
                sp.get_xaxis().get_major_formatter().set_scientific(False)
                sp.get_xaxis().get_major_formatter().set_useOffset(False)

        self.changefig(self.fig)

    def changefig(self, item):
        #text = str(item.text())
        self.rmmpl()
        self.addmpl(self.fig)

    def changesidefig(self, item):
        #text = str(item.text())
        self.rmsidempl()
        self.addsidempl(self.sidefig)
        
        
    def on_click(self, event):
        self.lastclick=event.xdata
        self.sideplot(self.lastclick)
        
    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.canvas.mpl_connect('button_press_event',self.on_click)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,self.mplwindow,coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def addsidempl(self, sidefig):
        self.sidecanvas = FigureCanvas(sidefig)
        self.sidecanvas.setParent(self.sideMplWindow)
        if len(self.sidefig.axes) == 0:
            self.sidemplvl = QVBoxLayout()
        if len(self.sidefig.axes) != 0:
            self.sidemplvl.addWidget(self.sidecanvas)
        if len(self.sidefig.axes) == 0:
            self.sideMplWindow.setLayout(self.sidemplvl)
        self.sidecanvas.draw()

        
    def rmmpl(self,):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def rmsidempl(self, ):
        self.sidemplvl.removeWidget(self.sidecanvas)
        self.sidecanvas.close()
class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.simpleData.clicked.connect(self.simpleModel)
        self.realData.clicked.connect(self.realModel)
        self.BBdata.clicked.connect(self.BBspec)

        #Notice that an empty figure instance was added to our plotting window in the initialization method.
        #This is necessary because our first call to changefig will try to remove a previously displayed figure,
        #which will throw an error if one is not displayed.
        #The empty figure serves as a placeholder so the changefig method functions properly.
        fig = Figure()
        fig2 = Figure()
        fig3 = Figure()
        fig4 = Figure()
        fig5 = Figure()
        self.addmpl(fig)
        self.addmpl_2(fig2)
        self.addmpl_3(fig3)
        self.addmpl_4(fig4)
        self.addmpl_5(fig5)

    def BBspec(self):
        wavelen = np.linspace(float(self.min.toPlainText()),
                              float(self.max.toPlainText()),
                              float(self.samples.toPlainText())) * 1e-6

        path = 'C:/Users/Joe/Documents/Python Scripts/Scatter Pixel Code/Consolidated_Filter.txt'

        BBT = float(self.BBtemp.toPlainText())
        specIr = BBS.blackBodySpec(wavelen, BBT)
        FilterCoeff = FT.filterTransmission(path, wavelen)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)
        ax1f1.set_title('BBspec Irradiance')
        ax1f1.plot(wavelen, baseUnits(specIr), label='Simple Model')
        ax1f1.plot(wavelen,
                   baseUnits(specIr) * FilterCoeff,
                   label='After Filter')
        ax1f1.set_xlabel('Wavelength (m)')
        ax1f1.set_ylabel('Amp (%s)' % (specIr.units))
        ax1f1.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
        ax1f1.legend(loc='best')
        ax1f1.grid()
        #print('okay')
        self.rmmpl_3()
        self.addmpl_3(figx)

    def realModel(self):
        ##Load and Store data
        wavelen = np.linspace(float(self.min.toPlainText()),
                              float(self.max.toPlainText()),
                              float(self.samples.toPlainText()))

        absEff0pol, absEff90pol = np.empty(len(wavelen)), np.empty(
            len(wavelen))
        apertureData0pol, apertureData90pol = [], []
        print(wavelen)

        step = 100 / (2 * len(wavelen))
        i = 0
        self.progressBar.setValue(i)
        for wnIdx, wn in enumerate(wavelen):
            absEff0pol[wnIdx], apertureData0poln = SPI.scatterPixelIntegrator(
                wn, 0, 0.65e-3, 0.65e-3)
            apertureData0pol.append(apertureData0poln)
            i = i + step
            self.progressBar.setValue(i)

        for wnIdx, wn in enumerate(wavelen):
            absEff90pol[
                wnIdx], apertureData90poln = SPI.scatterPixelIntegrator(
                    wn, 90, 0.65e-3, 0.65e-3)
            apertureData90pol.append(apertureData90poln)
            i = i + step
            self.progressBar.setValue(i)
        apertureData0pol, apertureData90pol = np.asarray(
            apertureData0pol), np.asarray(apertureData90pol)

        ##Plot 0 deg results averaged##
        PW = np.linspace(0, 10, 41)

        azimuthalSum0pol = []
        for wnIdx, _ in enumerate(wavelen):
            azimuthalSum0pol.append(np.sum(apertureData0pol[wnIdx], axis=0))
        azimuthalSum0pol = np.asarray(azimuthalSum0pol)

        fig0 = Figure()
        ax1f1 = fig0.add_subplot(211)
        ax1f1.set_title('Averaged Aperture Efficenty for each PW angle')
        for azimuthalSum0polN, wn in zip(azimuthalSum0pol, wavelen):
            ax1f1.plot(PW, azimuthalSum0polN / 37, label='%d 0pol' % (wn))

        ax1f1.set_xlabel('PW inclination angle (deg)')
        ax1f1.set_ylabel('Efficientcy')
        ax1f1.legend(loc='best')

        ##Plot 90 deg results azimuthally averaged##
        azimuthalSum90pol = []
        for wnIdx, _ in enumerate(wavelen):
            azimuthalSum90pol.append(np.sum(apertureData90pol[wnIdx], axis=0))
        azimuthalSum90pol = np.asarray(azimuthalSum90pol)

        ax1f2 = fig0.add_subplot(212)
        for azimuthalSum90polN, wn in zip(azimuthalSum90pol, wavelen):
            ax1f2.plot(PW, azimuthalSum90polN / 37, label='%d 90pol' % (wn))

        ax1f2.set_xlabel('PW inclination angle (deg)')
        ax1f2.set_ylabel('Efficientcy')
        ax1f2.legend(loc='best')

        ##Plot Absolute eff for 0/90deg results
        fig1 = Figure()
        ax1f3 = fig1.add_subplot(211)
        ax1f3.set_title('Aperture Efficenty for each wavelength')
        ax1f3.plot(wavelen,
                   absEff0pol,
                   marker='o',
                   linestyle='--',
                   label='Aperture Efficientcy 0pol')
        ax1f3.set_xlabel('Wavelenght (um)')
        ax1f3.set_ylabel('Aperture (%s)' % (ureg.steradian * ureg.meter**2))
        ax1f3.legend(loc='best')

        ax1f4 = fig1.add_subplot(212)
        ax1f4.plot(wavelen,
                   absEff90pol,
                   marker='o',
                   linestyle='--',
                   c='r',
                   label='Aperture Efficientcy 90pol')
        ax1f4.set_xlabel('Wavelenght (um)')
        ax1f4.set_ylabel('Aperture (%s)' % (ureg.steradian * ureg.meter**2))
        ax1f4.legend(loc='best')

        path = 'C:/Users/Joe/Documents/Python Scripts/Scatter Pixel Code/Consolidated_Filter.txt'
        FilterCoeff = FT.filterTransmission(path, wavelen * 1e-6)
        BBT = float(self.BBtemp.toPlainText())
        specIr = BBS.blackBodySpec(wavelen * 1e-6, BBT)

        specPowerAbsorbed0 = FilterCoeff * specIr * absEff0pol * ureg.steradian * ureg.mm**2
        specPowerAbsorbed90 = FilterCoeff * specIr * absEff90pol * ureg.steradian * ureg.mm**2

        fig3 = Figure()
        ax1f5 = fig3.add_subplot(211)
        ax1f5.set_title('Spectral Power Absorbed for each wavelength')
        ax1f5.plot(wavelen,
                   baseUnits(specPowerAbsorbed0),
                   marker='o',
                   linestyle='--',
                   label='Aperture Efficientcy 0pol')
        ax1f5.set_xlabel('Wavelenght (um)')
        #ax1f5.set_ylabel('Amplitude (%s)'%(specPowerAbsorbed0.units))
        ax1f5.legend(loc='best')

        ax1f6 = fig3.add_subplot(212)
        ax1f6.plot(wavelen,
                   baseUnits(specPowerAbsorbed90),
                   marker='o',
                   linestyle='--',
                   label='Aperture Efficientcy 90pol')
        ax1f6.set_xlabel('Wavelenght (um)')
        ax1f6.set_ylabel('Amplitude \n(%s)' %
                         (specPowerAbsorbed90.to_base_units().units))
        ax1f6.legend(loc='best')

        totalPowerAbsorbed0pol = simps(baseUnits(specPowerAbsorbed0),
                                       wavelen * 1e-6)  #(watts)
        totalPowerAbsorbed90pol = simps(baseUnits(specPowerAbsorbed90),
                                        wavelen * 1e-6)  #(watts)

        self.totalPowerAbsorbed0.setText(
            str(totalPowerAbsorbed0pol * ureg.watt))
        self.totalPowerAbsorbed90.setText(
            str(totalPowerAbsorbed90pol * ureg.watt))

        self.rmmpl_2()
        self.addmpl_2(fig0)
        self.rmmpl_4()
        self.addmpl_4(fig1)
        self.rmmpl_5()
        self.addmpl_5(fig3)

    def simpleModel(self):
        PW = np.linspace(0, 10, 41)
        wavelen = np.linspace(float(self.min.toPlainText()),
                              float(self.max.toPlainText()),
                              float(self.samples.toPlainText()))

        AppEff = SEAF.simulateEffectiveApertureEfficiency(
            wavelen, float(self.EOAX.toPlainText()),
            float(self.EOAM.toPlainText()), float(self.PAEX.toPlainText()),
            float(self.PAEM.toPlainText()), PW)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)
        ax1f1.set_title('Simulated Effective Aperture Eff')

        for an, wn in zip(AppEff, wavelen):
            ax1f1.plot(PW, an, label='wavelength %dum' % wn)
        ax1f1.set_xlabel('Inclination angle')
        ax1f1.set_ylabel('Efficiency')
        ax1f1.legend(loc='best')
        ax1f1.grid()

        self.rmmpl()
        self.addmpl(figx)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl_0.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl_0.addWidget(self.toolbar)

    def addmpl_2(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.mplvl_2.addWidget(self.canvas2)
        self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.mplwindow_2,
                                          coordinates=True)
        self.mplvl_2.addWidget(self.toolbar2)

    def addmpl_3(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.mplvl_3.addWidget(self.canvas3)
        self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.mplwindow_3,
                                          coordinates=True)
        self.mplvl_3.addWidget(self.toolbar3)

    def addmpl_4(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.mplvl_4.addWidget(self.canvas4)
        self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.mplwindow_4,
                                          coordinates=True)
        self.mplvl_4.addWidget(self.toolbar4)

    def addmpl_5(self, fig):
        self.canvas5 = FigureCanvas(fig)
        self.mplvl_5.addWidget(self.canvas5)
        self.canvas5.draw()
        self.toolbar5 = NavigationToolbar(self.canvas5,
                                          self.mplwindow_5,
                                          coordinates=True)
        self.mplvl_5.addWidget(self.toolbar5)

    def rmmpl(self):
        self.mplvl_0.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl_0.removeWidget(self.toolbar)
        self.toolbar.close()

    def rmmpl_2(self):
        self.mplvl_2.removeWidget(self.canvas2)
        self.canvas2.close()
        self.mplvl_2.removeWidget(self.toolbar2)
        self.toolbar2.close()

    def rmmpl_3(self):
        self.mplvl_3.removeWidget(self.canvas3)
        self.canvas3.close()
        self.mplvl_3.removeWidget(self.toolbar3)
        self.toolbar3.close()

    def rmmpl_4(self):
        self.mplvl_4.removeWidget(self.canvas4)
        self.canvas4.close()
        self.mplvl_4.removeWidget(self.toolbar4)
        self.toolbar4.close()

    def rmmpl_5(self):
        self.mplvl_5.removeWidget(self.canvas5)
        self.canvas5.close()
        self.mplvl_5.removeWidget(self.toolbar5)
        self.toolbar5.close()

    def scatterPixelIntegrator(self, wavelen, pol, width, height):
        fileMI = 'mode amplitude vs angle %dp0 um %dp0 deg.dat' % (wavelen,
                                                                   pol)
        fileMS = 'mode coefficients %dp0 um %dp0 deg-%d.dat'
        folderM = 'C:\\Users\\Joe\\Documents\\Python Scripts\\Scatter Pixel Code\\mode coefficients %dp0 um %dp0 deg' % (
            wavelen, pol)
        fileS = 'sron_rect_gap%dum_s11_abso400_%s.dat'
        folderS = 'C:\\Users\\Joe\\Documents\\Python Scripts\\Scatter Pixel Code\\Scatter Data'

        ApertureArea = width * height

        ##WORKING
        Amps, modelist, PW, Azi = LMD.loadModalData(fileMI, fileMS, folderM,
                                                    wavelen, pol)
        print('Modal Data Loaded')
        ##WORKING
        S11 = LSD.loadScatterData(fileS, folderS, wavelen)
        print('Scatter Data Loaded')
        ##WORKING
        AbosrbtionChannels = FAM.findAbsorptionModes(S11)
        #    return PW,Azi,ApertureArea,modelist,Amps,AbosrbtionChannels
        print('AbsorbtionChannels got')
        absEff, RRR = IPP.integratePixelPerformance(PW, Azi, ApertureArea,
                                                    modelist, Amps,
                                                    AbosrbtionChannels)
        print('done')
        return absEff, RRR
class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.plotField.clicked.connect(self.plotfield)

        #Notice that an empty figure instance was added to our plotting window in the initialization method.
        #This is necessary because our first call to changefig will try to remove a previously displayed figure,
        #which will throw an error if one is not displayed.
        #The empty figure serves as a placeholder so the changefig method functions properly.
        fig = Figure()
        fig2 = Figure()
        self.addmpl(fig)
        self.addmpl_2(fig2)

    def plotfield(self):
        wavelen = float(self.wavelen.toPlainText())
        modenum = int(self.modeNum.toPlainText())
        Zin = float(self.z.toPlainText())

        AN = genData.generateAN(wavelen, modenum)

        ET, ETz, x, z = genData.generateET(wavelen, modenum, AN, Zin)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)
        ax1f1.set_title('Beam Pattern')

        ax1f1.plot(x, np.abs(ETz)**2, label='Beam Pattern at %s m' % Zin)
        ax1f1.set_xlabel('x [m]')
        ax1f1.set_ylabel('Intensity')
        ax1f1.legend(loc='best')
        ax1f1.grid()

        self.rmmpl()
        self.addmpl(figx)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)

        # Make data.
        X = x
        Y = z
        ET = np.asanyarray(ET)
        Z = np.abs(ET)  #/numpy.amax(np.abs(ETz))
        X, Y = np.meshgrid(X, Y)
        # Plot the surface.
        ax1f1.pcolor(Y, X, Z**2)
        ax1f1.axis([z.min(), z.max(), x.min(), x.max()])

        self.rmmpl_2()
        self.addmpl_2(figx)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl_0.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl_0.addWidget(self.toolbar)

    def addmpl_2(self, fig):
        self.canvas1 = FigureCanvas(fig)
        self.mplvl_1.addWidget(self.canvas1)
        self.canvas1.draw()
        self.toolbar1 = NavigationToolbar(self.canvas1,
                                          self.mplwindow_1,
                                          coordinates=True)
        self.mplvl_1.addWidget(self.toolbar1)

    def rmmpl(self):
        self.mplvl_0.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl_0.removeWidget(self.toolbar)
        self.toolbar.close()

    def rmmpl_2(self):
        self.mplvl_1.removeWidget(self.canvas1)
        self.canvas1.close()
        self.mplvl_1.removeWidget(self.toolbar1)
        self.toolbar1.close()
예제 #11
0
class doeplotDlg(QtWidgets.QDialog,Ui_doeplotDialog):
    def __init__(self,parent=None):
        super(doeplotDlg,self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window |QtCore.Qt.CustomizeWindowHint |
            QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint |
            QtCore.Qt.WindowMaximizeButtonHint)
        Lc=DS.Lc[DS.Ic]
        Gc=DS.Gc[DS.Ic]
        Lcx=Lc[-Gc]
        Lcy=Lc[Gc]
        for x in Lcy:
            self.responcecomboBox.addItem(str(x))
        for x in Lcx:
            self.factorcomboBox.addItem(str(x))
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig=Figure()
        ax=fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0,1])
        ax.set_ylim([0,1])
        self.addmpl(fig)
    def redraw(self):
        if DOE.lfac is None: 
            doemodel(self)
        fig=Figure()
        ny=self.responcecomboBox.currentIndex()
        nf=self.factorcomboBox.currentIndex()
        Lc=DS.Lc[DS.Ic]
        Gc=DS.Gc[DS.Ic]
        Lcy=Lc[Gc]
        Lcx=Lc[-Gc]
        nX=len(Lcx)
        data=DS.Raw.loc[DS.Ir,DS.Ic]
        Y=data[Lcy]
        X=data[Lcx]
        nr=X.shape[0]
        w_text=''
        if nr>2**nX:
            Xu=X[-X.duplicated().values]
            Yu=[]
            for i in range(Xu.shape[0]):
                Yu.append(Y[(X==Xu.ix[i,:]).product(axis=1).astype('bool').values].mean().values)
            nr=2**nX
            X=Xu
            Y=pd.DataFrame(Yu,index=X.index,columns=Y.columns)
            w_text='Duplicated tests are averaged'
        ncx=len(Lcx)
        Y=Y.values.astype('float')
        X=X.values.astype('float')
        if self.orderedradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            order=Y.argsort()
            ind=np.arange(nr)
            X=X[order,:]
            if((X.max()==1)&(X.min()==-1)):
                X=X.astype('str')
                X[X=='-1.0']='-'
                X[X=='1.0']='+'
                X[X=='0.0']='O'
            else:
                X=X.astype('str')            
            label=nr*['']
            for i in range(nr):
                label[i]=' '.join(X[i,:].tolist())
            ax.scatter(ind,Y[order],color='red',alpha=0.3,marker='o')
            ax.set_xticks(ind)
            ax.set_xticklabels(ind[order]+1, rotation='vertical')
            ax.set_xlabel('Experiment Sequence')
            ax.set_ylabel('Responce')
            ax.xaxis.grid()
            ax.yaxis.grid()
            ax.set_title('Ordered Plot for '+self.responcecomboBox.currentText())
            for i in range(nr):
                ax.annotate(label[i],(ind[i],Y[order][i]),rotation=-90,size=14)
        elif self.meanradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            vmain=np.zeros(2*ncx)
            Ym=np.mean(Y)
            labels=[]
            for i in range(ncx):
                labels=labels+['-']+[Lcx[i]]+['+']+[' ']
                vmain[2*i]=np.mean(Y[X[:,i]==-1])
                vmain[2*i+1]=np.mean(Y[X[:,i]==1])
            ind=np.arange(2*ncx)
            ax.scatter(ind,vmain,color='red',alpha=0.3,marker='o')
            ax.set_xlabel('Factors')
            ax.set_ylabel('Responce')
            ax.set_xticks(np.arange(0,2*ncx,0.5))
            ax.set_xticklabels(labels,rotation='vertical')
            ax.set_title('Main Effects Plot for Responce: '+self.responcecomboBox.currentText())
            ax.add_line(Line2D([-1,2*ncx],[Ym,Ym],color='green'))
            for i in range(ncx):
                ax.add_line(Line2D([ind[2*i],ind[2*i+1]],[vmain[2*i],vmain[2*i+1]],color='red')) 
        elif self.mainradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            Ym=np.mean(Y)
            labels=[]
            ind=[]
            vy=[]
            for i in range(ncx):
                labels=labels+['-']+[Lcx[i]]+['+']+[' ']
                vy=vy+[Y[X[:,i]==-1].T.tolist()]+[Y[X[:,i]==1].T.tolist()]
                ind=ind+len(Y[X[:,i]==-1])*[2*i]+len(Y[X[:,i]==1])*[2*i+1]
            ax.scatter(ind,vy,color='red',alpha=0.3,marker='o')
            ax.set_xlabel('Factors')
            ax.set_ylabel('Responce')
            ax.set_xticks(np.arange(0,2*ncx,0.5))
            ax.set_xticklabels(labels, rotation='vertical')
            ax.set_title('Scatter Plot for Responce :'+self.responcecomboBox.currentText())
            ax.add_line(Line2D([-1,2*ncx],[Ym,Ym],color='green'))
            for i in range(2*ncx):
                ax.add_line(Line2D([i,i],[min(vy[i]),max(vy[i])],color='red')) 
        elif self.interactionradioButton.isChecked():
            def int_plot(ax,X,Y,Ym,i,j):
                x=X[:,i]
                if(i!=j):
                    x=x*X[:,j]
                y_m=np.mean(Y[x==-1])
                y_p=np.mean(Y[x==+1])
                ax.set_ylim([min(Y),max(Y)])
                ax.scatter([0,1],[y_m,y_p],color='red',alpha=0.3,marker='o')
                ax.add_line(Line2D([-1,2],[Ym,Ym],color='green'))
                ax.add_line(Line2D([0,1],[y_m,y_p],color='red'))
            Y=Y[:,ny]
            Ym=np.mean(Y)
            gs=GridSpec(ncx,ncx)
            for i in range(ncx):
                for j in range(ncx):
                    if(j>=i):
                        ax=fig.add_subplot(gs[i,j])
                        int_plot(ax,X,Y,Ym,i,j)
                        if(i==j):
                            ax.set_ylabel('Responce')
                            ax.set_xticks([0,1], minor=False)
                            ax.set_xticklabels(['-','+'],minor=False)
                            ax.set_xlabel(Lcx[i])
                        else:
                            ax.xaxis.set_visible(False)
        elif self.blockradioButton.isChecked():
            ax=fig.add_subplot(111)
            for i in range(nr):
                for j in range(ncx):
                   if((X[i,j]!=1)&(X[i,j]!=-1)):
                        QtWidgets.QMessageBox.critical(self,'Output Limit',
                        'There are more then 2 levels\n I am not able to plot',QtWidgets.QMessageBox.Ok)
                        return()
            labels=np.array(range(1,ncx+1))
            labels=np.delete(labels,nf)
            x=X[:,nf]
            X=np.delete(X,nf,1)
            Y=Y[:,ny]
            if(min(Y)>0):
                ymin=0.99*min(Y)
            else:
                ymin=1.01*min(Y)
            if(max(Y)>0):
                ymax=1.01*max(Y)
            else:
                ymax=0.99*max(Y)
            n_factor=math.factorial(ncx)/math.factorial(ncx-2)/2
            if(n_factor>10):
                QtWidgets.QMessageBox.critical(self,'Output Limit',
                'There are more then 10 interactions. \n Too many to be plotted!',QtWidgets.QMessageBox.Ok)
                return()
            if(n_factor==1):
                QtWidgets.QMessageBox.critical(self,'Output Limit',
                'There are no interaction left. \n Block Plot does not apply',QtWidgets.QMessageBox.Ok)
                return()
            for i in range(ncx-1):
                for j in range(i,(ncx-1)):
                    if(j>i):
                        vm=np.zeros(4)
                        vp=np.zeros(4)
                        vm[0]=np.mean(Y[(x==-1)&(X[:,j]==-1)&(X[:,i]==-1)])
                        vm[1]=np.mean(Y[(x==-1)&(X[:,j]==-1)&(X[:,i]==+1)])
                        vm[2]=np.mean(Y[(x==-1)&(X[:,j]==+1)&(X[:,i]==-1)])
                        vm[3]=np.mean(Y[(x==-1)&(X[:,j]==+1)&(X[:,i]==+1)])
                        vp[0]=np.mean(Y[(x==+1)&(X[:,j]==-1)&(X[:,i]==-1)])
                        vp[1]=np.mean(Y[(x==+1)&(X[:,j]==-1)&(X[:,i]==+1)])
                        vp[2]=np.mean(Y[(x==+1)&(X[:,j]==+1)&(X[:,i]==-1)])
                        vp[3]=np.mean(Y[(x==+1)&(X[:,j]==+1)&(X[:,i]==+1)])
                        ind=[0,1,2,3]
                        ax.set_xlabel('Factors Combinations')
                        ax.set_ylabel('Responce')
                        ax.set_title('Block Plot: Responce '+self.responcecomboBox.currentText()+', Factor '+
                        self.factorcomboBox.currentText()+', interaction '+Lcx[labels[i]-1]+'-'+Lcx[labels[j]-1])
                        ax.plot(ind,vm,'_',ind,vp,'+')
                        ax.set_xticks(ind)
                        ax.set_xticklabels(['(- -)','(-,+)','(+,-)','(+,+)'], rotation='horizontal')
                        ax.set_xlim([-1,4])
                        ax.set_ylim([ymin,ymax])
                        currentAxis = plt.gca()
                        for k in range(4):
                            someX=k
                            someY=np.mean([vm[k],vp[k]])
                            d=abs(vm[k]-vp[k])*1.1
                            currentAxis.add_patch(Rectangle((someX-.1,someY-d/2),0.2,d,fill=False))
        elif self.vsfactoradioButton.isChecked():
            ax=fig.add_subplot(111)
            for i in range(nr):
                for j in range(ncx):
                   if((X[i,j]!=1)&(X[i,j]!=-1)):
                        QtWidgets.QMessageBox.critical(self,'Output Limit',
                        'There are more then 2 levels\n I am not able to plot',QtWidgets.QMessageBox.Ok)
                        return()
            x=X[:,nf]
            Y=Y[:,ny]
            ym=Y[x==-1]
            yp=Y[x==+1]
            ind=range(1,3)
            ax.set_ylabel('Responce')
            ax.set_title('Block Plot: Responce '+self.responcecomboBox.currentText()
            +' vs. Factor '+self.factorcomboBox.currentText())
            ax.boxplot([ym,yp])
            ax.set_xticks(ind)
            ax.set_xticklabels(['(-)','(+)'], rotation='horizontal')
        elif self.youdenradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            nfac=len(DOE.lfac)
            sfac=np.array([str(i) for i in DOE.lfac])
            x=np.zeros(nfac)
            y=np.zeros(nfac)
            for i in range(nfac):
                Xp=X[:,(DOE.lfac[i][0]-1)]
                for j in range(1,len(DOE.lfac[i])):
                    Xp=Xp*X[:,(DOE.lfac[i][j]-1)]
                x[i]=Y[Xp==-1].mean()
                y[i]=Y[Xp==+1].mean()
            ax.set_ylabel('Average Responce for +1')
            ax.set_xlabel('Average Responce for -1')
            ax.set_title('Youden Plot: Responce :'+self.responcecomboBox.currentText())
            ax.scatter(x,y,color='red',alpha=0.3,marker='o')
            ax.scatter(Y.mean(),Y.mean(),color='blue',s=60,marker='+')
            ax.axhline(y=Y.mean(),color='red',linewidth=1.5,zorder=0)
            ax.axvline(x=Y.mean(),color='red',linewidth=1.5,zorder=0)
            for i in range(nfac):
                ax.annotate(sfac[i],(x[i],y[i]),rotation=0,size=12)
        elif self.effectradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            nfac=len(DOE.lfac)
            sfac=np.array([str(i) for i in DOE.lfac])
            x=np.zeros(nfac)
            y=np.zeros(nfac)
            for i in range(nfac):
                Xp=X[:,(DOE.lfac[i][0]-1)]
                for j in range(1,len(DOE.lfac[i])):
                    Xp=Xp*X[:,(DOE.lfac[i][j]-1)]
                x[i]=Y[Xp==-1].mean()
                y[i]=Y[Xp==+1].mean()
            eff=abs(x-y)
            order=eff.argsort()
            width=0.35 
            ind=np.arange(1,nfac+1)
            ax.bar(ind,eff[order],width,color='blue')
            ax.set_xticks(ind+width/2)
            ax.set_xticklabels(sfac[order], rotation='vertical')
            ax.set_xlabel('Factors') 
            ax.set_ylabel('|Effects|') 
            ax.set_title('Effect Plot for '+self.responcecomboBox.currentText())
        elif self.halfradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            nfac=len(DOE.lfac)
            sfac=np.array([str(i) for i in DOE.lfac])
            x=np.zeros(nfac)
            y=np.zeros(nfac)
            for i in range(nfac):
                Xp=X[:,(DOE.lfac[i][0]-1)]
                for j in range(1,len(DOE.lfac[i])):
                    Xp=Xp*X[:,(DOE.lfac[i][j]-1)]
                x[i]=Y[Xp==-1].mean()
                y[i]=Y[Xp==+1].mean()
            eff=abs(x-y)
            order=eff.argsort()
            ind=[sp.stats.halfnorm.ppf(i/(nfac+1)) for i in range(1,nfac+1)]
            ax.scatter(ind,eff[order],color='blue',s=60,marker='+')
            for i in range(nfac):
                ax.annotate(sfac[order][i],(ind[i],eff[order][i]),rotation=-90,size=12)
            ax.set_xlabel('Half Normal Distribution Order') 
            ax.set_ylabel('Ordered |Effects|') 
            ax.set_title('Half Normal Distribution Plot for '+self.responcecomboBox.currentText())
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():    
            ax.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',which='both',left='off',right='off',labelleft='off')
        ax.annotate(w_text,xy=(0.75,0.95),xycoords='figure fraction',fontsize=8)
        self.rmmpl()
        self.addmpl(fig)        
    def reset(self):
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.update()        
    def addmpl(self, fig):
        self.canvas=FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar=NavigationToolbar(self.canvas, 
                self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)
    def rmmpl(self,):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #12
0
class MainITCfit(QMainWindow, Ui_MainWindow):
    def __init__(self,file_path_=None,exp_path_=None,outpath_=None,json_file=None,cakeit=False,_mask_file=None):
        super(MainITCfit,self).__init__()
        self.setupUi(self)
        self.imsc = imsc
        self.cakeit=cakeit
        self.json_file=json_file
        self.fig_dict = {}
        self.img=None
        self.img_orig=None
        self.file_path=None
        self.exp_path=None
        self.exp_load=None
        self.outpath=outpath_
        self.active_fig=None
        self.last_exp_path=None
        self.subp=None
        self.k=None
        self.qpix=None
        self.config=None
        self.bs_mask=None
        self.mask_file=_mask_file
        self.Ibs = 1
        #self.Absscale = 1/0.03078362722368851
        #self.Absscale = 1/0.018774281015716697
        self.Absscale = 1 #/0.007099958798999592
        #self.Absscale = 1/0.031619
        default_dict={
                        "dist": 1.0,
                        "energy": 12.0,
                        "c_max_s": 1000.0,
                        "outpath": "/",
                        "c_max": 1000.0,
                        "gamma_delta": 0,
                        "pixel2": 0.000172,
                        "pixel1": 0.000172,
                        "fit_tol": 0.1,
                        "radial_int": 20,
                        "c_min": 0.0,
                        "fit_thresh": 200.0,
                        "bkg_angle": 10.0,
                        "damping": 0.5,
                        "c_min_s": 0.0,
                        "exp_path": " ",
                        "val_dummy": -1,
                        "d_radius": 200,
                        "fit_q_min": 0.8,
                        "alpha_ini": 144,
                        "delta_dummy": 1,
                        "exp_load": 0,
                        "fit_q_max": 2,
                        "alpha_delta": 10,
                        "qmax": 6.0,
                        "save_img": True,
                        "file_path": " ",
                        "cmap": "jet",
                        "gamma_ini": 3.55,
                        "Beam_x": 587,
                        "Beam_y": 591,
                        "Bsc_size" : 16,
                        "Bs_alpha": -59.5,
                        "Bs_w2": 9,
                        "Bs_l1": 40,
                        "Bs_l2": 5000,
                        "Bs_x_off": 0,
                        "Bs_y_off": 0
                    }
        default_dict["file_path"] = os.path.join(os.getcwd(),'test_data','a-SiO2_01_0003.tif')
        default_dict["outpath"] = os.path.join(os.getcwd(),'test_data_reduced')

        ok=False
        if self.json_file is not None:
            print ("loading parameter ", self.json_file)
            ok=self.restore(self.json_file)
        if not ok:
                print ("loading default itcfit.json")
                ok=self.restore(".itcfit.json")
        if not ok:
            print ("no file to load, using the default dict")
            self.set_config(default_dict)
        print ("json_file: ", self.json_file)

        ok=False
        if file_path_:
            if op.isfile(file_path_):
                self.file_path=file_path_
                ok=True
        if not ok:
            if self.config["file_path"]:
                if op.isfile(self.config["file_path"]):
                    self.file_path=self.config["file_path"]
                    ok=True
        print ("file_path: ",self.file_path)

        ok=False
        self.exp_load=self.config["exp_load"]
        if self.exp_load !=0:
            if exp_path_:
                if op.isfile(exp_path_):
                    self.exp_path=exp_path_
                    ok=True
            if not ok:
                if self.config["exp_path"]:
                    if op.isfile(self.config["exp_path"]):
                        self.exp_path=self.config["exp_path"]
                        ok=True
        if not ok:
            self.exp_load=0
        print ("exp_path: ",self.exp_path)

        self.k,self.qpix = qr_conv(self.config)
        self.phis=np.arange(0,6.3,0.01)

        self.exp_cb.addItem("ScatterBrain")
        self.exp_cb.addItem("pyFAI")
        self.exp_cb.currentIndexChanged.connect(self.exp_cb_change)
        self.cb_logscale.setChecked(True)
        self.cb_logscale.stateChanged.connect(self.update_view)
        self.cb_log_x_scale.stateChanged.connect(self.update_view)
        self.dsb_alpha.valueChanged.connect(self.update_view)
        self.dsb_gamma.valueChanged.connect(self.update_view)
        self.dsb_bkg_angle.editingFinished.connect(self.update_view)
        self.dsb_qmax.editingFinished.connect(self.update_view)
        self.dsb_radial_int.editingFinished.connect(self.update_view)
        self.exp_btn.pressed.connect(self.exp_btn_handl)
        self.mask_btn.pressed.connect(self.mask_btn_handl)
        self.subfolder_btn.pressed.connect(self.subfolder_btn_handl)
        self.file_path_btn.pressed.connect(self.file_path_btn_handl)
        self.fit_it_btn.pressed.connect(self.fit_it_btn_handl)
        self.mplfigs.itemClicked.connect(self.changefig)
        self.btn_reset_clim.clicked.connect(self.btn_reset_handl)
        self.qfile_path.editingFinished.connect(self.qfile_path_handl)
        self.dsb_max.editingFinished.connect(self.dsb_max_handl)#CN todo: change to pressed enter
        self.dsb_min.editingFinished.connect(self.dsb_min_handl)
#        self.slider_max.sliderReleased.connect(self.slider_max_handl)
        self.slider_max.valueChanged.connect(self.slider_max_handl)
        self.slider_min.valueChanged.connect(self.slider_max_handl)
        self.save_btn.pressed.connect(self.save_config)
        self.save_as_btn.pressed.connect(self.save_as_config)
        self.sub_folder_cb.stateChanged.connect(self.sub_folder_cb_handl)
        self.qexp_path.setEnabled(False)
        fig = Figure()
        self.addmpl((fig,None))
        if self.exp_path:
            self.last_exp_path=self.exp_path
#            self.qexp_path.setText(self.exp_path)
            self.exp_path=None
        self.exp_cb_change(self.exp_load)
        if self.file_path:
            self.qfile_path.setText(self.file_path)
            if '.tif' not in self.file_path:
                img = fabio.edfimage.EdfImage()
                img.read(self.file_path)
                try:
                    self.Ibs = 1 #float(img.header['Ibs'])
                except:
                    self.Ibs=1
                    #self.img_orig = np.copy(img.data)#,'float64')
                if self.Ibs < 1:
                    self.Ibs=1
                self.img_orig = np.copy(img.data/self.Ibs*self.Absscale)#,'float64')
                #img = img.next()
                #self.img_orig = np.copy(img.data)
                print('scale=',self.Absscale/self.Ibs)
                #print('scale=',self.Ibs)
            else:
                self.img_orig=np.array(Image.open(self.file_path))
            
            self.img=np.copy(self.img_orig)
            self.bs_mask = beam_stop_threshold_mask(self.img,self.img.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),bsc_size=float(self.config["Bsc_size"]),bs_alpha=float(self.config["Bs_alpha"]),bs_w2=float(self.config["Bs_w2"]),bs_l1=float(self.config["Bs_l1"]),bs_l2=float(self.config["Bs_l2"]),bs_x_off=float(self.config["Bs_x_off"]),bs_y_off=float(self.config["Bs_y_off"]),mask_file=self.mask_file)
            self.img[self.bs_mask > 0]=-1
            self.update_view()
        if self.outpath:
            self.sub_folder_path.setText(self.outpath)
        self.exp_cb.setCurrentIndex(self.exp_load)
        self.sub_folder_cb_handl()

    def hide_fit(self):
        self.dsb_fit_q_max.hide()
        self.label_4.hide()
        self.dsb_fit_q_min.hide()
        self.label_5.hide()
        self.dsb_fit_thresh.hide()
        self.label_6.hide()                
        self.dsb_damping.hide()
        self.label_10.hide()                
        self.dsb_fit_tol.hide()
        self.label_8.hide()                
        self.dsb_d_radius.hide()
        self.label_12.hide()                
        self.fit_it_btn.hide()
        
    def update_view(self):
        alpha = float(self.dsb_alpha.text())
        self.dsb_alpha.setValue(alpha%360)
        self.k,self.qpix = qr_conv(self.config)
        self.cmap= cm.get_cmap(self.config['cmap'])
        if not self.active_fig and self.img is not None:#initzialize figures
            fig1  = Figure()
            ax1f1 = fig1.add_subplot(111)
            self.subp=ax1f1
            #im1=ax1f1.imshow(np.power(self.img,1),zorder=1,cmap=self.cmap)
            im1=ax1f1.imshow(self.imsc(self.img),zorder=1,cmap=self.cmap)
            fig1.colorbar(im1)
            ax=ax1f1.axis()
            im_w,im_h=self.img.shape
            x_beam=self.config["Beam_x"]#+0.5
            y_beam=float(self.config["Beam_y"])#+0.5
            k=self.k
            alpha = float(self.dsb_alpha.text())
            gamma = float(self.dsb_gamma.text())
            r =k*np.tan(np.radians(gamma))
            x0=r*np.cos(np.radians(alpha))+x_beam
            y0=r*np.sin(-np.radians(alpha))+y_beam
            self.circ1 = ax1f1.plot( *(xy)(r,self.phis,x0,y0), c='r',ls='-',zorder=2)
            self.circ3 = ax1f1.plot( *(xy)(r+0.5*int(self.dsb_radial_int.text()),self.phis,x0,y0), c='r',ls='-',zorder=4)
            self.circ4 = ax1f1.plot( *(xy)(r-0.5*int(self.dsb_radial_int.text()),self.phis,x0,y0), c='r',ls='-',zorder=5)
            
            alpha+=float(self.dsb_bkg_angle.text())
            x0=r*np.cos(np.radians(alpha))+x_beam
            y0=r*np.sin(-np.radians(alpha))+y_beam
            self.circ2 = ax1f1.plot( *(xy)(r,self.phis,x0,y0), c='g',ls='-',zorder=3)
            ax1f1.axis(ax)
            self.addfig('orig. image', [fig1,im1,float(self.config["c_max"]),float(self.config["c_min"]),float(self.config["c_max_s"]),float(self.config["c_min_s"])])
            if self.cakeit:
                fig5  = Figure()
                ax1f5 = fig5.add_subplot(111)
                im5=ax1f5.imshow(self.imsc(self.img),zorder=1)
                self.addfig('cake', [fig5,im5,float(self.config["c_max"]),float(self.config["c_min"]),float(self.config["c_max_s"]),float(self.config["c_min_s"])])
            abschi,chi,I1d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
            I1d[I1d<0]=1
            alpha+=float(self.dsb_bkg_angle.text())
            abschi2,chi2,I2d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
            I2d[I2d<0]=1
            fig2= Figure()
            ax1f2 = fig2.add_subplot(111)
            ax1f2.plot(chi,I1d)
            self.addfig('1d reduced', [fig2,None])
            fig3= Figure()
            ax1f3 = fig3.add_subplot(111)
            ax1f3.plot(abschi,I1d)
            self.addfig('1d reduced |q|', [fig3,None])
            fig4= Figure()
            ax1f4 = fig4.add_subplot(111)
#            ax1f4.plot(chi,np.power(I2d,0.2))
            ax1f4.plot(chi,I2d)
            self.addfig('1d sub', [fig4,None])
            fig5= Figure()
            ax1f5 = fig5.add_subplot(111)
            ax1f5.plot(abschi,I2d)
            self.addfig('1d sub |q|', [fig5,None])
            if self.cb_logscale.isChecked():
#                im1.set_norm(mpl.colors.LogNorm())
                ax1f2.set_yscale('log')
                ax1f3.set_yscale('log')
                ax1f4.set_yscale('log')
                ax1f5.set_yscale('log')
            self.rmmpl()
            self.addmpl(self.fig_dict['orig. image'])
            self.active_fig='orig. image'
        elif self.img is not None:
            if self.active_fig == 'orig. image':
                alpha = float(self.dsb_alpha.text())
                gamma = float(self.dsb_gamma.text())
                fig=self.fig_dict['orig. image'][0]
                im=self.fig_dict['orig. image'][1]
#                print(im.get_extent())
#                print(self.img.shape)
#                im.set_extent((-0.5,self.img.shape[1]-0.5,self.img.shape[0]-0.5,-0.5))
#                print(im.get_extent())
                self.subp.lines.pop(0)
                self.subp.lines.pop(0)
                self.subp.lines.pop(0)
                self.subp.lines.pop(0)
                
                ax=self.subp.axis()
                
#                if self.cb_logscale.isChecked():
#                    im.set_norm(mpl.colors.LogNorm())
#                else:
#                    im.set_norm(mpl.colors.LogNorm())
                im_w,im_h=self.img.shape
                x_beam=self.config["Beam_x"]#+0.5
                y_beam=self.config["Beam_y"]#+0.5
                k=self.k
                r =k*np.tan(np.radians(gamma))
                x0=r*np.cos(np.radians(alpha))+x_beam
                y0=r*np.sin(-np.radians(alpha))+y_beam
                self.circ1 = self.subp.plot( *(xy)(r,self.phis,x0,y0), c='r',ls='-',zorder=2)
                self.circ3 = self.subp.plot( *(xy)(r+0.5*int(self.dsb_radial_int.text()),self.phis,x0,y0), c='r',ls='-',zorder=4)
                self.circ4 = self.subp.plot( *(xy)(r-0.5*int(self.dsb_radial_int.text()),self.phis,x0,y0), c='r',ls='-',zorder=5)
            
                alpha+=float(self.dsb_bkg_angle.text())
                x0=r*np.cos(np.radians(alpha))+x_beam
                y0=r*np.sin(-np.radians(alpha))+y_beam
                self.circ2 = self.subp.plot( *(xy)(r,self.phis,x0,y0), c='g',ls='-',zorder=3)
                self.subp.axis(ax)
                if fig.canvas:
                    fig.canvas.draw()
            else:
                self.get_config()
                alpha=float(self.dsb_alpha.text())
                gamma=float(self.dsb_gamma.text())
                if self.active_fig == '1d reduced':
                    fig = self.fig_dict['1d reduced'][0]
                    abschi,chi,I1d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                    I1d[I1d<0]=1
                    pchi=chi
                    pI=I1d
                    pI_err=I1d*0
                elif self.active_fig == '1d reduced |q|':
                    fig = self.fig_dict['1d reduced |q|'][0]
#                    abschi,chi,I1d,pI_err = do_integration1d_x(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                    abschi,chi,I1d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                    I1d[I1d<0]=1
                    pchi=abschi
                    pI=I1d
                elif self.active_fig == '1d sub':
                    fig = self.fig_dict['1d sub'][0]
                    abschi,chi,I1d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                    I1d[I1d<0]=1
                    alpha+=float(self.dsb_bkg_angle.text())
                    abschi,chi,I2d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                    I2d[I2d<0]=1
                    pchi=chi
                    pI=I1d-I2d
                    pI_err=I1d*0
                elif self.active_fig == '1d sub |q|':
                    fig = self.fig_dict['1d sub |q|'][0]
                    abschi,chi,I1d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                    I1d[I1d<0]=1
                    alpha+=float(self.dsb_bkg_angle.text())
                    abschi,chi,I2d,_ = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                    I2d[I2d<0]=1
                    pchi=abschi
                    pI=I1d-I2d
                    pI_err=I1d*0
                #ax1 = fig.add_subplot(111)
                ax1 = fig.gca()
                
                ax1.lines.pop(0)
                #                ax1.plot(pchi,np.power(pI,0.2),c='b')
                ax1.plot(pchi,pI,c='b')
                #ax1.errorbar(pchi,pI,yerr=pI_err, fmt='o',c='b')
                if self.cb_logscale.isChecked():
                    ax1.set_yscale('log')
                else:
                    ax1.set_yscale('linear')
                if self.cb_log_x_scale.isChecked():
                    ax1.set_xscale('log')
                else:
                    ax1.set_xscale('linear')
                if fig.canvas:
                    fig.canvas.draw()

    def load_img(self,file_path):
        if file_path != self.file_path:
            if op.isfile(file_path):
                self.file_path=file_path
                self.get_config()
                if '.tif' not in self.file_path:
                    img = fabio.edfimage.EdfImage()
                    img.read(self.file_path)
                    try:
                        self.Ibs = 1 #float(img.header['Ibs'])
                    except:
                        self.Ibs=1
                        #self.img_orig = np.copy(img.data)#,'float64')
                    if self.Ibs < 1:
                        self.Ibs=1

                    self.img_orig = np.copy(img.data/self.Ibs*self.Absscale)#,'float64')

                    print(self.Ibs)
                    #img = img.next()
                    
                else:
                    self.img_orig=np.array(Image.open(self.file_path))
                self.img=np.copy(self.img_orig)
#                self.bs_mask = beam_stop_threshold_mask(self.img,self.img.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),self.mask_file)
                self.bs_mask = beam_stop_threshold_mask(self.img,self.img.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),bsc_size=float(self.config["Bsc_size"]),bs_alpha=float(self.config["Bs_alpha"]),bs_w2=float(self.config["Bs_w2"]),bs_l1=float(self.config["Bs_l1"]),bs_l2=float(self.config["Bs_l2"]),bs_x_off=float(self.config["Bs_x_off"]),bs_y_off=float(self.config["Bs_y_off"]),mask_file=self.mask_file)
                self.img[self.bs_mask > 0]=-1
                if self.active_fig is not None:
                    self.fig_dict['orig. image'][1].set_data(self.imsc(self.img))
                    #self.fig_dict['orig. image'][0].gca().set_autoscale_on(True)
                    self.fig_dict['orig. image'][1].set_extent((-0.5,self.img.shape[1]-0.5,self.img.shape[0]-0.5,-0.5))
                    self.fig_dict['orig. image'][0].gca().set_autoscale_on(False)
            self.update_view()
        self.qfile_path.setText(self.file_path)
        
    def file_path_btn_handl(self):
        file_path,_=(qt.QFileDialog.getOpenFileName())
        if file_path:
            self.load_img(file_path)
            
    def qfile_path_handl(self):
        self.load_img(str_(self.qfile_path.text()))
#            self.update_1d(img)
#            self.mplfigs.setCurrentRow(0)
#            item=self.mplfigs.selectedItems()[0]
#            self.changefig(item)

    def fit_it_btn_handl(self):
        self.get_config()
        alpha_corr=100
        alpha=float(self.dsb_alpha.text())
        gamma=float(self.dsb_gamma.text())
        max_iter=20
        i=0
        img=self.fig_dict['orig. image'][1].get_array()
        fit_tol=self.config["fit_tol"]
        fit_r_range=self.config['d_radius']*self.config['pixel1']*1e+3
        while abs(alpha_corr) > fit_tol and i < max_iter:
            alpha_corr,gamma,k,qpix=fit_circ_pyFAI(img,gamma,alpha,fit_r_range,self.config,self.bs_mask)
            
            alpha+=alpha_corr*self.config['damping']
            if alpha_corr == 90:
                print('error while fitting')
                i = max_iter
            print ("a=",alpha_corr)
            #print ("al=",alpha)
            #print ("d=",self.config['damping'])
            print ("i=",i)
            i+=1
        
        alpha=alpha%360
        if alpha < 0:
            alpha+=360
        self.dsb_alpha.setValue(alpha)
        self.update_view()
        
    def sub_folder_cb_handl(self):
        if self.sub_folder_cb.isChecked():
            self.sub_folder_path.setEnabled(True)
            self.subfolder_btn.setEnabled(True)
        else:
            self.sub_folder_path.setEnabled(False)
            self.subfolder_btn.setEnabled(False)
            
    def exp_load_set(self,a):
        self.exp_load=int(a)
        
    def set_config(self, dico):
        """Setup the widget from its description
            
            :param dico: dictionary with description of the widget
            :type dico: dict
        """
        self.config=dico
        setup_data = {
                    "exp_load": lambda a: self.exp_load_set(a),
                    "exp_path": self.qexp_path.setText,
                    "file_path": self.qfile_path.setText,
                    "outpath": self.sub_folder_path.setText,
                    "save_img": self.sub_folder_cb.setChecked,
                    "alpha_ini": self.dsb_alpha.setValue,
                    "gamma_ini": self.dsb_gamma.setValue,
                    "fit_q_max": self.dsb_fit_q_max.setValue,
                    "fit_q_min": self.dsb_fit_q_min.setValue,
                    "fit_thresh": self.dsb_fit_thresh.setValue,
                    "fit_tol": self.dsb_fit_tol.setValue,
                    "qmax": self.dsb_qmax.setValue,
                    "radial_int": self.dsb_radial_int.setValue,
                    "damping": self.dsb_damping.setValue,
                    "bkg_angle": self.dsb_bkg_angle.setValue,
                    "d_radius": self.dsb_d_radius.setValue,
                    "c_min": self.dsb_min.setValue,
                    "c_max": self.dsb_max.setValue
#                    "c_max_s": self.slider_max.setValue,
#                    "c_min_s": self.slider_min.setValue
                    #"poni": self.poni.setText,
                    # "detector": self.all_detectors[self.detector.getCurrentIndex()],
                    #"wavelength": lambda a: self.wavelength.setText(str_(a)),
                    #"splineFile": lambda a: self.splineFile.setText(str_(a)),
                    #"pixel1": lambda a: self.pixel1.setText(str_(a)),
                    #"pixel2": lambda a: self.pixel2.setText(str_(a)),
                    #"dist": lambda a: self.dist.setText(str_(a)),
                    #"poni1": lambda a: self.poni1.setText(str_(a)),
                    #"poni2": lambda a: self.poni2.setText(str_(a)),
                    #"rot1": lambda a: self.rot1.setText(str_(a)),
                    #"rot2": lambda a: self.rot2.setText(str_(a)),
                    #"rot3": lambda a: self.rot3.setText(str_(a)),
                    #"do_dummy": self.do_dummy.setChecked,
                    #"do_dark": self.do_dark.setChecked,
                    #"do_flat": self.do_flat.setChecked,
                    #"do_polarization": self.do_polarization.setChecked,
                    #"val_dummy": lambda a: self.val_dummy.setText(str_(a)),
                    #"delta_dummy": lambda a: self.delta_dummy.setText(str_(a)),
                    #"do_mask": self.do_mask.setChecked,
                    #"mask_file": lambda a: self.mask_file.setText(str_(a)),
                    #"dark_current": lambda a: self.dark_current.setText(str_(a)),
                    #"flat_field": lambda a: self.flat_field.setText(str_(a)),
                    #"polarization_factor": self.polarization_factor.setValue,
                    #"nbpt_rad": lambda a: self.nbpt_rad.setText(str_(a)),
                    #"do_2D": self.do_2D.setChecked,
                    #"nbpt_azim": lambda a: self.nbpt_azim.setText(str_(a)),
                    #"chi_discontinuity_at_0": self.chi_discontinuity_at_0.setChecked,
                    #"do_radial_range": self.do_radial_range.setChecked,
                    #"do_azimuthal_range": self.do_azimuthal_range.setChecked,
                    #"do_poisson": self.do_poisson.setChecked,
                    #"radial_range_min": lambda a: self.radial_range_min.setText(str_(a)),
                    #"radial_range_max": lambda a: self.radial_range_max.setText(str_(a)),
                    #"azimuth_range_min": lambda a: self.azimuth_range_min.setText(str_(a)),
                    #"azimuth_range_max": lambda a: self.azimuth_range_max.setText(str_(a)),
                    #"do_solid_angle": self.do_solid_angle.setChecked
                    }
        for key, value in setup_data.items():
            if key in dico and (value is not None):
                value(dico[key])
                
    def restore(self,filename):
        """Restore from JSON file the status of the current widget
            
            :param filename: path where the config was saved
            :type filename: str
            """
#        logger.debug("Restore from %s", filename)
        if not op.isfile(filename):
#            logger.error("No such file: %s", filename)
            return False
        data = json.load(open(filename))
        self.set_config(data)
        return True
    
    def save_as_config(self):
#        logger.debug("save_config")
        self.get_config()
        json_file,_ = (qt.QFileDialog.getSaveFileName(caption="Save configuration as json",
                                                        directory=self.outpath,
                                                        filter="Config (*.json)"))
        if json_file:
            self.json_file=json_file
            self.dump(json_file)
            self.dump(".itcfit.json")
            
    def save_config(self):
        self.get_config()
        if self.sub_folder_cb.isChecked():
            self.outpath=self.sub_folder_path.text()
            print ("saveing to:", self.outpath)
            if not os.path.isdir(self.outpath):#making the subfolder, if it doesn't exist
                os.makedirs(self.outpath)
            alpha = float(self.dsb_alpha.text())
            gamma = float(self.dsb_gamma.text())
#            abschi,chi1,I1,sig1 = do_integration1d_x(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
            abschi,chi1,I1,sig1 = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
#            chi1 = I1 = sig1 = np.arange(0.0,5.0,1.0)
#            data = np.array(zip(chi1,I1,sig1))
            data = np.array([chi1,I1,sig1]).transpose()
 
            basename=os.path.basename(self.file_path)
            np.savetxt(self.outpath+"/"+basename+"_sig.xy", data, delimiter="\t",header="q\tI\tsig")
            if self.config["bkg_angle"]!=0:
                print ("bkg=",self.config["bkg_angle"])
                alpha+=float(self.config["bkg_angle"])
#                abschi,chi2,I2,sig2 = do_integration1d_x(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                abschi,chi2,I2,sig2 = do_integration1d(self.img,alpha,gamma,self.config,self.k,self.qpix,self.bs_mask)
                I3=I1-I2
                sig3=sig1*sig1+sig2*sig2
                sig3=np.sqrt(sig3)
#                data = np.array(zip(chi2,I2,sig2))
                data = np.array([chi2,I2,sig2]).transpose()
                np.savetxt(self.outpath+"/"+basename+"_bkg.xy", data, delimiter="\t",header="q\tI\tsig")
#                data = np.array(zip(chi2,I3,sig3))
                data = np.array([chi2,I3,sig3]).transpose()
                
                np.savetxt(self.outpath+"/"+basename+"_sub.xy", data, delimiter="\t",header="q\tI\tsig")
        self.dump()
#        self.dump("itcfit.json")

    def get_config(self):
        """Read the configuration of the plugin and returns it as a dictionary
            
            :return: dict with all information.
            """
        self.config["alpha_ini"]=float(self.dsb_alpha.text())
        self.config["gamma_ini"]=float(self.dsb_gamma.text())
        self.config["qmax"]=float(self.dsb_qmax.text())
        self.config["fit_q_max"]=float(self.dsb_fit_q_max.text())
        self.config["fit_q_min"]=float(self.dsb_fit_q_min.text())
        self.config["exp_load"]=self.exp_cb.currentIndex()
        if self.config["exp_load"] == 0:
            self.config["exp_path"]=None
        else:
            self.config["exp_path"]=self.qexp_path.text()
        self.config["file_path"]=self.qfile_path.text()
        self.config["outpath"]=self.sub_folder_path.text()
        self.config["save_img"]=self.sub_folder_cb.isChecked()
        self.config["fit_thresh"]=float(self.dsb_fit_thresh.text())
        self.config["fit_tol"]=float(self.dsb_fit_tol.text())
        self.config["radial_int"]=int(self.dsb_radial_int.text())
        self.config["d_radius"]=int(self.dsb_d_radius.text())
        self.config["damping"]=float(self.dsb_damping.text())
        self.config["bkg_angle"]=float(self.dsb_bkg_angle.text())
        self.config["c_max"]=float(self.dsb_max.text())
        self.config["c_min"]=float(self.dsb_min.text())
        self.config["c_max_s"]=float(self.slider_max.value())
        self.config["c_min_s"]=float(self.slider_min.value())
#        to_save = {
#                "poni": str_(self.poni.text()).strip(),
#                "detector": str_(self.detector.currentText()).lower(),
#                "wavelength": self._float("wavelength", None),
#                "splineFile": str_(self.splineFile.text()).strip(),
#                "pixel1": self._float("pixel1", None),
#                "pixel2": self._float("pixel2", None),
#                "dist": self._float("dist", None),
#                "poni1": self._float("poni1", None),
#                "poni2": self._float("poni2", None),
#                "rot1": self._float("rot1", None),
#                "rot2": self._float("rot2", None),
#                "rot3": self._float("rot3", None),
#                "do_dummy": bool(self.do_dummy.isChecked()),
#                "do_mask": bool(self.do_mask.isChecked()),
#                "do_dark": bool(self.do_dark.isChecked()),
#                "do_flat": bool(self.do_flat.isChecked()),
#                "do_polarization": bool(self.do_polarization.isChecked()),
#                "val_dummy": self._float("val_dummy", None),
#                "delta_dummy": self._float("delta_dummy", None),
#                "mask_file": str_(self.mask_file.text()).strip(),
#                "dark_current": str_(self.dark_current.text()).strip(),
#                "flat_field": str_(self.flat_field.text()).strip(),
#                "polarization_factor": float_(self.polarization_factor.value()),
#                "nbpt_rad": int_(self.nbpt_rad.text()),
#                "do_2D": bool(self.do_2D.isChecked()),
#                "nbpt_azim": int_(self.nbpt_azim.text()),
#                "chi_discontinuity_at_0": bool(self.chi_discontinuity_at_0.isChecked()),
#                "do_solid_angle": bool(self.do_solid_angle.isChecked()),
#                "do_radial_range": bool(self.do_radial_range.isChecked()),
#                "do_azimuthal_range": bool(self.do_azimuthal_range.isChecked()),
#                "do_poisson": bool(self.do_poisson.isChecked()),
#                "radial_range_min": self._float("radial_range_min", None),
#                "radial_range_max": self._float("radial_range_max", None),
#                "azimuth_range_min": self._float("azimuth_range_min", None),
#                "azimuth_range_max": self._float("azimuth_range_max", None),
#                "do_OpenCL": bool(self.do_OpenCL.isChecked())
#                }
        return self.config
    
    def dump(self, filename=None):
        """
        Dump the status of the current widget to a file in JSON
        
        :param filename: path where to save the config
        :type filename: string
        :return: dict with configuration
        """
        to_save = self.get_config()
        if filename is None:
            filename = '.itcfit.json' #self.json_file
        print ("saving to: ",filename)
        if filename is not None:
#            logger.info("Dump to %s", filename)
            try:
                with open(filename, "w") as myFile:
                    json.dump(to_save, myFile, indent=4)
            except IOError as error:
                print ("Error while saving config: ", error)
#                logger.error("Error while saving config: %s", error)
            else:
                print ("Saved")
#                logger.debug("Saved")
        return to_save

    def update_cake(self):
        alpha = float(self.dsb_alpha.text())
        gamma = float(self.dsb_gamma.text())
        fig=self.fig_dict['cake'][0]
        im=self.fig_dict['cake'][1]
#        img=self.fig_dict['orig. image'][1].get_array()
        fit_r_range=self.config['d_radius']*self.config['pixel1']*1e+3
        I1,th1,chi1=show_2D_cake(self.img,gamma,alpha,fit_r_range,self.config)
        im.set_data(I1)
#        if self.active_fig != self.fig_dict['orig. image']:
#            img=self.fig_dict['orig. image'][1].get_array()
#            self.update_1d(img)
#            item=self.mplfigs.selectedItems()[0]
#            self.changefig(item)
        if fig.canvas:
            fig.canvas.draw()
    def subfolder_btn_handl(self):
        file_path=(qt.QFileDialog.getExistingDirectory(self, "Select Directory"))
        print (file_path)
        if file_path:
            self.outpath=file_path
            self.sub_folder_path.setText(self.outpath)
    def load_exp(self,exp_path):
        self.config["exp_path"]=exp_path
        self.config["exp_load"]=self.exp_load
        loader=exp.LoadExperimet(self.config)
        self.exp_path=exp_path # do this only if success
        self.k,self.qpix = qr_conv(self.config)
        if self.img_orig is not None:
            self.img=np.copy(self.img_orig)
            self.bs_mask = beam_stop_threshold_mask(self.img,self.img.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),bsc_size=float(self.config["Bsc_size"]),bs_alpha=float(self.config["Bs_alpha"]),bs_w2=float(self.config["Bs_w2"]),bs_l1=float(self.config["Bs_l1"]),bs_l2=float(self.config["Bs_l2"]),bs_x_off=float(self.config["Bs_x_off"]),bs_y_off=float(self.config["Bs_y_off"]),mask_file=self.mask_file)
            #beam_stop_threshold_mask(self.img,self.img.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),mask_file=self.mask_file)
            self.img[self.bs_mask > 0]=-1
            if self.active_fig is not None:
                self.fig_dict['orig. image'][1].set_data(self.imsc(self.img))

    def mask_btn_handl(self):
        if self.img_orig is not None:
            window = MaskImageWidget.runx(self.img_orig,self)

    def set_mask(self,mask):
        self.pyFAI_mask = np.copy(mask)
        if self.img_orig is not None:
            self.img=np.copy(self.img_orig)
            self.bs_mask = beam_stop_threshold_mask(self.img,self.img.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),bsc_size=float(self.config["Bsc_size"]),bs_alpha=float(self.config["Bs_alpha"]),bs_w2=float(self.config["Bs_w2"]),bs_l1=float(self.config["Bs_l1"]),bs_l2=float(self.config["Bs_l2"]),bs_x_off=float(self.config["Bs_x_off"]),bs_y_off=float(self.config["Bs_y_off"]),mask_file=None,pyFAI_mask=self.pyFAI_mask)

            self.img[self.bs_mask > 0]=-1
            if self.active_fig is not None:
                self.fig_dict['orig. image'][1].set_data(self.imsc(self.img))
        self.update_view()

    def exp_btn_handl(self):
        if self.exp_cb.currentIndex() != 0:
            if self.exp_cb.currentIndex() == 1:
                exp_path,_ = (qt.QFileDialog.getOpenFileName(caption="Open Experiment configuration",
                                                           filter="Config (*.xml)"))#str_(qt.QFileDialog.getOpenFileName())
            if self.exp_cb.currentIndex() == 2:
                exp_path,_ = (qt.QFileDialog.getOpenFileName(caption="Open Experiment configuration",
                                                             filter="Config (*.poni)"))#str_(qt.QFileDialog.getOpenFileName())
            
            if exp_path:
                self.last_exp_path=self.exp_path
                self.load_exp(exp_path)
                self.qexp_path.setText(exp_path)
        else:
            data,ok=DetPar.getParams(self.config)
            if ok:
                self.config=data
                self.exp_path=None
        if self.img_orig is None:
            return
        self.img=np.copy(self.img_orig)
        self.bs_mask = beam_stop_threshold_mask(self.img,self.img.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),bsc_size=float(self.config["Bsc_size"]),bs_alpha=float(self.config["Bs_alpha"]),bs_w2=float(self.config["Bs_w2"]),bs_l1=float(self.config["Bs_l1"]),bs_l2=float(self.config["Bs_l2"]),bs_x_off=float(self.config["Bs_x_off"]),bs_y_off=float(self.config["Bs_y_off"]),mask_file=self.mask_file)
        #beam_stop_threshold_mask(self.img,self.img_orig.max()-10,0,float(self.config["Beam_x"]),float(self.config["Beam_y"]),mask_file=self.mask_file)
        self.img[self.bs_mask > 0]=-1
        if self.active_fig is not None:
            self.fig_dict['orig. image'][1].set_data(self.imsc(self.img))
        self.update_view()
    def exp_cb_change(self,i):
        self.exp_load=i
        if i == 0:
            self.last_exp_path=self.exp_path
            self.qexp_path.setText("<--- select experiment loader or use button to set parameter manualy --->")
        elif self.last_exp_path:
                if self.last_exp_path != self.exp_path:
                    self.load_exp(self.last_exp_path)
                    self.update_view()
                self.qexp_path.setText(self.exp_path)
    def slider_max_handl(self):
        if self.active_fig:
            max=float(self.slider_max.value())
            min=float(self.slider_min.value())
            #print(self.active_fig)
            #self.fig_dict[self.active_fig][4]=max
            #self.fig_dict[self.active_fig][5]=min
            #self.fig_dict[self.active_fig][1].set_clim([min,max])
            self.fig_dict['orig. image'][4]=max
            self.fig_dict['orig. image'][5]=min
            self.fig_dict['orig. image'][1].set_clim([min,max])
            self.canvas.draw()
    def dsb_max_handl(self):
        max = float(self.dsb_max.text())
        min = float(self.dsb_min.text())
        curr_max=self.slider_max.value()
        curr_min=self.slider_min.value()
        if max < curr_max:
            curr_max=max*0.5
            self.slider_max.setValue(curr_max)
        if curr_min > max:
            curr_min=max*0.5
            self.slider_min.setValue(curr_min)
        self.slider_min.setRange(min,max)
        self.slider_max.setRange(min,max)
    def dsb_min_handl(self):
        max = float(self.dsb_max.text())
        min = float(self.dsb_min.text())
        curr_max=self.slider_max.value()
        curr_min=self.slider_min.value()
        if min > curr_min:
            curr_min=min
            self.slider_min.setValue(curr_min)
        if curr_max < min:
            curr_max=min
            self.slider_max.setValue(curr_max)
        self.slider_min.setRange(min,max)
        self.slider_max.setRange(min,max)
    def btn_reset_handl(self):
        if self.active_fig and self.fig_dict[self.active_fig][1]:
            max=self.imsc(self.img).max()
            min=self.imsc(self.img).min()
            self.dsb_max.setValue(max)
            self.dsb_min.setValue(min)
            self.slider_min.setRange(min,max)
            self.slider_max.setRange(min,max)
    def changefig(self,item):
        text = unicode(item.text())
        self.rmmpl()
        self.addmpl(self.fig_dict[text])
        self.active_fig=text
        self.update_view()
    def addfig(self, name, fig):
        self.fig_dict[name] = fig
        self.mplfigs.addItem(name)
    def addmpl(self,fig):
        self.canvas = FigureCanvas(fig[0])
        self.mplvl.addWidget(self.canvas)
        if fig[1]:
            #max=fig[1].get_array().max()
            #max=fig[2]
            #min=fig[3]
            actual_max=fig[4]
            actual_min=fig[5]
            #self.dsb_max.setValue(max)
            #self.dsb_min.setValue(min)
            #self.slider_max.setRange(min,max)
            #self.slider_max.setValue(actual_max)
            #self.slider_min.setRange(min,max)
            #self.slider_min.setValue(actual_min)
            fig[1].set_clim([actual_min,actual_max])
        
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)
#        self.addToolBar(self.toolbar)
    def rmmpl(self,):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #13
0
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, ):  #provide values for attributes at runtime
        super(Main, self).__init__()
        self.setupUi(self)
        self.pushBrowse.clicked.connect(self.selectFile)
        self.pushBrowse_2.clicked.connect(self.selectmlFile)
        self.pushApply.clicked.connect(self.apply)
        self.pushRun.clicked.connect(self.run)
        self.HomeDirectory = os.getcwd()  #saves the primary working directory
        self.directory = os.listdir(self.HomeDirectory)
        self.saveBttn.clicked.connect(self.file_save)
        self.actionOpen.triggered.connect(self.file_open)
        self.actionReset.triggered.connect(self.plots_close)
        self.message = 0
        #self.UI = []
        self.reset = 0

        #check User input for correct .csv file
        self.inputFile.setValidator(validator)
        self.inputFile.textChanged.connect(self.check_state)
        self.inputFile.textChanged.emit(self.inputFile.text())
        #check User input for correct .csv file for ml
        self.mlData.setValidator(validator)
        self.mlData.textChanged.connect(self.check_state)
        self.mlData.textChanged.emit(self.mlData.text())
        #shaft speed
        self.shaftSpeed.setValidator(regexp_checkdouble)
        self.shaftSpeed.textChanged.connect(self.check_state)
        self.shaftSpeed.textChanged.emit(self.shaftSpeed.text())
        #Num of rolling elements
        self.numberofElements.setValidator(regexp_checkint)
        self.numberofElements.textChanged.connect(self.check_state)
        self.numberofElements.textChanged.emit(self.numberofElements.text())
        #diameter of rolling elements
        self.diameterofElements.setValidator(regexp_checkdouble)
        self.diameterofElements.textChanged.connect(self.check_state)
        self.diameterofElements.textChanged.emit(
            self.diameterofElements.text())
        #pitch diameter
        self.pitchDiameter.setValidator(regexp_checkdouble)
        self.pitchDiameter.textChanged.connect(self.check_state)
        self.pitchDiameter.textChanged.emit(self.pitchDiameter.text())
        #Contact angle
        self.contactAngle.setValidator(regexp_checkdouble)
        self.contactAngle.textChanged.connect(self.check_state)
        self.contactAngle.textChanged.emit(self.contactAngle.text())
        #Frequency Acoustic
        self.samFreqAcst.setValidator(regexp_checkdouble)
        self.samFreqAcst.textChanged.connect(self.check_state)
        self.samFreqAcst.textChanged.emit(self.samFreqAcst.text())
        #Frequency Accelerometer
        """self.samFreqac.setValidator(regexp_checkdouble)
        self.samFreqac.textChanged.connect(self.check_state)
        self.samFreqac.textChanged.emit(self.samFreqac.text())"""

    def check_state(self, *args, **kwargs):
        #this function is changes the color of the lineedit fields depending on state
        sender = self.sender()
        validator = sender.validator()
        state = validator.validate(sender.text(), 0)[0]
        if state == QValidator.Acceptable:
            color = '#c4df9b'  # green
        elif state == QtGui.QValidator.Intermediate:
            color = '#fff79a'  # yellow
        else:
            color = '#f6989d'  # red
        sender.setStyleSheet('QLineEdit { background-color: %s }' % color)

    #creates a dictionary from the saved CSV
    def file_open(self):
        #function called when the open file action in triggered. Creates a dictionary from a CSV file.
        filename = QFileDialog.getOpenFileName()[0]
        reader = csv.reader(open(filename, 'r'))
        d = {}
        for row in reader:
            k, v = row
            d[k] = v

        print(d)

        self.setTextInfile(d)
        return True

    #used the dicitonary created above to assign saved variables to input parameters
    def setTextInfile(self, d):
        self.inputName.setText(d['inputName'])
        self.inputApplication.setText(d['inputApplication'])
        self.inputModelnum.setText(d['inputModelnum'])
        self.inputSavingalias.setText(d['inputSavingalias'])
        self.inputFile.setText(d['inputFile'])
        self.mlData.setText(d['mlData'])
        self.horsepower.setText(d['horsepower'])
        self.voltage.setText(d['voltage'])
        self.phase.setText(d['phase'])
        self.shaftnum.setText(d['shaftnum'])
        self.shaftSpeed.setText(d['shaftSpeed'])
        self.numberofElements.setText(d['numberofElements'])
        self.diameterofElements.setText(d['diameterofElements'])
        self.pitchDiameter.setText(d['pitchDiameter'])
        self.contactAngle.setText(d['contactAngle'])
        self.samFreqAcst.setText(d['samFreq'])

    """
    Hmm i wonder if this is used to save the file?
    """

    def file_save(self, ):
        #called when the save btn is clicked. converts user input to dictionary
        #then to dataframe then to csv file.
        dict = CreateSaveDictionary(self.inputName.text(),\
                                    self.inputApplication.text(),\
                                    self.inputModelnum.text(),\
                                    self.inputSavingalias.text(),\
                                    self.inputFile.text(),\
                                    self.mlData.text(), \
                                    self.horsepower.text(), \
                                    self.voltage.text(), \
                                    self.phase.text(), \
                                    self.shaftnum.text(),\
                                    self.shaftSpeed.text(),\
                                    self.numberofElements.text(), \
                                    self.diameterofElements.text(), \
                                    self.pitchDiameter.text(), \
                                    self.contactAngle.text(), \
                                    self.samFreqAcst.text())
        CreateCSVfromDict(dict)

    """
    These functions create the figure widgets and toolbars
    """

    #Accelerometer
    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph01UI.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph01UI.addWidget(self.toolbar)

    def addmpl02(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph02UI.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph02UI.addWidget(self.toolbar)

    def addgraph11(self, fig):
        self.canvas1 = FigureCanvas(fig)
        self.graph11UI.addWidget(self.canvas1)
        #self.canvas1.draw()
        self.toolbar1 = NavigationToolbar(self.canvas1,
                                          self.graph11,
                                          coordinates=True)
        self.graph11UI.addWidget(self.toolbar1)

    def addgraph12(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.graph12UI.addWidget(self.canvas2)
        #self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.graph12,
                                          coordinates=True)
        self.graph12UI.addWidget(self.toolbar2)

    def addgraph21(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.graph21UI.addWidget(self.canvas3)
        #self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.graph21,
                                          coordinates=True)
        self.graph21UI.addWidget(self.toolbar3)

    def addgraph22(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.graph22UI.addWidget(self.canvas4)
        #self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.graph22,
                                          coordinates=True)
        self.graph22UI.addWidget(self.toolbar4)

    #ACOUSTIC
    def addmpl_2(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph01UI_2.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum_2,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph01UI_2.addWidget(self.toolbar)

    def addmpl02_2(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph02UI_2.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum_2,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph02UI_2.addWidget(self.toolbar)

    def addgraph11_2(self, fig):
        self.canvas1 = FigureCanvas(fig)
        self.graph11UI_2.addWidget(self.canvas1)
        #self.canvas1.draw()
        self.toolbar1 = NavigationToolbar(self.canvas1,
                                          self.graph11_2,
                                          coordinates=True)
        self.graph11UI_2.addWidget(self.toolbar1)

    def addgraph12_2(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.graph12UI_2.addWidget(self.canvas2)
        #self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.graph12_2,
                                          coordinates=True)
        self.graph12UI_2.addWidget(self.toolbar2)

    def addgraph21_2(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.graph21UI_2.addWidget(self.canvas3)
        #self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.graph21_2,
                                          coordinates=True)
        self.graph21UI_2.addWidget(self.toolbar3)

    def addgraph22_2(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.graph22UI_2.addWidget(self.canvas4)
        #self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.graph22_2,
                                          coordinates=True)
        self.graph22UI_2.addWidget(self.toolbar4)

    #clearly selects file
    def selectFile(self, ):
        self.inputFile.setText(QFileDialog.getOpenFileName()[0])
        self.inputfile = self.inputFile.text()

    #selects file but for a ml data
    def selectmlFile(self, ):
        self.mlData.setText(QFileDialog.getOpenFileName()[0])

    """
    Apply checks user inputs and then assigns them to function parameter variables
    In case the user doesn't supply input for a specific field, default inputs will
    be inserted.
    """

    def apply(self, ):
        if self.inputSavingalias.text() != "":
            self.savingalias = self.inputSavingalias.text() + ".csv"
            self.inputSavingalias.setText(self.savingalias)

        if self.inputFile.text() != "":
            try:
                temp = self.inputFile.text()
                self.FileOfInterest = self.inputFile.text()
            except:
                print("pu")
        else:
            print("no input file selected, using demo file.")
            self.FileOfInterest = "AccelerometerActualDataEdited.csv"
            self.inputFile.setText("/AccelerometerActualDataEdited.csv")

        if self.mlData.text() != "":
            self.TrainingDataFile = self.mlData.text()
            print(self.TrainingDataFile)
        else:
            print("no ML data selected")
            self.TrainingDataFile = "MLSynthesizedData.csv"  #default file location
            self.mlData.setText("/MLSynthesizedData.csv")

        if self.shaftSpeed.text() != "":
            self.n = float(self.shaftSpeed.text())
            print("type n =", type(self.n))
        else:
            print("no shaft speed selected")
            self.n = 2000 / 60
            self.shaftSpeed.setText(str(self.n))

        if self.numberofElements.text() != "":
            self.N = int(self.numberofElements.text())
        else:
            print("Number of elements not specified")
            self.N = 16
            self.numberofElements.setText(str(self.N))

        if self.diameterofElements.text() != "":
            self.Bd = float(self.diameterofElements.text())
        else:
            print("Diameter of elements not specified")
            self.Bd = 0.331 * 254
            self.diameterofElements.setText(str(self.Bd))

        if self.pitchDiameter.text() != "":
            self.Pd = float(self.pitchDiameter.text())
        else:
            print("no pitch diameter specified")
            self.Pd = Pd = 2.815 * 254
            self.pitchDiameter.setText(str(self.Pd))

        if self.contactAngle.text() != "":
            self.phi = float(self.contactAngle.text())
        else:
            print("Contact angle not specified")
            self.phi = (15.17 * 3.14159) / 180
            self.contactAngle.setText(str(self.phi))

        if self.samFreqAcst.text() != "":
            self.SampleFrequency = float(self.samFreqAcst.text())

        else:
            print("no sample frequency specified")
            self.SampleFrequency = 20000
            self.samFreqAcst.setText(str(self.SampleFrequency))

        self.popup = MyPopup("Applied")
        self.popup.setGeometry(QtCore.QRect(100, 100, 400, 200))
        self.popup.show()

##############################################

    def getPlot(self, WhichPlot, plotinfo):
        X = plotinfo[0]
        Y = plotinfo[1]
        xlabel = plotinfo[2]
        ylabel = plotinfo[3]
        Title = plotinfo[4]
        if self.reset != 0:
            WhichPlot.cla()

        WhichPlot.plot(X, Y, c=np.random.rand(3, ))
        WhichPlot.set_xlabel(xlabel, fontsize=12)
        WhichPlot.set_ylabel(ylabel, fontsize=12)
        WhichPlot.set_title(Title)
        WhichPlot.grid(True)
        if self.reset != 0:
            self.canvas.draw()

        return None

    def run(self, ):  #called when run is clicked

        if self.reset == 0:
            #instantiate the figures
            self.fig0 = plt.figure()
            self.sub0 = self.fig0.subplots()

            self.fig1 = plt.figure()
            self.sub1 = self.fig1.subplots()

            self.fig2 = plt.figure()
            self.sub2 = self.fig2.subplots()

            self.fig3 = plt.figure()
            self.sub3 = self.fig3.subplots()

            self.fig4 = plt.figure()
            self.sub4 = self.fig4.subplots()

            self.fig5 = plt.figure()
            self.sub5 = self.fig5.subplots()

            self.fig6 = plt.figure()
            self.sub6 = self.fig6.subplots()

            self.fig7 = plt.figure()
            self.sub7 = self.fig7.subplots()

            self.fig8 = plt.figure()
            self.sub8 = self.fig8.subplots()

            self.fig9 = plt.figure()
            self.sub9 = self.fig9.subplots()

        ###############################################################################
        #begin calling ml functions for processing

        #General
        Name_ID = "1"
        Application = "2"
        ModelNumber = "3"
        SavingAlias = "4"
        AccelerometerDataFilename = 'AccelerometerActualDataEdited.csv'  #filename #main file
        AcousticDataFilename = 'DataOutputMic2Col.csv'  #Wave File Name
        MLDataFilename = "MLSynthesizedData.csv"

        #Motor Characteristics
        Horsepower = "6"
        RatedVoltage = "7"
        ACorDC = "DC"
        NumberOfPolePairs = "9"
        NumberofShafts = "10"

        #Bearing Information
        ShaftSpeed = 300  #Also Used for Motor Characteristics
        NumberOfRollingElements = 3
        DiameterOfRollingElements = 3
        PitchDiameter = .2
        ContactAngle = .2

        #Processing Information
        AccelerometerSamplingFrequency = 14000  #must be an non-zero int or float
        AcousticSamplingFrequency = 30000  #must be an non-zero int or float

        #Microcontroller Information
        #Receive Required Information
        A2DResolution = 16
        VoltageMax = 5
        VoltageMin = 0

        #System/Sensor Known Constants
        AccelerationMax = 50
        AccelerationMin = -50

        #Convert User Inputs into a condensed form
        OnlyUserInput = Inputs2CondensedForm(Name_ID, Application, ModelNumber, SavingAlias,\
                                             AccelerometerDataFilename, AcousticDataFilename, \
                                             MLDataFilename, Horsepower, RatedVoltage, ACorDC, \
                                             NumberOfPolePairs, NumberofShafts, \
                                             ShaftSpeed, NumberOfRollingElements, \
                                             DiameterOfRollingElements,PitchDiameter, ContactAngle, \
                                             AccelerometerSamplingFrequency, AcousticSamplingFrequency)

        SystemInputs = System2CondensedForm(A2DResolution, VoltageMax,
                                            VoltageMin, AccelerationMax,
                                            AccelerationMin)

        #Acquire Accelerometer Actual Data
        time, amp, Voltage, Acceleration = ExtractAccelerometerData(
            OnlyUserInput, SystemInputs)

        #Acquire Acoustic Actual Data
        Channel1Time, Channel1Value, Channe21Time, Channe2Value = ExtractAcousticData(
            OnlyUserInput, SystemInputs)

        #Put All Data into Working Form
        trial = 2  #Select the instance of the data ************needs future work
        AllData = AllData2WorkingForm(OnlyUserInput,SystemInputs,time[trial], \
                                      amp[trial], Voltage[trial], Acceleration[trial],\
                                     Channel1Time,Channel1Value,Channe21Time,Channe2Value)

        #Machine Learning
        TestDF = getTESTDataFrame(AllData)
        TestMatrix = getTESTMatrix(TestDF)
        Xall_train, Yall_train, dataset = GetTrainingData(AllData)
        FinalClassifier = TrainModel(Xall_train, Yall_train)

        #Predict
        prediction, prediction_string = PredictModel(FinalClassifier,
                                                     TestMatrix)
        prediction_proba = PredictProbModel(FinalClassifier, TestMatrix)

        plt.close('all')
        plot0info,plot1info,plot2info,plot3info,plot4info,\
        plot5info,plot6info,plot7info,plot8info,plot9info = getGraphs(AllData)
        #ACCELEROMETER PLOTTING
        self.getPlot(self.sub0, plot0info)  #Raw Data
        self.getPlot(self.sub1, plot1info)  #Raw Data w DC removed
        self.getPlot(self.sub2, plot2info)  #Normalized
        self.getPlot(self.sub3, plot3info)  #FFT
        self.getPlot(self.sub4, plot4info)  #PSD

        #ACOUSTIC PLOTTING
        self.getPlot(self.sub5, plot5info)
        self.getPlot(self.sub6, plot6info)
        self.getPlot(self.sub7, plot7info)
        self.getPlot(self.sub8, plot8info)
        self.getPlot(self.sub9, plot9info)

        ###############################################################################

        if self.reset == 0:
            self.addmpl(self.fig0)
            self.addmpl02(self.fig2)
            self.addgraph11(self.fig1)
            self.addgraph12(self.fig3)
            self.addgraph21(self.fig4)
            #self.addgraph22(self.fig4)

            self.addmpl_2(self.fig5)  #Raw Data
            self.addmpl02_2(self.fig7)  #normalized
            self.addgraph11_2(self.fig6)  #Raw Data w DC removed
            self.addgraph12_2(self.fig8)  #FFT
            self.addgraph21_2(self.fig9)  #PSD

        self.BSF.setText(str(truncate(TestDF["BSF"].values[0], 2)))
        self.BPFI.setText(str(truncate(TestDF["BPFI"].values[0], 2)))
        self.BPFO.setText(str(truncate(TestDF["BPFO"].values[0], 2)))
        self.FTF.setText(str(truncate(TestDF["FTF"].values[0], 2)))

        self.earlyEdit.setText(str(truncate(prediction_proba[0, 0], 2)))
        self.suspectEdit.setText(str(truncate(prediction_proba[0, 1], 2)))
        self.normalEdit.setText(str(truncate(prediction_proba[0, 2], 2)))
        """
        self.immEdit.setText(str(Prediction[0,3]))
        self.innerEdit.setText(str(Prediction[0,4]))
        self.rollingEdit.setText(str(Prediction[0,5]))
        self.stageEdit.setText(str(Prediction[0,6]))
        """
        self.reset = 1

    def close_application(self, ):  #self explanitory
        sys.exit()


###############################################################################

    def updategraphs(self, fig):
        print("made it to update graphs")
        sip.delete(self.canvas)
        sip.delete(self.canvas1)
        sip.delete(self.canvas2)
        sip.delete(self.canvas3)
        sip.delete(self.canvas4)
        #self.spectrumUI.removeWidget(self.canvas)
        self.canvas = FigureCanvas(fig[0])
        self.canvas1 = FigureCanvas(fig[1])
        self.canvas2 = FigureCanvas(fig[2])
        self.canvas3 = FigureCanvas(fig[3])
        self.canvas4 = FigureCanvas(fig[4])

    def rmmpl(self, ):
        print("in rmmpl")
        self.spectrumUI.removeWidget(self.canvas)
        self.canvas.close()
        self.spectrumUI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph11UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph11UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph12UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph12UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph21UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph21UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph22UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph22UI.removeWidget(self.toolbar)
        self.toolbar.close()

    def plots_close(self, ):
        print("made it to plots_close")
        self.spectrumUI.removeWidget(self.canvas)
        sip.delete(self.canvas)
        self.canvas = None
        self.spectrumUI.removeWidget(self.toolbar)
        sip.delete(self.toolbar)
        self.toolbar = None

        self.graph11UI.removeWidget(self.canvas1)
        self.canvas1.close()
        self.graph11UI.removeWidget(self.toolbar1)
        self.toolbar1.close()

        self.graph12UI.removeWidget(self.canvas2)
        self.canvas2.close()
        self.graph12UI.removeWidget(self.toolbar2)
        self.toolbar2.close()

        self.graph21UI.removeWidget(self.canvas3)
        self.canvas3.close()
        self.graph21UI.removeWidget(self.toolbar3)
        self.toolbar3.close()

        self.graph22UI.removeWidget(self.canvas4)
        self.canvas4.close()
        self.graph22UI.removeWidget(self.toolbar4)
        self.toolbar4.close()
예제 #14
0
class SWATGraph(QObject):
    """Display SWAT result data as line graphs or bar chart."""
    
    def __init__(self, csvFile):
        """Initialise class variables."""
        QObject.__init__(self)
        self._dlg = GraphDialog()
        self._dlg.setWindowFlags(self._dlg.windowFlags() & ~Qt.WindowContextHelpButtonHint)
        ## csv file of results
        self.csvFile = csvFile
        ## canvas for displaying matplotlib figure
        self.canvas = None
        ## matplotlib tool bar
        self.toolbar = None
        ## matplotlib axes
        self.ax1 = None
        
    def run(self):
        """Initialise form and run on initial csv file."""
        self._dlg.lineOrBar.addItem('Line graph')
        self._dlg.lineOrBar.addItem('Bar chart')
        self._dlg.lineOrBar.setCurrentIndex(0)
        self._dlg.newFile.clicked.connect(self.getCsv)
        self._dlg.updateButton.clicked.connect(self.updateGraph)
        self._dlg.closeForm.clicked.connect(self.closeFun)
        self.setUbuntuFont()
        self.readCsv()
        self._dlg.exec_()
        
    def addmpl(self, fig):
        """Add graph defined in fig."""
        self.canvas = FigureCanvas(fig)
        # graphvl is the QVBoxLayout instance added to the graph widget.
        # Needed to make fig expand to fill graph widget.
        self._dlg.graphvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, 
                self._dlg.graph, coordinates=True)
        self._dlg.graphvl.addWidget(self.toolbar)
        
    def rmmpl(self):
        """Remove current graph if any."""
        try:
            self._dlg.graphvl.removeWidget(self.canvas)
            self.canvas.close()
            self._dlg.graphvl.removeWidget(self.toolbar)
            self.toolbar.close()
            self.ax1 = None
            return
        except Exception:
            # no problem = may not have been a graph
            return
        
    @staticmethod
    def trans(msg):
        """Translate message."""
        return QApplication.translate("QSWATPlus", msg, None)
        
    @staticmethod
    def error(msg):
        """Report msg as an error."""
        msgbox = QMessageBox()
        msgbox.setWindowTitle('SWATGraph')
        msgbox.setIcon(QMessageBox.Critical)
        msgbox.setText(SWATGraph.trans(msg))
        msgbox.exec_()
        return
    
    def getCsv(self):
        """Ask user for csv file."""
        settings = QSettings()
        if settings.contains('/QSWATPlus/LastInputPath'):
            path = str(settings.value('/QSWATPlus/LastInputPath'))
        else:
            path = ''
        filtr = self.trans('CSV files (*.csv)')
        csvFile, _ = QFileDialog.getOpenFileName(None, 'Open csv file', path, filtr)
        if csvFile is not None and csvFile != '':
            settings.setValue('/QSWATPlus/LastInputPath', os.path.dirname(str(csvFile)))
            self.csvFile = csvFile
            self.readCsv()
        
    def readCsv(self):
        """Read current csv file (if any)."""
        # csvFile may be none if run from command line
        if not self.csvFile or self.csvFile == '':
            return
        if not os.path.exists(self.csvFile):
            self.error('Error: Cannot find csv file {0}'.format(self.csvFile))
            return
        """Read csv file into table; create statistics (coefficients); draw graph."""
        # clear graph
        self.rmmpl()
        # clear table
        self._dlg.table.clear()
        for i in range(self._dlg.table.columnCount()-1, -1, -1):
            self._dlg.table.removeColumn(i)
        self._dlg.table.setColumnCount(0)
        self._dlg.table.setRowCount(0)
        row = 0
        numCols = 0
        with open(self.csvFile, 'r', newline='') as csvFil:
            reader = csv.reader(csvFil)
            for line in reader:
                try:
                    # use headers in first line
                    if row == 0:
                        numCols = len(line)
                        for i in range(numCols):
                            self._dlg.table.insertColumn(i)
                        self._dlg.table.setHorizontalHeaderLabels(line)
                    else:
                        self._dlg.table.insertRow(row-1)
                        for i in range(numCols):
                            try:
                                val = line[i].strip()
                            except Exception as e:
                                self.error('Error: could not read file {0} at line {1} column {2}: {3}'.format(self.csvFile, row+1, i+1, repr(e)))
                                return
                            item = QTableWidgetItem(val)
                            self._dlg.table.setItem(row-1, i, item)
                    row = row + 1
                except Exception as e:
                    self.error('Error: could not read file {0} at line {1}: {2}'.format(self.csvFile, row+1, repr(e)))
                    return
        # columns are too narrow for headings
        self._dlg.table.resizeColumnsToContents()
        # rows are too widely spaced vertically
        self._dlg.table.resizeRowsToContents()
        self.writeStats()
        self.updateGraph()
        
    @staticmethod
    def makeFloat(s):
        """Parse string s as float and return; return nan on failure."""
        try:
            return float(s)
        except Exception:
            return float('nan')
        
    def updateGraph(self):
        """Redraw graph as line or bar chart according to lineOrBar setting."""
        style = 'bar' if self._dlg.lineOrBar.currentText() == 'Bar chart' else 'line'
        self.drawGraph(style)
        
    @staticmethod
    def shiftDates(dates, shift):
        """Add shift (number of days) to each date in dates."""
        delta = timedelta(days=shift)
        return [x + delta for x in dates]
    
    @staticmethod
    def getDateFormat(date):
        """
        Return date strptime format string from example date, plus basic width for drawing bar charts.
    
        Basic width is how matplotlib divides the date axis: number of days in the time unit
        Assumes date has one of 3 formats:
        yyyy: annual: return %Y and 12
        yyyy/m or yyyy/mm: monthly: return %Y/%m and 30
        yyyyddd: daily: return %Y%j and 24
        """
        if date.find('/') > 0:
            return '%Y/%m', 30
        length = len(date)
        if length == 4:
            return '%Y', 365
        if length == 7:
            return '%Y%j', 1
        SWATGraph.error('Cannot parse date {0}'.format(date))
        return '', 1
    
        
    def drawGraph(self, style):
        """Draw graph as line or bar chart according to style."""
        # preserve title, labels and yscale if they exist
        # in order to replace them when updating graph
        try:
            title = self.ax1.get_title()
        except Exception:
            title = ''
        try:
            xlbl = self.ax1.get_xlabel()
        except Exception:
            xlbl = ''
        try:
            ylbl = self.ax1.get_ylabel()
        except Exception:
            ylbl = ''
        try:
            yscl = self.ax1.get_yscale()
        except Exception:
            yscl = ''
        self.rmmpl()
        fig = Figure()
        # left, bottom, width, height adjusted to leave space for legend below
        self.ax1 = fig.add_axes([0.05, 0.22, 0.92, 0.68])
        numPlots = self._dlg.table.columnCount() - 1
        rng = range(self._dlg.table.rowCount())
        fmt, widthBase = self.getDateFormat(str(self._dlg.table.item(0, 0).text()).strip())
        if fmt == '':
            # could not parse
            return
        xVals = [datetime.strptime(str(self._dlg.table.item(i, 0).text()).strip(), fmt) for i in rng]
        for col in range(1, numPlots+1):
            yVals = [self.makeFloat(self._dlg.table.item(i, col).text()) for i in rng]
            h = self._dlg.table.horizontalHeaderItem(col).text()
            if style == 'line':
                self.ax1.plot(xVals, yVals, label=h)
                # reinstate yscale (only relevant for line graphs)
                if yscl == 'log':
                    self.ax1.set_yscale(yscl, nonposy='mask')
                elif yscl != '':
                    self.ax1.set_yscale(yscl)
            else:
                # width of bars in days.  
                # adding 1 to divisor gives space of size width between each date's group
                width = float(widthBase) / (numPlots+1)
                # can't imagine anyone wanting more than 7 colours
                # but just in case we'll use shades of grey for 8 upwards
                colours = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
                colour = colours[col-1] if col <= 7 else str(float((col - 7)/(numPlots - 6)))
                mid = numPlots / 2
                shift = width * (col - 1 - mid)
                xValsShifted = xVals if shift == 0 else self.shiftDates(xVals, shift)
                self.ax1.bar(xValsShifted, yVals, width, color=colour, linewidth=0, label=h)
        # reinstate title and labels
        if title != '':
            self.ax1.set_title(title)
        if xlbl != '':
            self.ax1.set_xlabel(xlbl)
        else:
            self.ax1.set_xlabel('Date')
        if ylbl != '':
            self.ax1.set_ylabel(ylbl)
        self.ax1.grid(True)
        legendCols = min(4, self._dlg.table.columnCount())
        fontSize = 'x-small' if legendCols > 4 else 'small'
        self.ax1.legend(bbox_to_anchor=(1.0, -0.15), ncol=legendCols, fontsize=fontSize)
        self.addmpl(fig)
    
    def closeFun(self):
        """Close dialog."""
        self._dlg.close()
        
    def writeStats(self):
        """Write Pearson and Nash coefficients."""
        numCols = self._dlg.table.columnCount()
        numRows = self._dlg.table.rowCount()
        self._dlg.coeffs.clear()
        for i in range(1, numCols):
            for j in range(i+1, numCols):
                self.pearson(i, j, numRows)
        for i in range(1, numCols):
            for j in range(i+1, numCols):
                # only compute Nash-Sutcliffe Efficiency
                # if one plot is observed,
                # and use that as the first plot
                if str(self._dlg.table.horizontalHeaderItem(i).text()).find('observed') == 0:
                    if not str(self._dlg.table.horizontalHeaderItem(j).text()).find('observed') == 0:
                        self.nash(i, j, numRows)
                elif str(self._dlg.table.horizontalHeaderItem(j).text()).find('observed') == 0:
                    self.nash(j, i, numRows)
        
    def multiSums(self, idx1, idx2, N):
        """Return various sums for two series, only including points where both are numbers, plus count of such values."""
        s1 = 0
        s2 = 0
        s11 = 0
        s22 = 0
        s12 = 0
        count = 0
        for i in range(N):
            val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
            val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
            # ignore missing values
            if not (math.isnan(val1) or math.isnan(val2)):
                s1 += val1
                s2 += val2
                s11 += val1 * val1
                s22 += val2 * val2
                s12 += val1 * val2
                count = count + 1
        return (s1, s2, s11, s22, s12, count)
        
        
    def sum1(self, idx1, idx2, N):
        """Return sum for series1, only including points where both are numbers, plus count of such values.""" 
        s1 = 0
        count = 0
        for i in range(N):
            val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
            val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
            # ignore missing values
            if not (math.isnan(val1) or math.isnan(val2)):
                s1 += val1
                count = count + 1
        return (s1, count)
        
    def pearson(self, idx1, idx2, N):
        """Calculate and display Pearson correlation coefficients for each pair of plots."""
        s1, s2, s11, s22, s12, count = self.multiSums(idx1, idx2, N)
        if count == 0: return
        sqx = (count * s11) - (s1 * s1)
        sqy = (count * s22) - (s2 * s2)
        sxy = (count * s12) - (s1 * s2)
        deno = math.sqrt(sqx * sqy)
        if deno == 0: return
        rho = sxy / deno
        if count < N:
            extra = ' (using {0!s} of {1!s} values)'.format(count , N)
        else:
            extra = ''
        msg = 'Series1: ' + self._dlg.table.horizontalHeaderItem(idx1).text() + \
            '  Series2: ' + self._dlg.table.horizontalHeaderItem(idx2).text() + '  Pearson Correlation Coefficient = {0}{1}'.format(locale.format_string('%.2F', rho), extra)
        self._dlg.coeffs.append(SWATGraph.trans(msg))

    def nash(self, idx1, idx2, N):
        """Calculate and display Nash-Sutcliffe efficiency coefficients for each pair of plots where one is observed."""
        s1, count = self.sum1(idx1, idx2, N)
        if count == 0: return
        mean = s1 / count
        num = 0
        deno = 0
        for i in range(N):
            val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
            val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
            # ignore missing values
            if not (math.isnan(val1) or math.isnan(val2)):
                diff12 = val1 - val2
                diff1m = val1 - mean
                num += diff12 * diff12
                deno += diff1m * diff1m
        if deno == 0: return
        result = 1 - (num / deno)
        if count < N:
            extra = ' (using {0!s} of {1!s} values)'.format(count , N)
        else:
            extra = ''
        msg = 'Series1: ' + self._dlg.table.horizontalHeaderItem(idx1).text() + \
            '  Series2: ' + self._dlg.table.horizontalHeaderItem(idx2).text() + '   Nash-Sutcliffe Efficiency Coefficient = {0}{1}'.format(locale.format_string('%.2F', result), extra)
        self._dlg.coeffs.append(SWATGraph.trans(msg))
        
    def setUbuntuFont(self):
        """Set Ubuntu font size 10 as default."""
        QFontDatabase.addApplicationFont(":/fonts/Ubuntu-R.ttf")
        ufont = QFont("Ubuntu", 10, 1)
        QApplication.setFont(ufont)
예제 #15
0
class bivariaterowDlg(QtWidgets.QDialog, Ui_bivariaterow):
    def __init__(self, parent=None):
        super(bivariaterowDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.XcomboBox.addItem('Auto')
        self.YcomboBox.addItem('All')
        self.XcomboBox.addItems(DS.Lr[DS.Ir])
        self.YcomboBox.addItems(DS.Lr[DS.Ir])
        self.GcomboBox.addItem('All')
        groups = pd.Series(DS.Gr[DS.Ir])
        groups = groups.dropna()
        groups = groups.values
        groups = groups.astype('str')
        groups = np.unique(groups)
        groups = np.sort(groups)
        self.GcomboBox.addItems(groups)
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        if self.XcomboBox.currentText() == self.YcomboBox.currentText():
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           'Variables \n must be different !',
                                           QtWidgets.QMessageBox.Ok)
            return ()
        if (self.XcomboBox.currentText() == 'Auto') and (
                self.YcomboBox.currentText()
                == 'All') and not self.scatterradioButton.isChecked():
            QtWidgets.QMessageBox.critical(
                self, 'Error',
                "You have to select two rows \n for this kind of plot!",
                QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        data = data.assign(Lr=DS.Lr[DS.Ir])
        data = data.assign(Cr=DS.Cr[DS.Ir])
        data = data.assign(Gr=DS.Gr[DS.Ir])
        if (self.XcomboBox.currentText() !=
                'Auto') and (self.YcomboBox.currentText() != 'All'):
            data = data.loc[[
                self.XcomboBox.currentText(),
                self.YcomboBox.currentText()
            ]]
        elif (self.XcomboBox.currentText() !=
              'Auto') and (self.YcomboBox.currentText() == 'All'):
            QtWidgets.QMessageBox.critical(self, 'Error', "Select two rows!",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        elif (self.XcomboBox.currentText()
              == 'Auto') and (self.YcomboBox.currentText() != 'All'):
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "Use Univariate plot!",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        Nnan = data.isnull().isnull().all().all()
        data = data.T.dropna()
        data = data.T
        Lr = data['Lr'].values
        Cr = data['Cr'].values
        Gr = data['Gr'].values
        data = data.drop('Lr', axis=1)
        data = data.drop('Cr', axis=1)
        data = data.drop('Gr', axis=1)
        if data.dtypes.all() == 'float' and data.dtypes.all() == 'int':
            QtWidgets.QMessageBox.critical(self,'Error',"Some values are not numbers!",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        if (self.XcomboBox.currentText() !=
                'Auto') and (self.YcomboBox.currentText() != 'All'):
            if data.shape[0] != 2:
                QtWidgets.QMessageBox.critical(self,'Error',"Raw labels must be different",\
                                               QtWidgets.QMessageBox.Ok)
                return ()
            x = data.loc[self.XcomboBox.currentText()].values
            y = data.loc[self.YcomboBox.currentText()].values
        fig = Figure()
        ax = fig.add_subplot(111)
        color = 'blue'
        if self.scatterradioButton.isChecked():
            if (self.XcomboBox.currentText() !=
                    'Auto') and (self.YcomboBox.currentText() != 'All'):
                if self.PcheckBox.isChecked():
                    ax.scatter(x, y, marker='o', color=Cr)
                if self.LcheckBox.isChecked():
                    ax.plot(x, y, color='blue')
                if self.VcheckBox.isChecked():
                    for i, txt in enumerate(Lr):
                        ax.annotate(txt, (x[i], y[i]))
                ax.set_xlabel(self.XcomboBox.currentText())
                ax.set_ylabel(self.YcomboBox.currentText())
            else:
                nr, nc = data.shape
                Lc = DS.Lc[DS.Ic]
                x = range(1, nc + 1)
                color = Cr
                if self.GcheckBox.isChecked():
                    groups = Gr
                    ngr = len(np.unique(groups))
                    color = []
                    for key in groups:
                        color.append(cm.viridis.colors[int(
                            (len(cm.viridis.colors) - 1) / ngr * key)])
                for i in range(nr):
                    y = data.iloc[i, :]
                    col = color[i]
                    if self.GcomboBox.currentText() == 'All':
                        if self.PcheckBox.isChecked():
                            ax.scatter(x, y, marker='o', color=col)
                        if self.LcheckBox.isChecked():
                            ax.plot(x, y, color=col)
                    else:
                        if int(self.GcomboBox.currentText()) == groups[i]:
                            if self.PcheckBox.isChecked():
                                ax.scatter(x, y, marker='o', color=col)
                            if self.LcheckBox.isChecked():
                                ax.plot(x, y, color=col)
                if (nc > 30):
                    itick = np.linspace(0, nc - 1, 20).astype(int)
                    ltick = Lc[itick]
                else:
                    itick = x
                    ltick = Lc
                ax.set_xlim([0, nc + 2])
                ax.set_xticks(itick)
                ax.set_xticklabels(ltick, rotation='vertical')
        if self.ellipseradioButton.isChecked():

            def plot_ellipse(x, y, nstd=2, ax=None, **kwargs):
                def eigsorted(cov):
                    vals, vecs = np.linalg.eigh(cov)
                    order = vals.argsort()[::-1]
                    return vals[order], vecs[:, order]

                pos = (x.mean(), y.mean())
                cov = np.cov(x, y).tolist()
                vals, vecs = eigsorted(cov)
                theta = np.degrees(np.arctan2(*vecs[:, 0][::-1]))
                width, height = 2 * nstd * np.sqrt(vals)
                ellip = Ellipse(xy=pos,
                                width=width,
                                height=height,
                                angle=theta,
                                fill=False,
                                **kwargs)
                ax.add_artist(ellip)
                return ellip

            for j in range(1, 4):
                plot_ellipse(x, y, j, ax)
            ax.scatter(x, y)
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.YcomboBox.currentText())
            ax.set_title('Ellipse for 1,2,3 times the Standard Deviation')
        if self.boxcoxradioButton.isChecked():
            if (not (x > 0).all()) and (not (y > 0).all()):
                QtWidgets.QMessageBox.critical(self,'Error',"Values must be strictly positive",\
                                               QtWidgets.QMessageBox.Ok)
                return ()
            CBC = np.zeros(50)
            vlambda = np.linspace(-2, 2, 50)
            for i in range(50):
                trans_x = stats.boxcox(x, vlambda[i])
                CBC[i] = np.corrcoef(trans_x, y)[0, 1]
            if self.PcheckBox.isChecked():
                ax.scatter(vlambda, CBC, marker='o', color=color)
            if self.LcheckBox.isChecked():
                ax.plot(vlambda, CBC, color=color)
            ax.set_xlabel('Lambda')
            ax.set_ylabel('Correlation Coefficient')
        if self.histogramradioButton.isChecked():
            cx = 'blue'
            cy = 'red'
            xm = x.mean()
            ym = y.mean()
            xstd = x.std()
            ystd = y.std()
            dy = (ym - 3 * ystd) - (xm + 3 * xstd)
            dx = (xm - 3 * xstd) - (ym + 3 * ystd)
            if (dy > 0) | (dx > 0):
                x = sk.preprocessing.normalize(x.reshape(1, -1),
                                               norm='l2',
                                               axis=1,
                                               copy=True,
                                               return_norm=False)
                y = sk.preprocessing.normalize(y.reshape(1, -1),
                                               norm='l2',
                                               axis=1,
                                               copy=True,
                                               return_norm=False)
                x = x.ravel()
                y = y.ravel()
                ax.set_xlabel('Normalized Quantities')
            iqr = np.percentile(x, [75, 25])
            iqr = iqr[0] - iqr[1]
            n = x.size
            dx = abs(max((x.max(), y.max())) - min((x.min(), y.min())))
            nbins = int(np.floor(dx / (2 * iqr) * n**(1 / 3))) + 1
            if nbins > self.spinBox.value():
                self.spinBox.setValue(nbins)
            else:
                nbins = self.spinBox.value()
            bins = np.linspace(min((x.min(), y.min())), max(
                (x.max(), y.max())), nbins)
            ax.hist(x,
                    bins=bins,
                    histtype='bar',
                    color=cx,
                    alpha=0.5,
                    orientation='vertical',
                    label=str(self.XcomboBox.currentText()))
            ax.hist(y,
                    bins=bins,
                    histtype='bar',
                    color=cy,
                    alpha=0.5,
                    orientation='vertical',
                    label=str(self.YcomboBox.currentText()))
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
            ax.legend(bbox_to_anchor=(1, 1),
                      loc='upper left',
                      borderaxespad=0.2)
        if Nnan:
            ax.annotate('{:04.2f} NaN'.format(Nnan),
                        xy=(0.80, 0.95),
                        xycoords='figure fraction')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        if self.TlineEdit.text():
            ax.set_title(self.TlineEdit.text())
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.LcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #16
0
class WidgetMatplot(
        QWidget, ):
    def __init__(self, ):
        super().__init__()
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        # defualt figure
        self.figure_default = Figure()
        ax = self.figure_default.add_subplot(111)
        x = np.arange(0., 2 * np.pi, 0.1)
        ax.plot(x, np.sin(x))
        ax.text(0.5, 0.0, 'This is a plot area')
        # setup Widgets
        self.add_plot(self.figure_default)
        #self.canvas = FigureCanvas(self.figure_default)
        #self.layout.addWidget(self.canvas)
        #self.toolbar = NavigationToolbar(self.canvas,
        #        self, coordinates =True)
        #self.layout.addWidget(self.toolbar)
        self.axes = None  #this is defined when figure/plot is added

    def draw_axes(self, x, y, label=None, fmt=None):
        """
        Instead of replace whole figure,
        add a line in existing axes  
        
        Seems to be not working !! 
        """
        if fmt:
            self.axes.plot(x, y, fmt, label=label)
        else:
            self.axes.plot(x, y, label=label)

    def reset_plot(self, ):
        # show default figure
        self.rm_plot()
        self.add_plot(self.figure_default)

    def add_plot(self, fig):
        # add a figure in the canvas.
        # figure is external
        # add canvas and toolbar to layout
        # To replace it one have to remove previous one first.
        self.axes = fig.get_axes()[0]  # suppose only one axes..
        self.canvas = FigureCanvas(fig)
        self.layout.addWidget(self.canvas)  # add a canvas widget into layout
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.layout.addWidget(self.toolbar)

    def rm_plot(self, ):
        # remove canvas and toolbar
        self.layout.removeWidget(self.canvas)
        self.canvas.close()
        #self.canvas.setParent(None)
        self.layout.removeWidget(self.toolbar)
        self.toolbar.close()
        #self.toolbar.setParent(None)

    def add_plot_by_data(self,
                         list_of_data_to_plot,
                         show_grid=False,
                         yscale='linear',
                         xlabel='c.m. angle[deg]',
                         ylabel='mb'):
        """
        Parameters
        ----------
        list_of_data_to_plot : list of dictionary to plot
            [plot1={'x': array ,'y':array,'yerr': array,
                    'label' : legend txt, 'fmt': matplotlib fmt},
             plot2={},..]


        show_grid : TYPE, optional
            DESCRIPTION. The default is False.
        yscale : TYPE, optional
            DESCRIPTION. The default is 'linear'.
        xlabel : TYPE, optional
            DESCRIPTION. The default is 'c.m. angle[deg]'.
        ylabel : TYPE, optional
            DESCRIPTION. The default is 'mb'.

        Returns
        -------
        None.

        """
        # remove previous plot
        self.rm_plot()
        fig = Figure()
        ax = fig.add_subplot(111)
        for p in list_of_data_to_plot:
            try:  # check whether a yerr exits in data
                yerr = p['yerr']
                plot_err = True
            except:
                plot_err = False
            if plot_err:  #error exist
                ax.errorbar(p['x'],
                            p['y'],
                            yerr=yerr,
                            fmt=p['fmt'],
                            label=p['label'])
            else:
                ax.plot(p['x'], p['y'], p['fmt'], label=p['label'])
        ax.legend()
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        ax.set_yscale(yscale)
        if show_grid:
            ax.grid()
        self.add_plot(fig)
        return
예제 #17
0
class univariategroupDlg(QtWidgets.QDialog, Ui_univariategroup):
    def __init__(self, parent=None):
        super(univariategroupDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.YcomboBox.addItems(DS.Lc[DS.Ic])
        self.YcomboBox.setCurrentIndex(0)
        self.TcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        groups = pd.Series(DS.Gr[DS.Ir], dtype='int')
        gr_type = [isinstance(e, (int, np.integer)) for e in groups]
        if sum(gr_type) != len(groups):
            QtWidgets.QMessageBox.critical(self,'Error',"Groups must be all present \n and must be integers",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw[DS.Ir]
        data = data[self.YcomboBox.currentText()]
        if data.shape[1] != 1:
            QtWidgets.QMessageBox.critical(self,'Error',"More columns have the same name",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        YG = pd.DataFrame({
            'G': groups.values,
            'Y': data.values,
            'Cr': DS.Cr[DS.Ir],
            'Lr': DS.Lr[DS.Ir]
        })
        YG = YG[~data.isnull().values]
        YGg = YG.groupby(['G'])
        ngr = len(YGg)
        color = 'blue'
        fig = Figure()
        ax = fig.add_subplot(111)
        if self.CcheckBox.isChecked():
            color = DS.Cc[self.YcomboBox.currentIndex() - 1]
        if (self.BoxradioButton.isChecked()):
            plt.hold = True
            boxes = []
            nbox = []
            medianprops = dict(marker='D',
                               markeredgecolor='black',
                               markerfacecolor=color)
            for key, grp in YGg:
                nbox.append(key)
                boxes.append(grp['Y'])
            ax.boxplot(boxes, vert=1, medianprops=medianprops)
            ax.set_xticklabels(nbox, rotation='vertical')
            if self.YcheckBox.isChecked():
                if self.YlineEdit.text():
                    ax.set_ylabel(self.YlineEdit.text())
            if self.XcheckBox.isChecked():
                if self.XlineEdit.text():
                    ax.set_xlabel(self.YlineEdit.text())
        if (self.ConditioningradioButton.isChecked()):
            for key, grp in YGg:
                if self.CcheckBox.isChecked():
                    vcol = grp['Cr']
                else:
                    vcol = cm.magma.colors[int(
                        (len(cm.magma.colors) - 1) / ngr * key)]
                ax.scatter(range(1,
                                 len(grp['Y']) + 1),
                           grp['Y'],
                           color=vcol,
                           marker='o',
                           label=key)
            ax.legend(loc='best')
        elif (self.histogramradioButton.isChecked()):
            iqr = np.percentile(YG['Y'], [75, 25])
            iqr = iqr[0] - iqr[1]
            n = YG['Y'].shape[0]
            dy = abs(float(YG['Y'].max()) - float(YG['Y'].min()))
            nbins = np.floor(dy / (2 * iqr) * n**(1 / 3)) + 1
            nbins = 2 * nbins
            bins = np.linspace(float(YG['Y'].min()), float(YG['Y'].max()),
                               nbins)
            for key, grp in YGg:
                if self.CcheckBox.isChecked():
                    vcol = grp['Cr']
                    vcol = np.unique(vcol)[0]
                else:
                    vcol = cm.magma.colors[int(
                        (len(cm.magma.colors) - 1) / ngr * key)]
                ax.hist(grp['Y'],
                        bins=bins,
                        histtype='bar',
                        color=vcol,
                        alpha=0.5,
                        orientation='vertical',
                        label=str(key))
            ax.legend(loc='best')
        if self.TcheckBox.isChecked():
            if self.TlineEdit.text():
                ax.set_title(self.TlineEdit.text())
        else:
            ax.set_title('')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.YcomboBox.setCurrentIndex(0)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
class timeseriesplotDlg(QtWidgets.QDialog, Ui_timeseriesplotDialog):
    def __init__(self, parent=None):
        super(timeseriesplotDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.YcomboBox.addItems(DS.Lc)
        self.autocorrelationadioButton.setChecked(True)
        self.TcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        if self.YcomboBox.currentText() == 'None':
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "No Variable \n Selected !",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        data = data.assign(Lr=DS.Lr[DS.Ir])
        data = data.assign(Cr=DS.Cr[DS.Ir])
        data = data[[self.YcomboBox.currentText(), 'Lr', 'Cr']]
        Nnan = data[self.YcomboBox.currentText()].isnull().all()
        data = data.dropna()
        #Lr=data['Lr'].values
        data = data.drop('Lr', axis=1)
        data = data.drop('Cr', axis=1)
        Y = data.values.ravel()
        if Y.dtype == 'float' and Y.dtype == 'int':
            QtWidgets.QMessageBox.critical(self,'Error',"Some values are not numbers!",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        color = 'blue'
        fig = Figure()
        ax = fig.add_subplot(111)
        if self.CcheckBox.isChecked():
            color = DS.Cc[self.YcomboBox.currentIndex() - 1]
        if (self.autocorrelationadioButton.isChecked()):
            plot_acf(Y,
                     ax=ax,
                     color=color,
                     alpha=int(self.alfaspinBox.value()) / 100)
            ax.set_title('Autocorrelation Plot of ' +
                         self.YcomboBox.currentText())
        elif (self.spectrumradioButton.isChecked()):
            ps = abs(np.fft.fft(Y))**2
            time_step = 1 / int(self.freqSpinBox.value())
            freqs = np.fft.fftfreq(Y.size, time_step)
            idx = np.argsort(freqs)
            freqs = freqs[idx]
            ps = ps[idx]
            red = freqs > 0
            ax.plot(freqs[red], ps[red])
            ax.set_title('Spectral Plot of ' + self.YcomboBox.currentText())
            ax.set_xlabel('Frequency')
            ax.set_ylabel('Spectrum')
        if Nnan:
            ax.annotate('NaN present',
                        xy=(0.05, 0.95),
                        xycoords='axes fraction')
        if self.TcheckBox.isChecked():
            if self.TlineEdit.text():
                ax.set_title(self.TlineEdit.text())
        else:
            ax.set_title('')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.YcomboBox.setCurrentIndex(0)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #19
0
class regplotDlg(QtWidgets.QDialog, Ui_regplot):
    def __init__(self, parent=None):
        super(regplotDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.TcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        fig = Figure()
        ax = fig.add_subplot(111)
        data = DS.Raw.loc[DS.Ir, DS.Ic]
        Gc = DS.Gc[DS.Ic]
        Ts = DS.Ts[DS.Ir]
        X = data.loc[:, -Gc]
        Y = data.loc[:, Gc]
        Yname = Y.columns[0]
        X = X[-Ts]
        Y = Y[-Ts]
        Lr = DS.Lr[DS.Ir]
        Cr = DS.Cr[DS.Ir]
        Lr = Lr[-Ts]
        Cr = Cr[-Ts]
        nr = len(Lr)
        Lc = DS.Lc[DS.Ic]
        Lcx = Lc[-Gc]
        ncx = len(Lcx)
        Lcy = Lc[Gc]
        scoretr = REG.lr.score(X, Y)
        np.random.seed(REG.seed)
        if self.coeffradioButton.isChecked():
            B = REG.lr.coef_
            ind = np.array(range(1, ncx + 1))
            if (ncx > 30):
                itick = np.linspace(0, ncx - 1, 20).astype(int)
                ltick = Lcx[itick]
            else:
                itick = ind
                ltick = Lcx
            vcol = []
            for i in ind:
                if (REG.pS[i] < 0.05): vcol.append('red')
                else: vcol.append('blue')
            ax.bar(ind,
                   B,
                   align='center',
                   color=vcol,
                   label='Score: {:04.2f}'.format(scoretr))
            ax.errorbar(ind, B, yerr=REG.dB[1:], fmt='o', ecolor='green')
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick, rotation='vertical')
            ax.set_xlim([0, ncx + 1])
            ax.set_xlabel('Variables')
            ax.set_ylabel('Coefficients')
            ax.set_title('Regression for ' + Lcy[0] +
                         ' Intercept:  {:10.4f}-{:10.4f}-{:10.4f}'.format(
                             REG.lr.intercept_ -
                             REG.dB[0], REG.lr.intercept_, REG.lr.intercept_ +
                             REG.dB[0]))
            ax.set_ylabel('Coefficients')
            ax.legend(loc='upper left')
        elif self.resradioButton.isChecked():
            Yfit = REG.lr.predict(X)
            Q = np.squeeze(Y.values) - Yfit
            ind = range(1, nr + 1)
            vcol = nr * ['blue']
            if self.CcheckBox.isChecked():
                vcol = Cr
            ax.scatter(ind, Q, alpha=0.3, color=vcol, s=30, marker='o')
            if self.VcheckBox.isChecked():
                for i in range(nr):
                    ax.annotate(Lr[i], (ind[i], Q[i]))
            lim = [Q.min(), 0, Q.max()]
            vlim = max(abs(np.array(lim))) * 1.1
            ax.set_xlim([0, nr + 2])
            ax.set_ylim([np.copysign(vlim, lim[0]), np.copysign(vlim, lim[2])])
            ax.set_title('Residual Plot: Model with for ' + Lcy[0])
            ax.set_xlabel('Point Index')
            ax.set_ylabel('Residuals')
        elif self.cvradioButton.isChecked():
            scores = sk.model_selection.cross_val_score(
                REG.lr, X, Y, cv=self.segmentspinBox.value())
            REG.Ycv = sk.model_selection.cross_val_predict(
                REG.lr, X, Y, cv=self.segmentspinBox.value())
            if self.CcheckBox.isChecked():
                vcol = Cr
            else:
                vcol = 'red'
            ax.scatter(Y, REG.Ycv, color=vcol, alpha=0.3, marker='o')
            if self.VcheckBox.isChecked():
                for i in range(nr):
                    ax.annotate(Lr[i], (Y.values[i], REG.Ycv[i]))
            ax.set_xlabel(Yname + ' measured')
            ax.set_ylabel(Yname + ' CV predicted')
            xmin, xmax = ax.get_xlim()
            ax.set_ylim([xmin, xmax])
            ax.set_title('CV Predicted Plot for ' + Lcy[0] +
                         ' : Score {:04.2f}'.format(scores.mean()))
            ax.add_line(Line2D([xmin, xmax], [xmin, xmax], color='red'))
        elif self.dispersionradioButton.isChecked():
            fig = plt.figure()
            ax = fig.add_subplot(111)
            cax = ax.matshow(REG.dism)
            fig.colorbar(cax, format='%.2E')
            ax.set_title('Trace = {:10.4E}'.format(np.trace(REG.dism)))
        elif self.leverageradioButton.isChecked():
            Ftable = surtabDlg.launch(None)
            if len(np.shape(Ftable)) == 0: return ()
            if np.argmax(Ftable['X axis'].values) == np.argmax(
                    Ftable['Y axis'].values):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', "Two variables on the same axis",
                    QtWidgets.QMessageBox.Ok)
                return ()
            fig = plt.figure()
            ax = fig.add_subplot(111)
            npts = 20
            xname = Ftable[(Ftable['X axis'] == True).values].index[0]
            yname = Ftable[(Ftable['Y axis'] == True).values].index[0]
            cname = Ftable[(Ftable['Constant'] == True).values].index.tolist()
            cvalue = Ftable.loc[(Ftable['Constant'] == True).values, 'value']
            x = np.linspace(float(Ftable['min'][xname]),
                            float(Ftable['max'][xname]), npts)
            y = np.linspace(float(Ftable['min'][yname]),
                            float(Ftable['max'][yname]), npts)
            px = []
            py = []
            for i in range(npts):
                for j in range(npts):
                    px.append(x[i])
                    py.append(y[j])
            mx = pd.DataFrame({xname: px, yname: py})
            xtitle = ''
            for i in range(len(cname)):
                xtitle = xtitle + cname[i] + ' = ' + str(
                    cvalue.values.tolist()[i])
                mx[cname[i]] = np.ones(npts**2) * float(cvalue[i])
            mx = mx[Lcx]
            pz = np.diag(np.dot(np.dot(mx, REG.dism), mx.T))
            px = np.array(px)
            py = np.array(py)
            pz = np.array(pz)
            z = plt.mlab.griddata(px, py, pz, x, y, interp='linear')
            plt.contour(x, y, z, 15, linewidths=0.5, colors='k')
            plt.contourf(x, y, z, 15, cmap=plt.cm.rainbow)
            plt.colorbar()
            ax.set_xlabel(xname)
            ax.set_ylabel(yname)
            ax.set_title(xtitle)
            ax.set_xlim([px.min(), px.max()])
            ax.set_ylim([py.min(), py.max()])
        elif self.surfaceradioButton.isChecked():
            Ftable = surtabDlg.launch(None)
            if len(np.shape(Ftable)) == 0: return ()
            if np.argmax(Ftable['X axis'].values) == np.argmax(
                    Ftable['Y axis'].values):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', "Two variables on the same axis",
                    QtWidgets.QMessageBox.Ok)
                return ()
            fig = plt.figure()
            ax = fig.add_subplot(111)
            npts = 100
            xname = Ftable[(Ftable['X axis'] == True).values].index[0]
            yname = Ftable[(Ftable['Y axis'] == True).values].index[0]
            cname = Ftable[(Ftable['Constant'] == True).values].index.tolist()
            cvalue = Ftable.loc[(Ftable['Constant'] == True).values, 'value']
            x = np.linspace(float(Ftable['min'][xname]),
                            float(Ftable['max'][xname]), npts)
            y = np.linspace(float(Ftable['min'][yname]),
                            float(Ftable['max'][yname]), npts)
            px = []
            py = []
            for i in range(npts):
                for j in range(npts):
                    px.append(x[i])
                    py.append(y[j])
            mx = pd.DataFrame({xname: px, yname: py})
            xtitle = ''
            for i in range(len(cname)):
                xtitle = xtitle + cname[i] + ' = ' + str(
                    cvalue.values.tolist()[i])
                mx[cname[i]] = np.ones(npts**2) * float(cvalue[i])
            mx = mx[Lcx]
            pz = REG.lr.predict(mx)
            px = np.array(px)
            py = np.array(py)
            pz = np.array(pz)
            z = plt.mlab.griddata(px, py, pz, x, y, interp='linear')
            plt.contour(x, y, z, 15, linewidths=0.5, colors='k')
            plt.contourf(x, y, z, 15, cmap=plt.cm.rainbow)
            plt.colorbar()
            ax.set_xlabel(xname)
            ax.set_ylabel(yname)
            ax.set_title(xtitle)
            ax.set_xlim([px.min(), px.max()])
            ax.set_ylim([py.min(), py.max()])
        if self.TcheckBox.isChecked():
            if self.TlineEdit.text():
                ax.set_title(self.TlineEdit.text())
        else:
            ax.set_title('')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        try:
            self.rmmpl()
        except:
            pass
        self.addmpl(fig)

    def reset(self):
        self.TcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #20
0
class trivariateplotDlg(QtWidgets.QDialog, Ui_trivariateplot):
    def __init__(self, parent=None):
        super(trivariateplotDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.XcomboBox.addItems(DS.Lc[DS.Ic])
        self.YcomboBox.addItems(DS.Lc[DS.Ic])
        self.ZcomboBox.addItems(DS.Lc[DS.Ic])
        self.VcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.ZcomboBox.setCurrentIndex(0)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.ZlineEdit.setText('')
        self.TlineEdit.setText('')
        self.VcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.update()

    def redraw(self):
        def annotate3D(ax, s, *args, **kwargs):
            '''add anotation text s to to Axes3d ax'''
            tag = Annotation3D(s, *args, **kwargs)
            ax.add_artist(tag)
        if self.XcomboBox.currentText()==self.YcomboBox.currentText() or \
           self.YcomboBox.currentText()==self.ZcomboBox.currentText() or \
           self.YcomboBox.currentText()==self.ZcomboBox.currentText():
            QtWidgets.QMessageBox.critical(self,'Error',"Variables must be different",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        data = data.assign(Lr=DS.Lr[DS.Ir])
        data = data.assign(Cr=DS.Cr[DS.Ir])
        data=data[[self.XcomboBox.currentText(),self.YcomboBox.currentText(), \
                   self.ZcomboBox.currentText(),'Lr','Cr']]
        Nnan = data.isnull().isnull().all().all()
        data = data.dropna()
        Lr = data['Lr'].values
        Cr = data['Cr'].values
        data = data.drop('Lr', axis=1)
        data = data.drop('Cr', axis=1)
        if data.dtypes.all() == 'float' and data.dtypes.all() == 'int':
            QtWidgets.QMessageBox.critical(self,'Error',"Some values are not numbers!",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        x = data[self.XcomboBox.currentText()].values
        y = data[self.YcomboBox.currentText()].values
        z = data[self.ZcomboBox.currentText()].values
        npts = int(self.spinBox.value())
        color_point = 'blue'
        if self.CcheckBox.isChecked():
            color_point = Cr
        if self.scatterradioButton.isChecked():
            fig = plt.figure()
            ax = fig.gca(projection='3d')
            ax.scatter(x, y, z, c=color_point, marker='o')
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.YcomboBox.currentText())
            ax.set_zlabel(self.ZcomboBox.currentText())
            ax.view_init(elev=self.elevationspinBox.value(),
                         azim=self.azimutspinBox.value())
            ax.dist = self.distancespinBox.value()
            if self.VcheckBox.isChecked():
                xyzn = zip(x, y, z)
                for j, xyz_ in enumerate(xyzn):
                    annotate3D(ax,
                               s=Lr[j],
                               xyz=xyz_,
                               fontsize=10,
                               xytext=(-3, 3),
                               textcoords='offset points',
                               ha='right',
                               va='bottom')
        if self.contouradioButton.isChecked():
            fig = plt.figure()
            ax = fig.add_subplot(111)
            xi = np.linspace(x.min(), x.max(), npts)
            yi = np.linspace(y.min(), y.max(), npts)
            zi = plt.mlab.griddata(x, y, z, xi, yi, interp='linear')
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.YcomboBox.currentText())
            ax.set_title(self.ZcomboBox.currentText() +
                         ' : griddata test (%d points)' % npts)
            plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
            plt.contourf(xi, yi, zi, 15, cmap=plt.cm.rainbow)
            plt.colorbar()
            ax.scatter(x, y, marker='o', c=color_point, s=7, zorder=10)
            ax.set_xlim(x.min(), x.max())
            ax.set_ylim(y.min(), y.max())
        if self.surfaceradioButton.isChecked():
            fig = plt.figure()
            ax = fig.gca(projection='3d')
            xi = np.linspace(x.min(), x.max(), npts)
            yi = np.linspace(y.min(), y.max(), npts)
            zi = plt.mlab.griddata(x, y, z, xi, yi, interp='linear')
            xi, yi = np.meshgrid(xi, yi)
            ax.view_init(elev=self.elevationspinBox.value(),
                         azim=self.azimutspinBox.value())
            surf = ax.plot_surface(xi,
                                   yi,
                                   zi,
                                   rstride=10,
                                   cstride=10,
                                   cmap=cm.rainbow,
                                   linewidth=0,
                                   antialiased=False)
            surf.set_clim(vmin=zi.min(), vmax=zi.max())
            ax.set_zlim3d(zi.min(), zi.max())
            fig.colorbar(surf,
                         orientation='vertical',
                         shrink=0.5,
                         format='%.2f')
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.YcomboBox.currentText())
            ax.set_zlabel(self.ZcomboBox.currentText())
        if Nnan:
            ax.annotate('{:04.2f} NaN'.format(Nnan),
                        xy=(0.80, 0.95),
                        xycoords='figure fraction')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #21
0
class pcaplotDlg(QtWidgets.QDialog, Ui_pcaplotDialog):
    def __init__(self, parent=None):
        super(pcaplotDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        for x in range(PCA.ncp):
            self.XcomboBox.addItem(str(x + 1))
            self.YcomboBox.addItem(str(x + 1))
        Lr = DS.Lr[DS.Ir]
        Ts = DS.Ts[DS.Ir]
        Lr = Lr[-Ts]
        for x in Lr:
            self.objcomboBox.addItem(str(x))
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(1)
        self.VcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        fig = Figure()
        ax = fig.add_subplot(111)
        c1 = self.XcomboBox.currentIndex()
        c2 = self.YcomboBox.currentIndex()
        Lc = DS.Lc[DS.Ic]
        Lr = DS.Lr[DS.Ir]
        Ts = DS.Ts[DS.Ir]
        Cr = DS.Cr[DS.Ir]
        Lr = Lr[-Ts]
        Cr = Cr[-Ts]
        nr = len(Lr)
        nc = len(Lc)
        if (self.varianceradioButton.isChecked()):
            ind = range(1, len(PCA.rv) + 1)
            ax.bar(ind, PCA.rv, align='center')
            ax.set_xticks(ind)
            ax.set_xticklabels(ind, rotation='vertical')
            ax.set_xlabel('Component Number')
            ax.yaxis.grid()
            ax.set_title('Explained Variance')
        elif self.variancecumradioButton.isChecked():
            rvc = PCA.rv.cumsum()
            ind = range(1, len(rvc) + 1)
            ax.bar(ind, rvc, align='center')
            ax.set_xticks(ind)
            ax.set_xticklabels(ind, rotation='vertical')
            ax.set_xlabel('Component Number')
            ax.yaxis.grid()
            ax.set_title('Cumulative Explained Variance')
        elif self.loadingradioButton.isChecked():
            color = ['red'] * nc
            if self.CcheckBox.isChecked():
                color = DS.Cc[DS.Ic]
            rv = PCA.rv * 100
            llim = [PCA.lo.min(), 0, PCA.lo.max()]
            vllim = 1.1 * max(abs(np.array(llim)))
            ax.scatter(PCA.lo[:, c1],
                       PCA.lo[:, c2],
                       color='k',
                       alpha=0.3,
                       marker='.')
            ax.scatter(0, 0, color='red', s=60, marker='+')
            for i, txt in enumerate(Lc):
                ax.arrow(0,
                         0,
                         PCA.lo[:, c1][i],
                         PCA.lo[:, c2][i],
                         fc="r",
                         ec="r",
                         head_width=0.02,
                         head_length=0.03,
                         color=color[i])
                ax.annotate(txt, (PCA.lo[:, c1][i], PCA.lo[:, c2][i]))
            ax.set_xlim(
                [math.copysign(vllim, llim[0]),
                 math.copysign(vllim, llim[2])])
            ax.set_ylim(
                [math.copysign(vllim, llim[0]),
                 math.copysign(vllim, llim[2])])
            ax.set_title('Loading Plot (' + str(round(rv[c1] + rv[c2], 2)) +
                         '%)')
            ax.set_xlabel('Comp.' + self.XcomboBox.currentText() + ' (' +
                          str(round(rv[c1], 2)) + '%)')
            ax.set_ylabel('Comp.' + self.YcomboBox.currentText() + ' (' +
                          str(round(rv[c2], 2)) + '%)')
        elif self.scoreradioButton.isChecked():
            vcol = ['red'] * nr
            if self.CcheckBox.isChecked():
                vcol = Cr
            rv = PCA.rv * 100
            slim = [PCA.sco.min(), 0, PCA.sco.max()]
            vslim = 1.2 * max(abs(np.array(slim)))
            ax.scatter(PCA.sco[:, c1],
                       PCA.sco[:, c2],
                       alpha=0.3,
                       color=vcol,
                       s=30,
                       marker='o')
            ax.scatter(0, 0, color='red', s=60, marker='+')
            if self.VcheckBox.isChecked():
                for i, txt in enumerate(Lr):
                    ax.annotate(txt, (PCA.sco[:, c1][i], PCA.sco[:, c2][i]))
            ax.set_xlim(
                [math.copysign(vslim, slim[0]),
                 math.copysign(vslim, slim[2])])
            ax.set_ylim(
                [math.copysign(vslim, slim[0]),
                 math.copysign(vslim, slim[2])])
            ax.set_title('Score Plot (' + str(round(rv[c1] + rv[c2], 2)) +
                         '%)')
            ax.set_xlabel('Comp.' + self.XcomboBox.currentText() + ' (' +
                          str(round(rv[c1], 2)) + '%)')
            ax.set_ylabel('Comp.' + self.YcomboBox.currentText() + ' (' +
                          str(round(rv[c2], 2)) + '%)')
        elif self.ellipseradioButton.isChecked():
            st2 = PCA.rv * nc
            alpha = (100 - self.alphaspinBox.value()) / 100
            fn = np.array(range(1, (PCA.ncp + 1)))
            fd = (nr + 1) - fn
            fn = tuple(fn)
            fd = tuple(fd)
            falpha = sp.stats.f.ppf(alpha, fn, fd)
            color = []
            txt = []
            for i in range(nr):
                txt.append('')
                color.append('blue')
                rp = (PCA.sco[:, c1][i])**2 / st2[c1] + (
                    PCA.sco[:, c2][i])**2 / st2[c2]
                if (rp > falpha[PCA.ncp - 1]):
                    color[i] = 'red'
                    txt[i] = Lr[i]
            slim = [PCA.sco.min(), 0, PCA.sco.max()]
            vslim = 1.2 * max(abs(np.array(slim)))
            ax.scatter(PCA.sco[:, c1],
                       PCA.sco[:, c2],
                       alpha=0.3,
                       color=color,
                       s=30,
                       marker='o')
            ax.scatter(0, 0, color='red', s=60, marker='+')
            for i, label in enumerate(txt):
                ax.annotate(label, (PCA.sco[:, c1][i], PCA.sco[:, c2][i]))
            ax.set_xlim(
                [math.copysign(vslim, slim[0]),
                 math.copysign(vslim, slim[2])])
            ax.set_ylim(
                [math.copysign(vslim, slim[0]),
                 math.copysign(vslim, slim[2])])
            ax.set_title('Score Plot (' +
                         str(round(PCA.rv[c1] + PCA.rv[c2], 2)) + '%)')
            ax.set_xlabel('Comp.' + self.XcomboBox.currentText() + ' (' +
                          str(round(PCA.rv[c1], 2)) + '%)')
            ax.set_ylabel('Comp.' + self.YcomboBox.currentText() + ' (' +
                          str(round(PCA.rv[c2], 2)) + '%)')
            ella = Ellipse(xy=(0, 0),
                           width=2 * math.sqrt(falpha[PCA.ncp - 1] * st2[c1]),
                           height=2 * math.sqrt(falpha[PCA.ncp - 1] * st2[c2]),
                           color='red')
            ella.set_facecolor('none')
            ax.add_artist(ella)
            alpha = alpha * 100
            ax.annotate(str(round(alpha, 0)) + '%',
                        xy=(0, -math.sqrt(falpha[PCA.ncp - 1] * st2[c2])),
                        color='red')
        elif self.biplotradioButton.isChecked():
            xs = PCA.sco[:, c1]
            ys = PCA.sco[:, c2]
            if self.VcheckBox.isChecked():
                for i, (t, x, y) in enumerate(zip(Lr, xs, ys)):
                    ax.text(x, y, t, color=Cr[i], ha='center', va='center')
                xmin, xmax = xs.min(), xs.max()
                ymin, ymax = ys.min(), ys.max()
                xpad = (xmax - xmin) * 0.1
                ypad = (ymax - ymin) * 0.1
                ax.set_xlim(xmin - xpad, xmax + xpad)
                ax.set_ylim(ymin - ypad, ymax + ypad)
            else:
                ax.scatter(xs, ys, c=Cr, marker='o')
            for i, col in enumerate(Lc):
                x, y = PCA.lo[i, c1], PCA.lo[i, c2]
                ax.arrow(0, 0, x, y, color='r', width=0.002, head_width=0.05)
                ax.text(x * 1.4,
                        y * 1.4,
                        col,
                        color='r',
                        ha='center',
                        va='center')
            ax.set_xlabel('PC{}'.format(c1 + 1))
            ax.set_ylabel('PC{}'.format(c2 + 1))
        elif self.ht2radioButton.isChecked():
            ind = np.array(range(1, nr + 1))
            colors = []
            for i in range(nr):
                colors.append('blue')
                if (PCA.ht2[i] > PCA.T95):
                    colors[i] = 'red'
            if (nr > 30):
                itick = np.linspace(0, nr - 1, 20).astype(int)
                ltick = Lr[itick]
            else:
                itick = ind
                ltick = Lr
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick, rotation='vertical')
            ax.set_xlabel('Object')
            ax.bar(ind, PCA.ht2, align='center', color=colors)
            ax.yaxis.grid()
            ax.set_title('Hoteling T2 for ' + str(PCA.ncp) +
                         ' components model')
            ax.axhline(y=PCA.T95, color='red', linewidth=1.5, zorder=0)
            ax.annotate('95%', xy=(0, PCA.T95), color='red')
            ax.axhline(y=PCA.T99, color='red', linewidth=1.5, zorder=0)
            ax.annotate('99%', xy=(0, PCA.T99), color='red')
            ax.set_ylim([0, 1.2 * max([PCA.T99, max(PCA.ht2)])])
            ax.set_xlim([0, nr + 1])
        elif self.speradioButton.isChecked():
            ind = np.array(range(1, nr + 1))
            colors = []
            for i in range(nr):
                colors.append('blue')
                if (PCA.Q[i] > PCA.Q95):
                    colors[i] = 'red'
            if (nr > 30):
                itick = np.linspace(0, nr - 1, 20).astype(int)
                ltick = Lr[itick]
            else:
                itick = ind
                ltick = Lr
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick, rotation='vertical', fontsize=8)
            ax.set_xlabel('Object')
            ax.scatter(ind, PCA.Q, color=colors)
            ax.set_ylabel('Q-SPE')
            ax.yaxis.grid()
            ax.set_title('Q-SPE for ' + str(PCA.ncp) + ' components model')
            ax.axhline(y=PCA.Q95, color='red', linewidth=1.5, zorder=0)
            ax.annotate('95%', xy=(0, PCA.Q95), color='red')
            ax.axhline(y=PCA.Q99, color='red', linewidth=1.5, zorder=0)
            ax.annotate('99%', xy=(0, PCA.Q99), color='red')
            ax.set_ylim([0, 1.2 * max([PCA.Q99, max(PCA.Q)])])
        elif self.columnresradioButton.isChecked():
            ind = np.array(range(1, nc + 1))
            if (nc > 30):
                itick = np.linspace(0, nc - 1, 20).astype(int)
                ltick = Lc[itick]
            else:
                itick = ind
                ltick = Lc
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick, rotation='vertical')
            ax.set_xlabel('Variable')
            ax.bar(ind, PCA.cres, align='center', width=0.8)
            ax.set_ylabel('Column R2 over all components')
            ax.yaxis.grid()
            ax.set_title('Column Residual Plot for ' + str(PCA.ncp) +
                         ' components space')
            ax.set_ylim([0, 1])
            ax.set_xlim([0, nc + 1])
        elif self.specontribradioButton.isChecked():
            obj = self.objcomboBox.currentIndex()
            res = PCA.res[obj, :]
            ind = np.array(range(1, nc + 1))
            if (nc > 30):
                itick = np.linspace(0, nc - 1, 20).astype(int)
                ltick = Lc[itick]
            else:
                itick = ind
                ltick = Lc
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick, rotation='vertical')
            ax.set_xlabel('Variable')
            ax.bar(ind, res, align='center', width=0.8)
            ax.set_ylabel('Variable Contributon on SPE')
            ax.yaxis.grid()
            ax.set_title('Residual Plot for object ' +
                         self.objcomboBox.currentText())
            ax.set_xlim([0, nc + 1])
        elif self.cvradioButton.isChecked():
            Xc = pd.DataFrame(PCA.Xc)
            np.random.seed(nr)
            kf = sk.model_selection.KFold(n_splits=self.segmentspinBox.value(),
                                          shuffle=True)
            Xcv = pd.DataFrame(0, index=range(nr), columns=range(nc))
            for train, test in kf.split(Xc):
                Xt = Xc.iloc[train, :]
                T, P, LAM, SSX, RV, X, SPE, SSE, R2X, HT2, Res, Xm, Xstd, MQ, MT, Q95, T95, Q99, T99 = pca_model(
                    Xt, PCA.ncp, False, False, False)
                X_test = Xc.iloc[test, :]
                T_test = np.dot(X_test, P)
                Xcv.iloc[test, :] = np.dot(T_test, P.T)
            PCA.Xcv = Xcv
            Xcv = np.array(Xcv)
            Xc = np.array(Xc)
            for i in range(nc):
                ax.scatter(Xc[:, i],
                           Xcv[:, i],
                           color=(0, i / float(nc), 0, 1),
                           alpha=0.3,
                           marker='o')
            if self.VcheckBox.isChecked():
                for i in range(nc):
                    for j, txt in enumerate(Lr):
                        ax.annotate(txt, (Xc[j, i], Xcv[j, i]))
            ax.set_xlabel('Original Data')
            ax.set_ylabel('CV Predicted Data')
            ax.xaxis.grid()
            ax.yaxis.grid()
            ax.set_ylim([-3, 3])
            ax.set_xlim([-3, 3])
            ax.set_title('Predicted Data vs. Original')
            ax.add_line(Line2D([-3, 3], [-3, 3], color='red'))
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(1)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #22
0
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, ):  #provide values for attributes at runtime
        super(Main, self).__init__()
        self.setupUi(self)
        self.pushBrowse.clicked.connect(self.selectFile)
        self.pushBrowse_2.clicked.connect(self.selectmlFile)
        self.pushApply.clicked.connect(self.apply)
        self.pushRun.clicked.connect(self.run)
        self.HomeDirectory = os.getcwd()  #saves the primary working directory
        self.directory = os.listdir(self.HomeDirectory)
        self.saveBttn.clicked.connect(self.file_save)
        self.actionOpen.triggered.connect(self.file_open)
        self.actionReset.triggered.connect(self.plots_close)
        self.message = 0
        self.UI = []
        self.reset = 0
        self.plotinfo = []
        self.plot2info = []
        self.plot3info = []
        self.plot4info = []
        self.plot5info = []
        self.plot6info = []

        #check User input for correct .csv file
        self.inputFile.setValidator(validator)
        self.inputFile.textChanged.connect(self.check_state)
        self.inputFile.textChanged.emit(self.inputFile.text())
        #check User input for correct .csv file for ml
        self.mlData.setValidator(validator)
        self.mlData.textChanged.connect(self.check_state)
        self.mlData.textChanged.emit(self.mlData.text())
        #shaft speed
        self.shaftSpeed.setValidator(regexp_checkdouble)
        self.shaftSpeed.textChanged.connect(self.check_state)
        self.shaftSpeed.textChanged.emit(self.shaftSpeed.text())
        #Num of rolling elements
        self.numberofElements.setValidator(regexp_checkint)
        self.numberofElements.textChanged.connect(self.check_state)
        self.numberofElements.textChanged.emit(self.numberofElements.text())
        #diameter of rolling elements
        self.diameterofElements.setValidator(regexp_checkdouble)
        self.diameterofElements.textChanged.connect(self.check_state)
        self.diameterofElements.textChanged.emit(
            self.diameterofElements.text())
        #pitch diameter
        self.pitchDiameter.setValidator(regexp_checkdouble)
        self.pitchDiameter.textChanged.connect(self.check_state)
        self.pitchDiameter.textChanged.emit(self.pitchDiameter.text())
        #Contact angle
        self.contactAngle.setValidator(regexp_checkdouble)
        self.contactAngle.textChanged.connect(self.check_state)
        self.contactAngle.textChanged.emit(self.contactAngle.text())
        #Frequency
        self.samFreq.setValidator(regexp_checkdouble)
        self.samFreq.textChanged.connect(self.check_state)
        self.samFreq.textChanged.emit(self.samFreq.text())

    def check_state(
        self, *args, **kwargs
    ):  #this function is changes the color of the lineedit fields depending on state
        sender = self.sender()
        validator = sender.validator()
        state = validator.validate(sender.text(), 0)[0]
        if state == QValidator.Acceptable:
            color = '#c4df9b'  # green
        elif state == QtGui.QValidator.Intermediate:
            color = '#fff79a'  # yellow
        else:
            color = '#f6989d'  # red
        sender.setStyleSheet('QLineEdit { background-color: %s }' % color)

    #creates a dictionary from the saved CSV
    def file_open(
        self
    ):  #function called when the open file action in triggered. Creates a dictionary from a CSV file.
        filename = QFileDialog.getOpenFileName()[0]
        reader = csv.reader(open(filename, 'r'))
        d = {}
        for row in reader:
            k, v = row
            d[k] = v

        print(d)

        self.setTextInfile(d)
        return True

    #used the dicitonary created above to assign saved variables to input parameters
    def setTextInfile(self, d):
        self.inputName.setText(d['inputName'])
        self.inputApplication.setText(d['inputApplication'])
        self.inputModelnum.setText(d['inputModelnum'])
        self.inputSavingalias.setText(d['inputSavingalias'])
        self.inputFile.setText(d['inputFile'])
        self.mlData.setText(d['mlData'])
        self.horsepower.setText(d['horsepower'])
        self.voltage.setText(d['voltage'])
        self.phase.setText(d['phase'])
        self.shaftnum.setText(d['shaftnum'])
        self.shaftSpeed.setText(d['shaftSpeed'])
        self.numberofElements.setText(d['numberofElements'])
        self.diameterofElements.setText(d['diameterofElements'])
        self.pitchDiameter.setText(d['pitchDiameter'])
        self.contactAngle.setText(d['contactAngle'])
        self.samFreq.setText(d['samFreq'])

    """
    Hmm i wonder if this is used to save the file?
    """

    def file_save(
        self,
    ):  #called when the save btn is clicked. converts user input to dictionary then to dataframe then to csv file.
        dict = CreateSaveDictionary(self.inputName.text(),
                                    self.inputApplication.text(),
                                    self.inputModelnum.text(),
                                    self.inputSavingalias.text(),
                                    self.inputFile.text(), self.mlData.text(),
                                    self.horsepower.text(),
                                    self.voltage.text(), self.phase.text(),
                                    self.shaftnum.text(),
                                    self.shaftSpeed.text(),
                                    self.numberofElements.text(),
                                    self.diameterofElements.text(),
                                    self.pitchDiameter.text(),
                                    self.contactAngle.text(),
                                    self.samFreq.text())
        CreateCSVfromDict(dict)

    """
    These functions create the figure widgets and toolbars
    """

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.spectrumUI.addWidget(self.canvas)
        #self.canvas.draw()
        #self.toolbar = NavigationToolbar(self.canvas,self.mainspectrum, coordinates=True)
        #self.toolbar.setParent(None)
        #self.spectrumUI.addWidget(self.toolbar)

    def addgraph11(self, fig):
        self.canvas1 = FigureCanvas(fig)
        self.graph11UI.addWidget(self.canvas1)
        #self.canvas1.draw()
        self.toolbar1 = NavigationToolbar(self.canvas1,
                                          self.graph11,
                                          coordinates=True)
        self.graph11UI.addWidget(self.toolbar1)

    def addgraph12(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.graph12UI.addWidget(self.canvas2)
        #self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.graph12,
                                          coordinates=True)
        self.graph12UI.addWidget(self.toolbar2)

    def addgraph13(self, fig):
        self.canvas5 = FigureCanvas(fig)
        self.graph13UI.addWidget(self.canvas5)
        #self.canvas2.draw()
        self.toolbar5 = NavigationToolbar(self.canvas5,
                                          self.graph13,
                                          coordinates=True)
        self.graph13UI.addWidget(self.toolbar5)

    def addgraph21(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.graph21UI.addWidget(self.canvas3)
        #self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.graph21,
                                          coordinates=True)
        self.graph21UI.addWidget(self.toolbar3)

    def addgraph22(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.graph22UI.addWidget(self.canvas4)
        #self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.graph22,
                                          coordinates=True)
        self.graph22UI.addWidget(self.toolbar4)

    #clearly selects file
    def selectFile(self, ):
        self.inputFile.setText(QFileDialog.getOpenFileName()[0])
        self.inputfile = self.inputFile.text()

    #selects file but for a ml data
    def selectmlFile(self, ):
        self.mlData.setText(QFileDialog.getOpenFileName()[0])

    """
    Apply checks user inputs and then assigns them to function parameter variables
    In case the user doesn't supply input for a specific field, default inputs will
    be inserted.
    """

    def apply(self, ):
        if self.inputSavingalias.text() != "":
            self.savingalias = self.inputSavingalias.text() + ".csv"
            self.inputSavingalias.setText(self.savingalias)

        if self.inputFile.text() != "":
            try:
                temp = self.inputFile.text()
                self.FileOfInterest = self.inputFile.text()
            except:
                print("pu")
        else:
            print("no input file selected, using demo file.")
            self.FileOfInterest = "AccelerometerActualData.csv"
            self.inputFile.setText("/AccelerometerActualData.csv")

        if self.mlData.text() != "":
            self.TrainingDataFile = self.mlData.text()
            print(self.TrainingDataFile)
        else:
            print("no ML data selected")
            self.TrainingDataFile = "NoNegatives.csv"  #default file location
            self.mlData.setText("/NoNegatives.csv")

        if self.shaftSpeed.text() != "":
            self.n = float(self.shaftSpeed.text())
            print("type n =", type(self.n))
        else:
            print("no shaft speed selected")
            self.n = 2000 / 60
            self.shaftSpeed.setText(str(self.n))

        if self.numberofElements.text() != "":
            self.N = int(self.numberofElements.text())
        else:
            print("Number of elements not specified")
            self.N = 16
            self.numberofElements.setText(str(self.N))

        if self.diameterofElements.text() != "":
            self.Bd = float(self.diameterofElements.text())
        else:
            print("Diameter of elements not specified")
            self.Bd = 0.331 * 254
            self.diameterofElements.setText(str(self.Bd))

        if self.pitchDiameter.text() != "":
            self.Pd = float(self.pitchDiameter.text())
        else:
            print("no pitch diameter specified")
            self.Pd = Pd = 2.815 * 254
            self.pitchDiameter.setText(str(self.Pd))

        if self.contactAngle.text() != "":
            self.phi = float(self.contactAngle.text())
        else:
            print("Contact angle not specified")
            self.phi = (15.17 * 3.14159) / 180
            self.contactAngle.setText(str(self.phi))

        if self.samFreq.text() != "":
            self.SampleFrequency = float(self.samFreq.text())

        else:
            print("no sample frequency specified")
            self.SampleFrequency = 20000
            self.samFreq.setText(str(self.SampleFrequency))

        self.popup = MyPopup("Applied")
        self.popup.setGeometry(QtCore.QRect(100, 100, 400, 200))
        self.popup.show()

##############################################

    def getPlot(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub0.cla()
        self.sub0.plot(X, Y, c=np.random.rand(3, ))
        self.sub0.set_xlabel(xlabel, fontsize=12)
        self.sub0.set_ylabel(ylabel, fontsize=12)
        self.sub0.set_title(Title)
        self.sub0.grid(True)
        if self.reset != 0:
            self.canvas.draw()

        return True

    def getPlot1(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub1.cla()
        self.sub1.plot(X, Y, c=np.random.rand(3, ))
        self.sub1.set_xlabel(xlabel, fontsize=12)
        self.sub1.set_ylabel(ylabel, fontsize=12)
        self.sub1.set_title(Title)
        self.sub1.grid(True)
        if self.reset != 0:
            self.canvas1.draw()

        return True

    def getPlot2(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub2.cla()
        self.sub2.plot(X, Y, c=np.random.rand(3, ))
        self.sub2.set_xlabel(xlabel, fontsize=12)
        self.sub2.set_ylabel(ylabel, fontsize=12)
        self.sub2.set_title(Title)
        self.sub2.grid(True)
        if self.reset != 0:
            self.canvas2.draw()

        return True

    def getPlot3(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub3.cla()
        self.sub3.plot(X, Y, c=np.random.rand(3, ))
        self.sub3.set_xlabel(xlabel, fontsize=12)
        self.sub3.set_ylabel(ylabel, fontsize=12)
        self.sub3.set_title(Title)
        self.sub3.grid(True)
        if self.reset != 0:
            self.canvas3.draw()

        return True

    def getPlot4(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub4.cla()
        self.sub4.plot(X, Y, c=np.random.rand(3, ))
        self.sub4.set_xlabel(xlabel, fontsize=12)
        self.sub4.set_ylabel(ylabel, fontsize=12)
        self.sub4.set_title(Title)
        self.sub4.grid(True)
        if self.reset != 0:
            self.canvas4.draw()

        return True

    def getPlot5(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub4.cla()
        self.sub5.plot(X, Y, c=np.random.rand(3, ))
        self.sub5.set_xlabel(xlabel, fontsize=12)
        self.sub5.set_ylabel(ylabel, fontsize=12)
        self.sub5.set_title(Title)
        self.sub5.grid(True)
        if self.reset != 0:
            self.canvas5.draw()

        return True

    def run(self, ):  #called when run is clicked

        if self.reset == 0:
            #instantiate the figures
            self.fig0 = plt.figure()
            self.sub0 = self.fig0.add_subplot()

            self.fig1 = plt.figure()
            self.sub1 = self.fig1.add_subplot()

            self.fig2 = plt.figure()
            self.sub2 = self.fig2.add_subplot()

            self.fig3 = plt.figure()
            self.sub3 = self.fig3.add_subplot()

            self.fig4 = plt.figure()
            self.sub4 = self.fig4.add_subplot()

            self.fig5 = plt.figure()
            self.sub5 = self.fig5.add_subplot()

        #begin calling ml functions for processing
        Data = getValuesFromRawData(self.FileOfInterest)
        UserInput = UserInputs2WorkingForm(
            self.n, self.N, self.Bd, self.Pd, self.phi, self.SampleFrequency,
            Data, self.HomeDirectory, self.directory, self.TrainingDataFile,
            self.inputSavingalias, self.inputName, self.inputModelnum)
        self.UI = UserInput
        BearingInfo = BearingInfomation(UserInput)
        X_train, X_test, Y_train, Y_test = GetSplitTrainingData(UserInput)
        classifier = TrainModel(X_train, Y_train)
        Y_test_pred = PredictModel(classifier, X_test)
        ##
        #Create time series array
        t = np.arange(0, UserInput['Time of Sampling'],
                      1 / UserInput['Sampling Frequency'])

        #Perform FFT, PSD, Correlation, DC Offset
        UserInput1 = RemoveDCOffset(UserInput)
        x1 = FourierTransform(UserInput1)
        x2 = get_psd_values(UserInput1)
        x3 = get_autocorr_values(UserInput1)

        TEST = getTESTDataFrame(UserInput)
        TEST1 = TEST.values[:, 0:(TEST.shape[1] - 1)]
        OUTCOME = PredictModel(classifier, TEST1)
        plt.close('all')
        figs = []
        #figs = getGraphs(UserInput)
        self.plotinfo, self.plot2info, self.plot3info, self.plot4info, self.plot5info, self.plot6info = getGraphs(
            UserInput)

        self.getPlot(self.plotinfo[0], self.plotinfo[1], self.plotinfo[2],
                     self.plotinfo[3], self.plotinfo[4])
        self.getPlot1(self.plot2info[0], self.plot2info[1], self.plot2info[2],
                      self.plot2info[3], self.plot2info[4])
        self.getPlot2(self.plot3info[0], self.plot3info[1], self.plot3info[2],
                      self.plot3info[3], self.plot3info[4])
        self.getPlot3(self.plot4info[0], self.plot4info[1], self.plot4info[2],
                      self.plot4info[3], self.plot4info[4])
        self.getPlot4(self.plot5info[0], self.plot5info[1], self.plot5info[2],
                      self.plot5info[3], self.plot5info[4])
        self.getPlot5(self.plot6info[0], self.plot6info[1], self.plot6info[2],
                      self.plot6info[3], self.plot6info[4])

        Prediction = PredictProbModel(classifier, X_test)
        print("outcome:")
        print(OUTCOME)
        print("Accuracy on training set is : {}".format(
            classifier.score(X_train, Y_train)))
        print("Accuracy on test set is : {}".format(
            classifier.score(X_test, Y_test)))

        if self.reset == 0:
            self.addmpl(self.fig0)
            self.addgraph11(self.fig1)
            self.addgraph12(self.fig2)
            self.addgraph21(self.fig3)
            self.addgraph22(self.fig4)

        self.BSF.setText(str(truncate(BearingInfo['BSF'], 3)))
        self.BPFI.setText(str(truncate(BearingInfo['BPFI'], 3)))
        self.BPFO.setText(str(truncate(BearingInfo['BPFO'], 3)))
        self.FTF.setText(str(truncate(BearingInfo['FTF'], 3)))
        self.earlyEdit.setText(str(Prediction[0, 0]))
        self.suspectEdit.setText(str(Prediction[0, 1]))
        self.normalEdit.setText(str(Prediction[0, 2]))
        self.immEdit.setText(str(Prediction[0, 3]))
        self.innerEdit.setText(str(Prediction[0, 4]))
        self.rollingEdit.setText(str(Prediction[0, 5]))
        self.stageEdit.setText(str(Prediction[0, 6]))
        self.reset = 1

    def close_application(self, ):  #self explanitory
        sys.exit()


###############################################################################

    def updategraphs(self, fig):
        print("made it to update graphs")
        sip.delete(self.canvas)
        sip.delete(self.canvas1)
        sip.delete(self.canvas2)
        sip.delete(self.canvas3)
        sip.delete(self.canvas4)
        #self.spectrumUI.removeWidget(self.canvas)
        self.canvas = FigureCanvas(fig[0])
        self.canvas1 = FigureCanvas(fig[1])
        self.canvas2 = FigureCanvas(fig[2])
        self.canvas3 = FigureCanvas(fig[3])
        self.canvas4 = FigureCanvas(fig[4])

    def rmmpl(self, ):
        print("in rmmpl")
        self.spectrumUI.removeWidget(self.canvas)
        self.canvas.close()
        self.spectrumUI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph11UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph11UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph12UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph12UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph21UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph21UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph22UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph22UI.removeWidget(self.toolbar)
        self.toolbar.close()

    def plots_close(self, ):
        print("made it to plots_close")
        self.spectrumUI.removeWidget(self.canvas)
        sip.delete(self.canvas)
        self.canvas = None
        self.spectrumUI.removeWidget(self.toolbar)
        sip.delete(self.toolbar)
        self.toolbar = None

        self.graph11UI.removeWidget(self.canvas1)
        self.canvas1.close()
        self.graph11UI.removeWidget(self.toolbar1)
        self.toolbar1.close()

        self.graph12UI.removeWidget(self.canvas2)
        self.canvas2.close()
        self.graph12UI.removeWidget(self.toolbar2)
        self.toolbar2.close()

        self.graph21UI.removeWidget(self.canvas3)
        self.canvas3.close()
        self.graph21UI.removeWidget(self.toolbar3)
        self.toolbar3.close()

        self.graph22UI.removeWidget(self.canvas4)
        self.canvas4.close()
        self.graph22UI.removeWidget(self.toolbar4)
        self.toolbar4.close()
예제 #23
0
class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi()

    def setupUi(self):

        #Initilize variables for tutorial box pages
        self.current_page, self.max_Page = 0, 0
        self.app = 1

        #Size window to default dimensions
        self.resize(1800, 900)
        self.setWindowTitle("MaLeTS")

        #Create a central Widget
        self.centralwidget = QtWidgets.QWidget(self)

        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)

        self.gridLayout = QGridLayout()
        self.inputBox = QtWidgets.QGroupBox(self.centralwidget, title="Input")
        self.inputBox.setAlignment(QtCore.Qt.AlignCenter)

        self.dataTypeGroupBox = QtWidgets.QGroupBox()
        self.dataTypeLabel = QtWidgets.QLabel("Data Type:")
        self.dataSelectionComboBox = QtWidgets.QComboBox()
        self.dataSelectionComboBox.addItem("Numerical")
        self.dataGroupBox = QtWidgets.QStackedWidget()
        self.dataGroupBox.setInputMethodHints(QtCore.Qt.ImhNone)
        self.dataGroupBox.setFrameShape(QtWidgets.QFrame.Box)

        self.numericalPage = QtWidgets.QWidget()
        self.dataBox = QtWidgets.QGroupBox()
        self.generateNumbersGroupBox = QtWidgets.QGroupBox()
        self.generateNumbersLabel = QtWidgets.QLabel("(# of Dims, # of Points, # of Labels, Min #, Max #):")
        self.generateNumbersEdit = QtWidgets.QLineEdit()

        self.dataLabel = QtWidgets.QLabel("Data:")
        self.dataPlainTextEdit = QtWidgets.QPlainTextEdit()
        self.labelsBox = QtWidgets.QGroupBox()

        self.labelsLabel = QtWidgets.QLabel("Labels:")
        self.labelsPlainTextEdit = QtWidgets.QPlainTextEdit()
        self.generateButton = QtWidgets.QPushButton("Generate Numbers")
        self.generateButton.clicked.connect(self.generateNumbers)
        self.dataGroupBox.addWidget(self.numericalPage)
        self.inputVerticalLayout = QtWidgets.QVBoxLayout(self.inputBox)
        self.inputVerticalLayout.addWidget(self.dataTypeGroupBox)
        self.inputVerticalLayout.addWidget(self.dataGroupBox)
        self.dataTypeGroupBoxHorizontalLayout = QtWidgets.QHBoxLayout(self.dataTypeGroupBox)
        self.dataTypeGroupBoxHorizontalLayout.addWidget(self.dataTypeLabel)
        self.dataTypeGroupBoxHorizontalLayout.addWidget(self.dataSelectionComboBox)
        self.numericalPageVerticalLayout = QtWidgets.QVBoxLayout(self.numericalPage)
        self.numericalPageVerticalLayout.addWidget(self.dataBox)
        self.numericalPageVerticalLayout.addWidget(self.labelsBox)
        self.numericalPageVerticalLayout.addWidget(self.generateNumbersGroupBox)
        self.generateNumbersGroupBoxVerticalLayout = QtWidgets.QVBoxLayout(self.generateNumbersGroupBox)
        self.generateNumbersGroupBoxVerticalLayout.addWidget(self.generateNumbersLabel)
        self.generateNumbersGroupBoxVerticalLayout.addWidget(self.generateNumbersEdit)
        self.generateNumbersGroupBoxVerticalLayout.addWidget(self.generateButton)
        self.dataBoxHorizontalLayout = QtWidgets.QHBoxLayout(self.dataBox)
        self.dataBoxHorizontalLayout.addWidget(self.dataLabel)
        self.dataBoxHorizontalLayout.addWidget(self.dataPlainTextEdit)
        self.labelBoxHorizontalLayout = QtWidgets.QHBoxLayout(self.labelsBox)
        self.labelBoxHorizontalLayout.addWidget(self.labelsLabel)
        self.labelBoxHorizontalLayout.addWidget(self.labelsPlainTextEdit)

        self.algorithmBox = QtWidgets.QGroupBox(self.centralwidget, title="Algorithm")
        self.algorithmBox.setAlignment(QtCore.Qt.AlignCenter)
        self.algorithmSelectionGroupBox = QtWidgets.QGroupBox()
        self.algorithmSelectionGroupBox.setTitle("")
        self.algorithmLabel = QtWidgets.QLabel("Algorithm:")
        self.algorithmSelectionBox = QtWidgets.QComboBox()
        self.algorithmSelectionBox.addItem("Linear Support Vector Classifier")
        self.algorithmSelectionBox.addItem("K Nearest Neighbors Classifier")
        self.algorithmSelectionBox.addItem("Decision Tree Classifier")
        self.parametersBox = QtWidgets.QGroupBox("Parameters")
        self.parametersBox.setGeometry(QtCore.QRect(0, 40, 275, 270))
        self.parametersBox2 = QtWidgets.QGroupBox("Parameters2")
        self.algorithmBoxVerticalLayout = QtWidgets.QVBoxLayout(self.algorithmBox)
        self.algorithmBoxVerticalLayout.addWidget(self.algorithmSelectionGroupBox)
        self.algorithmBoxVerticalLayout.addWidget(self.parametersBox)
        self.algorithmSelectionGroupBoxHorizontalLayout = QtWidgets.QVBoxLayout(self.algorithmSelectionGroupBox)
        self.algorithmSelectionGroupBoxHorizontalLayout.addWidget(self.algorithmLabel)
        self.algorithmSelectionGroupBoxHorizontalLayout.addWidget(self.algorithmSelectionBox)

        self.outputBox = QtWidgets.QGroupBox(self.centralwidget, title="Output")
        self.outputBox.setAlignment(QtCore.Qt.AlignCenter)

        self.outputWidget = QtWidgets.QWidget(self.outputBox)
        self.plotBox = QtWidgets.QGroupBox(self.centralwidget, title="Plot")
        self.plotBox.setAlignment(QtCore.Qt.AlignCenter)
        fig = plt.figure(1, figsize=(1, 1),dpi=50)
        self.canvas = FigureCanvas(fig)
        self.canvas.draw()
        self.hbox3 = QVBoxLayout(self.plotBox)

        self.outputPlainTextEdit = QtWidgets.QPlainTextEdit()
        self.outputPredictLabel = QtWidgets.QLabel()
        self.outputPredictLabel.setText("Predict:")
        self.outputPredictLineEdit = QtWidgets.QLineEdit()
        self.outputVerticalLayout = QtWidgets.QVBoxLayout(self.outputBox)
        self.outputVerticalLayout.addWidget(self.outputPlainTextEdit)
        self.outputVerticalLayout.addWidget(self.outputPredictLabel)
        self.outputVerticalLayout.addWidget(self.outputPredictLineEdit)


        self.tutorialBox = QtWidgets.QGroupBox(self.centralwidget, title="Tutorial")
        self.tutorialBox.setAlignment(QtCore.Qt.AlignCenter)
        self.tutorialPlainTextEdit = QtWidgets.QPlainTextEdit()

        self.previousButton = QtWidgets.QPushButton('Previous Page')
        self.previousButton.clicked.connect(self.previous_Page)
        self.nextButton = QtWidgets.QPushButton('Next Page')
        self.nextButton.clicked.connect(self.next_Page)

        self.tutorialGroupBox = QtWidgets.QGroupBox()
        self.tutorialHorizontalLayout = QtWidgets.QHBoxLayout(self.tutorialGroupBox)
        self.tutorialHorizontalLayout.addWidget(self.previousButton)
        self.tutorialHorizontalLayout.addWidget(self.nextButton)
        self.tutorialVerticalLayout = QtWidgets.QVBoxLayout(self.tutorialBox)
        self.tutorialVerticalLayout.addWidget(self.tutorialPlainTextEdit)
        self.tutorialVerticalLayout.addWidget(self.tutorialGroupBox)
        self.plotVerticalLayout = QtWidgets.QVBoxLayout(self.plotBox)
        self.plotVerticalLayout.addWidget(self.canvas)
        self.scroll = QtWidgets.QScrollArea(self.parametersBox)
        self.scrollAreaContents = QtWidgets.QWidget(self.scroll)
        self.scroll.setWidgetResizable(True)
        self.scroll.setWidget(self.scrollAreaContents)
        self.hbox2 = QVBoxLayout(self.parametersBox)
        self.hbox2.addWidget(self.scroll)
        self.hbox4 = QVBoxLayout(self.scroll)
        self.hbox4.addWidget(self.scrollAreaContents)
        self.parametersVerticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaContents)
        menubar = self.menuBar()

        for letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']:
            exec('self.%s =QtWidgets.QLineEdit()' % letter)
            exec('self.%sl = QtWidgets.QLabel()' % letter)
            exec('self.%sBox = QtWidgets.QGroupBox(self.scrollAreaContents)' % letter)
            exec('self.%sHorizontalLayout = QtWidgets.QHBoxLayout(self.%sBox)' % (letter, letter))
            exec('self.%sHorizontalLayout.addWidget(self.%sl)' % (letter, letter))
            exec('self.%sHorizontalLayout.addWidget(self.%s)' % (letter, letter))
            exec('self.parametersVerticalLayout.addWidget(self.%sBox)' % letter)
            exec('self.%sBox.hide()' % letter)

        font = QtGui.QFont()
        font.setFamily("Segoe UI Historic")
        font.setWeight(50)
        menubar.setFont(font)
        menuFile = QtWidgets.QMenu(menubar)
        menuFile.setObjectName("menuFile")

        actionOpen = QtWidgets.QAction(self)
        actionOpen.setObjectName("actionOpen")
        actionOpen.setShortcut("Ctrl+O")
        actionOpen.setStatusTip('Open File')
        actionOpen.triggered.connect(self.file_open)
        actionSave = QtWidgets.QAction(self)
        actionSave.setObjectName("actionSave")
        actionSave.setShortcut("Ctrl+S")
        actionSave.setStatusTip('Save File')
        actionSave.triggered.connect(self.file_save)
        actionExit = QtWidgets.QAction(self)
        actionExit.setShortcut('Ctrl+Q')
        actionExit.triggered.connect(self.close)
        menuFile.addAction(actionOpen)
        menuFile.addAction(actionSave)
        menuFile.addAction(actionExit)
        menuFile.setTitle("File")
        actionOpen.setText("Open")
        actionSave.setText("Save")
        actionExit.setText("Exit")
        menubar.addAction(menuFile.menuAction())
        self.gridLayout.addWidget(self.inputBox,0,0,1,1)
        self.gridLayout.addWidget(self.algorithmBox,0,1,1,1)
        self.gridLayout.addWidget(self.outputBox,0,2,1,1)
        self.gridLayout.addWidget(self.tutorialBox,1,0,1,2)
        self.gridLayout.addWidget(self.plotBox, 1, 2, 1, 1)
        self.setCentralWidget(self.centralwidget)
        self.verticalLayout_2.addLayout(self.gridLayout)
        self.centralwidget.setLayout(self.verticalLayout_2)
        self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMaximizeButtonHint)
        self.dataGroupBox.setCurrentIndex(1)
        self.dataSelectionComboBox.activated['int'].connect(self.dataGroupBox.setCurrentIndex)
        self.dataSelectionComboBox.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(self)

    # Save File
    def file_save(self):
        name = QtWidgets.QFileDialog.getSaveFileName(self, 'Save File', filter="*.mld", )
        if name[0] != '':
            with open(name[0], 'w') as file:
                text = self.dataPlainTextEdit.toPlainText()
                text2 = self.labelsPlainTextEdit.toPlainText()
                text3 = str(self.algorithmSelectionBox.currentText())
                algorithms = [LinearSVC(), KNeighborsClassifier(), DecisionTreeClassifier()]
                to_Write = text + ":" + text2 + ":" + text3
                letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
                for i in range(len([j for j in algorithms[self.algorithmSelectionBox.currentIndex()].get_params()])):
                    to_Write += ":"
                    to_Write += eval("self.%s.text()" % letters[i])
                file.writelines(to_Write)

    # Open File
    def file_open(self):
        name = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', filter="*.mld")
        if name[0] != '':
            with open(name[0], 'r') as file:
                text = ('%s' % file.read())
            data, labels,algorithm= text.split(":")[:3]
            self.dataPlainTextEdit.setPlainText(data)
            self.labelsPlainTextEdit.setPlainText(labels)
            self.algorithmSelectionBox.setCurrentText(algorithm)

    # Add initial figure
    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.hbox3.addWidget(self.canvas, stretch=1)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)

    # Add 3d toolbar
    def addbar(self):
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.hbox3.addWidget(self.toolbar)

    # Remove 3d toolbar
    def rmbar(self):
        self.hbox3.removeWidget(self.toolbar)
        self.toolbar.close()

    # Number generator for labels and data
    def generateNumbers(self):
        text = eval(u'[%s]' % self.generateNumbersEdit.text())
        if len(text) == 5:
            dims, points, label, mini, maxi = text
            data = []
            labels = []
            label = [j for j in range(0, label + 1)]
            for i in range(1, points + 1):
                data_Point = []
                for j in range(dims):
                    data_Point.append(random.uniform(mini, maxi))
                data.append(data_Point)
                labels.append(random.randrange(min(label), max(label)))
            data = "%s" % data
            labels = "%s" % labels
            self.dataPlainTextEdit.setPlainText(data[1:-1])
            self.labelsPlainTextEdit.setPlainText(labels[1:-1])

    # Get and set parameters for chosen algorithm
    def algoParams(self, algorithm):
        y_pos = -22
        n = -1
        parameters = []
        letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
        for i in sorted([j for j in algorithm.get_params()]):
            n += 1
            y_pos += 22
            statement = eval(u'algorithm.%s' % i)
            label = eval('self.%sl' % letters[n])
            textEdit = eval('self.%s' % letters[n])
            label.setGeometry(QtCore.QRect(0, y_pos, 100, 21))
            label.setObjectName("%sl" % letters[n])
            label.setText(str(i + ":"))
            textEdit.setGeometry(QtCore.QRect(105, y_pos, 170, 21))
            textEdit.setObjectName("%s" % letters[n])
            textEdit.setText(str(statement))
            self.update()
            parameters.append(statement)

    def next_Page(self):
        if self.current_page != self.max_Page:
            self.current_page += 1

    def previous_Page(self):
        if self.current_page != 0:
            self.current_page -= 1

    def closeEvent(self, event):
        close = QtWidgets.QMessageBox.question(self, 'Exit', "Are you sure you want to quit?",
                                               QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
        if close == QtWidgets.QMessageBox.Yes:
            event.accept()
            sys.exit()
        else:
            event.ignore()
예제 #24
0
class bivariategroupDlg(QtWidgets.QDialog, Ui_bivariategroup):
    def __init__(self, parent=None):
        super(bivariategroupDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.XcomboBox.addItem('Auto')
        self.Y2comboBox.addItem('None')
        self.XcomboBox.addItems(DS.Lc[DS.Ic])
        self.Y1comboBox.addItems(DS.Lc[DS.Ic])
        self.Y2comboBox.addItems(DS.Lc[DS.Ic])
        self.XcomboBox.setCurrentIndex(0)
        self.Y1comboBox.setCurrentIndex(0)
        self.Y2comboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        self.farwardButton.clicked.connect(self.farward)
        self.backwardButton.clicked.connect(self.backward)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)
        self.id_group = 0

    def redraw(self):
        if self.XcomboBox.currentText()==self.Y1comboBox.currentText() or   \
            self.XcomboBox.currentText()==self.Y2comboBox.currentText() :
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "Variables \n must be different !",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        groups = pd.Series(DS.Gr)
        if groups.isnull().all():
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "Not all groups are defined !",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        groups = groups.astype('int')
        gr_type = [isinstance(e, (int, np.integer)) for e in groups]
        if sum(gr_type) != len(groups):
            QtWidgets.QMessageBox.critical(
                self, 'Error',
                "Groups must be all present \n and must be integers",
                QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        Lr = DS.Lr[DS.Ir]
        Cr = DS.Cr[DS.Ir]
        fig = Figure()
        color = 'blue'
        color1 = 'blue'
        Y1 = data[self.Y1comboBox.currentText()]
        if self.CcheckBox.isChecked():
            color = DS.Cc[self.Y1comboBox.currentIndex() - 1]
        if self.Y2comboBox.currentText() == "None":
            dual = False
            Y2 = Y1
        else:
            dual = True
            Y2 = data[self.Y2comboBox.currentText()]
            if self.CcheckBox.isChecked():
                color1 = DS.Cc[self.Y2comboBox.currentIndex() - 2]
        if self.XcomboBox.currentText() == 'Auto':
            auto = True
            X = Y1
        else:
            auto = False
            X = data[self.XcomboBox.currentText()]
        df = pd.DataFrame({
            'G': groups.values,
            'X': X,
            'Y1': Y1,
            'Y2': Y2,
            'C': Cr,
            'L': Lr
        })
        dfg = df.groupby(['G'])
        ngr = len(dfg)
        if (ngr == 1):
            QtWidgets.QMessageBox.critical(
                self, 'Error',
                'Your data have just one group.\n Slide Show needs more than a single group.',
                QtWidgets.QMessageBox.Ok)
            return ()
        groups = df.G.unique()
        if (auto):
            X = []
            for gr in groups:
                grp = dfg.get_group(gr)
                X = X + list(range(len(grp['X'])))
            df['X'] = X
        if (self.slideradioButton.isChecked()):
            if dual:
                ax = fig.add_subplot(1, 2, 1)
            else:
                ax = fig.add_subplot(1, 1, 1)
            if (self.id_group > ngr - 1):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', 'Highest element of list is reached',
                    QtWidgets.QMessageBox.Ok)
                self.id_group = ngr - 1
                return ()
            if (self.id_group < 0):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', 'Lowest element of list is reached',
                    QtWidgets.QMessageBox.Ok)
                self.id_group = 0
                return ()
            min_X = df['X'].min()
            max_X = df['X'].max()
            min_Y1 = df['Y1'].min()
            max_Y1 = df['Y1'].max()
            min_Y2 = df['Y2'].min()
            max_Y2 = df['Y2'].max()
            M = dfg.get_group(groups[self.id_group])
            ax.set_xlim([min_X, max_X])
            ax.set_ylim([min_Y1, max_Y1])
            if self.PcheckBox.isChecked():
                ax.scatter(M['X'], M['Y1'], marker='o', color=color)
            if self.LcheckBox.isChecked():
                ax.plot(M['X'], M['Y1'], linestyle='-', color=color)
            ax.xaxis.grid()
            ax.yaxis.grid()
            ax.set_ylabel(self.Y1comboBox.currentText())
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_title('Group: ' + str(groups[self.id_group]))
            if dual:
                ax1 = fig.add_subplot(1, 2, 2)
                ax1.set_xlim([min_X, max_X])
                ax1.set_ylim([min_Y2, max_Y2])
                if self.PcheckBox.isChecked():
                    ax1.scatter(M['X'], M['Y2'], marker='o', color=color1)
                if self.LcheckBox.isChecked():
                    ax1.plot(M['X'], M['Y2'], linestyle='-', color=color1)
                ax1.xaxis.grid()
                ax1.yaxis.grid()
                ax1.set_title('Group:' + str(groups[self.id_group]))
                ax1.set_xlabel(self.XcomboBox.currentText())
                ax1.set_ylabel(self.Y2comboBox.currentText())
        if (self.conditioningradioButton.isChecked()):
            if dual:
                ax = fig.add_subplot(1, 2, 1)
            else:
                ax = fig.add_subplot(1, 1, 1)
            for key, grp in df.groupby(['G']):
                if self.CcheckBox.isChecked():
                    vcol = grp['C']
                else:
                    vcol = cm.viridis.colors[int(
                        len(cm.viridis.colors) / ngr * key)]
                if self.PcheckBox.isChecked():
                    ax.scatter(grp['X'],
                               grp['Y1'],
                               color=vcol,
                               marker='o',
                               label=key)
                if self.LcheckBox.isChecked():
                    ax.plot(grp['X'],
                            grp['Y1'],
                            linestyle='-',
                            color=vcol,
                            label=key)
            ax.xaxis.grid()
            ax.yaxis.grid()
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.Y1comboBox.currentText())
            ax.legend(loc='best')
            if dual:
                ax1 = fig.add_subplot(1, 2, 2)
                for key, grp in df.groupby(['G']):
                    if self.CcheckBox.isChecked():
                        vcol = grp['C']
                    else:
                        vcol = cm.viridis.colors[int(
                            len(cm.viridis.colors) / ngr * key)]
                    if self.PcheckBox.isChecked():
                        ax1.scatter(grp['X'],
                                    grp['Y2'],
                                    color=vcol,
                                    marker='o',
                                    label=key)
                    if self.LcheckBox.isChecked():
                        ax1.plot(grp['X'],
                                 grp['Y2'],
                                 linestyle='-',
                                 color=vcol,
                                 label=key)
                ax1.xaxis.grid()
                ax1.yaxis.grid()
                ax1.set_xlabel(self.XcomboBox.currentText())
                ax1.set_ylabel(self.Y2comboBox.currentText())
                ax1.legend(loc='best')
        if (self.scatteradioButton.isChecked()):
            ax = fig.add_subplot(1, 1, 1)
            if dual:
                ax1 = ax.twinx()
            for key, grp in df.groupby(['G']):
                if self.CcheckBox.isChecked():
                    vcol = grp['C']
                else:
                    vcol = cm.viridis.colors[int(
                        len(cm.viridis.colors) / ngr * key)]
                if self.PcheckBox.isChecked():
                    ax.scatter(grp['X'],
                               grp['Y1'],
                               color=vcol,
                               marker='o',
                               label=key)
                    if dual:
                        ax1.scatter(grp['X'],
                                    grp['Y2'],
                                    color=vcol,
                                    marker='*',
                                    label=key)
                if self.LcheckBox.isChecked():
                    ax.plot(grp['X'],
                            grp['Y1'],
                            linestyle='-',
                            color=vcol,
                            label=key)
                    if dual:
                        ax1.plot(grp['X'],
                                 grp['Y2'],
                                 linestyle='-',
                                 color=vcol,
                                 label=key)
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.Y1comboBox.currentText())
            if dual:
                ax1.set_ylabel(self.Y2comboBox.currentText())
            ax.legend(loc='best')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
                if dual:
                    ax1.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
            if dual:
                ax1.set_xlabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
            if dual:
                ax1.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
            if dual:
                ax1.xaxis.grid(False)
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
            if dual:
                ax1.yaxis.grid(True)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
            if dual:
                ax1.tick_params(axis='x',
                                which='both',
                                bottom='off',
                                top='off',
                                labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
            if dual:
                ax1.tick_params(axis='y',
                                which='both',
                                bottom='off',
                                top='off',
                                labelbottom='off')
        self.rmmpl()
        self.addmpl(fig)

    def farward(self):
        self.id_group += 1
        self.redraw()

    def backward(self):
        self.id_group -= 1
        self.redraw()

    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.Y1comboBox.setCurrentIndex(0)
        self.Y2comboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.LcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #25
0
class Main(QMainWindow, Ui_MainWindow):
    """The class's docstring"""

    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)
        self.menubar = self.menuBar()
        self.menubar.setNativeMenuBar(False)

        # define some callback functions here
        from classes.pytecpiv_dialog_conf_class import dialog_conf

        self.actionConfiguration.triggered.connect(self.show_conf_fn)  # menu settings
        self.dialog_conf = dialog_conf(self)

        from classes.pytecpiv_dialog_fig_class import dialog_img
        self.dialog_img = dialog_img(self)

        self.new_project_menu.triggered.connect(self.create_new_project)  # menu new project
        self.import_calib_menu.triggered.connect(self.import_calib_img)  # menu data import calib image

        self.Dataset_comboBox.currentIndexChanged.connect(self.dataset_combobox_fn)

        self.Img_pushButton.clicked.connect(self.show_dialog_img)

    def show_dialog_img(self):
        self.dialog_img.show()

    def addmpl(self, fig):
        """The method's docstring"""
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self.mplfig, coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        """The method's docstring"""
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def show_conf_fn(self):
        """This function makes visible the dialogue box for the configuration"""
        import os
        from pytecpiv_conf import pytecpiv_get_pref

        current_directory = os.getcwd()
        (file_exist, sources_path, projects_path) = pytecpiv_get_pref()
        self.dialog_conf.code_label.setText(current_directory)
        self.dialog_conf.sources_label.setText(sources_path)
        self.dialog_conf.projects_label.setText(projects_path)
        self.dialog_conf.show()

    def create_new_project(self):
        from PyQt5.QtWidgets import QFileDialog
        from pytecpiv_conf import pytecpiv_get_pref
        from datetime import datetime
        from pytecpiv_util import dprint
        import json

        file_exist, sources_path, projects_path = pytecpiv_get_pref()
        this_project_path = QFileDialog.getExistingDirectory(self, 'Open directory', projects_path)

        drive, path_and_file = os.path.splitdrive(this_project_path)
        path_project, project_name = os.path.split(path_and_file)

        # create new project
        project_create_time = str(datetime.now())
        dprint(' ')
        dprint(project_create_time)
        dprint('creating new project')

        # create new project_metadata file
        t = os.path.isfile('project_metadata.json')

        if t:
            os.remove('project_metadata.json')

        metadata = {'project': []}
        metadata['project'].append({'project_path': this_project_path})
        metadata['project'].append({'path': path_project})
        metadata['project'].append({'name': project_name})
        metadata['project'].append({'create_date': project_create_time})

        with open('project_metadata.json', 'w') as outfile:
            json.dump(metadata, outfile)

    def import_calib_img(self):
        from PyQt5.QtWidgets import QFileDialog
        from pytecpiv_conf import pytecpiv_get_pref, pytecpiv_set_cores
        from pytecpiv_util import dprint
        from pytecpiv_import import import_img

        import json
        import os

        global dataset_index, current_dataset, time_step

        #  get the preferences for where sources are located
        file_exist, sources_path, projects_path = pytecpiv_get_pref()

        #  pick the directory with the raw calib images; open the sources directory
        source_path_calib_img = QFileDialog.getExistingDirectory(self, 'Open directory', sources_path)

        #  get the fraction core for parallel processing from conf dialog
        fraction_cores = self.dialog_conf.SliderCores.value()
        fraction_cores = fraction_cores / 100

        n_cores, use_cores = pytecpiv_set_cores(fraction_cores)

        dprint(str(n_cores) + ' cores available')
        dprint('using ' + str(use_cores) + ' cores when parallel')

        #  open the project_metadata to get the name and path of the new project
        with open('project_metadata.json') as f:
            project_data = json.load(f)

            project = project_data["project"]
            project = project[0]
            project_path = project["project_path"]

        #  create a directory named CALIB inside the new project directory to store the imported calibration images
        project_path_calib_img = os.path.join(project_path, 'CALIB')

        t = os.path.exists(project_path_calib_img)

        if t:
            dprint('saving in directory: ' + project_path_calib_img)
        else:
            os.mkdir(project_path_calib_img)
            dprint('saving in directory: ' + project_path_calib_img)

        n_img = import_img(source_path_calib_img, project_path_calib_img, use_cores)

        project_data['project'].append({'source_calibration': source_path_calib_img,
                                        'number_calibration_images': n_img})

        # save metadata
        with open('project_metadata.json', 'w') as outfile:
            json.dump(project_data, outfile)

        # create new dataset entry and select
        dataset_index = dataset_index + 1

        current_dataset[0] = 'calibration'
        current_dataset[1] = 1
        current_dataset[2] = 1
        current_dataset[3] = 0
        current_dataset[4] = 0
        current_dataset[5] = project_path_calib_img
        current_dataset[6] = 0
        current_dataset[7] = 1
        current_dataset[8] = 'Greys'

        #  change the frame number and time in gui
        self.frame_text.setText(str(1))
        self.time_text.setText(str((1 - 1) * time_step))

        # change status of chow image checkbox
        self.Img_checkBox.setCheckState(2)

        # save dataset in json file
        table_dataset = {dataset_index: []}
        table_dataset[dataset_index].append({
            'name': 'calibration',
            'frame': 1,
            'plot_image': 1,
            'plot_vector': 0,
            'plot_scalar': 0,
            'image_path': project_path_calib_img,
            'img_value_min': 0,
            'img_value_max': 1,
            'img_colormap': 'Greys'
        })

        # save data in json file in sources
        with open('table_dataset.json', 'w') as outfile:
            json.dump(table_dataset, outfile)

        # change the selected combobox
        self.Dataset_comboBox.insertItem(int(dataset_index), 'calibration images')
        self.Dataset_comboBox.setCurrentIndex(int(dataset_index))

    def dataset_combobox_fn(self):
        """

        :param self:
        :return:
        """
        import json
        from pytecpiv_util import create_fig

        global current_dataset, fig1

        index = self.Dataset_comboBox.currentIndex()

        self.rmmpl()  # clear the mpl for a replot

        if index == 0:  # this is a special index with the credits & license

            fig1 = Figure()
            ax1f1 = fig1.add_subplot(111)
            s1 = 'pyTecPIV v0.1-alpha'
            s2 = 'build on Python 3.7 with the following packages:'
            s3 = 'numpy, scikit-image, rawpy, json, hdf5, matplotlib'
            s4 = 'GUI build with Qt5'
            s5 = 'D. Boutelier, 2020'
            ax1f1.margins(0, 0, tight=True)
            ax1f1.set_ylim([0, 1])
            ax1f1.set_xlim([0, 1])
            ax1f1.text(0.01, 0.95, s1, fontsize=12)
            ax1f1.text(0.01, 0.9, s2, fontsize=10)
            ax1f1.text(0.01, 0.85, s3, fontsize=10)
            ax1f1.text(0.01, 0.775, s4, fontsize=10)
            ax1f1.text(0.01, 0.675, s5, fontsize=8)
            ax1f1.set_aspect('equal')
            ax1f1.set_axis_off()

        else:
            #  open the table_dataset file
            with open('table_dataset.json') as f:
                table_dataset = json.load(f)
                selected_dataset = table_dataset[str(index)]
                selected_dataset = selected_dataset[0]
                current_dataset[0] = selected_dataset['name']
                current_dataset[1] = selected_dataset['frame']
                current_dataset[2] = selected_dataset['plot_image']
                current_dataset[3] = selected_dataset['plot_vector']
                current_dataset[4] = selected_dataset['plot_scalar']
                current_dataset[5] = selected_dataset['image_path']
                current_dataset[6] = selected_dataset['img_value_min']
                current_dataset[7] = selected_dataset['img_value_max']
                current_dataset[8] = selected_dataset['img_colormap']

            fig1 = Figure()
            create_fig(fig1, current_dataset)

        self.addmpl(fig1)
예제 #26
0
class Main(QMainWindow, Ui_MainWindow):

    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)

        self.showMaximized()
        self.ave = np.array([])         # empty array for average bore
        self.auto_flag = False          # autoscale flag
        self.spinBoxval = 0             # defualt 4D data_cube dimension
        self.spinBox.hide()             # hide option unless 4D
        self.colourmap = 'viridis'      # default colourmap
        self.interpMethod = 'nearest'   # default interp method
        self.cmapmin = None             # default colourbar range, i.e let matplotlib decide
        self.cmapmax = None
        self.hres = 1                   # default res, i.e jut voxel numbers on axis
        self.vres = 1
        self.Normx = None               # normalisation method. default set to None
        self.Normy = None
        self.Normz = None

        self.XView.setChecked(True)
        self.fig = Figure()
        self.ax1 = self.fig.add_subplot(111)
        self.X = datacube()
        self.AveBoreView = 'X'

        # change view of cube
        self.XView.toggled.connect(lambda: self.btnstate(self.XView))
        self.YView.toggled.connect(lambda: self.btnstate(self.YView))
        self.ZView.toggled.connect(lambda: self.btnstate(self.ZView))
        self.Bore.toggled.connect(lambda: self.btnstate(self.Bore))
        self.AverageBore.toggled.connect(
            lambda: self.btnstate(self.AverageBore))

        # update data when slider moved
        self.Scroll_Horz.valueChanged[int].connect(self.sliderval)
        self.Scroll_Vert.valueChanged[int].connect(self.sliderval)

        self.file_open(args)
        self.Open.triggered.connect(self.file_open)
        self.Save_Avg_Bore.triggered.connect(self.saveBore)
        self.Reset.triggered.connect(self.reset_plot)
        self.AutoScale.triggered.connect(self.Auto_Scale_plot)
        # self.Bore_View.triggered.connect(self.ViewBore)
        self.action_Save_Gif.triggered.connect(self.saveGif)
        self.action_Colour_Map.triggered.connect(self.changeColourMap)
        self.action_Interpolation_Method.triggered.connect(self.changeInterpolationMethod)
        self.action_Colour_Bar_Clip.triggered.connect(self.changeclipColourBarRange)
        self.action_Save_Image.triggered.connect(self.saveImage)
        self.action_Normalisation_Method.triggered.connect(self.changeNormMethod)
        self.action_Bore_Location.triggered.connect(self.setBoreLocation)

        self.spinBox.valueChanged.connect(self.changeSpinbox)

    def setBoreLocation(self, ):

        xloc, ok = QtWidgets.QInputDialog.getInt(
            self, 'Input location', 'Enter X location:')

        yloc, ok = QtWidgets.QInputDialog.getInt(
            self, 'Input location', 'Enter Y location:')

        self.Scroll_Horz.setValue(xloc)
        self.Scroll_Vert.setValue(yloc)

        if self.Bore.isChecked():
            if self.BoreView == 'X':
                self.im.set_ydata(self.X.data[:, xloc, yloc][::])
            elif self.BoreView == 'Y':
                self.im.set_ydata(self.X.data[xloc, :, yloc][::])
            elif self.BoreView == 'Z':
                self.im.set_ydata(self.X.data[yloc, xloc, :][::])

        # try and redraw
        try:
            self.im.axes.figure.canvas.draw()
            if self.auto_flag:
                self.im.autoscale()
        except AttributeError:
            pass

    def changeNormMethod(self, ):
        # func to change Normalisation method of matshow
        method = self.getNormDialog()
        if(method == 'Log'):
            self.Normx = colors.LogNorm(vmin=0.1,
                                        vmax=self.X.data[self.ind, :, :].max())
            self.Normy = colors.LogNorm(vmin=0.1,
                                        vmax=self.X.data[:, self.ind, :].max())
            self.Normz = colors.LogNorm(vmin=0.1,
                                        vmax=self.X.data[:, :, self.ind].max())
        elif(method == 'Symmetric Log'):
            self.Normx = colors.SymLogNorm(linthresh=1.,
                                           vmin=self.X.data[self.ind, :, :].min(),
                                           vmax=self.X.data[self.ind, :, :].max())
            self.Normy = colors.SymLogNorm(linthresh=1.,
                                           vmin=self.X.data[:, self.ind, :].min(),
                                           vmax=self.X.data[:, self.ind, :].max())
            self.Normz = colors.SymLogNorm(linthresh=1.,
                                           vmin=self.X.data[:, :, self.ind].max(),
                                           vmax=self.X.data[:, :, self.ind].max())
        elif(method == 'Linear'):
            self.Normx = None
            self.Normy = None
            self.Normz = None
        self.reset_plot(False)
        self.init_plot()

    def saveImage(self, ):
        # saves data and image of current view
        name = self.showGifDialog()
        if self.XView.isChecked():
            np.savetxt(name + '.dat', self.X.data[self.Scroll_Vert.value(), :, :],
                       delimiter=' ')
        elif self.YView.isChecked():
            np.savetxt(name + '.dat', self.X.data[:, self.Scroll_Vert.value(), :],
                       delimiter=' ')
        elif self.ZView.isChecked():
            np.savetxt(name + '.dat', self.X.data[:, :, self.Scroll_Vert.value()],
                       delimiter=' ')

        self.hres, self.vres = self.showextentDialog()
        # scale x, y ticks to actual scale based upon user definition
        # thanks https://stackoverflow.com/a/17816809/6106938
        # change so that it uses extent=[xmin, xmax, ymin, ymax]
        # set default as None
        # then change here. extent added to matshow(*args, extent=[...])
        ticks = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * self.hres))
        self.ax1.xaxis.set_major_formatter(ticks)
        ticks = ticker.FuncFormatter(lambda y, pos: '{0:g}'.format(y * self.vres))
        self.ax1.yaxis.set_major_formatter(ticks)
        self.fig.savefig(name + '.png')

    def changeColourMap(self, ):
        # change cmap
        self.colourmap = self.showColourmapsDialog()
        self.reset_plot(False)
        self.init_plot()

    def changeclipColourBarRange(self, ):
        # change vmin, vmax for cbar

        self.cmapmin, self.cmapmax = self.showclipColourBarDialog()

        self.reset_plot(False)
        self.init_plot()

    def changeInterpolationMethod(self, ):
        # change interpolation method for image
        self.interpMethod = str(self.showInterpolationDialog())
        self.reset_plot(False)
        self.init_plot()

    def saveGif(self):
        rang = self.showGifframesDialog()  # get range of frames
        step = self.showGifstepDialog()    # get number of images
        name = self.showGifDialog()        # name of file
        tight = self.showGifExtent()       # tight or not
        # loop over range and make images
        tmpplace = self.Scroll_Vert.value()
        if rang * step > tmpplace:
            rang = tmpplace
        for i in range(rang):
            self.Scroll_Horz.setValue(self.ind)
            self.Scroll_Vert.setValue(tmpplace - (i * step))
            self.sliderval()
            if tight:
                extent = self.ax1.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
                self.fig.savefig(str(i).zfill(3) + 'pic.png', bbox_inches=extent)
            else:
                self.fig.savefig(str(i).zfill(3) + 'pic.png')
        # use ffmpeg to create gif
        if tight:
            os.system("mogrify -trim *pic.png")
        os.system("ffmpeg -framerate 10 -pattern_type glob -i '*pic.png' -c:v libx264 -r 24 -pix_fmt yuv420p -vf 'pad=ceil(iw/2)*2:ceil(ih/2)*2' " + name + ".mp4")
        os.system('rm *pic.png')
        print('done')

    def changeSpinbox(self):
        # for 4d data cubes
        self.spinBoxval = int(self.spinBox.value())
        fd = open(self.name, 'rb')
        self.readslice(fd, self.ndim, np.float64, self.cubeorder)
        self.reset_plot()
        self.init_plot()

    def Auto_Scale_plot(self):
        # autoscale cbar on plot and reset clipping if any
        self.cmapmin = None
        self.cmapmax = None
        if not self.auto_flag:
            self.auto_flag = True
        else:
            self.auto_flag = False

    def sliderval(self):
        # move slider and update data
        if self.XView.isChecked():
            # fd = open(self.name, 'rb')
            self.X.readslice(self.Scroll_Horz.value())
            self.im.set_data(self.X.data[self.Scroll_Vert.value(), :, :])
            # self.Scroll_Horz.setValue(0)  # pin unsed slider
        elif self.YView.isChecked():
            self.X.readslice(self.Scroll_Horz.value())
            self.im.set_data(self.X.data[:, self.Scroll_Vert.value(), :])
            # self.Scroll_Horz.setValue(0)  # pin unsed slider
        elif self.ZView.isChecked():
            self.X.readslice(self.Scroll_Horz.value())
            self.im.set_data(self.X.data[:, :, self.Scroll_Vert.value()])
            # self.Scroll_Horz.setValue(0)  # pin unsed slider
        elif self.Bore.isChecked():
            if self.BoreView == 'X':
                self.im.set_ydata(self.X.data[:, self.Scroll_Horz.value(), self.Scroll_Vert.value()][::])
                if self.auto_flag:
                    self.ax1.relim()
                    self.ax1.autoscale_view(True, True, True)
            elif self.BoreView == 'Y':
                self.im.set_ydata(self.X.data[self.Scroll_Horz.value(), :, self.Scroll_Vert.value()][::])
                if self.auto_flag:
                    self.ax1.relim()
                    self.ax1.autoscale_view(True, True, True)
            elif self.BoreView == 'Z':
                self.im.set_ydata(self.X.data[self.Scroll_Vert.value(), self.Scroll_Horz.value(), :][::])
                if self.auto_flag:
                    self.ax1.relim()
                    self.ax1.autoscale_view(True, True, True)
        elif self.AverageBore.isChecked():
            self.Scroll_Horz.setValue(self.ind)
            self.Scroll_Vert.setValue(self.ind)

        # try and redraw
        try:
            self.im.axes.figure.canvas.draw()
            if self.auto_flag:
                self.im.autoscale()
        except AttributeError:
            pass

    def addmpl(self):
        # add plot to anvas
        self.rmmpl()
        self.canvas = FigureCanvas(self.fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self.mplwindow)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self):
        # delete plot from canvas
        try:
            self.canvas.close()
            self.canvas.deleteLater()
            self.toolbar.close()
            self.toolbar.deleteLater()
            gc.collect()
        except:
            pass

    def saveBore(self,):
        # save bore as a list of points
        name = QtWidgets.QFileDialog.getSaveFileName(self, 'Save File')
        f = open(name, 'w')
        if len(self.ave) > 1:
            for i in range(len(self.ave)):
                f.write(str(self.ave[i]) + '\n')
            f.close()
        else:
            if self.BoreView == "X":
                tmp = self.X[:, self.Scroll_Horz.value(), self.Scroll_Vert.value()]
            elif self.BoreView == "Y":
                tmp = self.X[self.Scroll_Horz.value(), :, self.Scroll_Vert.value()]
            elif self.BoreView == "Z":
                tmp = self.X[self.Scroll_Horz.value(), self.Scroll_Vert.value(), :]

            for i in range(len(tmp)):
                f.write(str(tmp[i]) + '\n')

    def file_open(self, args):

        self.reset_plot()

        while True:
            try:
                # get file name
                if args.file is None:
                    self.X.name = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File')[0]
                else:
                    self.X.name = args.file

                # get precision of data cube
                self.X.dtype, self.X.cubeorder, item = self.getPrec(args)

                # get dimensions of data cube. can be guessed
                bool4d = False
                if self.X.cubeorder == 4:
                    bool4d = True
                self.X.ndim = self.getSize(args, item, bool4d)

                try:
                    fd = open(self.X.name, 'rb')
                except FileNotFoundError:
                    self.ErrorDialog("File not Found!")
                    self.X.name = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File')[0]

                self.X.readslice(0)
                self.init_plot()
                break
            except ValueError:
                size = os.path.getsize(self.X.name)
                if "Real*8" in item:
                    size /= 8
                elif "Real*4" in item:
                    size /= 4

                mssg = "Value of Ndim or precision is incorrect for this data cube.\n On disk size is: {:010d}.\n".format(int(size))

                val2 = self.X.is_perfect_n(size, 2.)
                val3 = self.X.is_perfect_n(size, 3.)

                if (val2 and val3) != 0:
                    mssg += " Try x=y={:04d}, z=1\n or x=y=z={:04d}.".format(int(val2), int(val3))
                elif val2 != 0:
                    mssg += "Try x=y={:04d}, z=1.".format(int(val2))
                elif val3 != 0:
                    mssg += "Try x=y=z={:04d}.".format(int(val3))
                self.ErrorDialog(mssg)

                args.ndim = None
                args.fpprec = None
            except UnboundLocalError:
                pass
                break

    def getSize(self, args, item, bool4d):
        if args.ndim is None and item:
            size = os.path.getsize(self.X.name)
            if "Real*8" in item:
                size /= 8
            elif "Real*4" in item:
                size /= 4
            if self.X.is_perfect_n(size, 3.) != 0:
                size = self.X.is_perfect_n(size, 3.)
                ndim = (size, size, size)
            else:
                ndim = self.showNdimDialog(bool4d)
        else:
            ndim = (args.ndim, args.ndim, args.ndim)

        return ndim

    def getPrec(self, args):
        # get precision of data cube
        item = None
        if args.fpprec is None:
            item = str(self.showDtDialog())
            if "Real*8" in item:
                dt = np.float64
            elif "Real*4" in item:
                dt = np.float32

            if "4 dim" in item:
                dim = 4
            elif "3 dim" in item:
                dim = 3
        else:
            if args.fpprec == 1:
                dt = np.float32
                dim = 4
            elif args.fpprec == 2:
                dt = np.float64
                dim = 4
            elif args.fpprec == 3:
                dt = np.float32
                dim = 3
            elif args.fpprec == 4:
                dt = np.float64
                dim = 3
        return dt, dim, item

    def btnstate(self, b):

        if b.text() == "X View":
            if b.isChecked() is True:
                self.reset_plot(False)
                self.Scroll_Vert.setMaximum(self.rows - 1)
                self.im = self.ax1.matshow(self.X.data[self.ind, :, :],
                                           vmin=self.cmapmin, vmax=self.cmapmax,
                                           cmap=str(self.colourmap), interpolation=self.interpMethod,
                                           norm=self.Normx)
                self.fig.colorbar(self.im)
                self.fig.set_tight_layout(True)
                self.ax1.set_aspect('auto')
                self.addmpl()

        if b.text() == "Y View":
            if b.isChecked() is True:
                self.reset_plot(False)
                self.Scroll_Vert.setMaximum(self.cols - 1)
                self.im = self.ax1.matshow(self.X.data[:, self.ind, :],
                                           vmin=self.cmapmin, vmax=self.cmapmax,
                                           cmap=str(self.colourmap), interpolation=self.interpMethod,
                                           norm=self.Normy)
                self.fig.colorbar(self.im)
                self.fig.set_tight_layout(True)
                self.ax1.set_aspect('auto')
                self.addmpl()

        if b.text() == "Z View":
            if b.isChecked() is True:
                self.reset_plot(False)
                self.Scroll_Vert.setMaximum(self.slices - 1)
                self.im = self.ax1.matshow(self.X.data[:, :, self.ind],
                                           vmin=self.cmapmin, vmax=self.cmapmax,
                                           cmap=str(self.colourmap), interpolation=self.interpMethod,
                                           norm=self.Normz)
                try:
                    self.fig.colorbar(self.im)
                except ZeroDivisionError:
                    self.ErrorDialog("Divison by zero, try another range")
                    self.Normz = None
                    self.Normy = None
                    self.Normz = None
                    self.cmapmin = None
                    self.cmapmax = None
                    self.btnstate(b)
                self.fig.set_tight_layout(True)
                self.ax1.set_aspect('auto')
                self.addmpl()

        if b.text() == "Draw Bore":
            if b.isChecked() is True:
                self.ViewBore()
                self.reset_plot(False)
                if self.BoreView == 'X':
                    self.im, = self.ax1.plot(self.X.data[:, self.ind, self.ind])
                elif self.BoreView == 'Y':
                    self.im, = self.ax1.plot(self.X.data[self.ind, :, self.ind])
                elif self.BoreView == 'Z':
                    self.im, = self.ax1.plot(self.X.data[self.ind, self.ind, :])
                self.fig.set_tight_layout(True)
                self.addmpl()

        if b.text() == "Avg. Bore":
            if b.isChecked() is True:
                self.AveBoreChecked()

    def ViewBore(self):
        self.BoreView = self.showBoreViewDialog()
        if self.BoreView == 'X':
            self.view = (1, 2)
        elif self.BoreView == 'Y':
            self.view = (0, 2)
        elif self.BoreView == 'Z':
            self.view = (0, 1)

    def AveBoreChecked(self):

        self.ViewBore()

        self.reset_plot(False)
        if len(self.ave) == 0:
            self.ave = np.array([])
            self.ave = np.sum(self.X.data, self.view)
            self.ave /= (len(self.X.data[self.view[0]]) * len(self.X.data[self.view[1]]))

        self.im = self.ax1.plot(self.ave[::])
        self.fig.set_tight_layout(True)
        self.addmpl()

    def reset_plot(self, *args):
        self.ave = np.array([])

        self.fig.clf()
        self.ax1.clear()
        gc.collect()  # fixes most of memory leak

        self.fig = Figure()
        self.ax1 = self.fig.add_subplot(111)
        self.rmmpl()

    def init_plot(self, ):

        self.rmmpl()

        if self.X.cubeorder == 4:
            self.rows, self.cols, self.slices, self.depth = self.X.ndim
        else:
            self.rows, self.cols, self.slices = self.X.ndim
            self.depth = 0
        self.ind = 0  # int(rows / 2)
        if self.XView.isChecked():
            view = self.XView
            self.Scroll_Vert.setMaximum(self.rows)
            self.Scroll_Horz.setMaximum(self.depth)
        elif self.YView.isChecked():
            view = self.YView
            self.Scroll_Vert.setMaximum(self.cols)
            self.Scroll_Horz.setMaximum(self.depth)
        elif self.ZView.isChecked():
            view = self.ZView
            self.Scroll_Vert.setMaximum(self.slices)
            self.Scroll_Horz.setMaximum(self.cols)
        elif self.AverageBore.isChecked():
            view = self.AverageBore
            self.Scroll_Vert.setMaximum(self.rows)
        elif self.Bore_View.isChecked():
            view = self.ViewBore
            self.Scroll_Vert.setMaximum(self.cols)
            self.Scroll_Horz.setMaximum(self.rows)
        self.btnstate(view)
        self.Scroll_Horz.setValue(self.ind)
        self.Scroll_Vert.setValue(self.ind)

    def showGifframesDialog(self, ):
        text, ok = QtWidgets.QInputDialog.getInt(
            self, '# of frames', 'Enter # of frames:')
        if ok:
            return(text)

    def showGifstepDialog(self, ):
        text, ok = QtWidgets.QInputDialog.getInt(
            self, 'Step size', 'Enter value of step:')
        if ok:
            return(text)

    def showGifExtent(self, ):
        items = ("Colour Bar", "No Colour Bar")

        text, ok = QtWidgets.QInputDialog.getItem(
            self, "Colour Bar on GIF?", " ", items, 0, False)

        if ok and text:
            if items == "Colour Bar":
                text = False
            else:
                text = True
            return text

    def showNdimDialog(self, bool4d):
        text1, ok1 = QtWidgets.QInputDialog.getInt(
            self, 'Input Ndim', 'Enter X Ndim:')
        if ok1:
            text2, ok2 = QtWidgets.QInputDialog.getInt(
                self, 'Input Ndim', 'Enter Y Ndim:')
            if ok2:
                text3, ok3 = QtWidgets.QInputDialog.getInt(
                    self, 'Input Ndim', 'Enter Z Ndim:')
                if ok3 and bool4d:
                    text4, ok4 = QtWidgets.QInputDialog.getInt(
                        self, 'Input Ndim', 'Enter T Ndim:')
                    return (text1, text2, text3, text4)
                else:
                    return (text1, text2, text3)

    def showDtDialog(self, ):
        items = ("4 dim Real*4", "4 dim Real*8",
                 "3 dim Real*4", "3 dim Real*8")

        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Fortran Precision",
                                                  "Precisions", items, 0, False)

        if ok and item:
            return item

    def showBoreViewDialog(self, ):
        items = ("X", "Y", "Z")
        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Average Bore Direction",
                                                  "Views", items, 0, False)
        if ok and item:
            return item

    def showGifDialog(self, ):
        text, ok = QtWidgets.QInputDialog.getText(
            self, 'Filename Dialog', 'Enter filename:')
        if ok:
            return str(text)

    def showextentDialog(self, ):
        hres, ok = QtWidgets.QInputDialog.getDouble(
            self, 'Data Extent', 'Enter horizontal resolution:', 0, -100, 100, 9,)
        if ok:
            vres, ok = QtWidgets.QInputDialog.getDouble(
                self, 'Data Extent', 'Enter vertical resolution:', 0, -100, 100, 9,)
            if ok:
                return (hres, vres)

    def getNormDialog(self, ):
        items = ("Log", "Linear", "Symmetric Log")

        item, ok = QtWidgets.QInputDialog.getItem(self, "Select cbar normalisation method",
                                                  "Method:", items, 0, False)
        if ok and item:
            return item

    def showColourmapsDialog(self, ):
        items = ('viridis', 'inferno', 'plasma', 'magma', 'Blues', 'BuGn',
                 'BuPu', 'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd', 'PuBu',
                 'PuBuGn', 'PuRd', 'Purples', 'RdPu', 'Reds', 'YlGn', 'YlGnBu',
                 'YlOrBr', 'YlOrRd', 'afmhot', 'autumn', 'bone', 'cool',
                 'copper', 'gist_heat', 'gray', 'hot', 'pink', 'spring',
                 'summer', 'winter', 'BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn',
                 'PuOr', 'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',
                 'seismic', 'Accent', 'Dark2', 'Paired', 'Pastel1', 'Pastel2',
                 'Set1', 'Set2', 'Set3', 'Vega10', 'Vega20', 'Vega20b',
                 'Vega20c', 'gist_earth', 'terrain', 'ocean', 'gist_stern',
                 'brg', 'CMRmap', 'cubehelix', 'gnuplot', 'gnuplot2',
                 'gist_ncar', 'nipy_spectral', 'jet', 'rainbow', 'gist_rainbow',
                 'hsv', 'flag', 'prism')
        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Colour Map",
                                                  "Cmaps", items, 0, False)
        if ok and item:
            return item

    def showInterpolationDialog(self, ):
        items = ('none', 'nearest', 'bilinear', 'bicubic', 'spline16',
                 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser',
                 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell',
                 'sinc', 'lanczos')
        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Interpolation Method",
                                                  "Methods", items, 0, False)
        if ok and item:
            return item

    def showclipColourBarDialog(self, ):
        text1, ok1 = QtWidgets.QInputDialog.getDouble(
            self, 'Input cbar min', 'Enter min:', 0., np.finfo("d").min, np.finfo("d").max, 10)
        if ok1:
            text2, ok2 = QtWidgets.QInputDialog.getDouble(
                self, 'Input cbar max', 'Enter max:', 0., np.finfo("d").min, np.finfo("d").max, 10)
            if ok2:
                return (float(text1), float(text2))

    def ErrorDialog(self, ErrMsg):
        QtWidgets.QMessageBox.warning(self, "Error", ErrMsg)
예제 #27
0
class Application(Ui_QtBaseClass, Ui_MainWindow):
    def __init__(self, parent=None):
        Ui_QtBaseClass.__init__(self, parent)
        self._figure = None

        self.setupUi(self)
        self.update_parameters(self.static_default_parameters())
        self.show()

        self.on_click_submit()  # load graph based on default values

        # (figure_width, figure_height) = tuple(self._figure.figure.get_size_inches)
        # self.update_parameters(figure_size_width = figure_width, figure_size_height = figure_height, figure_size_scale = 1.0)

        # connect button to function on_click
        self.pushButton_submit.clicked.connect(self.on_click_submit)
        self.pushButton_quit.clicked.connect(self.on_click_quit)
        self.pushButton_update.clicked.connect(self.on_click_update)
        self.pushButton_save.clicked.connect(self.on_click_save)

    def on_click_submit(self):
        p = self.get_parameters()
        print("PARSED PARAMETERS ARE:")
        for i in p:
            print(i + ': ' + str(p[i]))
        time.sleep(0.1)

        self._figure = main_function(
            p['time_step'], p['time_cap'], p['gas_emissivity'], p['vent_mass_flow_rate'],
            p['specimen_exposed_area'], p['specimen_volume'], p['specimen_heat_of_combustion'], p['specimen_burning_rate'], p['specimen_density'],
            p['lining_surface_emissivity'], p['lining_surface_area'], p['lining_thickness'], p['lining_thermal_conductivity'], p['lining_density'], p['lining_specific_heat']
        )
        self.add_mpl(self._figure.figure)
        self.on_click_save()
        return self.get_parameters()

    def on_click_update(self):
        self.remove_mpl()
        self.on_click_submit()
        self.on_click_save()

    def on_click_quit(self):
        self.close()

    def on_click_save(self):
        old_size = self._figure.figure.get_size_inches().tolist()

        p = self.get_parameters()
        new_size = [p['figure_size_width']*p['figure_size_scale'], p['figure_size_height']*p['figure_size_scale']]

        self._figure.figure.set_size_inches(new_size)
        self._figure.save_figure("output")
        print("Figure saved at: " + os.getcwd())

        self._figure.figure.set_size_inches(old_size)
        self.canvas.draw()

    def add_mpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def remove_mpl(self):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def update_parameters(self, parameters):
        for key in parameters:
            parameters[key] = str(parameters[key])

        self.lineEdit_timeStep.setText(parameters['time_step'])
        self.lineEdit_timeCap.setText(parameters['time_cap'])
        self.lineEdit_gasEmissivity.setText(parameters['gas_emissivity'])
        self.lineEdit_ventMassFlowRate.setText(parameters['vent_mass_flow_rate'])

        self.lineEdit_specimenExposedArea.setText(parameters['specimen_exposed_area'])
        self.lineEdit_specimenVolume.setText(parameters['specimen_volume'])
        self.lineEdit_specimenHeatOfCombustion.setText(parameters['specimen_heat_of_combustion'])
        self.lineEdit_specimenBurningRate.setText(parameters['specimen_burning_rate'])
        self.lineEdit_specimenDensity.setText(parameters['specimen_density'])

        self.lineEdit_liningSurfaceEmissivity.setText(parameters['lining_surface_emissivity'])
        self.lineEdit_liningSurfaceArea.setText(parameters['lining_surface_area'])
        self.lineEdit_liningThickness.setText(parameters['lining_thickness'])
        self.lineEdit_liningThermalConductivity.setText(parameters['lining_thermal_conductivity'])
        self.lineEdit_liningDensity.setText(parameters['lining_density'])
        self.lineEdit_liningSpecificHeat.setText(parameters['lining_specific_heat'])

        self.lineEdit_figure_size_width.setText(parameters['figure_size_width'])
        self.lineEdit_figure_size_height.setText(parameters['figure_size_height'])
        self.lineEdit_figure_size_scale.setText(parameters['figure_size_scale'])
        pass

    def get_parameters(self):
        # self.lineEdit_timeStep.text()
        # self.lineEdit_timeCap.text()
        # self.lineEdit_gasEmissivity.text()
        # self.lineEdit_ventMassFlowRate.text()
        #
        # self.lineEdit_specimenExposedArea.text()
        # self.lineEdit_specimenVolume.text()
        # self.lineEdit_specimenHeatOfCombustion.text()
        # self.lineEdit_specimenBurningRate.text()
        # self.lineEdit_specimenDensity.text()
        #
        # self.lineEdit_liningSurfaceEmissivity.text()
        # self.lineEdit_liningSurfaceArea.text()
        # self.lineEdit_liningThickness.text()
        # self.lineEdit_liningThermalConductivity.text()
        # self.lineEdit_liningDensity.text()
        # self.lineEdit_liningSpecificHeat.text()

        parameters={
            "time_step": self.lineEdit_timeStep.text(),
            "time_cap": self.lineEdit_timeCap.text(),
            'gas_emissivity': self.lineEdit_gasEmissivity.text(),
            'vent_mass_flow_rate': self.lineEdit_ventMassFlowRate.text(),

            'specimen_exposed_area': self.lineEdit_specimenExposedArea.text(),
            'specimen_volume': self.lineEdit_specimenVolume.text(),
            'specimen_heat_of_combustion': self.lineEdit_specimenHeatOfCombustion.text(),
            'specimen_burning_rate': self.lineEdit_specimenBurningRate.text(),
            'specimen_density': self.lineEdit_specimenDensity.text(),

            'lining_surface_emissivity': self.lineEdit_liningSurfaceEmissivity.text(),
            'lining_surface_area': self.lineEdit_liningSurfaceArea.text(),
            'lining_thickness': self.lineEdit_liningThickness.text(),
            'lining_thermal_conductivity': self.lineEdit_liningThermalConductivity.text(),
            'lining_density': self.lineEdit_liningDensity.text(),
            'lining_specific_heat': self.lineEdit_liningSpecificHeat.text(),

            'figure_size_width': self.lineEdit_figure_size_width.text(),
            'figure_size_height': self.lineEdit_figure_size_height.text(),
            'figure_size_scale': self.lineEdit_figure_size_scale.text(),
        }

        for item in parameters:
            parameters[item] = float(eval(parameters[item]))

        return parameters

    def run_simulation(self):
        d=1

    @staticmethod
    def static_default_parameters():
        parameters = {
            'time_step': 0.5,
            'time_cap': '60*60*2',
            'gas_emissivity': 0.7,
            'vent_mass_flow_rate': 0.0735,

            'specimen_exposed_area': 1,
            'specimen_volume': 0.4,
            'specimen_heat_of_combustion': '19e6',
            'specimen_burning_rate': '0.7/1000./60.',
            'specimen_density': 640,

            'lining_surface_emissivity': 0.7,
            'lining_surface_area': '(4*3+3*1.5+1.5*4)*2',
            'lining_thickness': .05,
            'lining_thermal_conductivity': 1.08997,
            'lining_density': 1700,
            'lining_specific_heat': 820,

            'figure_size_width': 10,
            'figure_size_height': 7.5,
            'figure_size_scale': 0.6,
        }
        return parameters
예제 #28
0
class GuiProgram(Ui_sweepergui):

    cols = 0
    sweep_id = 0
    full_data = []
    negative_current = False
    log_sweep = True
    stop = True
    decade_combo_values = ['5', '10', '25', '50']

    def __init__(self, dialog):
        Ui_sweepergui.__init__(self)
        self.setupUi(dialog)

        # Connect "add" button with a custom function (addInputTextToListbox)
        self.exportButton.clicked.connect(self.export_data)
        self.startBtn.clicked.connect(self.startFunc)
        self.logRadioButton.clicked.connect(self.switch_linear)
        self.linRadioButton.clicked.connect(self.switch_linear)

        # Populate comboBox
        self.decadeComboBox.clear()
        self.decadeComboBox.addItems(self.decade_combo_values)
        self.decadeComboBox.setCurrentIndex(1)

        # Try to load last parameters from Pickle
        self.load_parameters()

        # Připojení k instrumentu
        #self.inst = Instrument('GPIB0::17::INSTR', visa_location='C:\WINDOWS\SysWOW64\\visa32.dll')
        self.inst = Instrument('GPIB0::17::INSTR', virtual=False)

    def artSleep(self, sleepTime):
        """
        Čeká čas sleepTime v sekundách, zatím ale každých 50 milisekund řeší
        akce, o které se někdo pokoušel v GUI.
        """
        stop_time = QtCore.QTime()
        stop_time.restart()
        while stop_time.elapsed() < sleepTime * 1000:
            QtWidgets.QApplication.processEvents(QtCore.QEventLoop.AllEvents,
                                                 50)

    def plot_figure(self):
        fig1 = Figure()
        ax1f1 = fig1.add_subplot(111)
        ax1f1.plot(np.random.rand(5))
        self.rmmpl()
        self.addmpl(fig1)

    def plot_data(self, data):
        fig1 = Figure()
        ax1f1 = fig1.add_subplot(111)
        ax1f1.set_xlabel('I [A]')
        ax1f1.set_ylabel('U [V]')
        ax1f1.set_title('Sweep ID: ' + self.sweep_id)
        for d in data:
            x, y = d
            ax1f1.plot(x, y, color='red')
            if self.log_sweep:
                ax1f1.set_xscale('log')
            else:
                ax1f1.set_xscale('linear')
        self.rmmpl()
        self.addmpl(fig1)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.canvas.deleteLater()

        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
        self.toolbar.deleteLater()

    def save_parameters(self):
        parameters_dict = {
            'sw_min': str(self.startEdit.text()),
            'sw_max': str(self.endEdit.text()),
            'step': str(self.stepEdit.text()),
            'delay': int(self.delaySpinBox.value()),
            'decade': int(self.decadeComboBox.currentIndex()),
            'n': int(self.pocetMereniBox.value()),
            'log_sweep': self.logRadioButton.isChecked(),
            'lin_sweep': self.linRadioButton.isChecked(),
            'sense_local': self.senseLocalRadioButton.isChecked(),
            'sense_remote': self.senseRemoteRadioButton.isChecked(),
            'col_source': self.sourceCheckBox.checkState(),
            'col_delay': self.delayCheckBox.checkState(),
            'col_measure': self.measureCheckBox.checkState(),
            'col_time': self.timeCheckBox.checkState(),
            'stabilize_time': self.stableSpinBox.value(),
            'sleep_time': self.sleepSpinBox.value()
        }

        # For pickle, the file needs to be opened in binary mode, hence the "wb"
        with open("last_parameters.pickle", "wb") as params_file:
            pickle.dump(parameters_dict, params_file)

    def load_parameters(self):
        try:
            # For pickle, the file needs to be opened in binary mode, hence the "wb"
            with open("last_parameters.pickle", "rb") as params_file:
                parameters_dict = pickle.load(params_file)

            self.startEdit.setText(parameters_dict['sw_min'])
            self.endEdit.setText(parameters_dict['sw_max'])
            self.stepEdit.setText(parameters_dict['step'])
            self.delaySpinBox.setValue(parameters_dict['delay'])
            self.decadeComboBox.setCurrentIndex(parameters_dict['decade'])
            self.pocetMereniBox.setValue(parameters_dict['n'])

            self.logRadioButton.setChecked(parameters_dict['log_sweep'])
            self.linRadioButton.setChecked(parameters_dict['lin_sweep'])
            self.senseLocalRadioButton.setChecked(
                parameters_dict['sense_local'])
            self.senseRemoteRadioButton.setChecked(
                parameters_dict['sense_remote'])

            self.sourceCheckBox.setChecked(parameters_dict['col_source'])
            self.delayCheckBox.setChecked(parameters_dict['col_delay'])
            self.measureCheckBox.setChecked(parameters_dict['col_measure'])
            self.timeCheckBox.setChecked(parameters_dict['col_time'])

            self.stableSpinBox.setValue(parameters_dict['stabilize_time'])
            self.sleepSpinBox.setValue(parameters_dict['sleep_time'])

            self.switch_linear()

            print("Nacteni poslednich parametru uspesne! :)")

        except:
            print("Nacteni poslednich parametru selhalo. :(")

    def validateInput(self, sw_min, sw_max, decade, delay, log_sweep, step):
        # Unit testing :P
        try:
            a = float(sw_min)
            a = float(sw_max)
            if not log_sweep:
                a = float(step)
        except:
            return False

        # Code
        if log_sweep:
            if (float(sw_min) > 0 and float(sw_max) > 0
                    and float(sw_min) != float(sw_max)):
                return True
            else:
                return False
        else:
            # lin sweep
            if (float(sw_min) != float(sw_max)
                    and abs(float(sw_max) - float(sw_min)) > float(step)):
                return True
            else:
                return False

    def stopFunc(self):
        print('ABORT! Attempted to STOP sweep!')
        self.inst.operate(False)
        self.stop = True

    def startFunc(self):
        ''' Posbírá z GUI parametry a odpovídajícím způsobem přeloží.'''
        # vypnout ruční brzdu
        self.stop = False

        # aktuální čas pro ID
        self.sweep_id = '{0:%Y-%m-%d_%H-%M-%S}'.format(datetime.datetime.now())

        sw_min = str(self.startEdit.text())
        sw_max = str(self.endEdit.text())
        step = str(self.stepEdit.text())
        delay = str(self.delaySpinBox.value())
        decade = str(self.decadeComboBox.currentIndex())
        n = self.pocetMereniBox.value()

        self.bigProgBar.setValue(0)
        self.bigProgBar.setMaximum(n)
        self.littleProgBar.setValue(0)

        log_sweep = self.logRadioButton.isChecked()
        lin_sweep = self.linRadioButton.isChecked()
        self.log_sweep = log_sweep

        sense_local = self.senseLocalRadioButton.isChecked()
        sense_remote = self.senseRemoteRadioButton.isChecked()
        self.sense_local = sense_local

        col_source = self.sourceCheckBox.checkState()
        col_delay = self.delayCheckBox.checkState()
        col_measure = self.measureCheckBox.checkState()
        col_time = self.timeCheckBox.checkState()

        self.cols = 0
        if col_source:
            self.cols += 1
        if col_delay:
            self.cols += 2
        if col_measure:
            self.cols += 4
        if col_time:
            self.cols += 8

        if self.validateInput(sw_min, sw_max, decade, delay, log_sweep, step):
            self.save_parameters()
            self.measure(sw_min, sw_max, decade, delay, log_sweep, step, n)
        else:
            print('Input failed validation.')
            self.show_notification('failed_validation')

    def show_notification(self, code):
        if code == 'failed_validation':
            msg = QtWidgets.QMessageBox()
            msg.setIcon(QtWidgets.QMessageBox.Warning)
            msg.setText("Chybné parametry sweepu.")
            msg.setInformativeText("")
            msg.setWindowTitle("Sweeper - varování")
            msg.setDetailedText("TODO")
            msg.exec_()

    def measure(self, sw_min, sw_max, decade, delay, log_sweep, step, n):
        ''' Spustí měření s již validovanými parametry. '''
        # Disable UI
        self.enable_ui(False)

        # Declare Export not done on this sweep
        self.exportButton.setText('Export')

        # Local or Remote sense?
        if self.sense_local:
            print("Zapinam Sense - LOCAL.")
            self.inst.write("O0X")
        else:
            print("Zapinam Sense - REMOTE.")
            self.inst.write("O1X")

        # Úvodní stabilizace
        stabilize_time = self.stableSpinBox.value()
        print("Uvodni stabilizace v DC modu - {} s.".format(stabilize_time))
        self.stabilize(sw_min, stabilize_time)

        # Nastavení sweepu
        self.inst.set_source_and_function('I', 'Sweep')
        # Nastavení formátu dat
        self.inst.write("G" + str(self.cols) + ",2,2X")

        if self.log_sweep:
            self.inst.write("Q2," + sw_min + "," + sw_max + "," + decade +
                            ",0," + delay + "X")
        else:
            self.inst.write("Q1," + sw_min + "," + sw_max + "," + step +
                            ",0," + delay + "X")

        data = []
        self.full_data = []
        n_hotovych_mereni = 0
        for mereni in range(n):
            if self.stop:
                break
            output = self.run_sweep()
            if output:
                sweep_results = misc.nice_format(output, cols=self.cols)
                data.append(misc.unpack(sweep_results, cols=self.cols))
                self.full_data.append(sweep_results)

                print('Ukladam docasna data...')
                self.dump_data()

                n_hotovych_mereni += 1
                self.bigProgBar.setValue(n_hotovych_mereni)
                self.plot_data(data)

                if n_hotovych_mereni != n:
                    sleep_time = self.sleepSpinBox.value()
                    print("Pauza mezi sweepy - {} s.".format(sleep_time))
                    self.artSleep(sleep_time)

            else:
                print("Output was empty. Interrupted measurement?")

        self.inst.operate(False)
        self.enable_ui(True)
        #self.plot_data(data)

    def run_sweep(self):
        ''' Provede jeden sweep, který už musí být definovaný ve stroji.
            Na konci 3 vteřiny spí, aby se mělo napětí čas ustálit před dalším měřením.

            Vrací string se všemi hodnotami sweepu.
        '''
        print('\nSpoustim sweep...')
        self.inst.write("U8X")
        out = self.inst.read()
        print('U8X -> ' + out)
        out = out.replace('\r', '').replace('\n', '')
        print("Out: {}".format(out))
        sweep_defined_size = int(out[-4:])
        print('Pocet bodu ve sweepu: ' + str(sweep_defined_size))

        self.littleProgBar.setValue(0)
        self.inst.trigger()  # Immediate trigger
        sweep_done = False
        while not sweep_done:
            if self.stop:
                break
            self.artSleep(0.2)
            self.inst.write("U11X")
            status = self.inst.read()
            if (status == 'SMS' + str(sweep_defined_size).zfill(4) + '\r\n'):
                sweep_done = True
            else:
                status_edit = status.replace('\r', '').replace('\n', '')
                try:
                    progress = int(status_edit[-4:])
                    self.littleProgBar.setValue(
                        int(progress / sweep_defined_size * 100))
                except:
                    print('Invalid progress!')
        print('Jeden sweep hotov.')
        self.littleProgBar.setValue(100)

        if self.stop:
            return ""
        else:
            return self.inst.read()

    def stabilize(self, bias, stabilize_time):
        # Počáteční stabilizace
        self.inst.set_source_and_function('I', 'DC')
        self.inst.write("B" + bias + ",0,20X")
        self.inst.operate(True)
        self.inst.trigger()
        self.artSleep(stabilize_time)

    def switch_linear(self):
        log_sweep = self.logRadioButton.isChecked()
        lin_sweep = self.linRadioButton.isChecked()
        if log_sweep:
            self.stepEdit.setEnabled(False)
            self.decadeComboBox.setEnabled(True)
        else:
            self.decadeComboBox.setEnabled(False)
            self.stepEdit.setEnabled(True)

    def enable_ui(self, status):
        ui_elements = [
            self.startEdit,
            self.endEdit,
            self.stepEdit,
            self.delaySpinBox,
            self.decadeComboBox,
            self.pocetMereniBox,
            self.logRadioButton,
            self.linRadioButton,
            self.senseLocalRadioButton,
            self.senseRemoteRadioButton,
            self.sourceCheckBox,
            self.delayCheckBox,
            self.measureCheckBox,
            self.timeCheckBox,
            self.stableSpinBox,
            self.sleepSpinBox,
        ]

        for element in ui_elements:
            element.setEnabled(status)

        if status:
            self.switch_linear()

        if status:
            self.startBtn.clicked.disconnect()
            self.startBtn.clicked.connect(self.startFunc)
            self.startBtn.setText("Start")
        else:
            self.startBtn.clicked.disconnect()
            self.startBtn.clicked.connect(self.stopFunc)
            self.startBtn.setText("Abort")
            self.exportButton.setText('Export')

    def get_export_data(self):
        output = ""
        output += '# ======== Sweep ID: ' + self.sweep_id + ' ========' + '\n'
        output += '# ======== ' + str(len(
            self.full_data)) + ' sweeps' + ' ========' + '\n'

        output += self.get_measurement_parameters()

        for data in self.full_data:
            output += '# ======== SWEEP START ========\n'
            output += data.replace(',', '    ')
            output += '# ======== SWEEP END ========\n\n'

        return output

    def dump_data(self):
        save_file_name = 'C:\Repa\k237\sweeper\data_temp.txt'
        try:
            with open(save_file_name, "w", encoding="utf-8") as text_file:
                text = self.get_export_data()
                text_file.write(text)
        except:
            print("Nouzovy dump se nepovedl.")

    def export_data(self):
        """
        Exportuje data pomocí ukládacího dialogu Qt. V případě chybného zadání
        souboru nic neudělá a postěžuje si do konzole.
        """
        proposed_name = self.sweep_id

        # Qt Dialog na výběr souboru k uložení
        save_file_name, _ = QtWidgets.QFileDialog.getSaveFileName(
            None, 'Exportovat výsledky měření',
            'C:\Repa\k237\data\sweep_' + proposed_name + '.txt',
            'Text Files (*.txt);;All Files (*)')

        # Vlastní uložení souboru
        try:
            with open(save_file_name, "w", encoding="utf-8") as text_file:
                text = self.get_export_data()
                text_file.write(text)

            # Update GUI
            self.exportButton.setText('Export ✔')
        except:
            print("Export neuspesny. Data pravdepodobne nejsou ulozena!")

            # Update GUI
            self.exportButton.setText('Export ✗')

    def get_measurement_parameters(self):
        out = ''
        out += "# Sweep ID: {}\n".format(self.sweep_id)
        if self.log_sweep:
            out += "# Log sweep:\n"
            out += "# Rozsah (min, max) [A]: {}, {}\n".format(
                self.startEdit.text(), self.endEdit.text())
            out += "# Bodů na dekádu [-]: {}\n".format(
                self.decade_combo_values[self.decadeComboBox.currentIndex()])
        else:
            out += "# Linear sweep\n"
            out += "# Rozsah (min, max, points) [A]: {}, {}, {}\n".format(
                self.startEdit.text(), self.endEdit.text(),
                self.stepEdit.text())

        out += "# Delay [ms]: {}\n".format(self.delaySpinBox.value())
        out += "# Počet charakteristik [-]: {}\n".format(
            self.pocetMereniBox.value())

        if self.sense_local:
            out += "# Sense: local\n"
        else:
            out += "# Sense: remote\n"

        out += "# Sloupce (Source, Measure, Delay, Time): {} {} {} {}\n".format(
            int(self.sourceCheckBox.checkState() / 2),
            int(self.measureCheckBox.checkState() / 2),
            int(self.delayCheckBox.checkState() / 2),
            int(self.timeCheckBox.checkState() / 2))

        out += "# Uvodní DC stabilizace [s]: {}\n".format(
            self.stableSpinBox.value())
        out += "# Stabilizace [s]: {}\n".format(self.sleepSpinBox.value())

        return out
class MainWindow(QtWidgets.QMainWindow, form_class):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.cellpoints = np.array([])
        self.FindCells.clicked.connect(self.Id_cells)
        self.AddClassified.clicked.connect(self.create_csv)
        self.imageviewbutton.clicked.connect(self.openMainFig)
        self.numLayers.valueChanged.connect(self.redrawLayers)
        self.maxSigSpin.valueChanged.connect(self.Id_cells)
        self.minSigSpin.valueChanged.connect(self.Id_cells)
        self.log_overlap.valueChanged.connect(self.Id_cells)
        self.thresholdSpin.valueChanged.connect(self.Id_cells)
        self.cropsize = 25
        self.fig = Figure()
        self.THEimage = np.array([])
        self.BLUEimage = 0
        self.BLUEblobs = np.array([])
        self.REDimage = 0
        self.GREENimage = 0
        self.THEblobs = np.array([])
        self.table.setColumnCount(6)
        self.layout.addWidget(self.table, 1, 0)
        self.table.setHorizontalHeaderLabels([
            'Layer', 'Fluorescent cell count', 'Area', 'Density',
            'Nuclei count', 'Fluorescent fraction'
        ])
        for num, layer in enumerate(
            [str(x + 1) for x in range(int(self.numLayers.text()))] +
            ['Total selected reg', 'Total image']):
            self.table.insertRow(num)
            self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
            self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))

        self.directory = 'singleCells/'
        self.guidePoints = {'TR': 0, 'TL': 0, 'BL': 0, 'BR': 0}
        self.innergridRight = [
            (self.guidePoints['TR'] * i + self.guidePoints['BR'] *
             (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
            for i in range(1,
                           int(self.numLayers.text()) + 1)
        ]
        self.innergridLeft = [
            (self.guidePoints['TL'] * i + self.guidePoints['BL'] *
             (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
            for i in range(1,
                           int(self.numLayers.text()) + 1)
        ]
        self.polygonList = []
        self.bigpoligon = 0
        self.figname = 0
        self.imgPolygon = 0
        #self.saveDir.setText('singleCells/')

    def openDIRwindow(self):
        dirwindow = allDirectoriesWindow(self)
        dirwindow.exec_()

    def removeCell(self, cellnumber):
        self.THEblobs[cellnumber:-1] = self.THEblobs[cellnumber + 1:]
        self.THEblobs = self.THEblobs[:-1]
        self.ImgAddPatches()

    def chooseDirectory(self):
        directory = QtWidgets.QFileDialog.getExistingDirectory(self)
        self.saveDir.setText(str(directory) + '/')
        self.DatabaseSize.setText(
            str(len(glob.glob(str(self.saveDir.text()) + '*.png'))))

    def openMainFig(self):
        if self.THEimage.any() == True:
            self.rmmpl()
            self.THEimage = np.array([])
            self.BLUEimage = 0
            while self.table.rowCount() < int(self.numLayers.text()) + 2:
                self.table.insertRow(0)
            while self.table.rowCount() > int(self.numLayers.text()) + 2:
                self.table.removeRow(0)
            for num, layer in enumerate(
                [str(x + 1) for x in range(int(self.numLayers.text()))] +
                ['Total selected reg', 'Total image']):
                self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
                self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))
            self.directory = 'singleCells/'
            self.guidePoints = {'TR': 0, 'TL': 0, 'BL': 0, 'BR': 0}
            self.innergridRight = [
                (self.guidePoints['TR'] * i + self.guidePoints['BR'] *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(1,
                               int(self.numLayers.text()) + 1)
            ]
            self.innergridLeft = [
                (self.guidePoints['TL'] * i + self.guidePoints['BL'] *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(1,
                               int(self.numLayers.text()) + 1)
            ]
            self.polygonList = []
            self.bigpoligon = 0
            self.nMarkedCells.setText(str(0))
            self.THEblobs = np.array([])

        name = QtWidgets.QFileDialog.getOpenFileName(
            self, 'Single File', '~/Desktop/',
            "Image files (*.jpg *.png *.tif)")
        self.figname = str(name[0])
        image = imageio.imread(self.figname)
        #self.saveNames.setText(str(name).split("/")[-1][:-4] + 'i')
        self.THEimage = image
        self.imgPolygon = Polygon([[0, 0], [0, image.shape[1]],
                                   [image.shape[0], image.shape[1]],
                                   [image.shape[0], 0]])
        self.BLUEimage = image[:, :, 2]
        #self.BLUEblobs = blob_log(self.BLUEimage[self.cropsize:-self.cropsize,self.cropsize:-self.cropsize],  max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text()) ,threshold=float(self.thresholdSpin.text()))
        self.REDimage = image[:, :, 0]
        self.GREENimage = image[:, :, 1]
        baseimage = self.fig.add_subplot(111)
        #baseimage.axis('off', frameon=False)
        #baseimage.grid(False)
        #baseimage.imshow(image)
        #axis('off')
        #subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

        self.fig, ax = subplots(1, 1)
        ax.imshow(self.THEimage)
        ax.axis('off')
        subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

        self.canvas = FigureCanvas(self.fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.widget,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)
        cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)

    def onclick(self, event):
        print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
              (event.button, event.x, event.y, event.xdata, event.ydata))
        if event.button == 3:
            if str(self.rClicktype.currentText()) == 'Add cell':
                squaresize = self.cropsize
                #print(len(self.THEblobs))
                self.THEblobs = np.array(
                    self.THEblobs.tolist() +
                    [[int(event.ydata),
                      int(event.xdata), self.cropsize]])
                #print(len(self.THEblobs))
                #self.table.setHorizontalHeaderLabels(['index', 'auto class', 'manual class'])
                #rowPosition = self.table.rowCount()
                #self.table.insertRow(rowPosition)

                self.nMarkedCells.setText(
                    str(int(self.nMarkedCells.text()) + 1))
                self.ImgAddPatches()
            if str(self.rClicktype.currentText()) == 'Add 1st box corner':
                self.guidePoints['TR'] = [int(event.ydata), int(event.xdata)]
                self.rClicktype.setCurrentIndex == 'Add 2nd box corner'

                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()

            if str(self.rClicktype.currentText()) == 'Add 2nd box corner':
                self.guidePoints['TL'] = [int(event.ydata), int(event.xdata)]
                self.rClicktype.setCurrentIndex == 'Add 3rd box corner'

                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()

            if str(self.rClicktype.currentText()) == 'Add 3rd box corner':
                self.guidePoints['BL'] = [int(event.ydata), int(event.xdata)]
                self.rClicktype.setCurrentIndex == 'Add 4th box corner'

                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()

            if str(self.rClicktype.currentText()) == 'Add 4th box corner':
                self.guidePoints['BR'] = [int(event.ydata), int(event.xdata)]
                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    #print(self.bigpoligon)
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()
            if str(self.rClicktype.currentText()) == 'Remove cell':
                dist = np.sum(
                    (self.THEblobs[:, 0:2] - [event.ydata, event.xdata])**2, 1)
                if min(dist) < 800:
                    line = dist.tolist().index(min(dist))
                    #print(line)
                    self.removeCell(line)
                    self.nMarkedCells.setText(
                        str(int(self.nMarkedCells.text()) - 1))
                #self.ImgAddPatches()

        elif event.button == 2:
            #print(self.THEblobs[:,0:2])
            dist = np.sum(
                (self.THEblobs[:, 0:2] - [event.ydata, event.xdata])**2, 1)
            if min(dist) < 800:
                line = dist.tolist().index(min(dist))
                #print(line)
                self.removeCell(line)
                self.nMarkedCells.setText(
                    str(int(self.nMarkedCells.text()) - 1))
            #self.ImgAddPatches()

    def redrawLayers(self):
        if 0 not in self.guidePoints.values():
            self.polygonList = []
            self.innergridRight = [
                (array(self.guidePoints['TR']) * i +
                 array(self.guidePoints['BR']) *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(0,
                               int(self.numLayers.text()) + 1)
            ]
            self.innergridLeft = [
                (array(self.guidePoints['TL']) * i +
                 array(self.guidePoints['BL']) *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(0,
                               int(self.numLayers.text()) + 1)
            ]
            #print(self.innergridLeft, self.innergridRight)
            self.bigpoligon = Polygon([
                self.guidePoints['TR'], self.guidePoints['TL'],
                self.guidePoints['BL'], self.guidePoints['BR']
            ])
            #print(self.bigpoligon)
            for i in range(len(self.innergridLeft) - 1):
                self.polygonList += [
                    Polygon([
                        self.innergridRight[i], self.innergridLeft[i],
                        self.innergridLeft[i + 1], self.innergridRight[i + 1]
                    ])
                ]
        self.ImgAddPatches()

    def changeFIGURE(self, newFIG):
        self.rmmpl()
        self.canvas = FigureCanvas(newFIG)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.widget,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)
        cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def Id_cells(self):
        if type(self.BLUEimage) == type(0): return
        while self.table.rowCount() < int(self.numLayers.text()) + 2:
            self.table.insertRow(0)
        while self.table.rowCount() > int(self.numLayers.text()) + 2:
            self.table.removeRow(0)
        for num, layer in enumerate(
            [str(x + 1) for x in range(int(self.numLayers.text()))] +
            ['Total selected reg', 'Total image']):
            self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
            self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))

        squaresize = self.cropsize
        image_gray = self.BLUEimage

        self.BLUEblobs = blob_log(self.BLUEimage,
                                  max_sigma=int(self.maxSigSpin.text()),
                                  num_sigma=10,
                                  min_sigma=int(self.minSigSpin.text()),
                                  overlap=float(self.log_overlap.text()),
                                  threshold=float(self.thresholdSpin.text()),
                                  exclude_border=squaresize)
        self.table.setItem(
            int(self.numLayers.text()) + 1, 4,
            QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if str(self.fMarker.currentText()) == 'RFP':
            blobs = blob_log(self.REDimage,
                             max_sigma=int(self.maxSigSpin.text()),
                             num_sigma=10,
                             min_sigma=int(self.minSigSpin.text()),
                             overlap=float(self.log_overlap.text()),
                             threshold=float(self.thresholdSpin.text()),
                             exclude_border=squaresize)
            self.table.setItem(
                int(self.numLayers.text()) + 1, 4,
                QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if str(self.fMarker.currentText()) == 'GFP':
            blobs = blob_log(self.GREENimage,
                             max_sigma=int(self.maxSigSpin.text()),
                             num_sigma=10,
                             min_sigma=int(self.minSigSpin.text()),
                             overlap=float(self.log_overlap.text()),
                             threshold=float(self.thresholdSpin.text()),
                             exclude_border=squaresize)
            self.table.setItem(
                int(self.numLayers.text()) + 1, 4,
                QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if str(self.fMarker.currentText()) == 'GFP or RFP':
            blobs = blob_log(self.REDimage + self.GREENimage,
                             max_sigma=int(self.maxSigSpin.text()),
                             num_sigma=10,
                             min_sigma=int(self.minSigSpin.text()),
                             overlap=float(self.log_overlap.text()),
                             threshold=float(self.thresholdSpin.text()),
                             exclude_border=squaresize)
            self.table.setItem(
                int(self.numLayers.text()) + 1, 4,
                QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        #blobsDAPI = blob_log(self.BLUEimage[squaresize:-squaresize,squaresize:-squaresize],  max_sigma=10, num_sigma=10, min_sigma = 3, threshold=.1)
        self.THEblobs = blobs
        self.nMarkedCells.setText(str(len(blobs)))
        self.table.setItem(
            int(self.numLayers.text()) + 1, 1,
            QtWidgets.QTableWidgetItem(str(len(blobs))))
        #self.table.setItem(9 , 2, QtWidgets.QTableWidgetItem(str(len(blobsDAPI))))
        if float(self.table.item(int(self.numLayers.text()) + 1,
                                 2).text()) != 0:
            self.table.setItem(
                int(self.numLayers.text()) + 1, 3,
                QtWidgets.QTableWidgetItem(
                    str(
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            1).text()) /
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            2).text()))))
        self.ImgAddPatches()

    def ImgAddPatches(self):
        colors = ['w', 'r', 'g', 'y', 'w', 'r', 'g', 'y', 'orange', 'w', 'r'
                  ] * 100
        squaresize = self.cropsize
        close(self.fig)
        self.fig, ax = subplots(1, 1)
        ax.imshow(self.THEimage)
        ax.axis('off')
        subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

        while self.table.rowCount() < int(self.numLayers.text()) + 2:
            self.table.insertRow(0)
        while self.table.rowCount() > int(self.numLayers.text()) + 2:
            self.table.removeRow(0)
        for num, layer in enumerate(
            [str(x + 1) for x in range(int(self.numLayers.text()))] +
            ['Total selected reg', 'Total image']):
            self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
            self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))

        self.table.setItem(
            int(self.numLayers.text()) + 1, 1,
            QtWidgets.QTableWidgetItem(str(len(self.THEblobs))))
        self.table.setItem(
            int(self.numLayers.text()) + 1, 4,
            QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if float(self.table.item(int(self.numLayers.text()) + 1,
                                 4).text()) > 0:
            self.table.setItem(
                int(self.numLayers.text()) + 1, 5,
                QtWidgets.QTableWidgetItem(
                    str(
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            1).text()) /
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            4).text()))[:10]))

        if 0 not in self.guidePoints.values():
            ctr = 0
            polygonListCount = array([0 for i in self.polygonList])
            #print('pollistcount before:'+str(polygonListCount))
            for number, blob in enumerate(self.THEblobs):
                y, x, r = blob
                blobPoint = Point(y, x)
                if self.bigpoligon.contains(blobPoint):
                    ctr += 1
                    whichpolygon = [
                        1 if x.contains(blobPoint) else 0
                        for x in self.polygonList
                    ]
                    polygonListCount += array(whichpolygon)
                    #print('pollistcount:'+str(polygonListCount))
                    #c = Rectangle((x + int(squaresize/2), y + int(squaresize/2)),squaresize,squaresize, color=colors[whichpolygon.index(1)], linewidth=.5, alpha = 0.3)
                    c = Circle((x, y),
                               r + 10 if r + 10 < squaresize else squaresize,
                               color=colors[whichpolygon.index(1)],
                               linewidth=.5,
                               alpha=0.3)
                    ax.add_patch(c)
                    ax.text(x,
                            y,
                            polygonListCount[whichpolygon.index(1)],
                            color='white',
                            fontsize=10)
            self.nMarkedCells.setText(str(ctr))
            self.table.setItem(
                int(self.numLayers.text()) + 1, 2,
                QtWidgets.QTableWidgetItem(
                    str(self.imgPolygon.area / self.bigpoligon.area)[:4]))
            self.table.setItem(
                int(self.numLayers.text()), 2,
                QtWidgets.QTableWidgetItem(
                    str(int(self.bigpoligon.area / self.bigpoligon.area))))
            self.table.setItem(int(self.numLayers.text()), 1,
                               QtWidgets.QTableWidgetItem(str(ctr)))

            self.table.setItem(int(self.numLayers.text()), 3,
                               QtWidgets.QTableWidgetItem(str(ctr)))
            self.table.setItem(
                int(self.numLayers.text()) + 1, 3,
                QtWidgets.QTableWidgetItem(
                    str(
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            1).text()) /
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            2).text()))[:6]))

            for n, pol in enumerate(self.polygonList):
                self.table.setItem(
                    n, 2,
                    QtWidgets.QTableWidgetItem(
                        str(pol.area / self.bigpoligon.area)[:4]))
                self.table.setItem(
                    n, 3,
                    QtWidgets.QTableWidgetItem(
                        str(polygonListCount[n] /
                            (pol.area / self.bigpoligon.area))[:6]))
                self.table.setItem(
                    n, 1, QtWidgets.QTableWidgetItem(str(polygonListCount[n])))

            #### add blue cells to dapi count
            ctrDAPI = 0
            polygonListCountDAPI = array([0 for i in self.polygonList])
            for number, blob in enumerate(self.BLUEblobs):
                y, x, r = blob
                blobPoint = Point(y, x)
                if self.bigpoligon.contains(blobPoint):
                    ctrDAPI += 1
                    whichpolygonDAPI = [
                        1 if x.contains(blobPoint) else 0
                        for x in self.polygonList
                    ]
                    polygonListCountDAPI += array(whichpolygonDAPI)

            self.table.setItem(int(self.numLayers.text()), 4,
                               QtWidgets.QTableWidgetItem(str(ctrDAPI)))
            if float(self.table.item(int(self.numLayers.text()),
                                     4).text()) > 0:
                self.table.setItem(
                    int(self.numLayers.text()), 5,
                    QtWidgets.QTableWidgetItem(
                        str(
                            float(
                                self.table.item(int(self.numLayers.text()),
                                                1).text()) / ctrDAPI)))

            for n, pol in enumerate(self.polygonList):
                self.table.setItem(
                    n, 5,
                    QtWidgets.QTableWidgetItem(
                        str(polygonListCount[n] /
                            polygonListCountDAPI[n])[:6]))
                self.table.setItem(
                    n, 4,
                    QtWidgets.QTableWidgetItem(str(polygonListCountDAPI[n])))

        if 0 in self.guidePoints.values():
            for number, blob in enumerate(self.THEblobs):
                y, x, r = blob
                #c = Rectangle((x + int(squaresize/2), y + int(squaresize/2)),squaresize,squaresize, color='gray', linewidth=.5, alpha = 0.3)
                c = Circle((x, y),
                           r + 10 if r + 10 < squaresize else squaresize,
                           color='gray',
                           linewidth=.5,
                           alpha=0.3)
                ax.add_patch(c)
                ax.text(x, y, str(number), color='white', fontsize=6)
        for number, key in enumerate(self.guidePoints):
            if self.guidePoints[key] != 0:
                ax.add_patch(
                    Circle(self.guidePoints[key][::-1],
                           int(self.numLayers.text()),
                           color='w',
                           linewidth=2,
                           fill=True))

        if self.guidePoints['TR'] != 0 and self.guidePoints['TL'] != 0:
            ax.plot([self.guidePoints['TR'][1], self.guidePoints['TL'][1]],
                    [self.guidePoints['TR'][0], self.guidePoints['TL'][0]],
                    '-',
                    color='w',
                    linewidth=2)
        if self.guidePoints['TL'] != 0 and self.guidePoints['BL'] != 0:
            ax.plot([self.guidePoints['TL'][1], self.guidePoints['BL'][1]],
                    [self.guidePoints['TL'][0], self.guidePoints['BL'][0]],
                    '-',
                    color='w',
                    linewidth=2)
        if self.guidePoints['BR'] != 0 and self.guidePoints['BL'] != 0:
            ax.plot([self.guidePoints['BR'][1], self.guidePoints['BL'][1]],
                    [self.guidePoints['BR'][0], self.guidePoints['BL'][0]],
                    '-',
                    color='w',
                    linewidth=2)
        if self.guidePoints['TR'] != 0 and self.guidePoints['BR'] != 0:
            ax.plot([self.guidePoints['TR'][1], self.guidePoints['BR'][1]],
                    [self.guidePoints['TR'][0], self.guidePoints['BR'][0]],
                    '-',
                    color='w',
                    linewidth=2)

        if 0 not in self.guidePoints.values():
            for i in range(len(self.innergridLeft)):
                ax.plot([self.innergridRight[i][1], self.innergridLeft[i][1]],
                        [self.innergridRight[i][0], self.innergridLeft[i][0]],
                        '-',
                        color='w',
                        linewidth=1)
        ax.axis('off')
        subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
        for item in [self.fig, ax]:
            item.patch.set_visible(False)
        self.changeFIGURE(self.fig)

    def create_csv(self):
        layer = np.array([
            str(self.table.item(i, 0).text())
            for i in range(self.table.rowCount())
        ])
        fcells = np.array([
            str(self.table.item(i, 1).text())
            for i in range(self.table.rowCount())
        ])
        area = np.array([
            str(self.table.item(i, 2).text())
            for i in range(self.table.rowCount())
        ])
        density = np.array([
            str(self.table.item(i, 3).text())
            for i in range(self.table.rowCount())
        ])
        nucleiCount = np.array([
            str(self.table.item(i, 4).text())
            for i in range(self.table.rowCount())
        ])
        transfectedRatio = np.array([
            str(self.table.item(i, 5).text())
            for i in range(self.table.rowCount())
        ])
        classtable = DataFrame(
            np.transpose(
                np.vstack((layer, fcells, area, density, nucleiCount,
                           transfectedRatio
                           ))))  #, index=dates, columns=[nome , classe])
        print(classtable)
        saveclassification = classtable.to_csv(self.figname + '_count.csv',
                                               index=False,
                                               header=[
                                                   'layers',
                                                   'Fluorescent cells', 'Area',
                                                   'Density',
                                                   'DAPI cell count',
                                                   'Transfection efficiency'
                                               ])
예제 #30
0
class plsplotDlg(QtWidgets.QDialog,Ui_plsplotDialog):
    def __init__(self,parent=None):
        super(plsplotDlg,self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window |QtCore.Qt.CustomizeWindowHint |
            QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint |
            QtCore.Qt.WindowMaximizeButtonHint)
        for x in range(PLS.ncp):
            self.XcomboBox.addItem(str(x+1))
            self.YcomboBox.addItem(str(x+1))
        Lc=DS.Lc[DS.Ic]
        Gc=DS.Gc[DS.Ic]
        Lcy=Lc[Gc]
        for x in Lcy:
            self.responcecomboBox.addItem(str(x))
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(1)
        self.VcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig=Figure()
        ax=fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0,1])
        ax.set_ylim([0,1])
        self.addmpl(fig)
    def redraw(self):
        fig=Figure()
        ax=fig.add_subplot(111)
        c1=self.XcomboBox.currentIndex()
        c2=self.YcomboBox.currentIndex()
        ny=self.responcecomboBox.currentIndex()
        Yname=self.responcecomboBox.currentText()
        Ts=DS.Ts[DS.Ir]
        Gc=DS.Gc[DS.Ic]
        Cr=DS.Cr[DS.Ir]
        Cr=Cr[-Ts]
        Lr=DS.Lr[DS.Ir] 
        Lr=Lr[-Ts]
        Lc=DS.Lc[DS.Ic]
        Lcx=Lc[-Gc]
        Lcy=Lc[Gc]
        Cc=DS.Cc[DS.Ic]
        Ccx=Cc[-Gc]
        ncx=len(Lcx)
        ncy=len(Lcy)
        nr=len(Lr)
        Y=DS.Raw.loc[DS.Ir,DS.Ic][Lcy]
        Y=Y[-Ts]
        if(self.fittedradioButton.isChecked()):
            Y=Y.values
            ax.scatter(Y[:,ny],PLS.Ys[:,ny],color=Cr,alpha=0.3,marker='o')
            xmin,xmax=ax.get_xlim()
            ax.set_ylim([xmin,xmax])
            ax.set_xlabel(Yname+' measured')
            ax.set_ylabel(Yname+' fitted')
            ax.set_title('Fitted vs. Measured Plot for '+Yname+ ' : Model '+str(PLS.ncp)+' components')
            ax.add_line(Line2D([xmin,xmax],[xmin,xmax],color='red'))
            if self.VcheckBox.isChecked():
                for i in range(nr):
                    ax.annotate(Lr[i],(Y[:,c1][i],PLS.Ys[:,c1][i]))
        elif(self.allfittedradioButton.isChecked()):
            Yc=pd.melt(pd.DataFrame(PLS.Yc))
            Ysc=pd.melt(pd.DataFrame(PLS.Ysc))
            df=pd.DataFrame(dict(X=Yc['value'],Y=Ysc['value'],point=Yc['variable']))
            np.random.seed(nr)
            groups=df.groupby('point')
            colors=pd.tools.plotting._get_standard_colors(len(groups),color_type='random')
            ax.set_prop_cycle(cycler('color',colors))
            ax.margins(0.05)
            for group in groups:
                ax.plot(group[1].X,group[1].Y, marker='o',linestyle='',ms=5,label=Lcy[group[0]])
            ax.set_xlabel('Measured Data')
            ax.set_ylabel('Fitted Data')
            Dmin=min([Yc.value.min(),Ysc.value.min()])
            Dmax=max([Yc.value.max(),Ysc.value.max()])
            ax.set_xlim([Dmin,Dmax])
            ax.set_ylim([Dmin,Dmax])
            ax.set_title('Measured vs. Fitted Data')
            ax.add_line(Line2D([Dmin,Dmax],[Dmin,Dmax],color='red')) 
            ax.legend(loc='best') 
        elif(self.coefficientradioButton.isChecked()):
            B=PLS.B[:,ny]
            ind=np.array(range(1,ncx+1))
            if(ncx>30):
                itick=np.linspace(0,ncx-1,20).astype(int)
                ltick=Lcx[itick]
            else:
                itick=ind
                ltick=Lcx
            if self.CcheckBox.isChecked():
                vcol=Ccx
            else:
                vcol='blue'
            ax.bar(ind,B,align='center',color=vcol)
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlim([0,ncx+1])
            ax.set_xlabel('Variables') 
            ax.set_ylabel('Coefficients') 
            ax.set_title('Coefficients for '+Yname+' : Model '+str(PLS.ncp)+' components')
        elif self.wiradioButton.isChecked():
            WS=np.dot(PLS.WS,np.linalg.inv(PLS.C))
            txt=range(1,ncx+1)
            if self.VcheckBox.isChecked():
                txt=Lcx
            vcol=ncx*['blue']
            if self.CcheckBox.isChecked():
                vcol=Ccx
            ax.scatter(WS[:,c1],WS[:,c2],alpha=0.3,color=vcol,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(ncx):
                ax.annotate(txt[i],(WS[:,c1][i],WS[:,c2][i]))
            lim=[WS.min(),0, WS.max()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Weight Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('W*'+str(c1+1))
            ax.set_ylabel('W*'+str(c2+1))
        elif(self.qiradioButton.isChecked()):
            txt=range(1,ncy+1)
            if self.VcheckBox.isChecked():
                txt=Lcx
            vcol=ncy*['blue']
            if self.CcheckBox.isChecked():
                vcol=Ccx
            ax.scatter(PLS.Q[:,c1],PLS.Q[:,c2],alpha=0.3,color=vcol,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(ncy):
                ax.annotate(txt[i],(PLS.Q[:,c1][i],PLS.Q[:,c2][i]))
            lim=[PLS.Q.min(),0,PLS.Q.max()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Loading Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('C'+str(c1+1))
            ax.set_ylabel('C'+str(c2+1))
        elif(self.wqiradioButton.isChecked()):
            WS=np.dot(PLS.WS,np.linalg.inv(PLS.C))
            txtc=range(1,ncy+1)
            txtw=range(1,ncx+1)
            if self.VcheckBox.isChecked():
                txt=DS.Lc[DS.Ic]
                txtc=txt[DS.Gc[DS.Ic]]
                txtw=txt[-DS.Gc[DS.Ic]]
            vcolc=ncy*['red']
            vcolw=ncx*['blue']
            if self.CcheckBox.isChecked():
                vcol=DS.Cc[DS.Ic]
                vcolc=vcol[DS.Gc[DS.Ic]]
                vcolw=vcol[-DS.Gc[DS.Ic]]
            ax.scatter(PLS.Q[:,c1],PLS.Q[:,c2],alpha=0.3,color=vcolc,s=30,marker='o')
            ax.scatter(WS[:,c1],WS[:,c2],alpha=0.3,color=vcolw,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(ncy):
                ax.annotate(txtc[i],(PLS.Q[:,c1][i],PLS.Q[:,c2][i]))
            for i in range(ncx):
                ax.annotate(txtw[i],(WS[:,c1][i],WS[:,c2][i]))
            lim=[PLS.Q.min(),0,PLS.Q.max(),WS.max(),WS.min()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Loading Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('W* Q'+str(c1+1))
            ax.set_ylabel('W* Q'+str(c2+1))
        elif(self.ht2radioButton.isChecked()):
            ind=np.array(range(1,nr+1))
            colors=[]
            for i in range(nr):
                colors.append('blue')
                if(PLS.HT2[i]>PLS.T95):
                    colors[i]='red'
            if(nr>30):
                itick=np.linspace(0,nr-1,20).astype(int)
                ltick=Lr[itick]
            else:
                itick=ind
                ltick=Lr
            ax.bar(ind,PLS.HT2,align='center',color=colors)
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlabel('Object') 
            ax.set_title('Hoteling T2 for '+str(PLS.ncp)+' components model')
            ax.axhline(y=PLS.T95,color='red',linewidth=1.5,zorder=0)
            ax.annotate('95%',xy=(0,PLS.T95),color='red')
            ax.axhline(y=PLS.T99,color='red',linewidth=1.5,zorder=0)
            ax.annotate('99%',xy=(0,PLS.T99),color='red')
            ax.set_ylim([0,1.2*max([PLS.T99,max(PLS.HT2)])])
            ax.set_xlim([0,nr+1])
        elif(self.scoreradioButton.isChecked()):
            st2=PLS.T.var(axis=0)
            alpha=(100-self.alphaspinBox.value())/100
            fn=np.array(range(1,(PLS.ncp+1)))
            fd=(nr+1)-fn
            fn =tuple(fn)
            fd =tuple(fd)
            falpha=sp.stats.f.ppf(alpha,fn,fd)
            colors=[]
            txt=[]
            for i in range(nr):
                txt.append('')
                colors.append('blue')
                rp=(PLS.T[:,c1][i])**2/st2[c1]+(PLS.T[:,c2][i])**2/st2[c2]
                if(rp>falpha[PLS.ncp-1]):
                    colors[i]='red'
                    txt[i]=Lr[i]
            ax.scatter(PLS.T[:,c1],PLS.T[:,c2],alpha=0.3,color=colors,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(nr):
                ax.annotate(txt[i],(PLS.T[:,c1][i],PLS.T[:,c2][i]))
            lim=[PLS.T.min(),0,PLS.T.max()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Score Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('Comp.'+str(c1+1)+' ('+str(round(st2[c1]/PLS.ncp*100,2))+'%)')
            ax.set_ylabel('Comp.'+str(c2+1)+' ('+str(round(st2[c2]/PLS.ncp*100,2))+'%)')
            ella=Ellipse(xy=(0,0),width=2*math.sqrt(falpha[PLS.ncp-1]*st2[c1]),height=2*math.sqrt(falpha[PLS.ncp-1]*st2[c2]),color='red')
            ella.set_facecolor('none')
            ax.add_artist(ella)
            alpha=alpha*100
            ax.annotate(str(round(alpha,0))+'%',xy=(0,-math.sqrt(falpha[PLS.ncp-1]*st2[c2])),color='red')
        elif(self.spexradioButton.isChecked()):
            T95=sp.stats.chi2.ppf(0.95,nr)/(nr-PLS.ncp)
            T99=sp.stats.chi2.ppf(0.99,nr)/(nr-PLS.ncp)
            ind=np.array(range(1,nr+1))
            if(nr>30):
                itick=np.linspace(0,nr-1,20).astype(int)
                ltick=Lr[itick]
            else:
                itick=ind
                ltick=Lr
            ax.bar(ind,PLS.SPEX,color='blue')
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlabel('X Variables') 
            ax.set_ylabel('SPE X') 
            ax.set_title('SPE X for '+str(PLS.ncp)+' components model')
            ax.set_ylim([0,1.2*max([max(PLS.SPEX),T99])])
            ax.set_xlim([0,max(itick)])
            ax.axhline(y=T95,color='red',linewidth=1.5)
            ax.annotate('95%',xy=(0,T95),color='red')
            ax.axhline(y=T99,color='red',linewidth=1.5)
            ax.annotate('99%',xy=(0,T99),color='red')
        elif self.speyradioButton.isChecked():
            T95=sp.stats.chi2.ppf(0.95,nr)/(nr-PLS.ncp)
            T99=sp.stats.chi2.ppf(0.99,nr)/(nr-PLS.ncp)
            ind=np.array(range(1,nr+1))
            if(nr>30):
                itick=np.linspace(0,nr-1,20).astype(int)
                ltick=Lr[itick]
            else:
                itick=ind
                ltick=Lr
            ax.bar(ind,PLS.SPEY,color='blue')
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlabel('Y Variables') 
            ax.set_ylabel('SPE Y') 
            ax.set_title('SPE Y for '+str(PLS.ncp)+' components model')
            ax.set_ylim([0,1.2*max([max(PLS.SPEY),T99])])
            ax.set_xlim([0,max(itick)])
            ax.axhline(y=T95,color='red',linewidth=1.5)
            ax.annotate('95%',xy=(0,T95),color='red')
            ax.axhline(y=T99,color='red',linewidth=1.5)
            ax.annotate('99%',xy=(0,T99),color='red')
        elif self.cvradioButton.isChecked():
            X0=pd.DataFrame(PLS.Xc)
            Y0=pd.DataFrame(PLS.Yc)
            np.random.seed(nr)
            kf=sk.model_selection.KFold(n_splits=self.segmentspinBox.value(),shuffle=True)
            Ycv=pd.DataFrame(0,index=range(nr),columns=range(ncy))
            for train, test in kf.split(X0):
                 X=X0.iloc[train,:]
                 Y=Y0.iloc[train,:]
                 Xm,Ym,Sx,Sy,Xc,Yc,SSX,SSY,P,C,U,T,Q,W,WS,B,Ysc,Ys,R2,SPEX,SPEY,HT2,VIP,T95,T99=pls_model(X,Y,PLS.ncp,False,False,False)
                 X_test=X0.iloc[test,:]
                 Ycv.iloc[test,:]=np.dot(X_test,B)
            Y0=Y0.values
            PLS.Ycv=Ycv.values
            Ycvs=PLS.Ym+PLS.Sy*Ycv.T
            PLS.Ycvs=Ycvs.T.values
            Y=PLS.Ym+PLS.Sy*Y0.T
            Y=Y.T
            if self.CcheckBox.isChecked():
                vcol=Cr
            else:
                vcol='red'
            ax.scatter(Y[:,ny],PLS.Ycvs.iloc[:,ny],color=vcol,alpha=0.3,marker='o')
            if self.VcheckBox.isChecked():       
                for i in range(nr):
                    ax.annotate(Lr[i],(Y[:,ny][i],PLS.Ycvs.iloc[:,ny][i]))
            ax.set_xlabel(Yname+' measured')
            ax.set_ylabel(Yname+' CV predicted')
            xmin,xmax=ax.get_xlim()
            ax.set_ylim([xmin,xmax])
            ax.set_title('CV Predicted vs. Measured Plot for '+Yname+' : Model '+str(PLS.ncp)+' components')
            ax.add_line(Line2D([xmin,xmax],[xmin,xmax],color='red'))
        elif self.turadioButton.isChecked():
            if self.CcheckBox.isChecked():
                vcol=Cr
            else:
                vcol='red'
            ax.scatter(PLS.T[:,c1],PLS.U[:,c1],color=vcol,alpha=0.3,marker='o')
            if self.VcheckBox.isChecked():       
                for i in range(nr):
                    ax.annotate(Lr[i],(PLS.T[:,c1][i],PLS.U[:,c1][i]))
            ax.set_xlabel('T('+str(c1+1)+')')
            ax.set_ylabel('U('+str(c1+1)+')')
            ax.set_title('T vs. U Plot')
        elif self.vipradioButton.isChecked():
            ind=np.array(range(1,ncx+1))
            if(ncx>30):
                itick=np.linspace(0,ncx-1,20).astype(int)
                ltick=Lcx[itick]
            else:
                itick=ind
                ltick=Lcx
            colors=[]
            for i in range(ncx):
                colors.append('blue')
                if(PLS.VIP[i]>1):
                    colors[i]='red'
            ax.bar(ind,PLS.VIP,align='center',color=colors)
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlim([0,max(itick)+1])
            ax.set_xlabel('Variables') 
            ax.yaxis.grid()
            ax.set_title('Variable Importance Plot (VIP): Model '+str(PLS.ncp)+' components')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():    
            ax.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',which='both',left='off',right='off',labelleft='off')
        self.rmmpl()
        self.addmpl(fig)        
    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(1)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.update()        
    def addmpl(self, fig):
        self.canvas=FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar=NavigationToolbar(self.canvas, 
                self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)
    def rmmpl(self,):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #31
0
class plsweightDlg(QtWidgets.QDialog, Ui_plsweightDialog):
    def __init__(self, parent=None):
        super(plsweightDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        for x in range(PLS.ncp):
            self.componentcomboBox.addItem(str(x + 1))
        Lr = DS.Lr[DS.Ir]
        Ts = DS.Ts[DS.Ir]
        for x in Lr[-Ts]:
            self.firstpointcomboBox.addItem(str(x))
            self.secondpointcomboBox.addItem(str(x))
        self.firstpointcomboBox.setCurrentIndex(0)
        self.secondpointcomboBox.setCurrentIndex(1)
        Gr = DS.Gr[DS.Ir]
        Gr = Gr[-Ts]
        Gr = pd.Series(Gr)
        for x in Gr.groupby(Gr).groups.keys():
            self.firstgroupcomboBox.addItem(str(x))
            self.secondgroupcomboBox.addItem(str(x))
        self.VcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        nw = self.componentcomboBox.currentIndex()
        fig = Figure()
        ax = fig.add_subplot(111)
        Gc = DS.Gc[DS.Ic]
        Lc = DS.Lc[DS.Ic]
        Lcx = Lc[-Gc]
        Ccx = DS.Cc[DS.Ic]
        Ccx = Ccx[-Gc]
        ncx = len(Lcx)
        WS = np.dot(PLS.WS, np.linalg.inv(PLS.C))
        ind = np.array(range(1, ncx + 1))
        if (ncx > 30):
            itick = np.linspace(0, ncx - 1, 20).astype(int)
            ltick = Lcx[itick]
        else:
            itick = ind
            ltick = Lcx
        ax.set_xticks(itick)
        ax.set_xticklabels(ltick, rotation='vertical')
        ax.set_xlabel('Variable')
        if self.CcheckBox.isChecked():
            cl = Ccx
        else:
            cl = ncx * ['blue']
        ax.set_ylabel('Variable Contributon on Weight ' + str(nw + 1))
        ax.set_xlim([0, ncx + 1])
        if self.pointradioButton.isChecked():
            p1 = self.firstpointcomboBox.currentIndex()
            p2 = self.secondpointcomboBox.currentIndex()
            x1 = np.array(PLS.Xc[p1, :])
            x2 = np.array(PLS.Xc[p2, :])
            lx = WS[:, nw] * (x1 - x2)
            ax.bar(ind, lx, align='center', width=0.8, color=cl)
            ax.set_title('Contribution of object ' +
                         self.firstpointcomboBox.currentText() +
                         ' vs. object ' +
                         self.secondpointcomboBox.currentText() +
                         ' to Weight ' + str(nw + 1))
        elif self.averageradioButton.isChecked():
            p1 = self.firstpointcomboBox.currentIndex()
            x1 = np.array(PLS.Xc[p1, :])
            lx = WS[:, nw] * x1
            ax.bar(ind, lx, align='center', width=0.8, color=cl)
            ax.set_title('Contribution of object ' +
                         self.firstpointcomboBox.currentText() +
                         ' to Weight ' + str(nw + 1))
        elif self.groupradioButton.isChecked():
            g1 = self.firstgroupcomboBox.currentIndex()
            g2 = self.secondgroupcomboBox.currentIndex()
            Gr = DS.Gr[DS.Ir]
            Gr = Gr[-DS.Ts[DS.Ir]]
            df = pd.DataFrame(PLS.Xc).groupby(Gr).mean()
            Xa = df.values
            x1 = np.array(Xa[g1, :])
            x2 = np.array(Xa[g2, :])
            lx = WS[:, nw] * (x1 - x2)
            ax.bar(ind, lx, align='center', width=0.8, color=cl)
            ax.set_title('Contribution of Group ' +
                         self.firstgroupcomboBox.currentText() +
                         ' vs. Group ' +
                         self.secondgroupcomboBox.currentText() +
                         ' to Weight ' + str(nw + 1))
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.firstpointcomboBox.setCurrentIndex(0)
        self.secondpointcomboBox.setCurrentIndex(1)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
예제 #32
0
class fitmodelDlg(QtWidgets.QDialog, Ui_fitmodel):
    def __init__(self, parent=None):
        super(fitmodelDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.XcomboBox.addItem('None')
        self.YcomboBox.addItem('None')
        listmodel = [
            'cubic: p[0]x^3+p[1]x^2+p[2]x+p[3]',
            'parabolic: p[0]x^2+p[1]x+p[2]', 'linear: p[0]x+p[1]',
            'exponential: p[0]e^(p[1]x)', 'antoine: p[0]e^(p[1]/(p[2]+x))',
            'arrhenius: p[0]e^(-p[1]/x)', 'logarithm: p[0] ln (p[1]*x)',
            'power: p[0] x^p[1]', 'iperbolic: p[0]/(1+p[1]x)',
            'rational: p[0]/(1+p[1] x^2)', 'logistic: p[0]/(1+e^(-p[1] x))',
            'enzimatic: p[0] (x^2+p[1]*x)/(x^2+p[2] x+p[3])'
        ]
        self.McomboBox.addItems(listmodel)
        self.XcomboBox.addItems(DS.getLc())
        self.YcomboBox.addItems(DS.getLc())
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        if self.YcomboBox.currentText(
        ) == 'None' or self.XcomboBox.currentText() == 'None':
            QtWidgets.QMessageBox.critical(
                self, 'Error', "You have to choose both variables!",
                QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.getData()
        x = data[self.XcomboBox.currentText()]
        y = data[self.YcomboBox.currentText()]
        if self.normalizecheckBox.isChecked():
            xscaler = preprocessing.MinMaxScaler().fit(x.reshape(-1, 1))
            x = xscaler.transform(x.reshape(-1, 1))
            yscaler = preprocessing.MinMaxScaler().fit(y.reshape(-1, 1))
            y = yscaler.transform(y.reshape(-1, 1))
            x = np.squeeze(x.T)
            y = np.squeeze(y.T)
        x = x.values.astype('float').ravel()
        y = y.values.astype('float').ravel()
        if self.radioButton_a0.isChecked(): mth = 'trf'
        elif self.radioButton_a1.isChecked(): mth = 'dogbox'
        elif self.radioButton_a2.isChecked(): mth = 'lm'
        p0 = np.array([
            float(self.lineEdit_0.text()),
            float(self.lineEdit_1.text()),
            float(self.lineEdit_2.text()),
            float(self.lineEdit_3.text())
        ])
        bounds = np.array([-np.inf, np.inf])
        if self.lineEdit_low.text() != '':
            bounds[0] = float(self.lineEdit_low.text())
        if self.lineEdit_up.text() != '':
            bounds[1] = float(self.lineEdit_up.text())
        if self.McomboBox.currentIndex() == 0: p0 = p0[:4]
        elif self.McomboBox.currentIndex() == 1: p0 = p0[:3]
        elif self.McomboBox.currentIndex() == 2: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 3: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 4: p0 = p0[:3]
        elif self.McomboBox.currentIndex() == 5: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 6: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 7: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 8: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 9: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 10: p0 = p0[:2]
        elif self.McomboBox.currentIndex() == 11: p0 = p0[:4]
        bounds = (bounds[0], bounds[1])

        def fun(p, x, y):
            return model(p, x) - y

        def model(p, x):
            if self.McomboBox.currentIndex() == 0:
                return p[0] * x**3 + p[1] * x**2 + p[2] * x + p[3]
            elif self.McomboBox.currentIndex() == 1:
                return p[0] * x**2 + p[1] * x + p[2]
            elif self.McomboBox.currentIndex() == 2:
                return p[0] * x + p[1]
            elif self.McomboBox.currentIndex() == 3:
                return p[0] * np.exp(p[1] * x)
            elif self.McomboBox.currentIndex() == 4:
                return p[0] * np.exp(p[1] / (p[2] + x))
            elif self.McomboBox.currentIndex() == 5:
                return p[0] * np.exp(-p[1] / x)
            elif self.McomboBox.currentIndex() == 6:
                return p[0] * np.log(p[1] * x)
            elif self.McomboBox.currentIndex() == 7:
                return p[0] * x**(p[1])
            elif self.McomboBox.currentIndex() == 8:
                return p[0] / (1 + p[1] * x)
            elif self.McomboBox.currentIndex() == 9:
                return p[0] / (1 + p[1] * x**2)
            elif self.McomboBox.currentIndex() == 10:
                return p[0] / (1 + np.exp(-p[1] * x))
            elif self.McomboBox.currentIndex() == 11:
                return p[0] * (x**2 + p[1] * x) / (x**2 + p[2] * x + p[3])

        def jac(p, x, y):
            J = np.empty((x.size, p.size))
            if self.McomboBox.currentIndex() == 0:
                J[:, 0] = x**3
                J[:, 1] = x**2
                J[:, 2] = x
                J[:, 3] = 1
            elif self.McomboBox.currentIndex() == 1:
                J[:, 0] = x**2
                J[:, 1] = x
                J[:, 2] = 1
            elif self.McomboBox.currentIndex() == 2:
                J[:, 0] = x
                J[:, 1] = 1
            elif self.McomboBox.currentIndex() == 3:
                J[:, 0] = np.exp(p[1] * x)
                J[:, 1] = p[0] * x * np.exp(p[1] * x)
            elif self.McomboBox.currentIndex() == 4:
                J[:, 0] = np.exp(p[1] / (p[2] + x))
                J[:, 1] = p[0] * np.exp(p[1] / (p[2] + x)) / (p[2] + x)
                J[:,
                  2] = -p[0] * np.exp(p[1] / (p[2] + x)) * p[1] / (p[2] + x)**2
            elif self.McomboBox.currentIndex() == 5:
                J[:, 0] = np.exp(-p[1] / x)
                J[:, 1] = -p[0] * np.exp(-p[1] / x) / x
            elif self.McomboBox.currentIndex() == 6:
                J[:, 0] = np.log(p[1] * x)
                J[:, 1] = p[0] * x / p[1]
            elif self.McomboBox.currentIndex() == 7:
                J[:, 0] = x**(p[1])
                J[:, 1] = p[0] * x**(p[1]) * np.log(x)
            elif self.McomboBox.currentIndex() == 8:
                J[:, 0] = 1 / (1 + p[1] * x)
                J[:, 1] = p[0] * x / (1 + p[1] * x)**2
            elif self.McomboBox.currentIndex() == 9:
                J[:, 0] = 1 / (1 + p[1] * x**2)
                J[:, 1] = p[0] * x**2 / (1 + p[1] * x)**2
            elif self.McomboBox.currentIndex() == 10:
                J[:, 0] = 1 / (1 + np.exp(-p[1] * x))
                J[:, 1] = p[0] * x * np.exp(
                    -p[1] * x) / (1 + np.exp(-p[1] * x))**2
            elif self.McomboBox.currentIndex() == 11:
                den = x**2 + p[2] * x + p[3]
                num = x**2 + p[1] * x
                J[:, 0] = num / den
                J[:, 1] = p[0] * num * x / den
                J[:, 2] = -p[0] * num * x / den**2
                J[:, 3] = -p[0] * num / den**2
            return J

        try:
            res = least_squares(fun,
                                p0,
                                jac=jac,
                                bounds=bounds,
                                args=(x, y),
                                verbose=0,
                                method=mth,
                                xtol=float(self.lineEdit_xtol.text()),
                                ftol=float(self.lineEdit_ytol.text()))
        except:
            QtWidgets.QMessageBox.critical(
                self, 'Error',
                "Solver crashed! \n something wrong in parameters or bounds set.",
                QtWidgets.QMessageBox.Ok)
            return ()
        FIT.setx(x)
        FIT.sety(y)
        FIT.setp(res.x)
        FIT.setjac(res.jac)
        FIT.setres(res.fun)
        FIT.setfit(True)
        FIT.setmodel(self.McomboBox.currentIndex())
        x_test = np.linspace(x.min(), x.max(), 50)
        y_test = model(res.x, x_test)
        fig = Figure()
        ax = fig.add_subplot(111)
        vcol = ['red'] * len(y)
        if self.CcheckBox.isChecked():
            vcol = DS.getCr()
        ax.scatter(x, y, color=vcol, marker='o', label='data')
        ax.plot(x_test, y_test, label='fitted model')
        ax.set_ylabel(self.YcomboBox.currentText())
        ax.set_xlabel(self.XcomboBox.currentText())
        ax.legend(loc='lower right')
        ax.annotate(res.message,
                    xy=(1, 1),
                    xycoords='axes fraction',
                    fontsize=6,
                    xytext=(0, +15),
                    textcoords='offset points',
                    ha='right',
                    va='top')
        if self.VcheckBox.isChecked():
            for txt in enumerate(DS.getLr()):
                ax.annotate(txt, (x, y))
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        if self.TlineEdit.text():
            ax.set_title(self.TlineEdit.text())
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()