Skip to content

iden.utils

iden.utils

Contain the utility functions.

iden.utils.fallback

Contain fallback implementations used when optional dependencies are missing, ensuring the package remains importable and can operate with limited functionality.

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
>>> 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.format.str_kwargs

str_kwargs(mapping: Mapping[Any, Any]) -> str

Return a string of the input mapping.

This function is designed to be used in __repr__ and __str__ methods.

Parameters:

Name Type Description Default
mapping Mapping[Any, Any]

The mapping of key-value pairs to format as a string.

required

Returns:

Type Description
str

The generated string.

Example
>>> from iden.utils.format import str_kwargs
>>> str_kwargs({"key1": 1})
', key1=1'
>>> str_kwargs({"key1": 1, "key2": 2})
', key1=1, key2=2'

iden.utils.imports

Implement some utility functions to manage optional dependencies.

iden.utils.imports.check_cloudpickle

check_cloudpickle() -> None

Check if the cloudpickle package is installed.

Raises:

Type Description
RuntimeError

if the cloudpickle package is not installed.

Example
>>> from iden.utils.imports import check_cloudpickle
>>> check_cloudpickle()

iden.utils.imports.check_joblib

check_joblib() -> None

Check if the joblib package is installed.

Raises:

Type Description
RuntimeError

if the joblib package is not installed.

Example
>>> from iden.utils.imports import check_joblib
>>> check_joblib()

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
>>> 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
>>> from iden.utils.imports import check_yaml
>>> check_yaml()

iden.utils.imports.cloudpickle_available

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

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

Parameters:

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

The function to execute.

required

Returns:

Type Description
Callable[..., Any]

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

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

iden.utils.imports.is_cloudpickle_available

is_cloudpickle_available() -> bool

Indicate if the cloudpickle package is installed or not.

Returns:

Type Description
bool

True if cloudpickle is available otherwise False.

Example
>>> from iden.utils.imports import is_cloudpickle_available
>>> is_cloudpickle_available()

iden.utils.imports.is_joblib_available

is_joblib_available() -> bool

Indicate if the joblib package is installed or not.

Returns:

Type Description
bool

True if joblib is available otherwise False.

Example
>>> from iden.utils.imports import is_joblib_available
>>> is_joblib_available()

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
>>> 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
>>> from iden.utils.imports import is_yaml_available
>>> is_yaml_available()

iden.utils.imports.joblib_available

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

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

Parameters:

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

The function to execute.

required

Returns:

Type Description
Callable[..., Any]

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

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

iden.utils.imports.raise_error_cloudpickle_missing

raise_error_cloudpickle_missing() -> NoReturn

Raise a RuntimeError to indicate the cloudpickle package is missing.

iden.utils.imports.raise_error_joblib_missing

raise_error_joblib_missing() -> NoReturn

Raise a RuntimeError to indicate the joblib package is missing.

iden.utils.imports.raise_error_safetensors_missing

raise_error_safetensors_missing() -> NoReturn

Raise a RuntimeError to indicate the safetensors package is missing.

iden.utils.imports.raise_error_yaml_missing

raise_error_yaml_missing() -> NoReturn

Raise a RuntimeError to indicate the yaml package is missing.

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]

The function to execute.

required

Returns:

Type Description
Callable[..., Any]

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

Example
>>> 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]

The function to execute.

required

Returns:

Type Description
Callable[..., Any]

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

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

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
>>> 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
>>> from iden.utils.time import timeblock
>>> with timeblock():
...     x = [1, 2, 3]
...
>>> with timeblock("Training: {time}"):
...     y = [1, 2, 3]
...