def loadPreviousMarxanResults(self, setupObject): bestFieldName = self.bestNameLineEdit.text() summedFieldName = self.summedNameLineEdit.text() if self.bestCheckBox.isChecked(): bestPath = self.bestLineEdit.text() else: bestPath = "blank" if self.summedCheckBox.isChecked(): summedPath = self.summedLineEdit.text() else: summedPath = "blank" puLayer = QgsVectorLayer(setupObject.puPath, "Planning units", "ogr") fieldNameList = [field.name() for field in puLayer.pendingFields()] progressString = "check_files" if bestFieldName in fieldNameList: QMessageBox.warning(self,"Best field name duplication", "The planning unit theme already contains a field named " + bestFieldName + ". Please choose another name.") progressString = "stop" if summedFieldName in fieldNameList: QMessageBox.warning(self,"Summed field name duplication", "The planning unit theme already contains a field named " + summedFieldName + ". Please choose another name.") progressString = "stop" if len(bestFieldName) > 10: QMessageBox.warning(self,"Invalid field name", "The Best field name cannot be more than 10 characters long.") progressString = "stop" if len(summedFieldName) > 10: QMessageBox.warning(self,"Invalid field name", "The Summed field name cannot be more than 10 characters long.") progressString = "stop" if progressString == "check_files": self.close() if bestPath <> "blank": if os.path.isfile(bestPath): with open(bestPath, 'rb') as f: bestReader = csv.reader(f) bestHeader = next(bestReader, None) # skip the headers if bestHeader == setupObject.bestHeadingFieldNames: cluz_functions2.addBestMarxanOutputToPUShapefile(setupObject, bestPath, bestFieldName) bestShapefileName = bestFieldName cluz_display.displayBestOutput(setupObject, bestFieldName, bestShapefileName) else: QMessageBox.warning(self,"Invalid file","The specified Marxan best output file is incorrectly formatted. It must contain only two fields named planning_unit and solution.") else: QMessageBox.warning(self,"Incorrect pathname","The specified pathname for the Marxan best output is invalid. Please choose another one.") if summedPath <> "blank": if os.path.isfile(summedPath): with open(summedPath, 'rb') as f: summedReader = csv.reader(f) summedHeader = next(summedReader, None) # skip the headers if summedHeader == setupObject.summedHeadingFieldNames: cluz_functions2.addSummedMarxanOutputToPUShapefile(setupObject, summedPath, summedFieldName) summedShapefileName = summedFieldName cluz_display.displayGraduatedLayer(setupObject, summedFieldName, summedShapefileName, 1) #1 is SF legend code else: QMessageBox.warning(self,"Invalid file","The specified Marxan summed output file is incorrectly formatted. It must contain only two fields named planning_unit and number") else: QMessageBox.warning(self,"Incorrect pathname","The specified pathname for the Marxan summed output is invalid. Please choose another one") if bestPath <> "blank" or summedPath <> "blank": cluz_display.reloadPULayer(setupObject)
def runMarxan(self, setupObject, targetsMetAction): numIterString = self.iterLineEdit.text() numRunString = self.runLineEdit.text() outputName = str(self.outputLineEdit.text()) setupObject.outputName = outputName if self.boundCheckBox.isChecked(): blmValueString = self.boundLineEdit.text() else: blmValueString = "0" missingPropString = self.missingLineEdit.text() initialPropString = self.propLineEdit.text() extraOutputsBool = self.extraCheckBox.isChecked() if self.parallelCheckBox.isChecked(): numParallelAnalyses = int(self.parallelListWidget.selectedItems()[0].text()) else: numParallelAnalyses = 1 checkMarxanInputValuesBool = cluz_functions2.checkMarxanInputValuesBool(numIterString, numRunString, blmValueString, missingPropString, initialPropString, numParallelAnalyses) if checkMarxanInputValuesBool == True: numIter = int(numIterString) numRun = int(numRunString) blmValue = float(blmValueString) missingProp = float(missingPropString) initialProp = float(initialPropString) cluz_functions2.createSpecDatFile(setupObject) setupObject = cluz_functions2.marxanUpdateSetupObject(setupObject, outputName, numIter, numRun, blmValue, extraOutputsBool, missingProp, initialProp) cluz_setup.updateClzSetupFile(setupObject) self.close() if numParallelAnalyses == 1: bestOutputFile, summedOutputFile = launchSingleMarxanAnalysis(setupObject, numIter, numRun, blmValue, missingProp, initialProp, outputName, extraOutputsBool) else: bestOutputFile, summedOutputFile = launchMultiMarxanAnalysis(setupObject, numIter, numRun, blmValue, missingProp, initialProp, outputName, extraOutputsBool, numParallelAnalyses) cluz_functions2.addBestMarxanOutputToPUShapefile(setupObject, bestOutputFile, "Best") cluz_functions2.addSummedMarxanOutputToPUShapefile(setupObject, summedOutputFile, "SF_Score") cluz_display.reloadPULayer(setupObject) cluz_display.removePreviousMarxanLayers() bestLayerName = "Best (" + outputName + ")" summedLayerName = "SF_Score (" + outputName + ")" cluz_display.displayBestOutput(setupObject, "Best", bestLayerName) cluz_display.displayGraduatedLayer(setupObject, "SF_Score", summedLayerName, 1) #1 is SF legend code targetsMetAction.setEnabled(True)
def runMinPatch(setupObject, minpatchObject, minpatchDataDict): marxanNameString = minpatchObject.marxanFileName + "_r" finalNameString = "mp_" + marxanNameString marxanSolFileList = cluz_mpsetup.makeMarxanFileList(setupObject, marxanNameString) preMarxanUnitDict = minpatchDataDict["initialUnitDictionary"] summedSolDict = cluz_mpoutputs.produceSummedDict(preMarxanUnitDict) patchResultsDict = {} zoneStatsDict = {} zoneFeaturePropStatsDict = {} bestPortfolioCost = -1 continueBool = True for marxanSolFilePath in marxanSolFileList: runningUnitDict = createRunningUnitDictionary(minpatchDataDict, marxanSolFilePath) patchDict = cluz_mpfunctions.makePatchDict(runningUnitDict, minpatchDataDict) qgis.utils.iface.mainWindow().statusBar().showMessage("Processing file " + marxanSolFilePath + ".") if minpatchDataDict["patch_stats"] and continueBool: beforePatchStatsDict = cluz_mpoutputs.makePatchStatsDict(patchDict, minpatchDataDict) if minpatchDataDict["rem_small_patch"] and continueBool: runningUnitDict = cluz_mpfunctions.remSmallPatchesFromUnitDict(minpatchDataDict,runningUnitDict, patchDict) qgis.utils.iface.mainWindow().statusBar().showMessage("Processing file " + marxanSolFilePath + ". Removing patches that are smaller than the specified thresholds...") if minpatchDataDict["add_patches"] and continueBool: runningUnitDict, continueBool = cluz_mpfunctions.addPatches(minpatchDataDict, runningUnitDict) qgis.utils.iface.mainWindow().statusBar().showMessage("Processing file " + marxanSolFilePath + ". Adding new patches...") if minpatchDataDict["whittle_polish"] and continueBool: runningUnitDict = cluz_mpfunctions.runSimWhittle(runningUnitDict, minpatchDataDict) qgis.utils.iface.mainWindow().statusBar().showMessage("Processing file " + marxanSolFilePath + ". Simulated whittling...") runningUnitDict = addConservedPUs(runningUnitDict,minpatchDataDict) if minpatchDataDict["patch_stats"] and continueBool: patchDict = cluz_mpfunctions.makePatchDict(runningUnitDict, minpatchDataDict) afterPatchStatsDict = cluz_mpoutputs.makePatchStatsDict(patchDict, minpatchDataDict) if continueBool: outputFilePath = marxanSolFilePath.replace(marxanNameString, finalNameString) cluz_mpoutputs.printRunResults(minpatchDataDict, runningUnitDict, outputFilePath) costDict = makeCostDict(minpatchDataDict, runningUnitDict) totalCost = costDict['totalBoundaryCost'] + costDict['totalUnitCost'] if minpatchDataDict["patch_stats"]: patchResultsDict = cluz_mpoutputs.producePatchResultsDict(patchResultsDict, marxanSolFilePath, beforePatchStatsDict, afterPatchStatsDict, costDict) if minpatchDataDict["zone_stats"]: zoneNameString = os.path.basename(marxanSolFilePath) zoneStatsDict[zoneNameString] = cluz_mpoutputs.makeRunZoneStatsDict(minpatchDataDict, runningUnitDict, zoneStatsDict) zoneFeaturePropStatsDict[zoneNameString] = cluz_mpoutputs.makeRunZoneFeaturePropStatsDict(minpatchDataDict, runningUnitDict) if bestPortfolioCost == -1: bestPortfolioCost = totalCost bestPortfolio = copy.deepcopy(runningUnitDict) if bestPortfolioCost <> -1 and totalCost < bestPortfolioCost: bestPortfolioCost = totalCost bestPortfolio = copy.deepcopy(runningUnitDict) summedDict = cluz_mpoutputs.updateSummedDict(summedSolDict,runningUnitDict) if continueBool: bestFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName + '_best.txt' cluz_mpoutputs.printRunResults(minpatchDataDict, bestPortfolio, bestFileName) summedFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName + '_summed.txt' cluz_mpoutputs.printSummedResults(summedDict, summedFileName) if minpatchDataDict["patch_stats"]: patchstatsFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName + '_patchstats.csv' cluz_mpoutputs.printPatchStats(patchResultsDict, patchstatsFileName) if minpatchDataDict["zone_stats"]: zoneStatsBaseFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName cluz_mpoutputs.printZoneStats(minpatchDataDict, zoneStatsDict, zoneStatsBaseFileName) cluz_mpoutputs.printZoneFeaturePropStats(minpatchDataDict, zoneFeaturePropStatsDict, zoneStatsBaseFileName) cluz_functions2.addBestMarxanOutputToPUShapefile(setupObject, bestFileName, "MP_Best") cluz_functions2.addSummedMarxanOutputToPUShapefile(setupObject, summedFileName, "MP_SF_Scr") cluz_display.reloadPULayer(setupObject) cluz_display.removePreviousMinPatchLayers() bestLayerName = "MP Best (" + minpatchObject.marxanFileName + ")" summedLayerName = "MP SF_Score (" + minpatchObject.marxanFileName + ")" cluz_display.displayBestOutput(setupObject, "MP_Best", bestLayerName) cluz_display.displayGraduatedLayer(setupObject, "MP_SF_Scr", summedLayerName, 1) #1 is SF legend code qgis.utils.iface.mainWindow().statusBar().showMessage("") qgis.utils.iface.messageBar().pushMessage("MinPatch results", "MinPatch has completed the analysis and the results files are in the specified output folder.", QgsMessageBar.INFO, 3)
def runMinPatch(setupObject, minpatchObject, minpatchDataDict): marxanNameString = minpatchObject.marxanFileName + "_r" finalNameString = "mp_" + marxanNameString marxanSolFileList = cluz_mpsetup.makeMarxanFileList( setupObject, marxanNameString) preMarxanUnitDict = minpatchDataDict["initialUnitDictionary"] summedSolDict = cluz_mpoutputs.produceSummedDict(preMarxanUnitDict) patchResultsDict = {} zoneStatsDict = {} zoneFeaturePropStatsDict = {} bestPortfolioCost = -1 continueBool = True for marxanSolFilePath in marxanSolFileList: runningUnitDict = createRunningUnitDictionary(minpatchDataDict, marxanSolFilePath) patchDict = cluz_mpfunctions.makePatchDict(runningUnitDict, minpatchDataDict) qgis.utils.iface.mainWindow().statusBar().showMessage( "Processing file " + marxanSolFilePath + ".") if minpatchDataDict["patch_stats"] and continueBool: beforePatchStatsDict = cluz_mpoutputs.makePatchStatsDict( patchDict, minpatchDataDict) if minpatchDataDict["rem_small_patch"] and continueBool: runningUnitDict = cluz_mpfunctions.remSmallPatchesFromUnitDict( minpatchDataDict, runningUnitDict, patchDict) qgis.utils.iface.mainWindow().statusBar().showMessage( "Processing file " + marxanSolFilePath + ". Removing patches that are smaller than the specified thresholds..." ) if minpatchDataDict["add_patches"] and continueBool: runningUnitDict, continueBool = cluz_mpfunctions.addPatches( minpatchDataDict, runningUnitDict) qgis.utils.iface.mainWindow().statusBar().showMessage( "Processing file " + marxanSolFilePath + ". Adding new patches...") if minpatchDataDict["whittle_polish"] and continueBool: runningUnitDict = cluz_mpfunctions.runSimWhittle( runningUnitDict, minpatchDataDict) qgis.utils.iface.mainWindow().statusBar().showMessage( "Processing file " + marxanSolFilePath + ". Simulated whittling...") runningUnitDict = addConservedPUs(runningUnitDict, minpatchDataDict) if minpatchDataDict["patch_stats"] and continueBool: patchDict = cluz_mpfunctions.makePatchDict(runningUnitDict, minpatchDataDict) afterPatchStatsDict = cluz_mpoutputs.makePatchStatsDict( patchDict, minpatchDataDict) if continueBool: outputFilePath = marxanSolFilePath.replace(marxanNameString, finalNameString) cluz_mpoutputs.printRunResults(minpatchDataDict, runningUnitDict, outputFilePath) costDict = makeCostDict(minpatchDataDict, runningUnitDict) totalCost = costDict['totalBoundaryCost'] + costDict[ 'totalUnitCost'] if minpatchDataDict["patch_stats"]: patchResultsDict = cluz_mpoutputs.producePatchResultsDict( patchResultsDict, marxanSolFilePath, beforePatchStatsDict, afterPatchStatsDict, costDict) if minpatchDataDict["zone_stats"]: zoneNameString = os.path.basename(marxanSolFilePath) zoneStatsDict[ zoneNameString] = cluz_mpoutputs.makeRunZoneStatsDict( minpatchDataDict, runningUnitDict, zoneStatsDict) zoneFeaturePropStatsDict[ zoneNameString] = cluz_mpoutputs.makeRunZoneFeaturePropStatsDict( minpatchDataDict, runningUnitDict) if bestPortfolioCost == -1: bestPortfolioCost = totalCost bestPortfolio = copy.deepcopy(runningUnitDict) if bestPortfolioCost <> -1 and totalCost < bestPortfolioCost: bestPortfolioCost = totalCost bestPortfolio = copy.deepcopy(runningUnitDict) summedDict = cluz_mpoutputs.updateSummedDict( summedSolDict, runningUnitDict) if continueBool: bestFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName + '_best.txt' cluz_mpoutputs.printRunResults(minpatchDataDict, bestPortfolio, bestFileName) summedFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName + '_summed.txt' cluz_mpoutputs.printSummedResults(summedDict, summedFileName) if minpatchDataDict["patch_stats"]: patchstatsFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName + '_patchstats.csv' cluz_mpoutputs.printPatchStats(patchResultsDict, patchstatsFileName) if minpatchDataDict["zone_stats"]: zoneStatsBaseFileName = setupObject.outputPath + os.sep + 'mp_' + minpatchObject.marxanFileName cluz_mpoutputs.printZoneStats(minpatchDataDict, zoneStatsDict, zoneStatsBaseFileName) cluz_mpoutputs.printZoneFeaturePropStats(minpatchDataDict, zoneFeaturePropStatsDict, zoneStatsBaseFileName) cluz_functions2.addBestMarxanOutputToPUShapefile( setupObject, bestFileName, "MP_Best") cluz_functions2.addSummedMarxanOutputToPUShapefile( setupObject, summedFileName, "MP_SF_Scr") cluz_display.reloadPULayer(setupObject) cluz_display.removePreviousMinPatchLayers() bestLayerName = "MP Best (" + minpatchObject.marxanFileName + ")" summedLayerName = "MP SF_Score (" + minpatchObject.marxanFileName + ")" cluz_display.displayBestOutput(setupObject, "MP_Best", bestLayerName) cluz_display.displayGraduatedLayer(setupObject, "MP_SF_Scr", summedLayerName, 1) #1 is SF legend code qgis.utils.iface.mainWindow().statusBar().showMessage("") qgis.utils.iface.messageBar().pushMessage( "MinPatch results", "MinPatch has completed the analysis and the results files are in the specified output folder.", QgsMessageBar.INFO, 3)