def poly2Weights(ssdo, contiguityType="ROOK", rowStandard=True): """Uses GP Polygon Neighbor Tool to construct contiguity relationships and stores them in PySAL Sparse Spatial Weights class. INPUTS: ssdo (class): instance of SSDataObject [1] contiguityType {str, ROOK}: ROOK or QUEEN contiguity rowStandard {bool, True}: whether to row standardize the spatial weights NOTES: (1) Data must already be obtained using ssdo.obtainData() or ssdo.obtainDataGA () """ neighbors = {} weights = {} polyNeighDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.masterField, contiguityType=contiguityType) for masterID, neighIDs in UTILS.iteritems(polyNeighDict): orderID = ssdo.master2Order[masterID] neighbors[orderID] = [ssdo.master2Order[i] for i in neighIDs] w = PYSAL.W(neighbors) if rowStandard: w.transform = 'R' return w
def poly2Weights(ssdo, contiguityType = "ROOK", rowStandard = True): """Uses GP Polygon Neighbor Tool to construct contiguity relationships and stores them in PySAL Sparse Spatial Weights class. INPUTS: ssdo (class): instance of SSDataObject [1] contiguityType {str, ROOK}: ROOK or QUEEN contiguity rowStandard {bool, True}: whether to row standardize the spatial weights NOTES: (1) Data must already be obtained using ssdo.obtainData() or ssdo.obtainDataGA () """ neighbors = {} weights = {} polyNeighDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.masterField, contiguityType = contiguityType) for masterID, neighIDs in UTILS.iteritems(polyNeighDict): orderID = ssdo.master2Order[masterID] neighbors[orderID] = [ssdo.master2Order[i] for i in neighIDs] w = PYSAL.W(neighbors) if rowStandard: w.transform = 'R' return w
def buildWeights(self): """Performs Contiguity-based Weights Creation.""" ARCPY.SetProgressor("default", "Constructing spatial weights object...") #### Shorthand Attributes #### ssdo = self.ssdo isLowOrder = self.isLowOrder weightOrder = self.weightOrder #### Get Neighbor Dictionary for All Polygons #### master2Order = ssdo.master2Order polyNeighborDict = WU.polygonNeighborDict(self.inputFC, \ self.masterField, \ contiguityType=self.weightType) #### Assign empty list to polygons without neighbors #### if ssdo.numObs > len(polyNeighborDict): for masterKey in master2Order.keys(): if not polyNeighborDict.has_key(masterKey): polyNeighborDict[masterKey] = [] #### Convert DefaultDict to Real Dict ?#### if not self.idField: polyNeighborCopy = {} for key in polyNeighborDict.keys(): polyNeighborCopy[master2Order[key]] = [] for item in polyNeighborDict[key]: polyNeighborCopy[master2Order[key]].\ append(master2Order[item]) polyNeighborDict = polyNeighborCopy #### Create a PySAL W Object #### weightObj = W(polyNeighborDict) #### Building up Lower Order Spatial Weights #### if weightOrder > 1: ARCPY.SetProgressor("default", \ "Building up Lower Order Spatial Weights...") origWeight = weightObj weightObj = PYSAL.higher_order(weightObj, weightOrder) if isLowOrder: for order in xrange(weightOrder - 1, 1, -1): lowOrderW = PYSAL.higher_order(origWeight, order) weightObj = PYSAL.w_union(weightObj, lowOrderW) weightObj = PYSAL.w_union(weightObj, origWeight) #### Save weightObj Class Object for Writing Result #### self.weightObj = weightObj
def buildWeights(self): """Performs Contiguity-based Weights Creation.""" ARCPY.SetProgressor("default", "Constructing spatial weights object...") #### Shorthand Attributes #### ssdo = self.ssdo isLowOrder = self.isLowOrder weightOrder = self.weightOrder #### Get Neighbor Dictionary for All Polygons #### master2Order = ssdo.master2Order polyNeighborDict = WU.polygonNeighborDict(self.inputFC, \ self.masterField, \ contiguityType=self.weightType) #### Assign empty list to polygons without neighbors #### if ssdo.numObs > len(polyNeighborDict): for masterKey in master2Order.keys(): if not polyNeighborDict.has_key(masterKey): polyNeighborDict[masterKey] = [] #### Convert DefaultDict to Real Dict ?#### if not self.idField: polyNeighborCopy = {} for key in polyNeighborDict.keys(): polyNeighborCopy[master2Order[key]] = [] for item in polyNeighborDict[key]: polyNeighborCopy[master2Order[key]].\ append(master2Order[item]) polyNeighborDict = polyNeighborCopy #### Create a PySAL W Object #### weightObj = W(polyNeighborDict) #### Building up Lower Order Spatial Weights #### if weightOrder > 1: ARCPY.SetProgressor("default", \ "Building up Lower Order Spatial Weights...") origWeight = weightObj weightObj = PYSAL.higher_order(weightObj, weightOrder) if isLowOrder: for order in xrange(weightOrder-1, 1, -1): lowOrderW = PYSAL.higher_order(origWeight, order) weightObj = PYSAL.w_union(weightObj, lowOrderW) weightObj = PYSAL.w_union(weightObj, origWeight) #### Save weightObj Class Object for Writing Result #### self.weightObj = weightObj
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile potentialField = self.potentialField #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("ERROR", 906) raise SystemExit() #### Create Summed Variables #### self.intRange = NUM.arange(numObs) self.floatN = self.numObs * 1.0 ySum = self.y.sum() ySum2 = (self.y**2.0).sum() self.yBar = ySum / self.floatN self.S = NUM.sqrt((ySum2 / self.floatN) - self.yBar**2.0) self.nm1 = self.floatN - 1.0 #### Create Base Data Structures/Variables #### self.gi = NUM.zeros(numObs) self.pVals = NUM.ones(numObs) if self.permutations: self.pseudoPVals = NUM.ones(numObs) #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard self.swm = swm #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("ERROR", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.keys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType=contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, self.numNeighs, gaConcept) iterVals = range(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type=wType, exponent=exponent, row_standard=False, include_self=True) #### Create Progressor #### msg = ARCPY.GetIDMessage(84007) if self.permutations: msg += ": Using Permutations = %i" % self.permutations ARCPY.SetProgressor("step", msg, 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.y, rowStandard=rowStandard, potVals=self.potVals) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.y, potVals=self.potVals, allowSelf=True) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.y, rowStandard=False, potVals=self.potVals) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF_Potent( neighWeights, i, self.y, self.potVals) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiVal, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo( masterID, nhIDs, nhVals, weights) if nn: #### Calculate Local G #### self.calculateGI(orderID, yiVal, nhVals, weights) ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors(failAllNoNeighs=False) self.setNullValues(ni.idsNoNeighs) #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni #### Set p-values for Gi Bins #### if self.permutations: #### Use Pseudo p-values #### pv = self.pseudoPVals else: #### Use Traditional p-values #### pv = self.pVals toolMSG = ARCPY.GetIDMessage(84466) if self.applyFDR: #### Set Bins Using FDR #### msg = ARCPY.GetIDMessage(84472).format(toolMSG) ARCPY.SetProgressor("default", msg) self.giBins = STATS.fdrTransform(pv, self.gi) else: msg = ARCPY.GetIDMessage(84473).format(toolMSG) ARCPY.SetProgressor("default", msg) self.giBins = STATS.pValueBins(pv, self.gi)
def polygon2SWM(inputFC, swmFile, masterField, concept = "EUCLIDEAN", kNeighs = 0, rowStandard = True, contiguityType = "ROOK"): """Creates a sparse spatial weights matrix (SWM) based on polygon contiguity. INPUTS: inputFC (str): path to the input feature class swmFile (str): path to the SWM file. masterField (str): field in table that serves as the mapping. concept: {str, EUCLIDEAN}: EUCLIDEAN or MANHATTAN kNeighs {int, 0}: number of neighbors to return (1) rowStandard {bool, True}: row standardize weights? contiguityType {str, Rook}: {Rook = Edges Only, Queen = Edges/Vertices} NOTES: (1) kNeighs is used if polygon is not contiguous. E.g. Islands """ #### Set Default Progressor for Neigborhood Structure #### ARCPY.SetProgressor("default", ARCPY.GetIDMessage(84143)) #### Create SSDataObject #### ssdo = SSDO.SSDataObject(inputFC, templateFC = inputFC, useChordal = True) cnt = UTILS.getCount(inputFC) ERROR.errorNumberOfObs(cnt, minNumObs = 2) #### Validation of Master Field #### verifyMaster = ERROR.checkField(ssdo.allFields, masterField, types = [0,1]) #### Create GA Data Structure #### gaTable, gaInfo = WU.gaTable(ssdo.catPath, [masterField], spatRef = ssdo.spatialRefString) #### Assure Enough Observations #### N = gaInfo[0] ERROR.errorNumberOfObs(N, minNumObs = 2) #### Assure k-Nearest is Less Than Number of Features #### if kNeighs >= N: ARCPY.AddIDMessage("ERROR", 975) raise SystemExit() #### Create Nearest Neighbor Search Type For Islands #### if kNeighs > 0: gaSearch = GAPY.ga_nsearch(gaTable) concept, gaConcept = WU.validateDistanceMethod(concept, ssdo.spatialRef) gaSearch.init_nearest(0.0, kNeighs, gaConcept) forceNeighbor = True neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = 1, row_standard = False) else: forceNeighbor = False neighSearch = None #### Create Polygon Neighbors #### polyNeighborDict = WU.polygonNeighborDict(inputFC, masterField, contiguityType = contiguityType) #### Write Poly Neighbor List (Dict) #### #### Set Progressor for SWM Writing #### ARCPY.SetProgressor("step", ARCPY.GetIDMessage(84127), 0, N, 1) #### Initialize Spatial Weights Matrix File #### if contiguityType == "ROOK": wType = 4 else: wType = 5 swmWriter = WU.SWMWriter(swmFile, masterField, ssdo.spatialRefName, N, rowStandard, inputFC = inputFC, wType = wType, distanceMethod = concept, numNeighs = kNeighs) #### Keep Track of Polygons w/o Neighbors #### islandPolys = [] #### Write Polygon Contiguity to SWM File #### for row in xrange(N): rowInfo = gaTable[row] oid = rowInfo[0] masterID = rowInfo[2] neighs = polyNeighborDict[masterID] nn = len(neighs) if forceNeighbor: if nn < kNeighs: #### Only Force KNN If Specified & Contiguity is Less #### islandPolys.append(oid) flag = True knnNeighs, knnWeights = neighWeights[row] c = 0 while flag: try: neighID = gaTable[knnNeighs[c]][2] if neighID not in neighs: neighs.append(neighID) nn += 1 if nn == kNeighs: flag = False c += 1 except: flag = False weights = NUM.ones(nn) #### Add Weights Entry #### swmWriter.swm.writeEntry(masterID, neighs, weights) #### Set Progress #### ARCPY.SetProgressorPosition() #### Report on Features with No Neighbors #### countIslands = len(islandPolys) if countIslands: islandPolys.sort() if countIslands > 30: islandPolys = islandPolys[0:30] ERROR.warningNoNeighbors(N, countIslands, islandPolys, ssdo.oidName, forceNeighbor = forceNeighbor, contiguity = True) #### Clean Up #### swmWriter.close() del gaTable #### Report Spatial Weights Summary #### swmWriter.report() #### Report SWM File is Large #### swmWriter.reportLargeSWM() del polyNeighborDict
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("Error", 906) raise SystemExit() #### Create Deviation Variables #### self.yBar = NUM.mean(self.y) self.yDev = self.y - self.yBar self.nm1 = numObs - 1. self.nm2 = numObs - 2. self.nm12 = self.nm1 * self.nm2 yDev2 = self.yDev**2.0 yDev2Norm = yDev2 / self.nm1 self.yDev2NormSum = sum(yDev2Norm) yDev4 = self.yDev**4.0 yDev4Norm = yDev4 / self.nm1 yDev4NormSum = sum(yDev4Norm) self.b2i = yDev4NormSum / (self.yDev2NormSum**2.0) #### Create Base Data Structures/Variables #### self.li = NUM.zeros(numObs) self.ei = NUM.zeros(numObs) self.vi = NUM.zeros(numObs) self.zi = NUM.zeros(numObs) self.pVals = NUM.ones(numObs) if self.permutations: self.pseudoPVals = NUM.ones(numObs) self.moranInfo = {} #### Keep Track of Features with No Neighbors #### self.idsNoNeighs = [] #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard self.swm = swm #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("Error", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.keys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType = contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, 0, gaConcept) iterVals = range(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = wType, exponent = exponent, row_standard = rowStandard) #### Create Progressor #### msg = ARCPY.GetIDMessage(84007) if self.permutations: msg += ": Using Permutations = %i" % self.permutations ARCPY.SetProgressor("step", msg , 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.yDev, rowStandard = rowStandard, isSubSet = isSubSet) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.yDev) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.yDev, rowStandard = rowStandard) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF(neighWeights, i, self.yDev) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiDev, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo(masterID, nhIDs, nhVals, weights) if nn: #### Calculate Local I #### self.calculateLI(orderID, yiDev, nhVals, weights) ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors() self.setNullValues(ni.idsNoNeighs) #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni #### Set p-values for Gi Bins #### if self.permutations: #### Use Pseudo p-values #### pv = self.pseudoPVals else: #### Use Traditional p-values #### pv = self.pVals #### Calculate FDR and Moran Bins #### toolMSG = ARCPY.GetIDMessage(84474) if self.applyFDR: #### Set Bins Using FDR #### msg = ARCPY.GetIDMessage(84472).format(toolMSG) ARCPY.SetProgressor("default", msg) fdrBins = STATS.fdrTransform(pv, self.li) self.moranBins = STATS.moranBinFromPVals(pv, self.moranInfo, fdrBins = fdrBins) else: msg = ARCPY.GetIDMessage(84473).format(toolMSG) ARCPY.SetProgressor("default", msg) self.moranBins = STATS.moranBinFromPVals(pv, self.moranInfo)
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("Error", 906) raise SystemExit() #### Create Deviation Variables #### self.yBar = NUM.mean(self.y) self.yDev = self.y - self.yBar #### Create Base Data Structures/Variables #### self.numer = 0.0 self.denom = NUM.sum(self.yDev**2.0) self.rowSum = NUM.zeros(numObs) self.colSum = NUM.zeros(numObs) self.s0 = 0 self.s1 = 0 self.wij = {} #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("Error", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.keys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType = contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, 0, gaConcept) iterVals = range(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = wType, exponent = exponent, row_standard = rowStandard) #### Create Progressor #### ARCPY.SetProgressor("step", ARCPY.GetIDMessage(84007), 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.yDev, rowStandard = rowStandard, isSubSet = isSubSet) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.yDev) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.yDev, rowStandard = rowStandard) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF(neighWeights, i, self.yDev) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiDev, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo(masterID, nhIDs, nhVals, weights) if nn: #### Process Feature Contribution to Moran's I #### self.processRow(orderID, yiDev, nhIDs, nhVals, weights) #### Reset Progessor #### ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors() #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni
def construct(self): """Constructs the neighborhood structure for each feature and dispatches the appropriate values for the calculation of the statistic.""" #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept gaConcept = concept.lower() threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard numObs = self.numObs master2Order = self.master2Order masterField = ssdo.masterField weightsFile = self.weightsFile #### Check That All Input Values are Positive #### if NUM.sum(self.y < 0.0) != 0: ARCPY.AddIDMessage("Error", 915) raise SystemExit() #### Assure that Variance is Larger than Zero #### yVar = NUM.var(self.y) if NUM.isnan(yVar) or yVar <= 0.0: ARCPY.AddIDMessage("Error", 906) raise SystemExit() #### Create Base Data Structures/Variables #### self.numer = 0.0 self.denom = 0.0 self.rowSum = NUM.zeros(numObs) self.colSum = NUM.zeros(numObs) self.ySum = 0.0 self.y2Sum = 0.0 self.y3Sum = 0.0 self.y4Sum = 0.0 self.s0 = 0 self.s1 = 0 self.wij = {} #### Set Neighborhood Structure Type #### if self.weightsFile: if self.swmFileBool: #### Open Spatial Weights and Obtain Chars #### swm = WU.SWMReader(weightsFile) N = swm.numObs rowStandard = swm.rowStandard #### Check to Assure Complete Set of Weights #### if numObs > N: ARCPY.AddIDMessage("Error", 842, numObs, N) raise SystemExit() #### Check if Selection Set #### isSubSet = False if numObs < N: isSubSet = True iterVals = xrange(N) else: #### Warning for GWT with Bad Records/Selection #### if ssdo.selectionSet or ssdo.badRecords: ARCPY.AddIDMessage("WARNING", 1029) #### Build Weights Dictionary #### weightDict = WU.buildTextWeightDict(weightsFile, master2Order) iterVals = master2Order.iterkeys() N = numObs elif wType in [4, 5]: #### Polygon Contiguity #### if wType == 4: contiguityType = "ROOK" else: contiguityType = "QUEEN" contDict = WU.polygonNeighborDict(ssdo.inputFC, ssdo.oidName, contiguityType = contiguityType) iterVals = master2Order.keys() N = numObs else: gaTable = ssdo.gaTable gaSearch = GAPY.ga_nsearch(gaTable) if wType == 7: #### Zone of Indiff, All Related to All #### gaSearch.init_nearest(threshold, numObs, gaConcept) else: #### Inverse and Fixed Distances #### gaSearch.init_nearest(threshold, 0, gaConcept) iterVals = xrange(numObs) N = numObs neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = wType, exponent = exponent, row_standard = rowStandard) #### Create Progressor #### ARCPY.SetProgressor("step", ARCPY.GetIDMessage(84007), 0, N, 1) #### Create Neighbor Info Class #### ni = WU.NeighborInfo(masterField) #### Calculation For Each Feature #### for i in iterVals: if self.swmFileBool: #### Using SWM File #### info = swm.swm.readEntry() masterID = info[0] if master2Order.has_key(masterID): rowInfo = WU.getWeightsValuesSWM(info, master2Order, self.y, rowStandard = rowStandard, isSubSet = isSubSet) includeIt = True else: includeIt = False elif self.weightsFile and not self.swmFileBool: #### Text Weights #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesText(masterID, master2Order, weightDict, self.y) elif wType in [4, 5]: #### Polygon Contiguity #### masterID = i includeIt = True rowInfo = WU.getWeightsValuesCont(masterID, master2Order, contDict, self.y, rowStandard = rowStandard) else: #### Distance Based #### masterID = gaTable[i][0] includeIt = True rowInfo = WU.getWeightsValuesOTF(neighWeights, i, self.y) #### Subset Boolean for SWM File #### if includeIt: #### Parse Row Info #### orderID, yiVal, nhIDs, nhVals, weights = rowInfo #### Assure Neighbors Exist After Selection #### nn, nhIDs, nhVals, weights = ni.processInfo(masterID, nhIDs, nhVals, weights) if nn: #### Process Feature Contribution to General G #### self.processRow(orderID, yiVal, nhIDs, nhVals, weights) #### Reset Progessor #### ARCPY.SetProgressorPosition() #### Clean Up #### if self.swmFileBool: swm.close() #### Report on Features with No Neighbors #### ni.reportNoNeighbors() #### Report on Features with Large Number of Neighbors #### ni.reportWarnings() ni.reportMaximums() self.neighInfo = ni
def polygon2SWM(inputFC, swmFile, masterField, concept = "EUCLIDEAN", kNeighs = 0, rowStandard = True, contiguityType = "ROOK"): """Creates a sparse spatial weights matrix (SWM) based on polygon contiguity. INPUTS: inputFC (str): path to the input feature class swmFile (str): path to the SWM file. masterField (str): field in table that serves as the mapping. concept: {str, EUCLIDEAN}: EUCLIDEAN or MANHATTAN kNeighs {int, 0}: number of neighbors to return (1) rowStandard {bool, True}: row standardize weights? contiguityType {str, Rook}: {Rook = Edges Only, Queen = Edges/Vertices} NOTES: (1) kNeighs is used if polygon is not contiguous. E.g. Islands """ #### Set Default Progressor for Neigborhood Structure #### ARCPY.SetProgressor("default", ARCPY.GetIDMessage(84143)) #### Create SSDataObject #### ssdo = SSDO.SSDataObject(inputFC, templateFC = inputFC, useChordal = True) cnt = UTILS.getCount(inputFC) ERROR.errorNumberOfObs(cnt, minNumObs = 2) #### Validation of Master Field #### verifyMaster = ERROR.checkField(ssdo.allFields, masterField, types = [0,1]) #### Create GA Data Structure #### gaTable, gaInfo = WU.gaTable(ssdo.catPath, [masterField], spatRef = ssdo.spatialRefString) #### Assure Enough Observations #### N = gaInfo[0] ERROR.errorNumberOfObs(N, minNumObs = 2) #### Assure k-Nearest is Less Than Number of Features #### if kNeighs >= N: ARCPY.AddIDMessage("ERROR", 975) raise SystemExit() #### Create Nearest Neighbor Search Type For Islands #### gaSearch = GAPY.ga_nsearch(gaTable) concept, gaConcept = WU.validateDistanceMethod(concept, ssdo.spatialRef) gaSearch.init_nearest(0.0, kNeighs, gaConcept) if kNeighs > 0: forceNeighbor = True neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = 1, row_standard = False) else: forceNeighbor = False neighSearch = None #### Create Polygon Neighbors #### polyNeighborDict = WU.polygonNeighborDict(inputFC, masterField, contiguityType = contiguityType) #### Write Poly Neighbor List (Dict) #### #### Set Progressor for SWM Writing #### ARCPY.SetProgressor("step", ARCPY.GetIDMessage(84127), 0, N, 1) #### Initialize Spatial Weights Matrix File #### if contiguityType == "ROOK": wType = 4 else: wType = 5 swmWriter = WU.SWMWriter(swmFile, masterField, ssdo.spatialRefName, N, rowStandard, inputFC = inputFC, wType = wType, distanceMethod = concept, numNeighs = kNeighs) #### Keep Track of Polygons w/o Neighbors #### islandPolys = [] #### Write Polygon Contiguity to SWM File #### for row in xrange(N): rowInfo = gaTable[row] oid = rowInfo[0] masterID = rowInfo[2] neighs = polyNeighborDict[masterID] if neighs: weights = [ 1. for nh in neighs ] isIsland = False else: isIsland = True islandPolys.append(oid) weights = [] #### Get Nearest Neighbor Based On Centroid Distance #### if isIsland and forceNeighbor: neighs, weights = neighWeights[row] neighs = [ gaTable[nh][2] for nh in neighs ] #### Add Weights Entry #### swmWriter.swm.writeEntry(masterID, neighs, weights) #### Set Progress #### ARCPY.SetProgressorPosition() #### Report on Features with No Neighbors #### countIslands = len(islandPolys) if countIslands: islandPolys.sort() if countIslands > 30: islandPolys = islandPolys[0:30] ERROR.warningNoNeighbors(N, countIslands, islandPolys, ssdo.oidName, forceNeighbor = forceNeighbor, contiguity = True) #### Clean Up #### swmWriter.close() del gaTable #### Report Spatial Weights Summary #### swmWriter.report() #### Report SWM File is Large #### swmWriter.reportLargeSWM() del polyNeighborDict