|
1 | 1 | import json
|
2 | 2 |
|
| 3 | +from labelbox.data.annotation_types.classification.classification import ( |
| 4 | + Checklist, |
| 5 | + Radio, |
| 6 | + Text, |
| 7 | +) |
3 | 8 | from labelbox.data.serialization.ndjson.converter import NDJsonConverter
|
4 | 9 |
|
| 10 | +from labelbox.types import ( |
| 11 | + Label, |
| 12 | + ClassificationAnnotation, |
| 13 | + ClassificationAnswer, |
| 14 | + GenericDataRowData, |
| 15 | +) |
| 16 | +from labelbox.data.mixins import CustomMetric |
| 17 | + |
5 | 18 |
|
6 | 19 | def test_classification():
|
7 |
| - with open('tests/data/assets/ndjson/classification_import.json', |
8 |
| - 'r') as file: |
| 20 | + with open("tests/data/assets/ndjson/classification_import.json", "r") as file: |
9 | 21 | data = json.load(file)
|
10 |
| - res = list(NDJsonConverter.deserialize(data)) |
11 |
| - res = list(NDJsonConverter.serialize(res)) |
| 22 | + |
| 23 | + label = Label( |
| 24 | + data=GenericDataRowData( |
| 25 | + uid="ckrb1sf1i1g7i0ybcdc6oc8ct", |
| 26 | + ), |
| 27 | + annotations=[ |
| 28 | + ClassificationAnnotation( |
| 29 | + feature_schema_id="ckrb1sfjx099a0y914hl319ie", |
| 30 | + extra={"uuid": "f6879f59-d2b5-49c2-aceb-d9e8dc478673"}, |
| 31 | + value=Radio( |
| 32 | + answer=ClassificationAnswer( |
| 33 | + custom_metrics=[ |
| 34 | + CustomMetric(name="customMetric1", value=0.5), |
| 35 | + CustomMetric(name="customMetric2", value=0.3), |
| 36 | + ], |
| 37 | + confidence=0.8, |
| 38 | + feature_schema_id="ckrb1sfl8099g0y91cxbd5ftb", |
| 39 | + ), |
| 40 | + ), |
| 41 | + ), |
| 42 | + ClassificationAnnotation( |
| 43 | + feature_schema_id="ckrb1sfkn099c0y910wbo0p1a", |
| 44 | + extra={"uuid": "d009925d-91a3-4f67-abd9-753453f5a584"}, |
| 45 | + value=Checklist( |
| 46 | + answer=[ |
| 47 | + ClassificationAnswer( |
| 48 | + custom_metrics=[ |
| 49 | + CustomMetric(name="customMetric1", value=0.5), |
| 50 | + CustomMetric(name="customMetric2", value=0.3), |
| 51 | + ], |
| 52 | + confidence=0.82, |
| 53 | + feature_schema_id="ckrb1sfl8099e0y919v260awv", |
| 54 | + ) |
| 55 | + ], |
| 56 | + ), |
| 57 | + ), |
| 58 | + ClassificationAnnotation( |
| 59 | + feature_schema_id="ckrb1sfkn099c0y910wbo0p1a", |
| 60 | + extra={"uuid": "78ff6a23-bebe-475c-8f67-4c456909648f"}, |
| 61 | + value=Text(answer="a value"), |
| 62 | + ), |
| 63 | + ], |
| 64 | + ) |
| 65 | + |
| 66 | + res = list(NDJsonConverter.serialize([label])) |
12 | 67 | assert res == data
|
13 | 68 |
|
14 | 69 |
|
15 | 70 | def test_classification_with_name():
|
16 |
| - with open('tests/data/assets/ndjson/classification_import_name_only.json', |
17 |
| - 'r') as file: |
| 71 | + with open( |
| 72 | + "tests/data/assets/ndjson/classification_import_name_only.json", "r" |
| 73 | + ) as file: |
18 | 74 | data = json.load(file)
|
19 |
| - res = list(NDJsonConverter.deserialize(data)) |
20 |
| - res = list(NDJsonConverter.serialize(res)) |
| 75 | + label = Label( |
| 76 | + data=GenericDataRowData( |
| 77 | + uid="ckrb1sf1i1g7i0ybcdc6oc8ct", |
| 78 | + ), |
| 79 | + annotations=[ |
| 80 | + ClassificationAnnotation( |
| 81 | + name="classification a", |
| 82 | + extra={"uuid": "f6879f59-d2b5-49c2-aceb-d9e8dc478673"}, |
| 83 | + value=Radio( |
| 84 | + answer=ClassificationAnswer( |
| 85 | + custom_metrics=[ |
| 86 | + CustomMetric(name="customMetric1", value=0.5), |
| 87 | + CustomMetric(name="customMetric2", value=0.3), |
| 88 | + ], |
| 89 | + confidence=0.99, |
| 90 | + name="choice 1", |
| 91 | + ), |
| 92 | + ), |
| 93 | + ), |
| 94 | + ClassificationAnnotation( |
| 95 | + name="classification b", |
| 96 | + extra={"uuid": "d009925d-91a3-4f67-abd9-753453f5a584"}, |
| 97 | + value=Checklist( |
| 98 | + answer=[ |
| 99 | + ClassificationAnswer( |
| 100 | + custom_metrics=[ |
| 101 | + CustomMetric(name="customMetric1", value=0.5), |
| 102 | + CustomMetric(name="customMetric2", value=0.3), |
| 103 | + ], |
| 104 | + confidence=0.945, |
| 105 | + name="choice 2", |
| 106 | + ) |
| 107 | + ], |
| 108 | + ), |
| 109 | + ), |
| 110 | + ClassificationAnnotation( |
| 111 | + name="classification c", |
| 112 | + extra={"uuid": "150d60de-30af-44e4-be20-55201c533312"}, |
| 113 | + value=Text(answer="a value"), |
| 114 | + ), |
| 115 | + ], |
| 116 | + ) |
| 117 | + |
| 118 | + res = list(NDJsonConverter.serialize([label])) |
21 | 119 | assert res == data
|
0 commit comments