-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDag.py
361 lines (305 loc) · 11.3 KB
/
Dag.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import os
from Node import *
import pickle as pik
from globals import *
from utils import *
import graphviz as gv
from IPython.display import display, Image
from PIL.Image import open as open_image
class Dag:
"""
This class creates a DAG (directed acyclic graph) for the movie entitled
`m_title`. The DAG has nodes `nodes` and arrows `arrows`. Each arrow has
a two weights `num_acc` and `num_rej`. Those weights are the number of
times the arrow has been accepted and rejected. They are stored in the
dictionary `arrow_to_acc_rej_nums`.
Attributes
----------
arrow_to_acc_rej_nums: dict[tuple(Node), tuple(int)]
arrows: list[tuple[Node, Node]]
arrows of self. Arrows are defined as a pair of Node objects.
The first element of the pair is the origin of the arrow and the
second is the target of the arrow.
m_title: str
nodes: list[Node]
"""
def __init__(self, m_title, simp_dir):
"""
Constructor
Parameters
----------
m_title: str
title of movie to which this DAG refers to.
simp_dir: str
the directory in which simplified files are stored, and from
which objects of this class are constructed.
"""
self.m_title = m_title
path = simp_dir + "/" + m_title + ".txt"
with open(path, "r") as f:
lines = [line for line in f]
self.nodes = []
for time, line in enumerate(lines):
if line.strip() not in [ZTZ_SEPARATOR, ""]:
ztz_list = line.split(ZTZ_SEPARATOR)
for place in range(len(ztz_list)):
self.nodes.append(Node(time, place))
self.arrows = []
self.arrow_to_acc_rej_nums = {}
def save_self(self, dag_dir):
"""
This method stores self as a pickled file.
Parameters
----------
dag_dir: str
Directory in which pickled file is stored.
Returns
-------
None
"""
path = dag_dir + "/" + self.m_title + ".pkl"
with open(path, "wb") as f:
pik.dump(self, f, protocol=pik.HIGHEST_PROTOCOL)
def update_arrow(self, arrow, accepted):
"""
This method changes the tuple (num_accepted, num_rejected) of
`arrow`. If accepted=True, num_accepted is increased by one. If
accepted=False, num_rejected is increased by one.
Parameters
----------
arrow: tuple[Node, Node]
accepted: bool
Returns
-------
None
"""
if arrow not in self.arrows:
self.arrows.append(arrow)
self.arrow_to_acc_rej_nums[arrow] = [0, 0]
if accepted:
self.arrow_to_acc_rej_nums[arrow][0] += 1
else:
self.arrow_to_acc_rej_nums[arrow][1] += 1
def build_node_to_clean_ztz_dict(self,
clean_dir,
skip_1st_line=False):
"""
This method builds from scratch and returns a dictionary called
`nd_to_clean_ztz` that maps each node to a clean sentence. ztz
stands for sentence.
Parameters
----------
clean_dir: str
directory of movie scripts after cleaning.
Returns
-------
dict(Node, str)
"""
path = clean_dir + "/" + self.m_title + ".txt"
is_csv = False
if not os.path.isfile(path):
path = path.replace(".txt", ".csv")
is_csv = True
assert os.path.isfile(path)
time_to_clean_ztz = {}
with open(path, "r") as f:
time = -1
for line in f:
time += 1
if is_csv:
if time == 0:
continue
else:
time_to_clean_ztz[time - 1] = line.strip()
else:
time_to_clean_ztz[time] = line.strip()
nd_to_clean_ztz = {}
for nd in self.nodes:
nd_to_clean_ztz[nd] = time_to_clean_ztz[nd.time]
return nd_to_clean_ztz
def build_node_to_simple_ztz_dict(self, simp_dir):
"""
This method builds from scratch and returns a dictionary called
`nd_to_simple_ztz` that maps each node to a simplified sentence. ztz
stands for sentence.
Parameters
----------
simp_dir: str
directory of movie scripts after simplifying.
Returns
-------
dict(Node, str)
"""
path = simp_dir + "/" + self.m_title + ".txt"
time_to_simp_ztz_list = {}
with open(path, "r") as f:
time = -1
for line in f:
time += 1
if line.strip() != ZTZ_SEPARATOR:
time_to_simp_ztz_list[time] = \
line.split(ZTZ_SEPARATOR)
nd_to_simp_ztz = {}
for nd in self.nodes:
nd_to_simp_ztz[nd] = \
time_to_simp_ztz_list[nd.time][nd.place].strip()
return nd_to_simp_ztz
def build_high_prob_acc_arrows(self,
prob_acc_thold,
nsam_thold):
"""
This method builds from scratch and returns a list of all arrows
whose `prob_acc` (i.e., probability of acceptance) is >=
`prob_acc_thold` with `nsam` (i.e., number of samples used to
calculate that probability) >= `nsam_thold`. thold = threshold
Parameters
----------
prob_acc_thold: float
nsam_thold: int
Returns
-------
list[tuple[Node, Node]]
"""
high_prob_arrows = []
for arrow in self.arrows:
prob_acc, nsam = get_prob_acc_and_nsam(
*self.arrow_to_acc_rej_nums[arrow])
if prob_acc >= prob_acc_thold and \
nsam >= nsam_thold:
high_prob_arrows.append(arrow)
return high_prob_arrows
def print_map_legend(self,
clean_dir,
simp_dir,
prob_acc_thold,
nsam_thold):
"""
This method prints the DAG Rosetta stone (map legend).
For each node labeled `( time, place)`, this method prints the
simplified clause ( i.e., simplified sentence) in line `time` of the
simplified file, after a number `place` of separator-tokens. It also
prints the original sentence from which that simplified clause came
from. The full sentence is preceded by the label `(full)` and the
simplified sentence by the label `(part)`.
It only prints the `(full)` and `(part)` for those nodes that appear
in the DAG, after removing all arrows with probability of acceptance
< `prob_acc_thold` or number of sample used to calculate that
probability < `nsam_thold`.
Parameters
----------
clean_dir: str
directory of movie scripts after cleaning
simp_dir: str
directory of movie scripts after simplification
prob_acc_thold: float
nsam_thold: int
Returns
-------
None
"""
hprob_arrows = self.build_high_prob_acc_arrows(
prob_acc_thold, nsam_thold)
print("MAP LEGEND")
print("title:", self.m_title)
print("prob of acceptance threshold:", prob_acc_thold)
print("number of samples threshold:", nsam_thold)
print("number of arrows shown:", len(hprob_arrows))
print("number of arrows dropped:",
len(self.arrows) - len(hprob_arrows))
hprob_nodes = []
for arrow in hprob_arrows:
if arrow[0] not in hprob_nodes:
hprob_nodes.append(arrow[0])
if arrow[1] not in hprob_nodes:
hprob_nodes.append(arrow[1])
hprob_nodes = sorted(hprob_nodes, key=lambda x: x.time)
if clean_dir:
nd_to_clean_ztz = self.build_node_to_clean_ztz_dict(clean_dir)
else:
nd_to_clean_ztz = None
nd_to_simple_ztz = self.build_node_to_simple_ztz_dict(simp_dir)
for nd in hprob_nodes:
print(color.GREEN + color.BOLD + node_str(nd) + ":" + color.END)
ztz0 = ""
if nd_to_clean_ztz:
ztz0 = nd_to_clean_ztz[nd]
print("(FULL)", ztz0)
print("(PART)", nd_to_simple_ztz[nd])
@staticmethod
def draw_dot(s, j_embed):
"""
This method draws a dot string.
Using display(s) will draw the graph but will not embed it permanently
in the notebook. To embed it permanently, must generate temporary image
file and use Image().display(s)
Parameters
----------
s: output of graphviz Source(dot_str)
j_embed: bool
True iff want to embed image in jupyter notebook. If you are
using a python terminal instead of a jupyter notebook,
only j_embed=False will draw image.
Returns
-------
None
"""
x = s.render("tempo", format='png', view=False)
if j_embed:
display(Image(x))
else:
open_image("tempo.png").show()
def draw(self, prob_acc_thold, nsam_thold, jupyter=False):
"""
This method draws the graph for self. Only arrows with
`prob_acceptance` >= `prob_acc_thold` are drawn.
Parameters
----------
prob_acc_thold: float
nsam_thold: int
jupyter: bool
Returns
-------
None
"""
hprob_arrows = self.build_high_prob_acc_arrows(
prob_acc_thold, nsam_thold)
dot = "digraph {\n"
for arrow in hprob_arrows:
prob_acc, nsam = get_prob_acc_and_nsam(
*self.arrow_to_acc_rej_nums[arrow])
X = '"' + str(prob_acc) + " (" + str(nsam) + ")" + '"'
dot += '"' + node_str(arrow[0]) + '"' + "->" + \
'"' + node_str(arrow[1]) + '"' + \
' [label=' + X + "];\n"
dot += 'labelloc="b";\n'
dot += 'label="' + self.m_title + '";\n'
dot += "}\n"
# print("vvbn", dot)
Dag.draw_dot(gv.Source(dot), j_embed=jupyter)
if __name__ == "__main__":
def main1(prob_acc_thold, nsam_thold, draw):
dag_dir = "short_stories_dag_atlas"
simp_dir = "short_stories_simp"
clean_dir = "short_stories_clean"
file_names = [file_name for
file_name in my_listdir(dag_dir)[0:3]]
dags = []
for fname in file_names:
path = dag_dir + "/" + fname
# print("ghty", path)
with open(path, "rb") as f:
dag = pik.load(f)
dags.append(dag)
for dag in dags:
print("==================================")
print(dag.m_title)
hprob_arrows = dag.build_high_prob_acc_arrows(
prob_acc_thold, nsam_thold)
print({arrow_str(arrow):
dag.arrow_to_acc_rej_nums[arrow] \
for arrow in hprob_arrows})
print()
if draw:
dag.draw(prob_acc_thold, nsam_thold)
dag.print_map_legend(clean_dir, simp_dir, prob_acc_thold)
main1(prob_acc_thold=.90, nsam_thold=2, draw=True)