From 46d960c08efcd986c3e20fc658f24af638fb8d83 Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Wed, 18 May 2022 16:37:00 +0200
Subject: [PATCH] Polish

---
 gt_matlab_trajs.py | 40 +++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/gt_matlab_trajs.py b/gt_matlab_trajs.py
index 8d02cb93..42a0b34c 100755
--- a/gt_matlab_trajs.py
+++ b/gt_matlab_trajs.py
@@ -1,10 +1,10 @@
 #!/usr/bin/env python3
 
 # A script that loads a segmented cf graph in the gt format, iterates on all
-# of the edges and cost functions and generates the trajectories as done in 
+# of the edges and cost functions and generates the trajectories as done in
 # MATLAB. Segments (nodes) have a new vertex property called "traj" that holds
-# the trajectory ID that that segment belongs to. The output is either an 
-# expanded JSON file and/or an edgelist file with all of the trajectories. 
+# the trajectory ID that that segment belongs to. The output is either an
+# expanded JSON file and/or an edgelist file with all of the trajectories.
 # Inputs:
     # orientation
     # name and the location of the gt segmented cf graph
@@ -28,25 +28,25 @@ g.vp['traj'] = traj_mark
 # assign the new vertex property
 for node in g.vertices():
     g.vp.traj[node] = g.vp.name[node]
-    
-    
+
+
 ##################
 # MAIN ALGORITHM #
 ##################
 
 # iterating on edges:
-    
+
 print('Algorithm starting...')
 
 for edge in g.edges():
     current_cf = g.ep.cost_function[edge]
     src = edge.source()
-    trg = edge.target()    
-    
+    trg = edge.target()
+
     # source is a split
-    if src.out_degree() > 1:       
+    if src.out_degree() > 1:
         if current_cf <= min({g.ep.cost_function[e] for e in src.out_edges()}):
-            if trg.in_degree() > 1:                
+            if trg.in_degree() > 1:
                 if current_cf <= min({g.ep.cost_function[e]
                                       for e in trg.out_edges()}):
                     # Set trg trajectory to src trajectory
@@ -55,10 +55,10 @@ for edge in g.edges():
                 g.vp.traj[trg] = g.vp.traj[src]
     else:
         # source is a continuation or root with one out
-        if trg.in_degree() > 1:                    
+        if trg.in_degree() > 1:
             if current_cf <= min({g.ep.cost_function[e]
                                   for e in trg.in_edges()}):
-                g.vp.traj[trg] = g.vp.traj[src] 
+                g.vp.traj[trg] = g.vp.traj[src]
         else:
             g.vp.traj[trg] = g.vp.traj[src]
 
@@ -73,28 +73,30 @@ for node in g.vertices():
         trajectories[g.vp.traj[node]] = [g.vp.name[node]]
     else:
         trajectories[g.vp.traj[node]].append(g.vp.name[node])
-        
+
 # setup a new trajectory that holds the expanded segments
 expanded_trajectories = {}
 
 for trajectory in trajectories.values():
-    # exp traj of current traj key = []
+    # Expand trajectory of current traj key = []
     key = trajectory[0]
     for val in trajectory:
         node = ut.find_vertex(g, g.vp.name, val)
+
         if len(node) != 1:
-            print('Something is wrong.')            
+            print('Something is wrong.')
+
         node = node[0]
         seg = g.vp.inst_eddies[node]
+
         for s in seg:
             if key not in expanded_trajectories:
-                expanded_trajectories[key] = []
-                expanded_trajectories[key].append(s)
+                expanded_trajectories[key] = [s]
             else:
                 expanded_trajectories[key].append(s)
-        
+
 # write out the json file
 with open("trajectories.json", "w") as outfile:
     json.dump(expanded_trajectories, outfile, indent = 4)
-    
+
 print(f'Done saving json: {len(trajectories)} trajectories.')
-- 
GitLab