1- from labelbox .data .annotation_types .data import TextData , ImageData
1+ from labelbox .data .annotation_types .metrics .scalar import CustomScalarMetric
2+ from typing import Literal , Union
3+
4+ from fiftyone .core .collections import aggregation
5+
6+ from labelbox .data .annotation_types .data import ImageData , TextData
27from labelbox .data .annotation_types .metrics import ScalarMetric
38from labelbox .data .serialization .ndjson .base import NDJsonBase
4- from typing import Union
59
610
711class NDDataRowScalarMetric (NDJsonBase ):
@@ -19,16 +23,47 @@ def from_common(
1923 data_row = {'id' : data .uid })
2024
2125
26+ class NDCustomScalarMetric (NDJsonBase ):
27+ metric_name : float
28+ metric_value : float
29+ sublcass_name : float
30+ aggregation : Union [Literal ["ARITHMETIC_MEAN" ], Literal ["GEOMETRIC_MEAN" ],
31+ Literal ["HARMONIC_MEAN" ], Literal ["SUM" ]]
32+
33+ def to_common (self ) -> CustomScalarMetric :
34+ return ScalarMetric (value = self .metric_value , extra = {'uuid' : self .uuid })
35+
36+ @classmethod
37+ def from_common (cls , metric : ScalarMetric ,
38+ data : Union [TextData , ImageData ]) -> "NDCustomScalarMetric" :
39+ return NDCustomScalarMetric (uuid = metric .extra .get ('uuid' ),
40+ metric_value = metric .value ,
41+ data_row = {'id' : data .uid })
42+
43+
2244class NDMetricAnnotation :
2345
2446 @classmethod
25- def to_common (cls , annotation : NDDataRowScalarMetric ) -> ScalarMetric :
47+ def to_common (cls , annotation : "NDMetricType" ) -> ScalarMetric :
48+
2649 return annotation .to_common ()
2750
2851 @classmethod
29- def from_common (cls , annotation : ScalarMetric ,
30- data : Union [TextData , ImageData ]) -> NDDataRowScalarMetric :
52+ def from_common (cls , annotation : Union [ ScalarMetric , CustomScalarMetric ] ,
53+ data : Union [TextData , ImageData ]) -> "NDMetricType" :
3154 return NDDataRowScalarMetric .from_common (annotation , data )
3255
56+ @staticmethod
57+ def lookup_object (
58+ metric : Union [ScalarMetric , CustomScalarMetric ]) -> "NDMetricType" :
59+ result = {
60+ ScalarMetric : NDDataRowScalarMetric ,
61+ CustomScalarMetric : NDCustomScalarMetric ,
62+ }.get (type (metric ))
63+ if result is None :
64+ raise TypeError (
65+ f"Unable to convert object to MAL format. `{ type (metric )} `" )
66+ return result
67+
3368
34- NDMetricType = NDDataRowScalarMetric
69+ NDMetricType = Union [ NDDataRowScalarMetric , NDCustomScalarMetric ]
0 commit comments