-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathcreate_data_test.py
More file actions
377 lines (336 loc) · 12.6 KB
/
create_data_test.py
File metadata and controls
377 lines (336 loc) · 12.6 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Tests for create_data.py."""
import copy
import json
import shutil
import tempfile
import unittest
from glob import glob
from os import path
import tensorflow as tf
from reddit import create_data
class CreateDataPipelineTest(unittest.TestCase):
"""Test running the pipeline end-to-end."""
def setUp(self):
self._temp_dir = tempfile.mkdtemp()
self.maxDiff = None
def tearDown(self):
shutil.rmtree(self._temp_dir)
def test_run(self):
with open("reddit/testdata/simple_thread.json") as f:
comments = json.loads(f.read())
# Duplicate the thread with a different ID, chosing a link_id that
# will be put in the test set.
test_comments = []
for comment in comments:
test_comment = copy.copy(comment)
test_comment['link_id'] = "t3_testthread"
test_comments.append(test_comment)
create_data.run(
argv=[
"--runner=DirectRunner",
"--reddit_table=ignored",
"--output_dir=" + self._temp_dir,
"--dataset_format=TF",
"--num_shards_test=2",
"--num_shards_train=2",
"--min_length=4",
"--max_length=5",
"--train_split=0.5",
],
comments=(comments + test_comments)
)
self.assertItemsEqual(
[path.join(self._temp_dir, expected_file) for expected_file in
["train-00000-of-00002.tfrecord",
"train-00001-of-00002.tfrecord"]],
glob(path.join(self._temp_dir, "train-*"))
)
self.assertItemsEqual(
[path.join(self._temp_dir, expected_file) for expected_file in
["test-00000-of-00002.tfrecord",
"test-00001-of-00002.tfrecord"]],
glob(path.join(self._temp_dir, "test-*"))
)
train_examples = self._read_examples("train-*")
expected_train_examples = [
self._create_example(
{
'context': "AAAA",
'context_author': "author-A",
'response': "BBBB",
'response_author': "author-B",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
}
),
self._create_example(
{
'context/0': "AAAA",
'context': "BBBB",
'context_author': "author-B",
'response': "CCCC",
'response_author': "author-C",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
}
),
self._create_example(
{
'context/0': "AAAA",
'context': "BBBB",
'context_author': "author-B",
'response': "DDDD",
'response_author': "author-D",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
}
),
self._create_example(
{
'context/1': "AAAA",
'context/0': "BBBB",
'context': "DDDD",
'context_author': "author-D",
'response': "EEEE",
'response_author': "author-E",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
}
),
]
self.assertItemsEqual(expected_train_examples, train_examples)
expected_test_examples = []
for example in expected_train_examples:
example.features.feature['thread_id'].bytes_list.value[0] = (
"testthread").encode("utf-8")
expected_test_examples.append(example)
test_examples = self._read_examples("test-*")
self.assertItemsEqual(expected_test_examples, test_examples)
def test_run_json(self):
with open("reddit/testdata/simple_thread.json") as f:
comments = json.loads(f.read())
# Duplicate the thread with a different ID, chosing a link_id that
# will be put in the test set.
test_comments = []
for comment in comments:
test_comment = copy.copy(comment)
test_comment['link_id'] = "t3_testthread"
test_comments.append(test_comment)
create_data.run(
argv=[
"--runner=DirectRunner",
"--reddit_table=ignored",
"--output_dir=" + self._temp_dir,
"--dataset_format=JSON",
"--num_shards_test=2",
"--num_shards_train=2",
"--min_length=4",
"--max_length=5",
"--train_split=0.5",
],
comments=(comments + test_comments)
)
self.assertItemsEqual(
[path.join(self._temp_dir, expected_file) for expected_file in
["train-00000-of-00002.json",
"train-00001-of-00002.json"]],
glob(path.join(self._temp_dir, "train-*"))
)
self.assertItemsEqual(
[path.join(self._temp_dir, expected_file) for expected_file in
["test-00000-of-00002.json",
"test-00001-of-00002.json"]],
glob(path.join(self._temp_dir, "test-*"))
)
train_examples = self._read_json_examples("train-*")
expected_train_examples = [
{
'context': "AAAA",
'context_author': "author-A",
'response': "BBBB",
'response_author': "author-B",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
},
{
'context/0': "AAAA",
'context': "BBBB",
'context_author': "author-B",
'response': "CCCC",
'response_author': "author-C",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
},
{
'context/0': "AAAA",
'context': "BBBB",
'context_author': "author-B",
'response': "DDDD",
'response_author': "author-D",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
},
{
'context/1': "AAAA",
'context/0': "BBBB",
'context': "DDDD",
'context_author': "author-D",
'response': "EEEE",
'response_author': "author-E",
'subreddit': "subreddit-A",
'thread_id': 'thread-A',
}
]
self.assertItemsEqual(expected_train_examples, train_examples)
expected_test_examples = []
for example in expected_train_examples:
example['thread_id'] = "testthread"
expected_test_examples.append(example)
test_examples = self._read_json_examples("test-*")
self.assertItemsEqual(expected_test_examples, test_examples)
def _read_examples(self, pattern):
examples = []
for file_name in glob(path.join(self._temp_dir, pattern)):
for record in tf.io.tf_record_iterator(file_name):
example = tf.train.Example()
example.ParseFromString(record)
examples.append(example)
return examples
def _read_json_examples(self, pattern):
examples = []
for file_name in glob(path.join(self._temp_dir, pattern)):
for line in open(file_name):
examples.append(json.loads(line))
return examples
@staticmethod
def _create_example(features):
example = tf.train.Example()
for feature_name, feature_value in features.items():
example.features.feature[feature_name].bytes_list.value.append(
feature_value.encode("utf-8"))
return example
class CreateDataTest(unittest.TestCase):
"""Test individual helper functions."""
def test_trim(self):
self.assertEqual(
"Matthew",
create_data.trim("Matthew Henderson", 7)
)
def test_trim_do_not_split_word(self):
self.assertEqual(
"Matthew ",
create_data.trim("Matthew Henderson", 9)
)
def test_trim_string_short(self):
self.assertEqual(
"Matthew",
create_data.trim("Matthew", 9)
)
def test_trim_long_word(self):
self.assertEqual(
"",
create_data.trim("Matthew", 2)
)
def test_normalise_comment(self):
comment = create_data.normalise_comment(
{
'body': "ABC EFG HIJ KLM NOP",
'score_hidden': None,
'archived': None,
'name': None,
'author_flair_text': None,
'downs': None,
'created_utc': "1520704245",
'subreddit_id': "t5_AAAAA",
'link_id': "t3_BBBBB",
'parent_id': "t1_CCCCC",
'score': "1",
'retrieved_on': "1525020075",
'controversiality': "0",
'gilded': "0",
'id': "DDDDD",
'subreddit': "EEEEE",
'author': "FFFFF",
'ups': None,
'distinguished': None,
'author_flair_css_class': None,
},
max_length=16)
self.assertEqual(
comment,
create_data.Comment(
body="ABC EFG HIJ KLM ",
thread_id="BBBBB",
parent_id="CCCCC",
id="DDDDD",
body_is_trimmed=True,
subreddit="EEEEE",
author="FFFFF",
)
)
def test_linear_paths(self):
with open("reddit/testdata/thread.json") as f:
comments = json.loads(f.read())
comments = [
create_data.normalise_comment(comment, max_length=127)
for comment in comments]
id_to_comment = {comment.id: comment for comment in comments}
paths = list(create_data.linear_paths(id_to_comment, parent_depth=100))
self.assertItemsEqual(
[["dvedzte", "dvfdfd4"], ["dvedzte", "dveh7r5"],
["dve3v95", "dvhjrkc"], ["dve3v95", "dvhjrkc", "dvhktmd"],
["dve3v95", "dvhjrkc", "dvhktmd", "dvhn7hh"],
["dve3v95", "dvhjrkc", "dvhktmd", "dvhn7hh", "dvhvg4m"]],
paths
)
@staticmethod
def _create_test_comment(id, parent_id):
return create_data.Comment(
body="body",
thread_id="thread_id",
parent_id=parent_id,
id=id,
body_is_trimmed=True,
subreddit="subreddit",
author="author",
)
def test_linear_paths_with_self_loop(self):
id_to_comment = {
"1": self._create_test_comment(id="1", parent_id="1"),
}
paths = list(create_data.linear_paths(id_to_comment, parent_depth=100))
self.assertEqual([], paths)
def test_linear_paths_with_loop(self):
id_to_comment = {
"1": self._create_test_comment(id="1", parent_id="3"),
"2": self._create_test_comment(id="2", parent_id="1"),
"3": self._create_test_comment(id="3", parent_id="2"),
}
paths = list(create_data.linear_paths(id_to_comment, parent_depth=100))
self.assertEqual([], paths)
def test_linear_paths_with_stranded_threads(self):
"""Check that it picks up threads whose parents are missing."""
id_to_comment = {
"1": self._create_test_comment(id="1", parent_id="unseen"),
"2": self._create_test_comment(id="2", parent_id="1"),
"3": self._create_test_comment(id="3", parent_id="unseen 2"),
"4": self._create_test_comment(id="4", parent_id="3"),
}
paths = list(create_data.linear_paths(id_to_comment, parent_depth=100))
self.assertItemsEqual([
["1", "2"],
["3", "4"],
], paths)
def test_long_thread(self):
"""Check there is no issue with long threads (e.g. recursion limits)"""
id_to_comment = {
i: self._create_test_comment(id=i, parent_id=i - 1)
for i in range(2000)
}
paths = list(create_data.linear_paths(id_to_comment, parent_depth=10))
self.assertItemsEqual([
range(max(i - 11, 0), i)
for i in range(2, 2001)
], paths)
if __name__ == "__main__":
unittest.main()