Skip to content

Commit 220fd61

Browse files
authored
Merge pull request #397 from superannotateai/friday-add-logs
Friday add logs
2 parents eaea25a + 043591e commit 220fd61

File tree

3 files changed

+102
-23
lines changed

3 files changed

+102
-23
lines changed

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,12 +2710,12 @@ def __init__(
27102710
self,
27112711
annotation_classes: BaseManageableRepository,
27122712
annotation_class: AnnotationClassEntity,
2713-
project_name: str,
2713+
project: ProjectEntity,
27142714
):
27152715
super().__init__()
27162716
self._annotation_classes = annotation_classes
27172717
self._annotation_class = annotation_class
2718-
self._project_name = project_name
2718+
self._project = project
27192719

27202720
def validate_uniqueness(self):
27212721
annotation_classes = self._annotation_classes.get_all(
@@ -2730,11 +2730,20 @@ def validate_uniqueness(self):
27302730
):
27312731
raise AppValidationException("Annotation class already exits.")
27322732

2733+
def validate_project_type(self):
2734+
if (
2735+
self._project.project_type != ProjectType.VECTOR.value
2736+
and self._annotation_class.type == "tag"
2737+
):
2738+
raise AppException(
2739+
f"Predefined tagging functionality is not supported for projects of type {ProjectType.get_name(self._project.project_type)}."
2740+
)
2741+
27332742
def execute(self):
27342743
if self.is_valid():
27352744
logger.info(
27362745
"Creating annotation class in project %s with name %s",
2737-
self._project_name,
2746+
self._project.name,
27382747
self._annotation_class.name,
27392748
)
27402749
created = self._annotation_classes.insert(entity=self._annotation_class)
@@ -2840,27 +2849,34 @@ def validate_annotation_classes(self):
28402849
if "attribute_groups" not in self._annotation_classes:
28412850
raise AppValidationException("Field attribute_groups is required.")
28422851

2852+
def validate_project_type(self):
2853+
if self._project.project_type != ProjectType.VECTOR.value and "tag" in [
2854+
i.type for i in self._annotation_classes
2855+
]:
2856+
raise AppException(
2857+
f"Predefined tagging functionality is not supported for projects of type {ProjectType.get_name(self._project.project_type)}."
2858+
)
2859+
28432860
def execute(self):
2844-
existing_annotation_classes = self._annotation_classes_repo.get_all()
2845-
existing_classes_name = [i.name for i in existing_annotation_classes]
2846-
unique_annotation_classes = []
2847-
for annotation_class in self._annotation_classes:
2848-
if annotation_class.name in existing_classes_name:
2849-
logger.warning(
2850-
"Annotation class %s already in project. Skipping.",
2851-
annotation_class.name,
2861+
if self.is_valid():
2862+
existing_annotation_classes = self._annotation_classes_repo.get_all()
2863+
existing_classes_name = [i.name for i in existing_annotation_classes]
2864+
unique_annotation_classes = []
2865+
for annotation_class in self._annotation_classes:
2866+
if annotation_class.name in existing_classes_name:
2867+
logger.warning(
2868+
"Annotation class %s already in project. Skipping.",
2869+
annotation_class.name,
2870+
)
2871+
continue
2872+
else:
2873+
unique_annotation_classes.append(annotation_class)
2874+
created = []
2875+
for i in range(len(unique_annotation_classes) - self.CHUNK_SIZE, 0, self.CHUNK_SIZE):
2876+
created += self._annotation_classes_repo.bulk_insert(
2877+
entities=unique_annotation_classes[i : i + self.CHUNK_SIZE], # noqa: E203
28522878
)
2853-
continue
2854-
else:
2855-
unique_annotation_classes.append(annotation_class)
2856-
2857-
created = []
2858-
2859-
for i in range(len(unique_annotation_classes) - self.CHUNK_SIZE, 0, self.CHUNK_SIZE):
2860-
created += self._annotation_classes_repo.bulk_insert(
2861-
entities=unique_annotation_classes[i : i + self.CHUNK_SIZE], # noqa: E203
2862-
)
2863-
self._response.data = created
2879+
self._response.data = created
28642880
return self._response
28652881

28662882

src/superannotate/lib/infrastructure/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ def create_annotation_class(
11421142
use_case = usecases.CreateAnnotationClassUseCase(
11431143
annotation_classes=annotation_classes,
11441144
annotation_class=annotation_class,
1145-
project_name=project_name,
1145+
project=project,
11461146
)
11471147
use_case.execute()
11481148
return use_case.execute()

tests/integration/classes/test_create_annotation_class.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,66 @@ def test_create_annotation_class(self):
1515
sa.create_annotation_class(self.PROJECT_NAME, "test_add", "#FF0000", type="tag")
1616
classes = sa.search_annotation_classes(self.PROJECT_NAME)
1717
self.assertEqual(classes[0]["type"], "tag")
18+
19+
20+
class TestCreateAnnotationClassNonVectorWithError(BaseTestCase):
21+
PROJECT_NAME = "test_create_annotation_class"
22+
PROJECT_TYPE = "Video"
23+
PROJECT_DESCRIPTION = "Example Project test pixel basic images"
24+
25+
def test_create_annotation_class(self):
26+
msg = ""
27+
try:
28+
sa.create_annotation_class(self.PROJECT_NAME, "test_add", "#FF0000", type="tag")
29+
except Exception as e:
30+
msg = str(e)
31+
self.assertEqual(msg, "Predefined tagging functionality is not supported for projects of type Video.")
32+
33+
34+
class TestCreateAnnotationClassesNonVectorWithError(BaseTestCase):
35+
PROJECT_NAME = "test_create_annotation_class"
36+
PROJECT_TYPE = "Video"
37+
PROJECT_DESCRIPTION = "Example Project test pixel basic images"
38+
39+
def test_create_annotation_class(self):
40+
with tempfile.TemporaryDirectory() as tmpdir_name:
41+
temp_path = f"{tmpdir_name}/new_classes.json"
42+
with open(temp_path,
43+
"w") as new_classes:
44+
new_classes.write(
45+
'''
46+
[
47+
{
48+
"id":56820,
49+
"project_id":7617,
50+
"name":"Personal vehicle",
51+
"color":"#547497",
52+
"count":18,
53+
"createdAt":"2020-09-29T10:39:39.000Z",
54+
"updatedAt":"2020-09-29T10:48:18.000Z",
55+
"type": "tag",
56+
"attribute_groups":[
57+
{
58+
"id":21448,
59+
"class_id":56820,
60+
"name":"Large",
61+
"is_multiselect":0,
62+
"createdAt":"2020-09-29T10:39:39.000Z",
63+
"updatedAt":"2020-09-29T10:39:39.000Z",
64+
"attributes":[]
65+
}
66+
]
67+
}
68+
]
69+
70+
'''
71+
)
72+
msg = ""
73+
try:
74+
sa.create_annotation_classes_from_classes_json(
75+
self.PROJECT_NAME, temp_path
76+
)
77+
except Exception as e:
78+
msg = str(e)
79+
self.assertEqual(msg, "Predefined tagging functionality is not supported for projects of type Video.")
80+

0 commit comments

Comments
 (0)