Changeset 400
- Timestamp:
- 05/31/09 20:35:21 (4 years ago)
- Location:
- MGET/Branches/Jason/PythonPackage/src/GeoEco
- Files:
-
- 3 modified
-
R.py (modified) (4 diffs)
-
Statistics/Exploratory.py (modified) (3 diffs)
-
Statistics/Modeling.py (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
MGET/Branches/Jason/PythonPackage/src/GeoEco/R.py
r391 r400 48 48 49 49 @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): 51 51 cls.__doc__.Obj.ValidateMethodInvocation() 52 52 … … 138 138 139 139 @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): 141 141 cls.__doc__.Obj.ValidateMethodInvocation() 142 142 … … 1838 1838 arcGISDisplayName=_(u'R statements')) 1839 1839 1840 AddArgumentMetadata(R.Evaluate, u'clearGlobalEnv', 1841 typeMetadata=BooleanTypeMetadata(), 1842 description=_( 1843 u"""If True, the default, all objects will be removed from the R 1844 global environment after the statements are evaluated. If False, the R 1845 global environment will be left unchanged. 1846 1847 This parameter is intended as a convenience to help minimize the 1848 memory utilization of this tool when it is run "in process". This 1849 parameter has no effect when this tool is run "out of process", as 1850 will occur when you run this tool from an ArcGIS geoprocessing model 1851 or script with ArcGIS version 9.2 or earlier. If you are in that 1852 situation, you should ignore this parameter. 1853 1854 When this tool is run for the first time, it instantiates the R 1855 interpreter in the process that invoked it. If you are using ArcGIS, 1856 this process is typically an instance of ArcCatalog or ArcMap. When 1857 you run the tool again, it reuses the same R interpreter. Thus, if you 1858 specified False for this parameter the first time you ran this tool, 1859 the R variables defined by your statements will remain defined and be 1860 available the second time you run this tool. That can be good or bad, 1861 depending on what you want. 1862 1863 The main benefit is that your second batch of statements can access 1864 results obtained by the first batch. This allows you to interleave 1865 ArcGIS operations between batches of R statements. The main downside 1866 is that the objects allocated by the first batch occupy memory. 1867 Because ArcGIS processes can only utilize about 1.5 GB of memory 1868 before crashing, leaving the R objects in memory may limit your 1869 subsequent geoprocessing operations. 1870 1871 Because we anticipate that most users will not want to pass results 1872 between subsequent executions of this tool, we set the default value 1873 of this parameter to True, to minimize memory utilization. If you do 1874 want to pass results between subsequent executions, set this parameter 1875 to False. 1876 1877 When this parameter is true, it cleans out the R global environment by 1878 executing the following R statement:: 1879 1880 rm(list = ls()) 1881 """), 1882 arcGISDisplayName=_(u'Clear R global environment after evaluating statements')) 1883 1840 1884 AddArgumentMetadata(R.Evaluate, u'variableNames', 1841 1885 typeMetadata=ListTypeMetadata(elementType=UnicodeStringTypeMetadata(minLength=1), canBeNone=True), … … 2137 2181 arcGISDisplayName=_(u'Text file')) 2138 2182 2183 CopyArgumentMetadata(R.Evaluate, u'clearGlobalEnv', R.EvaluateFile, u'clearGlobalEnv') 2139 2184 CopyArgumentMetadata(R.Evaluate, u'variableNames', R.EvaluateFile, u'variableNames') 2140 2185 CopyArgumentMetadata(R.Evaluate, u'variableValues', R.EvaluateFile, u'variableValues') -
MGET/Branches/Jason/PythonPackage/src/GeoEco/Statistics/Exploratory.py
r339 r400 239 239 arcGISDisplayName=_(u'Scatterplot Matrix for Table'), 240 240 arcGISToolCategory=_(u'Statistics\\Explore Data'), 241 isOutOfProcArcGISTool=True,242 241 dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 243 242 … … 459 458 arcGISDisplayName=_(u'Density Histogram for Field'), 460 459 arcGISToolCategory=_(u'Statistics\\Explore Data'), 461 isOutOfProcArcGISTool=True,462 460 dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 463 461 … … 553 551 arcGISDisplayName=_(u'Density Histogram for Point Coordinate'), 554 552 arcGISToolCategory=_(u'Statistics\\Explore Data'), 555 isOutOfProcArcGISTool=True,556 553 dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 557 554 -
MGET/Branches/Jason/PythonPackage/src/GeoEco/Statistics/Modeling.py
r398 r400 775 775 if templateRaster is None: 776 776 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 781 785 782 786 # Load the fitted model. … … 868 872 # First prepare the input rasters for the prediction. 869 873 870 _PreparePredictorRasters(r, gp, tempDir, variableNames, inputPredictorRasters, templateRaster, resamplingTechniques)874 _PreparePredictorRasters(r, gp, tempDir, variableNames, inputPredictorRasters, templateRaster, templateDescribe, coordinateSystem, cellSize, extent, resamplingTechniques) 871 875 872 876 # Do the prediction. The prediction rasters are … … 945 949 r('if (exists("fam20985982305")) rm("fam20985982305", pos=globalenv())') 946 950 947 def _PreparePredictorRasters(r, gp, tempDir, variableNames, inputPredictorRasters, templateRaster, resamplingTechniques):948 949 # Build a dictionary that maps predictor variable names in 950 # themodel to rasters provided by the caller.951 def _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. 951 955 952 956 rastersForPredictors = {} … … 965 969 rastersForPredictors[variable] = inputPredictorRasters[variableNames.index(variable)] 966 970 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. 969 972 970 973 if createXRaster: … … 992 995 rastersForPredictors[r['yVar']] = yRaster 993 996 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, 997 999 # 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. 998 1004 999 1005 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) 1000 1009 1001 1010 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. … … 1004 1013 1005 1014 for i in range(len(vars)): 1006 needToProject = True 1015 if rasters[i] == templateRaster: 1016 continue 1017 1018 needToProject = False 1019 needToClip = False 1007 1020 d = gp.Describe(rasters[i]) 1008 1021 cs = gp.CreateSpatialReference_management(d.SpatialReference).split(u';')[0] 1009 1022 1010 1023 if cs != coordinateSystem: 1011 1024 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 1016 1028 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 1018 1075 1019 1076 if needToProject: … … 1142 1199 arcGISDisplayName=_(u'Fit GLM'), 1143 1200 arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Linear Models'), 1144 isOutOfProcArcGISTool=True,1145 1201 dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 1146 1202 … … 1567 1623 arcGISDisplayName=_(u'Predict GLM From Rasters'), 1568 1624 arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Linear Models'), 1569 isOutOfProcArcGISTool=True,1570 1625 dependencies=[ArcGISDependency(9, 2), RDependency(2, 5, 0), RPackageDependency(u'rgdal')]) 1571 1626 … … 1822 1877 arcGISDisplayName=_(u'Fit GAM'), 1823 1878 arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Additive Models'), 1824 isOutOfProcArcGISTool=True,1825 1879 dependencies=[ArcGISDependency(9, 1), RDependency(2, 5, 0)]) 1826 1880 … … 2005 2059 arcGISDisplayName=_(u'Predict GAM From Rasters'), 2006 2060 arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Additive Models'), 2007 isOutOfProcArcGISTool=True,2008 2061 dependencies=[ArcGISDependency(9, 2), RDependency(2, 5, 0), RPackageDependency(u'rgdal')]) 2009 2062 … … 2038 2091 arcGISDisplayName=_(u'Predict Bayesian Probabilities for GAM From Rasters'), 2039 2092 arcGISToolCategory=_(u'Statistics\\Model Data\\Generalized Additive Models'), 2040 isOutOfProcArcGISTool=True,2041 2093 dependencies=[ArcGISDependency(9, 2), RDependency(2, 5, 0), RPackageDependency(u'mgcv'), RPackageDependency(u'MASS'), RPackageDependency(u'rgdal')]) 2042 2094 … … 2157 2209 arcGISDisplayName=_(u'Plot Performance of Binary Classification Model'), 2158 2210 arcGISToolCategory=_(u'Statistics\\Model Data\\Evaluate Model Performance'), 2159 isOutOfProcArcGISTool=True,2160 2211 dependencies=[RDependency(2, 5, 0), RPackageDependency(u'ROCR')]) 2161 2212 … … 2501 2552 arcGISDisplayName=_(u'Plot ROC of Binary Classification Model'), 2502 2553 arcGISToolCategory=_(u'Statistics\\Model Data\\Evaluate Model Performance'), 2503 isOutOfProcArcGISTool=True,2504 2554 dependencies=[RDependency(2, 5, 0), RPackageDependency(u'ROCR')]) 2505 2555
