mythos.utils.helpers

Helper functions for the mythos package.

Attributes

ERR_BATCHED_N

Functions

batched(→ collections.abc.Iterable[Any])

Batch an iterable into chunks of size n.

tree_stack(→ jaxtyping.PyTree)

Stacks corresponding leaves of PyTrees into arrays along a new axis.

tree_concatenate(→ jaxtyping.PyTree)

Concatenates corresponding leaves of PyTrees along the first axis.

tail_file(→ str)

Return the last n lines of a file as a string.

run_command(→ None)

Run a command in a subprocess, raising RuntimeError on failure.

Module Contents

mythos.utils.helpers.ERR_BATCHED_N = 'n must be at least one'
mythos.utils.helpers.batched(iterable: collections.abc.Iterable[Any], n: int) collections.abc.Iterable[Any][source]

Batch an iterable into chunks of size n.

Parameters:
  • iterable (iter[Any]) – iterable to batch

  • n (int) – batch size

Returns:

batched iterable

Return type:

iter[Any]

mythos.utils.helpers.tree_stack(trees: list[jaxtyping.PyTree]) jaxtyping.PyTree[source]

Stacks corresponding leaves of PyTrees into arrays along a new axis.

mythos.utils.helpers.tree_concatenate(trees: list[jaxtyping.PyTree]) jaxtyping.PyTree[source]

Concatenates corresponding leaves of PyTrees along the first axis.

mythos.utils.helpers.tail_file(path: pathlib.Path, n: int = 10) str[source]

Return the last n lines of a file as a string.

mythos.utils.helpers.run_command(cmd: list[str], cwd: pathlib.Path, log_prefix: str = 'command-output', err_tail_lines: int = 20) None[source]

Run a command in a subprocess, raising RuntimeError on failure.

Stderr and stdout are captured to files in the cwd directory, named with provided prefix. If the process fails with a CalledProcessError, the last tail_lines of each log file are included in the raised RuntimeError.

Parameters:
  • cmd (list[str]) – command and arguments to run

  • cwd (Path) – working directory to run the command in

  • log_prefix (str) – prefix for the output log files, within the cwd directory

  • err_tail_lines (int) – number of lines from the end of each log file to include in the error message on failure