|
| 1 | +from typing import Dict, List, Tuple |
| 2 | + |
| 3 | +from labelbox.schema import ontology |
| 4 | +from .annotation_types import ( |
| 5 | + Text, |
| 6 | + Dropdown, |
| 7 | + Checklist, |
| 8 | + Radio, |
| 9 | + ClassificationAnnotation, |
| 10 | + ObjectAnnotation, |
| 11 | + Mask, |
| 12 | + Point, |
| 13 | + Line, |
| 14 | + Polygon, |
| 15 | + Rectangle, |
| 16 | + TextEntity |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def get_feature_schema_lookup( |
| 21 | + ontology_builder: ontology.OntologyBuilder |
| 22 | +) -> Tuple[Dict[str, str], Dict[str, str]]: |
| 23 | + tool_lookup = {} |
| 24 | + classification_lookup = {} |
| 25 | + |
| 26 | + def flatten_classification(classifications): |
| 27 | + for classification in classifications: |
| 28 | + if isinstance(classification, ontology.Classification): |
| 29 | + classification_lookup[ |
| 30 | + classification. |
| 31 | + instructions] = classification.feature_schema_id |
| 32 | + elif isinstance(classification, ontology.Option): |
| 33 | + classification_lookup[ |
| 34 | + classification.value] = classification.feature_schema_id |
| 35 | + else: |
| 36 | + raise TypeError( |
| 37 | + f"Unexpected type found in ontology. `{type(classification)}`" |
| 38 | + ) |
| 39 | + flatten_classification(classification.options) |
| 40 | + |
| 41 | + for tool in ontology_builder.tools: |
| 42 | + tool_lookup[tool.name] = tool.feature_schema_id |
| 43 | + flatten_classification(tool.classifications) |
| 44 | + flatten_classification(ontology_builder.classifications) |
| 45 | + return tool_lookup, classification_lookup |
| 46 | + |
| 47 | + |
| 48 | +def _get_options(annotation: ClassificationAnnotation, |
| 49 | + existing_options: List[ontology.Option]): |
| 50 | + if isinstance(annotation.value, Radio): |
| 51 | + answers = [annotation.value.answer] |
| 52 | + elif isinstance(annotation.value, Text): |
| 53 | + return existing_options |
| 54 | + elif isinstance(annotation.value, (Checklist, Dropdown)): |
| 55 | + answers = annotation.value.answer |
| 56 | + else: |
| 57 | + raise TypeError( |
| 58 | + f"Expected one of Radio, Text, Checklist, Dropdown. Found {type(annotation.value)}" |
| 59 | + ) |
| 60 | + |
| 61 | + option_names = {option.value for option in existing_options} |
| 62 | + for answer in answers: |
| 63 | + if answer.name not in option_names: |
| 64 | + existing_options.append(ontology.Option(value=answer.name)) |
| 65 | + option_names.add(answer.name) |
| 66 | + return existing_options |
| 67 | + |
| 68 | + |
| 69 | +def get_classifications( |
| 70 | + annotations: List[ClassificationAnnotation], |
| 71 | + existing_classifications: List[ontology.Classification] |
| 72 | +) -> List[ontology.Classification]: |
| 73 | + existing_classifications = { |
| 74 | + classification.instructions: classification |
| 75 | + for classification in existing_classifications |
| 76 | + } |
| 77 | + for annotation in annotations: |
| 78 | + # If the classification exists then we just want to add options to it |
| 79 | + classification_feature = existing_classifications.get(annotation.name) |
| 80 | + if classification_feature: |
| 81 | + classification_feature.options = _get_options( |
| 82 | + annotation, classification_feature.options) |
| 83 | + elif annotation.name not in existing_classifications: |
| 84 | + existing_classifications[annotation.name] = ontology.Classification( |
| 85 | + class_type=classification_mapping(annotation), |
| 86 | + instructions=annotation.name, |
| 87 | + options=_get_options(annotation, [])) |
| 88 | + return list(existing_classifications.values()) |
| 89 | + |
| 90 | + |
| 91 | +def get_tools(annotations: List[ObjectAnnotation], |
| 92 | + existing_tools: List[ontology.Classification]): |
| 93 | + existing_tools = {tool.name: tool for tool in existing_tools} |
| 94 | + for annotation in annotations: |
| 95 | + if annotation.name in existing_tools: |
| 96 | + # We just want to update classifications |
| 97 | + existing_tools[ |
| 98 | + annotation.name].classifications = get_classifications( |
| 99 | + annotation.classifications, |
| 100 | + existing_tools[annotation.name].classifications) |
| 101 | + else: |
| 102 | + existing_tools[annotation.name] = ontology.Tool( |
| 103 | + tool=tool_mapping(annotation), |
| 104 | + name=annotation.name, |
| 105 | + classifications=get_classifications(annotation.classifications, |
| 106 | + [])) |
| 107 | + return list(existing_tools.values()) |
| 108 | + |
| 109 | + |
| 110 | +def tool_mapping(annotation): |
| 111 | + tool_types = ontology.Tool.Type |
| 112 | + mapping = { |
| 113 | + Mask: tool_types.SEGMENTATION, |
| 114 | + Polygon: tool_types.POLYGON, |
| 115 | + Point: tool_types.POINT, |
| 116 | + Rectangle: tool_types.BBOX, |
| 117 | + Line: tool_types.LINE, |
| 118 | + TextEntity: tool_types.NER, |
| 119 | + } |
| 120 | + result = mapping.get(type(annotation.value)) |
| 121 | + if result is None: |
| 122 | + raise TypeError( |
| 123 | + f"Unexpected type found. {type(annotation.value)}. Expected one of {list(mapping.keys())}" |
| 124 | + ) |
| 125 | + return result |
| 126 | + |
| 127 | + |
| 128 | +def classification_mapping(annotation): |
| 129 | + classification_types = ontology.Classification.Type |
| 130 | + mapping = { |
| 131 | + Text: classification_types.TEXT, |
| 132 | + Checklist: classification_types.CHECKLIST, |
| 133 | + Radio: classification_types.RADIO, |
| 134 | + Dropdown: classification_types.DROPDOWN |
| 135 | + } |
| 136 | + result = mapping.get(type(annotation.value)) |
| 137 | + if result is None: |
| 138 | + raise TypeError( |
| 139 | + f"Unexpected type found. {type(annotation.value)}. Expected one of {list(mapping.keys())}" |
| 140 | + ) |
| 141 | + return result |
0 commit comments