itwinai.utilsο
Utilities for itwinai package.
- itwinai.utils.normalize_tracking_uri(uri: str) str[source]ο
Normalize a tracking URI to a valid URI. If the URI is empty, it will be treated as the current directory.
- Parameters:
uri (str) β URI to normalize.
- Returns:
Normalized URI.
- Return type:
str
- itwinai.utils.load_yaml(path: str) Dict[source]ο
Load YAML file as dict.
- Parameters:
path (str) β path to YAML file.
- Raises:
yaml.YAMLError β for loading/parsing errors.
- Returns:
nested dict representation of parsed YAML file.
- Return type:
Dict
- itwinai.utils.dynamically_import_class(name: str) Type[source]ο
Dynamically import class by module path. Adapted from https://stackoverflow.com/a/547867
- Parameters:
name (str) β path to the class (e.g., mypackage.mymodule.MyClass)
- Returns:
class type.
- Return type:
__class__
- itwinai.utils.flatten_dict(d: MutableMapping, parent_key: str = '', sep: str = '.') MutableMapping[source]ο
Flatten dictionary
- Parameters:
d (MutableMapping) β nested dictionary to flatten
parent_key (str, optional) β prefix for all keys. Defaults to ββ.
sep (str, optional) β separator for nested key concatenation. Defaults to β.β.
- Returns:
flattened dictionary with new keys.
- Return type:
MutableMapping
- class itwinai.utils.SignatureInspector(func: Callable)[source]ο
Bases:
objectProvides the functionalities to inspect the signature of a function or a method.
- Parameters:
func (Callable) β function to be inspected.
- INFTY: int = 9223372036854775807ο
- property has_varargs: boolο
Checks if the function has
*argsparameter.
- property has_kwargs: boolο
Checks if the function has
**kwargsparameter.
- property required_params: Tuple[str]ο
Names of required parameters. Class methodβs βselfβ is skipped.
- property min_params_num: intο
Minimum number of arguments required.
- property max_params_num: intο
Max number of supported input arguments. If no limit,
SignatureInspector.INFTYis returned.
- itwinai.utils.clear_key(my_dict: Dict, dict_name: str, key: Hashable, complain: bool = True) Dict[source]ο
Remove key from dictionary if present and complain.
- Parameters:
my_dict (Dict) β Dictionary.
dict_name (str) β name of the dictionary.
key (Hashable) β Key to remove.
- itwinai.utils.make_config_paths_absolute(args: List[str])[source]ο
Process CLI arguments to make paths specified for βconfig-path or -cp absolute. Returns the modified arguments list.
- Parameters:
args (List[str]) β a list of system arguments
- Returns:
- the updated list of system arguments, where all the config path argument is
absolute.
- Return type:
List(str)
- itwinai.utils.get_root_cause(exception: Exception) Exception[source]ο
Recursively extract the first exception in the exception chain.
- itwinai.utils.to_uri(path_str: str | Path) str[source]ο
Parse a path and convert it to a URI.
- Parameters:
path_str (str) β path to convert.
- Returns:
URI.
- Return type:
str
- itwinai.utils.time_and_log(func: Callable, logger: Logger, identifier: str, step: int | None = None, destroy_current_logger_context: bool = False) Any[source]ο
Time and log the execution of a function (using time.monotonic()).
- Parameters:
func (Callable) β function to execute, time and log, expects zero arguments. Use partial from functools if you need to add arguments.
logger
identifier (str) β identifier for the logged metric
step (int | None) β step for logging. Defaults to None.
destroy_current_logger_context (bool) β Whether to destroy the current logger context. Default is False.
- Returns:
result of the function call
- Return type:
result (Any)
- itwinai.utils.filter_pipeline_steps(pipeline_cfg: DictConfig, pipe_steps: List[Any]) None[source]ο
Filters the steps in the pipeline configuration, pipeline_cfg, using pipe_steps, and validates the provided pipe_steps. Changes the pipeline_cfg object in-place. In the event of a validation error, the program is exited, as this is expected to be used from the CLI.
- itwinai.utils.retrieve_remote_omegaconf_file(url: str) DictConfig | ListConfig[source]ο
Fetches and parses a remote OmegaConf configuration file.
- Parameters:
url β URL to the raw configuration file (YAML/JSON format), e.g. raw GitHub link.
- Returns:
Parsed OmegaConf configuration as DictConfig or ListConfig.
- Raises:
typer.Exit β If the request to the URL or the parsing fails.