Skip to content

Commit 65dab9c

Browse files
author
Claire Pajot
committed
Added tests for upload_update and upload_ignore
1 parent fc0b731 commit 65dab9c

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

nucleus/annotation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
POLYGON_TYPE,
2424
POSITION_KEY,
2525
REFERENCE_ID_KEY,
26+
TAXONOMY_NAME_KEY,
2627
TYPE_KEY,
2728
VERTICES_KEY,
2829
WIDTH_KEY,
@@ -319,9 +320,9 @@ def to_payload(self) -> dict:
319320
@dataclass
320321
class CategoryAnnotation(Annotation):
321322
label: str
323+
taxonomy_name: str
322324
reference_id: Optional[str] = None
323325
item_id: Optional[str] = None
324-
annotation_id: Optional[str] = None
325326
metadata: Optional[Dict] = None
326327

327328
def __post_init__(self):
@@ -330,22 +331,21 @@ def __post_init__(self):
330331

331332
@classmethod
332333
def from_json(cls, payload: dict):
333-
# TODO: Remove? geometry = payload.get(GEOMETRY_KEY, {})
334334
return cls(
335335
label=payload.get(LABEL_KEY, 0),
336+
taxonomy_name=payload.get(TAXONOMY_NAME_KEY, None),
336337
reference_id=payload.get(REFERENCE_ID_KEY, None),
337338
item_id=payload.get(DATASET_ITEM_ID_KEY, None),
338-
annotation_id=payload.get(ANNOTATION_ID_KEY, None),
339339
metadata=payload.get(METADATA_KEY, {}),
340340
)
341341

342342
def to_payload(self) -> dict:
343343
return {
344344
LABEL_KEY: self.label,
345+
TAXONOMY_NAME_KEY: self.taxonomy_name,
345346
TYPE_KEY: CATEGORY_TYPE,
346347
GEOMETRY_KEY: {},
347348
REFERENCE_ID_KEY: self.reference_id,
348-
ANNOTATION_ID_KEY: self.annotation_id,
349349
METADATA_KEY: self.metadata,
350350
}
351351

nucleus/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
STATUS_CODE_KEY = "status_code"
8585
STATUS_KEY = "status"
8686
SUCCESS_STATUS_CODES = [200, 201, 202]
87+
TAXONOMY_NAME_KEY = "taxonomy_name"
8788
TYPE_KEY = "type"
8889
UPDATED_ITEMS = "updated_items"
8990
UPDATE_KEY = "update"

tests/helpers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def reference_id_from_url(url):
162162
{
163163
"label": f"[Pytest] Category Annotation ${i}",
164164
"reference_id": reference_id_from_url(TEST_IMG_URLS[i]),
165-
"annotation_id": f"[Pytest] Category Annotation Annotation Id{i}",
165+
"taxonomy_name": f"[Pytest] Category Taxonomy{i}",
166166
}
167167
for i in range(len(TEST_IMG_URLS))
168168
]
@@ -278,9 +278,8 @@ def assert_category_annotation_matches_dict(
278278
annotation_instance, annotation_dict
279279
):
280280
assert annotation_instance.label == annotation_dict["label"]
281-
282281
assert (
283-
annotation_instance.annotation_id == annotation_dict["annotation_id"]
282+
annotation_instance.taxonomy_name == annotation_dict["taxonomy_name"]
284283
)
285284

286285

tests/test_annotation.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def dataset(CLIENT):
6666

6767
def test_box_gt_upload(dataset):
6868
annotation = BoxAnnotation(**TEST_BOX_ANNOTATIONS[0])
69+
print(annotation)
6970
response = dataset.annotate(annotations=[annotation])
71+
print(response)
7072

7173
assert response["dataset_id"] == dataset.id
7274
assert response["annotations_processed"] == 1
@@ -324,6 +326,63 @@ def test_polygon_gt_upload_ignore(dataset):
324326
response_annotation, TEST_POLYGON_ANNOTATIONS[0]
325327
)
326328

329+
330+
def test_category_gt_upload_update(dataset):
331+
annotation = CategoryAnnotation.from_json(TEST_CATEGORY_ANNOTATIONS[0])
332+
response = dataset.annotate(annotations=[annotation])
333+
334+
assert response["annotations_processed"] == 1
335+
336+
# Copy so we don't modify the original.
337+
annotation_update_params = dict(TEST_CATEGORY_ANNOTATIONS[1])
338+
annotation_update_params["reference_id"] = TEST_CATEGORY_ANNOTATIONS[0][
339+
"reference_id"
340+
]
341+
342+
annotation_update = CategoryAnnotation.from_json(annotation_update_params)
343+
response = dataset.annotate(annotations=[annotation_update], update=True)
344+
345+
assert response["annotations_processed"] == 1
346+
assert response["annotations_ignored"] == 0
347+
348+
response = dataset.refloc(annotation.reference_id)["annotations"][
349+
"category"
350+
]
351+
assert len(response) == 1
352+
response_annotation = response[0]
353+
assert_category_annotation_matches_dict(
354+
response_annotation, annotation_update_params
355+
)
356+
357+
358+
def test_category_gt_upload_ignore(dataset):
359+
annotation = CategoryAnnotation.from_json(TEST_CATEGORY_ANNOTATIONS[0])
360+
response = dataset.annotate(annotations=[annotation])
361+
362+
assert response["annotations_processed"] == 1
363+
364+
# Copy so we don't modify the original.
365+
annotation_update_params = dict(TEST_CATEGORY_ANNOTATIONS[1])
366+
annotation_update_params["reference_id"] = TEST_CATEGORY_ANNOTATIONS[0][
367+
"reference_id"
368+
]
369+
370+
annotation_update = CategoryAnnotation.from_json(annotation_update_params)
371+
# Default behavior is ignore.
372+
response = dataset.annotate(annotations=[annotation_update])
373+
374+
assert response["annotations_processed"] == 0
375+
assert response["annotations_ignored"] == 1
376+
377+
response = dataset.refloc(annotation.reference_id)["annotations"][
378+
"category"
379+
]
380+
assert len(response) == 1
381+
response_annotation = response[0]
382+
assert_category_annotation_matches_dict(
383+
response_annotation, TEST_CATEGORY_ANNOTATIONS[0]
384+
)
385+
327386
@pytest.mark.integration
328387
def test_box_gt_deletion(dataset):
329388
annotation = BoxAnnotation(**TEST_BOX_ANNOTATIONS[0])

0 commit comments

Comments
 (0)