Skip to content

iden.utils

iden.utils

Contain the utility functions.

iden.utils.format

Contain utility functions to compute formatted strings.

iden.utils.format.human_time

human_time(seconds: float) -> str

Return a number of seconds in an easier format to read hh:mm:ss.

If the number of seconds is bigger than 1 day, this representation also encodes the number of days.

Parameters:

Name Type Description Default
seconds float

The number of seconds.

required

Returns:

Type Description
str

The number of seconds in a string format (hh:mm:ss).

Example usage:

>>> from iden.utils.format import human_time
>>> human_time(1.2)
'0:00:01.200000'
>>> human_time(61.2)
'0:01:01.200000'
>>> human_time(3661.2)
'1:01:01.200000'

iden.utils.imports

Implement some utility functions to manage optional dependencies.

iden.utils.imports.check_safetensors

check_safetensors() -> None

Check if the safetensors package is installed.

Raises:

Type Description
RuntimeError

if the safetensors package is not installed.

Example usage:

>>> from iden.utils.imports import check_safetensors
>>> check_safetensors()

iden.utils.imports.check_yaml

check_yaml() -> None

Check if the yaml package is installed.

Raises:

Type Description
RuntimeError

if the yaml package is not installed.

Example usage:

>>> from iden.utils.imports import check_yaml
>>> check_yaml()

iden.utils.imports.is_safetensors_available

is_safetensors_available() -> bool

Indicate if the safetensors package is installed or not.

Returns:

Type Description
bool

True if safetensors is available otherwise False.

Example usage:

>>> from iden.utils.imports import is_safetensors_available
>>> is_safetensors_available()

iden.utils.imports.is_yaml_available

is_yaml_available() -> bool

Indicate if the yaml package is installed or not.

Returns:

Type Description
bool

True if yaml is available otherwise False.

Example usage:

>>> from iden.utils.imports import is_yaml_available
>>> is_yaml_available()

iden.utils.imports.safetensors_available

safetensors_available(
    fn: Callable[..., Any]
) -> Callable[..., Any]

Implement a decorator to execute a function only if safetensors package is installed.

Parameters:

Name Type Description Default
fn Callable[..., Any]

Specifies the function to execute.

required

Returns:

Type Description
Callable[..., Any]

A wrapper around fn if safetensors package is installed, otherwise None.

Example usage:

>>> from iden.utils.imports import safetensors_available
>>> @safetensors_available
... def my_function(n: int = 0) -> int:
...     return 42 + n
...
>>> my_function()

iden.utils.imports.yaml_available

yaml_available(
    fn: Callable[..., Any]
) -> Callable[..., Any]

Implement a decorator to execute a function only if yaml package is installed.

Parameters:

Name Type Description Default
fn Callable[..., Any]

Specifies the function to execute.

required

Returns:

Type Description
Callable[..., Any]

A wrapper around fn if yaml package is installed, otherwise None.

Example usage:

>>> from iden.utils.imports import yaml_available
>>> @yaml_available
... def my_function(n: int = 0) -> int:
...     return 42 + n
...
>>> my_function()

iden.utils.path

Contain path utility functions.

iden.utils.path.sanitize_path

sanitize_path(path: Path | str) -> Path

Sanitize the given path.

Parameters:

Name Type Description Default
path Path | str

Specifies the path to sanitize.

required

Returns:

Type Description
Path

The sanitized path.

Example usage:

>>> from pathlib import Path
>>> from iden.utils.path import sanitize_path
>>> sanitize_path("something")
PosixPath('.../something')
>>> sanitize_path("")
PosixPath('...')
>>> sanitize_path(Path("something"))
PosixPath('.../something')
>>> sanitize_path(Path("something/./../"))
PosixPath('...')

iden.utils.time

Contain utility functions to measure time.

iden.utils.time.sync_perf_counter

sync_perf_counter() -> float

Extension of time.perf_counter that waits for all kernels in all streams on a CUDA device to complete.

Returns:

Type Description
float

Same as time.perf_counter(). See https://docs.python.org/3/library/time.html#time.perf_counter for more information.

Example usage:

>>> from iden.utils.time import sync_perf_counter
>>> tic = sync_perf_counter()
>>> x = [1, 2, 3]
>>> toc = sync_perf_counter()
>>> toc - tic

iden.utils.time.timeblock

timeblock(
    message: str = "Total time: {time}",
) -> Generator[None, None, None]

Implement a context manager to measure the execution time of a block of code.

Parameters:

Name Type Description Default
message str

The message displayed when the time is logged.

'Total time: {time}'

Example usage:

>>> from iden.utils.time import timeblock
>>> with timeblock():
...     x = [1, 2, 3]
...
>>> with timeblock("Training: {time}"):
...     y = [1, 2, 3]
...