def check_dist_between_groups(days=None): if days is None: days = data.days out_dir = os.path.join(data.data_out_path, 'group_dist/') os.makedirs(out_dir, exist_ok=True) for day in days: print('-- {} --'.format(day)) t0 = time.clock() td.initialise(day) groups = data.read_group_typical_ids(day) n_groups = len(groups) dists = np.zeros([n_groups, n_groups]) for i1, i2 in itertools.combinations(range(n_groups), 2): print('\r{}, {} / {}'.format(i1, i2, n_groups), end='') dist = td.get_distances(groups.iloc[i1]['id'], groups.iloc[i2]['id']) dists[i1, i2] = dist dists[i2, i1] = dist print('\nDone!') save_path = os.path.join(out_dir, day) np.save(save_path, dists) t1 = time.clock() print('Take Taken = {}'.format(timedelta(seconds=t1-t0))) fig, ax = plt.subplots() ax.pcolormesh(dists) ax.set_title(day)
def check_dist_within_group(): for day in data.days: print('-- {} --'.format(day)) td.initialise(day) groups = data.read_groups(day) max_max_dist = -1 max_dist_group = -1 dists = [] for group_id, df in groups.groupby('group_id'): ids = df['id'] max_dist = -1 for id1, id2 in itertools.combinations(ids, 2): dist = td.get_distances(id1, id2) dists.append([group_id, dist]) if dist > max_dist: max_dist = dist if max_dist > max_max_dist: max_max_dist = max_dist max_dist_group = group_id # print('group {}: max dist = {}'.format(group_id, max_dist)) print('max group dist: group {}, dist = {}'.format(max_dist_group, max_max_dist)) dists = np.array(dists) fig, ax = plt.subplots() ax.scatter(dists[:, 0], dists[:, 1], lw=0) ax.set_title(day) ax.grid(linestyle='-', alpha=0.2) ax.yaxis.set_ticks_position('none') ax.xaxis.set_ticks_position('none')
from pprint import pprint import itertools import pandas as pd import data import os import time from datetime import timedelta outdir = os.path.join(data.data_out_path, 'groups/') os.makedirs(outdir, exist_ok=True) cutoff = 300 for day in data.days: t0 = time.clock() print('Day {}'.format(day)) td.initialise(day=day) # the first id of each group groups = [] ids = td.all_ids total = len(ids) group_df = pd.DataFrame(index=ids, columns=['group_id']) for i, id in enumerate(ids): print('\r{}/{}'.format(i, total), end='') xy = td.df2.loc[:, id] # as the ids are roughly grouped in the correct groups already, # we check the first few most recently added groups first, # which will be at the end for id2 in itertools.islice(reversed(groups), 30): # check against the first id xy2 = td.df2.loc[:, id2] # calling the private method here because this way we can short circuit and be a little faster
def on_click(event): if event.inaxes is not ax: return cutoff = event.ydata ax.lines.clear() ax.plot([0, 1], [cutoff, cutoff], color='red', lw=2) ids_below = [id for i, id in enumerate(td.all_ids) if dists[i] <= cutoff] ax3d.clear() tc.plot_trajectories(ax=ax3d, df=td.df[td.df['id'].isin(ids_below)]) ax3d.set_title('{0} trajectories.\nCutoff = {1:.2f}'.format( len(ids_below), cutoff)) fig.canvas.draw() td.initialise(day='Fri') # initialise display fig = plt.figure() ax = fig.add_subplot(1, 2, 1) ax3d = fig.add_subplot(1, 2, 2, projection='3d') fig.canvas.mpl_connect('key_press_event', on_key_press) fig.canvas.mpl_connect('pick_event', on_pick) fig.canvas.mpl_connect('button_release_event', on_click) fig.patch.set_color('#EEEEEE') # plot a random person plot_random_dists() plt.show()