def ODW(Wo, Wd, transform='r'): """ Constructs an o*d by o*d origin-destination style spatial weight for o*d flows using standard spatial weights on o origins and d destinations. Input spatial weights must be binary or able to be sutiably transformed to binary. Parameters ---------- Wo : W object for origin locations o x o spatial weight object amongst o origins Wd : W object for destination locations d x d spatial weight object amongst d destinations transform : Transformation for standardization of final OD spatial weight; default is 'r' for row standardized Returns ------- W : spatial contiguity W object for assocations between flows o*d x o*d spatial weight object amongst o*d flows between o origins and d destinations Examples -------- >>> O = pysal.weights.lat2W(2,2) >>> D = pysal.weights.lat2W(2,2) >>> OD = pysal.weights.spintW.ODW(O,D) >>> OD.weights[0] [0.25, 0.25, 0.25, 0.25] >>> OD.neighbors[0] array([ 5, 6, 9, 10], dtype=int32) >>> OD.full()[0][0] array([ 0. , 0. , 0. , 0. , 0. , 0.25, 0.25, 0. , 0. , 0.25, 0.25, 0. , 0. , 0. , 0. , 0. ]) """ if Wo.transform is not 'b': try: Wo.tranform = 'b' except: raise AttributeError( 'Wo is not binary and cannot be transformed to ' 'binary. Wo must be binary or suitably transformed to binary.') if Wd.transform is not 'b': try: Wd.tranform = 'b' except: raise AttributeError( 'Wd is not binary and cannot be transformed to ' 'binary. Wd must be binary or suitably transformed to binary.') Wo = Wo.sparse Wo.eliminate_zeros() Wd = Wd.sparse Wd.eliminate_zeros() Ww = kron(Wo, Wd, format='csr') Ww.eliminate_zeros() Ww = WSP2W(WSP(Ww)) Ww.transform = transform return Ww
def _distance_to_W(self, ids=None): if self.binary: self.dmat[self.dmat > 0] = 1 self.dmat.eliminate_zeros() tempW = WSP2W(WSP(self.dmat), silent_island_warning=self.silent) neighbors = tempW.neighbors weight_keys = tempW.weights.keys() weight_vals = tempW.weights.values() weights = dict(zip(weight_keys, map(list, weight_vals))) return neighbors, weights else: weighted = self.dmat.power(self.alpha) weighted[weighted == np.inf] = 0 weighted.eliminate_zeros() tempW = WSP2W(WSP(weighted), silent_island_warning=self.silent) neighbors = tempW.neighbors weight_keys = tempW.weights.keys() weight_vals = tempW.weights.values() weights = dict(zip(weight_keys, map(list, weight_vals))) return neighbors, weights