Vous êtes sur la page 1sur 3

# ------------------------------------------------------------------------------------------------------------------------------------------------------------# InVESTOptimization.

py # Created on: October 2014 # Created by: Shannon Chapin # Description: This script pulls the InVEST Crop Pollination Model into a # function and then uses simulated annealing to optimize the function. # It then runs a correlation on the predicted vs. observed bee abundances. # Finally, it returns the correlation coefficient (*-1) and the parameter values used. # ------------------------------------------------------------------------------------------------------------------------------------------------------------def InVESTCropPollination(lulcvec): # Import necessary modules import arcpy import numpy from dbf import * from arcpy import env import os from numpy import * # Set overwrite option arcpy.env.overwriteOutput = True # Check out any necessary licenses arcpy.CheckOutExtension("spatial") # Load required toolboxes arcpy.ImportToolbox("D:/InVEST_2_5_3/invest_253.tbx") # Local variables: Try1 = "F:\\Optimization\\Try1" noso1011122 = "F:\\Optimization\\noso1011122" bees_dbf = "F:\\Optimization\\Try1\\bees.dbf" FieldSites = "F:\\Optimization\\FieldSites_2010_2011_2012_edit.shp" Raster = "F:\\Optimization\\Try1\\output\\frm_avg_cur" OutPts = "F:\\Optimization\\Try1\\OutPts.shp" lulcdbf = "F:\\Optimization\\Try1\\lulc.dbf" # Put lulcvec into an array lulc = numpy.reshape(lulcvec, (8,5), 'F') # Create dbf to put lulc into lulcdbf = dbf.Table ('F:\\Optimization\\Try1\\lulc.dbf', 'LULC N(1,0); LULCName C(30); N_ground N(3,1); N_cavity N(3,1); F_spr N(3,1); F_earsum N(3,1); F_sum N(3,1)') # Create name list lulcname = ['Deciduous/Mixed Edge', 'Developed/Other', 'Coniferous', 'Deciduous/Mixed Forest', 'Emergent/SS Wetlands', 'Wetlands/Water', 'Agriculture/Field', 'Blueberries'] # Populate lulcdbf lulcdbf.open()

for i in range (0,8): t = (i+1, lulcname[i]) + tuple(lulc[i]) lulcdbf.append(t) # Local variable lulcdbf = "F:\\Optimization\\Try1\\lulc.dbf" # Process: InVEST Pollination arcpy.Pollination2_InVEST253(Try1, noso1011122, "", "10", "", "", bees_dbf, lulcdbf, "8", "0.125", "1", "", "false") # Process: Extract Values to Points arcpy.gp.ExtractValuesToPoints_sa(FieldSites, Raster, OutPts, "NONE", "VALUE_ONLY") # Create an output file for extracted raster values myOutputFile = open('F:\\Optimization\\PredictedAbund.txt', 'w') # Create a row object to navigate table rows = arcpy.SearchCursor(OutPts) # For each row in my table, retrieve the value in the 'RASTERVALU' field, convert it # to a string, write it to my output file, then move to the next line in both # my input table and output file for row in rows: myOutputFile.write(str(row.RASTERVALU) + '\n') del row, rows myOutputFile.close() # Obtain correlation coeffient data1 = loadtxt("F:\\Optimization\\FieldAbundance.txt", unpack="TRUE") data2 = loadtxt("F:\\Optimization\\PredictedAbund.txt", unpack="TRUE") cor = corrcoef(data1,data2)[0][1] mincor = cor*-1.0 # Print lulcvec and mincor in the PythonWin console print lulcvec print mincor # Write lulcvec and mincor in the results file results = open('F:\\Optimization\\Results.txt', 'a') results.write ('lulcvec:') results.write ('\n') results.write (str(lulcvec)) results.write ('\n') results.write ('mincor:') results.write ('\n') results.write (str(mincor)) results.write ('\n') results.close return mincor

# Optimization import scipy from scipy import optimize # Provide an initial guess for the parameters lulcvec = [0.9,0.9,0.5,0.6,0.2,0.1,0.7,1.0,1.0,0.6,0.6,0.9,0.4,0.1,0.2,0.4,0.9,1.0,0.1,0.7,0.7,0.3,0.9,0.4,0.9,0.9,0.1,0.5,0.6,0.2,0.7,1.0, 1.0,1.0,0.1,0.4,0.6,0.5,0.9,0.5] #Optimize! scipy.optimize.anneal(InVESTCropPollination, lulcvec, full_output=True, lower=0, upper=1)

Vous aimerez peut-être aussi