def generate_rectangle_sets(fname, r, p, q, max_side_length): trajectories = paths.read_dong_csv("/uusoc/scratch/raven4/dongx/trajectory_paper_code/data/samples/{}.tsv".format(fname)) disc = utils.disc_to_func("disc") while True: red, blue, planted_reg, _ = pyscan.plant_full_square(trajectories, r, p, q, disc) print(planted_reg.upX() - planted_reg.lowX(), max_side_length) if planted_reg.upX() - planted_reg.lowX() < max_side_length: break return red, blue
def generate_disk_sets(fname, r, p, q, min_r, max_r): disc = utils.disc_to_func("disc") trajectories = paths.read_dong_csv("/uusoc/scratch/raven1/dongx/trajectory_paper_code/data/samples/{}.tsv".format(fname)) while True: red, blue, planted_reg , planted_mx = pyscan.plant_full_disk(trajectories, r, p, q, disc) print(min_r, planted_reg.get_radius(), max_r) if min_r < planted_reg.get_radius() < max_r: break return red, blue
def plot_plane_full_trajectories(trajectories, r, p, q): disc = utils.disc_to_func("disc") red, blue, mx_plane, _ = pyscan.plant_full_halfplane(trajectories, r, p, q, disc) ax = plt.subplot() for traj in blue: plot_tuples(ax, traj, "b") for traj in red: plot_tuples(ax, traj, "r") xs = np.arange(0, 1, .01) ys = (-1 - mx_plane.get_coords()[0] * xs) * 1/ mx_plane.get_coords()[1] ax.plot(xs, ys, color="g") plt.show()
def plot_full_trajectories(trajectories, r, p, q): disc = utils.disc_to_func("disc") red, blue, mx_disk, _ = pyscan.plant_full_disk(trajectories, r, p, q, disc) ax = plt.subplot() for traj in blue: plot_tuples(ax, traj, "b") for traj in red: plot_tuples(ax, traj, "r") actor = plt.Circle((mx_disk.get_origin()[0], mx_disk.get_origin()[1]), mx_disk.get_radius()) print(mx_disk.get_origin(), mx_disk.get_radius()) ax.add_artist(actor) plt.show()
def plot_full_trajectories_intersection_rect(trajectories, r): disc = utils.disc_to_func("disc") red, blue, rect, _ = pyscan.plant_full_square(trajectories, r, 0.5, 0.5, disc) ax = plt.subplot() blue_c = 0 for traj in trajectories: if rect.intersects_trajectory(traj): blue_c += 1 plot_line(ax, traj, "b") else: plot_line(ax, traj, "r") print(blue_c / len(trajectories), r) actor = plt.Rectangle((rect.upX(), rect.upY()), rect.upX() - rect.lowX(), rect.upY() - rect.lowY()) ax.add_artist(actor) plt.show()
def plot_full_trajectories_intersection_check(trajectories, r): disc = utils.disc_to_func("disc") red, blue, mx_disk, _ = pyscan.plant_full_disk(trajectories, r, .5, .5, disc) #mx_disk = pyscan.Disk(mx_disk.get_origin()[0], mx_disk.get_origin()[1], .01) ax = plt.subplot() blue_c = 0 for traj in trajectories: if pyscan.Trajectory(traj).intersects_disk(mx_disk): blue_c += 1 plot_line(ax, traj, "b") else: plot_line(ax, traj, "r") print(blue_c / len(trajectories), r) actor = plt.Circle((mx_disk.get_origin()[0], mx_disk.get_origin()[1]), mx_disk.get_radius()) print(mx_disk.get_origin(), mx_disk.get_radius()) ax.plot(mx_disk.get_origin()[0], mx_disk.get_origin()[1], marker='o') ax.add_artist(actor) plt.show()
def plot_partial_trajectories(trajectories, r, p, q, eps_r): disc = utils.disc_to_func("disc") red, blue, mx_disk, _ = pyscan.plant_partial_disk(trajectories, r, p, q, eps_r, disc) ax = plt.subplot() rpts = list(itertools.chain.from_iterable([traj.get_pts() for traj in red])) rpts = random.sample(rpts, 500) plot_tuples(ax, list(rpts), "r") bpts = list(itertools.chain.from_iterable([traj.get_pts() for traj in blue])) bpts = random.sample(bpts, 500) plot_tuples(ax, bpts, "b") actor = plt.Circle((mx_disk.get_origin()[0], mx_disk.get_origin()[1]), mx_disk.get_radius(), edgecolor="k", linewidth=2, fill=False) ax.add_artist(actor) ax.set_xlim(0, 1.0) ax.set_ylim(0, 1.0) plt.tight_layout() plt.axis('off') plt.show()
def testing_flux_framework(output_file, red, blue, l_s, h_s, count, region_name="disk", two_level_sample=True, ham_sample=False, max_time=None): fieldnames = [ "disc", "region", "n", "s", "time", "m_disc", "m_disc_approx" ] with open(output_file, 'w') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() for i in np.logspace(l_s, h_s, count): eps = i n = 1 / eps s = 1 / (2 * eps * eps) n = int(round(n) + .1) s = int(round(s) + .1) disc = utils.disc_to_func("disc") start_time = time.time() m_sample = [ pyscan.WPoint(1.0, p[0], p[1], 1.0) for p in pyscan.my_sample(red, s) ] b_sample = [ pyscan.WPoint(1.0, p[0], p[1], 1.0) for p in pyscan.my_sample(blue, s) ] if two_level_sample: net_set1 = pyscan.my_sample(m_sample, n) net_set2 = pyscan.my_sample(b_sample, n) if ham_sample: s = int(1 / (2 * eps**(4.0 / 3)) * math.log(1 / eps)**(2 / 3.0)) m_sample = pyscan.ham_tree_sample(m_sample, s) b_sample = pyscan.ham_tree_sample(b_sample, s) else: net_set1 = [pyscan.Point(p[0], p[1], p[2]) for p in m_sample] net_set2 = [pyscan.Point(p[0], p[1], p[2]) for p in b_sample] n = s net_set1 = [pyscan.Point(p[0], p[1], p[2]) for p in net_set1] net_set2 = [pyscan.Point(p[0], p[1], p[2]) for p in net_set2] net_set = net_set1 + net_set2 if region_name == "halfplane": reg, mx = pyscan.max_halfplane(net_set, m_sample, b_sample, disc) elif region_name == "disk": reg, mx = pyscan.max_disk(net_set, m_sample, b_sample, disc) elif region_name == "rectangle": grid = pyscan.Grid(n, m_sample, b_sample) s1 = pyscan.max_subgrid_linear(grid, -1.0, 1.0) s2 = pyscan.max_subgrid_linear(grid, 1.0, -1.0) if s1.fValue() > s2.fValue(): reg = grid.toRectangle(s1) mx = s1.fValue() else: reg = grid.toRectangle(s2) mx = s2.fValue() else: return end_time = time.time() st = time.time() actual_mx = pyscan.evaluate_range(reg, red, blue, disc) et = time.time() print("Time to evaluate region {}".format(et - st)) row = { "disc": "disc", "region": region_name, "n": n, "s": s, "time": end_time - start_time, "m_disc_approx": mx, "m_disc": actual_mx } writer.writerow(row) f.flush() print(row) if max_time is not None and end_time - start_time > max_time: return
def testing_full_framework( trajectories, l_s, h_s, count, vparam="eps", eps=.01, r=.04, p=0.5, q=.2, alpha=.01, planted_points=None, actual_mx=None, max_disk_r=.1, min_disk_r=.05, disc_name="disc", input_size=10000): """ How do I convert the trajectories over? 1) Just sample evenly from the length. 2) Choose points evenly 3) Choose :param trajectories: :param l_s: :param h_s: :param count: :param vparam: :param eps: :param eps_r: :param r: :param q: :param disc_name: :param region_name: :param input_size: :return: """ output_file = "{0}_multi_disk_{1:.2f}_{2:.2f}_full_discrepancy.csv".format(disc_name, min_disk_r, max_disk_r) fieldnames = ["vparam", "input_size", "region", "disc", "n", "s", "r", "p", "q", "alpha", "time", "m_disc", "m_disc_approx"] with open(output_file, 'w') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() for i in np.logspace(l_s, h_s, count): if vparam == "eps": eps = i elif vparam == "r": r = i elif vparam == "q": q = i n = 1 / eps s = 1 / (2 * eps * eps) n = int(round(n) + .1) s = int(round(s) + .1) disc = utils.disc_to_func(disc_name) st = time.time() if planted_points is None: red, blue, _, actual_mx = pyscan.plant_full_disk(trajectories, r, p, q, disc) else: red, blue = planted_points red_sample = pyscan.my_sample(red, s) blue_sample = pyscan.my_sample(blue, s) red_net = pyscan.my_sample(red, n) blue_net = pyscan.my_sample(blue, n) net = red_net + blue_net et = time.time() print("Time to plant region {}".format(et - st)) start_time = time.time() reg, mx = pyscan.max_disk_traj_grid(net, [pyscan.WTrajectory(1.0, traj) for traj in red_sample], [pyscan.WTrajectory(1.0, traj) for traj in blue_sample], min_disk_r, max_disk_r, disc) end_time = time.time() row = {"vparam": vparam, "input_size": input_size, "disc": disc_name, "region": "multi_disk", "n": n, "s": s, "r": r, "q": q,"p":p, "alpha":alpha, "time": end_time - start_time, "m_disc_approx": mx, "m_disc": actual_mx} writer.writerow(row) f.flush() print(row)
"alpha":alpha, "time": end_time - start_time, "m_disc_approx": mx, "m_disc": actual_mx} writer.writerow(row) f.flush() print(row) if __name__ == "__main__": trajectories = paths.read_geolife_files(10000) # print(len(trajectories)) r = .05 p = .5 q = .8 alpha = .01 max_r = .05 disc = utils.disc_to_func("disc") red, blue, _ ,actual_mx = pyscan.plant_full_disk(trajectories, r, p, q, disc) # for min_r in np.logspace(math.log(alpha, 10), math.log(max_r, 10), num=5, endpoint=False): print(min_r, max_r) testing_full_framework(trajectories, -1, -1.8, 20, r=r, q=q, p=p, planted_points=(red,blue), actual_mx=actual_mx, min_disk_r = min_r, max_disk_r = max_r)
def testing_partial_framework(red, blue, output_file, l_s, h_s, count, r=.04, p=0.5, q=.2, error_thresh=3, two_level_sample=True, ham_sample=True, disc_name="disc", region_name="disk", sample_method="block", max_time=None): """ How do I convert the trajectories over? 1) Just sample evenly from the length. 2) Choose points evenly 3) Choose :param trajectories: :param l_s: :param h_s: :param count: :param vparam: :param eps: :param eps_r: :param r: :param q: :param disc_name: :param region_name: :param input_size: :return: """ fieldnames = [ "disc", "region", "n", "s", "r", "p", "q", "time", "m_disc", "m_disc_approx", "sample_method" ] with open(output_file, 'w') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() disc = utils.disc_to_func(disc_name) s_prime = int(10**(2 * error_thresh) + .5) m_sample_prime = pyscan.uniform_sample(red, s_prime, False) b_sample_prime = pyscan.uniform_sample(blue, s_prime, False) for i in np.logspace(l_s, h_s, count): eps = i n = 1 / eps s = 1 / (2 * eps * eps) n = int(round(n) + .1) s = int(round(s) + .1) start_time = time.time() if sample_method == "block": f_sample = pyscan.block_sample elif sample_method == "even": f_sample = pyscan.even_sample elif sample_method == "uniform": f_sample = pyscan.uniform_sample m_sample = f_sample(red, s, False) b_sample = f_sample(blue, s, False) m_sample = pyscan.to_weighted(m_sample) b_sample = pyscan.to_weighted(b_sample) if two_level_sample: net_set1 = pyscan.my_sample(m_sample, n) net_set2 = pyscan.my_sample(b_sample, n) if ham_sample: s = int(1 / (2 * eps**(4.0 / 3)) * math.log(1 / eps)**(2 / 3.0)) m_sample = pyscan.ham_tree_sample(m_sample, s) b_sample = pyscan.ham_tree_sample(b_sample, s) else: net_set1 = [pyscan.Point(p[0], p[1], p[2]) for p in m_sample] net_set2 = [pyscan.Point(p[0], p[1], p[2]) for p in b_sample] n = s net_set1 = [pyscan.Point(p[0], p[1], p[2]) for p in net_set1] net_set2 = [pyscan.Point(p[0], p[1], p[2]) for p in net_set2] net_set = net_set1 + net_set2 if region_name == "halfplane": reg, mx = pyscan.max_halfplane(net_set, m_sample, b_sample, disc) elif region_name == "disk": reg, mx = pyscan.max_disk(net_set, m_sample, b_sample, disc) elif region_name == "rectangle": grid = pyscan.Grid(n, m_sample, b_sample) s1 = pyscan.max_subgrid_linear(grid, -1.0, 1.0) s2 = pyscan.max_subgrid_linear(grid, 1.0, -1.0) if s1.fValue() > s2.fValue(): reg = grid.toRectangle(s1) mx = s1.fValue() else: reg = grid.toRectangle(s2) mx = s2.fValue() else: return end_time = time.time() actual_mx = pyscan.evaluate_range( reg, pyscan.to_weighted(m_sample_prime), pyscan.to_weighted(b_sample_prime), disc) row = { "disc": disc_name, "region": region_name, "n": n, "s": s, "r": r, "q": q, "p": p, "time": end_time - start_time, "m_disc_approx": mx, "m_disc": actual_mx, "sample_method": sample_method } writer.writerow(row) print(row) f.flush() if max_time is not None and end_time - start_time > max_time: return
def testing_full_framework( red, blue, output_file, l_s, h_s, count, vparam="eps", eps=.01, alpha=.01, max_disk_r=None, min_disk_r=None, disc_name="disc", region_name="halfplane", sample_method="halfplane", fast_disk = True, two_level_sample=True, max_time=None): """ How do I convert the trajectories over? 1) Just sample evenly from the length. 2) Choose points evenly 3) Choose :param trajectories: :param l_s: :param h_s: :param count: :param vparam: :param eps: :param eps_r: :param r: :param q: :param disc_name: :param region_name: :param input_size: :return: """ fieldnames = ["vparam", "disc", "region", "n", "s", "n_pts", "m_pts", "b_pts", "alpha", "time", "m_disc", "m_disc_approx", "sample_method"] with open(output_file, 'w') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() for i in np.logspace(l_s, h_s, count): if vparam == "eps": eps = i elif vparam == "alpha": alpha = i n = 1 / eps s = 1 / (2 * eps * eps) n = int(round(n) + .1) s = int(round(s) + .1) disc = utils.disc_to_func(disc_name) red_sample = pyscan.my_sample(red, s) blue_sample = pyscan.my_sample(blue, s) if two_level_sample: red_net = pyscan.my_sample(red, n) blue_net = pyscan.my_sample(blue, n) else: red_net = red_sample blue_net = blue_sample net = red_net + blue_net print("Running: {} {}".format(n, s)) start_time = time.time() if region_name == "multiscale_disk": if max_disk_r is not None and alpha > max_disk_r: print("Max Disk Radius is greater than alpha") continue reg, mx = multiscale_disk(min_disk_r, max_disk_r, alpha, red_sample, blue_sample, net, disc, fast_disk) m_sample, b_sample, net_set = [], [], [] else: if sample_method == "halfplane": m_sample = [pyscan.halfplane_kernel([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj], alpha) for traj in red_sample] b_sample = [pyscan.halfplane_kernel([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj], alpha) for traj in blue_sample] pt_net = [pyscan.halfplane_kernel([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj], alpha) for traj in net] elif sample_method == "dp": m_sample = [pyscan.dp_compress(traj, alpha) for traj in red_sample] b_sample = [pyscan.dp_compress(traj, alpha) for traj in blue_sample] pt_net = [pyscan.dp_compress(traj, alpha) for traj in net] elif sample_method == "hull": m_sample = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in red_sample] b_sample = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in blue_sample] pt_net = [pyscan.convex_hull([pyscan.Point(pt[0], pt[1], 1.0) for pt in traj]) for traj in net] elif sample_method is None: #just takes the waypoints. m_sample = [[pyscan.Point(pt[0], pt[1], 1.0) for pt in traj] for traj in red_sample] b_sample = [[pyscan.Point(pt[0], pt[1], 1.0) for pt in traj] for traj in blue_sample] pt_net = [[pyscan.Point(pt[0], pt[1], 1.0) for pt in traj] for traj in net] elif sample_method == "grid": m_sample = [pyscan.grid_kernel(traj, alpha) for traj in red_sample] b_sample = [pyscan.grid_kernel(traj, alpha) for traj in blue_sample] pt_net = [pyscan.grid_kernel(traj, alpha) for traj in net] elif sample_method == "lifting": m_sample = [pyscan.lifting_kernel(traj, alpha) for traj in red_sample] b_sample = [pyscan.lifting_kernel(traj, alpha) for traj in blue_sample] pt_net = [pyscan.lifting_kernel(traj, alpha) for traj in net] elif sample_method == "grid_direc": if max_disk_r is not None and alpha > max_disk_r: print("Max Disk Radius is greater than alpha") continue chord_l = math.sqrt(4 * alpha * max(min_disk_r, alpha) - 2 * alpha * alpha) m_sample = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in red_sample] b_sample = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in blue_sample] pt_net = [pyscan.grid_direc_kernel(pyscan.dp_compress(traj, alpha), chord_l, alpha) for traj in net] elif sample_method == "even": m_sample = [pyscan.even_sample_error(traj, alpha, False) for traj in red_sample] b_sample = [pyscan.even_sample_error(traj, alpha, False) for traj in blue_sample] pt_net = [pyscan.even_sample_error(traj, alpha, False) for traj in net] else: return if region_name == "multiscale_disk_fixed": m_sample = list(pyscan.trajectories_to_labels(m_sample)) b_sample = list(pyscan.trajectories_to_labels(b_sample)) net_set = list(pyscan.trajectories_to_labels(pt_net)) reg, mx = multiscale_disk_fixed(min_disk_r, max_disk_r, m_sample, b_sample, net_set, disc, fast_disk) else: m_sample = list(pyscan.trajectories_to_labels(m_sample)) b_sample = list(pyscan.trajectories_to_labels(b_sample)) net_set = list(itertools.chain.from_iterable(pt_net)) if region_name == "halfplane": reg, mx = pyscan.max_halfplane_labeled(net_set, m_sample, b_sample, disc) elif region_name == "disk": reg, mx = pyscan.max_disk_labeled(net_set, m_sample, b_sample, disc) elif region_name == "rectangle": reg, mx = pyscan.max_rect_labeled(n, 2 * max_disk_r, m_sample, b_sample, disc) elif region_name == "rectangle_scale": reg, mx = pyscan.max_rect_labeled_scale(n, 2 * max_disk_r, alpha, net_set, m_sample, b_sample, disc) else: return end_time = time.time() actual_mx = pyscan.evaluate_range_trajectory(reg, red, blue, disc) row = {"vparam": vparam, "disc": disc_name, "region": region_name, "n": n, "s": s, "n_pts": len(net_set), "m_pts":len(m_sample), "b_pts":len(b_sample), "alpha":alpha, "time": end_time - start_time, "m_disc_approx": mx, "m_disc": actual_mx, "sample_method": sample_method} writer.writerow(row) f.flush() print(row) if max_time is not None and end_time - start_time > max_time: return
def generate_halfplane_sets(fname, r, p, q): disc = utils.disc_to_func("disc") trajectories = paths.read_dong_csv("/uusoc/scratch/raven1/dongx/trajectory_paper_code/data/samples/{}.tsv".format(fname)) red, blue, planted_reg, planted_mx = pyscan.plant_full_halfplane(trajectories, r, p, q, disc) return red, blue