Skip to content

Commit f5d2c4d

Browse files
author
shogo-ma
committed
add convertIGraphToNxGraph to louvain.py
1 parent 0bd924b commit f5d2c4d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

louvain/louvain.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@
22
# coding: utf-8
33

44
import copy
5+
import networkx as nx
56

67
from itertools import permutations
78
from itertools import combinations
89
from collections import defaultdict
910

1011
class Louvain(object):
12+
13+
@classmethod
14+
def convertIGraphToNxGraph(cls, igraph):
15+
node_names = igraph.vs["names"]
16+
edge_list = igraph.get_edgelist()
17+
weight_list = igraph.es["weight"]
18+
node_dict = defaultdict(str)
19+
20+
for idx, node in enumerate(igraph.vs):
21+
node_dict[node.index] = node_names[idx]
22+
23+
convert_list = []
24+
for idx in range(len(edge_list)):
25+
edge = edge_list[idx]
26+
new_edge = (node_dict[edge[0]], node_dict[edge[1]], weight_list)
27+
convert_list.append(new_edge)
28+
29+
convert_graph = nx.Graph()
30+
convert_graph.add_weighted_edges_from(convert_list)
31+
return convert_graph
1132

1233
@classmethod
1334
def getBestPartition(cls, graph):

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
description="A implementation of louvain method on python",
88
author="shogo-ma",
99
packages=["louvain"],
10+
install_requires=["networkx"],
1011
)

0 commit comments

Comments
 (0)