# get number of nodes nodes = len(g.vs) # get edges in the right format edgelist = np.array( [edge.tuple for edge in g.es], np.int32) print 'igraph baseline' start = time.clock() igLayout = g.layout_fruchterman_reingold() elapsed = time.clock() elapsed = elapsed - start print "Time spent for igraph baseline is: ", elapsed ig.plot(g, "output/plot_baseline_igraph.png", layout=igLayout) print 'float baseline:' start = time.clock() pos = fl.layout_fr(nodes*dimension, edgelist, max_it, temp ) pos = np.reshape(pos, (nodes, dimension)) floatLayout = ig.Layout( tuple(map(tuple, pos )) ) elapsed = time.clock() elapsed = elapsed - start print "Time spent for fastlayout float baseline is: ", elapsed ig.plot(g, "output/plot_baseline_float.png", layout=floatLayout) # NOTE # this one works different as it uses a different layout # for the data, so no reshaping has to be done, but you # have to zip first and second half of the array print 'layout_fr_stride' start = time.clock() pos = fl.layout_fr_stride( nodes*dimension, edgelist, max_it, temp ) float6Layout = ig.Layout( tuple(zip(pos[0:nodes], pos[nodes:2*nodes])) )
_l = len(edges) #calculate node coords print "VIZUALISATION TIMER: igraph layouting :",datetime.now() #read from tmp file ly_start = datetime.now() graph = igraph.Graph.Read( tmp_name , directed=directed_graph,format="ncol",weights=False ) #new layouting by rcattano dimension = 2 max_it = 500 temp = 1 nodes = len(graph.vs) edges = np.array( [edge.tuple for edge in graph.es], np.int32) pos = fl.layout_fr(nodes*dimension, edges, max_it, temp ) #get the Layout object here. #http://igraph.org/python/doc/igraph.layout.Layout-class.html scl = int( _l/ 100 ) #int( _l * _l / 10 ) if ly_alg == "Fruchterman-Reingold": ly = igraph.Layout(tuple(zip(pos[0:nodes], pos[nodes:2*nodes]))) scl = scl / 3 elif ly_alg == "LGL": ly = graph.layout_lgl() elif ly_alg == "Kamada-Kawai": ly = graph.layout_kamada_kawai() scl = scl / 10 elif ly_alg == "Star": ly = graph.layout_star() scl = scl * 2
def layout(self, ly_alg = "Fruchterman-Reingold", directed_graph=True): if ly_alg not in SUPPORTED_LAYOUTS: return { "graph_ready": False, "errors": "Unsuspported layout algorithm" } print "VIZUALISATION TIMER: igraph layouting :",datetime.now() ly_start = datetime.now() #new layouting by rcattano dimension = 2 max_it = 500 temp = 1 nodes = len(self.igraph.vs) edges = np.array( [edge.tuple for edge in self.igraph.es], np.int32) _l = len(edges) pos = fl.layout_fr(nodes*dimension, edges, max_it, temp ) #get the Layout object here. #http://igraph.org/python/doc/igraph.layout.Layout-class.html scl = int( _l/ 100 ) #int( _l * _l / 10 ) if ly_alg == "Fruchterman-Reingold": ly = igraph.Layout(tuple(zip(pos[0:nodes], pos[nodes:2*nodes]))) scl = scl / 3 elif ly_alg == "LGL": ly = self.igraph.layout_lgl() elif ly_alg == "Kamada-Kawai": ly = self.igraph.layout_kamada_kawai() scl = scl / 10 elif ly_alg == "Star": ly = self.igraph.layout_star() scl = scl * 2 elif ly_alg == "Random": ly = self.igraph.layout_random() scl = scl * 2 else: #ly = igraph.Layout( tuple(map(tuple, pos )) ) #scl = scl / 3 ly = self.igraph.layout_fruchterman_reingold(dim=2,coolexp =1, area = int( _l * _l / 10 ) ) #ly_alg = "Fruchterman-Reingold" #OR standard fruchterman reingold ly_end = datetime.now() diff = ly_end - ly_start print "VIZUALISATION TIMER: returning coordinates :",ly_end print "Layouting took :", diff.seconds, "seconds using", ly_alg # TODO screws up positioning for graphs with few nodes #ly.scale( scl * 4) box = ly.bounding_box() width = abs(box.left) + abs(box.right) coords = ly.__dict__['_coords'] #numpy.float64 cannot be jsonified later, so we convert to standard float: coords = [ [ float( c[0] )* 1 , float( c[1] ) * 1 ] for c in coords ] #todo we have the node names somewhere already ... vertices = [] for i in self.igraph.vs: vertices.append( i['name'] ) all_coords = dict( zip( vertices, coords ) ) #add coords to each layer and get the nodes in each layer intersect with the nodes in the next layer layer_neighborhoods = [] for i,_l in enumerate(self.layer_names): _layer_data = self.data[_l] _layer_data['name'] = _l try: _layer_data_next = data[ self.layer_names[i+1] ] except: #last layer _layer_data_next = self.data[ self.layer_names[i-1] ] #possible with namedtuples #nodes1 = _layer_data.nodes #nodes2 = _layer_data_next.nodes #indegs = _layer_data.in_degrees max_in_deg = 0 max_out_deg = 0 max_total_deg = 0 nodes1 = _layer_data['nodes'] nodes2 = _layer_data_next['nodes'] indegs = _layer_data['in_degrees'] outdegs = _layer_data['out_degrees'] coords = {} common_nodes = list( set( nodes1 ).intersection( set( nodes2 ) ) ) for node_id in nodes1: _common = 1 if node_id in common_nodes else 0 try: indeg = indegs[ node_id ] outdeg = outdegs[ node_id ] except Exception,e: indeg = 0 outdeg = 0 coords[node_id] = [ all_coords[ node_id ], _common, indeg, outdeg, indeg + outdeg, self.custom_scale.get(node_id, 1)] max_out_deg = max(max_out_deg, outdeg) max_in_deg = max(max_in_deg, indeg) max_total_deg = max(max_total_deg, outdeg + indeg) #possible with namedtuples #_layer_data.coords = coords _layer_data['coords'] = coords # add dummy 0 values, so max_in_degree and max_out_degree have to # same offset as the node's degrees _layer_data['maxDeg'] = [0, 0, max_in_deg, max_out_deg, max_total_deg, 1.0] if directed_graph: get_key = lambda v1, v2: ''.join([v1, v2]) else: get_key = lambda v1, v2: ''.join(sorted([v1, v2])) edge_dict = { get_key(x[0], x[1]): x[2] for x in _layer_data['edges']} neighborhood = {} for i, nb in enumerate(self.igraph.neighborhood()): l = [] neighborhood[vertices[i]] = neighborhood.get(vertices[i], l) + [[vertices[j], edge_dict[get_key(vertices[i], vertices[j])]] for j in nb if get_key(vertices[i], vertices[j]) in edge_dict] _layer_data['neighborhood'] = neighborhood
float5Pos = np.copy( pos.astype(np.float32) ) float6Pos = np.copy( pos.astype(np.float32) ) #print np.array_str( floatPos ) #print np.array_str( float2Pos ) print 'igraph' start = time.clock() igLayout = g.layout_fruchterman_reingold() elapsed = time.clock() elapsed = elapsed - start print "Time spent in (function name) is: ", elapsed ig.plot(g, "output/plot_igraph.png", layout=igLayout) print 'float baseline:' fl.layout_fr( edges, floatPos, max_it, temp ) floatLayout = ig.Layout( tuple(map(tuple, floatPos )) ) ig.plot(g, "output/plot_float_baseline.png", layout=floatLayout) #print 'layout_fr_omp' #fl.layout_fr_omp( edges, float2Pos, max_it, temp ) #float2Layout = ig.Layout( tuple(map(tuple, float2Pos)) ) #ig.plot(g, "output/plot_fr_omp.png", layout=float2Layout) #print 'layout_fr_simd' #fl.layout_fr_simd( edges, float3Pos, max_it, temp ) #float3Layout = ig.Layout( tuple(map(tuple, float3Pos)) ) #ig.plot(g, "output/plot_fr_simd.png", layout=float3Layout) #print 'layout_fr_simd_1x4' #fl.layout_fr_simd_1x4( edges, float4Pos, max_it, temp )