SHARE
TWEET

Untitled

a guest Oct 14th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. IGraph wrapper for add node easily
  3. """
  4.  
  5. import igraph
  6. import leidenalg
  7.  
  8.  
  9. class IGraphWrapper:
  10.  
  11.     def __init__(self):
  12.         self.g = igraph.Graph(directed=False)
  13.         self.id2zid = {}
  14.         self.zid2id = {}
  15.  
  16.     def add_node(self, zid):
  17.         self.add_vertex(zid, quiet=True)
  18.  
  19.     @property
  20.     def nodes(self):
  21.         for zid in self.zid2id:
  22.             yield zid
  23.  
  24.     def add_vertex(self, zid, quiet=False):
  25.         if zid in self.zid2id:
  26.             if quiet:
  27.                 return
  28.             else:
  29.                 raise ValueError(f"{zid} was in graph")
  30.  
  31.         self.g.add_vertex(zid)
  32.         gid = len(self.id2zid)
  33.         self.id2zid[gid] = zid
  34.         self.zid2id[zid] = gid
  35.  
  36.     def add_vertex_from(self, zids, quiet=False):
  37.         for zid in zids:
  38.             self.add_vertex(zid, quiet)
  39.  
  40.     def add_edge(self, src_id, dst_id):
  41.         if src_id not in self.zid2id:
  42.             raise ValueError(f"Node {src_id} is not in graph")
  43.  
  44.         if dst_id not in self.zid2id:
  45.             raise ValueError(f"Node {dst_id} is not in graph")
  46.  
  47.         return self.g.add_edge(self.zid2id[src_id], self.zid2id[dst_id])
  48.  
  49.     def add_edge_from(self, edges):
  50.         self.g.add_edges([(self.zid2id[x[0]], self.zid2id[x[1]]) for x in edges])
  51.  
  52.     def neighbors(self, zid):
  53.         if zid not in self.zid2id:
  54.             raise ValueError(f"Can not found vertex id: {zid}")
  55.  
  56.         neighbors = self.g.neighbors(self.zid2id[zid])
  57.         return [self.id2zid[x] for x in neighbors]
  58.  
  59.     def run_personalize_pagerank(self, reset_vertices, damping_factor=0.85, niter=1000):
  60.         root_nodes = [self.zid2id[zid] for zid in reset_vertices if zid in self.zid2id]
  61.         result = self.g.personalized_pagerank(reset_vertices=root_nodes,
  62.                                               directed=False,
  63.                                               damping=damping_factor,
  64.                                               niter=niter)
  65.  
  66.         ppr = {}
  67.         for i in range(len(result)):
  68.             ppr[self.id2zid[i]] = result[i]
  69.  
  70.         return ppr
  71.  
  72.     def has_node(self, zid):
  73.         return zid in self.zid2id
  74.  
  75.     def has_edge(self, zid_src, zid_dst):
  76.         return self.g.get_eid(self.zid2id[zid_src], self.zid2id[zid_dst], False, error=False) > -1
  77.  
  78.     def number_of_edges(self):
  79.         return len(self.g.get_edgelist())
  80.  
  81.     def number_of_nodes(self):
  82.         return len(self.zid2id)
  83.  
  84.     def louvain(self):
  85.         communities = leidenalg.find_partition(self.g, leidenalg.SignificanceVertexPartition, n_iterations=2)
  86.         communities = [[self.id2zid[x] for x in community] for community in communities]
  87.         return communities
  88.  
  89.     def remove_redundant_graph(self):
  90.         delete_vertices = []
  91.         for node in self.id2zid:
  92.             if self.g.degree(node) == 1:
  93.                 delete_vertices.append(node)
  94.  
  95.         self.g.delete_vertices(delete_vertices)
  96.         print("Delete vertices: {0}".format(delete_vertices))
  97.  
  98. if __name__ == "__main__":
  99.     g = IGraphWrapper()
  100.     g.add_vertex(1)
  101.     g.add_vertex(2)
  102.     g.add_vertex(3)
  103.     g.add_vertex(5)
  104.     g.add_vertex(4)
  105.     g.add_vertex(6)
  106.     g.add_edge(1, 2)
  107.     g.add_edge(2, 6)
  108.  
  109.     g.add_edge(3, 5)
  110.     g.add_edge(4, 5)
  111.     g.add_edge(3, 4)
  112.  
  113.     a = g.louvain()
  114.     print(a[0])
  115.     print(a[1])
  116.     print(g.has_edge(1, 2))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top