def test_threshold_binaryW_from_array(self): points = [(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] w = pysal.threshold_binaryW_from_array(points, threshold=11.2) self.assertEquals(w.weights, {0: [1, 1], 1: [1, 1], 2: [], 3: [1, 1], 4: [1], 5: [1]}) self.assertEquals(w.neighbors, {0: [1, 3], 1: [0, 3], 2: [ ], 3: [1, 0], 4: [5], 5: [4]})
def calc_morans_i(self, raster_as_array, lag_distance=10): """ Calculates the value of Moran's I at given lag distances using Distance Band Weights; Default ranges from 1 pixel to 10 pixels In this particular project, that translates to: 1 pixel (30 m) to 10 pixels (300 m) Reference: http://pysal.readthedocs.org/en/latest/users/tutorials/weights.html#distance-band-weights @param raster_as_array: the raster to use in the calculations as an array @param lag_distance: given lag distance @returns: an array of Moran's I values. """ self.raster_as_array = raster_as_array self.lag_distance = lag_distance _flattened_raster_array = self.raster_as_array.ravel() _x, _y = numpy.indices((self.rows, self.columns)) _x.shape = (self.rows * self.columns, 1) _y.shape = (self.rows * self.columns, 1) _horizontal_stack = numpy.hstack([_x, _y]) _Morans_I = numpy.zeros(self.lag_distance) # Get weights based on distance (distance-band method) and calculation Moran's I for i in range(1, self.lag_distance + 1): _wthresh = pysal.threshold_binaryW_from_array( _horizontal_stack, i) # distance-based weights _mi = pysal.Moran( _flattened_raster_array, _wthresh) # calculate Moran's I for given distance _Morans_I[ i - 1] = _mi.I # Value of individual result of Moran's I (_mi.I) saved into array return _Morans_I
def buildWeights(self): """Performs Distance-based Weights Creation""" ARCPY.SetProgressor("default", "Constructing spatial weights object...") #### Shorthand Attributes #### distanceType = self.distanceType threshold = self.threshold knnNum = self.knnNum idField = self.idField outputExt = self.outputExt ssdo = self.ssdo #### Create Distance-based WeightObj (0-based IDs) #### dataArray = ssdo.xyCoords if distanceType.upper() == DISTTYPE[0]: weightObj = PYSAL.threshold_binaryW_from_array(dataArray, threshold) elif distanceType.upper() == DISTTYPE[1]: weightObj = PYSAL.knnW_from_array(dataArray, knnNum) elif distanceType.upper() == DISTTYPE[2]: alpha = -1 * self.inverseDist weightObj = PYSAL.threshold_continuousW_from_array(\ dataArray, threshold, alpha=alpha) #### Re-Create WeightObj for NOT 0-based idField #### if idField: if ssdo.master2Order.keys() != ssdo.master2Order.values(): o2M = ssdo.order2Master neighborDict = {o2M[oid] : [o2M[nid] for nid in nbrs] \ for oid,nbrs in weightObj.neighbors.items()} weightDict = {o2M[oid] : weights \ for oid, weights in weightObj.weights.items()} weightObj = W(neighborDict, weightDict) #### Save weightObj Class Object for Writing Result #### self.weightObj = weightObj
def buildWeights(self): """Performs Distance-based Weights Creation""" ARCPY.SetProgressor("default", "Constructing spatial weights object...") #### Shorthand Attributes #### distanceType = self.distanceType threshold = self.threshold knnNum = self.knnNum idField = self.idField outputExt = self.outputExt ssdo = self.ssdo #### Create Distance-based WeightObj (0-based IDs) #### dataArray = ssdo.xyCoords if distanceType.upper() == DISTTYPE[0]: weightObj = PYSAL.threshold_binaryW_from_array( dataArray, threshold) elif distanceType.upper() == DISTTYPE[1]: weightObj = PYSAL.knnW_from_array(dataArray, knnNum) elif distanceType.upper() == DISTTYPE[2]: alpha = -1 * self.inverseDist weightObj = PYSAL.threshold_continuousW_from_array(\ dataArray, threshold, alpha=alpha) #### Re-Create WeightObj for NOT 0-based idField #### if idField: if ssdo.master2Order.keys() != ssdo.master2Order.values(): o2M = ssdo.order2Master neighborDict = {o2M[oid] : [o2M[nid] for nid in nbrs] \ for oid,nbrs in weightObj.neighbors.items()} weightDict = {o2M[oid] : weights \ for oid, weights in weightObj.weights.items()} weightObj = W(neighborDict, weightDict) #### Save weightObj Class Object for Writing Result #### self.weightObj = weightObj