Exemplo n.º 1
0
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_ModelBuilder()
        self.ui.setupUi(self)
        self.Enable_Validation()

        QtCore.QObject.connect(self.ui.pushButton_Execute, QtCore.SIGNAL("clicked()"), self.Execute )
        QtCore.QObject.connect(self.ui.pushButton_pickle, QtCore.SIGNAL("clicked()"), self.Wild)
        QtCore.QObject.connect(self.ui.pushButton_find, QtCore.SIGNAL("clicked()"), self.Find)
        
        QtCore.QObject.connect(self.ui.radioButton_validation_year, QtCore.SIGNAL("clicked()"), self.Enable_Validation)
        QtCore.QObject.connect(self.ui.radioButton_validation_CV, QtCore.SIGNAL("clicked()"), self.Enable_Validation)
Exemplo n.º 2
0
class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_ModelBuilder()
        self.ui.setupUi(self)
        self.Enable_Validation()

        QtCore.QObject.connect(self.ui.pushButton_Execute, QtCore.SIGNAL("clicked()"), self.Execute )
        QtCore.QObject.connect(self.ui.pushButton_pickle, QtCore.SIGNAL("clicked()"), self.Wild)
        QtCore.QObject.connect(self.ui.pushButton_find, QtCore.SIGNAL("clicked()"), self.Find)
        
        QtCore.QObject.connect(self.ui.radioButton_validation_year, QtCore.SIGNAL("clicked()"), self.Enable_Validation)
        QtCore.QObject.connect(self.ui.radioButton_validation_CV, QtCore.SIGNAL("clicked()"), self.Enable_Validation)


    def Find(self):
        data_path = QtGui.QFileDialog.getOpenFileName(self, caption="Open Data File", directory='../../data', filter='*.csv')
        self.ui.lineEdit_path.setText( data_path )
        
    def Enable_Validation(self):
        if self.ui.radioButton_validation_year.isChecked() is True:
            self.ui.lineEdit_year.setEnabled( True )
            self.ui.lineEdit_CV_folds.setEnabled( False )
            
        elif self.ui.radioButton_validation_CV.isChecked() is True:
            self.ui.lineEdit_year.setEnabled( False )
            self.ui.lineEdit_CV_folds.setEnabled( True )


        
    def Wild(self):
        selected = self.ui.tableView.selectedIndexes()[0].row()
        

        method='pls'
        if self.ui.radioButton_PLS.isChecked(): method='pls'
        if self.ui.radioButton_Logistic.isChecked(): method='logistic'
        if self.ui.radioButton_Boost.isChecked(): method='boosting'

        model = model_script.Produce(self.result[selected], method, self.data_dict, self.target)
        
        outfile = 'model.mdl'
        o=open(outfile, 'w')
        model_pickler = pickle.dump(model, o)
        o.close()



 
    def Execute(self):
    
        if self.ui.radioButton_PLS.isChecked(): method='pls'
        if self.ui.radioButton_Logistic.isChecked(): method='logistic'
        if self.ui.radioButton_Boost.isChecked(): method='boosting'

        threshold=[]
        balance=[]
        
        if self.ui.checkBox_Proportions.isChecked():
            threshold.append(0)
            balance.append(1)
        if self.ui.checkBox_Counts.isChecked():
            threshold.append(1)
            balance.append(0)

        infile = str( self.ui.lineEdit_path.text() )
        self.target = model_target = str( self.ui.lineEdit_target.text() ).lower()
        
        specificities = str( self.ui.lineEdit_specificities.text() )
        specificity = [float(limit) for limit in specificities.split(',')]
        
        #If we're training on some years to predict another
        if self.ui.radioButton_validation_year.isChecked():
            validation='year'
            year = int( self.ui.lineEdit_year.text() )
            [headers, result, self.data_dict] = model_script.Test(infile, model_target, method, year=year, specificity=specificity, balance=balance, threshold=threshold)
        
        #If we're cross-validating on random folds of data
        elif self.ui.radioButton_validation_CV.isChecked():
            validation='CV'
            folds = int( self.ui.lineEdit_CV_folds.text() )
            [headers, result, self.data_dict] = model_script.Test_CV(infile, model_target, method, folds=folds, specificity=specificity, balance=balance, threshold=threshold)
        

        self.result=result

        '''if len(result.squeeze().shape)==1: result=list( data )
        else: result = [list(row) for row in result.squeeze()]'''

        self.Populate_Table(result, headers)
        self.ui.pushButton_pickle.setEnabled( True )



    def Populate_Table(self, data, headers):
        # create the view
        tv = self.ui.tableView
        
        # set the table model
        tm = MyTableModel(data, headers, self) 
        tv.setModel(tm)
    
        # set the minimum size
        #tv.setMinimumSize(400, 300)
    
        # hide grid
        tv.setShowGrid(True)
    
        # set the font
        #font = QtGui.QFont("Courier New", 8)
        #tv.setFont(font)
    
        # set vertical header properties
        vh = tv.verticalHeader()
        vh.setVisible(True)
    
        # set horizontal header properties
        hh = tv.horizontalHeader()
        hh.setStretchLastSection(False)
    
        # set column width to fit contents
        tv.resizeColumnsToContents()
    
        # set row height
        #nrows = len(self.tabledata)
        #for row in xrange(nrows):
        #    tv.setRowHeight(row, 18)
    
        # enable sorting
        #tv.setSortingEnabled(True)
    
        return tv