mlos_bench.util module¶
Various helper functions for mlos_bench.
- mlos_bench.util.check_required_params(config: Mapping[str, Any], required_params: Iterable[str]) None ¶
Check if all required parameters are present in the configuration. Raise ValueError if any of the parameters are missing.
- Parameters:
- configdict
Free-format dictionary with the configuration of the service or benchmarking environment.
- required_paramsIterable[str]
A collection of identifiers of the parameters that must be present in the configuration.
- mlos_bench.util.datetime_parser(datetime_col: Series, *, origin: Literal['utc', 'local']) Series ¶
Attempt to convert a pandas column to a datetime format.
- Parameters:
- datetime_colpandas.Series
The column to convert.
- originLiteral[“utc”, “local”]
Whether to interpret naive timestamps as originating from UTC or local time.
- Returns:
- pandas.Series
The converted datetime column.
- Raises:
- ValueError
On parse errors.
- mlos_bench.util.get_class_from_name(class_name: str) type ¶
Gets the class from the fully qualified name.
- Parameters:
- class_namestr
Fully qualified class name.
- Returns:
- type
Class object.
- mlos_bench.util.get_git_info(path: str = '/workspaces/MLOS/mlos_bench/mlos_bench/util.py') Tuple[str, str, str] ¶
Get the git repository, commit hash, and local path of the given file.
- Parameters:
- pathstr
Path to the file in git repository.
- Returns:
- (git_repo, git_commit, git_path)Tuple[str, str, str]
Git repository URL, last commit hash, and relative file path.
- mlos_bench.util.instantiate_from_config(base_class: Type[BaseTypeVar], class_name: str, *args: Any, **kwargs: Any) BaseTypeVar ¶
Factory method for a new class instantiated from config.
- Parameters:
- base_classtype
Base type of the class to instantiate. Currently it’s one of {Environment, Service, Optimizer}.
- class_namestr
FQN of a Python class to instantiate, e.g., “mlos_bench.environments.remote.HostEnv”. Must be derived from the base_class.
- argslist
Positional arguments to pass to the constructor.
- kwargsdict
Keyword arguments to pass to the constructor.
- Returns:
- instUnion[Environment, Service, Optimizer, Storage]
An instance of the class_name class.
- mlos_bench.util.merge_parameters(*, dest: dict, source: dict | None = None, required_keys: Iterable[str] | None = None) dict ¶
Merge the source config dict into the destination config. Pick from the source configs ONLY the keys that are already present in the destination config.
- Parameters:
- destdict
Destination config.
- sourceOptional[dict]
Source config.
- required_keysOptional[Iterable[str]]
An optional list of keys that must be present in the destination config.
- Returns:
- destdict
A reference to the destination config after the merge.
- mlos_bench.util.nullable(func: Callable, value: Any | None) Any | None ¶
Poor man’s Maybe monad: apply the function to the value if it’s not None.
- Parameters:
- funcCallable
Function to apply to the value.
- valueOptional[Any]
Value to apply the function to.
- Returns:
- valueOptional[Any]
The result of the function application or None if the value is None.
- mlos_bench.util.path_join(*args: str, abs_path: bool = False) str ¶
Joins the path components and normalizes the path.
- Parameters:
- argsstr
Path components.
- abs_pathbool
If True, the path is converted to be absolute.
- Returns:
- str
Joined path.
- mlos_bench.util.prepare_class_load(config: dict, global_config: Dict[str, Any] | None = None) Tuple[str, Dict[str, Any]] ¶
Extract the class instantiation parameters from the configuration.
- Parameters:
- configdict
Configuration of the optimizer.
- global_configdict
Global configuration parameters (optional).
- Returns:
- (class_name, class_config)(str, dict)
Name of the class to instantiate and its configuration.
- mlos_bench.util.preprocess_dynamic_configs(*, dest: dict, source: dict | None = None) dict ¶
Replaces all $name values in the destination config with the corresponding value from the source config.
- Parameters:
- destdict
Destination config.
- sourceOptional[dict]
Source config.
- Returns:
- destdict
A reference to the destination config after the preprocessing.
- mlos_bench.util.try_parse_val(val: str | None) int | float | str | None ¶
Try to parse the value as an int or float, otherwise return the string.
This can help with config schema validation to make sure early on that the args we’re expecting are the right type.
- Parameters:
- valstr
The initial cmd line arg value.
- Returns:
- TunableValue
The parsed value.
- mlos_bench.util.utcify_nullable_timestamp(timestamp: datetime | None, *, origin: Literal['utc', 'local']) datetime | None ¶
A nullable version of utcify_timestamp.
- mlos_bench.util.utcify_timestamp(timestamp: datetime, *, origin: Literal['utc', 'local']) datetime ¶
Augment a timestamp with zoneinfo if missing and convert it to UTC.
- Parameters:
- timestampdatetime
A timestamp to convert to UTC. Note: The original datetime may or may not have tzinfo associated with it.
- originLiteral[“utc”, “local”]
Whether the source timestamp is considered to be in UTC or local time. In the case of loading data from storage, where we intentionally convert all timestamps to UTC, this can help us retrieve the original timezone when the storage backend doesn’t explicitly store it. In the case of receiving data from a client or other source, this can help us convert the timestamp to UTC if it’s not already.
- Returns:
- datetime
A datetime with zoneinfo in UTC.