Skip to content

Commit 944ed30

Browse files
authored
Add CatalogSlice object and function to export data row IDs by slice
2 parents 0dfb3f4 + d9083a8 commit 944ed30

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

docs/source/index.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ DataRowMetadata
173173

174174
.. automodule:: labelbox.schema.data_row_metadata
175175
:members:
176-
:show-inheritance:
176+
:show-inheritance:
177177

178178
AnnotationImport
179179
----------------------------
180180

181181
.. automodule:: labelbox.schema.annotation_import
182182
:members:
183-
:show-inheritance:
183+
:show-inheritance:
184184

185185
Batch
186186
----------------------------
@@ -194,4 +194,19 @@ ResourceTag
194194

195195
.. automodule:: labelbox.schema.resource_tag
196196
:members:
197-
:show-inheritance:
197+
:show-inheritance:
198+
199+
Slice
200+
-----------------------------------------
201+
202+
.. automodule:: labelbox.schema.slice
203+
:members: Slice
204+
:exclude-members: CatalogSlice
205+
:show-inheritance:
206+
207+
CatalogSlice
208+
-----------------------------------------
209+
.. automodule:: labelbox.schema.slice
210+
:members: CatalogSlice
211+
:exclude-members: Slice
212+
:show-inheritance:

labelbox/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
from labelbox.schema.resource_tag import ResourceTag
2828
from labelbox.schema.project_resource_tag import ProjectResourceTag
2929
from labelbox.schema.media_type import MediaType
30+
from labelbox.schema.slice import Slice, CatalogSlice

labelbox/client.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from labelbox.schema.user import User
3434
from labelbox.schema.project import Project
3535
from labelbox.schema.role import Role
36+
from labelbox.schema.slice import CatalogSlice
3637

3738
from labelbox.schema.media_type import MediaType
3839

@@ -963,7 +964,7 @@ def assign_global_keys_to_data_rows(
963964
timeout_seconds=60) -> Dict[str, Union[str, List[Any]]]:
964965
"""
965966
Assigns global keys to data rows.
966-
967+
967968
Args:
968969
A list of dicts containing data_row_id and global_key.
969970
Returns:
@@ -1211,3 +1212,27 @@ def _format_failed_rows(rows: List[str],
12111212
"Timed out waiting for get_data_rows_for_global_keys job to complete."
12121213
)
12131214
time.sleep(sleep_time)
1215+
1216+
def get_catalog_slice(self, slice_id) -> CatalogSlice:
1217+
"""
1218+
Fetches a Catalog Slice by ID.
1219+
1220+
Args:
1221+
slice_id (str): The ID of the Slice
1222+
Returns:
1223+
CatalogSlice
1224+
"""
1225+
query_str = """
1226+
query getSavedQueryPyApi($id: ID!) {
1227+
getSavedQuery(id: $id) {
1228+
id
1229+
name
1230+
description
1231+
filter
1232+
createdAt
1233+
updatedAt
1234+
}
1235+
}
1236+
"""
1237+
res = self.execute(query_str, {'id': slice_id})
1238+
return Entity.CatalogSlice(self, res['getSavedQuery'])

labelbox/orm/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ class Entity(metaclass=EntityMeta):
377377
ProjectRole: Type[labelbox.ProjectRole]
378378
Project: Type[labelbox.Project]
379379
Batch: Type[labelbox.Batch]
380+
CatalogSlice: Type[labelbox.CatalogSlice]
380381

381382
@classmethod
382383
def _attributes_of_type(cls, attr_type):

labelbox/schema/slice.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from labelbox.orm.db_object import DbObject
2+
from labelbox.orm.model import Field
3+
from labelbox.pagination import PaginatedCollection
4+
5+
6+
class Slice(DbObject):
7+
"""
8+
A Slice is a saved set of filters (saved query).
9+
This is an abstract class and should not be instantiated.
10+
11+
Attributes:
12+
name (datetime)
13+
description (datetime)
14+
created_at (datetime)
15+
updated_at (datetime)
16+
filter (json)
17+
"""
18+
name = Field.String("name")
19+
description = Field.String("description")
20+
created_at = Field.DateTime("created_at")
21+
updated_at = Field.DateTime("updated_at")
22+
filter = Field.Json("filter")
23+
24+
25+
class CatalogSlice(Slice):
26+
"""
27+
Represents a Slice used for filtering data rows in Catalog.
28+
"""
29+
30+
def get_data_row_ids(self) -> PaginatedCollection:
31+
"""
32+
Fetches all data row ids that match this Slice
33+
34+
Returns:
35+
A PaginatedCollection of data row ids
36+
"""
37+
query_str = """
38+
query getDataRowIdsBySavedQueryPyApi($id: ID!, $from: String, $first: Int!) {
39+
getDataRowIdsBySavedQuery(input: {
40+
savedQueryId: $id,
41+
after: $from
42+
first: $first
43+
}) {
44+
totalCount
45+
nodes
46+
pageInfo {
47+
endCursor
48+
hasNextPage
49+
}
50+
}
51+
}
52+
"""
53+
return PaginatedCollection(
54+
client=self.client,
55+
query=query_str,
56+
params={'id': self.uid},
57+
dereferencing=['getDataRowIdsBySavedQuery', 'nodes'],
58+
obj_class=lambda _, data_row_id: data_row_id,
59+
cursor_path=['getDataRowIdsBySavedQuery', 'pageInfo', 'endCursor'])

0 commit comments

Comments
 (0)