[docs]@define(frozen=True,slots=False)classCategoricalParameter(DiscreteParameter):"""Parameter class for categorical parameters."""# class variablesis_numerical:ClassVar[bool]=False# See base class.# object variables_values:tuple[str,...]=field(converter=tuple,validator=(min_len(2),validate_unique_values,deep_iterable(member_validator=(instance_of(str),min_len(1))),),)# See base class.encoding:CategoricalEncoding=field(default=CategoricalEncoding.OHE,converter=CategoricalEncoding)# See base class.@propertydefvalues(self)->tuple:"""The values of the parameter."""returnself._values@cached_propertydefcomp_df(self)->pd.DataFrame:# noqa: D102# See base class.ifself.encodingisCategoricalEncoding.OHE:cols=[f"{self.name}_{val}"forvalinself.values]comp_df=pd.DataFrame(np.eye(len(self.values),dtype=DTypeFloatNumpy),columns=cols)elifself.encodingisCategoricalEncoding.INT:comp_df=pd.DataFrame(range(len(self.values)),dtype=DTypeFloatNumpy,columns=[self.name])comp_df.index=pd.Index(self.values)returncomp_df
[docs]@define(frozen=True,slots=False)classTaskParameter(CategoricalParameter):"""Parameter class for task parameters."""# object variablesactive_values:tuple=field(converter=tuple)"""An optional list of values describing for which tasks recommendations should be given. By default, all parameters are considered active."""encoding:CategoricalEncoding=field(default=CategoricalEncoding.INT,init=False)# See base class.@active_values.defaultdef_default_active_values(self)->tuple:"""Set all parameters active by default."""# TODO [16605]: Redesign metadata handlingreturnself.values@active_values.validatordef_validate_active_values(# noqa: DOC101, DOC103self,_:Any,values:tuple)->None:"""Validate the active parameter values. If no such list is provided, no validation is being performed. In particular, the errors listed below are only relevant if the ``values`` list is provided. Raises: ValueError: If an empty active parameters list is provided. ValueError: If the active parameter values are not unique. ValueError: If not all active values are valid parameter choices. """# TODO [16605]: Redesign metadata handlingiflen(values)==0:raiseValueError("If an active parameters list is provided, it must not be empty.")iflen(set(values))!=len(values):raiseValueError("The active parameter values must be unique.")ifnotall(vinself.valuesforvinvalues):raiseValueError("All active values must be valid parameter choices.")