def build_W(self, shapefile, type, idVariable=None): """ Building 2 W's the hard way. We need to do this so we can test both rtree and binning """ dbname = os.path.splitext(shapefile)[0] + '.dbf' db = pysal.open(dbname) shpObj = pysal.open(shapefile) neighbor_data = ContiguityWeights_rtree(shpObj, type).w neighbors = {} weights = {} if idVariable: ids = db.by_col[idVariable] self.assertEqual(len(ids), len(set(ids))) for key in neighbor_data: id = ids[key] if id not in neighbors: neighbors[id] = set() neighbors[id].update([ids[x] for x in neighbor_data[key]]) for key in neighbors: neighbors[key] = list(neighbors[key]) rtreeW = pysal.W(neighbors, id_order=ids) else: neighbors[key] = list(neighbors[key]) rtreeW = pysal.W(neighbors) shpObj.seek(0) return rtreeW
qb = ContiguityWeights_binning(pysal.open(fname), QUEEN) t1 = time.time() print("using " + str(fname)) print("time elapsed for queen... using bins: " + str(t1 - t0)) t0 = time.time() rb = ContiguityWeights_binning(pysal.open(fname), ROOK) t1 = time.time() print('Rook binning') print("using " + str(fname)) print("time elapsed for rook... using bins: " + str(t1 - t0)) from pysal.weights._contW_rtree import ContiguityWeights_rtree t0 = time.time() rt = ContiguityWeights_rtree(pysal.open(fname), ROOK) t1 = time.time() print("time elapsed for rook... using rtree: " + str(t1 - t0)) print(rt.w == rb.w) print('QUEEN') t0 = time.time() qt = ContiguityWeights_rtree(pysal.open(fname), QUEEN) t1 = time.time() print("using " + str(fname)) print("time elapsed for queen... using rtree: " + str(t1 - t0)) print(qb.w == qt.w) print('knn4') t0 = time.time()
def setUp(self): """ Setup the rtree contiguity weights""" shpObj = pysal.open(pysal.examples.get_path('virginia.shp'), 'r') self.rtreeW = ContiguityWeights_rtree(shpObj, QUEEN) shpObj.close()