-
Notifications
You must be signed in to change notification settings - Fork 0
/
py2ml.py
61 lines (48 loc) · 2.44 KB
/
py2ml.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
# interface module for matlab
import json
import utils
import csv
from markov import mkv
def compute_matlab_tps(file_input, separator, dir_out):
sequences = utils.read_from_file(file_input, separator)
# compute transitions frequencies
tf_agent = mkv.markov_trans_freq(sequences[:10])
tf_tot = mkv.markov_trans_freq(sequences)
# with open(dir_out + "agent_sym_freq.json", "w") as fp:
# json.dump(tf_agent[0], fp, default=mkv.serialize_sets, ensure_ascii=False)
# with open(dir_out + "process_sym_freq.json", "w") as fp:
# json.dump(tf_tot[0], fp, default=mkv.serialize_sets, ensure_ascii=False)
with open(dir_out + "agent_tps.json", "w") as fp:
json.dump(tf_agent, fp, default=mkv.serialize_sets, ensure_ascii=False)
with open(dir_out + "process_tps.json", "w") as fp:
json.dump(tf_tot, fp, default=mkv.serialize_sets, ensure_ascii=False)
choords = list(tf_tot[0].keys())
with open(dir_out + 'agent_sym.csv', mode='w') as asf:
ags_writer = csv.writer(asf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
ags_writer.writerow(tf_agent[0].keys())
ags_writer.writerow([x[1] for x in tf_agent[0].items()])
with open(dir_out + 'process_sym.csv', mode='w') as psf:
ps_writer = csv.writer(psf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
ps_writer.writerow(tf_tot[0].keys())
ps_writer.writerow([x[1] for x in tf_tot[0].items()])
with open(dir_out + 'agent.csv', mode='w') as af:
agent_writer = csv.writer(af, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
agent_writer.writerow(["sym"] + choords)
for itm in tf_agent[1].items():
agent_writer.writerow([itm[0]] + _create_coords(itm[1], choords))
with open(dir_out + 'process.csv', mode='w') as tf:
tot_writer = csv.writer(tf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
tot_writer.writerow(["sym"] + choords)
for itm in tf_tot[1].items():
tot_writer.writerow([itm[0]] + _create_coords(itm[1], choords))
def _create_coords(a_dict, dims):
res = []
for x in dims:
if x in a_dict.keys():
res.append(float(a_dict[x]))
else:
res.append(0)
return res
compute_matlab_tps("C:/Users/matti/OneDrive/Desktop/DOTT/projects/NohGen2/matlab/irish.txt",
" ",
"C:/Users/matti/OneDrive/Desktop/DOTT/projects/NohGen2/matlab/")