-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_annotation.py
More file actions
320 lines (253 loc) · 10.2 KB
/
Copy pathtest_annotation.py
File metadata and controls
320 lines (253 loc) · 10.2 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
import pytest
from .helpers import (
TEST_DATASET_NAME,
TEST_IMG_URLS,
TEST_BOX_ANNOTATIONS,
TEST_POLYGON_ANNOTATIONS,
TEST_SEGMENTATION_ANNOTATIONS,
reference_id_from_url,
assert_box_annotation_matches_dict,
assert_polygon_annotation_matches_dict,
assert_segmentation_annotation_matches_dict,
)
from nucleus import (
BoxAnnotation,
PolygonAnnotation,
SegmentationAnnotation,
DatasetItem,
Segment,
Point,
)
from nucleus.constants import ERROR_PAYLOAD
def test_reprs():
# Have to define here in order to have access to all relevant objects
def test_repr(test_object: any):
assert eval(str(test_object)) == test_object
[
test_repr(SegmentationAnnotation.from_json(_))
for _ in TEST_SEGMENTATION_ANNOTATIONS
]
[test_repr(BoxAnnotation.from_json(_)) for _ in TEST_BOX_ANNOTATIONS]
[
test_repr(PolygonAnnotation.from_json(_))
for _ in TEST_POLYGON_ANNOTATIONS
]
@pytest.fixture()
def dataset(CLIENT):
ds = CLIENT.create_dataset(TEST_DATASET_NAME)
ds_items = []
for url in TEST_IMG_URLS:
ds_items.append(
DatasetItem(
image_location=url,
reference_id=reference_id_from_url(url),
)
)
response = ds.append(ds_items)
assert ERROR_PAYLOAD not in response.json()
yield ds
response = CLIENT.delete_dataset(ds.id)
assert response == {"message": "Beginning dataset deletion..."}
def test_box_gt_upload(dataset):
annotation = BoxAnnotation(**TEST_BOX_ANNOTATIONS[0])
response = dataset.annotate(annotations=[annotation])
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 1
assert response["annotations_ignored"] == 0
response = dataset.refloc(annotation.reference_id)["annotations"]["box"]
assert len(response) == 1
response_annotation = response[0]
assert_box_annotation_matches_dict(
response_annotation, TEST_BOX_ANNOTATIONS[0]
)
def test_polygon_gt_upload(dataset):
annotation = PolygonAnnotation.from_json(TEST_POLYGON_ANNOTATIONS[0])
response = dataset.annotate(annotations=[annotation])
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 1
assert response["annotations_ignored"] == 0
response = dataset.refloc(annotation.reference_id)["annotations"][
"polygon"
]
assert len(response) == 1
response_annotation = response[0]
assert_polygon_annotation_matches_dict(
response_annotation, TEST_POLYGON_ANNOTATIONS[0]
)
def test_single_semseg_gt_upload(dataset):
annotation = SegmentationAnnotation.from_json(
TEST_SEGMENTATION_ANNOTATIONS[0]
)
response = dataset.annotate(annotations=[annotation])
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 1
assert response["annotations_ignored"] == 0
response_annotation = dataset.refloc(annotation.reference_id)[
"annotations"
]["segmentation"][0]
assert_segmentation_annotation_matches_dict(
response_annotation, TEST_SEGMENTATION_ANNOTATIONS[0]
)
def test_batch_semseg_gt_upload(dataset):
annotations = [
SegmentationAnnotation.from_json(ann)
for ann in TEST_SEGMENTATION_ANNOTATIONS
]
response = dataset.annotate(annotations=annotations)
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 5
assert response["annotations_ignored"] == 0
def test_batch_semseg_gt_upload_ignore(dataset):
# First upload annotations
annotations = [
SegmentationAnnotation.from_json(ann)
for ann in TEST_SEGMENTATION_ANNOTATIONS
]
response = dataset.annotate(annotations=annotations)
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 5
assert response["annotations_ignored"] == 0
# When we re-upload, expect them to be ignored
response = dataset.annotate(annotations=annotations)
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 0
assert response["annotations_ignored"] == 5
def test_batch_semseg_gt_upload_update(dataset):
# First upload annotations
annotations = [
SegmentationAnnotation.from_json(ann)
for ann in TEST_SEGMENTATION_ANNOTATIONS
]
response = dataset.annotate(annotations=annotations)
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 5
assert response["annotations_ignored"] == 0
# When we re-upload, expect uploads to be processed
response = dataset.annotate(annotations=annotations, update=True)
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 5
assert response["annotations_ignored"] == 0
def test_mixed_annotation_upload(dataset):
# First upload annotations
semseg_annotations = [
SegmentationAnnotation.from_json(ann)
for ann in TEST_SEGMENTATION_ANNOTATIONS
]
bbox_annotations = [BoxAnnotation(**(ann)) for ann in TEST_BOX_ANNOTATIONS]
annotations = bbox_annotations + semseg_annotations
response = dataset.annotate(annotations=annotations)
assert response["dataset_id"] == dataset.id
assert response["annotations_processed"] == 10
assert response["annotations_ignored"] == 0
response_annotations = dataset.refloc(bbox_annotations[0].reference_id)[
"annotations"
]
assert len(response_annotations) == 2
assert len(response_annotations["box"]) == 1
assert "segmentation" in response_annotations
def test_box_gt_upload_update(dataset):
annotation = BoxAnnotation(**TEST_BOX_ANNOTATIONS[0])
response = dataset.annotate(annotations=[annotation])
assert response["annotations_processed"] == 1
# Copy so we don't modify the original.
annotation_update_params = dict(TEST_BOX_ANNOTATIONS[1])
annotation_update_params["annotation_id"] = TEST_BOX_ANNOTATIONS[0][
"annotation_id"
]
annotation_update_params["reference_id"] = TEST_BOX_ANNOTATIONS[0][
"reference_id"
]
annotation_update = BoxAnnotation(**annotation_update_params)
response = dataset.annotate(annotations=[annotation_update], update=True)
assert response["annotations_processed"] == 1
assert response["annotations_ignored"] == 0
response = dataset.refloc(annotation.reference_id)["annotations"]["box"]
assert len(response) == 1
response_annotation = response[0]
assert_box_annotation_matches_dict(
response_annotation, annotation_update_params
)
def test_box_gt_upload_ignore(dataset):
annotation = BoxAnnotation(**TEST_BOX_ANNOTATIONS[0])
print(annotation)
response = dataset.annotate(annotations=[annotation])
assert response["annotations_processed"] == 1
# Copy so we don't modify the original.
annotation_update_params = dict(TEST_BOX_ANNOTATIONS[1])
annotation_update_params["annotation_id"] = TEST_BOX_ANNOTATIONS[0][
"annotation_id"
]
annotation_update_params["reference_id"] = TEST_BOX_ANNOTATIONS[0][
"reference_id"
]
annotation_update = BoxAnnotation(**annotation_update_params)
# Default behavior is ignore.
response = dataset.annotate(annotations=[annotation_update])
assert response["annotations_processed"] == 0
assert response["annotations_ignored"] == 1
response = dataset.refloc(annotation.reference_id)["annotations"]["box"]
assert len(response) == 1
response_annotation = response[0]
assert_box_annotation_matches_dict(
response_annotation, TEST_BOX_ANNOTATIONS[0]
)
def test_polygon_gt_upload_update(dataset):
annotation = PolygonAnnotation.from_json(TEST_POLYGON_ANNOTATIONS[0])
response = dataset.annotate(annotations=[annotation])
assert response["annotations_processed"] == 1
# Copy so we don't modify the original.
annotation_update_params = dict(TEST_POLYGON_ANNOTATIONS[1])
annotation_update_params["annotation_id"] = TEST_POLYGON_ANNOTATIONS[0][
"annotation_id"
]
annotation_update_params["reference_id"] = TEST_POLYGON_ANNOTATIONS[0][
"reference_id"
]
annotation_update = PolygonAnnotation.from_json(annotation_update_params)
response = dataset.annotate(annotations=[annotation_update], update=True)
assert response["annotations_processed"] == 1
assert response["annotations_ignored"] == 0
response = dataset.refloc(annotation.reference_id)["annotations"][
"polygon"
]
assert len(response) == 1
response_annotation = response[0]
assert_polygon_annotation_matches_dict(
response_annotation, annotation_update_params
)
def test_polygon_gt_upload_ignore(dataset):
annotation = PolygonAnnotation.from_json(TEST_POLYGON_ANNOTATIONS[0])
response = dataset.annotate(annotations=[annotation])
assert response["annotations_processed"] == 1
# Copy so we don't modify the original.
annotation_update_params = dict(TEST_POLYGON_ANNOTATIONS[1])
annotation_update_params["annotation_id"] = TEST_POLYGON_ANNOTATIONS[0][
"annotation_id"
]
annotation_update_params["reference_id"] = TEST_POLYGON_ANNOTATIONS[0][
"reference_id"
]
annotation_update = PolygonAnnotation.from_json(annotation_update_params)
# Default behavior is ignore.
response = dataset.annotate(annotations=[annotation_update])
assert response["annotations_processed"] == 0
assert response["annotations_ignored"] == 1
response = dataset.refloc(annotation.reference_id)["annotations"][
"polygon"
]
assert len(response) == 1
response_annotation = response[0]
assert_polygon_annotation_matches_dict(
response_annotation, TEST_POLYGON_ANNOTATIONS[0]
)
@pytest.mark.integration
def test_box_gt_deletion(dataset):
annotation = BoxAnnotation(**TEST_BOX_ANNOTATIONS[0])
print(annotation)
response = dataset.annotate(annotations=[annotation])
assert response["annotations_processed"] == 1
job = dataset.delete_annotations()
job.sleep_until_complete()
job_status = job.status()
assert job_status["status"] == "Completed"
assert job_status["job_id"] == job.id