stable_learning_control.utils.eval_robustness

Contains a utility that can be used to evaluate the stability and robustness of an algorithm. See the Robustness Evaluation Documentation for more information.

Module Contents

Functions

get_human_readable_disturber_label(disturber_label)

Get a human readable label for a given disturber label.

add_disturbance_label_column(dataframe)

Add a column that contains a disturbance label. This label will be created

get_available_disturbers()

Get all disturbers that are available in the stable_learning_control package.

get_available_disturbers_string()

Get a string with all available disturbers that are available in the

print_available_disturbers()

Print all available disturbers that are available in the

load_disturber(disturber_id)

Load a given disturber. The disturber name can include an unloaded module in

retrieve_disturber_variants(disturber_range_dict)

Retrieves all disturber variants from the given disturber configuration

run_disturbed_policy(env, policy, disturber, ...[, ...])

Evaluates the disturbed policy inside a given gymnasium environment. This

plot_robustness_results(dataframe[, observations, ...])

Creates several useful plots out of a robustness evaluation dataframe that was

Attributes

args

stable_learning_control.utils.eval_robustness.get_human_readable_disturber_label(disturber_label)[source]

Get a human readable label for a given disturber label.

Parameters:

disturber_label (str) – The disturber label.

Returns:

The human readable disturber label.

Return type:

str

stable_learning_control.utils.eval_robustness.add_disturbance_label_column(dataframe)[source]

Add a column that contains a disturbance label. This label will be created by concatenating all disturber parameters.

Note

This function will not add a disturbance label if the dataframe already contains a disturbance label.

Parameters:

dataframe (pd.DataFrame) – The dataframe containing the disturber parameters.

Returns:

The dataframe with the disturbance label column.

Return type:

pd.DataFrame

Raises:

ValueError – If the dataframe does not contain a ‘disturber’ column.

stable_learning_control.utils.eval_robustness.get_available_disturbers()[source]

Get all disturbers that are available in the stable_learning_control package.

Returns:

List with all available disturbers.

Return type:

list

stable_learning_control.utils.eval_robustness.get_available_disturbers_string()[source]

Get a string with all available disturbers that are available in the stable_learning_control package.

Returns:

String with all available disturbers.

Return type:

str

stable_learning_control.utils.eval_robustness.print_available_disturbers()[source]

Print all available disturbers that are available in the stable_learning_control package.

stable_learning_control.utils.eval_robustness.load_disturber(disturber_id)[source]

Load a given disturber. The disturber name can include an unloaded module in “module:disturber_name” style. If no module is given, the disturber is loaded from the stable_learning_control.disturbers module.

Parameters:

disturber_id (str) – The name of the disturber you want to load.

Returns:

The loaded disturber object.

Raises:
stable_learning_control.utils.eval_robustness.retrieve_disturber_variants(disturber_range_dict)[source]

Retrieves all disturber variants from the given disturber configuration dictionary. Variants are created by combining the key values over indexes.

Parameters:

disturber_range_dict (dict) – The disturber configuration dictionary.

Returns:

List with all disturber variants.

Return type:

list

Raises:
  • TypeError – Thrown when the values in the disturber configuration variables are not of type float, int or list.

  • ValueError – Thrown when the values in the disturber configuration variables do not have the same length.

stable_learning_control.utils.eval_robustness.run_disturbed_policy(env, policy, disturber, disturber_config, include_baseline=True, max_ep_len=None, num_episodes=100, render=True, deterministic=True, save_result=False, output_dir=None, use_wandb=False, wandb_job_type=None, wandb_project=None, wandb_group=None, wandb_run_name=None)[source]

Evaluates the disturbed policy inside a given gymnasium environment. This function loops to all the disturbances that are specified in the environment and outputs the results of all these episodes as a :obj:pandas.Dataframe`.

Parameters:
  • env (gym.env) – The gymnasium environment.

  • policy (Union[tf.keras.Model, torch.nn.Module]) – The policy.

  • (obj (disturber) – gym.Wrapper): The disturber you want to use.

  • disturber_config (dict) – The disturber configuration dictionary. Contains the variables that you want to pass to the disturber. It sets up the range of disturbances you wish to evaluate.

  • include_baseline (bool) – Whether you want to automatically add the baseline (i.e. zero disturbance) when it not present. Defaults to True.

  • max_ep_len (int, optional) – The maximum episode length. Defaults to None. Meaning the maximum episode length of the environment is used.

  • num_episodes (int, optional) – Number of episodes you want to perform in the environment. Defaults to 100.

  • render (bool, optional) – Whether you want to render the episode to the screen. Defaults to True.

  • deterministic (bool, optional) – Whether you want the action from the policy to be deterministic. Defaults to True.

  • save_result (bool, optional) – Whether you want to save the dataframe with the results to disk. Defaults to False.

  • output_dir (str, optional) – A directory for saving the diagnostics to. If None, defaults to a temp directory of the form /tmp/experiments/somerandomnumber.

  • use_wandb (bool, optional) – Whether to use Weights & Biases for logging. Defaults to False.

  • wandb_job_type (str, optional) – The type of job you are running. Defaults to None.

  • wandb_project (str, optional) – The name of the Weights & Biases project. Defaults to None which means that the project name is automatically generated.

  • wandb_group (str, optional) – The name of the Weights & Biases group you want to assign the run to. Defaults to None.

  • wandb_run_name (str, optional) – The name of the Weights & Biases run. Defaults to None which means that the run name is automatically generated.

Returns:

Dataframe that contains information about all the

episodes and disturbances.

Return type:

pandas.DataFrame

Raises:

AssertionError – Thrown when the environment or policy is not found.

stable_learning_control.utils.eval_robustness.plot_robustness_results(dataframe, observations=None, references=None, reference_errors=None, absolute_reference_errors=False, merge_reference_errors=False, use_subplots=False, use_time=False, save_plots=False, font_scale=1.5, figs_fmt='pdf', output_dir=None, use_wandb=False, wandb_job_type=None, wandb_project=None, wandb_group=None, wandb_run_name=None)[source]

Creates several useful plots out of a robustness evaluation dataframe that was collected in the run_disturbed_policy() method.

Parameters:
  • dataframe (pandas.DataFrame) – The data frame that contains the robustness evaluation information information.

  • observations (list) – The observations you want to show in the observations plot. By default for clarity only the first 6 observations are shown.

  • references (list) – The references you want to show in the reference plot. By default for clarity only the first references is shown.

  • reference_errors (list) – The reference errors you want to show in the reference error plot. By default for clarity only the first reference error is shown.

  • absolute_reference_errors (bool) – Whether you want to plot the absolute reference errors instead of relative reference errors. Defaults to False.

  • merge_reference_errors (bool) – Whether you want to merge the reference errors into one reference error. Defaults to False.

  • use_subplots (bool) – Whether you want to use subplots instead of separate figures. Defaults to False.

  • use_time (bool) – Whether you want to use the time as the x-axis. Defaults to False.

  • save_plots (bool) – Whether you want to save the created plots to disk. Defaults to False.

  • font_scale (int) – The font scale you want to use for the plot text. Defaults to 1.5.

  • figs_fmt (str, optional) – In which format you want to save the plots. Defaults to pdf.

  • output_dir (str, optional) – The directory where you want to save the output figures to. If None, defaults to a temp directory of the form /tmp/experiments/somerandomnumber.

  • use_wandb (bool, optional) – Whether to use Weights & Biases for logging. Defaults to False.

  • wandb_job_type (str, optional) – The type of job you are running. Defaults to None.

  • wandb_project (str, optional) – The name of the Weights & Biases project. Defaults to None which means that the project name is automatically generated.

  • wandb_group (str, optional) – The name of the Weights & Biases group you want to assign the run to. Defaults to None.

  • wandb_run_name (str, optional) – The name of the Weights & Biases run. Defaults to None which means that the run name is automatically generated.

stable_learning_control.utils.eval_robustness.args[source]