[docs]classLabel:def__init__(self,name:str,values:list,auxiliary_label_names:list=None,positive_class=None):self.name=nameself._values=valuesself.auxiliary_label_names=auxiliary_label_namesself._positive_class=positive_classdef__str__(self):returnf"label {self.name} ({', '.join([str(val)forvalinself.values])})"def__eq__(self,other):ifnotisinstance(other,Label):returnFalsereturnself.name==other.nameandself.values==other.valuesand \
self.positive_class==other.positive_classandself.auxiliary_label_names==other.auxiliary_label_names@propertydefpositive_class(self):""" Ensures the same class is always returned as the 'positive class', even when it was not explicitly set. """ifself._positive_classisnotNone:returnself._positive_classelse:assertself._valuesisnotNone,f"Label: cannot access positive class for label {self.name} " \
f"when neither 'positive_class' nor 'values' are set."positive_class=sorted(self._values)[0]logging.info(f"Label: No positive class was set for label {self.name}. "f"Assuming default positive class '{positive_class}'.")returnpositive_class@propertydefvalues(self):""" Make sure the positive class is listed last This is needed for compatibility with `~immuneML.ml_methods.util.Util.binarize_label_classes` """ifself._positive_classisnotNone:negative_classes=sorted(self._values)negative_classes.remove(self._positive_class)returnnegative_classes+[self._positive_class]else:returnsorted(self._values)
[docs]defget_binary_negative_class(self):""" Ensures the correct 'negative class' is returned when using the Label for binary classification. """assertlen(self._values)==2,f"Label: binary negative class was requested for label {self.name} but this label contains {len(self._values)} classes: {self.values}"return[valforvalinself._valuesifval!=self.positive_class][0]
[docs]defget_desc_for_storage(self):""" Method to call when storing a label to YAML format """return{key.lstrip("_"):valueforkey,valueinvars(self).items()}