Skip to content
Snippets Groups Projects
Commit aac6fb37 authored by Lionel GUEZ's avatar Lionel GUEZ
Browse files

Make `trajectories` an attribute of Visitor

Instead of a global variable with hidden modification in a method of Visitor.
parent 369efdba
No related branches found
No related tags found
No related merge requests found
......@@ -22,11 +22,14 @@ from graph_tool import util, search
import sys
class Visitor(search.BFSVisitor):
def __init__(self):
self.trajectories = {}
def discover_vertex(self, u):
if g.vp.traj[u] not in trajectories:
trajectories[g.vp.traj[u]] = [g.vp.name[u]]
if g.vp.traj[u] not in self.trajectories:
self.trajectories[g.vp.traj[u]] = [g.vp.name[u]]
else:
trajectories[g.vp.traj[u]].append(g.vp.name[u])
self.trajectories[g.vp.traj[u]].append(g.vp.name[u])
g = graph_tool.load_graph(sys.argv[1])
g.vp['traj'] = g.new_vp('int')
......@@ -65,17 +68,17 @@ print('Algorithm done, saving...')
# Make a dictionary of trajectories:
trajectories = {}
search.bfs_search(g, visitor = Visitor())
visitor = Visitor()
search.bfs_search(g, visitor = visitor)
with open("trajectories.json", "w") as outfile:
json.dump(trajectories, outfile, indent = 4)
json.dump(visitor.trajectories, outfile, indent = 4)
# Setup a new trajectory that holds the expanded segments:
expanded_trajectories = {}
for trajectory in trajectories.values():
for trajectory in visitor.trajectories.values():
# Expand trajectory of current traj key = []
key = trajectory[0]
for val in trajectory:
......@@ -98,4 +101,4 @@ for trajectory in trajectories.values():
with open("expanded_trajectories.json", "w") as outfile:
json.dump(expanded_trajectories, outfile, indent = 4)
print(f'Done saving json: {len(trajectories)} trajectories.')
print(f'Done saving json: {len(visitor.trajectories)} trajectories.')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment