def plot_users_trail(users, signals, cells, save_pic=False, save_trails_js=False): ''' save users' trail plot or save users' trail js file :param users: users id array :param signals: signals :param cells: get_cells :param save_pic: Boolean Whether to save the trail plot :param save_trails_js: Boolean Whether to save the trail js file :return: ''' n = len(users) for i in range(n): if save_pic & save_trails_js: break user_id = users[i] signal = signals[signals.user_id == user_id] signal = signal.sort_values('dates') signal = pd.merge(signal, cells, on='cell_id', how='left') signal = signal.dropna(0) print('user %d' % i) if save_pic: plt.plot(signal.lon, signal.lat) plt.savefig('../../pic/%.2d%.2d/%.2dt%.2d/pic%d.jpg' % (mon, dd, hh_start, hh_end, i)) plt.close('all') if save_trails_js: output_data_js(signal, '../../web/data/%.2d%.2d/%.2dt%.2d/user%d.js' % (mon, dd, hh_start, hh_end, i))
def output_pre_road_data(trail, direction, n, type='cell', interpolate_interval=1, smoothing=0, penalty=0.9, direction_window=15): ''' Output predicted road data with CTrack. For test wuhu data. :param trail: <Int> The i_th trail :param direction: <-1/1> -1 means reverse side; 1 means front. :param n: The n_th test of this trail. If None, return all the tests of the trail. :param type: 'cell' or 'GPS'. See details in function get_obs_data. :param interpolate_interval: Interval seconds of processed observables. Default = 1. :param smoothing: Window length of smoothing. If smoothing == 0, do not smooth. :param penalty: :param direction_window: :return: None. ''' df = get_road_tests(trail, direction, n) graph = get_graph('../../data/road_test/link_baidu_wuhu.txt') obs = get_obs_data(df, type=type) track = CTrack(obs, graph, smoothing=smoothing, interpolate_interval=interpolate_interval, penalty=penalty, direction_window=direction_window) states = track.viterbi() direction = u'正向' if direction == 1 else u'反向' output_data_js( states, '../../res/MapMatching_test/pre_%s/wuhu_pre_线路%d_%s%d_smoothing=%d_window=%d.js' % (type, trail, direction, n, smoothing, interpolate_interval))
def plot_users_trail(file_date, save_pic=True, save_trails_js=True, save_data=True, save_dir=work_dir + 'attach/'): ''' save users' trail plot or save users' trail js file :param users: users id array :param signals: signals :param cells: get_cells :param save_pic: Boolean Whether to save the trail plot :param save_trails_js: Boolean Whether to save the trail js file :return: ''' cells = pd.read_csv('../../res/[0926]cells_process.csv') cells_loc = cells[['cell_id', 'lat', 'lon']] dirs = [save_dir + 'user_pic/', save_dir + 'user_trail/'] signals = get_signals_from_csv(save_dir + 'user_data' + file_date + '.csv') labels = list(pd.read_csv(save_dir + 'user_pic/random_user.csv').user_id) users = [x.split('@')[0] for x in labels] n = len(users) for i in range(n): # if save_pic & save_trails_js: # break user_id = users[i] signal = signals[signals.user_id == user_id] signal = signal.sort_values('dates') signal = pd.merge(signal, cells, on='cell_id', how='left') signal = signal.dropna(0) print('user %d' % i) if save_pic: plt.plot(signal.lon, signal.lat) plt.savefig('%suser_trail_%s/pic/user%d.jpg' % (save_dir, file_date, i)) plt.close('all') if save_data: signal.to_csv('%suser_trail_%s/csv/user%d.csv' % (save_dir, file_date, i), index=False) if save_trails_js: output_data_js( signal, '%suser_trail_%s/data/user%d.js' % (save_dir, file_date, i))
def output_obs_data(trail, direction, n, type='cell', interpolate_interval=5, smoothing=20, interpolate=True): ''' Output observables data after pre-processing. For test wuhu data :param trail: <Int> The i_th trail :param direction: <-1/1> -1 means reverse side; 1 means front. :param n: The n_th test of this trail. If None, return all the tests of the trail. :param type: 'cell' or 'GPS'. See details in function get_obs_data. :param interpolate_interval: Interval seconds of processed observables. Default = 1. :param smoothing: Window length of smoothing. If smoothing == 0, do not smooth. :param interpolate: :return: None. ''' df = get_road_tests(trail, direction, n) graph = get_graph('../../data/road_test/link_baidu_wuhu.txt') obs = get_obs_data(df, type=type) track = CTrack(obs, graph, smoothing=smoothing, interpolate_interval=interpolate_interval, interpolate=interpolate) out = track.obs out['dates'] = out.time direction = u'正向' if direction == 1 else u'反向' if interpolate: output_data_js( out, '../../res/MapMatching_test/processed_%s/wuhu_线路%d_%s%d_smoothing=%d_window=%d.js' % (type, trail, direction, n, smoothing, interpolate_interval)) else: output_data_js( out, '../../res/MapMatching_test/raw_%s/wuhu_线路%d_%s%d.js' % (type, trail, direction, n))
def plot_users_trail(self, users=None, within_users=True, save_pic=False, save_trails_js=False): ''' save users' trail plot or save users' trail js file :param users: users id array. If users is None, it will plot all the users. Default = None :param within_users: read signals within users or not. Default = True :param save_pic: Boolean Whether to save the trail plot :param save_trails_js: Boolean Whether to save the trail js file :return: ''' cells = pd.read_csv('../../res/[0926]cells_process.csv') signals = self.get_clean_signals(within_users) if users is None: users = signals.user_id.unique() n = len(users) for i in range(n): if not (save_pic | save_trails_js): break user_id = users[i] signal = signals[signals.user_id == user_id] signal = signal.sort_values('dates') signal = pd.merge(signal, cells, on='cell_id', how='left') signal = signal.dropna(0) print('user %d' % i) if save_pic: self.check_dir(self.plot_dir) plt.plot(signal.lon, signal.lat) plt.title('user = %s' % user_id) plt.savefig('%s/pic%d.jpg' % (self.plot_dir, i)) plt.close('all') if save_trails_js: self.check_dir(self.js_dir) output_data_js(signal, user_id, '%s/user%d.js' % (self.js_dir, i))
def single_map_matching(obs, graph, save_name, mm_smoothing=10, mm_interpolate_interval=10, save_dir='../../res/work/0417_multi_mm/'): output_data_js(obs, save_dir + save_name + '_raw.js') track = CTrack(obs, graph, smoothing=mm_smoothing, interpolate_interval=mm_interpolate_interval) output_data_js(track.obs, save_dir + save_name + '_processed.js') states = track.viterbi() output_data_js(states, save_dir + save_name + '.js') return states
def multi_map_matching(obses, graph, save_name, mdtw_interval=5, mm_smoothing=10, mm_interpolate_interval=10, save_dir='../../res/work/0417_multi_mm/'): mdtw = MultiDTWIndx(obses, interval=mdtw_interval) new_obs = mdtw.run_one_by_one() print(new_obs) output_data_js(new_obs, save_dir + save_name + '_raw.js') track = CTrack(new_obs, graph, smoothing=mm_smoothing, interpolate_interval=mm_interpolate_interval) output_data_js(track.obs, save_dir + save_name + '_processed.js') states = track.viterbi() output_data_js(states, save_dir + save_name + '.js') return states
graph = get_graph('../../data/hefei_road/link_baidu.txt') user_id = users[10] signal, ind_sets, signal_sets = get_trajectory(signals, cells_loc, user_id) print(cal_rough_speed_per_cell2(signal)) for i in range(len(signal_sets)): if i != 1: continue obs = pd.DataFrame({ 'time': signal_sets[i]['dates'], 'lon': signal_sets[i]['lon'], 'lat': signal_sets[i]['lat'] }) # print(obs) obs = deal_same_obs(obs)[0:8] check_directory('../../res/MapMatching/%s' % user_id) output_data_js( obs, '../../res/MapMatching/%s/raw_trajectory%d.js' % (user_id, i)) obs = deal_outlier(obs) speed = cal_rough_speed_per_cell2(obs) print(speed) print([ abs(speed['direction'][i] - speed['direction'][i - 1]) for i in range(1, len(speed)) ]) track = CTrack(obs, graph, smoothing=20, interpolate_interval=5) out = track.obs states = track.viterbi() track.plot_each_time_best_trajectory(track.delta) # print(cal_rough_speed_per_cell2(states)) output_data_js( out, '../../res/MapMatching/%s/pre_trajectory%d.js' % (user_id, i)) output_data_js(