-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-communities.py
executable file
·66 lines (47 loc) · 1.92 KB
/
find-communities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
'''
Find communities in an graph
----------------------------
Uses the fast-greedy algorithm.
The output format is a gzipped string packed data.
Each line has the format:
osrm_id lat lon cluster
Author: Evan K. Friis
'''
import argparse
import gzip
import logging
import igraph
log = logging.getLogger(__name__)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('input', metavar='input.igraph.pkl.gz',
help='Gzipped igraph cornichon.')
parser.add_argument('output', metavar='communities.gz',
help='Output communities')
parser.add_argument('--clusters', default=0,
type=int, help='Number of clusters to form.'
' If not specified, use the # found by the algo')
args = parser.parse_args()
logging.basicConfig()
log.setLevel(logging.INFO)
log.info("Loading graph from %s", args.input)
graph = igraph.read(args.input, format='picklez')
log.info("=> %i nodes, %i edges", len(graph.vs), len(graph.es))
log.info("Finding communities via fastgreedy")
communities = graph.community_fastgreedy(weights='weight')
log.info("Found an optimal count of %i communities",
communities.optimal_count)
n_clusters = args.clusters if args.clusters else communities.optimal_count
log.info("Partitioning dendrogram into %i clusters", n_clusters)
clusters = communities.as_clustering(n_clusters)
log.info("Mini-fying data")
log.info("Writing to %s", args.output)
with gzip.open(args.output, 'wb') as outputfd:
for clust_idx, cluster in enumerate(clusters):
for vertex_idx in cluster:
vertex = graph.vs[vertex_idx]
outputfd.write(' '.join(str(x) for x in [
vertex['name'], vertex['lat'],
vertex['lon'], clust_idx, '\n'
]))