Skip to content

Instantly share code, notes, and snippets.

View joaofig's full-sized avatar

João Paulo Figueira joaofig

View GitHub Profile
@joaofig
joaofig / GraphTrajectory_get_top_matching_trajectories.py
Last active November 20, 2022 17:30
This function returns the top matching trajectories
def get_top_matching_trajectories(self, top=0.05, exclude_self=True):
df = load_matching_links(self.traj_id)
trajectories = np.unique(df["traj_id"].values)
query_set = set(df[df["traj_id"] == self.traj_id]["quadkey"].values)
if exclude_self:
df = df[df["traj_id"] != self.traj_id]
traj_df = pd.DataFrame(data=trajectories, columns=["traj_id"])
@joaofig
joaofig / EVED_load_matching_links.py
Created November 19, 2022 14:08
Loads all the links that match a given trajectory
@joaofig
joaofig / GraphRoute_get_top_match_trajectories.py
Created November 18, 2022 16:45
This function filters the top N percent best batching trajectories.
def get_top_match_trajectories(self, level=20, top=0.05):
match_df = pd.DataFrame(data=self.calculate_trajectory_matches(level), columns=['traj_id', 'similarity'])
match_df["percent_rank"] = match_df["similarity"].rank(pct=True)
filtered_df = match_df[match_df["percent_rank"] > (1.0 - top)]
trajectories = filtered_df["traj_id"].values
return trajectories
@joaofig
joaofig / GraphRoute_calculate_trajectory_matches.py
Created November 18, 2022 16:38
This function calculates how similar to the query trajectory the trajectories with matching links are
def calculate_trajectory_matches(self, level=20):
trajectories, links = self.get_matching_trajectories(level)
route_qks = {qk[0] for qk in self.get_route_quadkeys(level)}
data = []
for trajectory in trajectories:
traj_qks = load_trajectory_quadkeys(int(trajectory))
similarity = jaccard_similarity(traj_qks, route_qks)
data.append((trajectory, similarity))
return data
@joaofig
joaofig / jaccard_similarity.py
Created November 18, 2022 16:34
A very simple-minded implementation of the Jaccard Similarity between sets
def jaccard_similarity(set0, set1):
return len(set0 & set1) / len(set0 | set1)
@joaofig
joaofig / GraphRoute_get_overlapping_links.py
Created November 18, 2022 14:03
This function retrieves the database trajectory segments that match the query trajectory.
@joaofig
joaofig / GraphRoute_get_route_quadkeys.py
Created November 18, 2022 12:33
This function converts the generated route to a list of quadkey and bearing pairs.
def get_route_quadkeys(self, level=20):
qks = set()
g = self.graph
shift = 64 - 2 * level
for n0, n1 in pairwise(self.route):
edge = g[n0][n1]
l0 = g.nodes[n0]
l1 = g.nodes[n1]
qks.update([(qk.to_quadint() >> shift, edge[0]['bearing'])
for qk, _ in get_qk_line(l0, l1, level)])
@joaofig
joaofig / GraphRoute_create.py
Created November 18, 2022 11:11
This code instantiates a GraphRoute object and generates a route using two addresses
gr = GraphRoute('Ann Arbor, Michigan')
route = gr.generate_route(addr_ini="122 N Thayer St, Ann Arbor, MI 48104, USA",
addr_end="1431 Ardmoor Ave, Ann Arbor, MI 48103, USA")
@joaofig
joaofig / EVED_insert_link_quadkeys.py
Created November 10, 2022 14:31
This function bulk-inserts quadkey codes into the database
@joaofig
joaofig / EVED_insert_link.py
Created November 10, 2022 14:22
This function inserts a link into the database