-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_utils.py
198 lines (121 loc) · 6.05 KB
/
test_utils.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
"""
Test utils
"""
import os
import cv2
import numpy as np
import pandas as pd
import yaml
from pylithics.src.read_and_process import read_image, detect_lithic, process_image, find_lithic_contours, find_arrows
from pylithics.src.utils import mask_image, contour_characterization, classify_distributions, shape_detection, \
get_high_level_parent_and_hierarchy, pixulator, classify_surfaces, subtract_masked_image, measure_vertices, \
get_angles, \
measure_arrow_angle, contour_selection
# Global loads for all tests
image_array = read_image(os.path.join('tests', 'test_images'), 'test')
filename_config = os.path.join('tests', 'test_config.yml')
# Read YAML file
with open(filename_config, 'r') as config_file:
config_file = yaml.load(config_file)
config_file['conversion_px'] = 0.1 # hardcoded for now
def test_mask_image():
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
contours_cv, hierarchy = cv2.findContours(binary_edge_sobel, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cont = np.asarray([i[0] for i in list(contours_cv)[1]])
masked_image = mask_image(image_array, cont)
assert masked_image.shape != (0, 0)
assert masked_image.sum() < binary_edge_sobel.sum()
def test_contour_characterization():
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
contours_cv, hierarchy = cv2.findContours(binary_edge_sobel, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cont = np.asarray([i[0] for i in list(contours_cv)[2]])
cont_info = contour_characterization(image_array, cont, config_file['conversion_px'])
assert cont_info['length'] == 2965
assert cont_info['area_px'] == 490095.5
assert cont_info['height_px'] == 1339
assert cont_info['width_px'] == 539
assert cont_info['centroid'] == (930.1856303869774, 1362.474110977076)
assert cont_info['area_mm'] == 4901.0
assert cont_info['width_mm'] == 53.9
assert cont_info['height_mm'] == 133.9
assert cont_info['polygon_count'] == 7
def test_classify_distributions():
image_processed = process_image(image_array, config_file)
is_narrow = classify_distributions(image_processed)
assert is_narrow == True
def test_get_high_level_parent_and_hierarchy():
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
contours_cv, hierarchy = cv2.findContours(binary_edge_sobel, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
parent_index, hierarchy_level = get_high_level_parent_and_hierarchy(list(hierarchy)[0])
assert len(parent_index) != 0
assert len(hierarchy_level) != 0
def test_pixulator():
image_scale_array = read_image(os.path.join('tests', 'test_images'), 'sc_1')
conversion = pixulator(image_scale_array, 5)
assert conversion == 0.00423728813559322
def test_classify_surfaces():
# initial processing of the image
image_processed = process_image(image_array, config_file)
# processing to detect lithic and scars
binary_array, threshold_value = detect_lithic(image_processed, config_file)
# find contours
contours = find_lithic_contours(binary_array, config_file)
surfaces_classification = classify_surfaces(contours)
assert surfaces_classification == {0: 'Ventral', 1: 'Dorsal', 2: 'Lateral', 3: 'Platform'}
def test_subtract_masked_image():
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
contours_cv, hierarchy = cv2.findContours(binary_edge_sobel, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cont = np.asarray([i[0] for i in list(contours_cv)[1]])
rows, columns = subtract_masked_image(mask_image(image_array, cont))
assert len(rows) == 1835
assert len(columns) == 1659
def test_get_angles():
# initial processing of the image
image_processed = process_image(image_array, config_file)
# get the templates for the arrows
templates = find_arrows(image_array, image_processed)
# measure angles for existing arrows
arrow_df = get_angles(templates)
assert arrow_df.shape[0] == 4
assert arrow_df.shape[1] == 2
def test_contour_selection():
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
contours_cv, hierarchy = cv2.findContours(image_array, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
new_contours = []
cont_info_list = []
for index, cont in enumerate(list(contours_cv), start=0):
cont = np.asarray([i[0] for i in cont])
# calculate character listings of the contour.
cont_info = contour_characterization(image_array, cont, config_file['conversion_px'])
cont_info['index'] = index
cont_info['hierarchy'] = list(hierarchy)[0][index]
cont_info['contour'] = cont
new_contours.append(cont)
cont_info_list.append(cont_info)
df_cont_info = pd.DataFrame.from_dict(cont_info_list)
df_cont_info['parent_index'], df_cont_info['hierarchy_level'] = get_high_level_parent_and_hierarchy(
df_cont_info['hierarchy'].values)
indexes = contour_selection(df_cont_info)
assert len(indexes) > 1
assert len(indexes) < 20
def test_measure_arrow_angle():
# initial processing of the image
image_processed = process_image(image_array, config_file)
# get the templates for the arrows
templates = find_arrows(image_array, image_processed)
angle = measure_arrow_angle(templates[0])
# TODO: Once the angle measurement is fixed we need to change this test to the actual value.
assert angle != 0.0
def test_measure_vertices():
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
contours_cv, hierarchy = cv2.findContours(binary_edge_sobel, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cont = np.asarray([i[0] for i in list(contours_cv)[1]])
vertices, approx = measure_vertices(cont)
assert vertices == 6
def test_shape_detection():
config_file['conversion_px'] = 0.1 # hardcoded for now
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
contours_cv, hierarchy = cv2.findContours(binary_edge_sobel, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cont = np.asarray([i[0] for i in list(contours_cv)[2]])
shape = shape_detection(cont)
assert shape == ('arrow', 4)