In [2]:
from collections import defaultdict
In [31]:
infopath_output_file = "data/synth-zurich/output-wl10-tl5/output-wl10-tl5.txt"
timesteps_file = "data/synth-zurich/output-wl10-tl5/output-wl10-tl5-time-steps.txt"
ts_to_gen_values = [11,]
contains_nodes = True
In [32]:
f_infopath = open(infopath_output_file, 'r')
f_timesteps = open(timesteps_file, 'r')
In [33]:
if contains_nodes:
flag = True
while flag:
# Keep reading lines till a new line is encountered
for line in f_infopath:
if ',' not in line or line == '\n':
flag = False
break
In [34]:
timesteps = map(lambda x: float(x.strip()), f_timesteps.readlines())
first_ts = timesteps[0]
last_ts = timesteps[-1]
In [35]:
ts_to_edges_dct = defaultdict(list)
for line in f_infopath:
vals = map(float, line.split(','))
source, target = map(int, vals[0:2])
# Obtain a list of pairs <timestamp, weight>
edge_weights = [(int(vals[i]), float(vals[i+1])) for i in range(2, len(vals)-1, 2)]
for ts, weight in edge_weights:
if weight != 0.00:
ts_to_edges_dct[ts] += [(source, target, weight), ]
In [36]:
for ts_to_gen in ts_to_gen_values:
out_file = "%s-at-%d.txt" % (infopath_output_file.split('.')[0], ts_to_gen)
f_out = open(out_file, 'w')
for edge in sorted(ts_to_edges_dct[ts_to_gen], key=lambda x:x[0]):
source, target, weight = edge
f_out.write('%d,%d,%f\n' % (source, target, weight))
f_out.close()
In [37]:
f_infopath.close()
f_timesteps.close()
In [38]:
out_file
Out[38]:
In [ ]: