Skip to content

Commit c7fc676

Browse files
author
Claire Pajot
committed
Added api support for adding a taxonomy and an accompanying test
1 parent fb9b452 commit c7fc676

5 files changed

Lines changed: 73 additions & 0 deletions

File tree

nucleus/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
construct_box_predictions_payload,
129129
construct_model_creation_payload,
130130
construct_segmentation_payload,
131+
construct_taxonomy_payload,
131132
)
132133
from .prediction import (
133134
BoxPrediction,
@@ -1249,6 +1250,27 @@ def create_image_index(self, dataset_id: str):
12491250
requests_command=requests.post,
12501251
)
12511252

1253+
def add_taxonomy(
1254+
self,
1255+
dataset_id: str,
1256+
taxonomy_name: str,
1257+
taxonomy_type: str,
1258+
labels: List[str],
1259+
):
1260+
"""
1261+
Creates a new taxonomy.
1262+
Returns a response with dataset_id, taxonomy_name and type for the new taxonomy.
1263+
:param dataset_id: id of dataset to add the taxonomy to
1264+
:param taxonomy_name: name of the taxonomy
1265+
:param type: type of the taxonomy
1266+
:param labels: list of possible labels for the taxonomy
1267+
"""
1268+
return self.make_request(
1269+
construct_taxonomy_payload(taxonomy_name, taxonomy_type, labels),
1270+
f"dataset/{dataset_id}/add_taxonomy",
1271+
requests_command=requests.post,
1272+
)
1273+
12521274
def make_request(
12531275
self, payload: dict, route: str, requests_command=requests.post
12541276
) -> dict:

nucleus/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
MASK_TYPE = "mask"
99
SEGMENTATION_TYPE = "segmentation"
1010
CUBOID_TYPE = "cuboid"
11+
CATEGORY_TYPE = "category"
12+
MULTICATEGORY_TYPE = "multicategory"
1113
ANNOTATION_TYPES = (BOX_TYPE, POLYGON_TYPE, SEGMENTATION_TYPE, CUBOID_TYPE)
14+
TAXONOMY_TYPES = (CATEGORY_TYPE, MULTICATEGORY_TYPE)
1215
ANNOTATION_UPDATE_KEY = "update"
1316
AUTOTAGS_KEY = "autotags"
1417
EXPORTED_ROWS = "exportedRows"
@@ -58,6 +61,7 @@
5861
JOB_TYPE_KEY = "job_type"
5962
JOB_CREATION_TIME_KEY = "job_creation_time"
6063
LABEL_KEY = "label"
64+
LABELS_KEY = "labels"
6165
MASK_URL_KEY = "mask_url"
6266
MESSAGE_KEY = "message"
6367
METADATA_KEY = "metadata"
@@ -83,6 +87,7 @@
8387
STATUS_CODE_KEY = "status_code"
8488
STATUS_KEY = "status"
8589
SUCCESS_STATUS_CODES = [200, 201, 202]
90+
TAXONOMY_NAME_KEY = "taxonomy_name"
8691
TYPE_KEY = "type"
8792
UPDATED_ITEMS = "updated_items"
8893
UPDATE_KEY = "update"

nucleus/dataset.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,13 @@ def create_image_index(self):
407407
response = self._client.create_image_index(self.id)
408408
return AsyncJob.from_json(response, self._client)
409409

410+
def add_taxonomy(
411+
self, taxonomy_name: str, taxonomy_type: str, labels: List[str]
412+
):
413+
return self._client.add_taxonomy(
414+
self.id, taxonomy_name, taxonomy_type, labels
415+
)
416+
410417
def check_index_status(self, job_id: str):
411418
return self._client.check_index_status(job_id)
412419

nucleus/payload_constructor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
MODEL_ID_KEY,
2626
ANNOTATION_METADATA_SCHEMA_KEY,
2727
SEGMENTATIONS_KEY,
28+
TAXONOMY_NAME_KEY,
29+
TYPE_KEY,
30+
LABELS_KEY,
2831
)
2932

3033

@@ -126,3 +129,13 @@ def construct_model_run_creation_payload(
126129
METADATA_KEY: metadata if metadata else {},
127130
ANNOTATION_METADATA_SCHEMA_KEY: annotation_metadata_schema,
128131
}
132+
133+
134+
def construct_taxonomy_payload(
135+
taxonomy_name: str, taxonomy_type: str, labels: List[str]
136+
) -> dict:
137+
return {
138+
TAXONOMY_NAME_KEY: taxonomy_name,
139+
TYPE_KEY: taxonomy_type,
140+
LABELS_KEY: labels,
141+
}

tests/test_taxonomy.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
from .helpers import (
4+
TEST_DATASET_NAME,
5+
)
6+
7+
8+
@pytest.fixture()
9+
def dataset(CLIENT):
10+
ds = CLIENT.create_dataset(TEST_DATASET_NAME)
11+
yield ds
12+
13+
response = CLIENT.delete_dataset(ds.id)
14+
assert response == {"message": "Beginning dataset deletion..."}
15+
16+
17+
def test_add_taxonomy(dataset):
18+
response = dataset.add_taxonomy(
19+
"[Pytest] taxonomy",
20+
"category",
21+
["[Pytest] taxonomy label 1", "[Pytest] taxonomy label 2"],
22+
)
23+
24+
assert response["dataset_id"] == dataset.id
25+
assert response["taxonomy_name"] == "[Pytest] taxonomy"
26+
assert response["type"] == "category"

0 commit comments

Comments
 (0)