-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rep_gen_test.py
232 lines (191 loc) · 9.12 KB
/
rep_gen_test.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
# Copyright (C) 2020 and later: Unicode, Inc. and others.
# License & terms of use: http://www.unicode.org/copyright.html
import numpy as np
import os
import random
import shutil
import string
import time
import unittest
from unittest.mock import MagicMock, patch
import sys
sys.modules['tensorflow'] = MagicMock()
sys.modules['dataset_builder'] = MagicMock()
sys.modules['model_builder'] = MagicMock()
from rep_gen import RepresentationGenerator
import dataset_builder as mock_custom_dataset
import model_builder as mock_custom_model
class TestRepresentationGenerator(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Test data
cls.tmp_dir = '.tmp' + str(time.time())
cls.config_file = os.path.join(cls.tmp_dir, 'config.ini')
# Build temporary testing directory
print("Building temporary directory {}.".format(cls.tmp_dir))
os.mkdir(cls.tmp_dir)
# Build temporary config file
print("Building temporary source file {}.".format(cls.config_file))
with open(cls.config_file, 'w+') as f:
f.write('[GOOMBAS]\n')
f.write('TOAD = PRINCESS PEACH\n')
@classmethod
def tearDownClass(cls):
print("Deleting temporary directory and file for testing.")
shutil.rmtree(cls.tmp_dir)
def test_default_init(self):
"""Test default initialization. When default initialization value
changes, or any private attribute does not match public attribute, this
test will fail."""
# Assert rg._config_path and rg._out_dir are initialized correctly
rg = RepresentationGenerator()
self.assertEqual(rg._config_path, 'configs/sample_config.ini')
self.assertEqual(rg._out_dir, 'embeddings')
# Assert DatasetBuilder and ModelBuilder is created during
# initialization.
mock_custom_dataset.DatasetBuilder.assert_called_with(
config_path='configs/sample_config.ini', one_hot=False)
mock_custom_model.ModelBuilder.assert_called_with(
config_path='configs/sample_config.ini'
)
# Assert that model builder is used for getting self._model
rg._model_builder.get_encoder.assert_called()
self.assertEqual(rg._model, rg._model_builder.get_encoder())
def test_config_path_setter(self):
# Test setter in initialization
rg = RepresentationGenerator(config_path=self.config_file)
self.assertEqual(rg._config_path, self.config_file)
# Test setter after initialization
rg = RepresentationGenerator()
rg.config_path = self.config_file
self.assertEqual(rg._config_path, self.config_file)
# Test exception
with self.assertRaises(ValueError):
RepresentationGenerator(os.path.join(self.config_file,'123'))
with self.assertRaises(ValueError):
rg.config_path = os.path.join(self.config_file,'123')
def test_out_dir_setter(self):
# Test setter in initialization
rg = RepresentationGenerator(out_dir=self.tmp_dir)
self.assertEqual(rg._out_dir, self.tmp_dir)
# Test setter after initialization
rg = RepresentationGenerator()
rg.out_dir = self.tmp_dir
self.assertEqual(rg._out_dir, self.tmp_dir)
def test_get_embeddings(self):
# Assert that dataset_builder
rg = RepresentationGenerator()
# Get mocked filename dataset
mock_img = MagicMock()
mock_label = MagicMock()
mock_ds = [(mock_img, mock_label)]
# Mock function get_filename_dataset.
with patch.object(rg._dataset_builder, 'get_filename_dataset',
return_value=mock_ds) as get_fd:
codepoints, embeddings = rg.get_embeddings(img_dir=self.tmp_dir)
get_fd.assert_called_with(self.tmp_dir)
# Assert prediction is made on mock image
rg._model.predict.assert_called_with(mock_img)
# Assert labels (filenames) are handled correctly
self.assertEqual(mock_label.numpy()[0].decode('utf-8').split('.')[0],
codepoints[0])
# Assert embeddings are result of self._model.predict
self.assertEqual(rg._model.predict(mock_img)[0], embeddings[0])
def test_get_embeddings_filename(self):
"""Test filename extraction process (from tf.Tensor to Str). Tensor
cannot contain string type content, any string by be of bytes type."""
# Assert that dataset_builder
rg = RepresentationGenerator()
# Get mocked filename dataset
mock_img = MagicMock()
mock_label = MagicMock()
mock_ds = [(mock_img, mock_label)]
# Get fake Tensor content
letters = string.ascii_uppercase
filename = ''.join(random.choice(letters) for _ in range(10))
tensor_content = [str.encode(filename + ".format")]
# Mock function get_filename_dataset.
with patch.object(rg._dataset_builder, 'get_filename_dataset',
return_value=mock_ds) as get_fd,\
patch.object(mock_label, 'numpy', return_value=tensor_content):
codepoints, _ = rg.get_embeddings(img_dir=self.tmp_dir)
self.assertEqual(codepoints[0], filename)
def test_write_embeddings_from_image(self):
# Mock inputs
img_dir, out_file, char_as_label = MagicMock(), MagicMock(), MagicMock()
# Mock codepoints and embeddings
codepoints, embeddings = MagicMock(), MagicMock()
rg = RepresentationGenerator()
# Mock two functions: rg.get_embeddings and
# rg.write_embeddings_from_list
with patch.object(rg, 'get_embeddings',
return_value=(codepoints, embeddings)) as get_emb, \
patch.object(rg, 'write_embeddings_from_list') as w_emb_f_l:
rg.write_embeddings_from_image(img_dir=img_dir, out_file=out_file,
char_as_label=char_as_label)
get_emb.assert_called_with(img_dir=img_dir)
w_emb_f_l.assert_called_once_with(codepoints, embeddings, out_file,
char_as_label)
def test_write_embeddings_from_list(self):
# Use reasonable inputs
codepoints = ['U+4e00_additional_info', 'U+4e01_additional_info']
embeddings = [[random.random(), random.random()],
[random.random(), random.random()]]
filename = 'test' + str(time.time())
vec_file = os.path.join(self.tmp_dir, filename + '_vec.tsv')
meta_file = os.path.join(self.tmp_dir, filename + '_meta.tsv')
# Assert that:
# 1. Output files are directed to out_dir
# 2. Output files have the correct names
# 3. Output files have the correct format
# 4. Output files have the correct value
# Make sure no vec_file exists in self.tmp_dir
if os.path.isfile(vec_file):
os.remove(vec_file)
if os.path.isfile(meta_file):
os.remove(meta_file)
# Write codepoints and embeddings to file
rg = RepresentationGenerator(out_dir=self.tmp_dir)
rg.write_embeddings_from_list(codepoints, embeddings, out_file=filename,
char_as_label=False)
# Read newly generated vec file
output_embs = np.genfromtxt(fname=vec_file, delimiter="\t")
# Read newly generated meta file
with open(meta_file,'r') as f_in:
output_chars = [label.strip() for label in f_in.readlines()]
# Check vec file content
self.assertTrue(np.array_equal(embeddings, output_embs))
# Check meta file content
self.assertTrue(codepoints == output_chars)
os.remove(vec_file)
os.remove(meta_file)
def test_write_embeddings_from_list_output_char(self):
# Use reasonable inputs
codepoints = ['U+4e00_additional_info', 'U+4e01_additional_info']
embeddings = [[random.random(), random.random()],
[random.random(), random.random()]]
filename = 'test' + str(time.time())
vec_file = os.path.join(self.tmp_dir, filename + '_vec.tsv')
meta_file = os.path.join(self.tmp_dir, filename + '_meta.tsv')
# Make sure no vec_file exists in self.tmp_dir
if os.path.isfile(vec_file):
os.remove(vec_file)
if os.path.isfile(meta_file):
os.remove(meta_file)
# Write codepoints and embeddings to file
rg = RepresentationGenerator(out_dir=self.tmp_dir)
rg.write_embeddings_from_list(codepoints, embeddings, out_file=filename,
char_as_label=True)
# Read newly generated vec file
output_embs = np.genfromtxt(fname=vec_file, delimiter="\t")
# Read newly generated meta file
with open(meta_file, 'r') as f_in:
output_chars = [label.strip() for label in f_in.readlines()]
# Check vec file content
self.assertTrue(np.array_equal(embeddings, output_embs))
self.assertFalse(codepoints == output_chars)
self.assertTrue(output_chars == ['一', '丁'])
os.remove(vec_file)
os.remove(meta_file)
if __name__ == "__main__":
unittest.main(verbosity=2)