label-studio-sdk 0.0.32__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. label_studio_sdk/__init__.py +206 -6
  2. label_studio_sdk/_extensions/label_studio_tools/__init__.py +0 -0
  3. label_studio_sdk/_extensions/label_studio_tools/core/__init__.py +0 -0
  4. label_studio_sdk/_extensions/label_studio_tools/core/label_config.py +163 -0
  5. label_studio_sdk/_extensions/label_studio_tools/core/utils/__init__.py +0 -0
  6. label_studio_sdk/_extensions/label_studio_tools/core/utils/exceptions.py +2 -0
  7. label_studio_sdk/_extensions/label_studio_tools/core/utils/io.py +228 -0
  8. label_studio_sdk/_extensions/label_studio_tools/core/utils/params.py +45 -0
  9. label_studio_sdk/_extensions/label_studio_tools/etl/__init__.py +1 -0
  10. label_studio_sdk/_extensions/label_studio_tools/etl/beam.py +34 -0
  11. label_studio_sdk/_extensions/label_studio_tools/etl/example.py +17 -0
  12. label_studio_sdk/_extensions/label_studio_tools/etl/registry.py +67 -0
  13. label_studio_sdk/_extensions/label_studio_tools/postprocessing/__init__.py +0 -0
  14. label_studio_sdk/_extensions/label_studio_tools/postprocessing/video.py +97 -0
  15. label_studio_sdk/_legacy/__init__.py +11 -0
  16. label_studio_sdk/_legacy/client.py +471 -0
  17. label_studio_sdk/_legacy/exceptions.py +10 -0
  18. label_studio_sdk/_legacy/label_interface/__init__.py +1 -0
  19. label_studio_sdk/_legacy/label_interface/base.py +77 -0
  20. label_studio_sdk/_legacy/label_interface/control_tags.py +756 -0
  21. label_studio_sdk/_legacy/label_interface/data_examples.json +96 -0
  22. label_studio_sdk/_legacy/label_interface/interface.py +925 -0
  23. label_studio_sdk/_legacy/label_interface/label_tags.py +72 -0
  24. label_studio_sdk/_legacy/label_interface/object_tags.py +292 -0
  25. label_studio_sdk/_legacy/label_interface/region.py +43 -0
  26. label_studio_sdk/_legacy/objects.py +35 -0
  27. label_studio_sdk/{project.py → _legacy/project.py} +711 -258
  28. label_studio_sdk/_legacy/schema/label_config_schema.json +226 -0
  29. label_studio_sdk/{users.py → _legacy/users.py} +15 -13
  30. label_studio_sdk/{utils.py → _legacy/utils.py} +31 -30
  31. label_studio_sdk/{workspaces.py → _legacy/workspaces.py} +13 -11
  32. label_studio_sdk/actions/__init__.py +2 -0
  33. label_studio_sdk/actions/client.py +150 -0
  34. label_studio_sdk/annotations/__init__.py +2 -0
  35. label_studio_sdk/annotations/client.py +750 -0
  36. label_studio_sdk/client.py +164 -436
  37. label_studio_sdk/converter/__init__.py +7 -0
  38. label_studio_sdk/converter/audio.py +56 -0
  39. label_studio_sdk/converter/brush.py +452 -0
  40. label_studio_sdk/converter/converter.py +1175 -0
  41. label_studio_sdk/converter/exports/__init__.py +0 -0
  42. label_studio_sdk/converter/exports/csv.py +82 -0
  43. label_studio_sdk/converter/exports/csv2.py +103 -0
  44. label_studio_sdk/converter/funsd.py +85 -0
  45. label_studio_sdk/converter/imports/__init__.py +0 -0
  46. label_studio_sdk/converter/imports/coco.py +314 -0
  47. label_studio_sdk/converter/imports/colors.py +198 -0
  48. label_studio_sdk/converter/imports/label_config.py +45 -0
  49. label_studio_sdk/converter/imports/pathtrack.py +269 -0
  50. label_studio_sdk/converter/imports/yolo.py +236 -0
  51. label_studio_sdk/converter/main.py +202 -0
  52. label_studio_sdk/converter/utils.py +473 -0
  53. label_studio_sdk/core/__init__.py +33 -0
  54. label_studio_sdk/core/api_error.py +15 -0
  55. label_studio_sdk/core/client_wrapper.py +55 -0
  56. label_studio_sdk/core/datetime_utils.py +28 -0
  57. label_studio_sdk/core/file.py +38 -0
  58. label_studio_sdk/core/http_client.py +443 -0
  59. label_studio_sdk/core/jsonable_encoder.py +99 -0
  60. label_studio_sdk/core/pagination.py +87 -0
  61. label_studio_sdk/core/pydantic_utilities.py +28 -0
  62. label_studio_sdk/core/query_encoder.py +33 -0
  63. label_studio_sdk/core/remove_none_from_dict.py +11 -0
  64. label_studio_sdk/core/request_options.py +32 -0
  65. label_studio_sdk/data_manager.py +32 -23
  66. label_studio_sdk/environment.py +7 -0
  67. label_studio_sdk/errors/__init__.py +6 -0
  68. label_studio_sdk/errors/bad_request_error.py +8 -0
  69. label_studio_sdk/errors/internal_server_error.py +8 -0
  70. label_studio_sdk/export_storage/__init__.py +28 -0
  71. label_studio_sdk/export_storage/azure/__init__.py +5 -0
  72. label_studio_sdk/export_storage/azure/client.py +722 -0
  73. label_studio_sdk/export_storage/azure/types/__init__.py +6 -0
  74. label_studio_sdk/export_storage/azure/types/azure_create_response.py +52 -0
  75. label_studio_sdk/export_storage/azure/types/azure_update_response.py +52 -0
  76. label_studio_sdk/export_storage/client.py +107 -0
  77. label_studio_sdk/export_storage/gcs/__init__.py +5 -0
  78. label_studio_sdk/export_storage/gcs/client.py +722 -0
  79. label_studio_sdk/export_storage/gcs/types/__init__.py +6 -0
  80. label_studio_sdk/export_storage/gcs/types/gcs_create_response.py +52 -0
  81. label_studio_sdk/export_storage/gcs/types/gcs_update_response.py +52 -0
  82. label_studio_sdk/export_storage/local/__init__.py +5 -0
  83. label_studio_sdk/export_storage/local/client.py +688 -0
  84. label_studio_sdk/export_storage/local/types/__init__.py +6 -0
  85. label_studio_sdk/export_storage/local/types/local_create_response.py +47 -0
  86. label_studio_sdk/export_storage/local/types/local_update_response.py +47 -0
  87. label_studio_sdk/export_storage/redis/__init__.py +5 -0
  88. label_studio_sdk/export_storage/redis/client.py +714 -0
  89. label_studio_sdk/export_storage/redis/types/__init__.py +6 -0
  90. label_studio_sdk/export_storage/redis/types/redis_create_response.py +57 -0
  91. label_studio_sdk/export_storage/redis/types/redis_update_response.py +57 -0
  92. label_studio_sdk/export_storage/s3/__init__.py +5 -0
  93. label_studio_sdk/export_storage/s3/client.py +820 -0
  94. label_studio_sdk/export_storage/s3/types/__init__.py +6 -0
  95. label_studio_sdk/export_storage/s3/types/s3create_response.py +74 -0
  96. label_studio_sdk/export_storage/s3/types/s3update_response.py +74 -0
  97. label_studio_sdk/export_storage/types/__init__.py +5 -0
  98. label_studio_sdk/export_storage/types/export_storage_list_types_response_item.py +30 -0
  99. label_studio_sdk/files/__init__.py +2 -0
  100. label_studio_sdk/files/client.py +556 -0
  101. label_studio_sdk/import_storage/__init__.py +28 -0
  102. label_studio_sdk/import_storage/azure/__init__.py +5 -0
  103. label_studio_sdk/import_storage/azure/client.py +812 -0
  104. label_studio_sdk/import_storage/azure/types/__init__.py +6 -0
  105. label_studio_sdk/import_storage/azure/types/azure_create_response.py +72 -0
  106. label_studio_sdk/import_storage/azure/types/azure_update_response.py +72 -0
  107. label_studio_sdk/import_storage/client.py +107 -0
  108. label_studio_sdk/import_storage/gcs/__init__.py +5 -0
  109. label_studio_sdk/import_storage/gcs/client.py +812 -0
  110. label_studio_sdk/import_storage/gcs/types/__init__.py +6 -0
  111. label_studio_sdk/import_storage/gcs/types/gcs_create_response.py +72 -0
  112. label_studio_sdk/import_storage/gcs/types/gcs_update_response.py +72 -0
  113. label_studio_sdk/import_storage/local/__init__.py +5 -0
  114. label_studio_sdk/import_storage/local/client.py +690 -0
  115. label_studio_sdk/import_storage/local/types/__init__.py +6 -0
  116. label_studio_sdk/import_storage/local/types/local_create_response.py +47 -0
  117. label_studio_sdk/import_storage/local/types/local_update_response.py +47 -0
  118. label_studio_sdk/import_storage/redis/__init__.py +5 -0
  119. label_studio_sdk/import_storage/redis/client.py +768 -0
  120. label_studio_sdk/import_storage/redis/types/__init__.py +6 -0
  121. label_studio_sdk/import_storage/redis/types/redis_create_response.py +62 -0
  122. label_studio_sdk/import_storage/redis/types/redis_update_response.py +62 -0
  123. label_studio_sdk/import_storage/s3/__init__.py +5 -0
  124. label_studio_sdk/import_storage/s3/client.py +912 -0
  125. label_studio_sdk/import_storage/s3/types/__init__.py +6 -0
  126. label_studio_sdk/import_storage/s3/types/s3create_response.py +99 -0
  127. label_studio_sdk/import_storage/s3/types/s3update_response.py +99 -0
  128. label_studio_sdk/import_storage/types/__init__.py +5 -0
  129. label_studio_sdk/import_storage/types/import_storage_list_types_response_item.py +30 -0
  130. label_studio_sdk/ml/__init__.py +19 -0
  131. label_studio_sdk/ml/client.py +981 -0
  132. label_studio_sdk/ml/types/__init__.py +17 -0
  133. label_studio_sdk/ml/types/ml_create_request_auth_method.py +5 -0
  134. label_studio_sdk/ml/types/ml_create_response.py +78 -0
  135. label_studio_sdk/ml/types/ml_create_response_auth_method.py +5 -0
  136. label_studio_sdk/ml/types/ml_update_request_auth_method.py +5 -0
  137. label_studio_sdk/ml/types/ml_update_response.py +78 -0
  138. label_studio_sdk/ml/types/ml_update_response_auth_method.py +5 -0
  139. label_studio_sdk/predictions/__init__.py +2 -0
  140. label_studio_sdk/predictions/client.py +638 -0
  141. label_studio_sdk/projects/__init__.py +6 -0
  142. label_studio_sdk/projects/client.py +1053 -0
  143. label_studio_sdk/projects/exports/__init__.py +2 -0
  144. label_studio_sdk/projects/exports/client.py +930 -0
  145. label_studio_sdk/projects/types/__init__.py +7 -0
  146. label_studio_sdk/projects/types/projects_create_response.py +96 -0
  147. label_studio_sdk/projects/types/projects_import_tasks_response.py +71 -0
  148. label_studio_sdk/projects/types/projects_list_response.py +33 -0
  149. label_studio_sdk/py.typed +0 -0
  150. label_studio_sdk/tasks/__init__.py +5 -0
  151. label_studio_sdk/tasks/client.py +811 -0
  152. label_studio_sdk/tasks/types/__init__.py +6 -0
  153. label_studio_sdk/tasks/types/tasks_list_request_fields.py +5 -0
  154. label_studio_sdk/tasks/types/tasks_list_response.py +48 -0
  155. label_studio_sdk/types/__init__.py +115 -0
  156. label_studio_sdk/types/annotation.py +116 -0
  157. label_studio_sdk/types/annotation_filter_options.py +42 -0
  158. label_studio_sdk/types/annotation_last_action.py +19 -0
  159. label_studio_sdk/types/azure_blob_export_storage.py +112 -0
  160. label_studio_sdk/types/azure_blob_export_storage_status.py +7 -0
  161. label_studio_sdk/types/azure_blob_import_storage.py +113 -0
  162. label_studio_sdk/types/azure_blob_import_storage_status.py +7 -0
  163. label_studio_sdk/types/base_task.py +113 -0
  164. label_studio_sdk/types/base_user.py +42 -0
  165. label_studio_sdk/types/converted_format.py +36 -0
  166. label_studio_sdk/types/converted_format_status.py +5 -0
  167. label_studio_sdk/types/export.py +48 -0
  168. label_studio_sdk/types/export_convert.py +32 -0
  169. label_studio_sdk/types/export_create.py +54 -0
  170. label_studio_sdk/types/export_create_status.py +5 -0
  171. label_studio_sdk/types/export_status.py +5 -0
  172. label_studio_sdk/types/file_upload.py +30 -0
  173. label_studio_sdk/types/filter.py +53 -0
  174. label_studio_sdk/types/filter_group.py +35 -0
  175. label_studio_sdk/types/gcs_export_storage.py +112 -0
  176. label_studio_sdk/types/gcs_export_storage_status.py +7 -0
  177. label_studio_sdk/types/gcs_import_storage.py +113 -0
  178. label_studio_sdk/types/gcs_import_storage_status.py +7 -0
  179. label_studio_sdk/types/local_files_export_storage.py +97 -0
  180. label_studio_sdk/types/local_files_export_storage_status.py +7 -0
  181. label_studio_sdk/types/local_files_import_storage.py +92 -0
  182. label_studio_sdk/types/local_files_import_storage_status.py +7 -0
  183. label_studio_sdk/types/ml_backend.py +89 -0
  184. label_studio_sdk/types/ml_backend_auth_method.py +5 -0
  185. label_studio_sdk/types/ml_backend_state.py +5 -0
  186. label_studio_sdk/types/prediction.py +78 -0
  187. label_studio_sdk/types/project.py +198 -0
  188. label_studio_sdk/types/project_import.py +63 -0
  189. label_studio_sdk/types/project_import_status.py +5 -0
  190. label_studio_sdk/types/project_label_config.py +32 -0
  191. label_studio_sdk/types/project_sampling.py +7 -0
  192. label_studio_sdk/types/project_skip_queue.py +5 -0
  193. label_studio_sdk/types/redis_export_storage.py +117 -0
  194. label_studio_sdk/types/redis_export_storage_status.py +7 -0
  195. label_studio_sdk/types/redis_import_storage.py +112 -0
  196. label_studio_sdk/types/redis_import_storage_status.py +7 -0
  197. label_studio_sdk/types/s3export_storage.py +134 -0
  198. label_studio_sdk/types/s3export_storage_status.py +7 -0
  199. label_studio_sdk/types/s3import_storage.py +140 -0
  200. label_studio_sdk/types/s3import_storage_status.py +7 -0
  201. label_studio_sdk/types/serialization_option.py +36 -0
  202. label_studio_sdk/types/serialization_options.py +45 -0
  203. label_studio_sdk/types/task.py +157 -0
  204. label_studio_sdk/types/task_filter_options.py +49 -0
  205. label_studio_sdk/types/user_simple.py +37 -0
  206. label_studio_sdk/types/view.py +55 -0
  207. label_studio_sdk/types/webhook.py +67 -0
  208. label_studio_sdk/types/webhook_actions_item.py +21 -0
  209. label_studio_sdk/types/webhook_serializer_for_update.py +67 -0
  210. label_studio_sdk/types/webhook_serializer_for_update_actions_item.py +21 -0
  211. label_studio_sdk/users/__init__.py +5 -0
  212. label_studio_sdk/users/client.py +830 -0
  213. label_studio_sdk/users/types/__init__.py +6 -0
  214. label_studio_sdk/users/types/users_get_token_response.py +36 -0
  215. label_studio_sdk/users/types/users_reset_token_response.py +36 -0
  216. label_studio_sdk/version.py +4 -0
  217. label_studio_sdk/views/__init__.py +31 -0
  218. label_studio_sdk/views/client.py +564 -0
  219. label_studio_sdk/views/types/__init__.py +29 -0
  220. label_studio_sdk/views/types/views_create_request_data.py +43 -0
  221. label_studio_sdk/views/types/views_create_request_data_filters.py +43 -0
  222. label_studio_sdk/views/types/views_create_request_data_filters_conjunction.py +5 -0
  223. label_studio_sdk/views/types/views_create_request_data_filters_items_item.py +47 -0
  224. label_studio_sdk/views/types/views_create_request_data_ordering_item.py +38 -0
  225. label_studio_sdk/views/types/views_create_request_data_ordering_item_direction.py +5 -0
  226. label_studio_sdk/views/types/views_update_request_data.py +43 -0
  227. label_studio_sdk/views/types/views_update_request_data_filters.py +43 -0
  228. label_studio_sdk/views/types/views_update_request_data_filters_conjunction.py +5 -0
  229. label_studio_sdk/views/types/views_update_request_data_filters_items_item.py +47 -0
  230. label_studio_sdk/views/types/views_update_request_data_ordering_item.py +38 -0
  231. label_studio_sdk/views/types/views_update_request_data_ordering_item_direction.py +5 -0
  232. label_studio_sdk/webhooks/__init__.py +5 -0
  233. label_studio_sdk/webhooks/client.py +636 -0
  234. label_studio_sdk/webhooks/types/__init__.py +5 -0
  235. label_studio_sdk/webhooks/types/webhooks_update_request_actions_item.py +21 -0
  236. label_studio_sdk-1.0.0.dist-info/METADATA +307 -0
  237. label_studio_sdk-1.0.0.dist-info/RECORD +239 -0
  238. {label_studio_sdk-0.0.32.dist-info → label_studio_sdk-1.0.0.dist-info}/WHEEL +1 -2
  239. docs/__init__.py +0 -3
  240. label_studio_sdk-0.0.32.dist-info/LICENSE +0 -201
  241. label_studio_sdk-0.0.32.dist-info/METADATA +0 -22
  242. label_studio_sdk-0.0.32.dist-info/RECORD +0 -15
  243. label_studio_sdk-0.0.32.dist-info/top_level.txt +0 -3
  244. tests/test_client.py +0 -26
  245. {tests → label_studio_sdk/_extensions}/__init__.py +0 -0
@@ -0,0 +1,925 @@
1
+ """
2
+ """
3
+
4
+ import os
5
+ import copy
6
+ import logging
7
+ import re
8
+ import json
9
+ import jsonschema
10
+
11
+ from typing import Dict, Optional, List, Tuple, Any, Callable, Union
12
+ from pydantic import BaseModel
13
+
14
+ # from typing import Dict, Optional, List, Tuple, Any
15
+ from collections import defaultdict
16
+ from lxml import etree
17
+ import xmljson
18
+
19
+ from label_studio_sdk._legacy.exceptions import (
20
+ LSConfigParseException,
21
+ LabelStudioXMLSyntaxErrorSentryIgnored,
22
+ LabelStudioValidationErrorSentryIgnored,
23
+ )
24
+
25
+ from label_studio_sdk._legacy.label_interface.control_tags import (
26
+ ControlTag,
27
+ ChoicesTag,
28
+ LabelsTag,
29
+ )
30
+ from label_studio_sdk._legacy.label_interface.object_tags import ObjectTag
31
+ from label_studio_sdk._legacy.label_interface.label_tags import LabelTag
32
+ from label_studio_sdk._legacy.objects import AnnotationValue, TaskValue, PredictionValue
33
+
34
+
35
+ dir_path = os.path.dirname(os.path.realpath(__file__))
36
+ file_path = os.path.join(dir_path, "..", "schema", "label_config_schema.json")
37
+
38
+ with open(file_path) as f:
39
+ _LABEL_CONFIG_SCHEMA_DATA = json.load(f)
40
+
41
+ _LABEL_TAGS = {"Label", "Choice", "Relation"}
42
+
43
+ _DIR_APP_NAME = "label-studio"
44
+ _VIDEO_TRACKING_TAGS = {"videorectangle"}
45
+
46
+ RESULT_KEY = "result"
47
+
48
+ ############ core/label_config.py
49
+
50
+
51
+ def merge_labels_counters(dict1, dict2):
52
+ """
53
+ Merge two dictionaries with nested dictionary values into a single dictionary.
54
+
55
+ Args:
56
+ dict1 (dict): The first dictionary to merge.
57
+ dict2 (dict): The second dictionary to merge.
58
+
59
+ Returns:
60
+ dict: A new dictionary with the merged nested dictionaries.
61
+
62
+ Example:
63
+ dict1 = {'sentiment': {'Negative': 1, 'Positive': 1}}
64
+ dict2 = {'sentiment': {'Positive': 2, 'Neutral': 1}}
65
+ result_dict = merge_nested_dicts(dict1, dict2)
66
+ # {'sentiment': {'Negative': 1, 'Positive': 3, 'Neutral': 1}}
67
+ """
68
+ result_dict = {}
69
+
70
+ # iterate over keys in both dictionaries
71
+ for key in set(dict1.keys()) | set(dict2.keys()):
72
+ # add the corresponding values if they exist in both dictionaries
73
+ value = {}
74
+ if key in dict1:
75
+ value.update(dict1[key])
76
+ if key in dict2:
77
+ for subkey in dict2[key]:
78
+ value[subkey] = value.get(subkey, 0) + dict2[key][subkey]
79
+ # add the key-value pair to the result dictionary
80
+ result_dict[key] = value
81
+
82
+ return result_dict
83
+
84
+
85
+ def _fix_choices(config):
86
+ """
87
+ workaround for single choice
88
+ https://github.com/heartexlabs/label-studio/issues/1259
89
+ """
90
+ if "Choices" in config:
91
+ # for single Choices tag in View
92
+ if "Choice" in config["Choices"] and not isinstance(
93
+ config["Choices"]["Choice"], list
94
+ ):
95
+ config["Choices"]["Choice"] = [config["Choices"]["Choice"]]
96
+ # for several Choices tags in View
97
+ elif isinstance(config["Choices"], list) and all(
98
+ "Choice" in tag_choices for tag_choices in config["Choices"]
99
+ ):
100
+ for n in range(len(config["Choices"])):
101
+ # check that Choices tag has only 1 choice
102
+ if not isinstance(config["Choices"][n]["Choice"], list):
103
+ config["Choices"][n]["Choice"] = [config["Choices"][n]["Choice"]]
104
+ if "View" in config:
105
+ if isinstance(config["View"], OrderedDict):
106
+ config["View"] = _fix_choices(config["View"])
107
+ else:
108
+ config["View"] = [_fix_choices(view) for view in config["View"]]
109
+ return config
110
+
111
+
112
+ def get_annotation_tuple(from_name, to_name, type):
113
+ if isinstance(to_name, list):
114
+ to_name = ",".join(to_name)
115
+ return "|".join([from_name, to_name, type.lower()])
116
+
117
+
118
+ def get_all_control_tag_tuples(label_config):
119
+ # "chc|text|choices"
120
+ outputs = parse_config(label_config)
121
+ out = []
122
+ for control_name, info in outputs.items():
123
+ out.append(get_annotation_tuple(control_name, info["to_name"], info["type"]))
124
+ return out
125
+
126
+
127
+ def get_all_types(label_config):
128
+ """
129
+ Get all types from label_config
130
+ """
131
+ outputs = parse_config(label_config)
132
+ out = []
133
+ for control_name, info in outputs.items():
134
+ out.append(info["type"].lower())
135
+ return out
136
+
137
+
138
+ def display_count(count: int, type: str) -> Optional[str]:
139
+ """Helper for displaying pluralized sources of validation errors,
140
+ eg "1 draft" or "3 annotations"
141
+ """
142
+ if not count:
143
+ return None
144
+
145
+ return f'{count} {type}{"s" if count > 1 else ""}'
146
+
147
+
148
+ ######################
149
+
150
+
151
+ class LabelInterface:
152
+ """The LabelInterface class serves as an interface to parse and
153
+ validate labeling configurations, annotations, and predictions
154
+ within the Label Studio ecosystem.
155
+
156
+ It is designed to be compatible at the data structure level with
157
+ an existing parser widely used within the Label Studio ecosystem.
158
+ This ensures that it works seamlessly with most of the existing functions,
159
+ either by directly supporting them or by offering re-implemented versions
160
+ through the new interface.
161
+
162
+ Moreover, the parser adds value by offering functionality to
163
+ validate predictions and annotations against the specified
164
+ labeling configuration. Below is a simple example of how to use
165
+ the new API:
166
+
167
+ ```python
168
+ from label_studio_sdk.label_interface import LabelInterface
169
+
170
+ config = "<View><Text name='txt' value='$val' /><Choices name='chc' toName='txt'><Choice value='one'/> <Choice value='two'/></Choices></View>"
171
+
172
+ li = LabelInterface(config)
173
+ region = li.get_tag("chc").label("one")
174
+
175
+ # returns a JSON representing a Label Studio region
176
+ region.as_json()
177
+
178
+ # returns True
179
+ li.validate_prediction({
180
+ "model_version": "0.0.1",
181
+ "score": 0.90,
182
+ "result": [{
183
+ "from_name": "chc",
184
+ "to_name": "txt",
185
+ "type": "choices",
186
+ "value": { "choices": ["one"] }
187
+ }]
188
+ })
189
+ ```
190
+ """
191
+
192
+ def __init__(self, config: str, *args, **kwargs):
193
+ """
194
+ Create LabelInterface instance from the config string
195
+ Example:
196
+ ```
197
+ label_config = LabelInterface('''
198
+ <View>
199
+ <Choices name="sentiment" toName="txt">
200
+ <Choice value="Positive" />
201
+ <Choice value="Negative" />
202
+ <Choice value="Neutral" />
203
+ </Choices>
204
+ <Text name="txt" value="$text" />
205
+ ''')
206
+ """
207
+ self._config = config
208
+
209
+ # extract predefined task from the config
210
+ _task_data, _ann, _pred = LabelInterface.get_task_from_labeling_config(config)
211
+ self._sample_config_task = _task_data
212
+ self._sample_config_ann = _ann
213
+ self._sample_config_pred = _pred
214
+
215
+ controls, objects, labels, tree = self.parse(config)
216
+ controls = self._link_controls(controls, objects, labels)
217
+
218
+ # list of control tags that this config has
219
+ self._control_tags = set(controls.keys())
220
+ self._object_tags = set(objects.keys())
221
+ # self._label_names = set(labels.keys())
222
+
223
+ self._controls = controls
224
+ self._objects = objects
225
+ self._labels = labels
226
+ self._tree = tree
227
+
228
+ ##### NEW API
229
+
230
+ @property
231
+ def controls(self):
232
+ """Returns list of control tags"""
233
+ return self._controls and self._controls.values()
234
+
235
+ @property
236
+ def objects(self):
237
+ """Returns list of object tags"""
238
+ return self._objects and self._objects.values()
239
+
240
+ @property
241
+ def labels(self):
242
+ """Returns list of label tags"""
243
+ return self._labels and self._labels.values()
244
+
245
+ def _link_controls(self, controls: Dict, objects: Dict, labels: Dict) -> Dict:
246
+ """ """
247
+ for name, tag in controls.items():
248
+ inputs = []
249
+ for object_tag_name in tag.to_name:
250
+ if object_tag_name not in objects:
251
+ # logger.info(
252
+ # f'to_name={object_tag_name} is specified for output tag name={name}, '
253
+ # 'but we can\'t find it among input tags'
254
+ # )
255
+ continue
256
+
257
+ inputs.append(objects[object_tag_name])
258
+
259
+ tag.set_objects(inputs)
260
+ tag.set_labels(list(labels[name]))
261
+ tag.set_labels_attrs(labels[name])
262
+
263
+ return controls
264
+
265
+ def _get_tag(self, name, tag_store):
266
+ """ """
267
+ if name is not None:
268
+ if name not in tag_store:
269
+ raise Exception(
270
+ f"Name {name} is not found, available names: {tag_store.keys()}"
271
+ )
272
+ else:
273
+ return tag_store[name]
274
+
275
+ if tag_store and len(tag_store.keys()) > 1:
276
+ raise Exception("Multiple object tags connected, you should specify name")
277
+
278
+ return list(tag_store.values())[0]
279
+
280
+ def get_tag(self, name):
281
+ """Method to retrieve the tag object by its name from the current instance.
282
+
283
+ The method checks if the tag with the provided name exists in
284
+ either `_controls` or `_objects` attributes of the current
285
+ instance. If a match is found, it returns the tag. If the tag
286
+ is not found an exception is raised.
287
+
288
+ Args:
289
+ name (str): Name of the tag to be retrieved.
290
+
291
+ Returns:
292
+ object: The tag object if it exists in either `_controls` or `_objects`.
293
+
294
+ Raises:
295
+ Exception: If the tag with the given name does not exist in both `_controls` and `_objects`.
296
+
297
+ """
298
+ if name in self._controls:
299
+ return self._controls[name]
300
+
301
+ if name in self._objects:
302
+ return self._objects[name]
303
+
304
+ raise Exception(f"Tag with name {name} not found")
305
+
306
+ def get_object(self, name=None):
307
+ """Retrieves the object with the given name from `_objects`.
308
+
309
+ This utilizes the `_get_tag` method to obtain the named object.
310
+
311
+ Args:
312
+ name (str, optional): The name of the object to be retrieved from `_objects`.
313
+
314
+ Returns: object: The corresponding object if it exists in
315
+ `_objects`.
316
+
317
+ """
318
+ return self._get_tag(name, self._objects)
319
+
320
+ def get_output(self, name=None):
321
+ """Provides an alias for the `get_control` method."""
322
+ return self.get_control(name)
323
+
324
+ def get_control(self, name=None):
325
+ """Retrieves the control tag that the control tag maps to.
326
+
327
+ This uses the `_get_tag` method to obtain the named control.
328
+
329
+ Args:
330
+ name (str, optional): The name of the control to be retrieved.
331
+
332
+ Returns: object: The corresponding control if it exists in
333
+ `_controls`.
334
+
335
+ """
336
+ return self._get_tag(name, self._controls)
337
+
338
+ def find_tags_by_class(self, tag_class) -> List:
339
+ """Finds tags by their class type.
340
+
341
+ The function looks into both `self.objects` and
342
+ `self.controls` to find tags that are instances of the
343
+ provided class(es)
344
+
345
+ Args:
346
+ tag_class (class or list of classes): The class type(s) of the tags to be found.
347
+
348
+ Returns:
349
+ list: A list of tags that are instances of the provided `tag_class`(es).
350
+
351
+ """
352
+ lst = list(self.objects) + list(self.controls)
353
+ tag_classes = [tag_class] if not isinstance(tag_class, list) else tag_class
354
+
355
+ return [tag for tag in lst for cls in tag_classes if isinstance(tag, cls)]
356
+
357
+ def find_tags(
358
+ self, tag_type: Optional[str] = None, match_fn: Optional[Callable] = None
359
+ ) -> List:
360
+ """Finds tags that match the given function in the entire parsed tree.
361
+
362
+ This function searches through both `objects` and `controls`
363
+ based on `tag_type`, and applies the `match_fn` (if provided)
364
+ to filter matching tags.
365
+
366
+ Args:
367
+ tag_type (str, optional): The type of tags to be
368
+ searched. Categories include 'objects', 'controls',
369
+ 'inputs' (alias for 'objects'), 'outputs' (alias for
370
+ 'controls'). If not specified, searches both 'objects'
371
+ and 'controls'.
372
+ match_fn (Callable, optional): A function that takes a tag
373
+ as an input and returns a boolean value indicating
374
+ whether the tag matches the required condition.
375
+
376
+ Returns: list: A list of tags that match the given type and
377
+ satisfy `match_fn`.
378
+
379
+ """
380
+ tag_types = {
381
+ "objects": self.objects,
382
+ "controls": self.controls,
383
+ # aliases
384
+ "inputs": self.objects,
385
+ "outputs": self.controls,
386
+ }
387
+
388
+ lst = tag_types.get(tag_type, list(self.objects) + list(self.controls))
389
+
390
+ if match_fn is not None:
391
+ lst = list(filter(match_fn, lst))
392
+
393
+ return lst
394
+
395
+ def parse(self, config_string: str) -> Tuple[Dict, Dict, Dict, etree._Element]:
396
+ """Parses the received configuration string into dictionaries
397
+ of ControlTags, ObjectTags, and Labels, along with an XML tree
398
+ of the configuration.
399
+
400
+ Args:
401
+ config_string (str): the configuration string to be parsed.
402
+
403
+ Returns:
404
+ Tuple of:
405
+ - Dictionary where keys are control tag names and values are ControlTag instances.
406
+ - Dictionary where keys are object tag names and values are ObjectTag instances.
407
+ - Dictionary of dictionaries where primary keys are label parent names
408
+ and secondary keys are label values and values are LabelTag instances.
409
+ - An XML tree of the configuration.
410
+ """
411
+ try:
412
+ xml_tree = etree.fromstring(config_string)
413
+ except etree.XMLSyntaxError as e:
414
+ raise LabelStudioXMLSyntaxErrorSentryIgnored(str(e))
415
+
416
+ objects, controls, labels = {}, {}, defaultdict(dict)
417
+
418
+ variables = []
419
+
420
+ for tag in xml_tree.iter():
421
+ if tag.attrib and "indexFlag" in tag.attrib:
422
+ variables.append(tag.attrib["indexFlag"])
423
+
424
+ if ControlTag.validate_node(tag):
425
+ controls[tag.attrib["name"]] = ControlTag.parse_node(tag)
426
+
427
+ elif ObjectTag.validate_node(tag):
428
+ objects[tag.attrib["name"]] = ObjectTag.parse_node(tag)
429
+
430
+ elif LabelTag.validate_node(tag):
431
+ lb = LabelTag.parse_node(tag, controls)
432
+ # This case is hit when Label tag is missing `value` and `alias`
433
+ # For now we will skip that Label, but in future might want to raise an error
434
+ if lb:
435
+ labels[lb.parent_name][lb.value] = lb
436
+
437
+ return controls, objects, labels, xml_tree
438
+
439
+ @classmethod
440
+ def parse_config_to_json(cls, config_string):
441
+ """ """
442
+ try:
443
+ xml = etree.fromstring(config_string)
444
+ except TypeError:
445
+ raise etree.ParseError("can only parse strings")
446
+ if xml is None:
447
+ raise etree.ParseError("xml is empty or incorrect")
448
+
449
+ config = xmljson.badgerfish.data(xml)
450
+ config = _fix_choices(config)
451
+
452
+ return config
453
+
454
+ def _schema_validation(self, config_string):
455
+ """ """
456
+ try:
457
+ config = LabelInterface.parse_config_to_json(config_string)
458
+ jsonschema.validate(config, _LABEL_CONFIG_SCHEMA_DATA)
459
+ except (etree.ParseError, ValueError) as exc:
460
+ raise LabelStudioValidationErrorSentryIgnored(str(exc))
461
+ except jsonschema.exceptions.ValidationError as exc:
462
+ error_message = exc.context[-1].message if len(exc.context) else exc.message
463
+ error_message = "Validation failed on {}: {}".format(
464
+ "/".join(map(str, exc.path)), error_message.replace("@", "")
465
+ )
466
+ raise LabelStudioValidationErrorSentryIgnored(error_message)
467
+
468
+ def _to_name_validation(self, config_string):
469
+ """ """
470
+ # toName points to existent name
471
+ all_names = re.findall(r'name="([^"]*)"', config_string)
472
+
473
+ names = set(all_names)
474
+ toNames = re.findall(r'toName="([^"]*)"', config_string)
475
+ for toName_ in toNames:
476
+ for toName in toName_.split(","):
477
+ if toName not in names:
478
+ raise LabelStudioValidationErrorSentryIgnored(
479
+ f'toName="{toName}" not found in names: {sorted(names)}'
480
+ )
481
+
482
+ def _unique_names_validation(self, config_string):
483
+ """ """
484
+ # unique names in config # FIXME: 'name =' (with spaces) won't work
485
+ all_names = re.findall(r'name="([^"]*)"', config_string)
486
+ if len(set(all_names)) != len(all_names):
487
+ raise LabelStudioValidationErrorSentryIgnored(
488
+ "Label config contains non-unique names"
489
+ )
490
+
491
+ def load_task(self, task):
492
+ """Loads a task and substitutes the value in each object tag
493
+ with actual data from the task, returning a copy of the
494
+ LabelConfig object.
495
+
496
+ If the `value` field in an object tag is designed to take
497
+ variable input (i.e., `value_is_variable` is True), the
498
+ function replaces this value with the corresponding value from
499
+ the task dictionary.
500
+
501
+ Args:
502
+ task (dict): Dictionary representing the task, where
503
+ each key-value pair denotes an attribute-value of the
504
+ task.
505
+
506
+ Returns:
507
+ LabelInterface: A deep copy of the current LabelIntreface
508
+ instance with the object tags' value fields populated with
509
+ data from the task.
510
+
511
+ """
512
+ tree = copy.deepcopy(self)
513
+ for obj in tree.objects:
514
+ if obj.value_is_variable and obj.value_name in task:
515
+ obj.value = task.get(obj.value_name)
516
+
517
+ return tree
518
+
519
+ @property
520
+ def is_valid(self):
521
+ """ """
522
+ try:
523
+ self.validate()
524
+ return True
525
+ except LabelStudioValidationErrorSentryIgnored:
526
+ return False
527
+
528
+ def validate(self):
529
+ """Validates the provided configuration string against various validation criteria.
530
+
531
+ This method applies a series of validation checks to
532
+ `_config`, including schema validation, checking for
533
+ uniqueness of names used in the configuration, and the
534
+ "to_name" validation. It throws exceptions if any of these
535
+ validations fail.
536
+
537
+ Raises:
538
+ Exception: If any validation fails, specific to the type of validation.
539
+
540
+ """
541
+ config_string = self._config
542
+
543
+ self._schema_validation(config_string)
544
+ self._unique_names_validation(config_string)
545
+ self._to_name_validation(config_string)
546
+
547
+ @classmethod
548
+ def validate_with_data(cls, config):
549
+ """ """
550
+ raise NotImplemented()
551
+
552
+ def validate_task(self, task: "TaskValue", validate_regions_only=False):
553
+ """ """
554
+ # TODO this might not be always true, and we need to use
555
+ # "strict" param above to be able to configure
556
+
557
+ # for every object tag we've got that has value as it's
558
+ # variable we need to have an associated item in the task data
559
+ for obj in self.objects:
560
+ if obj.value_is_variable and task["data"].get(obj.value_name, None) is None:
561
+ return False
562
+
563
+ if "annotations" in task and not self.validate_annotation():
564
+ return False
565
+
566
+ if "predictions" in task and not self.validate_prediction():
567
+ return False
568
+
569
+ return True
570
+
571
+ def validate_annotation(self, annotation):
572
+ """Validates the given annotation against the current configuration.
573
+
574
+ This method applies the `validate_region` method to each
575
+ region in the annotation and returns False if any of these
576
+ validations fail. If all the regions pass the validation, it
577
+ returns True.
578
+
579
+ Args:
580
+ annotation (dict): The annotation to be validated, where
581
+ each key-value pair denotes an attribute-value of the
582
+ annotation.
583
+
584
+ Returns:
585
+ bool: True if all regions in the annotation pass the
586
+ validation, False otherwise.
587
+
588
+ """
589
+ return all(self.validate_region(r) for r in annotation.get(RESULT_KEY))
590
+
591
+ def validate_prediction(self, prediction):
592
+ """Same as validate_annotation right now"""
593
+ return all(self.validate_region(r) for r in prediction.get(RESULT_KEY))
594
+
595
+ def validate_region(self, region) -> bool:
596
+ """Validates a region from the annotation against the current
597
+ configuration.
598
+
599
+ The validation checks the following:
600
+ - Both control and object items are present in the labeling configuration.
601
+ - The type of the region matches the control tag name.
602
+ - The 'to_name' in the region data connects to the same tag as in the configuration.
603
+ - The actual value for example in <Labels /> tag is producing start, end, and labels.
604
+
605
+ If any of these validations fail, the function immediately
606
+ returns False. If all validations pass for a region, it
607
+ returns True.
608
+
609
+ Args:
610
+ region (dict): The region to be validated.
611
+
612
+ Returns:
613
+ bool: True if all checks pass for the region, False otherwise.
614
+
615
+ """
616
+ control = self.get_control(region["from_name"])
617
+ obj = self.get_object(region["to_name"])
618
+
619
+ # we should have both items present in the labeling config
620
+ if not control or not obj:
621
+ return False
622
+
623
+ # type of the region should match the tag name
624
+ if control.tag.lower() != region["type"]:
625
+ return False
626
+
627
+ # make sure that in config it connects to the same tag as
628
+ # immplied by the region data
629
+ if region["to_name"] not in control.to_name:
630
+ return False
631
+
632
+ # validate the actual value, for example that <Labels /> tag
633
+ # is producing start, end, and labels
634
+ if not control.validate_value(region["value"]):
635
+ return False
636
+
637
+ return True
638
+
639
+ ### Generation
640
+
641
+ def _sample_task(self, secure_mode=False):
642
+ """ """
643
+ # predefined_task, annotations, predictions = get_task_from_labeling_config(label_config)
644
+ generated_task = self.generate_sample_task(
645
+ mode="editor_preview", secure_mode=secure_mode
646
+ )
647
+
648
+ if self._sample_config_task is not None:
649
+ generated_task.update(self._sample_config_task)
650
+
651
+ return generated_task, self._sample_config_ann, self._sample_config_pred
652
+
653
+ def generate_sample_task(self, mode="upload", secure_mode=False):
654
+ """Generates a sample task based on the provided mode and
655
+ secure_mode.
656
+
657
+ This function generates an example value for each object in
658
+ `self.objects` using the specified `mode` and
659
+ `secure_mode`. The resulting task is a dictionary where each
660
+ key-value pair denotes an object's value-name and example
661
+ value.
662
+
663
+ Args:
664
+ mode (str, optional): The operation mode. Accepts any string but defaults to 'upload'.
665
+ secure_mode (bool, optional): The security mode. Defaults to False.
666
+
667
+ Returns:
668
+ dict: A dictionary representing the sample task.
669
+
670
+ """
671
+ task = {
672
+ obj.value_name: obj.generate_example_value(
673
+ mode=mode, secure_mode=secure_mode
674
+ )
675
+ for obj in self.objects
676
+ }
677
+
678
+ return task
679
+
680
+ def generate_sample_annotation(self):
681
+ """ """
682
+ raise NotImplemented()
683
+
684
+ #####
685
+ ##### COMPATIBILITY LAYER
686
+ #####
687
+ ##### This are re-implmenetation of functions found in different
688
+ ##### label_config.py files across the repo. Not all of this were
689
+ ##### tested, therefore I suggest to write a test first, and then
690
+ ##### replace where it's being used in the repo.
691
+
692
+ def config_essential_data_has_changed(self, new_config_str):
693
+ """Detect essential changes of the labeling config"""
694
+ new_obj = LabelInterface(config=new_config_str)
695
+
696
+ for new_tag_name, new_tag in new_obj._controls.items():
697
+ if new_tag_name not in self._controls:
698
+ return True
699
+
700
+ old_tag = self._controls[new_tag_name]
701
+
702
+ if new_tag.tag != old_tag.tag:
703
+ return True
704
+ if new_tag.objects != old_tag.objects:
705
+ return True
706
+ if not set(old_tag.labels).issubset(new_tag.labels):
707
+ return True
708
+
709
+ return False
710
+
711
+ def generate_sample_task_without_check(
712
+ label_config, mode="upload", secure_mode=False
713
+ ):
714
+ """ """
715
+ raise NotImplemented()
716
+
717
+ @classmethod
718
+ def get_task_from_labeling_config(cls, config):
719
+ """Get task, annotations and predictions from labeling config comment,
720
+ it must start from "<!-- {" and end as "} -->"
721
+ """
722
+ # try to get task data, annotations & predictions from config comment
723
+ task_data, annotations, predictions = {}, None, None
724
+ start = config.find("<!-- {")
725
+ start = start if start >= 0 else config.find("<!--{")
726
+ start += 4
727
+ end = config[start:].find("-->") if start >= 0 else -1
728
+
729
+ if 3 < start < start + end:
730
+ try:
731
+ # logger.debug('Parse ' + config[start : start + end])
732
+ body = json.loads(config[start : start + end])
733
+ except Exception:
734
+ # logger.error("Can't parse task from labeling config", exc_info=True)
735
+ pass
736
+ else:
737
+ # logger.debug(json.dumps(body, indent=2))
738
+ dont_use_root = "predictions" in body or "annotations" in body
739
+ task_data = (
740
+ body["data"]
741
+ if "data" in body
742
+ else (None if dont_use_root else body)
743
+ )
744
+ predictions = body["predictions"] if "predictions" in body else None
745
+ annotations = body["annotations"] if "annotations" in body else None
746
+
747
+ return task_data, annotations, predictions
748
+
749
+ @classmethod
750
+ def config_line_stipped(self, c):
751
+ tree = etree.fromstring(c, forbid_dtd=False)
752
+ comments = tree.xpath("//comment()")
753
+
754
+ for c in comments:
755
+ p = c.getparent()
756
+ if p is not None:
757
+ p.remove(c)
758
+ c = etree.tostring(tree, method="html").decode("utf-8")
759
+
760
+ return c.replace("\n", "").replace("\r", "")
761
+
762
+ def get_all_control_tag_tuples(label_config):
763
+ """ """
764
+ return [tag.as_tuple() for tag in self.controls]
765
+
766
+ def get_first_tag_occurence(
767
+ self,
768
+ control_type: Union[str, Tuple],
769
+ object_type: Union[str, Tuple],
770
+ name_filter: Optional[Callable] = None,
771
+ to_name_filter: Optional[Callable] = None,
772
+ ) -> Tuple[str, str, str]:
773
+ """
774
+ Reads config and fetches the first control tag along with first object tag that matches the type.
775
+
776
+ Args:
777
+ control_type (str or tuple): The control type for checking tag matches.
778
+ object_type (str or tuple): The object type for checking tag matches.
779
+ name_filter (function, optional): If given, only tags with this name will be considered.
780
+ Default is None.
781
+ to_name_filter (function, optional): If given, only tags with this name will be considered.
782
+ Default is None.
783
+
784
+ Returns:
785
+ tuple: (from_name, to_name, value), representing control tag, object tag and input value.
786
+ """
787
+
788
+ for tag in self.controls:
789
+ if tag.match(control_type, name_filter_fn=name_filter):
790
+ for object_tag in tag.objects:
791
+ if object_tag.match(object_type, to_name_filter_fn=to_name_filter):
792
+ return tag.name, object_tag.name, object_tag.value_name
793
+
794
+ raise ValueError(
795
+ f"No control tag of type {control_type} and object tag of type {object_type} found in label config"
796
+ )
797
+
798
+ def get_all_labels(self):
799
+ """ """
800
+ dynamic_values = {c.name: True for c in self.controls if c.dynamic_value}
801
+ return self._labels, dynamic_values
802
+
803
+ def get_all_object_tag_names(self):
804
+ """ """
805
+ return self._objects.keys()
806
+
807
+ def extract_data_types(self):
808
+ """ """
809
+ return self._objects
810
+
811
+ def is_video_object_tracking(self):
812
+ """ """
813
+ match_fn = lambda tag: tag.tag.lower() in _VIDEO_TRACKING_TAGS
814
+ tags = self.find_tags(match_fn=match_fn)
815
+
816
+ return bool(tags)
817
+
818
+ def is_type(self, tag_type=None):
819
+ """ """
820
+ raise NotImplemented
821
+
822
+ # NOTE: you can use validate() instead
823
+ # def validate_label_config(self, config_string):
824
+ # # xml and schema
825
+ # self._schema_validation(config_string)
826
+ # self._unique_names_validation(config_string)
827
+ # self._to_name_validation(config_string)
828
+
829
+ def validate_config_using_summary(self, summary, strict=False):
830
+ """Validate current config using LS Project Summary"""
831
+ # this is a rewrite of project.validate_config function
832
+ # self.validate_label_config(config_string)
833
+ if not self._objects:
834
+ return False
835
+
836
+ created_labels = summary.created_labels
837
+ created_labels_drafts = summary.created_labels_drafts
838
+ annotations_summary = summary.created_annotations
839
+
840
+ self.validate_annotations_consistency(annotations_summary)
841
+ self.validate_lables_consistency(created_labels, created_labels_drafts)
842
+
843
+ def validate_lables_consistency(self, created_labels, created_labels_drafts):
844
+ """ """
845
+ # validate labels consistency
846
+ # labels_from_config, dynamic_values_tags = self.get_all_labels(config_string)
847
+
848
+ created_labels = merge_labels_counters(created_labels, created_labels_drafts)
849
+
850
+ # <Labels name="sentinement" ...><Label value="Negative" ... />
851
+ # {'sentiment': {'Negative': 1, 'Positive': 3, 'Neutral': 1}}
852
+
853
+ for control_tag_from_data, labels_from_data in created_labels.items():
854
+ # Check if labels created in annotations, and their control tag has been removed
855
+ control_from_config = self.get_control(control_tag_from_data)
856
+
857
+ if labels_from_data and not control_from_config:
858
+ raise LabelStudioValidationErrorSentryIgnored(
859
+ f"There are {sum(labels_from_data.values(), 0)} annotation(s) created with tag "
860
+ f'"{control_tag_from_data}", you can\'t remove it'
861
+ )
862
+
863
+ removed_labels = []
864
+ # Check that labels themselves were not removed
865
+ for label_name, label_value in labels_from_data.items():
866
+ if label_value > 0 and not control_from_config.labels_attrs.get(
867
+ label_name, None
868
+ ):
869
+ # that label was used in labeling before, but not
870
+ # present in the current config
871
+ removed_labels.append(label_name)
872
+
873
+ # TODO that needs to be added back
874
+ # if 'VideoRectangle' in tag_types:
875
+ # for key in labels_from_config:
876
+ # labels_from_config_by_tag |= set(labels_from_config[key])
877
+
878
+ # if 'Taxonomy' in tag_types:
879
+ # custom_tags = Label.objects.filter(links__project=self).values_list('value', flat=True)
880
+ # flat_custom_tags = set([item for sublist in custom_tags for item in sublist])
881
+ # labels_from_config_by_tag |= flat_custom_tags
882
+
883
+ if len(removed_labels):
884
+ raise LabelStudioValidationErrorSentryIgnored(
885
+ f'These labels still exist in annotations or drafts:\n{",".join(removed_labels)}'
886
+ f'Please add labels to tag with name="{str(control_tag_from_data)}".'
887
+ )
888
+
889
+ def validate_annotations_consistency(self, annotations_summary):
890
+ """ """
891
+ # annotations_summary is coming from LS Project Summary, it's
892
+ # format is: { "chc|text|choices": 10 }
893
+ # which means that there are two tags, Choices, and one of
894
+ # object tags and there are 10 annotations
895
+
896
+ err = []
897
+ annotations_from_data = set(annotations_summary)
898
+
899
+ for ann in annotations_from_data:
900
+ from_name, to_name, tag_type = ann.split("|")
901
+
902
+ # avoid textarea to_name check (see DEV-1598)
903
+ if tag_type.lower() == "textarea":
904
+ continue
905
+
906
+ try:
907
+ control = self.get_control(from_name)
908
+ if not control or not control.get_object(to_name):
909
+ err.append(
910
+ f"with from_name={from_name}, to_name={to_name}, type={tag_type}"
911
+ )
912
+ except Exception as ex:
913
+ err.append(
914
+ f"Error occurred while processing from_name={from_name}, to_name={to_name}, type={tag_type}, error: {str(ex)}"
915
+ )
916
+
917
+ # control = self.get_control(from_name)
918
+ # if not control or not control.get_object(to_name):
919
+ # err.append(f'with from_name={from_name}, to_name={to_name}, type={tag_type}')
920
+
921
+ if err:
922
+ diff_str = "\n".join(err)
923
+ raise LabelStudioValidationErrorSentryIgnored(
924
+ f"Created annotations are incompatible with provided labeling schema, we found:\n{diff_str}"
925
+ )