Skip to content

Commit 5183828

Browse files
Narek MkhitaryanNarek Mkhitaryan
authored andcommitted
added OCR groupe type validation in class creation
1 parent addc7bc commit 5183828

File tree

7 files changed

+105
-20
lines changed

7 files changed

+105
-20
lines changed

docs/source/api_reference/api_folder.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Folders
88
.. automethod:: superannotate.SAClient.get_folder_by_id
99
.. automethod:: superannotate.SAClient.get_folder_metadata
1010
.. automethod:: superannotate.SAClient.create_folder
11-
.. automethod:: superannotate.SAClient.delete_folders
11+
.. automethod:: superannotate.SAClient.delete_folders

src/superannotate/lib/core/usecases/classes.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from lib.core.conditions import CONDITION_EQ as EQ
77
from lib.core.entities import AnnotationClassEntity
88
from lib.core.entities import ProjectEntity
9+
from lib.core.entities.classes import GroupTypeEnum
910
from lib.core.enums import ProjectType
1011
from lib.core.exceptions import AppException
1112
from lib.core.serviceproviders import BaseServiceProvider
@@ -66,6 +67,13 @@ def validate_project_type(self):
6667
"Predefined tagging functionality is not supported for projects"
6768
f" of type {ProjectType.get_name(self._project.type)}."
6869
)
70+
if self._project.type != ProjectType.VECTOR:
71+
for g in self._annotation_class.attribute_groups:
72+
if g.group_type == GroupTypeEnum.OCR:
73+
raise AppException(
74+
f"OCR attribute group is not supported for project type "
75+
f"{ProjectType.get_name(self._project.type)}."
76+
)
6977

7078
def validate_default_value(self):
7179
if self._project.type == ProjectType.PIXEL.value and any(
@@ -109,13 +117,19 @@ def __init__(
109117
self._annotation_classes = annotation_classes
110118

111119
def validate_project_type(self):
112-
if self._project.type == ProjectType.PIXEL and any(
113-
[True for i in self._annotation_classes if i.type == "tag"]
114-
):
115-
raise AppException(
116-
f"Predefined tagging functionality is not supported"
117-
f" for projects of type {ProjectType.get_name(self._project.type)}."
118-
)
120+
if self._project.type != ProjectType.VECTOR:
121+
for c in self._annotation_classes:
122+
if self._project.type == ProjectType.PIXEL and c.type == "tag":
123+
raise AppException(
124+
f"Predefined tagging functionality is not supported"
125+
f" for projects of type {ProjectType.get_name(self._project.type)}."
126+
)
127+
for g in c.attribute_groups:
128+
if g.group_type == GroupTypeEnum.OCR:
129+
raise AppException(
130+
f"OCR attribute group is not supported for project type "
131+
f"{ProjectType.get_name(self._project.type)}."
132+
)
119133

120134
def validate_default_value(self):
121135
if self._project.type == ProjectType.PIXEL.value:

tests/integration/classes/test_classes_serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ def test_group_type_wrong_arg(self):
7878
"'radio',",
7979
"'checklist',",
8080
"'numeric',",
81-
"'text'",
81+
"'text',",
8282
"'ocr'",
8383
] == wrap_error(e).split()

tests/integration/classes/test_create_annotation_class.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import tempfile
33

44
import pytest
5+
from lib.core.entities.classes import AttributeGroup
56
from src.superannotate import AppException
67
from src.superannotate import SAClient
78
from tests import DATA_SET_PATH
@@ -245,6 +246,73 @@ def test_create_annotation_class(self):
245246
"Predefined tagging functionality is not supported for projects of type Video.",
246247
)
247248

249+
def test_create_annotation_class_via_ocr_group_type(self):
250+
with self.assertRaisesRegexp(
251+
AppException,
252+
f"OCR attribute group is not supported for project type {self.PROJECT_TYPE}.",
253+
):
254+
attribute_groups = [
255+
AttributeGroup(
256+
**{
257+
"id": 21448,
258+
"class_id": 56820,
259+
"name": "Large",
260+
"group_type": "ocr",
261+
"is_multiselect": 0,
262+
"createdAt": "2020-09-29T10:39:39.000Z",
263+
"updatedAt": "2020-09-29T10:39:39.000Z",
264+
"attributes": [],
265+
}
266+
)
267+
]
268+
sa.create_annotation_class(
269+
self.PROJECT_NAME,
270+
"test_add",
271+
"#FF0000",
272+
attribute_groups,
273+
class_type="tag",
274+
)
275+
276+
def test_create_annotation_class_via_json_and_ocr_group_type(self):
277+
with tempfile.TemporaryDirectory() as tmpdir_name:
278+
temp_path = f"{tmpdir_name}/new_classes.json"
279+
with open(temp_path, "w") as new_classes:
280+
new_classes.write(
281+
"""
282+
[
283+
{
284+
"id":56820,
285+
"project_id":7617,
286+
"name":"Personal vehicle",
287+
"color":"#547497",
288+
"count":18,
289+
"createdAt":"2020-09-29T10:39:39.000Z",
290+
"updatedAt":"2020-09-29T10:48:18.000Z",
291+
"type": "tag",
292+
"attribute_groups":[
293+
{
294+
"id":21448,
295+
"class_id":56820,
296+
"name":"Large",
297+
"group_type": "ocr",
298+
"is_multiselect":0,
299+
"createdAt":"2020-09-29T10:39:39.000Z",
300+
"updatedAt":"2020-09-29T10:39:39.000Z",
301+
"attributes":[]
302+
}
303+
]
304+
}
305+
]
306+
"""
307+
)
308+
with self.assertRaisesRegexp(
309+
AppException,
310+
f"OCR attribute group is not supported for project type {self.PROJECT_TYPE}.",
311+
):
312+
sa.create_annotation_classes_from_classes_json(
313+
self.PROJECT_NAME, temp_path
314+
)
315+
248316

249317
class TestCreateAnnotationClassPixel(BaseTestCase):
250318
PROJECT_NAME = "TestCreateAnnotationClassPixel"

tests/integration/folders/test_set_folder_status.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ def tearDownClass(cls) -> None:
3434

3535
def test_set_folder_status(self):
3636
with self.assertLogs("sa", level="INFO") as cm:
37-
for index, status in enumerate(
38-
self.FOLDER_STATUSES
39-
):
37+
for index, status in enumerate(self.FOLDER_STATUSES):
4038
sa.set_folder_status(
4139
project=self.PROJECT_NAME, folder=self.FOLDER_NAME, status=status
4240
)

tests/integration/projects/test_set_project_status.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ def tearDownClass(cls) -> None:
3030

3131
def test_set_project_status(self):
3232
with self.assertLogs("sa", level="INFO") as cm:
33-
for index, status in enumerate(
34-
self.PROJECT_STATUSES
35-
):
33+
for index, status in enumerate(self.PROJECT_STATUSES):
3634
sa.set_project_status(project=self.PROJECT_NAME, status=status)
3735
project = sa.get_project_metadata(self.PROJECT_NAME)
3836
assert (

tests/unit/test_async_functions.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
2+
from unittest import TestCase
23

34
from superannotate import SAClient
4-
from unittest import TestCase
55

66

77
sa = SAClient()
@@ -30,6 +30,7 @@ def test_get_annotations_in_running_event_loop(self):
3030
async def _test():
3131
annotations = sa.get_annotations(self.PROJECT_NAME)
3232
assert len(annotations) == 4
33+
3334
asyncio.run(_test())
3435

3536
def test_multiple_get_annotations_in_running_event_loop(self):
@@ -38,26 +39,32 @@ async def nested():
3839
sa.attach_items(self.PROJECT_NAME, self.ATTACH_PAYLOAD)
3940
annotations = sa.get_annotations(self.PROJECT_NAME)
4041
assert len(annotations) == 4
42+
4143
async def create_task_test():
4244
import nest_asyncio
45+
4346
nest_asyncio.apply()
4447
task1 = asyncio.create_task(nested())
4548
task2 = asyncio.create_task(nested())
4649
await task1
4750
await task2
51+
4852
asyncio.run(create_task_test())
4953

5054
async def gather_test():
5155
import nest_asyncio
56+
5257
nest_asyncio.apply()
5358
await asyncio.gather(nested(), nested())
59+
5460
asyncio.run(gather_test())
5561

5662
def test_upload_annotations_in_running_event_loop(self):
5763
async def _test():
5864
sa.attach_items(self.PROJECT_NAME, self.ATTACH_PAYLOAD)
59-
annotations = sa.upload_annotations(self.PROJECT_NAME, annotations=self.UPLOAD_PAYLOAD)
60-
assert len(annotations['succeeded']) == 4
61-
asyncio.run(_test())
62-
65+
annotations = sa.upload_annotations(
66+
self.PROJECT_NAME, annotations=self.UPLOAD_PAYLOAD
67+
)
68+
assert len(annotations["succeeded"]) == 4
6369

70+
asyncio.run(_test())

0 commit comments

Comments
 (0)