def matched_turn_num(gen_cand_gps, real_cand_gps, dist=20): num = 0 for x0, y0, x1, y1 in gen_cand_gps: for _x0, _y0, _x1, _y1 in real_cand_gps: if gis.distance(x0, y0, _x0, _y0) < dist and gis.distance(x1, y1, _x1, _y1) < dist: num += 1 return num
def matched_turn_num(gen_cand_gps, real_cand_gps, dist=20): num = 0 for x0, y0, x1, y1 in gen_cand_gps: for _x0, _y0, _x1, _y1 in real_cand_gps: if gis.distance(x0, y0, _x0, _y0) < dist and gis.distance( x1, y1, _x1, _y1) < dist: num += 1 return num
def test_distance(self): # Hamburg lat1 = math.radians(53.550556) lon1 = math.radians(9.993333) # München lat2 = math.radians(48.137222) lon2 = math.radians(11.575556) # Setze Erdradius von 6373km vorraus: self.assertEqual(round(distance(lat1, lon1, lat2, lon2)), 612251)
def heuristic(a, b): lat1 = math.radians(graph.node[a]["lat"]) lon1 = math.radians(graph.node[a]["lon"]) lat2 = math.radians(graph.node[b]["lat"]) lon2 = math.radians(graph.node[b]["lon"]) return gis.distance(lat1, lon1, lat2, lon2)
print('{0}/{1} nodes processed'.format(i, n)) print('Delete {0} orphaned nodes'.format(len(orphaned))) graph.remove_nodes_from(orphaned) print('Calculate offset') points = [node[1]['pos'] for node in graph.nodes(data=True)] min_x = min(points, key=lambda p: p[0])[0] min_y = min(points, key=lambda p: p[1])[1] for node in graph.nodes_iter(): pos = (graph.node[node]['pos'][0] - min_x, graph.node[node]['pos'][1] - min_y) graph.node[node]['pos'] = pos print('Translated data by ({0}, {1})'.format(-min_x, -min_y)) print('Calculate edge weights') n = graph.number_of_edges() i = 0 for edge in graph.edges(): lat1 = math.radians(graph.node[edge[0]]['lat']) lon1 = math.radians(graph.node[edge[0]]['lon']) lat2 = math.radians(graph.node[edge[1]]['lat']) lon2 = math.radians(graph.node[edge[1]]['lon']) graph[edge[0]][edge[1]]['weight'] = distance(lat1, lon1, lat2, lon2) i += 1 print('{0}/{1} edges processed'.format(i, n), end='\r') print('{0}/{1} edges processed'.format(i, n)) print('Write {0}'.format(output_file)) write_gpickle(graph, output_file) stop = timeit.default_timer() print('Program ran in {0} seconds'.format(stop - start))
def main(ex_name, data, side, k, ratio, topic_model, percent, width, alpha, beta, sel_cand_method, maxiter, cands_num, fig_width, max_value, combine_dist, _log, _run, inc=0, true_pass=False, cut_length=0): if data == 'chicago': data_file = "../Data/Chicago/chicago.pickle" axis = gis.chc_utm_axis map_file = "../Data/Chicago/min_map_df.csv" elif data == 'minsh_2000': data_file = "../Data/Shanghai/minsh_2000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_4000': data_file = "../Data/Shanghai/minsh_4000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_6000': data_file = "../Data/Shanghai/minsh_6000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_8000': data_file = "../Data/Shanghai/minsh_8000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_10000': data_file = "../Data/Shanghai/minsh_10000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_sparse_1000': data_file = "../Data/Shanghai/minsh_sparse_1000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_sparse_5000': data_file = "../Data/Shanghai/minsh_sparse_5000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_sparse_10000': data_file = "../Data/Shanghai/minsh_sparse_10000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_20000': data_file = "../Data/Shanghai/maxsh_20000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_40000': data_file = "../Data/Shanghai/maxsh_40000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_60000': data_file = "../Data/Shanghai/maxsh_60000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_80000': data_file = "../Data/Shanghai/maxsh_80000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_100000': data_file = "../Data/Shanghai/maxsh_100000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" global ex #ex.add_artifact('origin_gen_map.png') #return {} map_df = pd.read_csv(map_file) map_df = gis.scale_map_by_axis(map_df, axis) map_df.index = map_df.rid _run.info['ex_name'] = ex_name _log.info( 'topic_model: %s \tdata_file: %s \tside: %d\tk: %d\tpercent: %.4f\twidth: %d\talpha: %.2f\tbeta: %.2f' % (topic_model, data_file, side, k, percent, width, alpha, beta)) trajs = pd.read_pickle(data_file) result = {} _run.info['time'] = {} _run.info['side_length'] = gis.distance(axis[0], axis[2], axis[0], axis[2] + side, utm=True) total_time = 0 t0 = time.time() mat = trajmap.Sag(trajs, side, inc=inc, cut_length=cut_length) t1 = time.time() total_time += t1 - t0 _run.info['time']['1_Make_matrix'] = t1 - t0 _run.info['matrix_shape'] = (mat.width, mat.height) delta_time = None io_time = 0 t0 = time.time() if topic_model == 'pLSA': trajmap.get_P(mat) mat.G = nx.DiGraph() mat.candidates = set() mat.pLSA(k, maxiter) trajmap.iteration(mat, width, k, percent, max_value, true_pass) elif topic_model == 'LDA': trajmap.get_P(mat) mat.G = nx.DiGraph() mat.candidates = set() io_time = mat.LDA(k, maxiter, alpha, beta) trajmap.iteration(mat, width, k, percent, max_value, true_pass) elif topic_model == 'pLSA_cut': trajmap.get_P(mat) #mat.sag = mat.sag.toarray() trajmap.generate_map(mat, width, k, maxiter, percent, max_value) t1 = time.time() total_time += t1 - t0 - io_time _run.info['time']['Generate_Map'] = t1 - t0 _run.info['candidates_num'] = len(mat.candidates) _run.info['candidates'] = list(mat.candidates) ''' result['candidates_error'] = gis.candidates_error(mat, map_df) plot.map_df(map_df, ca, axis=axis) plt.axis(axis) plt.savefig('candidates.png') ex.add_artifact('candidates.png') ''' _M = mat.G.copy() ''' t0 = time.time() gps = {} M, gps = trajmap.smooth_M(mat, mat.G, gps, _dist=combine_dist) t1 = time.time() total_time += t1-t0 _run.info['time']['4_Smooth_map'] = t1-t0 ''' _run.info['total_time'] = total_time gen_map_df = gis.convert_nx2map_df(mat, _M, {}) result['origin'] = {} if axis[0] == gis.maxsh_utm_axis[0]: result['origin'] = gis.evaluate_map(gen_map_df, map_df, gis.minsh_utm_axis) else: result['origin'] = gis.evaluate_map(gen_map_df, map_df, axis) result['origin']['length'] = gis.map_length(gen_map_df) result['time'] = total_time plt.figure(figsize=(fig_width, fig_width * (axis[3] - axis[2]) / (axis[1] - axis[0]))) ca = plt.gca() plot.map_df(map_df, ca=ca, axis=axis, color='b') plot.map_df(gen_map_df, ca=ca, axis=axis, color='r', linewidth=1.5) plot.dots(mat, mat.candidates, ca=ca, gps=True, color='r') plt.axis(axis) plt.savefig('origin_gen_map.png') ex.add_artifact('origin_gen_map.png') csv_name = '../Data/csv/%s_%s_%d_%d_%d_%f_%d_%f.csv' % ( data, topic_model, side, k, width, max_value, maxiter, percent) gen_map_df.to_csv(csv_name) ex.add_artifact(csv_name) M = mat.G.copy() #if data == 'minsh_6000' and ex_name == 'visualization': if ex_name == 'visualization': result['smoothed'] = {} for _dist in [1.5, 2, 3]: _M, gps = trajmap.smooth_M(mat, M.to_directed(), {}, _dist) gen_map_df = gis.convert_nx2map_df(mat, _M, gps) result['smoothed'][_dist] = gis.evaluate_map( gen_map_df, map_df, axis) #result['smoothed']['length'] = gis.map_length(gen_map_df) plt.figure(figsize=(fig_width, fig_width * (axis[3] - axis[2]) / (axis[1] - axis[0]))) ca = plt.gca() plot.map_df(map_df, ca=ca, axis=axis, color='b') plot.map_df(gen_map_df, ca=ca, axis=axis, color='r', linewidth=2) plt.axis(axis) plt.savefig('gen_map.png') ex.add_artifact('gen_map.png') return result
def main(ex_name, data, side, k, ratio, topic_model, percent, width, alpha, beta, sel_cand_method, maxiter, cands_num, fig_width, max_value, combine_dist,_log, _run, inc=0, true_pass=False, cut_length=0): if data == 'chicago': data_file = "../Data/Chicago/chicago.pickle" axis = gis.chc_utm_axis map_file = "../Data/Chicago/min_map_df.csv" elif data == 'minsh_2000': data_file = "../Data/Shanghai/minsh_2000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_4000': data_file = "../Data/Shanghai/minsh_4000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_6000': data_file = "../Data/Shanghai/minsh_6000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_8000': data_file = "../Data/Shanghai/minsh_8000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_10000': data_file = "../Data/Shanghai/minsh_10000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_sparse_1000': data_file = "../Data/Shanghai/minsh_sparse_1000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_sparse_5000': data_file = "../Data/Shanghai/minsh_sparse_5000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'minsh_sparse_10000': data_file = "../Data/Shanghai/minsh_sparse_10000.pickle" axis = gis.minsh_utm_axis map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_20000': data_file = "../Data/Shanghai/maxsh_20000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_40000': data_file = "../Data/Shanghai/maxsh_40000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_60000': data_file = "../Data/Shanghai/maxsh_60000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_80000': data_file = "../Data/Shanghai/maxsh_80000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" elif data == 'maxsh_100000': data_file = "../Data/Shanghai/maxsh_100000.pickle" axis = gis.maxsh_utm_axis #map_file = "../Data/Shanghai/sh_map_df.csv" map_file = "../Data/Shanghai/matched_sh_map_df.csv" global ex #ex.add_artifact('origin_gen_map.png') #return {} map_df = pd.read_csv(map_file) map_df = gis.scale_map_by_axis(map_df, axis) map_df.index = map_df.rid _run.info['ex_name'] = ex_name _log.info('topic_model: %s \tdata_file: %s \tside: %d\tk: %d\tpercent: %.4f\twidth: %d\talpha: %.2f\tbeta: %.2f' % (topic_model, data_file, side, k, percent, width, alpha, beta)) trajs = pd.read_pickle(data_file) result = {} _run.info['time'] = {} _run.info['side_length'] = gis.distance(axis[0], axis[2], axis[0], axis[2]+side, utm=True) total_time = 0 t0 = time.time() mat = trajmap.Sag(trajs, side, inc=inc, cut_length=cut_length) t1 = time.time() total_time += t1-t0 _run.info['time']['1_Make_matrix'] = t1 - t0 _run.info['matrix_shape'] = (mat.width, mat.height) delta_time = None io_time = 0 t0 = time.time() if topic_model == 'pLSA': trajmap.get_P(mat) mat.G = nx.DiGraph() mat.candidates = set() mat.pLSA(k, maxiter) trajmap.iteration(mat, width, k, percent, max_value, true_pass) elif topic_model == 'LDA': trajmap.get_P(mat) mat.G = nx.DiGraph() mat.candidates = set() io_time = mat.LDA(k, maxiter, alpha, beta) trajmap.iteration(mat, width, k, percent, max_value, true_pass) elif topic_model == 'pLSA_cut': trajmap.get_P(mat) #mat.sag = mat.sag.toarray() trajmap.generate_map(mat, width, k, maxiter, percent, max_value) t1 = time.time() total_time += t1-t0-io_time _run.info['time']['Generate_Map'] = t1 - t0 _run.info['candidates_num'] = len(mat.candidates) _run.info['candidates'] = list(mat.candidates) ''' result['candidates_error'] = gis.candidates_error(mat, map_df) plot.map_df(map_df, ca, axis=axis) plt.axis(axis) plt.savefig('candidates.png') ex.add_artifact('candidates.png') ''' _M = mat.G.copy() ''' t0 = time.time() gps = {} M, gps = trajmap.smooth_M(mat, mat.G, gps, _dist=combine_dist) t1 = time.time() total_time += t1-t0 _run.info['time']['4_Smooth_map'] = t1-t0 ''' _run.info['total_time'] = total_time gen_map_df = gis.convert_nx2map_df(mat, _M, {}) result['origin'] = {} if axis[0] == gis.maxsh_utm_axis[0]: result['origin'] = gis.evaluate_map(gen_map_df, map_df, gis.minsh_utm_axis) else: result['origin'] = gis.evaluate_map(gen_map_df, map_df, axis) result['origin']['length'] = gis.map_length(gen_map_df) result['time'] = total_time plt.figure(figsize=(fig_width, fig_width*(axis[3]-axis[2])/(axis[1]-axis[0]))) ca = plt.gca() plot.map_df(map_df, ca=ca, axis=axis, color='b') plot.map_df(gen_map_df, ca=ca, axis=axis, color='r', linewidth=1.5) plot.dots(mat, mat.candidates, ca=ca, gps=True, color='r') plt.axis(axis) plt.savefig('origin_gen_map.png') ex.add_artifact('origin_gen_map.png') csv_name = '../Data/csv/%s_%s_%d_%d_%d_%f_%d_%f.csv'%(data, topic_model, side, k, width, max_value, maxiter, percent) gen_map_df.to_csv(csv_name) ex.add_artifact(csv_name) M = mat.G.copy() #if data == 'minsh_6000' and ex_name == 'visualization': if ex_name == 'visualization': result['smoothed'] = {} for _dist in [1.5, 2, 3]: _M, gps = trajmap.smooth_M(mat, M.to_directed(), {}, _dist) gen_map_df = gis.convert_nx2map_df(mat, _M, gps) result['smoothed'][_dist] = gis.evaluate_map(gen_map_df, map_df, axis) #result['smoothed']['length'] = gis.map_length(gen_map_df) plt.figure(figsize=(fig_width, fig_width*(axis[3]-axis[2])/(axis[1]-axis[0]))) ca = plt.gca() plot.map_df(map_df, ca=ca, axis=axis, color='b') plot.map_df(gen_map_df, ca=ca, axis=axis, color='r', linewidth=2) plt.axis(axis) plt.savefig('gen_map.png') ex.add_artifact('gen_map.png') return result