from typing import Union
from typing import Sequence


def format_as_iter(vals: Union[int, Sequence[int]], iters_per_epoch: int, time_scale: str):
    """Format data to be at iteration time scale.

    If values are negative, they correspond to the opposite time scale.
    For example if `time_scale="epoch"` and `val=-1`, `val` corresponds to
    1 iteration.

    Args:
        vals (`int(s)`): Values to format.
        iters_per_epoch (int): Number of iterations per epoch.
        time_scale (str): Time scale of current values.

    Returns:
        int: Time (positive int) formatted in iteration format
    """
    assert time_scale in ["epoch", "iter"]

    single_value = not isinstance(vals, Sequence)
    if single_value:
        vals = [vals]

    epoch_convention = (time_scale == "epoch" and all(x >= 0 for x in vals)) or (
        time_scale == "iter" and all(x <= 0 for x in vals)
    )

    if epoch_convention:
        vals = type(vals)([iters_per_epoch * abs(x) for x in vals])
    else:
        vals = type(vals)([abs(x) for x in vals])

    if single_value:
        return vals[0]
    else:
        return vals