-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate_graph.py
More file actions
32 lines (27 loc) · 943 Bytes
/
Copy pathgenerate_graph.py
File metadata and controls
32 lines (27 loc) · 943 Bytes
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
import numpy as np
import glob
import re
import os
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
# Get the file path
all_file =sorted(glob.glob("G*/G*.txt"), key=natural_keys)
# Generate graph from file
for i in range(len(all_file)):
line_ct = 0
with open(all_file[i]) as f:
for line in f:
if line_ct == 0: # Get graph size
graph = np.zeros((int(line.split()[0]), int(line.split()[0])), dtype=int)
else:
idx_i = int(line.split()[0]) - 1
idx_j = int(line.split()[1]) - 1
weight = int(line.split()[2])
graph[idx_i, idx_j] = weight
line_ct += 1
# Save graph
dir_name = re.sub(".txt", "", all_file[i])
dir_name = re.sub(".*/", "", dir_name)
np.savetxt(dir_name + "/matrix.txt", graph, fmt="%d")