File: utils.py

package info (click to toggle)
pytorch-ignite 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,712 kB
  • sloc: python: 46,874; sh: 376; makefile: 27
file content (28 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import inspect
from typing import Any, Callable, Tuple, Union


def _check_signature(fn: Callable, fn_description: str, *args: Any, **kwargs: Any) -> None:
    # if handler with filter, check the handler rather than the decorator
    if hasattr(fn, "_parent"):
        signature = inspect.signature(fn._parent())
    else:
        signature = inspect.signature(fn)
    try:  # try without engine
        signature.bind(*args, **kwargs)
    except TypeError as exc:
        fn_params = list(signature.parameters)
        exception_msg = str(exc)
        passed_params = list(args) + list(kwargs)
        raise ValueError(
            f"Error adding {fn} '{fn_description}': "
            f"takes parameters {fn_params} but will be called with {passed_params}"
            f"({exception_msg})."
        )


def _to_hours_mins_secs(time_taken: Union[float, int]) -> Tuple[int, int, float]:
    """Convert seconds to hours, mins, seconds and milliseconds."""
    mins, secs = divmod(time_taken, 60)
    hours, mins = divmod(mins, 60)
    return round(hours), round(mins), secs