-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_walks.py
executable file
·54 lines (38 loc) · 1.47 KB
/
process_walks.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
import argparse
from metapathWalking import getConfiguredLogger
parser = argparse.ArgumentParser(description='Walk a Graph along Metapaths and store complete walks.')
parser.add_argument('--input', "-i", type=str,
help='path to the full walks file')
parser.add_argument('--output', "-o", type=str,
help='path to the uniquefied node ids')
args = parser.parse_args()
logger = getConfiguredLogger("Process Walks")
def buf_count_newlines_gen(fname):
def _make_gen(reader):
while True:
b = reader(2 ** 16)
if not b: break
yield b
with open(fname, "rb") as f:
count = sum(buf.count(b"\n") for buf in _make_gen(f.raw.read))
return count
logger.info("Counting complete walks...")
line_count = buf_count_newlines_gen(args.input)
ten_percent = int(line_count/10)
logger.info("{} Walks waiting to be processed.".format(line_count))
unique_ids = set()
with open(args.input,"r") as input:
counter = 0
percentage = 1
for i, line in enumerate(input):
unique_ids.update(line.strip().split(" "))
counter += 1
if counter == ten_percent:
logger.info("Processed {}0% of walks".format(percentage))
percentage += 1
counter = 0
with open(args.output,"w") as output:
for id in unique_ids:
if id != "":
output.write(str(id) + "\n")
logger.info("Identified {} unique ids.".format(buf_count_newlines_gen(args.output)))