Skip to content

Commit d653ba3

Browse files
authored
Merge pull request #119 from scaleapi/claire/add_taxonomy_api_support
API support for adding taxonomies
2 parents 8337d25 + 4429ebe commit d653ba3

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

nucleus/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
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)
1214
ANNOTATION_UPDATE_KEY = "update"
1315
AUTOTAGS_KEY = "autotags"
@@ -55,6 +57,7 @@
5557
JOB_TYPE_KEY = "job_type"
5658
JOB_CREATION_TIME_KEY = "job_creation_time"
5759
LABEL_KEY = "label"
60+
LABELS_KEY = "labels"
5861
MASK_URL_KEY = "mask_url"
5962
MESSAGE_KEY = "message"
6063
METADATA_KEY = "metadata"
@@ -80,6 +83,7 @@
8083
STATUS_CODE_KEY = "status_code"
8184
STATUS_KEY = "status"
8285
SUCCESS_STATUS_CODES = [200, 201, 202]
86+
TAXONOMY_NAME_KEY = "taxonomy_name"
8387
TYPE_KEY = "type"
8488
UPDATED_ITEMS = "updated_items"
8589
UPDATE_KEY = "update"

nucleus/dataset.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from .payload_constructor import (
3636
construct_append_scenes_payload,
3737
construct_model_run_creation_payload,
38+
construct_taxonomy_payload,
3839
)
3940

4041
WARN_FOR_LARGE_UPLOAD = 50000
@@ -393,6 +394,25 @@ def create_image_index(self):
393394
response = self._client.create_image_index(self.id)
394395
return AsyncJob.from_json(response, self._client)
395396

397+
def add_taxonomy(
398+
self,
399+
taxonomy_name: str,
400+
taxonomy_type: str,
401+
labels: List[str],
402+
):
403+
"""
404+
Creates a new taxonomy.
405+
Returns a response with dataset_id, taxonomy_name and type for the new taxonomy.
406+
:param taxonomy_name: name of the taxonomy
407+
:param type: type of the taxonomy
408+
:param labels: list of possible labels for the taxonomy
409+
"""
410+
return self._client.make_request(
411+
construct_taxonomy_payload(taxonomy_name, taxonomy_type, labels),
412+
f"dataset/{self.id}/add_taxonomy",
413+
requests_command=requests.post,
414+
)
415+
396416
def check_index_status(self, job_id: str):
397417
return self._client.check_index_status(job_id)
398418

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)