forked from pholme/tsir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_run.py
77 lines (61 loc) · 2.17 KB
/
read_run.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
## Common routines for Python wrapper for temporal network SIR C code
import networkx as nx
from random import getrandbits
from subprocess import Popen, PIPE, STDOUT
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# This function reads the network. The input file contains
# three columns [id 1] [id 2] [time], showing that id 1 has
# been in contact with id 2 at time t
# nodes with no contacts are not considered
def read_network (fname):
G = nx.Graph()
tmax = 0
try:
with open(fname) as f:
for l in f:
a = l.strip().split()
if len(a) == 3:
if a[0] != a[1]:
b = int(a[2])
if tmax < b:
tmax = b
if G.has_edge(a[0],a[1]):
if b not in G.edges[a[0],a[1]]['times']: # excluding identical events
G.edges[a[0],a[1]]['times'].append(b)
else:
print('ignoring multiple events', a[0], a[1], a[2])
else:
G.add_edge(a[0],a[1])
G.edges[a[0],a[1]]['times'] = [b]
else:
print('ignoring self-contact of', a[0])
except:
print('reading error in:', fname)
exit(1)
H = nx.convert_node_labels_to_integers(G) # this can change the id numbers from the original file
# assembling the temporal network in the format of the C code
nwk = str(H.number_of_nodes()) + ' ' + str(tmax) + '\n'
for i in range(H.number_of_nodes()):
nwk += str(H.degree(i)) + '\n'
to_sort = []
for v in H.neighbors(i):
to_sort.append((max(H.edges[i,v]['times']),v))
for x,v in sorted(to_sort,reverse=True):
nwk += str(v) + ' ' + str(len(H.edges[i,v]['times'])) + '\n'
for t in sorted(H.edges[i,v]['times']):
nwk += str(t) + '\n'
return nwk
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
def run (nwk, beta, nu):
assert(beta > 0.0)
if (beta > 1):
print('beta should be <= 1, so set to 1')
beta = 1.0
assert(nu > 0)
p = Popen(['./tsir', str(beta), str(nu), str(getrandbits(64))], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
o = p.communicate(input=bytes(nwk,encoding='utf-8'))[0]
# interpret and report the output
a = o.decode().split('\n')
b = a[0].split()
return float(b[0]), float(b[1])
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##