Skip to content

Instantly share code, notes, and snippets.

View joaofig's full-sized avatar

João Paulo Figueira joaofig

View GitHub Profile
def expand_path(path: PredictedPath,
max_branch: int = 3) -> list[PredictedPath]:
successors = get_successors(path.array[path.step-2], path.array[path.step-1])
best_expansions = []
total = successors.total()
for p in successors.most_common(max_branch):
new_probability = p[1] / total
new_path = evolve_path(path, p[0], new_probability)
best_expansions.append(new_path)
return best_expansions
def expand_seed(h0: int, h1: int,
max_branch: int = 3,
max_length: int = 10) -> list[PredictedPath]:
final = []
seed = PredictedPath.from_seed(h0, h1, max_length)
paths = expand_path(seed, max_branch)
while len(final) < max_branch:
expanded = []
for p in paths:
expanded.extend(expand_path(p, max_branch=max_branch))
class PredictedPath():
def __init__(self,
probability: float = 1.0,
step: int = 1,
size: int = 0):
self.probability = probability
self.step = step
self.size = size
self.array: np.ndarray = np.zeros(size, dtype=int)
def predict(max_branch: int = 3,
max_length: int = 10) -> FeatureGroup | None:
fg = folium.FeatureGroup(name="polylines")
if "token_list" in st.session_state:
token_list = st.session_state["token_list"]
seed = token_list[-3:-1]
if len(seed) > 1:
paths = expand_seed(seed[0], seed[1],
max_branch=max_branch,
def predict(max_branch: int = 3,
max_length: int = 10) -> FeatureGroup | None:
fg = folium.FeatureGroup(name="polylines")
if "token_list" in st.session_state:
hex_list = st.session_state["token_list"]
seed = hex_list[-3:-1]
if len(seed) > 1:
paths = expand_seed(seed[0], seed[1],
max_branch=max_branch,
def get_successors(h0: int, h1: int) -> Counter:
cnt = get_cache_successors(h0, h1)
if cnt is None:
db = TrajDb()
sql = "select t2 from triple where t0=? and t1=?"
successors = [r[0] for r in db.query(sql, [int(h0), int(h1)])]
cnt = Counter(successors)
set_cache_successors(h0, h1, cnt)
return cnt
def compute_probability(token_list: list[int]) -> float:
nodes = token_list[1:-1]
prob = 0.0
if len(nodes) > 2:
prob = 1.0
for i in range(len(nodes)-2):
t0, t1, t2 = nodes[i:i+3]
cnt = get_successors(int(t0), int(t1))
if len(cnt):
prob *= cnt[t2] / cnt.total()
@joaofig
joaofig / match-trips_insert_h3_nodes.py
Created July 2, 2023 17:37
Inserts the mapping between H3 tokens and map node coordinates
def insert_h3_nodes(h3_nodes: list[tuple[int,tuple[float,float]]]):
db = TrajDb()
sql = "insert or ignore into h3_node (h3, lat, lon) values (?, ?, ?)"
db.execute_sql(sql, [(n[0], n[1][1], n[1][0]) for n in h3_nodes], many=True)
@joaofig
joaofig / match-trips_insert_triples.py
Created July 2, 2023 17:35
Inserts the expanded triples list
def insert_triples(traj_id: int,
triples: list[(int,int,int)]):
db = TrajDb()
sql = "insert into triple (traj_id, t0, t1, t2) values (?, ?, ?, ?)"
params = [(traj_id, t0, t1, t2) for t0, t1, t2 in triples]
db.execute_sql(sql, params, many=True)
@joaofig
joaofig / match-trips_insert_h3.py
Created July 2, 2023 17:32
Inserts the trajectory H3 token list
def insert_h3(traj_id: int,
h3_list: list[int]) -> None:
db = TrajDb()
sql = "insert into traj_h3 (traj_id, h3) values (?, ?)"
params = [[traj_id, int(h)] for h in h3_list]
db.execute_sql(sql, params, many=True)