Changeset 400

Show
Ignore:
Timestamp:
05/31/09 20:35:21 (4 years ago)
Author:
jjr8
Message:

Checkin of unfinished code so that I can rebuild the Arc 9.3 toolbox for testing purposes.

Location:
MGET/Branches/Jason/PythonPackage/src/GeoEco
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/R.py

    r391 r400  
    4848 
    4949    @classmethod 
    50     def Evaluate(cls, statements, variableNames=None, variableValues=None, tables=None, dataFrameNamesForTables=None, xColumnName=u'x', yColumnName=u'y', zColumnName=u'z', mColumnName=u'm', stringsAsFactors=True, timeZone=None): 
     50    def Evaluate(cls, statements, clearGlobalEnv=True, variableNames=None, variableValues=None, tables=None, dataFrameNamesForTables=None, xColumnName=u'x', yColumnName=u'y', zColumnName=u'z', mColumnName=u'm', stringsAsFactors=True, timeZone=None): 
    5151        cls.__doc__.Obj.ValidateMethodInvocation() 
    5252 
     
    138138 
    139139    @classmethod 
    140     def EvaluateFile(cls, path, variableNames=None, variableValues=None, tables=None, dataFrameNamesForTables=None, xColumnName=u'x', yColumnName=u'y', zColumnName=u'z', mColumnName=u'm', stringsAsFactors=True, timeZone=None): 
     140    def EvaluateFile(cls, path, clearGlobalEnv=True, variableNames=None, variableValues=None, tables=None, dataFrameNamesForTables=None, xColumnName=u'x', yColumnName=u'y', zColumnName=u'z', mColumnName=u'm', stringsAsFactors=True, timeZone=None): 
    141141        cls.__doc__.Obj.ValidateMethodInvocation() 
    142142 
     
    18381838    arcGISDisplayName=_(u'R statements')) 
    18391839 
     1840AddArgumentMetadata(R.Evaluate, u'clearGlobalEnv', 
     1841    typeMetadata=BooleanTypeMetadata(), 
     1842    description=_( 
     1843u"""If True, the default, all objects will be removed from the R 
     1844global environment after the statements are evaluated. If False, the R 
     1845global environment will be left unchanged. 
     1846 
     1847This parameter is intended as a convenience to help minimize the 
     1848memory utilization of this tool when it is run "in process". This 
     1849parameter has no effect when this tool is run "out of process", as 
     1850will occur when you run this tool from an ArcGIS geoprocessing model 
     1851or script with ArcGIS version 9.2 or earlier. If you are in that 
     1852situation, you should ignore this parameter. 
     1853 
     1854When this tool is run for the first time, it instantiates the R 
     1855interpreter in the process that invoked it. If you are using ArcGIS, 
     1856this process is typically an instance of ArcCatalog or ArcMap. When 
     1857you run the tool again, it reuses the same R interpreter. Thus, if you 
     1858specified False for this parameter the first time you ran this tool, 
     1859the R variables defined by your statements will remain defined and be 
     1860available the second time you run this tool. That can be good or bad, 
     1861depending on what you want. 
     1862 
     1863The main benefit is that your second batch of statements can access 
     1864results obtained by the first batch. This allows you to interleave 
     1865ArcGIS operations between batches of R statements. The main downside 
     1866is that the objects allocated by the first batch occupy memory. 
     1867Because ArcGIS processes can only utilize about 1.5 GB of memory 
     1868before crashing, leaving the R objects in memory may limit your 
     1869subsequent geoprocessing operations. 
     1870 
     1871Because we anticipate that most users will not want to pass results 
     1872between subsequent executions of this tool, we set the default value 
     1873of this parameter to True, to minimize memory utilization. If you do 
     1874want to pass results between subsequent executions, set this parameter 
     1875to False. 
     1876 
     1877When this parameter is true, it cleans out the R global environment by 
     1878executing the following R statement:: 
     1879 
     1880    rm(list = ls()) 
     1881"""), 
     1882    arcGISDisplayName=_(u'Clear R global environment after evaluating statements')) 
     1883 
    18401884AddArgumentMetadata(R.Evaluate, u'variableNames', 
    18411885    typeMetadata=ListTypeMetadata(elementType=UnicodeStringTypeMetadata(minLength=1), canBeNone=True), 
     
    21372181    arcGISDisplayName=_(u'Text file')) 
    21382182 
     2183CopyArgumentMetadata(R.Evaluate, u'clearGlobalEnv', R.EvaluateFile, u'clearGlobalEnv') 
    21392184CopyArgumentMetadata(R.Evaluate, u'variableNames', R.EvaluateFile, u'variableNames') 
    21402185CopyArgumentMetadata(R.Evaluate, u'variableValues', R.EvaluateFile, u'variableValues') 
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/Statistics/Exploratory.py

    r339 r400  
    239239    arcGISDisplayName=_(u'Scatterplot Matrix for Table'), 
    240240    arcGISToolCategory=_(u'Statistics\\Explore Data'), 
    241     isOutOfProcArcGISTool=True, 
    242241    dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 
    243242 
     
    459458    arcGISDisplayName=_(u'Density Histogram for Field'), 
    460459    arcGISToolCategory=_(u'Statistics\\Explore Data'), 
    461     isOutOfProcArcGISTool=True, 
    462460    dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 
    463461 
     
    553551    arcGISDisplayName=_(u'Density Histogram for Point Coordinate'), 
    554552    arcGISToolCategory=_(u'Statistics\\Explore Data'), 
    555     isOutOfProcArcGISTool=True, 
    556553    dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 
    557554 
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/Statistics/Modeling.py

    r398 r400  
    775775    if templateRaster is None: 
    776776        templateRaster = inputPredictorRasters[0] 
    777     else: 
    778         d = gp.Describe(templateRaster) 
    779         if d.SpatialReference.Name is None or d.SpatialReference.Name.lower() == u'unknown': 
    780             Logger.RaiseException(ValueError(_(u'The template raster %(raster)s does not have a coordinate system defined. Please define the coordinate system for this raster and try again. (Use the ArcGIS Define Projection tool.)') % {u'raster': templateRaster})) 
     777 
     778    templateDescribe = gp.Describe(templateRaster) 
     779    if templateDescribe.SpatialReference.Name is None or templateDescribe.SpatialReference.Name.lower() == u'unknown': 
     780        Logger.RaiseException(ValueError(_(u'The template raster %(raster)s does not have a coordinate system defined. Please define the coordinate system for this raster and try again. (Use the ArcGIS Define Projection tool.)') % {u'raster': templateRaster})) 
     781 
     782    coordinateSystem = gp.CreateSpatialReference(templateDescribe.SpatialReference).split(';')[0] 
     783    cellSize = templateDescribe.MeanCellWidth 
     784    extent = templateDescribe.Extent 
    781785 
    782786    # Load the fitted model. 
     
    868872            # First prepare the input rasters for the prediction. 
    869873 
    870             _PreparePredictorRasters(r, gp, tempDir, variableNames, inputPredictorRasters, templateRaster, resamplingTechniques) 
     874            _PreparePredictorRasters(r, gp, tempDir, variableNames, inputPredictorRasters, templateRaster, templateDescribe, coordinateSystem, cellSize, extent, resamplingTechniques) 
    871875 
    872876            # Do the prediction. The prediction rasters are 
     
    945949        r('if (exists("fam20985982305")) rm("fam20985982305", pos=globalenv())') 
    946950 
    947 def _PreparePredictorRasters(r, gp, tempDir, variableNames, inputPredictorRasters, templateRaster, resamplingTechniques): 
    948  
    949     # Build a dictionary that maps predictor variable names in 
    950     # the model to rasters provided by the caller. 
     951def _PreparePredictorRasters(r, gp, tempDir, variableNames, inputPredictorRasters, templateRaster, templateDescribe, coordinateSystem, cellSize, extent, resamplingTechniques): 
     952 
     953    # Build a dictionary that maps predictor variable names in the 
     954    # model to rasters provided by the caller. 
    951955 
    952956    rastersForPredictors = {} 
     
    965969            rastersForPredictors[variable] = inputPredictorRasters[variableNames.index(variable)] 
    966970 
    967     # Generate the X and Y coordinate rasters, if needed 
    968     # and possible. 
     971    # Generate the X and Y coordinate rasters, if needed and possible. 
    969972 
    970973    if createXRaster: 
     
    992995        rastersForPredictors[r['yVar']] = yRaster 
    993996 
    994     # Check each predictor raster. If it has a different 
    995     # coordinate system, extent, or cell size than that 
    996     # specified by the caller for the output rasters, 
     997    # Check each predictor raster. If it has a different coordinate 
     998    # system, extent, or cell size than that of the template raster, 
    997999    # project it in the temp directory. 
     1000    # 
     1001    # Note that ArcGIS does not always seem to represent the cell size 
     1002    # and extent of rasters at the fullest possible precision, so we 
     1003    # perform comparisons at less than full precision. 
    9981004 
    9991005    Logger.Info('Checking projections, extents, and cell sizes of predictor rasters and reprojecting and clipping as needed...') 
     1006 
     1007    from GeoEco.Types import EnvelopeTypeMetadata 
     1008    [templateLeft, templateBottom, templateRight, templateTop] = EnvelopeTypeMetadata.ParseCoordinatesFromString(extent) 
    10001009 
    10011010    vars = copy.deepcopy(rastersForPredictors.keys())           # I'm going to modify the dictionary in the loop below; not sure if deepcopy is needed, but playing it safe. 
     
    10041013 
    10051014    for i in range(len(vars)): 
    1006         needToProject = True 
     1015        if rasters[i] == templateRaster: 
     1016            continue 
     1017         
     1018        needToProject = False 
     1019        needToClip = False 
    10071020        d = gp.Describe(rasters[i]) 
    10081021        cs = gp.CreateSpatialReference_management(d.SpatialReference).split(u';')[0] 
    1009          
     1022 
    10101023        if cs != coordinateSystem: 
    10111024            Logger.Debug(_(u'%(raster)s must be projected because its coordinate system (%(cs1)s) does not match the output coordinate system (%(cs2)s.') % {u'raster': rasters[i], u'cs1': cs, u'cs2': coordinateSystem}) 
    1012         elif d.Extent != extent: 
    1013             Logger.Debug(_(u'%(raster)s must be projected because its extent (%(e1)s) does not match the output extent (%(e2)s).') % {u'raster': rasters[i], u'e1': d.Extent, u'e2': extent}) 
    1014         elif d.MeanCellWidth != cellSize: 
    1015             Logger.Debug(_(u'%(raster)s must be projected because its cell size (%(cs1)f) does not match the output cell size (%(cs2)f).') % {u'raster': rasters[i], u'cs1': d.MeanCellWidth, u'cs2': cellSize}) 
     1025            Logger.Info(_(u'Projecting and clipping %(raster)s because its coordinate system does not match that of the template raster.') % {u'raster': rasters[i]}) 
     1026            needToProject = True 
     1027 
    10161028        else: 
    1017             needToProject = False 
     1029            [rasterLeft, rasterBottom, rasterRight, rasterTop] = EnvelopeTypeMetadata.ParseCoordinatesFromString(d.Extent) 
     1030 
     1031            if rasterLeft > templateLeft: 
     1032                Logger.RaiseException(_(u'The template raster is too large; its left edge falls outside the left edge of the predictor raster %(raster)s. Please reduce the extent of the template raster such that it does not exceed the extents of any of the predictor rasters.') % {u'raster': rasters[i]}) 
     1033            if rasterRight < templateRight: 
     1034                Logger.RaiseException(_(u'The template raster is too large; its right edge falls outside the right edge of the predictor raster %(raster)s. Please reduce the extent of the template raster such that it does not exceed the extents of any of the predictor rasters.') % {u'raster': rasters[i]}) 
     1035            if rasterTop > templateTop: 
     1036                Logger.RaiseException(_(u'The template raster is too large; its top edge falls outside the top edge of the predictor raster %(raster)s. Please reduce the extent of the template raster such that it does not exceed the extents of any of the predictor rasters.') % {u'raster': rasters[i]}) 
     1037            if rasterBottom < templateBottom: 
     1038                Logger.RaiseException(_(u'The template raster is too large; its bottom edge falls outside the bottom edge of the predictor raster %(raster)s. Please reduce the extent of the template raster such that it does not exceed the extents of any of the predictor rasters.') % {u'raster': rasters[i]}) 
     1039                 
     1040            elif abs(1.0 - cellSize / d.MeanCellWidth) > 0.00001: 
     1041                Logger.Debug(_(u'%(raster)s must be projected because its cell size (%(cs1)f) does not match the output cell size (%(cs2)f).') % {u'raster': rasters[i], u'cs1': d.MeanCellWidth, u'cs2': cellSize}) 
     1042                Logger.Info(_(u'Projecting and clipping %(raster)s because its cell size does not match that of the template raster.') % {u'raster': rasters[i]}) 
     1043                needToProject = True 
     1044 
     1045            else: 
     1046                deltaLeft = (float(extent.split()[0]) - float(d.Extent.split()[0])) / cellSize     # Difference, in cells, between the left edge of the template and this raster 
     1047                deltaBottom = (float(extent.split()[1]) - float(d.Extent.split()[1])) / cellSize   # Difference, in cells, between the bottom edge of the template and this raster 
     1048                deltaRight = (float(extent.split()[2]) - float(d.Extent.split()[2])) / cellSize    # Difference, in cells, between the right edge of the template and this raster 
     1049                deltaTop = (float(extent.split()[3]) - float(d.Extent.split()[3])) / cellSize      # Difference, in cells, between the top edge of the template and this raster 
     1050 
     1051                if deltaLeft < -0.01: 
     1052                    Logger.RaiseException(_(u'The template raster is too large. The left edge of the predictor raster %(raster)s is inside the left edge of the template raster. Please reduce the extent of the template raster such that it does not exceed the extents of any of the predictor rasters.') % {u'raster': rasters[i]}) 
     1053                if deltaRight > -0.01: 
     1054                    Logger.RaiseException(_(u'The template raster is too large. The left edge of the predictor raster %(raster)s is inside the left edge of the template raster. Please reduce the extent of the template raster such that it does not exceed the extents of any of the predictor rasters.') % {u'raster': rasters[i]}) 
     1055 
     1056                if deltaLeft < 0.01 and deltaBottom < 0.01 and deltaRight < 0.01 and deltaTop < 0.01: 
     1057                    Logger.Debug(_(u'%(raster)s has the same coordinate system, cell size, and extent as the template raster.') % {u'raster': rasters[i]}) 
     1058 
     1059                elif abs(deltaLeft - round(deltaLeft)) < 0.01 and abs(deltaBottom - round(deltaBottom)) < 0.01 and abs(deltaRight - round(deltaRight)) < 0.01 and abs(deltaTop - round(deltaTop)) < 0.01: 
     1060                    Logger.Debug(_(u'%(raster)s must be clipped because it has the same coordinate system and cell size as the template raster, and is aligned on the same grid coordinates, but has a different extent.') % {u'raster': rasters[i]}) 
     1061                    Logger.Info(_(u'Clipping %(raster)s to the extent of the template raster.') % {u'raster': rasters[i]}) 
     1062                    needToClip = True 
     1063 
     1064                else: 
     1065                    Logger.Debug(_(u'%(raster)s must be clipped because it has the same coordinate system and cell size as the template raster, and is aligned on the same grid coordinates, but has a different extent.') % {u'raster': rasters[i]}) 
     1066                    Logger.Info(_(u'Clipping %(raster)s to the extent of the template raster.') % {u'raster': rasters[i]}) 
     1067                    needToProject = True 
     1068                     
     1069 
     1070                if abs(offsetX) > 0.01 or abs(offsetY) > 0.01: 
     1071                    if abs(offsetX - round(offsetX)) > 0.01 or abs(offsetX - round(offsetX)) > 0.01: 
     1072                        Logger.Debug(_(u'%(raster)s must be projected because its cell size (%(cs1)f) does not match the output cell size (%(cs2)f).') % {u'raster': rasters[i], u'cs1': d.MeanCellWidth, u'cs2': cellSize}) 
     1073                        Logger.Info(_(u'Projecting and clipping %(raster)s because its cell size does not match that of the template raster.') % {u'raster': rasters[i]}) 
     1074                        needToProject = True 
    10181075 
    10191076        if needToProject: 
     
    11421199    arcGISDisplayName=_(u'Fit GLM'), 
    11431200    arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Linear Models'), 
    1144     isOutOfProcArcGISTool=True, 
    11451201    dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 
    11461202 
     
    15671623    arcGISDisplayName=_(u'Predict GLM From Rasters'), 
    15681624    arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Linear Models'), 
    1569     isOutOfProcArcGISTool=True, 
    15701625    dependencies=[ArcGISDependency(9, 2), RDependency(2, 5, 0), RPackageDependency(u'rgdal')]) 
    15711626 
     
    18221877    arcGISDisplayName=_(u'Fit GAM'), 
    18231878    arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Additive Models'), 
    1824     isOutOfProcArcGISTool=True, 
    18251879    dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 
    18261880 
     
    20052059    arcGISDisplayName=_(u'Predict GAM From Rasters'), 
    20062060    arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Additive Models'), 
    2007     isOutOfProcArcGISTool=True, 
    20082061    dependencies=[ArcGISDependency(9, 2), RDependency(2, 5, 0), RPackageDependency(u'rgdal')]) 
    20092062 
     
    20382091    arcGISDisplayName=_(u'Predict Bayesian Probabilities for GAM From Rasters'), 
    20392092    arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Additive Models'), 
    2040     isOutOfProcArcGISTool=True, 
    20412093    dependencies=[ArcGISDependency(9, 2), RDependency(2, 5, 0), RPackageDependency(u'mgcv'), RPackageDependency(u'MASS'), RPackageDependency(u'rgdal')]) 
    20422094 
     
    21572209    arcGISDisplayName=_(u'Plot Performance of Binary Classification Model'), 
    21582210    arcGISToolCategory=_(u'Statistics\\Model Data\\Evaluate Model Performance'), 
    2159     isOutOfProcArcGISTool=True, 
    21602211    dependencies=[RDependency(2, 5, 0), RPackageDependency(u'ROCR')]) 
    21612212 
     
    25012552    arcGISDisplayName=_(u'Plot ROC of Binary Classification Model'), 
    25022553    arcGISToolCategory=_(u'Statistics\\Model Data\\Evaluate Model Performance'), 
    2503     isOutOfProcArcGISTool=True, 
    25042554    dependencies=[RDependency(2, 5, 0), RPackageDependency(u'ROCR')]) 
    25052555