1+ from labelbox .data .annotation_types .metrics .aggregations import MetricAggregation
12from labelbox .data .annotation_types .metrics .scalar import CustomScalarMetric
2- from typing import Literal , Union
3-
4- from fiftyone .core .collections import aggregation
3+ from typing import Union , Optional
54
65from labelbox .data .annotation_types .data import ImageData , TextData
76from labelbox .data .annotation_types .metrics import ScalarMetric
@@ -24,34 +23,54 @@ def from_common(
2423
2524
2625class NDCustomScalarMetric (NDJsonBase ):
27- metric_name : float
26+ metric_name : str
2827 metric_value : float
29- sublcass_name : float
30- aggregation : Union [ Literal [ "ARITHMETIC_MEAN" ], Literal [ "GEOMETRIC_MEAN" ],
31- Literal [ "HARMONIC_MEAN" ], Literal [ "SUM" ]]
28+ feature_name : Optional [ str ] = None
29+ subclass_name : Optional [ str ] = None
30+ aggregation : MetricAggregation
3231
3332 def to_common (self ) -> CustomScalarMetric :
34- return ScalarMetric (value = self .metric_value , extra = {'uuid' : self .uuid })
33+ return CustomScalarMetric (
34+ metric_value = self .metric_value ,
35+ metric_name = self .metric_name ,
36+ feature_name = self .feature_name ,
37+ subclass_name = self .subclass_name ,
38+ aggregation = MetricAggregation [self .aggregation ],
39+ extra = {'uuid' : self .uuid })
3540
3641 @classmethod
37- def from_common (cls , metric : ScalarMetric ,
42+ def from_common (cls , metric : CustomScalarMetric ,
3843 data : Union [TextData , ImageData ]) -> "NDCustomScalarMetric" :
3944 return NDCustomScalarMetric (uuid = metric .extra .get ('uuid' ),
40- metric_value = metric .value ,
45+ metric_value = metric .metric_value ,
46+ metric_name = metric .metric_name ,
47+ feature_name = metric .feature_name ,
48+ subclass_name = metric .subclass_name ,
49+ aggregation = metric .aggregation .value ,
4150 data_row = {'id' : data .uid })
4251
52+ def dict (self , * args , ** kwargs ):
53+ res = super ().dict (* args , ** kwargs )
54+ for field in ['featureName' , 'subclassName' ]:
55+ if res [field ] is None :
56+ res .pop (field )
57+ return res
58+
59+ class Config :
60+ use_enum_values = True
61+
4362
4463class NDMetricAnnotation :
4564
4665 @classmethod
4766 def to_common (cls , annotation : "NDMetricType" ) -> ScalarMetric :
48-
4967 return annotation .to_common ()
5068
5169 @classmethod
5270 def from_common (cls , annotation : Union [ScalarMetric , CustomScalarMetric ],
5371 data : Union [TextData , ImageData ]) -> "NDMetricType" :
54- return NDDataRowScalarMetric .from_common (annotation , data )
72+ obj = cls .lookup_object (annotation )
73+ return obj .from_common (annotation , data )
5574
5675 @staticmethod
5776 def lookup_object (
@@ -66,4 +85,4 @@ def lookup_object(
6685 return result
6786
6887
69- NDMetricType = Union [NDDataRowScalarMetric , NDCustomScalarMetric ]
88+ NDMetricType = Union [NDCustomScalarMetric , NDDataRowScalarMetric ]
0 commit comments