Skip to content
Snippets Groups Projects

Fix 'create_classifications' helper

Merged Manon Blanco requested to merge fix-create-classifications into master
2 files
+ 152
155
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -2,8 +2,6 @@
ElementsWorker methods for classifications and ML classes.
"""
from uuid import UUID
from apistar.exceptions import ErrorResponse
from peewee import IntegrityError
@@ -178,10 +176,14 @@ class ClassificationMixin:
Create multiple classifications at once on the given element through the API.
:param element: The element to create classifications on.
:param classifications: The classifications to create, a list of dicts. Each of them contains
a **ml_class_id** (str), the ID of the MLClass for this classification;
a **confidence** (float), the confidence score, between 0 and 1;
a **high_confidence** (bool), the high confidence state of the classification.
:param classifications: A list of dicts representing a classification each, with the following keys:
ml_class (str)
Required. Name of the MLClass to use.
confidence (float)
Required. Confidence score for the classification. Must be between 0 and 1.
high_confidence (bool)
Optional. Whether or not the classification is of high confidence.
:returns: List of created classifications, as returned in the ``classifications`` field by
the ``CreateClassifications`` API endpoint.
@@ -194,18 +196,10 @@ class ClassificationMixin:
), "classifications shouldn't be null and should be of type list"
for index, classification in enumerate(classifications):
ml_class_id = classification.get("ml_class_id")
ml_class = classification.get("ml_class")
assert (
ml_class_id and isinstance(ml_class_id, str)
), f"Classification at index {index} in classifications: ml_class_id shouldn't be null and should be of type str"
# Make sure it's a valid UUID
try:
UUID(ml_class_id)
except ValueError as e:
raise ValueError(
f"Classification at index {index} in classifications: ml_class_id is not a valid uuid."
) from e
ml_class and isinstance(ml_class, str)
), f"Classification at index {index} in classifications: ml_class shouldn't be null and should be of type str"
confidence = classification.get("confidence")
assert (
@@ -231,7 +225,13 @@ class ClassificationMixin:
body={
"parent": str(element.id),
"worker_run_id": self.worker_run_id,
"classifications": classifications,
"classifications": [
{
**classification,
"ml_class": self.get_ml_class_id(classification["ml_class"]),
}
for classification in classifications
],
},
)["classifications"]
Loading