def get_bounds_and_task( search_space: SearchSpace, param_names: List[str] ) -> Tuple[List[Tuple[float, float]], List[int], Dict[int, float]]: """Extract box bounds from a search space in the usual Scipy format. Identify integer parameters as task features. """ bounds: List[Tuple[float, float]] = [] task_features: List[int] = [] target_fidelities: Dict[int, float] = {} for i, p_name in enumerate(param_names): p = search_space.parameters[p_name] # Validation if not isinstance(p, RangeParameter): raise ValueError(f"{p} not RangeParameter") elif p.log_scale: raise ValueError(f"{p} is log scale") # Set value bounds.append((p.lower, p.upper)) if p.parameter_type == ParameterType.INT: task_features.append(i) if p.is_fidelity: if not isinstance(not_none(p.target_value), (int, float)): raise NotImplementedError( "Only numerical target values currently supported.") target_fidelities[i] = float( checked_cast_to_tuple((int, float), p.target_value)) return bounds, task_features, target_fidelities
def _make_range_param(name: str, representation: TParameterRepresentation, parameter_type: Optional[str]) -> RangeParameter: assert "bounds" in representation, "Bounds are required for range parameters." bounds = representation["bounds"] assert isinstance(bounds, list) and len(bounds) == 2, ( f"Cannot parse parameter {name}: for range parameters, json representation " "should include a list of two values, lower and upper bounds of the bounds." ) return RangeParameter( name=name, parameter_type=_to_parameter_type(bounds, parameter_type, name, "bounds"), lower=checked_cast_to_tuple((float, int), bounds[0]), upper=checked_cast_to_tuple((float, int), bounds[1]), log_scale=checked_cast(bool, representation.get("log_scale", False)), is_fidelity=checked_cast(bool, representation.get("is_fidelity", False)), )
def _make_range_param( name: str, representation: TParameterRepresentation, parameter_type: Optional[str] ) -> RangeParameter: assert "bounds" in representation, "Bounds are required for range parameters." bounds = representation["bounds"] assert isinstance(bounds, list) and len(bounds) == 2, ( f"Cannot parse parameter {name}: for range parameters, json representation " "should include a list of two values, lower and upper bounds of the range." ) return RangeParameter( name=name, parameter_type=_to_parameter_type(bounds, parameter_type, name, "bounds"), lower=checked_cast_to_tuple((float, int), bounds[0]), upper=checked_cast_to_tuple((float, int), bounds[1]), log_scale=checked_cast(bool, representation.get("log_scale", False)), digits=representation.get("digits", None), # pyre-ignore[6] is_fidelity=checked_cast(bool, representation.get("is_fidelity", False)), # pyre-fixme[6]: Expected `Union[None, bool, float, int, str]` for 8th param # but got `Union[None, List[Union[None, bool, float, int, str]], bool, float, # int, str]`. target_value=representation.get("target_value", None), )
def extract_search_space_digest(search_space: SearchSpace, param_names: List[str]) -> SearchSpaceDigest: """Extract basic parameter prpoerties from a search space.""" bounds: List[Tuple[Union[int, float], Union[int, float]]] = [] ordinal_features: List[int] = [] categorical_features: List[int] = [] discrete_choices: Dict[int, List[Union[int, float]]] = {} task_features: List[int] = [] fidelity_features: List[int] = [] target_fidelities: Dict[int, Union[int, float]] = {} for i, p_name in enumerate(param_names): p = search_space.parameters[p_name] if isinstance(p, ChoiceParameter): if p.is_task: task_features.append(i) elif p.is_ordered: ordinal_features.append(i) else: categorical_features.append(i) # at this point we can assume that values are numeric due to transforms discrete_choices[i] = p.values # pyre-ignore [6] bounds.append((min(p.values), max(p.values))) # pyre-ignore [6] elif isinstance(p, RangeParameter): if p.log_scale: raise ValueError(f"{p} is log scale") if p.parameter_type == ParameterType.INT: ordinal_features.append(i) d_choices = list(range(int(p.lower), int(p.upper) + 1)) discrete_choices[i] = d_choices # pyre-ignore [6] bounds.append((p.lower, p.upper)) else: raise ValueError(f"Unknown parameter type {type(p)}") if p.is_fidelity: if not isinstance(not_none(p.target_value), (int, float)): raise NotImplementedError( "Only numerical target values are supported.") target_fidelities[i] = checked_cast_to_tuple((int, float), p.target_value) fidelity_features.append(i) return SearchSpaceDigest( feature_names=param_names, bounds=bounds, ordinal_features=ordinal_features, categorical_features=categorical_features, discrete_choices=discrete_choices, task_features=task_features, fidelity_features=fidelity_features, target_fidelities=target_fidelities, )
def extract_search_space_digest(search_space: SearchSpace, param_names: List[str]) -> SearchSpaceDigest: """Extract basic parameter prpoerties from a search space.""" bounds: List[Tuple[Union[int, float], Union[int, float]]] = [] ordinal_features: List[int] = [] categorical_features: List[int] = [] task_features: List[int] = [] fidelity_features: List[int] = [] target_fidelities: Dict[int, Union[int, float]] = {} for i, p_name in enumerate(param_names): p = search_space.parameters[p_name] if isinstance(p, ChoiceParameter): if p.parameter_type != ParameterType.INT: raise ValueError(f"Choice parameter {p} not of integer type") if p.is_task: task_features.append(i) elif p.is_ordered: ordinal_features.append(i) else: categorical_features.append(i) bounds.append( (int(p.values[0]), int(p.values[-1]))) # pyre-ignore [6] elif isinstance(p, RangeParameter): if p.log_scale: raise ValueError(f"{p} is log scale") if p.parameter_type == ParameterType.INT: task_features.append(i) # TODO: modify to ordinal_features.append(i) and adjust models # that rely on the current behavior bounds.append((p.lower, p.upper)) else: raise ValueError(f"Unknown parameter type {type(p)}") if p.is_fidelity: if not isinstance(not_none(p.target_value), (int, float)): raise NotImplementedError( "Only numerical target values are supported.") target_fidelities[i] = checked_cast_to_tuple((int, float), p.target_value) fidelity_features.append(i) return SearchSpaceDigest( feature_names=param_names, bounds=bounds, ordinal_features=ordinal_features, categorical_features=categorical_features, task_features=task_features, fidelity_features=fidelity_features, target_fidelities=target_fidelities, )