This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathepisode.py
213 lines (190 loc) · 9.02 KB
/
episode.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
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
try:
from IPython.display import display
except ImportError:
assert not INTERACTIVE
def display(*args, **kwargs):
pass
import functools
from pathlib import Path
import datetime
import abs_render
import codraw_data
from abs_metric import scene_similarity
class Episode(list):
def get_last(self, event_type):
for event in reversed(self):
if isinstance(event, event_type):
return event
return None
def reconstruct(self):
reconstructed_scene = []
for event in self:
if isinstance(event, codraw_data.DrawClipart):
reconstructed_scene = [c for c in reconstructed_scene if c.idx != event.clipart.idx]
reconstructed_scene.append(event.clipart)
elif isinstance(event, codraw_data.DrawGroup):
reconstructed_scene = [c for c in reconstructed_scene if c.idx not in [c2.idx for c2 in event.cliparts]]
reconstructed_scene.extend(event.cliparts)
return codraw_data.AbstractScene(reconstructed_scene)
def display(self):
scene = None
for event in self:
if isinstance(event, codraw_data.ObserveTruth):
assert scene is None, "Multiple ObserveTruth events not allowed in an episode"
scene = event.scene
elif isinstance(event, codraw_data.SelectClipart):
display(event.clipart)
elif isinstance(event, codraw_data.DrawClipart):
abs_render.display_cliparts([event.clipart], color='red', scale=0.75)
elif isinstance(event, codraw_data.DrawGroup):
abs_render.display_cliparts(event.cliparts, color='red', scale=0.75)
elif isinstance(event, codraw_data.TellGroup):
print("TELLER:", event.msg)
elif isinstance(event, codraw_data.ReplyGroup):
print("DRAWER:", event.msg)
elif isinstance(event, codraw_data.TellerIntention):
if event.drawn is not None:
abs_render.display_cliparts(event.drawn, color='purple', label='drawn', scale=0.33)
if event.draw_next is not None:
abs_render.display_cliparts(event.draw_next, color='yellow', label='draw next', scale=0.33)
if event.undrawn is not None:
abs_render.display_cliparts(event.undrawn, color='cyan', label='undrawn', scale=0.33)
print('===')
reconstructed_scene = self.reconstruct()
abs_render.display_cliparts(scene, label='ground truth', scale=0.75)
abs_render.display_cliparts(reconstructed_scene, color='red', label='reconstructed', scale=0.75)
print('Similarity =', scene_similarity(reconstructed_scene, scene))
def to_html(self):
res = ""
scene = None
delayed_selected_clipart = ""
for event in self:
if isinstance(event, codraw_data.ObserveTruth):
assert scene is None, "Multiple ObserveTruth events not allowed in an episode"
scene = event.scene
elif isinstance(event, codraw_data.SelectClipart):
delayed_selected_clipart += abs_render.svg_from_cliparts([event.clipart], inline_images=False)
elif isinstance(event, codraw_data.DrawClipart):
res += delayed_selected_clipart
delayed_selected_clipart = ""
res += abs_render.svg_from_cliparts([event.clipart], color='red', inline_images=False)
elif isinstance(event, codraw_data.DrawGroup):
res += delayed_selected_clipart
delayed_selected_clipart = ""
res += abs_render.svg_from_cliparts(event.cliparts, color='red', inline_images=False)
elif isinstance(event, codraw_data.TellGroup):
res += f"<p>TELLER: {event.msg}</p>"
elif isinstance(event, codraw_data.ReplyGroup):
res += f"<p>DRAWER: {event.msg}</p>"
elif isinstance(event, codraw_data.TellerIntention):
if event.drawn is not None:
res += abs_render.svg_from_cliparts(event.drawn, color='purple', label='drawn', scale=0.33)
if event.draw_next is not None:
res += abs_render.svg_from_cliparts(event.draw_next, color='yellow', label='draw next', scale=0.33)
if event.undrawn is not None:
res += abs_render.svg_from_cliparts(event.undrawn, color='cyan', label='undrawn', scale=0.33)
res += f"<p>===</p>"
reconstructed_scene = self.reconstruct()
res += abs_render.svg_from_cliparts(scene, label='ground truth', inline_images=False)
res += abs_render.svg_from_cliparts(reconstructed_scene, color='red', label='reconstructed', inline_images=False)
res += f"<p>Similarity = {scene_similarity(reconstructed_scene, scene)}</p>"
return res
def write_html(self, name_or_path):
if isinstance(name_or_path, Path):
path = name_or_path
else:
path = Path(f"./renders/{name_or_path}.html").resolve()
assert not path.exists(), "File already exists!"
assert path.parent.exists(), "Parent directory does not exist"
path.write_text(self.to_html())
def get_true_scene(self):
scene = None
for event in self:
if isinstance(event, codraw_data.ObserveTruth):
assert scene is None, "Multiple ObserveTruth events not allowed in an episode"
scene = event.scene
assert scene is not None, "Episode has no ObserveTruth events"
return scene
def scene_similarity(self):
return scene_similarity(self.reconstruct(), self.get_true_scene())
@classmethod
def run(cls, scene, fns):
episode = cls([codraw_data.ObserveTruth(scene)])
while True:
for fn in fns:
if type(episode[-1]) in fn._trigger_types:
old_len = len(episode)
fn(episode)
if len(episode) == old_len:
return episode
break
else:
assert False, f"No response for event: {type(episode[-1]).__name__}"
@classmethod
def run_script(cls, scene_and_script, fns):
scene, script = scene_and_script
episode = cls([codraw_data.ObserveTruth(scene)])
episode.script = script
episode.script_index = 0
while True:
for fn in fns:
if type(episode[-1]) in fn._trigger_types:
old_len = len(episode)
fn(episode)
if len(episode) == old_len:
return episode
break
else:
assert False, f"No response for event: {type(episode[-1]).__name__}"
def respond_to(*event_types):
types = set([(x if issubclass(x, codraw_data.Event) else None) for x in event_types])
assert None not in types, "Invalid event type in decorator"
def deco(fn):
if hasattr(fn, '_trigger_types'):
fn._trigger_types |= types
else:
fn._trigger_types = types
return fn
return deco
def response_partial(fn, *args, **kwargs):
res = functools.partial(fn, *args, **kwargs)
res._trigger_types = fn._trigger_types
return res
class Transcriber:
def __init__(self, filename, scenes=None, scenes_description="", scenes_and_scripts=None):
self.filename = filename
if scenes is not None:
self.scene_data = scenes
self.use_script = False
else:
self.scene_data = scenes_and_scripts
self.use_script = True
self.scenes_description = scenes_description
def __call__(self, name_or_path, description="", **partition_to_fns):
if isinstance(name_or_path, Path):
path = name_or_path
else:
path = Path(f"./renders/{name_or_path}.html").resolve()
assert not path.exists(), "File already exists!"
assert path.parent.exists(), "Parent directory does not exist"
assert isinstance(description, str)
res = ""
res += f"<p>Filename: {self.filename}</p>"
res += f"<p>Scenes: {self.scenes_description}</p>"
res += f"<p>Started: {datetime.datetime.now()}</p>"
res += f"<p>Description: {description}</p>"
for partition, fns in partition_to_fns.items():
res += f"<p></p>"
res += f"<h2>Partition {partition}</h2>"
for i, scene_datum in enumerate(self.scene_data):
res += f'<h3 id="{partition}_{i}">Scene {i} <a href="#{partition}_{i}">[here]</a></h3>'
if not self.use_script:
res += Episode.run(scene_datum, fns).to_html()
else:
res += Episode.run_script(scene_datum, fns).to_html()
path.write_text(res)