Skip to content

Commit 4cbc322

Browse files
author
Matt Sokoloff
committed
add metric type
1 parent 56f4bda commit 4cbc322

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from labelbox.data.annotation_types.data import TextData, RasterData
2+
from labelbox.data.annotation_types.metrics import ScalarMetric
3+
from labelbox.data.serialization.ndjson.base import NDJsonBase
4+
from typing import Union
5+
6+
7+
class NDDataRowScalarMetric(NDJsonBase):
8+
metric_value: float
9+
10+
def to_common(self) -> ScalarMetric:
11+
return ScalarMetric(value=self.metric_value, extra={'uuid': self.uuid})
12+
13+
@classmethod
14+
def from_common(
15+
cls, metric: ScalarMetric,
16+
data: Union[TextData, RasterData]) -> "NDDataRowScalarMetric":
17+
return NDDataRowScalarMetric(uuid=metric.extra.get('uuid'),
18+
metric_value=metric.value,
19+
data_row={'id': data.uid})
20+
21+
22+
class NDMetricAnnotation:
23+
24+
@classmethod
25+
def to_common(cls, annotation: NDDataRowScalarMetric) -> ScalarMetric:
26+
return annotation.to_common()
27+
28+
@classmethod
29+
def from_common(cls, annotation: ScalarMetric,
30+
data: Union[TextData, RasterData]) -> NDDataRowScalarMetric:
31+
return NDDataRowScalarMetric.from_common(annotation, data)
32+
33+
34+
NDMetricType = NDDataRowScalarMetric
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from labelbox.data.annotation_types.collection import LabelList
2+
from labelbox.data.annotation_types import ScalarMetric, Label, RasterData
3+
4+
5+
def test_scalar_metric():
6+
value = 10
7+
metric = ScalarMetric(value=value)
8+
assert metric.value == value
9+
10+
label = Label(data=RasterData(uid="ckrmd9q8g000009mg6vej7hzg"),
11+
annotations=[metric])
12+
expected = {
13+
'data': {
14+
'external_id': None,
15+
'uid': 'ckrmd9q8g000009mg6vej7hzg',
16+
'im_bytes': None,
17+
'file_path': None,
18+
'url': None,
19+
'arr': None
20+
},
21+
'annotations': [{
22+
'value': 10.0,
23+
'extra': {}
24+
}],
25+
'extra': {},
26+
'uid': None
27+
}
28+
assert label.dict() == expected
29+
next(LabelList([label])).dict() == expected
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"uuid" : "a22bbf6e-b2da-4abe-9a11-df84759f7672","dataRow" : {"id": "ckrmdnqj4000007msh9p2a27r"}, "metricValue" : 0.1}]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
from labelbox.data.serialization.labelbox_v1.converter import LBV1Converter
3+
4+
from labelbox.data.serialization.ndjson.converter import NDJsonConverter
5+
6+
7+
def test_metric():
8+
with open('tests/data/assets/ndjson/metric_import.json', 'r') as file:
9+
data = json.load(file)
10+
11+
label_list = NDJsonConverter.deserialize(data).as_list()
12+
reserialized = list(NDJsonConverter.serialize(label_list))
13+
assert reserialized == data
14+
15+
# Just make sure that this doesn't break
16+
list(LBV1Converter.serialize(label_list))

0 commit comments

Comments
 (0)