Skip to content

Commit 21f7b6c

Browse files
committed
Merge branch 'main' into feat/object-oriented-api
2 parents 5cd9eb5 + be468c3 commit 21f7b6c

4 files changed

Lines changed: 78 additions & 3 deletions

File tree

datamint/api/dto/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
"BoxGeometry",
1414
"LineGeometry",
1515
"CoordinateSystem"
16+
"LineGeometry",
17+
"CoordinateSystem"
1618
]

datamint/api/endpoints/annotations_api.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def create(self,
383383

384384
annotations = [annotation_dto] if isinstance(annotation_dto, CreateAnnotationDto) else annotation_dto
385385
annotations = [ann.to_dict() if isinstance(ann, CreateAnnotationDto) else ann for ann in annotations]
386-
resource_id = resource.id if isinstance(resource, Resource) else resource
386+
resource_id = self._entid(resource)
387387
respdata = self._make_request('POST',
388388
f'{self.endpoint_base}/{resource_id}/annotations',
389389
json=annotations).json()
@@ -786,6 +786,37 @@ def _numpy_to_bytesio_png(seg_imgs: np.ndarray) -> Generator[BinaryIO, None, Non
786786
img_bytes.seek(0)
787787
yield img_bytes
788788

789+
def create_image_classification(self,
790+
resource: str | Resource,
791+
identifier: str,
792+
value: str,
793+
imported_from: str | None = None,
794+
model_id: str | None = None,
795+
) -> str:
796+
"""
797+
Create an image-level classification annotation.
798+
799+
Args:
800+
resource: The resource unique id or Resource instance.
801+
identifier: The annotation identifier/label.
802+
value: The classification value.
803+
imported_from: The imported from source value.
804+
model_id: The model unique id.
805+
806+
Returns:
807+
The id of the created annotation.
808+
"""
809+
annotation_dto = CreateAnnotationDto(
810+
type=AnnotationType.CATEGORY,
811+
identifier=identifier,
812+
scope='image',
813+
value=value,
814+
imported_from=imported_from,
815+
model_id=model_id
816+
)
817+
818+
return self.create(resource, annotation_dto)
819+
789820
def add_line_annotation(self,
790821
point1: tuple[int, int] | tuple[float, float, float],
791822
point2: tuple[int, int] | tuple[float, float, float],

datamint/api/endpoints/resources_api.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,12 @@ def set_tags(self,
988988
resource: str | Resource,
989989
tags: Sequence[str],
990990
):
991+
"""
992+
Set tags for a resource, IMPORTANT: This replaces all existing tags.
993+
Args:
994+
resource: The resource unique id or Resource object.
995+
tags: The tags to set.
996+
"""
991997
data = {'tags': tags}
992998
resource_id = self._entid(resource)
993999

@@ -1010,3 +1016,39 @@ def set_tags(self,
10101016
# resource._ensure_attr('projects')
10111017
# proj_ids = [p['id'] for p in resource.projects]
10121018
# return [proj for proj in self.projects_api.get_all() if proj.id in proj_ids]
1019+
1020+
def add_tags(self,
1021+
resource: str | Resource,
1022+
tags: Sequence[str],
1023+
):
1024+
"""
1025+
Add tags to a resource, IMPORTANT: This appends to existing tags.
1026+
Args:
1027+
resource: The resource unique id or Resource object.
1028+
tags: The tags to add.
1029+
"""
1030+
if isinstance(resource, str):
1031+
resource = self.get_by_id(resource)
1032+
old_tags = resource.tags if resource.tags is not None else []
1033+
return self.set_tags(resource, old_tags + list(tags))
1034+
1035+
def bulk_delete(self, entities: Sequence[str | Resource]) -> None:
1036+
"""Delete multiple entities. Faster than deleting them one by one.
1037+
1038+
Args:
1039+
entities: Sequence of unique identifiers for the entities to delete or the entity instances themselves.
1040+
1041+
Raises:
1042+
httpx.HTTPStatusError: If deletion fails or any entity not found
1043+
"""
1044+
from math import ceil
1045+
1046+
resources_ids = [self._entid(ent) for ent in entities]
1047+
if len(resources_ids) == 0:
1048+
return
1049+
batch_size = 200
1050+
for i in range(0, ceil(len(resources_ids)/batch_size)):
1051+
batch_ids = resources_ids[i*batch_size:(i+1)*batch_size]
1052+
self._make_request('DELETE',
1053+
f'{self.endpoint_base}',
1054+
params={'resource_ids': ','.join(batch_ids)})

datamint/api/entity_base_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class DeletableEntityApi(EntityBaseApi[T]):
189189
retrieval and restoration of such entities.
190190
"""
191191

192-
def delete(self, entity: str | BaseEntity) -> None:
192+
def delete(self, entity: str | T) -> None:
193193
"""Delete an entity.
194194
195195
Args:
@@ -200,7 +200,7 @@ def delete(self, entity: str | BaseEntity) -> None:
200200
"""
201201
self._make_entity_request('DELETE', entity)
202202

203-
def bulk_delete(self, entities: Sequence[str | BaseEntity]) -> None:
203+
def bulk_delete(self, entities: Sequence[str | T]) -> None:
204204
"""Delete multiple entities.
205205
206206
Args:

0 commit comments

Comments
 (0)