-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclustering_coefficient.py
More file actions
49 lines (36 loc) · 960 Bytes
/
clustering_coefficient.py
File metadata and controls
49 lines (36 loc) · 960 Bytes
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
'''
script to compute the average clustering coefficient
first need to convert json-formatted graphs to a networkx graph
to run:
python clustering_coefficient.py <filename.json>
'''
import networkx as nx
import json
import sys
import os
nodes = {}
edges = []
n = 1
filename = sys.argv[-1]
# need to change the node names to just regular ints
with open(filename) as json_file:
data = json.load(json_file)
# creating list of nodes
for node in data['nodes']:
# map where nodes[name] = node_index
id = node['id']
nodes[id] = n
n = n+1
# creating list of edges where source,target are the node indices
for edge in data['links']:
source = edge['source']
target = edge['target']
edges.append((nodes[source], nodes[target]))
# create graph in networkX
G = nx.Graph()
for node in nodes:
G.add_node(nodes[node])
for edge in edges:
G.add_edge(edge[0], edge[1])
avg_clustering_coeff = nx.average_clustering(G)
print(avg_clustering_coeff)