diff --git a/gt_segmentation.py b/gt_segmentation.py index aeb75270a4f0af8697de6709b9e34ce8f15fc36a..23f7331762bddb7a1759b37dbe5ac86e165b3ad8 100755 --- a/gt_segmentation.py +++ b/gt_segmentation.py @@ -1,23 +1,10 @@ #!/usr/bin/env python3 -# The main script used for segmentation with a functional algorithm. -# If needed, change: -# node_id_param_file location, -# orientation -# edgelist file name and location -# OUTPUT: a gt file (change name as needed) +# segmentation +# OUTPUT: a graph-tool "gt" file (change name as needed) import graph_tool -from graph_tool import draw import time -import re -import json -import math -from os import path -import shapefile -import datetime -from numpy import loadtxt - import sys ########################## @@ -25,21 +12,22 @@ import sys ########################## if len(sys.argv) != 2: sys.exit("Required argument: edgelist-file") +t0 = time.perf_counter() print('Loading edgelist file...') g = graph_tool.load_graph_from_csv(sys.argv[1], directed = True, eprop_names = ['nl_cost_function'], csv_options = {'delimiter': ' ', 'skipinitialspace': True}) -print('Loading done') - +t1 = time.perf_counter() +print(f'Loading done in {t1 - t0:.0f} s') +t0 = t1 +g.set_fast_edge_removal() ##################### # Set property maps # ##################### -g.set_fast_edge_removal() - g.vp['segment'] = g.new_vp('object') # this object will be a list g.vp['pos_first'] = g.new_vp('object') g.vp['pos_last'] = g.new_vp('object') @@ -55,26 +43,12 @@ print('Starting processing') # Processing # ############## -counter = 1 verts_to_del = [] -t2 = time.perf_counter() - for v in g.vertices(): - if (counter % 10000 == 0): - t3 = time.perf_counter() - print(f'Processing {counter} of {g.num_vertices()}') - print(t3 - t2) - t2 = time.perf_counter() - - counter += 1 - if not g.vp.segment[v]: g.vp.segment[v] = [int(g.vp.name[v])] - #print (f'processing {g.vp.name[v]}...:') - #print (f'In deg: {v.in_degree()}, out deg: {v.out_degree()}') - if v.in_degree() == 1: v2 = next(v.in_edges()).source() @@ -89,16 +63,18 @@ for v in g.vertices(): if(v.in_degree() == 0 and v.out_degree() == 0): verts_to_del.append(v) - -print(f'Done processing ! New graph: {g}') +t1 = time.perf_counter() +print(f'Done processing in {t1 - t0:.0f} s') +t0 = t1 +print("New graph:", g) print(f'Empty nodes: {len(verts_to_del)}. Deleting empty nodes...') - -t2 = time.perf_counter() g.remove_vertex(verts_to_del, fast=True) -t3 = time.perf_counter() -print(t3 - t2) - +t1 = time.perf_counter() +print(f"Done deleting in {t1 - t0:.0f} s") +t0 = t1 print('Saving graph...') g.save('segmented.gt') -print('All done.') +t1 = time.perf_counter() +print(f'Done saving in {t1 - t0:.0f} s.') +print("Everything is cool.")