|
1 | | -from __future__ import annotations |
2 | | - |
3 | 1 | from typing import List, Iterable, Set, Tuple, Optional |
4 | 2 | from nucleus.dataset_item import DatasetItem |
5 | 3 | from nucleus.annotation import Annotation |
| 4 | +from nucleus.utils import format_dataset_item_response |
6 | 5 |
|
7 | | -from .constants import DEFAULT_ANNOTATION_UPDATE_MODE |
8 | | - |
9 | | - |
10 | | -def check_annotations_are_in_slice( |
11 | | - annotations: List[Annotation], slice_to_check: Slice |
12 | | -) -> Tuple[bool, Set[str], Set[str]]: |
13 | | - """Check membership of the annotation targets within this slice. |
14 | | -
|
15 | | - annotations: Annnotations with ids referring to targets. |
16 | | - slice: The slice to check against. |
17 | | - """ |
18 | | - info = slice_to_check.info() |
19 | | - item_ids_not_found_in_slice = { |
20 | | - annotation.item_id |
21 | | - for annotation in annotations |
22 | | - if annotation.item_id is not None |
23 | | - }.difference({item_metadata["id"] for item_metadata in info}) |
24 | | - reference_ids_not_found_in_slice = { |
25 | | - annotation.reference_id |
26 | | - for annotation in annotations |
27 | | - if annotation.reference_id is not None |
28 | | - }.difference({item_metadata["reference_id"] for item_metadata in info}) |
29 | | - if item_ids_not_found_in_slice or reference_ids_not_found_in_slice: |
30 | | - annotations_are_in_slice = False |
31 | | - else: |
32 | | - annotations_are_in_slice = True |
33 | | - |
34 | | - return ( |
35 | | - annotations_are_in_slice, |
36 | | - item_ids_not_found_in_slice, |
37 | | - reference_ids_not_found_in_slice, |
38 | | - ) |
| 6 | +from .constants import DEFAULT_ANNOTATION_UPDATE_MODE, ITEM_KEY |
39 | 7 |
|
40 | 8 |
|
41 | 9 | class Slice: |
@@ -106,13 +74,15 @@ def append( |
106 | 74 | return response |
107 | 75 |
|
108 | 76 | def items_generator(self) -> Iterable[DatasetItem]: |
109 | | - """Returns an iterable of DatasetItems in this slice.""" |
| 77 | + """Returns an iterable of DatasetItem/Annotation dicts.""" |
110 | 78 | info = self.info() |
111 | 79 | for item_metadata in info["dataset_items"]: |
112 | | - yield self._client.dataitem_loc( |
113 | | - dataset_id=info["dataset_id"], |
114 | | - dataset_item_id=item_metadata["id"], |
115 | | - ) |
| 80 | + yield format_dataset_item_response( |
| 81 | + self._client.dataitem_loc( |
| 82 | + dataset_id=info["dataset_id"], |
| 83 | + dataset_item_id=item_metadata["id"], |
| 84 | + ) |
| 85 | + )[ITEM_KEY] |
116 | 86 |
|
117 | 87 | def items(self) -> List[DatasetItem]: |
118 | 88 | """Returns a list of all DatasetItems in this slice.""" |
@@ -152,3 +122,34 @@ def annotate( |
152 | 122 | update=update, |
153 | 123 | batch_size=batch_size, |
154 | 124 | ) |
| 125 | + |
| 126 | + |
| 127 | +def check_annotations_are_in_slice( |
| 128 | + annotations: List[Annotation], slice_to_check: Slice |
| 129 | +) -> Tuple[bool, Set[str], Set[str]]: |
| 130 | + """Check membership of the annotation targets within this slice. |
| 131 | +
|
| 132 | + annotations: Annnotations with ids referring to targets. |
| 133 | + slice: The slice to check against. |
| 134 | + """ |
| 135 | + info = slice_to_check.info() |
| 136 | + item_ids_not_found_in_slice = { |
| 137 | + annotation.item_id |
| 138 | + for annotation in annotations |
| 139 | + if annotation.item_id is not None |
| 140 | + }.difference({item_metadata["id"] for item_metadata in info}) |
| 141 | + reference_ids_not_found_in_slice = { |
| 142 | + annotation.reference_id |
| 143 | + for annotation in annotations |
| 144 | + if annotation.reference_id is not None |
| 145 | + }.difference({item_metadata["reference_id"] for item_metadata in info}) |
| 146 | + if item_ids_not_found_in_slice or reference_ids_not_found_in_slice: |
| 147 | + annotations_are_in_slice = False |
| 148 | + else: |
| 149 | + annotations_are_in_slice = True |
| 150 | + |
| 151 | + return ( |
| 152 | + annotations_are_in_slice, |
| 153 | + item_ids_not_found_in_slice, |
| 154 | + reference_ids_not_found_in_slice, |
| 155 | + ) |
0 commit comments