Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/cosbidev/PyTrack
Browse files Browse the repository at this point in the history
  • Loading branch information
matteotortora committed Oct 20, 2022
2 parents 10452f9 + e5ed9cf commit c613ad3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pytrack/analytics/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def draw_candidates(self, candidates, radius, point_radius=1, point_color="black
popup = f'{i}-th point \n Latitude: {candidates[obs]["observation"][0]}\n Longitude: ' \
f'{candidates[obs]["observation"][1]}'
folium.Circle(location=candidates[obs]["observation"], popup=popup, radius=point_radius, color=point_color,
fill=point_fill, point_fill_opacity=point_fill_opacity).add_to(fg_gps)
fill=point_fill, fill_opacity=point_fill_opacity).add_to(fg_gps)

# plot candidates
for cand, label, cand_type in zip(candidates[obs]["candidates"], candidates[obs]["edge_osmid"],
Expand Down
14 changes: 8 additions & 6 deletions pytrack/matching/mpmatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ def viterbi_search(G, trellis, start="start", target="target", beta=mpmatching_u

if joint_prob[v_name] < new_prob:
joint_prob[v_name] = new_prob
if v_name.split("_")[0] not in predecessor:
predecessor[v_name.split("_")[0]] = u_name
predecessor_val[v_name.split("_")[0]] = new_prob
elif v_name.split("_")[0] in predecessor and predecessor_val[v_name.split("_")[0]] < new_prob:
predecessor[v_name.split("_")[0]] = u_name
predecessor_val[v_name.split("_")[0]] = new_prob
if v_name not in predecessor:
predecessor[v_name] = u_name
predecessor_val[v_name] = new_prob
elif v_name in predecessor and predecessor_val[v_name] < new_prob:
predecessor[v_name] = u_name
predecessor_val[v_name] = new_prob
if v_name not in queue:
queue.append(v_name)

except Exception as error:
print(error)

predecessor = mpmatching_utils.get_predecessor("target", predecessor)

return joint_prob[target], predecessor
25 changes: 24 additions & 1 deletion pytrack/matching/mpmatching_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def create_path(G, trellis, predecessor):
path_elab: list
list of node IDs.
"""
u, v = list(zip(*[(u, v) for v, u in predecessor.items()][2:]))
u, v = list(zip(*[(u, v) for v, u in predecessor.items()][::-1]))
path = [(u, v) for u, v in zip(u, u[1:])]

path_elab = [node for u, v in path for node in nx.shortest_path(G, trellis.nodes[u]["candidate"].node_id,
Expand Down Expand Up @@ -171,3 +171,26 @@ def create_matched_path(G, trellis, predecessor):
node_ids = create_path(G, trellis, predecessor)
path_coords = [(lat, lng) for lng, lat in LineString([G.nodes[node]["geometry"] for node in node_ids]).coords]
return node_ids, path_coords


def get_predecessor(target, predecessor):
""" Reconstruct predecessor dictionary of a decoded trellis DAG.
Parameters
----------
target: str
Target node of the trellis DAG.
predecessor: dict
Dictionary containing the predecessors of the nodes of a decoded Trellis DAG.
Returns
-------
pred_elab: dict
Dictionary containing the predecessors of the best nodes of a decoded Trellis DAG.
"""
pred_elab = {}
pred = predecessor[target]
while pred != "start":
pred_elab[target.split("_")[0]] = pred
target = pred
pred = predecessor[target]
return pred_elab

0 comments on commit c613ad3

Please sign in to comment.