-
Notifications
You must be signed in to change notification settings - Fork 0
/
depict_wfa.py
58 lines (51 loc) · 1.67 KB
/
depict_wfa.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
from graphviz import Digraph
import WFA
import util
import argparse
import util_boto3
import zipfile
import os.path
import pickle
colors = ["red", "blue", "green"]
def make_node_id(wfa, i):
q0 = wfa.q0.reshape((-1,))
final = wfa.final.reshape((-1,))
return f"[{i}]/{q0[i]:.2e}/{final[i]:.2e}"
def depict_wfa(wfa: WFA.WFA):
G = Digraph(format="png")
print(f"# q0")
print(wfa.q0)
print(f"# final")
print(wfa.final)
for i in range(wfa.get_size()):
G.node(make_node_id(wfa, i))
for aid, a in enumerate(wfa.alphabet):
for i in range(wfa.get_size()):
for j in range(wfa.get_size()):
if abs(wfa.delta[a][i,j]) > 0.01 and a in ["(", ")"]:
G.edge(make_node_id(wfa, i), make_node_id(wfa, j), f"{a}/{wfa.delta[a][i, j]:.2e}",
color=colors[aid % len(colors)])
print(f"# delta[{a}]")
print(wfa.delta[a])
fn = util.get_time_hash()
G.render(fn)
return fn + ".png"
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Depict WFA')
parser.add_argument('filename',
help='filename of pikcle')
parser.add_argument('--s3', type=str, default="",
help='target zip file if it exists')
args = parser.parse_args()
if args.s3 != "":
util_boto3.download(args.s3)
with zipfile.ZipFile(args.s3) as z:
z.extractall("temp_depict_wfa")
filepath = os.path.join("temp_depict_wfa", args.filename)
else:
filepath = args.filename
with open(filepath, "rb") as f:
wfa: WFA.WFA = pickle.load(f)
x = depict_wfa(wfa)
print(x)