-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstruction.py
executable file
·149 lines (100 loc) · 3.19 KB
/
construction.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import networkx as nx
import numpy as np
def add_labels(G,meta_path):
"""Summary or Description of the Function
Parameters:
argument1 (int): Description of arg1
Returns:
int:Returning value
"""
meta = load_metadata(meta_path) #dictionary
for n in G.nodes():
G.nodes()[n]["label"] = meta[n]
return(G)
def load_data(path):
"""load the cvs file representing the temporal graph
Parameters:
path (string): path of the input dataset
Returns:
np.array: a np array with the loaded data
"""
data = []
with open(path) as f:
for line in f:
tmp = line.split()[0:3]
arr_tmp = [int(tmp[0]),int(tmp[1]),int(tmp[2])]
data.append(arr_tmp)
data = np.array(data)
return(data)
def load_metadata(path_meta):
"""load the metadata of the tmporal graph
Parameters:
path_meta (string): path of the meta data
Returns:
dict: a dictionary whit key = node and value = attributes
"""
data = dict()
with open(path_meta) as f:
for line in f:
tmp = line.split()[0:2]
data[int(tmp[0])] = tmp[1]
return(data)
def individuals(data):
"""compute the number of individuals
Parameters:
data (np.array): loaded input data
Returns:
int: number of individuals in the network
"""
return(np.unique(data[:,1:]))
def build_graphs(data,gap=19,with_labels=False,meta_path=None):
"""build an arry of temoral graphs
Parameters:
data (np.array): loaded input data
gap (int): [default = 19] this integer is used to build each snapshot, in particular for each interactions within the temporal graph a new snaphsot is created
with_labels (boolean): [default = False] if ture, it adds to the temporal snapshots the node label
meta_path (str): [default = None] if with_labels is specified, then meta_path is the string of the meta data path
Returns:
np.array: an array of static graphs
"""
graphs = []
G=nx.Graph()
nodes = individuals(data)
G.add_nodes_from(nodes)
if(with_labels):
G = add_labels(G,meta_path)
splitted_data = split_input_data(data,gap)
for t in splitted_data:
g = G.copy()
for _,i,j in t:
g.add_edge(i,j)
graphs.append(g)
return(graphs)
def build_aggragated_graph(data):
"""compute the agregated network
Parameters:
data (np.array): loaded input data
Returns:
nx.graph: return a static graph, representing all the interactions
"""
data = data[:,1:]
aa = np.unique(data,axis=0)
G = nx.Graph()
G.add_edges_from(aa)
return(G)
def split_input_data(data, gap=19):
"""split the input data according to the temporal gap
Parameters:
data (np.array): loaded input data
gap (int): [default = 19]
Returns:
np.array(): return an array of array, the inner array represent the interactions within the gap.
"""
times = data[:,0]
pos = times[0]
chunks = []
for i in range(len(times)):
if not(times[i]<=(pos + gap)):
chunks.append(i)
pos = times[i]
return(np.split(data,chunks))