[docs]classLabelConfiguration:""" Class that encapsulates labels and transformers for the labels. Supports two types of labels: CLASSIFICATION and REGRESSION (as defined in LabelType class) """def__init__(self,labels:list=None):assertlabelsisNoneorall(isinstance(label,Label)forlabelinlabels), \
"LabelConfiguration: all labels should be instances of Label class."self._labels={label.name:labelforlabelinlabels}iflabelsisnotNoneelse{}
[docs]defadd_label(self,label_name:str,values:list=None,auxiliary_labels:list=None,positive_class=None):vals=list(values)ifvalueselseNoneiflabel_nameinself._labelsandself._labels[label_name]isnotNoneandlen(self._labels[label_name])>0:warnings.warn("Label "+label_name+" has already been set. Overriding existing values...",Warning)ifpositive_classisnotNone:ifall(isinstance(val,str)forvalinvals)andnotisinstance(positive_class,str):positive_class=str(positive_class)ParameterValidator.assert_in_valid_list(positive_class,vals,Label.__name__,'positive_class')else:positive_class=self._get_default_positive_class(vals)logging.info(f"LabelConfiguration: No positive label class was set. "f"Setting default positive class '{positive_class}' for label {label_name}")self._labels[label_name]=Label(label_name,vals,auxiliary_labels,positive_class)
def_get_default_positive_class(self,classes):"""Returns the default positive class when a class pair is given where the positive class is obvious (0, 1; true, false)"""iflen(classes)!=2:returnNoneifclasses[0]==Trueandclasses[1]==False:returnclasses[0]ifclasses[1]==Trueandclasses[0]==False:returnclasses[1]forpositive_str,negative_strin[("1","0"),("true","false"),("positive","negative"),("+","-"),("yes","no")]:ifset(classes)=={positive_str,negative_str}:returnpositive_strifset(classes)=={positive_str.upper(),negative_str.upper()}:returnpositive_str.upper()ifset(classes)=={positive_str.title(),negative_str.title()}:returnpositive_str.title()returnsorted(classes)[0]
[docs]defget_label_values(self,label_name:str):assertlabel_nameinself._labels,label_name+" is not in the list of labels, so there is no information on the values."returnself._labels[label_name].values