Ejemplo n.º 1
0
def convert_to_igraph(road_map):
    # First, re-index nodes from 0 to N-1

    shuffle(road_map.nodes)
    
    n = len(road_map.nodes)
    new_node_ids = {}
    for i in xrange(n):
        new_node_ids[road_map.nodes[i].node_id] = i
    
    # Now, create a Graph object with the correct number of nodes
    graph = Graph(n, directed=False)
    
    """
    # Add the correct links
    for i in xrange(n):
        if(i%1000==0):
            print i
        for link in road_map.nodes[i].forward_links:
            j = new_node_ids[link.connecting_node.node_id]
            graph.add_edge(i,j)
    """
    edge_set = set()
    
    for i in xrange(n):
        for link in road_map.nodes[i].forward_links:
            j = new_node_ids[link.connecting_node.node_id]
            
            x = min(i,j)
            y = max(i,j)
            edge_set.add((x,y))
    
    graph.add_edges(list(edge_set))
    
    return graph
def disp_tree(tree):

	from igraph import Graph, plot

	g = Graph(directed=True)
	g.add_vertices(len(tree))

	g.vs['label'] = [node.name for node in tree]

	nodes_to_add = set([0])
	while len(nodes_to_add) != 0:

		i_node = nodes_to_add.pop()
		node = tree[i_node]
		g.vs['label'][i_node] = node.name

		left_node = node.left_child
		right_node = node.right_child

		if left_node != None:
			i_left = tree.index(left_node)
			g.add_edges((i_node, i_left))
			nodes_to_add.add(i_left)
		
		if right_node != None:
			i_right = tree.index(right_node)
			g.add_edges((i_node, i_right))
			nodes_to_add.add(i_right)

	layout = g.layout_reingold_tilford(root=0)
	plot(g, layout=layout, bbox=(0, 0, 3000, 1000))
Ejemplo n.º 3
0
 def _gen_graph(self):
     # make it lazy to support optional addslaves command
     from igraph import Graph
     g = Graph().as_directed()
     g.add_vertices(self.vertex_count)
     g.es['weight'] = 1  # enable weight
     return g
Ejemplo n.º 4
0
 def testBug128(self):
     y = [1, 4, 9]
     g = Graph(n=len(y), directed=True, vertex_attrs={'y': y})
     self.assertEquals(3, g.vcount())
     g.add_vertices(1)
     # Bug #128 would prevent us from reaching the next statement
     # because an exception would have been thrown here
     self.assertEquals(4, g.vcount())
Ejemplo n.º 5
0
def growInstanceGraph(g, pattern, parentPattern, parentIg):
  ''' Create the instance graph for pattern @pattern from its parent pattern @parentPattern whose
      instance graph is given by @parentIg '''

  childEdges = set([x.index for x in pattern.getEdgeList()]) 
  parentEdges = set([x.index for x in parentPattern.getEdgeList()])
  newEdgeIndex = childEdges.difference(parentEdges)

  dfsCode = str(pattern.getDFSCode())
  parentDfsCode = str(parentPattern.getDFSCode())
  print pattern.getDFSCode()

  if dfsCode not in PATTERNS:
    PATTERNS[dfsCode] = set()

  if not newEdgeIndex:
    return None
  if len(newEdgeIndex) != 1:
    raise Exception("Cannot grow instance graph beucase has child pattern has %d edges more than the parent pattern"%len(newEdgeIndex))
  
  newEdge = g.es[newEdgeIndex.pop()]
  [v0, v1] = getVertices(g, newEdge) #V0 and V1 are the vertices of the new edge that is present in the child pattern

  newIg = Graph()
  for p in PATTERNS[parentDfsCode]:
    newPatternList = []
    pEdgeIndices = set([x.index for x in p.getEdgeList()])
    for gv in p.getVertices():
      if gv["nl"] != v0["nl"]:
        continue

      for gu in gv.neighbors():
        if gu["nl"] != v1["nl"]:
          continue

        e = getEdge(g, gv, gu)
        if e.index in pEdgeIndices:
          continue

        newPatternEdges = list(p.getEdgeList()) #TODO: Fix this.
        newPatternEdges.append(e)
        newPattern = Pattern(g, newPatternEdges)

        if newPattern in PATTERNS[dfsCode]:
          continue
        PATTERNS[dfsCode].add(newPattern)

        print newPattern
        print

        newPatternList.append(newPattern)
        newIg.add_vertex()
        vid = newIg.vs[-1:].indices[0]
        INSTANCE_GRAPH_NODES[newPattern] = vid
  
  #Create edges between vertices of newIg
  createInstanceGraphEdges(newIg, dfsCode) 
  return newIg
Ejemplo n.º 6
0
	def get_domain_graph(vnames,enames):
		g = Graph(directed=True)
		g.add_vertices(vnames)
		es = []
		for en in enames:
			sn,tn = en.split('-')
			es.append((sn,tn))
		g.add_edges(es)
		return g 		
Ejemplo n.º 7
0
def create_graph(vertice,edges=[]):
    """Create a graph using igraph
    :add_vertices() method of the Graph class adds the given number of vertices to the graph.
    :add_edges() method of the Graph class adds edges, they are specified by pairs of integers,
     so [(0,1), (1,2)] denotes a list of two edge. igraph uses integer vertex IDs starting from zero
    """
    graph = Graph(directed=True)
    graph.add_vertices(vertice)
    graph.add_edges(edges)
    return graph
def read_graph(file_name):
    # Input edge list file name and output igraph representation
    df = pd.read_csv(file_name, sep=" ", names=["Edge1", "Edge2"])
    n_vertex, n_edge = df.irow(0)
    df = df.drop(0)
    graph = Graph(edges=[(x[1]["Edge1"], x[1]["Edge2"])
                  for x in df.iterrows()], directed=False)
    assert(graph.vcount() == n_vertex)
    assert(graph.ecount() == n_edge)
    return preprocess_graph(graph)
Ejemplo n.º 9
0
def merge(g1,g2):
	""" merges graph g1 and graph g2 into the output graph"""
	g3nslst = list(set(g1.vs['name'][:]) | set(g2.vs['name'][:])) 
	g3 = Graph(0,directed=True)
	g3.add_vertices(g3nslst)
	g3elst = []
	for e in g1.get_edgelist():
		g3elst.append((g1.vs['name'][e[0]],g1.vs['name'][e[1]]))
	for e in g2.get_edgelist():
		g3elst.append((g2.vs['name'][e[0]],g2.vs['name'][e[1]]))
	g3.add_edges(g3elst)
	g3.simplify()
	#add attributes
	g1primlst = [vn for i,vn in enumerate(g1.vs['name'][:]) if int(g1.vs['inprim'][i]) == 1]
	g2primlst = [vn for i,vn in enumerate(g2.vs['name'][:]) if int(g2.vs['inprim'][i]) == 1]
	g3prim = [1 if vn in g1primlst or vn in g2primlst else 0 for vn in g3.vs['name'][:]]
	g3pnamelst = [[] for i in range(len(g3.vs['name'][:]))]
	for i,vn1 in enumerate(g3.vs['name'][:]):
		for j,vn2 in enumerate(g1.vs['name'][:]):
			if vn1 == vn2:
				g3pnamelst[i].extend(g1.vs['pnamelst'][j].strip().split('|'))
		for j,vn2 in enumerate(g2.vs['name'][:]):
			if vn1 == vn2:
				g3pnamelst[i].extend(g2.vs['pnamelst'][j].strip().split('|'))
	g3.vs['pnamelst'] = ['|'.join(map(str,list(set(inp)))) if inp != [] else '' for inp in g3pnamelst]
	#print g1.vs['pnamelst'][:]	
	#print g3.vs['name'][:]
	g3.vs['inprim'] = g3prim
	return g3
def load_adjlist(filename, directed=True):
    edgelist = []
    names = UniqueIdGenerator()
    for line in open(filename):
        parts = line.strip().split()
        u = names[parts.pop(0)]
        edgelist.extend([(u, names[v]) for v in parts])
    logging.debug("Edgelist for line %s : %s" % (parts, edgelist))
    g = Graph(edgelist, directed=directed)
    g.vs["name"] = names.values()
    return g
def file2igraph(file):
    """
    Converts graph file into iGraph object, adds artifacts
    """
    with open(file, 'r') as fi:
        v,e = fi.next().split()
        e_list = [(int(i.split()[0]), int(i.split()[1])) for i in list(fi)]
        assert (int(e) == len(e_list)),\
                    "#edges mentioned and # of edges in file differ"
        g = Graph()
        g.add_vertices(int(v))
        g.add_edges(e_list)
        return g
Ejemplo n.º 12
0
def graph_from_sparse(data, directed=None):
    from igraph import Graph
    sources, targets = data.nonzero()
    
    if directed==None:
        from numpy import all
        directed = not all(data[sources, targets]==data[targets, sources])
    from numpy import array
    g = Graph(zip(sources, targets), directed=directed, edge_attrs={'weight': array(data[sources, targets])[0]})
    if g.is_directed():
        return g
    else:
        return g.simplify(combine_edges="first")
Ejemplo n.º 13
0
def generate_seed_graph(g, k):
    vcounts = g.vcount()
    init_seed = random.randint(0, vcounts)

    seed_graph = Graph(directed=False)
    seed_graph.add_vertex(g.vs[init_seed]['name'], degree=g.degree(init_seed))

    while seed_graph.vcount() != k:
        choiced_vertex = random.choice(seed_graph.vs)
        choiced_vertex_index = g.vs.find(name=choiced_vertex['name'])
        choiced_neighor = g.vs[random.choice(g.neighbors(choiced_vertex_index))]
        if choiced_neighor['name'] in seed_graph.vs['name']:
            continue
        seed_graph.add_vertex(choiced_neighor['name'], degree=g.degree(choiced_neighor['name']))
        choiced_neighor_neighor = g.neighbors(choiced_neighor.index)
        choiced_neighor_neighor_name = [g.vs[i]['name'] for i in choiced_neighor_neighor]
        existed_nodes = set(choiced_neighor_neighor_name) & set(seed_graph.vs['name'])


        for node in existed_nodes:
            choiced_neighor_id = seed_graph.vs.find(name=choiced_neighor['name']).index
            node_id = seed_graph.vs.find(name=node).index
            seed_graph.add_edge(choiced_neighor_id, node_id)

    return seed_graph
Ejemplo n.º 14
0
def buildGraph(taskList, tsize, eList):

  G = Graph(tsize + len(eList), directed=False)
  G.vs['name'] = taskList.keys() + eList
  G.vs['type'] = 0
  G.vs[tsize:]['type'] = 1
  for task, entityList in taskList.iteritems():
    for entity, score in entityList.iteritems():
      #a dict -- entity: score
      #print task, entity
      G[task, entity] = score
  #print G	
  #print G.is_bipartite()
  return G
Ejemplo n.º 15
0
class Physic (BaseInputGraph):
  def __init__(self):
    '''
    @return: Arxiv ASTRO-PH (Astro Physics) collaboration network as iGraph graph instance 
    '''
    edges = []
    weights = []
    f = open("./physic/compact-physic.txt", "r")
    for line in f:
      if line and line[0]!='#':
        seg = line.split()
        edges.append( (int(seg[0]), int(seg[1])) )
        weights.append( 1 )
    maxid = max( edges, key=itemgetter(1) )[1]
    maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
    self.g = Graph()
    self.g.add_vertices(maxid + 1)
    self.g.add_edges(edges)
    self.g.to_undirected()
    self.g.simplify()
    self.g.vs["myID"] = [ str(int(i)) for i in range(maxid+1)]
    print "#nodes=", maxid + 1
    print "#edges=", len(self.g.es)

  def run(self):
    C = BaseInputGraph.unsupervised_logexpand(self)
    BaseInputGraph.run(self, C, p0=np.array([0.04, 0.04]))
    with open("./physic/Physic_weights.pairs", "w+") as txt:
      for e in self.g.es:
        txt.write("%d %d %f\n" %(e.tuple[0], e.tuple[1], e["weight"]) )
    def __findNegativeCut(self,debug=False):
        """Best negative cut heuristic.

        Heuristic to find the best cut value to construct the Gamma Model (RMgamma).

        Args:
            debug (bool,optional): Show debug information. 

        Returns:
            A Heuristic object that contains all the relevant info about the heuristic.
        
        """
        
        time_total = time.time()

        # Graph and unique set construction
        time_graph_construction = time.time()

        graph_negative = Graph()
        graph_negative.add_vertices(self.__n)
        unique_negative_weights = set()
        for i in range(self.__n):
            for j in range (i+1,self.__n):
                if self.__S[i][j] <= 0:
                    graph_negative.add_edge(i,j,weight=self.__S[i][j])
                    unique_negative_weights.add(self.__S[i][j])
        time_graph_construction = time.time() - time_graph_construction

        # Sort unique weights and start heuristic to find the best cut value
        time_find_best_cut = time.time()
        
        unique_negative_weights = sorted(unique_negative_weights)

        # Test different cuts and check connected
        best_negative_cut = 0
        for newCut in unique_negative_weights:
            edges_to_delete = graph_negative.es.select(weight_lt=newCut)
            graph_negative.delete_edges(edges_to_delete)
            if graph_negative.is_connected():
                best_negative_cut = newCut
            else:
                break

        time_find_best_cut = time.time() - time_find_best_cut
        time_total = time.time() - time_total

        if debug==True:
            print ("Time Graph Construction: %f"         %(time_graph_construction))
            print ("Time Heuristic to find best cut: %f" %(time_find_best_cut))
            print ("Total Time: %f"                      %(time_total))
            print ("NEW (Best cut-): %d"                 %(best_negative_cut))

        heuristic={}
        heuristic['cut'] = best_negative_cut
        heuristic['time_total']=time_total
        heuristic['time_graph_construction']=time_graph_construction
        heuristic['time_find_best_cut']=time_find_best_cut

        return heuristic
Ejemplo n.º 17
0
    def test_shouldReturnCommunityLevelWithMaximumCommunities(self):
        graph_1 = Graph()
        graph_1.add_vertices(["1","2","3","4"])
        graph_1.add_edges([("1","2"),("2","4"),("1","4"),("2","3")])
        graph_2 = Graph()
        graph_2.add_vertices(["1","3","4"])
        graph_2.add_edges([("3","4"),("1","4"),("1","3")])
        vertex_clustering_1 = VertexClustering(graph_1)
        vertex_clustering_2 = VertexClustering(graph_2)

        community_levels = [vertex_clustering_1, vertex_clustering_2]
        expected_communities = vertex_clustering_1
        actual_communities = SentenceGraph().find_best_community_level(community_levels)

        self.assertEquals(actual_communities, expected_communities)
Ejemplo n.º 18
0
class Ring ( BaseInputGraph ):
  wvalue = 1
  g = Graph() # the iGraph graph instance

  def __init__(self, v):
    '''
    @param v: the weight of edge connecting two cliques
    @return: the produced ring network as iGraph graph instance 
    '''
    self.wvalue = v
    edges = []
    ws = []
    clique_num = 30
    clique_size = 5
    for c in range(0, clique_num):
      for i in range(clique_size*c, clique_size*(c+1)):
        for j in range(i+1, clique_size*(c+1)):
          edges.append((i,j))
          ws.append(1)
    for c in range(0, clique_num):
          edges.append((clique_size*c, clique_size*( (c+1) % clique_num)))
          ws.append(self.wvalue)
    maxid = max( edges, key=itemgetter(1))[1]
    maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
    
    self.g = Graph()
    self.g.add_vertices(maxid + 1)
    self.g.add_edges(edges)
    self.g.es["weight"] = ws
    self.g.vs["comm"] = [str( int(_ / clique_size) ) for _ in range(len(self.g.vs))]
    print "#nodes=", maxid + 1
    print "#edges=", len(self.g.es)

  def run(self):
    '''
    run the algorithm
    '''
    #supervised
    group = [0,1,2,3,4]
    C = BaseInputGraph.get_C(self, group)
    #unsupervised
    #C = BaseInputGraph.unsupervised_logexpand(self)
    print C
    BaseInputGraph.run(self, C)
    for e in self.g.es:
      print "(",e.tuple[0]," ,",
      print e.tuple[1],")=",
      print e["weight"]
Ejemplo n.º 19
0
	def create_dgraph(self):
		""" initializes self.graph: the total directed graph of reactions. It ereases any preceding graph. """
		self.dgraph = Graph(0,directed = True)
		cnlst = []
		rnlst = []
		rlinks = []
		for rct in self.rlst:
			rnlst.append(rct.name)	
			for i in rct.innames:
				cnlst.append(i)
				rlinks.append((i,rct.name))
			for o in rct.outnames:
				cnlst.append(o)
				rlinks.append((rct.name,o))
			if rct.reversible == True:
				rnlst.append(rct.name+'_r')
				for o in rct.innames:
					rlinks.append((rct.name+'_r',o))
				for i in rct.outnames:
					rlinks.append((i,rct.name+'_r'))
		cnlst = list(set(cnlst))
		cinpathlst = [self.gsearch(cn).inpath if self.gsearch(cn) != None else [] for cn in cnlst]
		rnlst = list(set(rnlst))
		rinpathlst = [self.rsearch(rn).inpath if '_r' not in rn else self.rsearch(rn[0:len(rn)-2]) for rn in rnlst]
		cnlst.extend(rnlst)
		cinpathlst.extend(rinpathlst)
		self.dgraph.add_vertices(cnlst)
		self.dgraph.add_edges(rlinks)
		self.dgraph.vs['inpath'] = cinpathlst
		#print rlinks[len(rlinks)-3000:len(rlinks)-1]
		print self.dgraph.summary()
Ejemplo n.º 20
0
 def __init__(self, v):
   '''
   @param v: the weight of edge connecting two cliques
   @return: the produced ring network as iGraph graph instance 
   '''
   self.wvalue = v
   edges = []
   ws = []
   clique_num = 30
   clique_size = 5
   for c in range(0, clique_num):
     for i in range(clique_size*c, clique_size*(c+1)):
       for j in range(i+1, clique_size*(c+1)):
         edges.append((i,j))
         ws.append(1)
   for c in range(0, clique_num):
         edges.append((clique_size*c, clique_size*( (c+1) % clique_num)))
         ws.append(self.wvalue)
   maxid = max( edges, key=itemgetter(1))[1]
   maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
   
   self.g = Graph()
   self.g.add_vertices(maxid + 1)
   self.g.add_edges(edges)
   self.g.es["weight"] = ws
   self.g.vs["comm"] = [str( int(_ / clique_size) ) for _ in range(len(self.g.vs))]
   print "#nodes=", maxid + 1
   print "#edges=", len(self.g.es)
Ejemplo n.º 21
0
 def __init__(self, trialval=1):
   ws = []
   edges = []
   self.trial = trialval
   with open("./binary_networks/mu0.5/network%d.dat" % self.trial, "r") as txt:
     for line in txt:
       seg = line.split()
       edges.append((int(seg[0]), int(seg[1])))
       ws.append(1)
   maxid = max( edges, key=itemgetter(1))[1]
   maxid = max( maxid, max(edges,key=itemgetter(0))[0] )
   self.g = Graph()
   print maxid
   self.g.add_vertices(maxid + 1)
   with open("./binary_networks/mu0.5/community%d.dat" % self.trial, "r") as txt:
     for line in txt:
       seg = line.split()
       #print seg[0]
       self.g.vs[int(seg[0])]["comm"] = seg[1] #note: string is returned
   self.g.add_edges(edges)
   self.g.to_undirected()
   self.g.simplify()
   self.g.delete_vertices(0)
   self.g.es["weight"] = ws
   BaseInputGraph.write_ground_truth(self, "./ground_truth_community%d.groups" % self.trial)
   print "#nodes=", maxid + 1
   print "#edges=", len(self.g.es) 
Ejemplo n.º 22
0
def parse_seed_response(data):
    g = Graph(directed=False)
    lines = data.splitlines()
    #query_count = lines[1]
    node_attr, edge_attr = [int(i) for i in lines[2].split(' ')]
    node_count = int(lines[3])
    for i in lines[4: node_count+4]:
        attr_dict = parse_node_attribute(i, node_attr)
        if not attr_dict:
            continue
        g.add_vertex(**attr_dict)
    for i in lines[node_count+5:]:
        attr_dict = parse_edge_attribute(i, edge_attr)
        if not attr_dict:
            continue
        g.add_edge(**attr_dict)
    return g, node_attr, edge_attr
Ejemplo n.º 23
0
    def __init__(self, steps, links=None, nlinks=None, name=None):
        Graph.__init__(self, directed=True, graph_attrs={'name': name})

        for step in steps:
            self._set_io_fields(step)
            self.add_vertex(**step)
        assert len(steps) < 2 or links or nlinks, (
            'steps must be defined with links'
        )
        if nlinks:
            self._add_ctrlflow_links(self._get_links(nlinks))
        elif links:
            self._add_ctrlflow_links(links)
        self.validate()
        self.check_dataflow()
        self._add_dataflow_links()
        self.validate()
Ejemplo n.º 24
0
def createTestGraph():
  ''' Test code. Creates a graph to test on'''
  #g = Graph([(0,1), (0,2), (2,3), (3,4), (4,2), (2,5), (5,0), (6,3), (5,6)]) #Test graph
  g = Graph([(0,1),(1,2),(0,2),(2,3),(3,4),(4,5),(3,5),(5,6),(3,6),(6,7),(2,7),(7,8),(8,9),(9,10),(8,10),(6,8)])
  # Create vertex labels
  vertex_lables = ["A", "B", "B", "B", "A", "B", "B", "C", "A", "B", "B"]
  # Create edge labels
  #edge_labels = ["a", "a", "a", "a", "b", "b", "b", "c", "d"]
  edge_labels = ["a"]
  g.vs["l"] = vertex_lables
  g.es["l"] = edge_labels
  '''
  g = Graph([(0,1), (1,2), (2,0), (2,3)])
  g.vs["nl"] = ["A", "B", "C", "D"]
  g.es["nl"] = ["e"]
  '''
  return g
Ejemplo n.º 25
0
    def __init__(self, name):
        self._g = Graph(directed=True)
        self._kernels = dict()
        self._length = dict()
        self._max_depth = -1
        self._num_kernels = 0

        super().__init__(name, 'dag')
Ejemplo n.º 26
0
    def __init__(self):
        self.br = mechanize.Browser(factory=mechanize.RobustFactory())
        self.br.set_handle_robots(False)
        self.br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008\
071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
        self.graph = Graph(0, directed=True)
        self.urls = {}
        self.path = []
Ejemplo n.º 27
0
	def get_graph_from_matrix(assoc_mat,density,ignore_weights=True):
		""" get graph from association matrix """
		mat = assoc_mat.vmat
		l = mat.shape[0]
		g = Graph(directed=True)
        	g.add_vertices(l)
        	g.vs['name'] = assoc_mat.names
		max_edges = int(l*(l-1)*density)
		a,b = mat.nonzero()
		values = sortedlist()
		es = []
		weights = []
		for z in range(len(a)):
			values.add(mat[a[z],b[z]])
		if mat.nnz < max_edges:
			threshold = min(values)
		else:
			threshold = values[len(values)-(max_edges+1)]
                for z in range(len(a)):
			w = mat[a[z],b[z]]
                	if w >= threshold:
                        	es.append((a[z],b[z]))
				weights.append(w)
                g.add_edges(es)
		if not ignore_weights:
			g.es['weight'] = weights
			# this amounts to row-normalize the adjacency matrix
			GraphUtils.normalize_out_weights(g)
		return g
Ejemplo n.º 28
0
def translate_cfg(neo4j_db, function_node):
    cfg_edges = get_cfg_edges(neo4j_db, function_node)
    
    #create igraph cfg
    g = Graph(directed = True)
    
    #add edge and edge properties
    for edge in cfg_edges :
        start_node = edge.start_node
        end_node = edge.end_node
        
        if start_node is None or end_node is None:
            print "edge has no start or end node"
        
        try:
            g.vs.find(name=str(start_node._id))
        except:
            g.add_vertex(name=str(start_node._id), **get_node_properties(start_node.properties))
        try:
            g.vs.find(name=str(end_node._id))
        except:
            g.add_vertex(name=str(end_node._id), **get_node_properties(end_node.properties))
        
        g.add_edge(str(start_node._id), str(end_node._id),**get_cfg_edge_properties(edge.properties))
    
    return g
Ejemplo n.º 29
0
    def __json_loads(self, json_str):
        """
        Load graph from json file
        """
        json_dict = json.loads(json_str)
        cp = Graph()
        for v in json_dict["nodes"]:
            node = Node(v["id"], timestamps=v["timestamps"], colors=v["colors"],label=v["id"],
                        attributes=v["attributes"], membership=v["membership"],positions=v["positions"])
            cp.__add_node(node)

        for e in json_dict["edges"]:
            edge = Edge(e["source"], e["target"], timestamps=e["timestamps"],attributes=e["attributes"],
                        ranges=e["ranges"])
            cp.__add_edge(edge)
        self.__dict__ = json_dict
        self.nodes = cp.nodes
        self.edges = cp.edges
def readFromFile(file):
    """
    Function which forms igraph graph object by reading
    edgelsit from the input file
    :param file:
    :return:the graph g, number of vertices of g
    """
    with open(file, mode="r") as f:
        vertices, edges = f.readline().split()
        edgeList = list()
        for line in f:
            #add each line to edgelist as (a,b) where a and b are vertex ids
            edgeList.append((int(line.split()[0]), int(line.split()[1])))
    g = Graph(int(vertices))
    g.add_edges(edgeList)
    #makes sure that edges are equal to nubmer specified in file
    assert (int(edges) == len(edgeList))
    return g, int(vertices)
Ejemplo n.º 31
0
from igraph import Graph
from igraph import plot

grafo5 = Graph(edges=[(0, 1), (2, 3), (0, 2), (0, 3)], directed=True)
grafo5.vs['label'] = ['Fernando', 'Pedro', 'Jose', 'Antonio']
grafo5.vs['peso'] = [40, 30, 30, 25]
grafo5.es['TipoAmizade'] = ['Amigo', 'Inimigo', 'Inimigo', 'Amigo']
grafo5.es['weight'] = [1, 2, 1, 3]

for v in grafo5.vs:
    print(v)

for e in grafo5.es:
    print(e)

grafo5.vs['cor'] = ['blue', 'red', 'yellow', 'green']

plot(grafo5,
     bbox=(300, 300),
     vertex_size=grafo5.vs['peso'],
     edge_width=grafo5.es['weight'],
     vertex_color=grafo5.vs['cor'],
     edge_curved=0.4,
     vertex_shape='square')

# tkplot
Ejemplo n.º 32
0
    def calcAllRoutes(net):
        """ Configures IP routes between all nodes in the emulation topology. This is done in three
         steps:

        1) IP forwarding is enabled on all nodes
        2) The igraph lib is used to calculate all shortest paths between the nodes
        3) Route add commands are used to actually configure the ip routes

        :param net:
        """

        mini_nodes = net.hosts
        mini_links = net.links

        # Enabling IP forwaring on all nodes
        info('Configure IP forwarding on all nodes\n')
        for node in mini_nodes:
            node.cmd('sysctl -w net.ipv4.ip_forward=1')

        # Calculate igraph to calculate all shortest paths between nodes
        node_names = [node.name for node in mini_nodes]
        links = []
        for link in mini_links:
            links.append((link.intf1.node.name, link.intf2.node.name))
            links.append((link.intf2.node.name, link.intf1.node.name))

        networkGraph = Graph()
        networkGraph = networkGraph.as_directed()
        for node in node_names:
            networkGraph.add_vertex(node)
        for (a, b) in links:
            networkGraph.add_edges([(a, b), (b, a)])

        named_paths = []
        for from_node in node_names:
            for to_node in node_names:
                if from_node != to_node:
                    paths = networkGraph.get_all_shortest_paths(from_node, to_node)
                    if len(paths) == 0:
                        continue
                    shortest_path = paths[0]
                    shortest_path_with_nodenames = []
                    for node in shortest_path:
                        shortest_path_with_nodenames.append(networkGraph.vs['name'][node])
                    named_paths.append(shortest_path_with_nodenames)

        # Iterate over all paths and configure the routes using the 'route add'
        info('Configure routes on all nodes\n')
        for path in named_paths:
            start_node = path[0]
            end_node = path[-1]
            mini_start = net.get(start_node)
            mini_end = net.get(end_node)

            link_info = IPRoutingHelper.findLinkInformation(mini_links, path[0], path[1])
            start_intf = link_info.start_intf_name

            for intf in mini_end.intfs:
                addr = mini_end.intfs[intf].ip
                if len(path) == 2:
                    # For direct connection, configure exit interface
                    info('[{}] route add -host {} dev {}\n'.format(start_node, addr, start_intf))
                    mini_start.cmd('route add -host {} dev {}'.format(addr, start_intf))
                elif len(path) > 2:
                    # For longer paths, configure next hop as gateway
                    gateway_ip = link_info.end_ip
                    info('[{}] route add -host {} dev {} gw {}\n'
                         .format(start_node, addr, start_intf, gateway_ip))
                    mini_start.cmd('route add -host {} dev {} gw {}'
                                   .format(addr, start_intf, gateway_ip))
Ejemplo n.º 33
0
''' leitura dos arquivos e criacao do dataframe '''

f = os.listdir(rd + "/grafosMes")  # lista com os nomes dos grafos
f = ordenaNomesArquivos(f)  # ordena os nomes cronologicamente

for arquivo in f:  # percorre todos os grafos mensais em ordem
    # guarda o ano do grafo
    A = substringEntreChars(arquivo, '-', '.')
    dados['ano'].append(A)

    # guarda o mes do grafo
    M = substringEntreChars(arquivo, '_', '-')
    dados['mes'].append(M)  # guarda mes do grafo

    # abre o grafo
    g = Graph.Read_GML(rd + "/grafosMes/" + arquivo)

    # calculo e armazenamento do grau medio
    listaDado = g.degree()
    dado = somaLista(listaDado) / len(listaDado)
    dados['mean_degree'].append(dado)

    # calculo e armazenamento da betweenness media
    listaDado = g.betweenness()
    dado = somaLista(listaDado) / len(listaDado)
    dados['mean_betweenness'].append(dado)

    # calculo e armazenamento da closeness media
    listaDado = g.closeness()
    dado = somaLista(listaDado) / len(listaDado)
    dados['mean_closeness'].append(dado)
Ejemplo n.º 34
0
        in_edges += subgraph.ecount()

    print(f"fraction: {in_edges / graph.ecount()}")
    print("Degree Distribution: ")
    print(graph.degree_distribution())
    return parts.modularity


def desc_learners(learners: List[Learner]):
    result = list()

    for learner in learners:
        result.append(
            [
                [round(val, 4) for val in learner._rows],
                [round(val, 4) for val in learner._cols],
            ]
        )

    return json.dumps(result)


if __name__ == '__main__':

    graph_name = "4_50_1_10_0.9"
    test_graph = Graph.Read_GML(f"../data/gaussian/{graph_name}.gml")
    desc(test_graph)
    # test_graph.name = graph_name
    #
    # get_edges(test_graph, 1, fast_resistance, 1000, 1000)
Ejemplo n.º 35
0
def group_union(g_a_links, g_b_links):
    """
    Synonym groups are modelled by sets of couples in the DB

    Input : 2 arrays of links (ngramx_id, ngramy_id)
    Input : 1 array of links (ngramx_id, ngramy_id)

    Synonymity is considered transitive so in effect the groups
    can form a set (defined by the connected component of couples).

     A requested feature is also that one node dominates others
     (aka "leader effect"; leader will be in the map, the others won't)

    Summary of major union effects in various cases:

    GROUP 1         Group 2         Group 1 ∪ 2

    A -> B           A -> C           A -> B       (simple union)
                                      A -> C

    D -> E           E -> F           D -> E
                                      D -> F       (D "leader effect")


    G -> H           G -> I           G -> H       ( transitivity +
                     H -> J           G -> I        "leader effect")
                                      G -> J

     rloth: this is some slightly amended code
     from Samuel's in rest_v1_0.ngrams.Group.get

     TODO use "most frequent" score if leader candidates are ex aequo by degree.
    """

    # output: list of links forming new group
    new_links = []

    # 1) create graph with both lists
    # -------------------------------

    # from igraph import Graph

    # the set of all our ngram_ids
    all_vertices = set(
        [ngid for couple in g_a_links + g_b_links for ngid in couple])

    # initialize the synonym graph with size
    sg = Graph(len(all_vertices), directed=True)

    # add our IDs as "name" (special attribute good for edge creation)
    sg.vs['name'] = [str(x) for x in all_vertices]

    # add the edges as named couples
    sg.add_edges([(str(x), str(y)) for (x, y) in g_a_links])

    #print('UNION A:', g_a_links)
    #print('initially %i components' % len(sg.as_undirected().components()))

    # same with the other edges
    sg.add_edges([(str(x), str(y)) for (x, y) in g_b_links])

    #print('UNION B:', g_b_links)
    #print('after union %i components' % len(sg.as_undirected().components()))

    # 2) list resulting components
    # -----------------------------
    synonym_components = sg.as_undirected().components()

    # for example
    # cs = [[0, 3, 6], [1, 2, 8], [4, 5, 9, 11], [7,10]]

    # there should be no singletons by construction

    # list of all outdegrees for "leader" detection
    # (leader = term most often marked as source by the users)
    odegs = sg.outdegree()

    #for i, v in enumerate(sg.vs):
    #    print("%i - name:%s - odeg:%i" % (i, v['name'], odegs[i]))

    for component in synonym_components:
        # we map back to our ids, preserving order
        our_comp = [int(our_id) for our_id in sg.vs[component]['name']]

        # 3) take main node and unnest into new links list
        # -------------------------------------------------

        # position (within this component) of the best node (by degree)
        max_odeg = -1
        main_node_local_index = None
        for position, vertex_id in enumerate(component):
            this_odeg = odegs[vertex_id]
            if this_odeg > max_odeg:
                main_node_local_index = position
                max_odeg = this_odeg

        # we set it aside in our translated version our_comp
        main_node = our_comp.pop(main_node_local_index)

        # and unnest the others
        for remaining_id in our_comp:
            new_links.append((main_node, remaining_id))

    return new_links
Ejemplo n.º 36
0
def plotLatticeGraph_colorGroups(
    inputTuples,
    name_mapping,
    different_colors_group,
    metric="",
    color_map={},
    annotation_F=True,
    sizeDot="",
    useMarker=True,
    show=False,
    font_size_div=10,
    font_size_hover_labels=10,
    showTitle=False,
    round_v=3,
    width=None,
    height=None,
    showGrid=True,
    plot_bgcolor="rgb(248,248,248)",
    displayItemsetLabels=False,
    font_size_ItemsetLabels=10,
):

    from igraph import Graph, EdgeSeq

    G = Graph.TupleList([(k, v) for k, vs in inputTuples.items() for v in vs])

    lay = G.layout("rt", root=[0])

    nr_vertices = G.vcount()
    position = {k: lay[k] for k in range(nr_vertices)}
    Y = [lay[k][1] for k in range(nr_vertices)]
    M = max(Y)

    E = [e.tuple for e in G.es()]  # list of edges

    labels = G.vs()["name"]

    groups = {}
    groups_labels = {}
    X_group = {}
    Y_group = {}
    if useMarker:
        markers_type = {
            "normal": "circle-dot",
            "lower": "diamond",
            "greater": "square",
            "all_greater": "hexagon",
        }
    else:
        markers_type = {k: "circle-dot" for k in different_colors_group}
    colors = ["#6175c1", "#ff6666", "#008000", "#FFC0CB"]  # todo
    setColorMap = False if color_map != {} else True
    counter_c = 0
    for group_i in different_colors_group:
        different_color = different_colors_group[group_i]
        groups[group_i] = [
            i for i in range(0, len(labels)) if labels[i] in different_color
        ]
        groups_labels[group_i] = [
            labels[i] for i in range(0, len(labels))
            if labels[i] in different_color
        ]
        X_group[group_i] = [position[k][0] for k in groups[group_i]]
        Y_group[group_i] = [2 * M - position[k][1] for k in groups[group_i]]
        if setColorMap:
            color_map[group_i] = colors[counter_c]
            counter_c = counter_c + 1

    Xe = []
    Ye = []
    for edge in E:
        Xe += [position[edge[0]][0], position[edge[1]][0], None]
        Ye += [
            2 * M - position[edge[0]][1], 2 * M - position[edge[1]][1], None
        ]

    sizeDot = 10 if sizeDot == "small" else 18

    import plotly.graph_objects as go

    fig = go.Figure()

    fig.add_trace(
        go.Scatter(
            x=Xe,
            y=Ye,
            mode="lines",
            line=dict(color="rgb(210,210,210)", width=1),
            hoverinfo="none",
        ))
    for group_i in different_colors_group:
        fig.add_trace(
            go.Scatter(
                x=X_group[group_i],
                y=Y_group[group_i],
                mode="markers",
                name=metric,
                marker=dict(
                    symbol=markers_type[group_i],
                    size=sizeDot,
                    color=color_map[group_i],  #'#DB4551',
                    line=dict(color="rgb(50,50,50)", width=1),
                ),
                text=orderedNameMapping(groups_labels[group_i], name_mapping)
                if annotation_F else groups_labels[group_i],
                hoverinfo="text",
                opacity=0.8,
                hoverlabel=dict(font_size=font_size_hover_labels),
            ))

    if annotation_F:
        labels_text = [str(round(name_mapping[l], round_v)) for l in labels]

        axis = dict(
            showline=False,  # hide axis line, grid, ticklabels and  title
            zeroline=False,
            showgrid=showGrid,
            showticklabels=False,
        )

        def make_annotations(pos,
                             labels_text,
                             font_size=10,
                             font_color="rgb(0,0,0)"):
            L = len(pos)
            if len(labels_text) != L:
                raise ValueError(
                    "The lists pos and text must have the same len")
            annotations = []
            for k in range(L):
                annotations.append(
                    dict(
                        text=labels_text[
                            k],  # or replace labels with a different list for the text within the circle
                        x=pos[k][0],
                        # y=2 * M - position[k][1] + 0.05 * (2 * M - position[k][1]),
                        y=2 * M - position[k][1] + 0.03 * (2 * M),
                        xref="x1",
                        yref="y1",
                        font=dict(color=font_color, size=font_size),
                        showarrow=False,
                    ))
            return annotations

        fig.update_layout(
            title=metric if showTitle else None,  # TODO - TMP
            annotations=make_annotations(position,
                                         labels_text,
                                         font_size=font_size_div),
            font_size=10,
            showlegend=False,
            xaxis=axis,
            yaxis=axis,
            margin=dict(l=0, r=0, b=0, t=20)
            if showTitle else dict(l=0, r=0, b=0, t=0),
            hovermode="closest",
            plot_bgcolor=plot_bgcolor,
            width=width,
            height=height,
        )

    if displayItemsetLabels:
        max_len = max([len(i) for i in name_mapping.keys()])
        X_range = [abs(lay[k][0]) for k in range(nr_vertices)]
        X_range = max(X_range) - (min(X_range))
        order_mapping = {
            v: id_v
            for id_v, v in enumerate(name_mapping) if len(v) in [1, max_len]
        }
        for group_i, a in groups_labels.items():
            for i, itemset in enumerate(a):
                if len(itemset) not in [1, max_len]:
                    continue
                p = (X_group[group_i][i], Y_group[group_i][i])
                get_x_pos = lambda pos_x, pad: pos_x - pad * X_range
                get_y_pos = lambda pos_y, pad: pos_y + pad * pos_y

                p_ref_x = 0.2 if order_mapping[itemset] % 2 == 0 else 0.25
                p_ref_y = -0.045
                get_name = lambda v: ", ".join(sorted(list(v)))

                fig.add_annotation(
                    x=p[0],
                    y=p[1],
                    xref="x",
                    yref="y",
                    text=get_name(itemset),
                    align="left",
                    axref="x",
                    ayref="y",
                    ax=get_x_pos(
                        p[0], p_ref_x + 0.01 * (font_size_ItemsetLabels - 10))
                    if len(itemset) == 1 else get_x_pos(p[0], -0.7),
                    ay=get_y_pos(p[1],
                                 p_ref_y if len(itemset) == 1 else -0.03),
                    showarrow=True,
                    font=dict(
                        # family="Courier New, monospace",
                        size=font_size_ItemsetLabels,
                        color="black",
                    ),
                    bordercolor="white",
                    borderwidth=1,
                    borderpad=4,
                    bgcolor=color_map[group_i],
                    opacity=0.8,
                    # yanchor="middle"
                )

    if show:
        fig.show()
    # TMP
    return fig
Ejemplo n.º 37
0
au0=ROC('au0')
ag0=ROC('ag0')
cu0=ROC('cu0')

total=a0.join([y0,m0,oi0,rm0,p0,cs0,c0,ap0,sr0,cf0,jd0,ru0,rb0,hc0,i0,j0,jm0,zc0,fg0,sf0,sm0,l0,v0,pp0,ta0,ma0,al0,zn0,au0,ag0,cu0],how='inner').dropna(axis=0)

corr=total.corr()

sns.heatmap(corr,xticklabels=1,yticklabels=1)
plt.show()

A=corr.values
g=igraph.Graph.Adjacency((A>0).tolist())
g.es['weight'] = A[A.nonzero()]
g.vs['label'] = corr.columns
cluster=IGraph.community_walktrap(g, g.es['weight'])

clusters = IGraph.community_walktrap(g, g.es['weight'],steps=10000).as_clustering(8)

nodes = [{"label": node["label"]} for node in g.vs]
community = {}

for node in nodes:
    idx = g.vs.find(label=node["label"]).index
    node["community"] = clusters.membership[idx]
    if node["community"] not in community:
        community[node["community"]] = [node["label"]]
    else:
        community[node["community"]].append(node["label"])
for c,l in community.items():
    print("Community ", c, ": ", l)
Ejemplo n.º 38
0
"""
The following error says that modularity calculation for directed graph is not implemented
----
File "/home/mridul/playground/citation-club/env/lib/python3.8/site-packages/igraph/__init__.py", line 898, in modularity
    return GraphBase.modularity(self, membership.membership, weights)
igraph._igraph.InternalError: Error at ../../../source/igraph/src/community.c:921: modularity is implemented for undirected graphs, Invalid value
----
So we convert our citation network as undirected graph and calculate modularity
"""
import sys
import pickle
from igraph import Graph

try:
    journal = sys.argv[1]
except IndexError:
    #journal = "prl"
    print("usage: python3", sys.argv[0], "[jmlr|prl]")
    exit(0)

g = Graph.Read_Ncol("data/" + journal + "/el_cit", directed=True)
lcc = g.components(mode="WEAK").giant()
vd = lcc.community_walktrap()
cit = vd.as_clustering()
lcc.to_undirected()
# cit = pickle.load(open("results/experiment1/"+journal+".communities_cit.bin", 'rb'))
print("cit net modularity : ", lcc.modularity(cit))
Ejemplo n.º 39
0
os.makedirs(plot_dir, exist_ok=True)

#%%
# =============================================================================
#  Build class networks
# =============================================================================
class_files = [
    'ClassNetPlatoonU.graphml',
    'ClassNetPlatoonM.graphml',
    'ClassNetPlatoonT.graphml',
    'ClassNetPlatoonW.graphml',
    'ClassNetPlatoonR.graphml',
    'ClassNetPlatoonF.graphml',
    'ClassNetPlatoonS.graphml',
]
graphs = [Graph.Read_GraphML(os.path.join(net_dir, cf)) for cf in class_files]

class_layer_names = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
class_contacts = bu.get_all_class_contacts_dicts(graphs, class_layer_names, 3)

#%%
# =============================================================================
#  Build housing networks
# =============================================================================
h_files = ['RoomNet.graphml', 'HouseholdNet_10.graphml', 'FloorNet.graphml']
hgraphs = [Graph.Read_GraphML(os.path.join(hnet_dir, hf)) for hf in h_files]

hlayer_names = ['roommate', 'household', 'floor']
household_contacts = bu.get_all_housing_contacts_dict(hgraphs, hlayer_names)

#%%
Ejemplo n.º 40
0
# Lásd. halozatok_segedlet.tex

from igraph import Graph, summary
from igraph import plot as iplot
# iplot néven, hogy ne keveredjen a pylab.plot függvénnyel

# Programfájlban kellene még ez a sor is a -pylab opció helyett:
 from pylab import plot, average, array, grid, xlabel, ylabel, legend, show
# vagy egyszerűen
 from pylab import *
# és minden plot függvény után kellene
# show() függvény.
 import pylab
# esetén pedig pylab.függvény() alakban hívhatóak a pylab függvényei.

net = Graph.Erdos_Renyi(1000, .001)

summary(net)

M=net.ecount()
N = net.vcount()
Mmax = N*(N-1)/2
Mmax
M/Mmax
p = 1.*M/Mmax

net.diameter()

net.components()
cc=net.components()  # (összefüggő) komponensek, (connected) components
ccs = cc.sizes()
Ejemplo n.º 41
0
import os
import igraph
from igraph import Graph
print igraph

INFILE = "new_graph.net"
OUTFILE_SVG = "new_graph.svg"
OUTFILE_PNG = "new_graph.png"

#g = Graph.Load("output/SimpleModuleGraphALL.net")
g = Graph.Load(INFILE)
#g = igraph.load("output/SimpleModuleGraph.net")

#print g.es
#print g.es["weight"]
#print g.cliques()

print len(g.vs)
# delete edges that have low weight
to_del = []
#for e in g.es:
#  print e
#  if(e['weight'] < 0):
#    to_del.append(e.index)

#g.delete_edges(to_del)
g.simplify()

g1 = g.subgraph(g.vs.select(_degree_gt=0))
print len(g1.vs)
Ejemplo n.º 42
0
from igraph import Graph
from igraph import plot

# Criação de um gráfico direcionado com pesos entre as arestas
grafo = Graph(edges=[(0, 2), (0, 1), (1, 4), (1, 5), (2, 3), (6, 7), (3, 7),
                     (4, 7), (5, 6)],
              directed=True)
grafo.vs['label'] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
grafo.es['weight'] = [2, 1, 2, 1, 2, 1, 3, 1]

# Visualização dos resultados
plot(grafo, bbox=(0, 0, 300, 300), edge_label=grafo.es['weight'])

# Menor caminho entre A - H (retorna os vértices)
caminho_vertice = grafo.get_shortest_paths(0, 7,
                                           output='vpath')  #vpath vértices
caminho_vertice

# Retorna as arestas que ligam os dois vértices
caminho_aresta = grafo.get_shortest_paths(0, 7, output='epath')  #epath arestas
caminho_aresta

# Mostra o ID dos vértices que fazem parte do caminho
caminho_aresta_id = []
for n in caminho_aresta[0]:
    caminho_aresta_id.append(n)
caminho_aresta_id

# Mostra o nome dos vértices que fazem parte do caminho
caminho_nome_vertices = []
for n in caminho_vertice[0]:
Ejemplo n.º 43
0
class BaseNetwork(metaclass=abc.ABCMeta):

    NAME = 'Base Network'
    CODE = 'base_network'
    DIRECTED = False

    # CHANGE NAME
    NODE_METRICS_TO_PLOT = {
        'Tenure in the wiki': {
            'key': 'birth_value',
            'max': 'max_birth_value',
            'min': 'min_birth_value'
        },
        'Degree': {
            'key': 'degree',
            'max': 'max_degree',
            'min': 'min_degree'
        },
        'In-degree': {
            'key': 'indegree',
            'max': 'max_indegree',
            'min': 'min_indegree'
        },
        'Out-degree': {
            'key': 'outdegree',
            'max': 'max_outdegree',
            'min': 'min_outdegree'
        },
        'Closeness': {
            'key': 'closeness',
            'max': 'max_closeness',
            'min': 'min_closeness'
        },
        'Betweenness': {
            'key': 'betweenness',
            'max': 'max_betweenness',
            'min': 'min_betweenness'
        },
        'Page rank': {
            'key': 'page_rank',
            'max': 'max_page_rank',
            'min': 'min_page_rank'
        }
    }

    EDGE_METRICS_TO_PLOT = {
        'Weight': {
            'key': 'weight',
            'min': 'min_weight',
            'max': 'max_weight',
        }
    }

    NETWORK_STATS = {
        'Nodes': 'num_nodes',
        'Edges': 'num_edges',
        'Connected comp.': 'components',
        'Clusters': 'n_communities',
        'Density': 'density',
        'Assortativity': 'assortativity_degree',
        'Gini of betwee.': 'gini_betweenness',
        'Gini of close.': 'gini_closeness',
        'Gini of deg.': 'gini_degree',
        'Gini of in-deg.': 'gini_indegree',
        'Gini of out-deg.': 'gini_outdegree'
    }

    def __init__(self, is_directed=False, graph={}, alias=''):
        if not graph:
            self.graph = Graph(directed=is_directed)
        else:
            self.graph = graph

        self.alias = alias

    @abc.abstractmethod
    def generate_from_pandas(self, df: pd.DataFrame):
        """
        Fill the igraph attribute from pandas data

        Parameters:
            -df: A pandas object with the wiki info (coming from the csv file),
                   must be ordered by timestamp
        """
        pass

    @abc.abstractmethod
    def get_metric_dataframe(self, metric: str) -> pd.DataFrame:
        """
        This function generates a dateframe with 2 cols, the node name
        and a node metric value.
        Prarameters:
            - metric: an existing metric in the network
        Return:
            if metric exist a dataframe, if not None 
        """
        pass

    @abc.abstractclassmethod
    def get_metric_header(self, metric: str) -> list:
        """
        Returns a list with the header keys of the function get_metric_dataframe
        """
        pass

    @abc.abstractclassmethod
    def get_user_info(self) -> dict:
        """
        Return a dict with the user info
        """
        pass

    @abc.abstractclassmethod
    def get_node_name(self) -> dict:
        pass

    @abc.abstractclassmethod
    def is_directed(cls) -> bool:
        pass

    @abc.abstractclassmethod
    def get_network_stats(cls) -> dict:
        pass

    @abc.abstractclassmethod
    def get_metrics_to_plot(cls) -> dict:
        """
        This is used to clean NODE_METRICS_TO_PLOT
        """
        pass

    @abc.abstractclassmethod
    def get_metrics_to_show(cls) -> dict:
        """
        This is used to clean and prepare NODE_METRICS_TO_PLOT in order to show the data
        """
        pass

    @abc.abstractclassmethod
    def get_node_metrics(cls):
        pass

    @classmethod
    def remove_non_directed_node_metrics(cls, metrics):
        if 'Degree' in metrics:
            del metrics['Degree']

    @classmethod
    def remove_directed_node_metrics(cls, metrics):
        if 'In-degree' in metrics:
            del metrics['In-degree']
        if 'Out-degree' in metrics:
            del metrics['Out-degree']

    def add_graph_attrs(self):
        """
        Calculates and adds the graph attrs
        """
        self.graph['num_nodes'] = self.graph.vcount()
        self.graph['num_edges'] = self.graph.ecount()

    def to_cytoscape_dict(self) -> dict:
        """
        Transform a network to cytoscape dict

        Return:
            A dict with the cytoscape structure, graph attrs are keys, 
            and the cyto. elements are in the key 'network'
        """
        di_net = {}
        network = []
        metrics_to_plot = [
            val for key, val in self.NODE_METRICS_TO_PLOT.items()
        ]
        metrics_to_plot = metrics_to_plot + [
            val for key, val in self.EDGE_METRICS_TO_PLOT.items()
        ]
        log_keys = {
            metric['key']
            for metric in metrics_to_plot if 'log' in metric.keys()
        }

        # node attrs
        for node in self.graph.vs:
            data = {'data': {}}
            for attr in self.graph.vs.attributes():
                val = node[attr]
                if attr in log_keys:
                    data['data'][f'{attr}_log'] = int(log(val) * 100)

                data['data'][attr] = val

            network.append(data)

        # edge attrs
        for edge in self.graph.es:
            data = {'data': {}}
            for attr in self.graph.es.attributes():
                val = edge[attr]
                if attr == 'id':
                    continue
                if attr in log_keys:
                    data['data'][f'{attr}_log'] = int(log(val) * 100)

                data['data'][attr] = val
            network.append(data)

        # graph attrs
        for attr in self.graph.attributes():
            di_net[attr] = self.graph[attr]

        # add max min metrics to plot
        _max = 0
        _min = 0
        for metric in metrics_to_plot:
            if metric['key'] in self.graph.vs.attributes() and len(
                    self.graph.vs[metric['key']]):
                _max = max(self.graph.vs[metric['key']])
                _min = min(self.graph.vs[metric['key']])
            elif metric['key'] in self.graph.es.attributes() and len(
                    self.graph.es[metric['key']]):
                _max = max(self.graph.es[metric['key']])
                _min = min(self.graph.es[metric['key']])

            if 'log' in metric.keys():
                _max = int(log(_max) * 100)
                _min = int(log(_min) * 100)

            di_net[metric['max']] = _max
            di_net[metric['min']] = _min

        di_net['network'] = network
        return di_net

    def write_gml(self, file: str):
        """
        Writes a gml file
        Parameters:
            file: path to save
        """
        self.graph.write(f=file, format='gml')

    def calculate_page_rank(self):
        """
        Calculates the network pageRank
        """
        if not 'page_rank' in self.graph.vs.attributes():
            weight = 'weight' if 'weight' in self.graph.es.attributes(
            ) else None
            p_r = self.graph.pagerank(directed=self.graph.is_directed(),
                                      weights=weight)

            self.graph.vs['page_rank'] = list(
                map(lambda x: float(f"{x:.4f}"), p_r))

    def calculate_betweenness(self):
        """
        Calculates the network betweenness
        """
        if not 'betweenness' in self.graph.vs.attributes():
            weight = 'weight' if 'weight' in self.graph.es.attributes(
            ) else None
            bet = self.graph.betweenness(directed=self.graph.is_directed(),
                                         weights=weight)

            self.graph.vs['betweenness'] = list(
                map(lambda x: float(f"{x:.4f}"), bet))

    def calculate_gini_betweenness(self):
        if 'betweenness' in self.graph.vs.attributes() and 'gini_betweenness'\
            not in self.graph.attributes():
            gini = ineq.gini_corrected(self.graph.vs['betweenness'])
            value = 'nan'
            if gini is not np.nan:
                value = f"{gini:.2f}"

            self.graph['gini_betweenness'] = value

    def calculate_degree(self):
        if self.graph.is_directed() and 'indegree' not in self.graph.vs:
            self.graph.vs['indegree'] = self.graph.indegree()
            self.graph.vs['outdegree'] = self.graph.outdegree()
        elif 'degree' not in self.graph.vs:
            self.graph.vs['degree'] = self.graph.degree()

    def calculate_gini_degree(self):
        if self.graph.is_directed() and 'gini_indegree' not in\
            self.graph.attributes():

            gini = ineq.gini_corrected(self.graph.vs['indegree'])
            value = 'nan'
            if gini is not np.nan:
                value = value = f"{gini:.2f}"
            self.graph['gini_indegree'] = value
            gini = ineq.gini_corrected(self.graph.vs['outdegree'])
            value = 'nan'
            if gini is not np.nan:
                value = value = f"{gini:.2f}"
            self.graph['gini_outdegree'] = value

        elif 'gini_degree' not in self.graph.attributes():
            gini = ineq.gini_corrected(self.graph.vs['degree'])
            value = 'nan'
            if gini is not np.nan:
                value = value = f"{gini:.2f}"
            self.graph['gini_degree'] = value

    def calculate_communities(self):
        """
        Calculates communities and assigns a color per community
        """
        if not 'n_communities' in self.graph.attributes():
            weight = 'weight' if 'weight' in self.graph.es.attributes(
            ) else None
            # igraph bug: https://github.com/igraph/python-igraph/issues/17
            try:
                v_d = self.graph.community_walktrap(weights=weight, steps=6)
                mod = v_d.as_clustering()
            except:
                fix_dendrogram(self.graph, v_d)
                mod = v_d.as_clustering()

            self.graph.vs['cluster'] = mod.membership
            self.graph['n_communities'] = len(mod)
            pal = ClusterColoringPalette(len(mod))
            self.graph.vs['cluster_color'] = list(map(lambda x: rgb2hex(x[0],x[1],x[2],\
                normalised=True), pal.get_many(mod.membership)))

    def calculate_assortativity_degree(self):
        """
        Calculates the assortativity degree and put it as an graph attrb.
        * Reference:
            Newman MEJ: Assortative mixing in networks, Phys Rev Lett89:208701, 
            2002.@see:assortativity_degree()when thetypes are the vertex degrees
        """
        if not 'assortativity_degree' in self.graph.attributes():
            assortativity = self.graph.assortativity_degree(
                self.graph.is_directed())
            if assortativity:
                assortativity = f"{assortativity:.2f}"

            self.graph['assortativity_degree'] = assortativity

    def calculate_density(self):
        if not 'density' in self.graph.attributes():
            density = f'{self.graph.density():.2f}'
            self.graph['density'] = density

    def calculate_components(self):
        if not 'components' in self.graph.attributes():
            components = self.graph.clusters(mode=WEAK)
            self.graph['components'] = len(components.subgraphs())

    def calculate_closeness(self):
        if 'closeness' not in self.graph.vs.attributes() and 'weight'\
            in self.graph.es.attributes():

            rounder = lambda x: float(f"{x:.4f}")
            closeness = self.graph.closeness(weights='weight')
            closeness = list(map(rounder, closeness))
            self.graph.vs['closeness'] = closeness

    def calculate_gini_closeness(self):
        if 'closeness' in self.graph.vs.attributes() and 'gini_closeness'\
            not in self.graph.attributes():

            gini = ineq.gini_corrected(self.graph.vs['closeness'])
            if gini is not np.nan:
                self.graph['gini_closeness'] = f"{gini:.2f}"
        else:
            self.graph['gini_closeness'] = 'nan'

    def calculate_gini_article_edits(self):
        if 'article_edits' in self.graph.vs.attributes() and 'gini_article_edits'\
            not in self.graph.attributes():

            gini = ineq.gini_corrected(self.graph.vs['article_edits'])
            if gini is not np.nan:
                self.graph['gini_article_edits'] = f"{gini:.2f}"
        else:
            self.graph['gini_article_edits'] = 'nan'

    def calculate_gini_talk_edits(self):
        if 'talk_edits' in self.graph.vs.attributes() and 'gini_talk_edits'\
            not in self.graph.attributes():

            gini = ineq.gini_corrected(self.graph.vs['talk_edits'])
            if gini is not np.nan:
                self.graph['gini_talk_edits'] = f"{gini:.2f}"
        else:
            self.graph['gini_talk_edits'] = 'nan'

    def calculate_gini_user_talk_edits(self):
        if 'user_talks' in self.graph.vs.attributes() and 'gini_user_talks'\
            not in self.graph.attributes():

            gini = ineq.gini_corrected(self.graph.vs['user_talks'])
            if gini is not np.nan:
                self.graph['gini_user_talks'] = f"{gini:.2f}"
        else:
            self.graph['gini_user_talks'] = 'nan'

    def calculate_metrics(self):
        """
        A method which calculate the available metrics 
        """
        self.calculate_page_rank()
        self.calculate_betweenness()
        self.calculate_gini_betweenness()
        self.calculate_degree()
        self.calculate_gini_degree()
        self.calculate_assortativity_degree()
        self.calculate_communities()
        self.calculate_density()
        self.calculate_components()
        self.calculate_closeness()
        self.calculate_gini_closeness()
        self.calculate_gini_article_edits()
        self.calculate_gini_talk_edits()
        self.calculate_gini_user_talk_edits()

    def get_degree_distribution(self) -> (list, list):
        """
        Returns the degree distribution:
            if its directed is the sum of out and in degree
            if not its the out degree
        """

        degree = self.graph.degree()
        max_degree = self.graph.maxdegree()

        # Let's count the number of each degree
        p_k = [0 for i in range(0, max_degree + 1)]
        for x in degree:
            p_k[x] += 1

        # Now, we are gonna clean the 0's
        k = [i for i in range(0, max_degree + 1) if p_k[i] > 0]
        p_k = list(filter(lambda x: x > 0, p_k))

        return (k, p_k)

    def add_others(self, df):
        """
        It's used to add specific things (e.g. other metrics)
        """
        pass

    def build_network(self, df: pd.DataFrame, lower_bound: str,
                      upper_bound: str):
        """
        This method is used to generate the network and its metrics and attrs
        """
        dff = self.filter_by_time(df, lower_bound, upper_bound)
        dff = self.filter_anonymous(dff)
        self.generate_from_pandas(dff)
        self.calculate_metrics()
        self.calculate_abs_longevity(df)
        self.add_others(dff)
        self.add_graph_attrs()

    def filter_by_time(self,
                       df: pd.DataFrame,
                       lower_bound='',
                       upper_bound='') -> pd.DataFrame:

        dff = df
        if lower_bound and upper_bound:
            dff = dff[lower_bound <= dff['timestamp']]
            dff = dff[dff['timestamp'] <= upper_bound]

        return dff

    def filter_anonymous(self, df: pd.DataFrame) -> pd.DataFrame:
        dff = df
        if not dff.empty:
            dff = dff['Anonymous' != dff['contributor_name']]

        return dff

    def remove_non_article_data(self, df: pd.DataFrame) -> pd.DataFrame:
        """
          Filter out all edits made on non-article pages.

          df -- data to be filtered.
          Return a dataframe derived from the original but with all the
             editions made in non-article pages removed
       """
        # namespace 0 => wiki article
        return df[df['page_ns'] == 0]

    def remove_non_talk_data(self, df: pd.DataFrame) -> pd.DataFrame:
        """
          Filter out all edits made on non-talk pages.

          df -- data to be filtered.
          Return a dataframe derived from the original but with all the
             editions made in non-talk pages removed
       """
        # namespace 1 => wiki talk pages
        return df[df['page_ns'] == 1]

    def remove_non_user_talk_data(self, df: pd.DataFrame) -> pd.DataFrame:
        """
          Filter out all edits made on non-user-talk pages.

          df -- data to be filtered.
          Return a dataframe derived from the original but with all the
             editions made in non-user-talk pages removed
        """
        # namespace 3 => wiki user talk pages
        return df[df['page_ns'] == 3]

    def calculate_edits(self, df: pd.DataFrame, type_e: str):
        """
        This function adds as a vertex attr the edits from other namespace
        type_e parameter only accept {talk, article, user_talk}
        """
        allowed_types = {'talk', 'article', 'user_talk'}
        if type_e not in allowed_types:
            raise Exception(
                f"Not allowed type: {type_e}, it must be {allowed_types}")

        if 'label' not in self.graph.vs.attributes():
            return

        key = 'edits'
        if type_e == 'talk':
            dff = self.remove_non_talk_data(df)
            key = f'{type_e}_{key}'
        elif type_e == 'article':
            dff = self.remove_non_article_data(df)
            key = f'{type_e}_{key}'
        elif type_e == 'user_talk':
            raise Exception(f'type: {type_e} is not implemented yet')
        else:
            raise Exception(f'type: {type_e} is not defined')

        mapper = {
            self.graph.vs[i]['label']: i
            for i in range(self.graph.vcount())
        }
        edits = [0 for i in range(self.graph.vcount())]
        pages = [set() for _ in range(self.graph.vcount())]
        for _, row in dff.iterrows():
            if row['contributor_name'] in mapper.keys():
                edits[mapper[row['contributor_name']]] += 1
                pages[mapper[row['contributor_name']]].add(int(row['page_id']))

        sizer = lambda x: len(x)
        pages = list(map(sizer, pages))
        self.graph.vs[f"{type_e}s"] = pages
        self.graph.vs[key] = edits

    def calculate_abs_longevity(self, df: pd.DataFrame):
        """
        Calculates the birth of all the vertex without filter_by_time 
        """
        inverter = lambda x: 1 / x * 1000
        max_date = 0
        for node in self.graph.vs:
            dff = df[node['label'] == df['contributor_name']]
            if not dff.empty:
                row = dff.iloc[0]
                node['birth'] = datetime.strftime(row['timestamp'], "%d/%b/%Y")
                node['birth_value'] = int(
                    datetime.strptime(str(row['timestamp']),
                                      "%Y-%m-%d %H:%M:%S").strftime('%s'))

                # this is a weak solution to avoid users with no activity
                if max_date < node['birth_value']:
                    max_date = node['birth_value']
            else:
                node['birth'] = 'Not available'
                node['birth_value'] = max_date

        if 'birth_value' in self.graph.vs.attributes():
            self.graph.vs['birth_value'] = list(
                map(inverter, self.graph.vs['birth_value']))
Ejemplo n.º 44
0
def igraph_from_pandas_edgelist(df, source, target, directed):

    edgelist = df[[source, target]].apply(tuple, axis=1).tolist()

    return Graph.TupleList(edgelist, directed=directed)
Ejemplo n.º 45
0
def check(sol, paths):
	graph = Graph.Adjacency(sol)
	graph = graph.as_directed()
	if not len(paths_from_to(graph, 0, len(sol)-1))==paths:
		print("ERROR")
Ejemplo n.º 46
0
 def get_cascade_size(self):
     return Graph.vcount(self.cascade_nx)
Ejemplo n.º 47
0
class FlatGraph(object):
    """An internal class for graph flattening."""
    def __init__(self, parent: CommonGraph, quiet: bool = False):
        """Construct a FlatGraph."""
        super(FlatGraph, self).__init__()
        if not isinstance(parent, CommonGraph):
            raise TypeError("FlatGraph constructor needs a CommonGraph "
                            "parent, received a %s." %
                            parent.__class__.__name__)

        self.g = None
        self.clusters = None
        self.outputDir = parent.outputDir
        self.vertices = dict()
        self.edges = set()
        self.weights = dict()

        # Step 1. make a copy of the graph without file-file nodes, to
        # find paths between files that go through apps.
        if not quiet:
            tprnt("\t\t\tStep 1: copy graph, excluding file-file nodes...")
            tprnt("\t\t\t\tCopy graph...")
        copy = parent.g.copy()  # type: Graph
        types = parent.g.vs['type']
        names = parent.g.vs['name']
        toBeRemoved = []
        namesRemoved = []
        if not quiet:
            tprnt("\t\t\t\tFind edges to delete...")
        for edge in copy.es:
            if types[edge.source] == "file" and \
                    types[edge.target] == "file":
                toBeRemoved.append(edge)
                namesRemoved.append((names[edge.source], names[edge.target]))

        if not quiet:
            tprnt("\t\t\t\tDelete edges...")
        copy.delete_edges(toBeRemoved)

        # Step 2. run an all-pairs shortest path algorithm.
        # Step 2. pick out file-file paths with no intermediary files.
        # Step 2. save this info in the form of an edge list.
        if not quiet:
            tprnt("\t\t\tStep 2: run an all-pairs shortest path "
                  "algorithm, remove file-file paths with intermediary "
                  "files and gather final file-file edges...")
            tprnt("\t\t\t\tCopy file nodes...")
        fileNodes = list(
            (copy.vs[i] for i, t in enumerate(types) if t == "file"))

        edges = set()
        # weights = dict()
        self.idgen = UniqueIdGenerator()

        fileNodeCount = len(fileNodes)
        if not quiet:
            tprnt("\t\t\t\tGet shortest paths for each of %d file nodes..." %
                  fileNodeCount)
        threshold = fileNodeCount / 100
        nodeI = 0
        lastNodePct = 0
        nodePct = 0
        for v in fileNodes:
            nodeI += 1
            if nodeI >= (threshold * nodePct):
                nodePct = int(nodeI / threshold)
                if nodePct >= lastNodePct + 5:
                    print("\t\t\t\t\t... (%d%% done)" % nodePct)
                    lastNodePct = nodePct

            # Get shortest paths.
            vPaths = copy.get_shortest_paths(v, to=fileNodes)

            # Remove unnecessary bits.
            delSet = set()
            for (idx, p) in enumerate(vPaths):
                if len(p) < 1:
                    continue

                # Ignore paths with intermediary files.
                for node in p[1:-1]:
                    if types[node] == "file":
                        delSet.add(idx)

            # Remove unsuitable paths.
            for i in sorted(list(delSet), reverse=True):
                del vPaths[i]
            del delSet

            # Save the shortest paths remaining as edges.
            for p in vPaths:
                if len(p) <= 1:
                    continue
                key = (self.idgen[names[p[0]]], self.idgen[names[p[-1]]])
                edges.add(key)
                # weights[key] = 1 / (len(p) - 1)

        # Add edges for removed names
        if not quiet:
            tprnt("\t\t\t\tRe-add file-file direct nodes into graph...")
        for (src, dest) in namesRemoved:
            edges.add((self.idgen[src], self.idgen[dest]))

        # Step 3. construct a graph with only file nodes.
        if not quiet:
            tprnt("\t\t\tStep 3: construct a graph with only file nodes...")
        edges = list(edges)
        self.g = Graph(edges)
        del edges
        # self.g.es["weight"] = list((weights[e] for e in edges))
        self.g.vs["name"] = self.idgen.values()

        # Steph 4. apply community information to the nodes.
        if not quiet:
            tprnt("\t\t\tStep 4: apply communities to flat graph...")
        applyCommunities(self, parent.clusters.membership, names)

    def plot(self, output: str = None):
        """Plot the graph and its communities to an output file."""

        # Get style options set for the base graph plot.
        vs = {}
        vs["vertex_size"] = 5
        vs["vertex_shape"] = "circle"
        vs["layout"] = self.g.layout("fr")
        vs["bbox"] = (2400, 1600)
        vs["margin"] = 20

        # Plot the base graph with colours based on the communities.
        vs["vertex_color"] = self.membership
        edge_widths = []
        for (s, d) in self.g.get_edgelist():
            if self.membership[s] == self.membership[d]:
                edge_widths.append(1)
            else:
                edge_widths.append(3)
        vs["edge_width"] = edge_widths

        # Only keep labels for community-bridging vertices.
        minimal_labels = list(self.g.vs["name"])
        for (idx, label) in enumerate(minimal_labels):
            for neighbour in self.g.neighbors(label):
                if self.membership[neighbour] != self.membership[idx]:
                    break
            else:
                minimal_labels[idx] = None

        vs["vertex_label"] = minimal_labels

        try:
            if output:
                path = self.outputDir + "/" + output + ".flat.svg"
                plot(self.clusters, path, **vs)
            else:
                plot(self.clusters, **vs)
        except (OSError) as e:
            print("Error while plotting to %s: %s " %
                  (self.outputDir + "/" + output + ".flat.svg", e))
from py2neo import Graph
from igraph import Graph as IGraph
graph = Graph()

query = '''
MATCH (c1:Character)-[r:INTERACTS]->(c2:Character)
RETURN c1.name, c2.name, r.weight AS weight
'''

ig = IGraph.TupleList(graph.run(query), weights=True)
clusters = IGraph.community_walktrap(ig, weights="weight").as_clustering()
Ejemplo n.º 49
0
def generate_net_bert(texts_sents,
                      bert_dir,
                      book_names,
                      sent_dir,
                      net_dir,
                      save_nets=True):
    nets = []
    for sents, book_name in zip(texts_sents, book_names):
        with open(os.path.join(sent_dir, book_name), 'w') as sent_file:
            for sent in sents:
                sent_file.write(' '.join(sent).replace('\n', ' ') + '\n')
        M = []

        # run BERT with the official code from the paper

        bert_cmd = [
            'python',
            'bert/extract_features.py',
            '--input_file=%s' % os.path.join(sent_dir, book_name),
            '--output_file=/tmp/out_bert.jsonl',
            '--vocab_file=%s/vocab.txt' % bert_dir,
            '--bert_config_file=%s/bert_config.json' % bert_dir,
            '--init_checkpoint=%s/bert_model.ckpt' % bert_dir,
            '--layers=-1',
            '--max_seq_length=128',
            '--batch_size=8',
        ]

        process = subprocess.Popen(bert_cmd, stdout=subprocess.PIPE)
        process.wait()

        # load BERT generated features
        with open('/tmp/out_bert.jsonl') as vectors_file:
            for line in vectors_file:
                feature_json = json.loads(line)
                M.append(feature_json['features'][0]['layers'][0]['values'])

        eucl = euclidean_distances(M)
        del M

        simi_m = 1. / (1. + eucl)

        k = 1
        while True:
            simi = simi_m.copy()
            to_remove = simi.shape[0] - (k + 1)

            for vec in simi:
                vec[vec.argsort()[:to_remove]] = 0

            g = Graph.Weighted_Adjacency(simi.tolist(),
                                         mode=ADJ_UNDIRECTED,
                                         loops=False)

            if g.is_connected():
                break
            k += 1

        del simi_m

        if save_nets:
            to_xnet(g, os.path.join(net_dir, book_name), names=False)
            #g.write_pajek(os.path.join(net_dir, 'net_' + book_name + '.net'))
        """
        # Code for loading saved networks and reuse part of the pipeline.
        net_dir = 'output_bert/community_multilevel/net_dir'
        g = Graph.Read_Pajek(os.path.join(net_dir, 'net_' + book_name + '.net'))
        """
        nets.append(g)

    return nets
Ejemplo n.º 50
0
class ArgumentSet(object):
    """
    An ``ArgumentSet`` is modeled as a dependency graph where vertices represent
    the components of an argument. A vertex corresponding to the conclusion
    of an argument *A* will **depend on** the premises and exceptions in *A*.

    The graph is built using the `igraph <http://igraph.org/>`_ library. This
    allows *attributes* to be associated with both vertices and edges.
    Attributes are represented as Python dictionaries where the key (which
    must be a string) is the name of the attribute and the value is the
    attribute itself. For more details, see the
    `igraph tutorial\
    <http://igraph.org/python/doc/tutorial/tutorial.html#setting-and-retrieving-attributes>`_.
    """
    def __init__(self):
        self.graph = Graph()
        self.graph.to_directed()
        self.arg_count = 1
        self.arguments = []

    def propset(self):
        """
        The set of :class:`PropLiteral`\ s represented by the vertices in
        the graph.

        Retrieving this set relies on the fact that :meth:`add_proposition`
        sets a value for the ``prop`` attribute in vertices created when a
        new proposition is added to the graph.
        """
        g = self.graph
        props = set()
        try:
            props = {p for p in g.vs['prop']}
        except KeyError:
            pass
        return props

    def add_proposition(self, proposition):
        """
        Add a proposition to a graph if it is not already present as a vertex.

        :param proposition: The proposition to be added to the graph.
        :type proposition: :class:`PropLiteral`
        :return: The graph vertex corresponding to the proposition.
        :rtype: :class:`Graph.Vertex`
        :raises TypeError: if the input is not a :class:`PropLiteral`.
        """
        if isinstance(proposition, PropLiteral):
            if proposition in self.propset():
                logging.debug("Proposition '{}' is already in graph".\
                              format(proposition))
            else:
                # add the proposition as a vertex attribute, recovered via the
                # key 'prop'
                self.graph.add_vertex(prop=proposition)
                logging.debug("Added proposition '{}' to graph".\
                              format(proposition))
            return self.graph.vs.select(prop=proposition)[0]

        else:
            raise TypeError('Input {} should be PropLiteral'.\
                            format(proposition))

    def add_argument(self, argument, arg_id=None):
        """
        Add an argument to the graph.

        :parameter argument: The argument to be added to the graph.
        :type argument: :class:`Argument`
        :parameter arg_id: The ID of the argument
        :type arg_id: str or None
        """
        g = self.graph
        if arg_id is not None:
            argument.arg_id = arg_id
        else:
            argument.arg_id = 'arg{}'.format(self.arg_count)
        self.arg_count += 1
        self.arguments.append(argument)

        # add the arg_id as a vertex attribute, recovered via the 'arg' key
        self.graph.add_vertex(arg=argument.arg_id)
        arg_v = g.vs.select(arg=argument.arg_id)[0]

        # add proposition vertices to the graph
        conclusion_v = self.add_proposition(argument.conclusion)
        self.add_proposition(argument.conclusion.negate())
        premise_vs =\
            [self.add_proposition(prop) for prop in sorted(argument.premises)]
        exception_vs =\
            [self.add_proposition(prop) for prop in sorted(argument.exceptions)]
        target_vs = premise_vs + exception_vs

        # add new edges to the graph
        edge_to_arg = [(conclusion_v.index, arg_v.index)]
        edges_from_arg = [(arg_v.index, target.index) for target in target_vs]
        g.add_edges(edge_to_arg + edges_from_arg)


    def get_arguments(self, proposition):
        """
        Find the arguments for a proposition in an *ArgumentSet*.

        :param proposition: The proposition to be checked.
        :type proposition: :class:`PropLiteral`
        :return: A list of the arguments pro the proposition
        :rtype: list(:class:`Argument`)

        :raises ValueError: if the input :class:`PropLiteral` isn't present\
        in the graph.
        """
        g = self.graph

        # index of vertex associated with the proposition
        vs = g.vs.select(prop=proposition)

        try:
            conc_v_index = vs[0].index
            # IDs of vertices reachable in one hop from the proposition's vertex
            target_IDs = [e.target for e in g.es.select(_source=conc_v_index)]

            # the vertices indexed by target_IDs
            out_vs = [g.vs[i] for i in target_IDs]

            arg_IDs = [v['arg'] for v in out_vs]
            args = [arg for arg in self.arguments if arg.arg_id in arg_IDs]
            return args
        except IndexError:
            raise ValueError("Proposition '{}' is not in the current graph".\
                             format(proposition))





    def draw(self, debug=False):
        """
        Visualise an :class:`ArgumentSet` as a labeled graph.

        :parameter debug: If :class:`True`, add the vertex index to the label.
        """
        g = self.graph

        # labels for nodes that are classed as propositions
        labels = g.vs['prop']

        # insert the labels for nodes that are classed as arguments
        for i in range(len(labels)):
            if g.vs['arg'][i] is not None:
                labels[i] = g.vs['arg'][i]

        if debug:
            d_labels = []
            for (i, label) in enumerate(labels):
                d_labels.append("{}\nv{}".format(label, g.vs[i].index))

            labels = d_labels

        g.vs['label'] = labels


        roots = [i for i in range(len(g.vs)) if g.indegree()[i] == 0]
        ALL = 3 # from igraph
        layout = g.layout_reingold_tilford(mode=ALL, root=roots)

        plot_style = {}
        plot_style['vertex_color'] = \
            ['lightblue' if x is None else 'pink' for x in g.vs['arg']]
        plot_style['vertex_size'] = 60
        plot_style['vertex_shape'] = \
            ['circle' if x is None else 'rect' for x in g.vs['arg']]
        plot_style['margin'] = 40
        plot_style['layout'] = layout
        plot(g, **plot_style)



    def write_to_graphviz(self, fname=None):
        g = self.graph
        result = "digraph G{ \n"

        for vertex in g.vs:
            arg_label = vertex.attributes()['arg']
            prop_label = vertex.attributes()['prop']

            if arg_label:
                dot_str = (arg_label +
                           ' [color="black", fillcolor="pink", width=.75, '
                           'shape=box, style="filled"]; \n')

            elif prop_label:
                dot_str = ('"{}"'.format(prop_label) +
                           ' [color="black", fillcolor="lightblue", '
                           'fixedsize=true, width=1  shape="circle", '
                           'style="filled"]; \n')
            result += dot_str

        for edge in g.es:
            source_label = g.vs[edge.source]['prop'] if\
                g.vs[edge.source]['prop'] else g.vs[edge.source]['arg']
            target_label = g.vs[edge.target]['prop'] if\
                g.vs[edge.target]['prop'] else g.vs[edge.target]['arg']
            result += '"{}" -> "{}"'.format(source_label, target_label)
            dot_str = " ; \n"
            result += dot_str

        result += "}"

        if fname is None:
            fname = 'graph.dot'
        with open(fname, 'w') as f:
            print(result, file=f)
Ejemplo n.º 51
0
from igraph import Configuration
from igraph import Graph
from igraph import plot
'''
Em caso de duvida de instalacao dos pacotes =  
https://github.com/cleano01/Curso-Livro-BigData-I.A-DeepLearning/commit/7513ff5e442b37aec8f2dbfb5dbb15d660f9cba6
'''

#configuracao para exebir o grafo
cfg = Configuration.instance()
cfg["apps.eog"] = "start"

#edges = arestas
#direct = TRUE  estamos informando que o grafo sera direcionado
grafo1 = Graph(edges=[(0, 1), (1, 2), (2, 3), (3, 0)], directed=True)

#vcount() = informa quantos vertices temos
grafo1.vs['label'] = range(grafo1.vcount())
#print(grafo1)
#plot(grafo1, bbox= (300, 300))

#edges = arestas
#direct = TRUE  estamos informando que o grafo sera direcionado
grafo2 = Graph(edges=[(0, 1), (1, 2), (2, 3), (3, 0), (0, 3), (3, 2), (2, 1),
                      (1, 0)],
               directed=True)

#vcount() = informa quantos vertices temos
grafo2.vs['label'] = range(grafo2.vcount())
#print(grafo2)
#plot(grafo2, bbox= (300, 300))
Ejemplo n.º 52
0
 def __init__(self):
     self.graph = Graph()
     self.graph.to_directed()
     self.arg_count = 1
     self.arguments = []
Ejemplo n.º 53
0
import numpy as np

from igraph import Graph, plot

# Создание вершин и ребер
vertices = [i for i in range(7)]
edges = [(0, 2), (0, 1), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1),
         (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (4, 5), (4, 6), (5, 4),
         (5, 6), (6, 4), (6, 5)]

# Создание графа
g = Graph(vertex_attrs={"label": vertices}, edges=edges, directed=False)

# Задаем стиль отображения графа
N = len(vertices)
visual_style = {}
visual_style["layout"] = g.layout_fruchterman_reingold(maxiter=1000,
                                                       area=N**3,
                                                       repulserad=N**3)

# Отрисовываем граф
plot(g, **visual_style)
Ejemplo n.º 54
0
            tags[t] = [row[1]]
        else:
            tags[t].append(row[1])

####
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.figure()

plt.hist([len(pts) for t, pts in tags5.items()], bins=1000)

plt.savefig
plt.xlabel('posts per tag')
plt.ylabel('number of tags')
plt.yscale('log')
plt.xscale('log')
plt.title('Post count per tag')
plt.savefig('/tmp/so/tags5_distrib.png')

####

edges = []

for t, pts in tags.items():
    for p in pts:
        edges.append(('t' + str(t), 'p' + str(p)))

from igraph import Graph
g = Graph.TupleList(edges)
Ejemplo n.º 55
0
    def calcAllRoutes(net):
        """ Configures IP routes between all nodes in the emulation topology. This is done in three
         steps:

        1) IP forwarding is enabled on all nodes
        2) The igraph lib is used to calculate all shortest paths between the nodes
        3) Route add commands are used to actually configure the ip routes

        :param net:
        """

        mini_nodes = net.hosts
        mini_links = net.links

        # Enabling IP forwaring on all nodes
        info('Configure IP forwarding on all nodes\n')
        for node in mini_nodes:
            node.cmd('sysctl -w net.ipv4.ip_forward=1')

        # Create the network graph to calculate all shortest paths between nodes
        node_names = [node.name for node in mini_nodes]
        links = []
        for link in mini_links:
            links.append((link.intf1.node.name, link.intf2.node.name))
            links.append((link.intf2.node.name, link.intf1.node.name))
        networkGraph = Graph()
        networkGraph = networkGraph.as_directed()
        for node in node_names:
            networkGraph.add_vertex(node)
        for (a, b) in links:
            networkGraph.add_edges([(a, b), (b, a)])

        existing_paths = {
        }  # existing_paths stores all paths that are installed
        shortest_paths = [
        ]  # List of calculated shorted paths betweeen all nodes

        # Calculate shortest paths between all nodes using libigraph
        for from_node in node_names:
            for to_node in node_names:
                if from_node != to_node:
                    if (from_node, to_node) in existing_paths \
                            or (to_node, from_node) in existing_paths:
                        continue
                    paths = networkGraph.get_all_shortest_paths(
                        from_node, to_node)
                    if len(paths) == 0:
                        continue
                    paths.sort(key=lambda x: str(x))
                    paths.sort(key=lambda x: len(x))
                    shortest_path = paths[
                        0]  # Shortest path with node indizes as nodes
                    # Translate node indizes to node names
                    shortest_path_nodenames = [
                        networkGraph.vs['name'][node] for node in shortest_path
                    ]
                    shortest_paths.append(shortest_path_nodenames)

        # Iterate over shortest paths and store subpaths that need to be installed on the nodes.
        # Also, it is made sure that all paths and reverse paths are the same
        shortest_paths.sort(key=lambda x: len(x), reverse=True)
        for path in shortest_paths:
            # Replace already existing subpaths of the path to make sure that no two paths between two
            # nodes exist
            path = IPRoutingHelper.replaceExistingSubpaths(
                path, existing_paths)

            # Mark all subpaths of path to install on nodes, unless they already exist
            subpaths = IPRoutingHelper.calculateAllSubPaths(path)
            for subpath in subpaths:
                if (subpath[0], subpath[-1]) not in existing_paths:
                    existing_paths[(subpath[0], subpath[-1])] = subpath

        # Iterate over all paths and configure the routes using the 'route add'
        info('Configure routes on all nodes\n')
        for path in existing_paths.values():
            start_node = path[0]
            end_node = path[-1]
            mini_start = net.get(start_node)
            mini_end = net.get(end_node)

            link_info = IPRoutingHelper.findLinkInformation(
                mini_links, path[0], path[1])
            start_intf = link_info.start_intf_name

            # Configure the route for every IP address of the destination
            for intf in mini_end.intfs:
                addr = mini_end.intfs[intf].ip
                if len(path) == 2:
                    # For direct connection, configure exit interface
                    debug('[{}] route add -host {} dev {}\n'.format(
                        start_node, addr, start_intf))
                    mini_start.cmd('route add -host {} dev {}'.format(
                        addr, start_intf))
                elif len(path) > 2:
                    # For longer paths, configure next hop as gateway
                    gateway_ip = link_info.end_ip
                    debug('[{}] route add -host {} dev {} gw {}\n'.format(
                        start_node, addr, start_intf, gateway_ip))
                    mini_start.cmd('route add -host {} dev {} gw {}'.format(
                        addr, start_intf, gateway_ip))
Ejemplo n.º 56
0
def convert_graph(input_path):
    """
    Converts a CRED-like graph into a graph format supported by the igraph library. The input graph must have been
    generated by cli2 CRED command (look for credResult.json)
    :param input_path: The path to the CRED graph to convert (credResult.json)
    """

    with open(input_path, encoding="utf8") as f:
        cred_file = json.load(f)

    # Locating important elements in the graph
    cred_data = cred_file[1]['credData']
    graph = cred_file[1]['weightedGraph'][1]['graphJSON'][1]
    cred_node_addresses = graph['sortedNodeAddresses']

    # Summary of edges/nodes and also a reminder about dangling edges
    print(
        f'Found cred summary data for {len(cred_data["nodeSummaries"])} nodes and {len(cred_data["edgeSummaries"])} edges'
    )
    print(
        f'The graph has {len(graph["nodes"])} nodes, {len(graph["edges"])} edges and {len(graph["sortedNodeAddresses"])} node addresses'
    )
    print(
        f'Dangling edges expected: {len(graph["edges"]) - len(cred_data["edgeSummaries"])}'
    )

    g = Graph(directed=True)

    # Collecting nodes
    for i, cred_node in enumerate(graph['nodes']):
        cred_node_address = cred_node_addresses[cred_node['index']]
        igraph_node_atts = {
            'label':
            cred_node_address[2] + '-' + cred_node_address[-1][:7],
            'type':
            cred_node_address[2],
            'timestamp':
            cred_node['timestampMs']
            if cred_node['timestampMs'] is not None else 0,
            'totalCred':
            cred_data['nodeSummaries'][i]['cred'],
            'index':
            cred_node['index'],
        }
        g.add_vertex(name=str(cred_node['index']), **igraph_node_atts)

    # Collecting edges
    dangling_edges = []
    idx = 0
    for cred_edge in graph['edges']:
        # Checking if the edges is a dangling one. If so, we skip.
        if len(g.vs.select(name_eq=str(cred_edge['srcIndex']))) + len(
                g.vs.select(name_eq=str(cred_edge['dstIndex']))) < 2:
            dangling_edges.append({
                "srcIndex": cred_edge['srcIndex'],
                "dstIndex": cred_edge['dstIndex']
            })
            continue

        igraph_edge_atts = {
            'address': '-'.join(cred_edge['address']),
            'timestamp': cred_edge['timestampMs'],
            'backwardFlow': cred_data['edgeSummaries'][idx]['backwardFlow'],
            'forwardFlow': cred_data['edgeSummaries'][idx]['forwardFlow'],
        }
        g.add_edge(str(cred_edge['srcIndex']), str(cred_edge['dstIndex']),
                   **igraph_edge_atts)
        idx += 1

    # Reporting the number of dangling edges found
    print(f"Dangling edges found: {len(dangling_edges)}")

    return g
        raise ValueError('The lists pos and text must have the same len')
    annotations = []
    for k in range(L):
        annotations.append(
            dict(
                text=labels[k], # or replace labels with a different list for the text within the circle
                x=pos[k][0], y=2*M-position[k][1],
                xref='x1', yref='y1',
                font=dict(color=font_color, size=font_size),
                showarrow=False)
        )
    return annotations

nr_vertices = 13
v_label     = list(map(str, range(nr_vertices)))
G           = Graph.Tree(nr_vertices, 3) # 2 stands for children number
lay         = G.layout('fr')

print(G.get_edgelist())

position = {k: lay[k] for k in range(nr_vertices)}
Y = [lay[k][1] for k in range(nr_vertices)]
M = max(Y)

es = EdgeSeq(G) # sequence of edges
E = [e.tuple for e in G.es] # list of edges

L = len(position)

Xn = [position[k][0] for k in range(L)]
Yn = [2*M-position[k][1] for k in range(L)]
Ejemplo n.º 58
0
def gen_cay(id):
    g = Graph()

    g.add_vertices(6)

    if id != 1:
        for i in range(6):
            g.add_edge(i, (i + 1) % 6)
    else:
        for i in range(5):
            g.add_edge(i, i + 1 % 6)

    if id != 2:
        g.add_edge(0, 2)
    g.add_edge(2, 4)
    g.add_edge(4, 0)

    g.add_edge(1, 3)
    g.add_edge(3, 5)
    g.add_edge(5, 1)

    return g
Ejemplo n.º 59
0
def accelgreedy(input_graph: Graph,
                desired_size: int,
                method: SolveMethod,
                verbosity: int = 0) -> GRT:
    """
    Run the CELF algorithm for the DRO IM problem.

    The Cost Effective Lazy Forward (CELF) algorithm does not evaluate all
    marginal gains for all possible inputs. It utilises the submodular
    property and checks if the current best known input is indeed still
    the best known input, as marginal gains can only decrease, not increase.
    """
    greedy_solution: List[int] = []
    marg_gain_return: List[float] = []
    compute_times: List[float] = []
    if desired_size == 0:
        return ([], [], [])
    graph_nodes = [n.index for n in input_graph.vs()]
    if method == SolveMethod.graph_techniques:
        pi_: Dict[int, float] = {n: 0 for n in graph_nodes}
        # Build a distance matrix
        # This speeds up the expected influence calculation
        dist_mat = input_graph.shortest_paths(weights="q")
        marg_gain_list: List[float] = [
            sum(
                marg_dro_inf(input_graph, pi_, node,
                             dist_mat=dist_mat).values())
            for node in graph_nodes
        ]
    elif method == SolveMethod.linear_program:
        inf_fun_args = {"input_graph": input_graph}
        abstract_model = inf_abs_mod(input_graph)
        inf_fun: Callable[..., Any] = inf_conc_mod
        inf_fun_args["abs_mod"] = abstract_model
        inf_fun_args["solve"] = True
        marg_gain_list = [
            inf_fun(seed_set=[node], **inf_fun_args) for node in graph_nodes
        ]
    sorted_list = sorted(zip(graph_nodes, marg_gain_list),
                         key=lambda x: x[1],
                         reverse=True)
    # First seed, always optimal
    compute_times.append(perf_counter())
    greedy_to_add = sorted_list[0][0]
    if method == SolveMethod.graph_techniques:
        pi_ = marg_dro_inf(input_graph, pi_, greedy_to_add, dist_mat=dist_mat)
    cur_spread: float = sorted_list[0][1]
    greedy_solution.append(greedy_to_add)
    marg_gain_return.append(cur_spread)
    sorted_list.pop(0)
    for k in range(1, desired_size):
        # Finding next seed with highest marginal gain
        need_to_re_eval = True
        while need_to_re_eval:
            cur_node = sorted_list[0][0]
            if method == SolveMethod.linear_program:
                inf_fun_args["seed_set"] = greedy_solution + [cur_node]
                sorted_list[0] = (cur_node,
                                  inf_fun(**inf_fun_args) - cur_spread)
            else:
                sorted_list[0] = (
                    cur_node,
                    sum(
                        marg_dro_inf(
                            input_graph, pi_, cur_node,
                            dist_mat=dist_mat).values()) - cur_spread)
            sorted_list = sorted(sorted_list, key=lambda x: x[1], reverse=True)
            need_to_re_eval = sorted_list[0][0] != cur_node

        # Found highest marginal gain
        compute_times.append(perf_counter())
        greedy_to_add = sorted_list[0][0]
        if method == SolveMethod.graph_techniques:
            pi_ = marg_dro_inf(input_graph,
                               pi_,
                               greedy_to_add,
                               dist_mat=dist_mat)
        greedy_solution.append(greedy_to_add)
        marg_gain = sorted_list[0][1]
        cur_spread += marg_gain
        marg_gain_return.append(marg_gain)
        sorted_list.pop(0)

        # Verbose output
        _verbose_output(verbosity, k, greedy_solution, greedy_to_add,
                        cur_spread)
    return (greedy_solution, marg_gain_return, compute_times)
Ejemplo n.º 60
0
    if file.endswith(".csv"):
        with open(path + file, 'rb') as f:
            for row in f.readlines():
                if firstline == True:
                    firstline = False
                    continue
                parts = row.decode().replace(' ',
                                             '').replace('\r\n',
                                                         '').strip().split(",")
                try:
                    u, v, e, weight = [i for i in parts]
                    edges.append((u, v, int(weight), e))
                except ValueError:
                    continue
g = IGraph.TupleList(edges,
                     directed=True,
                     vertex_name_attr='name',
                     edge_attrs=['weight', 'relationship'])

#连通子图
subgraph = g.components(mode=WEAK)
nodes = [{"name": node["name"]} for node in g.vs]
graph = {}
for node in nodes:
    idx = g.vs.find(name=node["name"]).index
    node["subgraph"] = subgraph.membership[idx]
    if node["subgraph"] not in graph:
        graph[node["subgraph"]] = [node["name"]]
    else:
        graph[node["subgraph"]].append(node["name"])
graphs = {}
for key, value in graph.items():