diff --git a/Trajectories/draw_segments.py b/Trajectories/draw_segments.py index b157003fbb1277a68d2f6ede4b0d5f2ab6e43b84..0ba8906b0f1e45da052e56f1645eba3bf421eb5d 100755 --- a/Trajectories/draw_segments.py +++ b/Trajectories/draw_segments.py @@ -1,7 +1,11 @@ #!/usr/bin/env python3 -"""Reads a graph of segments with cost functions and creates the -corresponding Graphviz file, coloring the segments in red.""" +"""This script reads a graph of segments and creates the corresponding +Graphviz file. If the segments have cost functions then the width of +an edge is inversely proportionnal to the cost function. If the file +traj_segm.json is found then the trajectories are colored in red. + +""" import graph_tool import sys @@ -20,15 +24,22 @@ for v in g_in.vertices(): for e in g_in.edges(): source = e.source() target = e.target() - g_out.add_edge(g_in.vp.name[source], g_in.vp.name[target], - penwidth = 10 / g_in.ep.cost_function[e]) - -with open("traj_segm.json") as f: traj_segm = json.load(f) - -for segment_list in traj_segm: - for i in range(len(segment_list) - 1): - e = g_out.get_edge(segment_list[i], segment_list[i + 1]) - e.attr["color"] = "red" + try: + g_out.add_edge(g_in.vp.name[source], g_in.vp.name[target], + penwidth = 10 / g_in.ep.cost_function[e]) + except AttributeError: + g_out.add_edge(g_in.vp.name[source], g_in.vp.name[target]) + +try: + with open("traj_segm.json") as f: traj_segm = json.load(f) +except FileNotFoundError: + print("traj_segm.json not found") + print("Will not color trajectories.") +else: + for segment_list in traj_segm: + for i in range(len(segment_list) - 1): + e = g_out.get_edge(segment_list[i], segment_list[i + 1]) + e.attr["color"] = "red" g_out.write(sys.argv[2])