vp_suite.utils.utils

exception PytestExpectedException

Bases: Exception

A custom exception type that, when raised during pytest execution, causes pytest to skip the current test.

exception TimeOutException

Bases: Exception

A custom Exception type thrown when signals time out (e.g. for timed input prompts).

class TqdmUpTo(*_, **__)

Bases: tqdm.std.tqdm

A wrapper class around the tqdm progress bar that can be used for showing download progress. Provides update_to(n) which uses tqdm.update(delta_n). Taken from https://gist.github.com/leimao/37ff6e990b3226c2c9670a2cd1e4a6f5, Inspired by [twine#242](https://github.com/pypa/twine/pull/242), [mentioned here](https://github.com/pypa/twine/commit/42e55e06).

update_to(b=1, bsize=1, tsize=None)

Updates the tqdm progress indicator. b (int): Number of blocks transferred so far [default: 1]. bsize (int): Size of each block (in tqdm units) [default: 1]. tsize (int): Total size (in tqdm units). If [default: None] remains unchanged.

alarm_handler(signum, frame)

A simple alarm handler that raises a TimeoutException.

check_optuna_config(optuna_cfg)

Checks whether the syntax of given optuna configuration dict is correct.

Parameters

optuna_cfg (dict) – The optuna configuration to be checked.

download_from_url(url, dst_path)

Downloads the contents of specified URL to the specified destination filepath. Uses TqdmUpTo to show download progress.

Parameters
  • url (str) – The URL to download from.

  • dst_path (str) – The path to save the downloaded data to.

get_frame_count(fp)
Parameters

fp (Union[Path, str]) – The filepath of the video to be checked.

Returns: The number of frames in the video at given filepath

get_public_attrs(obj, calling_method=None, non_config_vars=None, model_mode=False)

Similarly to inspect.getmembers(), this method returns a dictionary containing all public attributes of an object.

Parameters
  • obj (Any) – The object to check.

  • calling_method (str) – The name of the calling method. If e.g. this gets called from one of the object’s property fields, it has to be excluded to avoid recursion.

  • non_config_vars (List[str]) – A list of the attributes that should not be included in the result.

  • model_mode (bool) – If set to True, disregards any attributes that are torch.nn.Module or torch.Tensor objects.

Returns: A dictionary containing all public attributes of an object.

most(l, factor=0.67)
Parameters
  • l (List[bool]) – The input list to check.

  • factor (float) – The fraction that needs to be surpassed.

Returns

Similar to List.all(), returns True if more than the specified fraction of entries is true in given list, False otherwise (e.g. for factor=0.5, returns True if at least half the entries of l are True.

read_video(fp, img_size=None, start_index=0, num_frames=- 1)

Reads and returns the video specified by given file path as a numpy array.

Parameters
  • fp (Union[Path, str]) – The filepath to read the video from.

  • img_size ((int, int)) – The desired frame size (height and width; frames will be reshaped to this size)

  • start_index (int) – Index of first frame to read.

  • num_frames (int) – Nmber of frames to read (default value -1 signifies that video is read to the end).

Returns: The read video as a numpy array of shape (frames, height, width, channels).

run_shell_command(command)

Runs the given command in the shell.

Parameters

command (str) – The given command as a single string.

set_from_kwarg(obj, kwarg_dict, attr_name, default=None, required=False, choices=None, skip_unusable=False)

Sets an attribute in given object by using given name, configuration dict and utility values.

Parameters
  • obj (Any) – The object in which the attribute should be set.

  • kwarg_dict (dict) – The configuration dict where the attribute should be taken from.

  • attr_name (str) – The attribute name.

  • default (Any) – If the given config. dict does not contain the given attribute name as key, this default value will be used instead.

  • required (bool) – If set to True, will raise an Error rather than using the default value.

  • choices (Any) – If specified, checks whether the value(s) to be set is (are) one of the valid choices.

  • skip_unusable (bool) – If specified, skips attributes that are not already defined in the object.

timed_input(description, default=None, secs=60)

A wrapper around the default input() statement, imposing a time limit and providing default values if the input is empty. :type description: str :param description: A description text that will be displayed with the input prompt. :type description: str :param default: The default value to assign to the variable if the the input is empty. :type default: Any :type secs: int :param secs: Time limit in seconds. :type secs: int

Returns: The input value (or the default value if input is empty).

timestamp(program)
Parameters

program (str) – A string identifier specifying which run is asking for the timestamp.

Returns: A timestamp of the current system time in a human-readable way, prepended by the given program string.