class LogisticRegressionWidget(QWidget, Ui_Widget): def __init__(self, plugin, parent=None): QWidget.__init__(self, parent) self.setupUi(self) self.plugin = plugin self.inputs = plugin.inputs self.settings = QSettings("NextGIS", "MOLUSCE") self.btnFitModel.clicked.connect(self.fitModel) self.manageGui() def manageGui(self): self.spnNeighbourhood.setValue(self.settings.value("ui/LR/neighborhood", 1).toInt()[0]) def fitModel(self): self.settings.setValue("ui/LR/neighborhood", self.spnNeighbourhood.value()) self.model = LR(ns=self.spnNeighbourhood.value()) self.model.setTrainingData(self.inputs["initial"], self.inputs["factors"].values(), self.inputs["final"], mode=self.inputs["samplingMode"], samples=self.plugin.spnSamplesCount.value() ) self.model.train() # populate table self.showCoefficients() self.inputs["model"] = self.model def showCoefficients(self): fm = self.model.getIntercept() coef = self.model.getCoef() self.tblCoefficients.clear() self.tblCoefficients.setColumnCount(len(fm)) self.tblCoefficients.setRowCount(len(coef[0]) + 1) for i in xrange(len(fm)): item = QTableWidgetItem(unicode(fm[i])) self.tblCoefficients.setItem(0, i, item) for j in xrange(len(coef[i])): item = QTableWidgetItem(unicode(coef[i][j])) self.tblCoefficients.setItem(j + 1, i, item) self.tblCoefficients.resizeRowsToContents() self.tblCoefficients.resizeColumnsToContents()
def fitModel(self): self.settings.setValue("ui/LR/neighborhood", self.spnNeighbourhood.value()) self.model = LR(ns=self.spnNeighbourhood.value()) self.model.setTrainingData(self.inputs["initial"], self.inputs["factors"].values(), self.inputs["final"], mode=self.inputs["samplingMode"], samples=self.plugin.spnSamplesCount.value() ) self.model.train() # populate table self.showCoefficients() self.inputs["model"] = self.model
def startFitModel(self): if not utils.checkInputRasters(self.inputs): QMessageBox.warning(self.plugin, self.tr("Missed input data"), self.tr("Initial or final raster is not set. Please specify input data and try again") ) return if not utils.checkFactors(self.inputs): QMessageBox.warning(self.plugin, self.tr("Missed input data"), self.tr("Factors rasters is not set. Please specify them and try again") ) return if not utils.checkChangeMap(self.inputs): QMessageBox.warning(self.plugin, self.tr("Missed input data"), self.tr("Change map raster is not set. Please create it try again") ) return self.settings.setValue("ui/LR/neighborhood", self.spnNeighbourhood.value()) self.plugin.logMessage(self.tr("Init LR model")) model = LR(ns=self.spnNeighbourhood.value()) self.inputs["model"] = model model.setMaxIter(self.spnMaxIterations.value()) model.setState(self.inputs["initial"]) model.setFactors(self.inputs["factors"].values()) model.setOutput(self.inputs["changeMap"]) model.setMode(self.inputs["samplingMode"],) model.setSamples(self.plugin.spnSamplesCount.value()) self.plugin.logMessage(self.tr("Set training data")) model.moveToThread(self.plugin.workThread) self.plugin.workThread.started.connect(model.startTrain) self.plugin.setProgressRange("Train LR model", 0) model.finished.connect(self.__trainFinished) model.errorReport.connect(self.plugin.logErrorReport) model.finished.connect(self.plugin.workThread.quit) self.plugin.workThread.start()