|
| 1 | +from typing import List, Union |
| 2 | + |
| 3 | +from pydantic.main import BaseModel |
| 4 | + |
| 5 | +from ...annotation_types.annotation import ClassificationAnnotation |
| 6 | +from ...annotation_types.classification import Checklist, ClassificationAnswer, Radio, Text, Dropdown |
| 7 | +from ...annotation_types.types import Cuid |
| 8 | +from .feature import LBV1Feature |
| 9 | + |
| 10 | + |
| 11 | +class LBV1ClassificationAnswer(LBV1Feature): |
| 12 | + ... |
| 13 | + |
| 14 | + |
| 15 | +class LBV1Radio(LBV1Feature): |
| 16 | + answer: LBV1ClassificationAnswer |
| 17 | + |
| 18 | + def to_common(self): |
| 19 | + return Radio(answer=ClassificationAnswer( |
| 20 | + schema_id=self.answer.schema_id, |
| 21 | + name=self.answer.title, |
| 22 | + extra={ |
| 23 | + 'feature_id': self.answer.feature_id, |
| 24 | + 'value': self.answer.value |
| 25 | + })) |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def from_common(cls, radio: Radio, schema_id: Cuid, **extra) -> "LBV1Radio": |
| 29 | + return cls(schema_id=schema_id, |
| 30 | + answer=LBV1ClassificationAnswer( |
| 31 | + schema_id=radio.answer.schema_id, |
| 32 | + title=radio.answer.name, |
| 33 | + value=radio.answer.extra.get('value'), |
| 34 | + feature_id=radio.answer.extra.get('feature_id')), |
| 35 | + **extra) |
| 36 | + |
| 37 | + |
| 38 | +class LBV1Checklist(LBV1Feature): |
| 39 | + answers: List[LBV1ClassificationAnswer] |
| 40 | + |
| 41 | + def to_common(self): |
| 42 | + return Checklist(answer=[ |
| 43 | + ClassificationAnswer(schema_id=answer.schema_id, |
| 44 | + name=answer.title, |
| 45 | + extra={ |
| 46 | + 'feature_id': answer.feature_id, |
| 47 | + 'value': answer.value |
| 48 | + }) for answer in self.answers |
| 49 | + ]) |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def from_common(cls, checklist: Checklist, schema_id: Cuid, |
| 53 | + **extra) -> "LBV1Checklist": |
| 54 | + return cls(schema_id=schema_id, |
| 55 | + answers=[ |
| 56 | + LBV1ClassificationAnswer( |
| 57 | + schema_id=answer.schema_id, |
| 58 | + title=answer.name, |
| 59 | + value=answer.extra.get('value'), |
| 60 | + feature_id=answer.extra.get('feature_id')) |
| 61 | + for answer in checklist.answer |
| 62 | + ], |
| 63 | + **extra) |
| 64 | + |
| 65 | + |
| 66 | +class LBV1Text(LBV1Feature): |
| 67 | + answer: str |
| 68 | + |
| 69 | + def to_common(self): |
| 70 | + return Text(answer=self.answer) |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def from_common(cls, text: Text, schema_id: Cuid, **extra) -> "LBV1Text": |
| 74 | + return cls(schema_id=schema_id, answer=text.answer, **extra) |
| 75 | + |
| 76 | + |
| 77 | +class LBV1Classifications(BaseModel): |
| 78 | + classifications: List[Union[LBV1Radio, LBV1Checklist, LBV1Text]] = [] |
| 79 | + |
| 80 | + def to_common(self) -> List[ClassificationAnnotation]: |
| 81 | + classifications = [ |
| 82 | + ClassificationAnnotation(value=classification.to_common(), |
| 83 | + classifications=[], |
| 84 | + name=classification.title, |
| 85 | + extra={ |
| 86 | + 'value': classification.value, |
| 87 | + 'feature_id': classification.feature_id |
| 88 | + }) |
| 89 | + for classification in self.classifications |
| 90 | + ] |
| 91 | + return classifications |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def from_common( |
| 95 | + cls, annotations: List[ClassificationAnnotation] |
| 96 | + ) -> "LBV1Classifications": |
| 97 | + classifications = [] |
| 98 | + for annotation in annotations: |
| 99 | + classification = cls.lookup_classification(annotation) |
| 100 | + if classification is not None: |
| 101 | + classifications.append( |
| 102 | + classification.from_common(annotation.value, |
| 103 | + annotation.schema_id, |
| 104 | + **annotation.extra)) |
| 105 | + else: |
| 106 | + raise TypeError(f"Unexpected type {type(annotation.value)}") |
| 107 | + return cls(classifications=classifications) |
| 108 | + |
| 109 | + @staticmethod |
| 110 | + def lookup_classification( |
| 111 | + annotation: ClassificationAnnotation |
| 112 | + ) -> Union[LBV1Text, LBV1Checklist, LBV1Radio]: |
| 113 | + return { |
| 114 | + Text: LBV1Text, |
| 115 | + Dropdown: LBV1Checklist, |
| 116 | + Checklist: LBV1Checklist, |
| 117 | + Radio: LBV1Radio |
| 118 | + }.get(type(annotation.value)) |
0 commit comments