Utilities#

class configilm.util.AverageMeter#

Computes and stores the average and current value of some kind of sequential calls.

__init__(name, fmt=':f')#

Initializes the counter.

Parameters:
  • name (str) – Name of the counter during __str__

  • fmt (str) – format of the numbers during __str__

reset()#

Resets the counter and all properties to the default value 0.

Return type:

None

update(val, n=1)#

Update the AverageMeter with a new value or the same value multiple times.

Parameters:
  • val – value to update the AverageMeter with

  • n (int) – how often to update with this value

Return type:

None

class configilm.util.MessageLevel#

An enumeration.

class configilm.util.Messages#

Class to print messages. Defines colors and formatting for bash shells. Class methods are wrapper for specific formatting combinations and can be called without defining an object.

FIELDS:
HEADER:

purple

OKBLUE:

blue

OKCYAN:

blue - used in HINT messages

OKGREEN:

green - used in SUCCESS messages

WARNING:

yellow - used in WARNING messages

FAIL:

red - used in ERROR messages

ENDC:

formatting: reverts to default

BOLD:

formatting: bolt font

UNDERLINE:

formatting: underlined font

Example:

print(f"{Messages.WARNING}WARNING: This is a Warning.{Messages.ENDC}")
Messages.warn("This is a Warning.")  # equal result
classmethod error(message, with_time=False)#

Prints a red error message with aligned indent and “[ERROR]”. If the message has multiple lines, it is aligned to the right of the colon. Prints only if the message level is set to ERROR. Message level is defined by the global variable MESSAGE_LEVEL. Printing is done with the global variable MESSAGE_PRINT_FN, which defaults to print.

Parameters:
  • message (str) – Message to print

  • with_time (bool) – If set, the time is printed in front of the message

Return type:

None

classmethod hint(message, with_time=False)#

Prints a blue hint message with aligned indent and “[HINT]”. If the message has multiple lines, it is aligned to the right of the colon. Prints only if the message level is set to HINT or higher. Message level is defined by the global variable MESSAGE_LEVEL. Printing is done with the global variable MESSAGE_PRINT_FN, which defaults to print.

Parameters:
  • message (str) – Message to print

  • with_time (bool) – If set, the time is printed in front of the message

Return type:

None

classmethod info(message, with_time=False)#

Prints a dark blue info message with aligned indent and “[INFO]”. If the message has multiple lines, it is aligned to the right of the colon. Prints only if the message level is set to INFO or higher. Message level is defined by the global variable MESSAGE_LEVEL. Printing is done with the global variable MESSAGE_PRINT_FN, which defaults to print.

Parameters:
  • message (str) – Message to print

  • with_time (bool) – If set, the time is printed in front of the message

Return type:

None

classmethod success(message, with_time=False)#

Prints a green success message with aligned indent and “[SUCCESS]” If the message has multiple lines, it is aligned to the right of the colon. Prints only if the message level is set to SUCCESS or higher. Message level is defined by the global variable MESSAGE_LEVEL. Printing is done with the global variable MESSAGE_PRINT_FN, which defaults to print.

Parameters:
  • message (str) – Message to print

  • with_time (bool) – If set, the time is printed in front of the message

Return type:

None

classmethod warn(message, with_time=False)#

Prints a yellow warning message with aligned indent and “[WARNING]”. If the message has multiple lines, it is aligned to the right of the colon. Prints only if the message level is set to WARNING or higher. Message level is defined by the global variable MESSAGE_LEVEL. Printing is done with the global variable MESSAGE_PRINT_FN, which defaults to print.

Parameters:
  • message (str) – Message to print

  • with_time (bool) – If set, the time is printed in front of the message

Return type:

None

configilm.util.convert(multi_true, multi_pred)#

Helper function to convert labels and logits into a format that is accepted by wandb.plot. Applicable for multi-class classification (not multi-label).

Example: tensor(0,0,1,0,0,0,1,…,0) will become array(2,6,…) for ground truth input (multi_true)

Parameters:
  • multi_true – list of multi hot labels of value 0 or 1, array has dimension n*d

  • multi_pred – list of logits, array has dimension n*d

Returns:

(labels, logits) as requested by wandb.plot

configilm.util.huggingface_tokenize_and_pad(tokenizer, string, seq_length)#

Tokenizes a string given a huggingface tokenizer. Assumes, that the tokenizer has a “[CLS]” start token, a “[SEP]” end token and a “[PAD]” padding token. If the string is too long it will be cut to the specific length. It the string is too short, it will be padded with the “[PAD]” token.

Parameters:
  • tokenizer – hugging face tokenizer

  • string (str) – string to be encoded

  • seq_length (int) – length of the output

Returns:

list of integers (IDs) of the tokenized string of length seq_length

configilm.util.indent(s, num_spaces=0, indent_first=False)#

Indents a string by a number of spaces. Indents every line individually except for the first line if indent_first is not set.

Parameters:
  • s – string to indent

  • num_spaces – number of spaces to indent

  • indent_first – if set, first line is indented as well, otherwise no spaces added to first line

Returns:

s with each line indented

configilm.util.round_to(x, base)#

Rounds to an arbitrary base, e.g. 5 or 2 or 0.002.

Result is not exact.

Parameters:
  • x – number to be rounded

  • base – base

Returns:

rounded number

Example:
>>> round_to(5, 2)
4
>>> round_to(5.1, 0.2)
5.0
>>> round_to(5.199, 0.2)
5.2