def test_regime_weights(self): regimes = np.ones(25) regimes[range(10, 20)] = 2 regimes[range(21, 25)] = 3 regimes = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1., 3., 3., 3., 3.]) w = pysal.regime_weights(regimes) ww0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] self.assertEquals(w.weights[0], ww0) wn0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] self.assertEquals(w.neighbors[0], wn0) regimes = ['n', 'n', 's', 's', 'e', 'e', 'w', 'w', 'e'] n = len(regimes) w = pysal.regime_weights(regimes) wn = {0: [1], 1: [0], 2: [3], 3: [2], 4: [5, 8], 5: [4, 8], 6: [7], 7: [6], 8: [4, 5]} self.assertEquals(w.neighbors, wn)
def bounded_world_from_shapefile(path, n, n_as=None): ''' Create W object for `n` agents with bounded locations assigned within polygons of a shapefile (neighbor if in the same polygon) in proportion to the polygon's area ... Arguments --------- path : str Link to shapefile containing the geography n : int Number of pixels to be created within the geography n_as : array [Optional] Sequence with number of of agents to be assigned to every neighborhood, in the order of the dbf accompaigning the shapefile. If not provided, proportions are based on area. Returns ------- W : pysal.W Weights object ns : ndarray Cardinalities for every observation to a neighborhood xys : ndarray Nx2 array with coordinates of agents (randomly within polygons) ''' if not n_as: shp = ps.open(path) n_shares = np.array([p.area for p in shp]) shp.close() n_shares = n_shares / n_shares.sum() n_as = np.round(n * n_shares).astype(int) n_as[-1] = n - n_as[:-1].sum() # Hack to get proportions to sum to n xys = np.zeros((n, 2), dtype=int) shp = ps.open(path) polys = list(shp) shp.close() parss = [(n_a, poly) for n_a, poly in zip(n_as, polys)] pool = mp.Pool(mp.cpu_count()) xys = pool.map(_random_pts_in_poly, parss) xys = np.concatenate(xys) ns = np.concatenate( [np.array([neigh] * nn) for neigh, nn in enumerate(n_as)]) w = ps.regime_weights(ns) return w, ns, xys
def bounded_world_from_shapefile(path, n, n_as=None): ''' Create W object for `n` agents with bounded locations assigned within polygons of a shapefile (neighbor if in the same polygon) in proportion to the polygon's area ... Arguments --------- path : str Link to shapefile containing the geography n : int Number of pixels to be created within the geography n_as : array [Optional] Sequence with number of of agents to be assigned to every neighborhood, in the order of the dbf accompaigning the shapefile. If not provided, proportions are based on area. Returns ------- W : pysal.W Weights object ns : ndarray Cardinalities for every observation to a neighborhood xys : ndarray Nx2 array with coordinates of agents (randomly within polygons) ''' if not n_as: shp = ps.open(path) n_shares = np.array([p.area for p in shp]) shp.close() n_shares = n_shares / n_shares.sum() n_as = np.round(n * n_shares).astype(int) n_as[-1] = n - n_as[:-1].sum() # Hack to get proportions to sum to n xys = np.zeros((n, 2), dtype=int) shp = ps.open(path) polys = list(shp) shp.close() parss = [(n_a, poly) for n_a, poly in zip(n_as, polys)] pool = mp.Pool(mp.cpu_count()) xys = pool.map(_random_pts_in_poly, parss) xys = np.concatenate(xys) ns = np.concatenate([np.array([neigh]*nn) for neigh, nn in enumerate(n_as)]) w = ps.regime_weights(ns) return w, ns, xys