-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_CI_retweet_networks.py
65 lines (51 loc) · 2 KB
/
compute_CI_retweet_networks.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
#-*- coding: utf-8 -*-
"""
Created on 2019-01-03 14:54:11
@author: https://kayzhou.github.io/
"""
import json
import os
# import pickle
import sys
import time
import graph_tool.all as gt
import numpy as np
PACKAGE_PARENT = '.'
SCRIPT_DIR = os.path.dirname(os.path.realpath(
os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
import CIcython
#%% add CI values to graph
def add_CI_to_graph(graph_file):
print('pid ' + str(os.getpid()) + ' loading graph ' + graph_file)
graph = gt.load_graph(graph_file)
# for direction in ['out', 'both', 'in']:
for direction in ['out', 'undir', 'in', 'both']:
print('pid ' + str(os.getpid()) + ' -- ' + direction)
t0 = time.time()
CIranks, CImap = CIcython.compute_graph_CI(graph, rad=2,
direction=direction,
verbose=True)
# print(CImap)
t1 = time.time() - t0
print('pid ' + str(os.getpid()) + ' -- ' + str(t1))
print('pid ' + str(os.getpid()) + ' saving CIranks ' +
direction + '_' + graph_file)
# CI_file_name = 'data/CI_rst/CI_{}_{}.rst'.format(
# direction, os.path.split(graph_file)[1][:-3])
# np.save(CI_file_name, np.array([CIranks, CImap]))
graph.vp['CI_' +
direction] = graph.new_vertex_property('int64_t', vals=0)
graph.vp['CI_' + direction].a = CImap
print('pid ' + str(os.getpid()) + ' -- adding katz centrality')
graph.vp['katz'] = gt.katz(graph)
graph.set_reversed(True)
graph.vp['katz_rev'] = gt.katz(graph)
graph.set_reversed(False)
print('pid ' + str(os.getpid()) + ' saving graph ' + graph_file)
graph.save(graph_file)
if __name__ == "__main__":
save_dir = 'disk/network'
for in_name in os.listdir(save_dir):
if in_name.endswith("nc.gt"):
add_CI_to_graph(os.path.join(save_dir, in_name))