-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfbNet_ego.py
More file actions
73 lines (59 loc) · 2.3 KB
/
fbNet_ego.py
File metadata and controls
73 lines (59 loc) · 2.3 KB
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
67
68
69
70
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 3 00:33:22 2017
@author: anilosmantur
"""
import os
import community
import networkx as nx
from networkx.algorithms import community as nx_com
from networkx.algorithms import approximation as nx_approx
import matplotlib.pyplot as plt
import UtilitiesNetwork as un
print('library imports done')
FILE_NAME = '/home/anilosmantur/***/complex_networks/project/ego_facebook/facebook/'
edgeFiles = [file for file in os.listdir(FILE_NAME) if 'edges' in file]
egoNodes = [int(ego[:-6]) for ego in edgeFiles ]
egoNodes.sort()
del edgeFiles
for egoNode in egoNodes:
edges = []
with open(FILE_NAME+ str(egoNode) + '.edges') as netfile:
print('file opened')
for i, line in enumerate(netfile):
words = line.split()
edges.append((egoNode, (int(words[0]))))
edges.append((egoNode, (int(words[1]))))
edges.append((int(words[0]), int(words[1])))
print('Reading edges finished')
G = nx.Graph(edges)
info = nx.info(G) + '\n'
# drawing the graph
pos = nx.spring_layout(G)
un.drawGraphSave(G, pos, 8, 'ego_'+str(egoNode))
plt.close()
part = community.best_partition(G)
size = float(len(set(part.values())))
com = 'Found community count: ' + str(size) + '\n'
mode = community.modularity(part, G)
mode = 'Modularity: ' + str(mode) + '\n'
un.drawCommunityGraphSave(G, pos, part, 8, 'ego_'+str(egoNode))
plt.close()
#part2 = nx_com.girvan_newman(G)
##perf = nx_com.performance(G, part2)
##print('Performance girvan_newman : ', perf)
#coms = list(sorted(c) for c in next(part2))
#un.nxDrawCommunityGraphSave(G, pos, coms, 8, gi)
centb = nx.centrality.betweenness_centrality(G)
un.centralityPlotSave(centb, 5, 'ego_'+str(egoNode), 'betweenness')
un.drawCentralityGraphSave(G, pos, centb, 8, 'ego_'+str(egoNode), 'betweenness')
plt.close()
centd = nx.centrality.degree_centrality(G)
un.centralityPlotSave(centd, 5, 'ego_'+str(egoNode), 'degree')
un.drawCentralityGraphSave(G, pos, centd, 8, 'ego_'+str(egoNode), 'degree')
plt.close()
with open('sums/egoNet_' + str(egoNode) + 'sum.txt', 'w') as sumfile:
sumfile.write(info)
sumfile.write(com)
sumfile.write(mode)