Skip to content

Commit 08c2f05

Browse files
author
Diego Ardila
committed
dataset export works
1 parent becb53b commit 08c2f05

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

nucleus/dataset.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from nucleus.job import AsyncJob
66
from nucleus.utils import (
7+
convert_export_payload,
78
format_dataset_item_response,
89
serialize_and_write_to_presigned_url,
910
)
@@ -327,3 +328,23 @@ def delete_custom_index(self):
327328

328329
def check_index_status(self, job_id: str):
329330
return self._client.check_index_status(job_id)
331+
332+
def items_and_annotations(
333+
self,
334+
) -> List[Dict[str, Union[DatasetItem, Dict[str, List[Annotation]]]]]:
335+
"""Returns a list of all DatasetItems and Annotations in this slice.
336+
337+
Returns:
338+
A list, where each item is a dict with two keys representing a row
339+
in the dataset.
340+
* One value in the dict is the DatasetItem, containing a reference to the
341+
item that was annotated.
342+
* The other value is a dictionary containing all the annotations for this
343+
dataset item, sorted by annotation type.
344+
"""
345+
api_payload = self._client.make_request(
346+
payload=None,
347+
route=f"dataset/{self.id}/exportForTraining",
348+
requests_command=requests.get,
349+
)
350+
return convert_export_payload(api_payload["exportedRows"])

tests/test_dataset.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import math
23
import os
34

@@ -21,6 +22,9 @@
2122
IGNORED_ITEMS,
2223
NEW_ITEMS,
2324
UPDATED_ITEMS,
25+
ITEM_KEY,
26+
ANNOTATIONS_KEY,
27+
BOX_TYPE,
2428
)
2529
from nucleus.job import AsyncJob, JobError
2630

@@ -331,3 +335,29 @@ def test_annotate_async_with_error(dataset: Dataset):
331335
}
332336

333337
assert "Item with id fake_garbage doesn" in str(job.errors())
338+
339+
340+
def test_append_and_export(dataset):
341+
# Dataset upload
342+
url = TEST_IMG_URLS[0]
343+
annotation = BoxAnnotation(**TEST_BOX_ANNOTATIONS[0])
344+
345+
ds_items = [
346+
DatasetItem(
347+
image_location=url,
348+
reference_id=reference_id_from_url(url),
349+
metadata={"test": "metadata"},
350+
),
351+
]
352+
response = dataset.append(ds_items)
353+
assert ERROR_PAYLOAD not in response.json()
354+
355+
dataset.annotate(annotations=[annotation])
356+
357+
expected_box_annotation = copy.deepcopy(annotation)
358+
expected_box_annotation.annotation_id = None
359+
expected_box_annotation.metadata = {}
360+
361+
exported = dataset.items_and_annotations()
362+
assert exported[0][ITEM_KEY] == ds_items[0]
363+
assert exported[0][ANNOTATIONS_KEY][BOX_TYPE][0] == expected_box_annotation

0 commit comments

Comments
 (0)