def map_length(map_df): dist = 0 for rid in map_df.rid.unique(): road = map_df[map_df.rid == rid][['x', 'y']].values for u, v in trajmap.pairwise(road): dist += math.sqrt((u[0] - v[0])**2 + (u[1] - v[1])**2) return dist
def map_df2nx(map_df): #pset = set() pdict = {} idict = {} idx = 0 for x, y in zip(map_df.lon, map_df.lat): #x = float('%.6f'%x) #y = float('%.6f'%y) _x, _y = x, y x = precision(float(x), 6) y = precision(float(y), 6) pstr = str((x,y)) if pdict.has_key(pstr) is False: pdict[pstr] = idx idict[idx] = (x,y) idx += 1 G = nx.Graph() for rid in map_df.rid.unique(): x = map_df[map_df.rid == rid].lon y = map_df[map_df.rid == rid].lat for (x0, y0), (x1, y1) in trajmap.pairwise(zip(x, y)): #x0 = float('%.6f'%x0) #y0 = float('%.6f'%y0) #x1 = float('%.6f'%x1) #y1 = float('%.6f'%y1) x0 = precision(float(x0), 6) y0 = precision(float(y0), 6) x1 = precision(float(x1), 6) y1 = precision(float(y1), 6) G.add_edge(pdict[str((x0,y0))], pdict[str((x1, y1))]) return G, pdict, idict
def map_length(map_df): dist = 0 for rid in map_df.rid.unique(): road = map_df[map_df.rid==rid][['x','y']].values for u, v in trajmap.pairwise(road): dist += math.sqrt((u[0]-v[0])**2+(u[1]-v[1])**2) return dist
def get_G(x0, x1, y0, y1, width, height, side, data): G = nx.Graph() min_x, max_x, min_y, max_y = x0, x1, y0, y1 col = [] row = [] for tid in data.tid.unique(): themap = cv.CreateMat(height, width, cv.CV_16UC1) cv.SetZero(themap) for p in trajmap.pairwise(data[data.tid == tid].values): x0, y0, x1, y1 = p[0][1], p[0][2], p[1][1], p[1][2] oy = height - int((y0 - min_y) / side) ox = int((x0 - min_x) / side) dy = height - int((y1 - min_y) / side) dx = int((x1 - min_x) / side) cv.Line(themap, (ox, oy), (dx, dy), (32), 1, cv.CV_AA) node_set = set() for y, x in zip(*np.matrix(themap).nonzero()): node_set.add((x, y)) a = x + (height - y) * width for _x, _y in [(x - 1, y), (x, y - 1), (x - 1, y - 1), (x + 1, y), (x, y + 1), (x + 1, y + 1), (x - 1, y + 1), (x + 1, y - 1)]: if (_x, _y) in node_set: _a = _x + (height - _y) * width G.add_edge(a, _a) for tup in zip(*np.matrix(themap).nonzero()): row.append(tup[1] + (height - tup[0]) * width) col.append(tid) sag = scipy.sparse.csc_matrix(([1] * len(row), (row, col)), shape=(max(row) + 1, max(col) + 1)) return sag, G
def get_G(x0, x1, y0, y1, width, height, side, data): G = nx.Graph() min_x, max_x, min_y, max_y = x0, x1, y0, y1 col = [] row = [] for tid in data.tid.unique(): themap = cv.CreateMat(height, width, cv.CV_16UC1) cv.SetZero(themap) for p in trajmap.pairwise(data[data.tid == tid].values): x0, y0, x1, y1 = p[0][1], p[0][2], p[1][1], p[1][2] oy = height-int((y0 - min_y) / side) ox = int((x0 - min_x) / side) dy = height-int((y1 - min_y) / side) dx = int((x1 - min_x) / side) cv.Line(themap, (ox, oy), (dx, dy), (32), 1, cv.CV_AA) node_set = set() for y, x in zip(*np.matrix(themap).nonzero()): node_set.add((x,y)) a = x+(height-y)*width for _x, _y in [(x-1, y), (x, y-1), (x-1,y-1), (x+1, y), (x, y+1), (x+1, y+1), (x-1, y+1), (x+1, y-1)]: if (_x, _y) in node_set: _a = _x+(height-_y)*width G.add_edge(a,_a) for tup in zip(*np.matrix(themap).nonzero()): row.append(tup[1]+(height-tup[0])*width) col.append(tid) sag = scipy.sparse.csc_matrix(([1]*len(row), (row, col)),shape=(max(row)+1, max(col)+1)) return sag, G
def map_df2nx(map_df): #pset = set() pdict = {} idict = {} idx = 0 for x, y in zip(map_df.lon, map_df.lat): #x = float('%.6f'%x) #y = float('%.6f'%y) _x, _y = x, y x = precision(float(x), 6) y = precision(float(y), 6) pstr = str((x, y)) if pdict.has_key(pstr) is False: pdict[pstr] = idx idict[idx] = (x, y) idx += 1 G = nx.Graph() for rid in map_df.rid.unique(): x = map_df[map_df.rid == rid].lon y = map_df[map_df.rid == rid].lat for (x0, y0), (x1, y1) in trajmap.pairwise(zip(x, y)): #x0 = float('%.6f'%x0) #y0 = float('%.6f'%y0) #x1 = float('%.6f'%x1) #y1 = float('%.6f'%y1) x0 = precision(float(x0), 6) y0 = precision(float(y0), 6) x1 = precision(float(x1), 6) y1 = precision(float(y1), 6) G.add_edge(pdict[str((x0, y0))], pdict[str((x1, y1))]) return G, pdict, idict
def evaluate_map(gen_map_df, map_df, axis=None, step = 5.0, max_dist=20.0): map_df = scale_map_by_axis(map_df, axis) map_df.index = map_df.rid gen_map_df = scale_map_by_axis(gen_map_df, axis) gen_map_df.index = gen_map_df.rid idx = build_index(map_df, axis) step = step error = [] cnt = 0 total = len(gen_map_df.rid.unique()) for rid in gen_map_df.rid.unique(): if cnt % int(total/20) == 0: sys.stdout.write("\rComplete (" + str(cnt) + "/" + str(total) + ")... ") sys.stdout.flush() cnt += 1 road = gen_map_df[gen_map_df.rid == rid][['x','y']].values for u, v in trajmap.pairwise(road): #print u, v ux, uy, vx, vy = u[0], u[1], v[0], v[1] dist = math.sqrt((ux-vx)**2+(uy-vy)**2) n = int(1.0*dist/step) #print dist, n, step _x, _y = ux, uy tups = [(_x, _y)] for i in range(1, n): _x += (vx-ux)/n _y += (vy-uy)/n tups.append((_x, _y)) tups.append((vx, vy)) for _x, _y in tups: error.append(min_dist_rtree((_x, _y), idx, map_df)) error = sorted(error) result = {} result['error'] = {'min': min(error), 'max': max(error), 'mean': sum(error)/len(error), 'p25': error[int(len(error)*0.25)], 'p50': error[int(len(error)*0.5)], 'p75': error[int(len(error)*0.75)]} print result['error'] '''precision''' mlen = matched_length(map_df, gen_map_df, axis, max_dist) result['gen_matched_len'] = mlen result['gen_len'] = map_length(gen_map_df) result['precision'] = result['gen_matched_len'] / result['gen_len'] print result['precision'] '''recall''' mlen = matched_length(gen_map_df, map_df, axis, max_dist) result['map_matched_len'] = mlen result['map_len'] = map_length(map_df) result['recall'] = result['map_matched_len'] / result['map_len'] print result['recall'] result['fscore'] = 2*result['precision']*result['recall']/(result['precision']+result['recall']) return result
def trajectory(xs, ys, axis, figsize=(10, 10)): plt.figure(figsize=figsize) currentAxis = plt.gca() for a, b in trajmap.pairwise(zip(xs, ys)): ix, iy, jx, jy = a[0], a[1], b[0], b[1] arr = Arrow(ix, iy, jx - ix, jy - iy, width=0.0005, color='k') currentAxis.add_patch(arr) plt.axis(axis) plt.show()
def trajectory(xs, ys, axis, figsize=(10,10)): plt.figure(figsize=figsize) currentAxis = plt.gca() for a, b in trajmap.pairwise(zip(xs, ys)): ix, iy, jx, jy = a[0], a[1], b[0], b[1] arr = Arrow(ix,iy, jx-ix, jy-iy, width=0.0005, color='k') currentAxis.add_patch(arr) plt.axis(axis) plt.show()
def build_index(map_df, axis=None): if axis is not None: map_df = scale_map_by_axis(map_df, axis) from rtree import index idx = index.Index() for rid in map_df.rid.unique(): road = map_df[map_df.rid==rid][['x','y']] for u, v in trajmap.pairwise(road.values): idx.insert(rid, (min(u[0], v[0]), min(u[1],v[1]), max(u[0], v[0]), max(u[1],v[1]))) return idx
def dist_to_road(p, map_df, rid): road = map_df.ix[rid][['x', 'y']].values _min_dist = -1 for u, v in trajmap.pairwise(road): try: _dist = dist(p, (u[0], u[1], v[0], v[1])) if (_dist < _min_dist) or (_min_dist == -1): _min_dist = _dist except Exception, data: print 'Error: dist to road:', u, v continue
def build_index(map_df, axis=None): if axis is not None: map_df = scale_map_by_axis(map_df, axis) from rtree import index idx = index.Index() for rid in map_df.rid.unique(): road = map_df[map_df.rid == rid][['x', 'y']] for u, v in trajmap.pairwise(road.values): idx.insert(rid, (min(u[0], v[0]), min(u[1], v[1]), max( u[0], v[0]), max(u[1], v[1]))) return idx
def dist_to_road(p, map_df, rid): road = map_df.ix[rid][['x','y']].values _min_dist = -1 for u, v in trajmap.pairwise(road): try: _dist = dist(p, (u[0], u[1], v[0], v[1])) if (_dist < _min_dist) or (_min_dist == -1): _min_dist = _dist except Exception, data: print 'Error: dist to road:',u, v continue
def matched_length(gen_map_df, map_df, axis=None, max_dist=20, step=5.0): idx = build_index(gen_map_df, axis) cnt = 0 total = len(map_df.rid.unique()) map_df.index = map_df.rid matched_len = 0 matched_list = [] not_match = [] for rid in map_df.rid.unique(): if cnt % int(total / 20) == 0: sys.stdout.write("\rComplete (" + str(cnt) + "/" + str(total) + ")... ") sys.stdout.flush() cnt += 1 road = map_df.ix[rid][['x', 'y']].values for u, v in trajmap.pairwise(road): #print u, v ux, uy, vx, vy = u[0], u[1], v[0], v[1] dist = math.sqrt((ux - vx)**2 + (uy - vy)**2) n = int(1.0 * dist / step) #print dist, n, step _x, _y = ux, uy tups = [(_x, _y)] for i in range(1, n): _x += (vx - ux) / n _y += (vy - uy) / n tups.append((_x, _y)) tups.append((vx, vy)) for u, v in trajmap.pairwise(tups): matched = road_within_dist((u, v), idx, gen_map_df, max_dist) matched_list.append(matched) if matched: matched_len += math.sqrt((u[0] - v[0])**2 + (u[1] - v[1])**2) ''' else: not_match.append((u,v)) ''' return matched_len
def save_db(map_df, db_file='sh_map.db'): gmap, pdict, idict = map_df2nx(map_df) nodes = [] for nid in idict.keys(): x, y = idict[nid] nodes.append((nid, x, y)) edges = [] for rid in map_df.rid.unique(): r = map_df[map_df.rid == rid] for (x0, y0), (x1, y1) in trajmap.pairwise(zip(r.lon, r.lat)): #x0 = float('%.6f'%x0) #y0 = float('%.6f'%y0) #x1 = float('%.6f'%x1) #y1 = float('%.6f'%y1) x0 = precision(float(x0), 6) y0 = precision(float(y0), 6) x1 = precision(float(x1), 6) y1 = precision(float(y1), 6) pstr = str((x0, y0)) u = pdict[pstr] pstr = str((x1, y1)) v = pdict[pstr] edges.append((u, v)) print len(nodes) print len(edges) conn = sqlite3.connect(db_file) cur = conn.cursor() cur.execute( "CREATE TABLE nodes (id INTEGER, latitude FLOAT, longitude FLOAT, weight FLOAT)" ) cur.execute( "CREATE TABLE edges (id INTEGER, in_node INTEGER, out_node INTEGER, weight FLOAT)" ) cur.execute("CREATE TABLE segments (id INTEGER, edge_ids TEXT)") cur.execute("CREATE TABLE intersections (node_id INTEGER)") conn.commit() nid_dict = {} nid = 1 for _id, _lon, _lat in nodes: nid_dict[_id] = nid cur.execute("INSERT INTO nodes VALUES (" + str(nid) + "," + str(_lat) + "," + str(_lon) + "," + str(1.0) + ")") nid += 1 conn.commit() eid = 1 for u, v in edges: cur.execute("INSERT INTO edges VALUES (" + str(eid) + "," + str(nid_dict[u]) + "," + str(nid_dict[v]) + "," + str(1.0) + ")") eid += 1 conn.commit()
def matched_length(gen_map_df, map_df, axis=None, max_dist = 20, step = 5.0): idx = build_index(gen_map_df, axis) cnt = 0 total = len(map_df.rid.unique()) map_df.index = map_df.rid matched_len = 0 matched_list = [] not_match = [] for rid in map_df.rid.unique(): if cnt % int(total/20) == 0: sys.stdout.write("\rComplete (" + str(cnt) + "/" + str(total) + ")... ") sys.stdout.flush() cnt += 1 road = map_df.ix[rid][['x','y']].values for u, v in trajmap.pairwise(road): #print u, v ux, uy, vx, vy = u[0], u[1], v[0], v[1] dist = math.sqrt((ux-vx)**2+(uy-vy)**2) n = int(1.0*dist/step) #print dist, n, step _x, _y = ux, uy tups = [(_x, _y)] for i in range(1, n): _x += (vx-ux)/n _y += (vy-uy)/n tups.append((_x, _y)) tups.append((vx, vy)) for u, v in trajmap.pairwise(tups): matched = road_within_dist((u, v), idx, gen_map_df, max_dist) matched_list.append(matched) if matched: matched_len += math.sqrt((u[0]-v[0])**2 + (u[1]-v[1])**2) ''' else: not_match.append((u,v)) ''' return matched_len
def min_dist(p, map_df, axis = None): if axis != None: map_df = scale_map_by_axis(map_df, axis) _min_dist = -1 _rid = -1 for rid in map_df.rid.unique(): road = map_df[map_df.rid==rid] for u, v in trajmap.pairwise(road.values): _dist = dist((p[0], p[1]), (u[1], u[2], v[1], v[2])) if _dist < _min_dist or (_min_dist == -1): _min_dist = _dist _rid = rid #print min_dist, _rid return _min_dist
def min_dist(p, map_df, axis=None): if axis != None: map_df = scale_map_by_axis(map_df, axis) _min_dist = -1 _rid = -1 for rid in map_df.rid.unique(): road = map_df[map_df.rid == rid] for u, v in trajmap.pairwise(road.values): _dist = dist((p[0], p[1]), (u[1], u[2], v[1], v[2])) if _dist < _min_dist or (_min_dist == -1): _min_dist = _dist _rid = rid #print min_dist, _rid return _min_dist
def min_dist_rtree_without_bound(p, map_idx, map_df): rids = set(map_idx.nearest((p[0], p[1], p[0], p[1]), 6)) | \ set(map_idx.intersection((p[0], p[1], p[0], p[1]))) _min_dist = -1 _rid = -1 #map_df.index = map_df.rid for rid in rids: road = map_df.ix[rid][['x', 'y']].values for u, v in trajmap.pairwise(road): _dist = dist((p[0], p[1]), (u[0], u[1], v[0], v[1])) if (_dist < _min_dist) or (_min_dist == -1): _min_dist = _dist _rid = rid #print _min_dist, _rid return _min_dist
def min_dist_rtree_without_bound(p, map_idx, map_df): rids = set(map_idx.nearest((p[0], p[1], p[0], p[1]), 6)) | \ set(map_idx.intersection((p[0], p[1], p[0], p[1]))) _min_dist = -1 _rid = -1 #map_df.index = map_df.rid for rid in rids: road = map_df.ix[rid][['x','y']].values for u, v in trajmap.pairwise(road): _dist = dist((p[0], p[1]), (u[0], u[1], v[0], v[1])) if (_dist < _min_dist) or (_min_dist == -1): _min_dist = _dist _rid = rid #print _min_dist, _rid return _min_dist
def save_db(map_df, db_file = 'sh_map.db'): gmap, pdict, idict = map_df2nx(map_df) nodes = [] for nid in idict.keys(): x, y = idict[nid] nodes.append((nid, x, y)) edges = [] for rid in map_df.rid.unique(): r = map_df[map_df.rid==rid] for (x0, y0), (x1, y1) in trajmap.pairwise(zip(r.lon, r.lat)): #x0 = float('%.6f'%x0) #y0 = float('%.6f'%y0) #x1 = float('%.6f'%x1) #y1 = float('%.6f'%y1) x0 = precision(float(x0), 6) y0 = precision(float(y0), 6) x1 = precision(float(x1), 6) y1 = precision(float(y1), 6) pstr = str((x0,y0)) u = pdict[pstr] pstr = str((x1,y1)) v = pdict[pstr] edges.append((u, v)) print len(nodes) print len(edges) conn = sqlite3.connect(db_file) cur = conn.cursor() cur.execute("CREATE TABLE nodes (id INTEGER, latitude FLOAT, longitude FLOAT, weight FLOAT)") cur.execute("CREATE TABLE edges (id INTEGER, in_node INTEGER, out_node INTEGER, weight FLOAT)") cur.execute("CREATE TABLE segments (id INTEGER, edge_ids TEXT)") cur.execute("CREATE TABLE intersections (node_id INTEGER)") conn.commit() nid_dict = {} nid = 1 for _id, _lon, _lat in nodes: nid_dict[_id] = nid cur.execute("INSERT INTO nodes VALUES (" + str(nid) + "," + str(_lat) + "," + str(_lon) + "," + str(1.0) + ")") nid += 1 conn.commit() eid = 1 for u, v in edges: cur.execute("INSERT INTO edges VALUES (" + str(eid) + "," + str(nid_dict[u]) + "," + str(nid_dict[v]) + "," + str(1.0) + ")") eid += 1 conn.commit()
def map_df2nx_old(map_df): #pset = set() pdict = {} idict = {} idx = 0 for x, y in zip(map_df.lon, map_df.lat): pstr = str((x,y)) if pdict.has_key(pstr) is False: pdict[pstr] = idx idict[idx] = (x,y) idx += 1 G = nx.Graph() for rid in map_df.rid.unique(): x = map_df[map_df.rid == rid].lon y = map_df[map_df.rid == rid].lat for (x0, y0), (x1, y1) in trajmap.pairwise(zip(x, y)): G.add_edge(pdict[str((x0,y0))], pdict[str((x1, y1))]) return G, pdict, idict
def map_df2nx_old(map_df): #pset = set() pdict = {} idict = {} idx = 0 for x, y in zip(map_df.lon, map_df.lat): pstr = str((x, y)) if pdict.has_key(pstr) is False: pdict[pstr] = idx idict[idx] = (x, y) idx += 1 G = nx.Graph() for rid in map_df.rid.unique(): x = map_df[map_df.rid == rid].lon y = map_df[map_df.rid == rid].lat for (x0, y0), (x1, y1) in trajmap.pairwise(zip(x, y)): G.add_edge(pdict[str((x0, y0))], pdict[str((x1, y1))]) return G, pdict, idict
def dist_to_bound(p, map_df, rid): road = map_df.ix[rid][['x', 'y']].values _min_dist = -1 x, y = p for u, v in trajmap.pairwise(road): try: ux, uy, vx, vy = u[0], u[1], v[0], v[1] except Exception, data: print 'Error: dist to bound:', u, v continue if min(ux, vx) <= x <= max(ux, vx): w = 0 else: w = min((ux - x)**2, (vx - x)**2) if min(uy, vy) <= y <= max(uy, vy): h = 0 else: h = min((uy - y)**2, (vy - y)**2) _dist = math.sqrt(w + h) if _min_dist == -1 or _dist < _min_dist: _min_dist = _dist
def dist_to_bound(p, map_df, rid): road = map_df.ix[rid][['x','y']].values _min_dist = -1 x, y = p for u, v in trajmap.pairwise(road): try: ux, uy, vx, vy = u[0], u[1], v[0], v[1] except Exception, data: print 'Error: dist to bound:',u, v continue if min(ux, vx) <= x <= max(ux, vx): w = 0 else: w = min( (ux-x)**2, (vx-x)**2 ) if min(uy, vy) <= y <= max(uy, vy): h = 0 else: h = min( (uy-y)**2, (vy-y)**2 ) _dist = math.sqrt(w+h) if _min_dist == -1 or _dist < _min_dist: _min_dist = _dist
def evaluate_map(gen_map_df, map_df, axis=None, step=5.0, max_dist=20.0): map_df = scale_map_by_axis(map_df, axis) map_df.index = map_df.rid gen_map_df = scale_map_by_axis(gen_map_df, axis) gen_map_df.index = gen_map_df.rid idx = build_index(map_df, axis) step = step error = [] cnt = 0 total = len(gen_map_df.rid.unique()) for rid in gen_map_df.rid.unique(): if cnt % int(total / 20) == 0: sys.stdout.write("\rComplete (" + str(cnt) + "/" + str(total) + ")... ") sys.stdout.flush() cnt += 1 road = gen_map_df[gen_map_df.rid == rid][['x', 'y']].values for u, v in trajmap.pairwise(road): #print u, v ux, uy, vx, vy = u[0], u[1], v[0], v[1] dist = math.sqrt((ux - vx)**2 + (uy - vy)**2) n = int(1.0 * dist / step) #print dist, n, step _x, _y = ux, uy tups = [(_x, _y)] for i in range(1, n): _x += (vx - ux) / n _y += (vy - uy) / n tups.append((_x, _y)) tups.append((vx, vy)) for _x, _y in tups: error.append(min_dist_rtree((_x, _y), idx, map_df)) error = sorted(error) result = {} result['error'] = { 'min': min(error), 'max': max(error), 'mean': sum(error) / len(error), 'p25': error[int(len(error) * 0.25)], 'p50': error[int(len(error) * 0.5)], 'p75': error[int(len(error) * 0.75)] } print result['error'] '''precision''' mlen = matched_length(map_df, gen_map_df, axis, max_dist) result['gen_matched_len'] = mlen result['gen_len'] = map_length(gen_map_df) result['precision'] = result['gen_matched_len'] / result['gen_len'] print result['precision'] '''recall''' mlen = matched_length(gen_map_df, map_df, axis, max_dist) result['map_matched_len'] = mlen result['map_len'] = map_length(map_df) result['recall'] = result['map_matched_len'] / result['map_len'] print result['recall'] result['fscore'] = 2 * result['precision'] * result['recall'] / ( result['precision'] + result['recall']) return result