77from nucleus .constants import EXPORTED_ROWS
88from nucleus .dataset_item import DatasetItem
99from nucleus .job import AsyncJob
10- from nucleus .utils import convert_export_payload , format_dataset_item_response
10+ from nucleus .utils import (
11+ KeyErrorDict ,
12+ convert_export_payload ,
13+ format_dataset_item_response ,
14+ )
1115
1216
1317class Slice :
@@ -41,6 +45,7 @@ def __init__(self, slice_id: str, client):
4145 self .id = slice_id
4246 self ._slice_id = slice_id
4347 self ._client = client
48+ self ._name = None
4449 self ._dataset_id = None
4550
4651 def __repr__ (self ):
@@ -52,6 +57,30 @@ def __eq__(self, other):
5257 return True
5358 return False
5459
60+ def _fetch_all (self ) -> dict :
61+ """Retrieves info and all items of the Slice.
62+
63+ Returns:
64+ A dict mapping keys to the corresponding info retrieved.
65+ ::
66+
67+ {
68+ "name": Union[str, int],
69+ "slice_id": str,
70+ "dataset_id": str,
71+ "dataset_items": List[{
72+ "id": str,
73+ "metadata": Dict[str, Union[str, int, float]],
74+ "ref_id": str,
75+ "original_image_url": str
76+ }]
77+ }
78+ """
79+ response = self ._client .make_request (
80+ {}, f"slice/{ self .id } " , requests_command = requests .get
81+ )
82+ return response
83+
5584 @property
5685 def slice_id (self ):
5786 warnings .warn (
@@ -60,33 +89,45 @@ def slice_id(self):
6089 )
6190 return self ._slice_id
6291
92+ @property
93+ def name (self ):
94+ """The name of the Slice."""
95+ if self ._name is None :
96+ self ._name = self .info ()["name" ]
97+ return self ._name
98+
6399 @property
64100 def dataset_id (self ):
65101 """The ID of the Dataset to which the Slice belongs."""
66102 if self ._dataset_id is None :
67- self .info ()
103+ self ._dataset_id = self . info ()[ "dataset_id" ]
68104 return self ._dataset_id
69105
106+ @property
107+ def items (self ):
108+ """All DatasetItems contained in the Slice."""
109+ return self ._fetch_all ()["dataset_items" ]
110+
70111 def info (self ) -> dict :
71- """Retrieves info and items of the Slice.
112+ """Retrieves the name, slice_id, and dataset_id of the Slice.
72113
73114 Returns:
74115 A dict mapping keys to the corresponding info retrieved.
75116 ::
76117
77118 {
78119 "name": Union[str, int],
120+ "slice_id": str,
79121 "dataset_id": str,
80- "dataset_items": List[{
81- "id": str,
82- "metadata": Dict[str, Union[str, int, float]],
83- "ref_id": str,
84- "original_image_url": str
85- }]
86122 }
87123 """
88- info = self ._client .slice_info (self .id )
89- self ._dataset_id = info ["dataset_id" ]
124+ info = KeyErrorDict (
125+ items = "The 'items' key is now deprecated for Slice.info. Use Slice.items instead."
126+ )
127+ res = self ._client .make_request (
128+ {}, f"slice/{ self .id } /info" , requests_command = requests .get
129+ )
130+ info .update (res )
90131 return info
91132
92133 def append (
@@ -137,11 +178,10 @@ def items_and_annotation_generator(
137178 }
138179 }]
139180 """
140- info = self .info ()
141- for item_metadata in info ["dataset_items" ]:
181+ for item_metadata in self .items :
142182 yield format_dataset_item_response (
143183 self ._client .dataitem_loc (
144- dataset_id = info [ " dataset_id" ] ,
184+ dataset_id = self . dataset_id ,
145185 dataset_item_id = item_metadata ["id" ],
146186 )
147187 )
@@ -259,14 +299,12 @@ def check_annotations_are_in_slice(
259299 1. True if all Annotations are in the Slice, False otherwise;
260300 2. List of reference IDs not in the Slice.
261301 """
262- info = slice_to_check .info ()
263-
264302 reference_ids_not_found_in_slice = {
265303 annotation .reference_id
266304 for annotation in annotations
267305 if annotation .reference_id is not None
268306 }.difference (
269- {item_metadata ["ref_id" ] for item_metadata in info [ "dataset_items" ] }
307+ {item_metadata ["ref_id" ] for item_metadata in slice_to_check . items }
270308 )
271309 if reference_ids_not_found_in_slice :
272310 annotations_are_in_slice = False
0 commit comments