-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tracker.py
executable file
·107 lines (91 loc) · 3.7 KB
/
Tracker.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
import json
import networkx as nx
import preprocessing,config
import sys
import time
from inclusion import *
from event import Event
from hypergraph import Hypergraph
trueEvents = {'merging':0,'splitting':0,'growing':0,'shrinking':0,
'continuing':0,'dissolving':0,'forming':0,'No_event':0}
class Tracker():
def __init__(self, graphs):
self.graphs = graphs
self.communities = []
self.results = []
self.inclusions = {}
self.Dcandidates = {}
self.Fcandidates = {}
self.hypergraphs = []
def compare_communities(self,):
for index, interval in enumerate(self.graphs):
if index < len(self.graphs) - 1:
self.inclusions = {}
window_id = 'TF%s -> TF%s' % (index,index+1)
Dhypergraph = nx.DiGraph(window=window_id)
print 'Initialize inclusions dict start...'
Dhypergraph = self.initialize_inclusions(index,Dhypergraph)
print 'Initialize inclusions dict finish...'
for ic,community_t in enumerate(interval):
for ic2, community_t1 in enumerate(self.graphs[index + 1]):
inclusion = self.inclusions[community_t.graph['cid']][community_t1.graph['cid']]['inclusion']
inversed = self.inclusions[community_t.graph['cid']][community_t1.graph['cid']]['inversed_inclusion']
event = Event(community_t,community_t1,inclusion,inversed,self.inclusions)
result = event.classify()
if result in ['growing','shrinking','continuing']:
Dhypergraph.add_edge(community_t,community_t1,event_type=result)
self.results.append({ 'network_t': community_t.graph['cid'],
'network_t1': community_t1.graph['cid'],
'resulted_event': result})
hypergraph = Hypergraph(Dhypergraph)
self.hypergraphs.append(hypergraph)
def initialize_inclusions(self,index,Dhypergraph):
self.Dcandidates = {}
self.Fcandidates = {}
pastTFcommunities = self.graphs[index]
futureTFcommunities = self.graphs[index+1]
for ic,community_t in enumerate(pastTFcommunities):
Dhypergraph.add_node(community_t)
key1 = community_t.graph['cid']
self.Dcandidates[key1] = community_t
self.inclusions[key1]={}
for ic2, community_t1 in enumerate(futureTFcommunities):
key2 = community_t1.graph['cid']
if ic==0:
Dhypergraph.add_node(community_t1)
self.Fcandidates[key2] = community_t1
self.inclusions[key2] = {}
inclusions = CentralityInclusion(community_t, community_t1)
inclusion, inversed = inclusions.compute_inclusion()
if inclusion >0.1 or inversed > 0.1:
if key1 in self.Dcandidates:
del self.Dcandidates[key1]
if key2 in self.Fcandidates:
del self.Fcandidates[key2]
self.inclusions[key1].update({key2:{'inclusion':inclusion,'inversed_inclusion':inversed}})
for key in self.Dcandidates.keys():
Dhypergraph.add_edge(self.Dcandidates[key],futureTFcommunities[-1],event_type='dissolving')
for key in self.Fcandidates.keys():
Dhypergraph.add_edge(pastTFcommunities[-1],self.Fcandidates[key],event_type='forming')
return Dhypergraph
def analyze_results(self,):
events_names = ['merging', 'splitting', 'growing',
'shrinking', 'continuing', 'dissolving',
'forming', 'no_event']
events = [e['resulted_event'] for e in self.results]
for name in events_names:
print name, events.count(name)
if __name__=='__main__':
if len(sys.argv) != 2:
print 'Usage: Tracker.py <inputfile.json>'
print 'Exiting with code 1'
exit(1)
start_time = time.time()
graphs = preprocessing.getGraphs(sys.argv[1])
tracker = Tracker(graphs)
tracker.compare_communities()
with open('tmpfiles/ged_results.csv','w')as f:
for hypergraph in tracker.hypergraphs:
hypergraph.calculateEvents(f)
print "--- %s seconds ---" %(time.time() - start_time)