Skip to content

plkit.utils

module

plkit.utils

Utility functions for plkit

Functions
  • capture_stderr() Capture the stderr</>
  • capture_stdout() Capture the stdout</>
  • check_config(config, item, how, msg) Check configuration items</>
  • collapse_suggest_config(config) (dict) Use the default value of OptunaSuggest for config items. So that the configs can be used in the case that optuna is opted out.</>
  • log_config(config, title, items_per_row) Log the configurations in a table in terminal</>
  • normalize_tvt_ratio(tvt_ratio) (int or float, list of int or float, list of int or float), optional Normalize the train-val-test data ratio into a format of (.7, [.1, .1], [.05, .05]).</>
  • output_to_logging(stdout_level, stderr_level) Capture the stdout or stderr to logging</>
  • plkit_seed_everything(config) Try to seed everything and set deterministic to True if seed in config has been set</>
  • warning_to_logging() Patch the warning message formatting to only show the message</>
function

plkit.utils.check_config(config, item, how=<function <lambda> at 0x7f7e732ea040>, msg='Configuration item {key} is required.')

Check configuration items

Parameters
  • config (dict) The configuration dictionary
  • item (str) The configuration key to check
  • how (callable) How to check. Return False to fail the check.
  • msg (str) The message to show in the exception. {key} is available to refer to the key checked.
Raises
  • PlkitConfigException When the check fails
function

plkit.utils.collapse_suggest_config(config)

Use the default value of OptunaSuggest for config items. So that the configs can be used in the case that optuna is opted out.

Parameters
  • config (dict) The configuration dictionary
Returns (dict)

The collapsed configuration

function

plkit.utils.normalize_tvt_ratio(tvt_ratio)

Normalize the train-val-test data ratio into a format of (.7, [.1, .1], [.05, .05]).

For config.data_tvt, the first element is required. If val or test ratios are not provided, it will be filled with None

All numbers could be absolute numbers (>1) or ratios (<=1)

Parameters
  • tvt_ratio (Union(int, float, iterable of int or float, nonetype)) The train-val-test ratio
Returns (int or float, list of int or float, list of int or float), optional

The normalized ratios

Raises
  • PlkitConfigException When the passed-in tvt_ratio is in malformat
generator

plkit.utils.warning_to_logging()

Patch the warning message formatting to only show the message

generator

plkit.utils.capture_stdout()

Capture the stdout

generator

plkit.utils.capture_stderr()

Capture the stderr

generator

plkit.utils.output_to_logging(stdout_level='info', stderr_level='error')

Capture the stdout or stderr to logging

function

plkit.utils.log_config(config, title='Configurations', items_per_row=1)

Log the configurations in a table in terminal

Parameters
  • config (dict) The configuration dictionary
  • title (str) The title of the table
  • items_per_row (int) The number of items to print per row
function

plkit.utils.plkit_seed_everything(config)

Try to seed everything and set deterministic to True if seed in config has been set

Parameters
  • config (FrozenDiot) The configurations