|
1
|
|
|
import pytest |
|
2
|
|
|
import numpy as np |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
@pytest.fixture |
|
6
|
|
|
def image_operations(): |
|
7
|
|
|
from artificial_artwork.image.image_operations import reshape_image, subtract, noisy, convert_to_uint8 |
|
8
|
|
|
return type('Ops', (), { |
|
9
|
|
|
'reshape': reshape_image, |
|
10
|
|
|
'subtract': subtract, |
|
11
|
|
|
'noisy': noisy, |
|
12
|
|
|
'convert_to_uint8': convert_to_uint8, |
|
13
|
|
|
}) |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
@pytest.mark.parametrize('test_image', [ |
|
17
|
|
|
([[11,12,13], [21, 22, 23]]), |
|
18
|
|
|
]) |
|
19
|
|
|
def test_image_reshape(test_image, image_operations): |
|
20
|
|
|
math_array = np.array(test_image, dtype=np.float32) |
|
21
|
|
|
image = image_operations.reshape(math_array, (1,) + math_array.shape) |
|
22
|
|
|
assert image.shape == (1,) + math_array.shape |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
@pytest.mark.parametrize('test_image, array', [ |
|
26
|
|
|
([[11,12,13], [21, 22, 23]], [[1, 2, 3], [4, 5, 6]]), |
|
27
|
|
|
]) |
|
28
|
|
|
def test_subtract_image(test_image, array, image_operations): |
|
29
|
|
|
math_array_1 = np.array(test_image, dtype=np.float32) |
|
30
|
|
|
math_array_2 = np.array(array, dtype=np.float32) |
|
31
|
|
|
|
|
32
|
|
|
image = image_operations.subtract(math_array_1, math_array_2) |
|
33
|
|
|
assert image.shape == math_array_1.shape |
|
34
|
|
|
assert image.tolist() == [ |
|
35
|
|
|
[10, 10, 10], |
|
36
|
|
|
[17, 17, 17] |
|
37
|
|
|
] |
|
38
|
|
|
|
|
39
|
|
|
@pytest.mark.parametrize('test_image, array', [ |
|
40
|
|
|
([[11,12,13], [21, 22, 23]], [[1, 2], [4, 5]]), |
|
41
|
|
|
]) |
|
42
|
|
|
def test_wrong_subtract(test_image, array, image_operations): |
|
43
|
|
|
from artificial_artwork.image.image_operations import ShapeMissmatchError |
|
44
|
|
|
with pytest.raises(ShapeMissmatchError): |
|
45
|
|
|
math_array_1 = np.array(test_image, dtype=np.float32) |
|
46
|
|
|
math_array_2 = np.array(array, dtype=np.float32) |
|
47
|
|
|
image_operations.subtract(math_array_1, math_array_2) |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
@pytest.mark.parametrize('test_image, ratio', [ |
|
52
|
|
|
([[11,12,13], [21, 22, 23]], 0), |
|
53
|
|
|
([[11,12,13], [21, 22, 23]], 0.6), |
|
54
|
|
|
([[11,12,13], [21, 22, 23]], 1), |
|
55
|
|
|
]) |
|
56
|
|
|
def test_noisy(test_image, ratio, image_operations): |
|
57
|
|
|
math_array = np.array(test_image, dtype=np.float32) |
|
58
|
|
|
min_pixel_value = np.min(math_array) |
|
59
|
|
|
max_pixel_value = np.max(math_array) |
|
60
|
|
|
image = image_operations.noisy(math_array, ratio) |
|
61
|
|
|
assert (image <= max(max_pixel_value, 20)).all() |
|
62
|
|
|
assert (min(min_pixel_value, -20) <= image).all() |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
@pytest.mark.parametrize('test_image, ratio', [ |
|
67
|
|
|
([[11, 12], [21, 22]], 1.1), |
|
68
|
|
|
([[12, 13], [21, 23]], -0.2), |
|
69
|
|
|
]) |
|
70
|
|
|
def test_wrong_noisy_ratio(test_image, ratio, image_operations): |
|
71
|
|
|
from artificial_artwork.image.image_operations import InvalidRatioError |
|
72
|
|
|
math_array = np.array(test_image, dtype=np.float32) |
|
73
|
|
|
with pytest.raises(InvalidRatioError): |
|
74
|
|
|
image = image_operations.noisy(math_array, ratio) |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
# UINT8 CONVERTION TESTS |
|
78
|
|
|
@pytest.mark.parametrize('test_image, expected_image', [ |
|
79
|
|
|
( |
|
80
|
|
|
[[1.2, 9.1], |
|
81
|
|
|
[10, 3]], |
|
82
|
|
|
|
|
83
|
|
|
[[0, 229], |
|
84
|
|
|
[255, 52]] |
|
85
|
|
|
), |
|
86
|
|
|
|
|
87
|
|
|
( |
|
88
|
|
|
[[1, 1], |
|
89
|
|
|
[3, 5]], |
|
90
|
|
|
|
|
91
|
|
|
[[0, 0], |
|
92
|
|
|
[127, 255]] |
|
93
|
|
|
), |
|
94
|
|
|
|
|
95
|
|
|
( |
|
96
|
|
|
[[1, 1], |
|
97
|
|
|
[1, 1]], |
|
98
|
|
|
|
|
99
|
|
|
[[1, 1], |
|
100
|
|
|
[1, 1]] |
|
101
|
|
|
), |
|
102
|
|
|
|
|
103
|
|
|
]) |
|
104
|
|
|
def test_uint8_convertion(test_image, expected_image, image_operations): |
|
105
|
|
|
runtime_image = image_operations.convert_to_uint8(np.array(test_image, dtype=np.float32)) |
|
106
|
|
|
assert runtime_image.dtype == np.uint8 |
|
107
|
|
|
assert 0 <= np.nanmin(runtime_image) |
|
108
|
|
|
assert np.nanmax(runtime_image) < np.power(2.0, 8) |
|
109
|
|
|
assert runtime_image.tolist() == expected_image |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
@pytest.mark.parametrize('test_image', [ |
|
113
|
|
|
( |
|
114
|
|
|
[[np.nan, np.nan], |
|
115
|
|
|
[np.nan, np.nan]], |
|
116
|
|
|
), |
|
117
|
|
|
|
|
118
|
|
|
( |
|
119
|
|
|
[[1, -float('inf')], |
|
120
|
|
|
[2, 3]], |
|
121
|
|
|
), |
|
122
|
|
|
|
|
123
|
|
|
]) |
|
124
|
|
|
def test_non_finite_minimum_value(test_image, image_operations): |
|
125
|
|
|
with pytest.raises(ValueError, match=r'Minimum image value is not finite'): |
|
126
|
|
|
runtime_image = image_operations.convert_to_uint8(np.array(test_image, dtype=np.float32)) |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
@pytest.mark.parametrize('test_image', [ |
|
130
|
|
|
( |
|
131
|
|
|
[[1, float('inf')], |
|
132
|
|
|
[2, 3]], |
|
133
|
|
|
), |
|
134
|
|
|
]) |
|
135
|
|
|
def test_non_finite_maximum_value(test_image, image_operations): |
|
136
|
|
|
with pytest.raises(ValueError, match=r'Maximum image value is not finite'): |
|
137
|
|
|
runtime_image = image_operations.convert_to_uint8(np.array(test_image, dtype=np.float32)) |
|
138
|
|
|
|