utilities Module

Utility methods for validation and conversion.

Classes

suppress_stdout_stderr

A context manager for doing a "deep suppression" of stdout and stderr.

Will suppress all print, even if the print originates in a compiled C/Fortran sub-function. Will not suppress raised exceptions, since exceptions are printed to stderr just before a script exits, and after the context manager has exited.

Create the context manager.

Functions

convert_dict_values_to_str

Convert a dictionary's values so that every value is a string.

convert_dict_values_to_str(input_dict: Dict[Any, Any]) -> Dict[str, str]

Parameters

Name Description
input_dict
Required

the dictionary that should be converted

Returns

Type Description

a dictionary with all values converted to strings

get_default_metric_with_objective

Get the dictionary of metric -> objective for the given task.

get_default_metric_with_objective(task)

Parameters

Name Description
task
Required

string "classification" or "regression"

Returns

Type Description

dictionary of metric -> objective

get_error_code

Build the error code from an exception.

get_error_code(exception: BaseException, as_hierarchy: bool = False) -> str

Parameters

Name Description
exception
Required

The exception that fails the run.

as_hierarchy

If the complete error hierarchy should be returned

Default value: False

Returns

Type Description

Return the str containing error_code. If as_hierarchy is True, the hierarchy returned is joined by a '.'

get_min_points

Return the minimum number of data points needed for training.

get_min_points(window_size: int, lags: List[int], max_horizon: int, cv: int | None, n_step: int | None = None) -> int

Parameters

Name Description
window_size
Required

the rolling window size.

lags
Required

The lag size.

max_horizon
Required

the desired length of forecasting.

cv
Required

the number of cross validations.

n_step

Number of periods between the origin_time of one CV fold and the next fold. For example, if n_step = 3 for daily data, the origin time for each fold will be three days apart.

Default value: None

Returns

Type Description

the minimum number of data points.

get_primary_metrics

Get the primary metrics supported for a given task as a list.

get_primary_metrics(task: str) -> List[str]

Parameters

Name Description
task
Required
str

Task type supported by AutoML, as defined in azureml.automl.core.shared.constants.Tasks

Returns

Type Description

A list of the primary metrics supported for the task.

get_value_float

Convert string value to float. :param floatstring: The input value to be converted. :type floatstring: str :return: The converted value. :rtype: float

get_value_float(floatstring: str) -> float | str | None

Parameters

Name Description
floatstring
Required

get_value_from_dict

Get the value of a configuration item that has a list of names.

get_value_from_dict(dictionary: Dict[str, Any], names: List[str], default_value: Any) -> Any

Parameters

Name Description
dictionary
Required

Dictionary of settings with key value pair to look the data for.

names
Required

The list of names for the item looking foi.

default_value
Required

Default value to return if no matching key found

Returns

Type Description

Returns the first value from the list of names.

get_value_int

Convert string value to int.

get_value_int(intstring: str) -> int | str | None

Parameters

Name Description
intstring
Required
str

The input value to be converted.

Returns

Type Description
int

The converted value.

interpret_exception

Translate an exception to an AzureMLException.

If the exception is already one of the known types (e.g. ServiceException, AzureMLException), return the exception as-is.

Dev note: If we see adding more exceptions, or new interpretations for remote vs. local runs, consider converting this functionality into its own class

interpret_exception(exception: BaseException, is_aml_compute: bool = True, **kwargs: Any) -> AzureMLException | ServiceException

Parameters

Name Description
exception
Required

The exception object that needs to be interpreted

is_aml_compute

If the context is an execution service managed run on an AML Compute (e.g. OSErrors, networking errors may need to be interpreted differently based on the run type)

Default value: True
kwargs
Required

Any run-time properties that the ErrorDefinition expects (such as reference_code)

Returns

Type Description

exception interpreted as an AzureMLException with error code

is_known_date_time_format

Check if a given string matches the known date time regular expressions.

is_known_date_time_format(datetime_str: str) -> bool

Parameters

Name Description
datetime_str
Required

Input string to check if it's a date or not

Returns

Type Description

Whether the given string is in a known date time format or not

minimize_or_maximize

Select the objective given a metric.

Some metrics should be minimized and some should be maximized :param metric: the name of the metric to look up :param task: one of constants.Tasks. :return: returns one of constants.OptimizerObjectives.

minimize_or_maximize(metric, task=None)

Parameters

Name Description
metric
Required
task
Default value: None
subsampling_recommended(num_samples)

Parameters

Name Description
num_samples
Required
int

number of samples.

Returns

Type Description

True if subsampling is recommended, else False.

to_ordinal_string

Convert an integer to an ordinal string.

to_ordinal_string(integer: int) -> str

Parameters

Name Description
integer
Required