forked from PAIR-code/facets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallelize_test.py
195 lines (151 loc) · 7.13 KB
/
parallelize_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
"""Tests for parallize."""
import os
from absl import logging
from absl.testing import absltest
from PIL import Image
from PIL import ImageFile
import parallelize
import convert
TESTDATA_DIR = 'testdata'
class ParallelizeTests(absltest.TestCase):
def setUp(self):
self.testdata_dir = os.path.join(os.getcwd(), TESTDATA_DIR)
# Ensure setting is set to default for each test.
try:
ImageFile.LOAD_TRUNCATED_IMAGES = False
except AttributeError as e:
logging.warning('Are you using PILLOW and not a very old version of PIL? '
'Unable to force load of truncated image files: %s', e)
def testGetAndConvertOneImageFromLocalFile(self):
# Test parallelization of a single image succeeds.
testfile_path = os.path.join(self.testdata_dir,
'Googleplex-Patio-Aug-2014.JPG')
expected_output_image_size = (100, 100)
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_image, status = parallelize.get_and_convert_image(
testfile_path, image_convert_settings)
del status # linter
self.assertSameElements(output_image.size, expected_output_image_size)
def testGetAndConvertOneImageMissingFile(self):
# Returns None if can't open file.
testfile_path = os.path.join(self.testdata_dir,
'file_doesnt_exist.png')
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_image, status = parallelize.get_and_convert_image(
testfile_path, image_convert_settings)
del status # linter
self.assertEqual(output_image, None)
def testGetAndConvertOneImageNotImageFile(self):
# Returns None if can't open file.
testfile_path = os.path.join(self.testdata_dir,
'attributions.txt')
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_image, status = parallelize.get_and_convert_image(
testfile_path, image_convert_settings)
del status # linter
self.assertEqual(output_image, None)
def testGetAndConvertTruncatedImageFail(self):
# Should note fail but return None when PIL fails on truncated image.
# To test image truncation, we actually need to write a file to disk.
img_filepath = os.path.join(self.testdata_dir, 'test_img.png')
try:
orig_img = Image.new('RGBA', (500, 500))
orig_img.save(img_filepath)
filesize = os.path.getsize(img_filepath)
with open(img_filepath, 'r+') as img_on_disk:
img_on_disk.truncate(filesize - 100)
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_image_with_status = parallelize.get_and_convert_image(
img_filepath, image_convert_settings)
self.assertIsNone(output_image_with_status[0])
self.assertTrue(output_image_with_status[1]) # Has error message.
except:
raise
finally:
# Cleanup.
if os.path.isfile(img_filepath):
os.remove(img_filepath)
def testGetAndConvertAllowTruncatedImage(self):
# Should return a converted image if we tolerate truncated images.
# To test image truncation, we actually need to write a file to disk.
img_filepath = os.path.join(self.testdata_dir, 'test_img.png')
try:
orig_img = Image.new('RGBA', (500, 500))
orig_img.save(img_filepath)
filesize = os.path.getsize(img_filepath)
with open(img_filepath, 'r+') as img_on_disk:
img_on_disk.truncate(filesize - 100)
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_image_with_status = parallelize.get_and_convert_image(
img_filepath, image_convert_settings, allow_truncated_images=True)
self.assertEqual(output_image_with_status[0].size, (100, 100))
except:
raise
finally:
# Cleanup.
if os.path.isfile(img_filepath):
os.remove(img_filepath)
def testGetAndConvertOneImageBadUrl(self):
# Returns None if can't open file.
testfile_path = 'http://www.google.com'
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_image, status = parallelize.get_and_convert_image(
testfile_path, image_convert_settings)
del status # linter
self.assertEqual(output_image, None)
def testParallelizeConvertImagesFromLocalfile(self):
# Test parallelization with multiple reads of the same image file produces
# expected output images with correct sizes.
testfile_path = os.path.join(self.testdata_dir,
'Googleplex-Patio-Aug-2014.JPG')
testfile_locations = [testfile_path] * 3
expected_output_image_sizes = [(100, 100)] * 3
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_imgs_with_status = parallelize.get_and_convert_images_parallel(
testfile_locations, image_convert_settings, verbose=1)
resulting_image_sizes = []
for (image, status) in output_imgs_with_status:
del status # linter
resulting_image_sizes.append(image.size)
self.assertSameElements(resulting_image_sizes, expected_output_image_sizes)
def testParallelizeConvertWithFailures(self):
# 3 images are attempted, the last one should fail.
testfile_path = os.path.join(self.testdata_dir,
'Googleplex-Patio-Aug-2014.JPG')
bad_testfile_path = os.path.join(self.testdata_dir,
'attributions.txt')
testfile_locations = [testfile_path, testfile_path, bad_testfile_path]
expected_image_size = (100, 100)
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
output_images = parallelize.get_and_convert_images_parallel(
testfile_locations, image_convert_settings, verbose=1)
first_converted_img = output_images[0][0]
second_converted_img = output_images[1][0]
third_converted_img = output_images[2][0]
self.assertSameElements(first_converted_img.size, expected_image_size)
self.assertSameElements(second_converted_img.size, expected_image_size)
self.assertEqual(third_converted_img, None) # Failed conversion.
def testConvertDefaultImageSucceeds(self):
testfile_path = os.path.join(self.testdata_dir,
'Googleplex-Patio-Aug-2014.JPG')
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
img = parallelize.convert_default_image(testfile_path,
image_convert_settings)
self.assertEqual(img.size, (100, 100))
def testConvertDefaultImageFails(self):
testfile_path = os.path.join(self.testdata_dir, 'attributions.txt')
image_convert_settings = convert.ImageConvertSettings(
img_format='png', width=100, height=100)
with self.assertRaises(IOError):
parallelize.convert_default_image(testfile_path, image_convert_settings)
if __name__ == '__main__':
absltest.main()