File tree Expand file tree Collapse file tree 4 files changed +80
-0
lines changed
labelbox/data/serialization/ndjson Expand file tree Collapse file tree 4 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ [{"uuid" : " a22bbf6e-b2da-4abe-9a11-df84759f7672" ,"dataRow" : {"id" : " ckrmdnqj4000007msh9p2a27r" }, "metricValue" : 0.1 }]
Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments