Skip to content

Commit 20949f4

Browse files
author
Val Brodsky
committed
Fix metrics serialization
1 parent 0943988 commit 20949f4

File tree

3 files changed

+61
-54
lines changed

3 files changed

+61
-54
lines changed

libs/labelbox/src/labelbox/data/serialization/ndjson/metric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def from_common(
5656

5757
class NDScalarMetric(BaseNDMetric):
5858
metric_value: Union[ScalarMetricValue, ScalarMetricConfidenceValue]
59-
metric_name: Optional[str]
59+
metric_name: Optional[str] = None
6060
aggregation: ScalarMetricAggregation = ScalarMetricAggregation.ARITHMETIC_MEAN
6161

6262
def to_common(self) -> ScalarMetric:
Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
1-
[
2-
{
3-
"answer": {
4-
"schemaId": "ckrb1sfl8099g0y91cxbd5ftb",
5-
"confidence": 0.8,
6-
"customMetrics": [
7-
{
8-
"name": "customMetric1",
9-
"value": 0.5
10-
},
11-
{
12-
"name": "customMetric2",
13-
"value": 0.3
14-
}
15-
]
16-
},
17-
"schemaId": "c123",
18-
"dataRow": {
19-
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
20-
},
21-
"uuid": "f6879f59-d2b5-49c2-aceb-d9e8dc478673"
1+
[{
2+
"answer": {
3+
"schemaId":
4+
"ckrb1sfl8099g0y91cxbd5ftb",
5+
"confidence":
6+
0.8,
7+
"customMetrics": [{
8+
"name": "customMetric1",
9+
"value": 0.5
10+
}, {
11+
"name": "customMetric2",
12+
"value": 0.3
13+
}]
2214
},
23-
{
24-
"answer": [
25-
{
26-
"schemaId": "ckrb1sfl8099e0y919v260awv",
27-
"confidence": 0.82,
28-
"customMetrics": [
29-
{
30-
"name": "customMetric1",
31-
"value": 0.5
32-
},
33-
{
34-
"name": "customMetric2",
35-
"value": 0.3
36-
}
37-
]
38-
}
39-
],
40-
"schemaId": "ckrb1sfkn099c0y910wbo0p1a",
41-
"dataRow": {
42-
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
43-
},
44-
"uuid": "d009925d-91a3-4f67-abd9-753453f5a584"
15+
"schemaId": "clxfaavvs00000cl9ai9i3etn",
16+
"dataRow": {
17+
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
4518
},
46-
{
47-
"answer": "a value",
48-
"schemaId": "ckrb1sfkn099c0y910wbo0p1a",
49-
"dataRow": {
50-
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
51-
},
52-
"uuid": "ee70fd88-9f88-48dd-b760-7469ff479b71"
53-
}
54-
]
19+
"uuid": "f6879f59-d2b5-49c2-aceb-d9e8dc478673"
20+
}, {
21+
"answer": [{
22+
"schemaId":
23+
"ckrb1sfl8099e0y919v260awv",
24+
"confidence":
25+
0.82,
26+
"customMetrics": [{
27+
"name": "customMetric1",
28+
"value": 0.5
29+
}, {
30+
"name": "customMetric2",
31+
"value": 0.3
32+
}]
33+
}],
34+
"schemaId": "ckrb1sfkn099c0y910wbo0p1a",
35+
"dataRow": {
36+
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
37+
},
38+
"uuid": "d009925d-91a3-4f67-abd9-753453f5a584"
39+
}, {
40+
"answer": "a value",
41+
"schemaId": "ckrb1sfkn099c0y910wbo0p1a",
42+
"dataRow": {
43+
"globalKey": "05e8ee85-072e-4eb2-b30a-501dee9b0d9d"
44+
},
45+
"uuid": "ee70fd88-9f88-48dd-b760-7469ff479b71"
46+
}]

libs/labelbox/tests/data/serialization/ndjson/test_global_key.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from labelbox.data.serialization.ndjson.converter import NDJsonConverter
77
from labelbox.data.serialization.ndjson.objects import NDLine
8+
from labelbox.data.annotation_types.metrics.scalar import ScalarMetricAggregation
89

910

1011
def round_dict(data):
@@ -21,8 +22,21 @@ def round_dict(data):
2122

2223

2324
@pytest.mark.parametrize('filename', [
24-
'tests/data/assets/ndjson/classification_import_global_key.json',
2525
'tests/data/assets/ndjson/metric_import_global_key.json',
26+
])
27+
def test_metric_import(filename: str):
28+
with open(filename, 'r') as f:
29+
data = json.load(f)
30+
res = list(NDJsonConverter.deserialize(data))
31+
res = list(NDJsonConverter.serialize(res))
32+
33+
data[0]['aggregation'] = ScalarMetricAggregation.ARITHMETIC_MEAN.name
34+
assert res == data
35+
f.close()
36+
37+
38+
@pytest.mark.parametrize('filename', [
39+
'tests/data/assets/ndjson/classification_import_global_key.json',
2640
'tests/data/assets/ndjson/polyline_import_global_key.json',
2741
'tests/data/assets/ndjson/text_entity_import_global_key.json',
2842
'tests/data/assets/ndjson/conversation_entity_import_global_key.json',
@@ -32,6 +46,7 @@ def test_many_types(filename: str):
3246
data = json.load(f)
3347
res = list(NDJsonConverter.deserialize(data))
3448
res = list(NDJsonConverter.serialize(res))
49+
3550
assert res == data
3651
f.close()
3752

0 commit comments

Comments
 (0)