|
| 1 | +import json |
| 2 | +from labelbox.data.annotation_types.annotation import ClassificationAnnotation |
| 3 | +from labelbox.data.annotation_types.classification.classification import Checklist, ClassificationAnswer, Radio |
| 4 | +from labelbox.data.annotation_types.data.text import TextData |
| 5 | +from labelbox.data.annotation_types.label import Label |
| 6 | + |
| 7 | +from labelbox.data.serialization.ndjson.converter import NDJsonConverter |
| 8 | + |
| 9 | + |
| 10 | +def test_serialization(): |
| 11 | + label = Label(uid="ckj7z2q0b0000jx6x0q2q7q0d", |
| 12 | + data=TextData( |
| 13 | + uid="bkj7z2q0b0000jx6x0q2q7q0d", |
| 14 | + text="This is a test", |
| 15 | + ), |
| 16 | + annotations=[ |
| 17 | + ClassificationAnnotation( |
| 18 | + name="checkbox_question_geo", |
| 19 | + confidence=0.5, |
| 20 | + value=Checklist(answer=[ |
| 21 | + ClassificationAnswer(name="first_answer"), |
| 22 | + ClassificationAnswer(name="second_answer") |
| 23 | + ])) |
| 24 | + ]) |
| 25 | + |
| 26 | + serialized = NDJsonConverter.serialize([label]) |
| 27 | + |
| 28 | + res = next(serialized) |
| 29 | + assert res['confidence'] == 0.5 |
| 30 | + assert res['name'] == "checkbox_question_geo" |
| 31 | + assert res['answer'][0]['name'] == "first_answer" |
| 32 | + assert res['answer'][1]['name'] == "second_answer" |
| 33 | + assert res['dataRow']['id'] == "bkj7z2q0b0000jx6x0q2q7q0d" |
| 34 | + |
| 35 | + deserialized = NDJsonConverter.deserialize([res]) |
| 36 | + res = next(deserialized) |
| 37 | + annotation = res.annotations[0] |
| 38 | + assert annotation.confidence == 0.5 |
| 39 | + |
| 40 | + annotation_value = annotation.value |
| 41 | + assert type(annotation_value) is Checklist |
| 42 | + assert annotation_value.answer[0].name == "first_answer" |
| 43 | + assert annotation_value.answer[1].name == "second_answer" |
0 commit comments