Skip to content

Utils

coola.utils

Contain the utility functions.

coola.utils.check_numpy

check_numpy() -> None

Check if the numpy package is installed.

Raises:

Type Description
RuntimeError

if the numpy package is not installed.

Example usage:

>>> from coola.utils.imports import check_numpy
>>> check_numpy()

coola.utils.check_pandas

check_pandas() -> None

Check if the pandas package is installed.

Raises:

Type Description
RuntimeError

if the pandas package is not installed.

Example usage:

>>> from coola.utils.imports import check_pandas
>>> check_pandas()

coola.utils.check_polars

check_polars() -> None

Check if the polars package is installed.

Raises:

Type Description
RuntimeError

if the polars package is not installed.

Example usage:

>>> from coola.utils.imports import check_polars
>>> check_polars()

coola.utils.check_torch

check_torch() -> None

Check if the torch package is installed.

Raises:

Type Description
RuntimeError

if the torch package is not installed.

Example usage:

>>> from coola.utils.imports import check_torch
>>> check_torch()

coola.utils.check_xarray

check_xarray() -> None

Check if the xarray package is installed.

Raises:

Type Description
RuntimeError

if the xarray package is not installed.

Example usage:

>>> from coola.utils.imports import check_xarray
>>> check_xarray()

coola.utils.is_jax_available

is_jax_available() -> bool

Indicate if the jax package is installed or not.

Returns:

Type Description
bool

True if jax is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_jax_available
>>> is_jax_available()

coola.utils.is_numpy_available

is_numpy_available() -> bool

Indicate if the numpy package is installed or not.

Returns:

Type Description
bool

True if numpy is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_numpy_available
>>> is_numpy_available()

coola.utils.is_pandas_available

is_pandas_available() -> bool

Indicate if the pandas package is installed or not.

Returns:

Type Description
bool

True if pandas is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_pandas_available
>>> is_pandas_available()

coola.utils.is_polars_available

is_polars_available() -> bool

Indicate if the polars package is installed or not.

Returns:

Type Description
bool

True if polars is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_polars_available
>>> is_polars_available()

coola.utils.is_torch_available

is_torch_available() -> bool

Indicate if the torch package is installed or not.

Returns:

Type Description
bool

True if torch is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_torch_available
>>> is_torch_available()

coola.utils.is_xarray_available

is_xarray_available() -> bool

Indicate if the xarray package is installed or not.

Returns:

Type Description
bool

True if xarray is available otherwise False.

Example usage:

>>> from coola.utils.imports import is_xarray_available
>>> is_xarray_available()

coola.utils.repr_indent

repr_indent(original: Any, num_spaces: int = 2) -> str

Add indentations if the original string is a multi-lines string.

Parameters:

Name Type Description Default
original Any

Specifies the original string. If the inputis not a string, it will be converted to a string with the function repr.

required
num_spaces int

Specifies the number of spaces used for the indentation.

2

Returns:

Type Description
str

The indented string.

Raises:

Type Description
RuntimeError

if num_spaces is not a positive integer.

Example usage:

>>> from coola.utils.format import repr_indent
>>> print(repr_indent("string1\nstring2\n  string3", 4))
string1
string2
  string3

coola.utils.repr_mapping

repr_mapping(
    mapping: Mapping,
    sorted_keys: bool = False,
    num_spaces: int = 2,
) -> str

Compute a string representation of a mapping.

This function was designed for flat dictionary. If you have a nested dictionary, you may consider other functions. Note that this function works for nested dict but the output may not be nice.

Parameters:

Name Type Description Default
mapping Mapping

Specifies the mapping.

required
sorted_keys bool

Specifies if the key of the dict are sorted or not.

False
num_spaces int

Specifies the number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the mapping.

Example usage:

>>> from coola.utils.format import repr_mapping
>>> print(repr_mapping({"key1": "abc", "key2": "something\nelse"}))
(key1): abc
(key2): something
  else

coola.utils.repr_sequence

repr_sequence(
    sequence: Sequence, num_spaces: int = 2
) -> str

Compute a string representation of a sequence.

Parameters:

Name Type Description Default
sequence Sequence

Specifies the sequence.

required
num_spaces int

Specifies the number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the sequence.

Example usage:

>>> from coola.utils.format import repr_indent
>>> print(repr_sequence(["abc", "something\nelse"]))
(0): abc
(1): something
  else

coola.utils.str_indent

str_indent(original: Any, num_spaces: int = 2) -> str

Add indentations if the original string is a multi-lines string.

Parameters:

Name Type Description Default
original Any

Specifies the original string. If the inputis not a string, it will be converted to a string with the function str.

required
num_spaces int

Specifies the number of spaces used for the indentation.

2

Returns:

Type Description
str

The indented string.

Raises:

Type Description
RuntimeError

if num_spaces is not a positive integer.

Example usage:

>>> from coola.utils.format import str_indent
>>> print(str_indent("string1\nstring2\n  string3", 4))
string1
string2
  string3

coola.utils.str_mapping

str_mapping(
    mapping: Mapping,
    sorted_keys: bool = False,
    num_spaces: int = 2,
) -> str

Compute a string representation of a mapping.

This function was designed for flat dictionary. If you have a nested dictionary, you may consider other functions. Note that this function works for nested dict but the output may not be nice.

Parameters:

Name Type Description Default
mapping Mapping

Specifies the mapping.

required
sorted_keys bool

Specifies if the key of the dict are sorted or not.

False
num_spaces int

Specifies the number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the mapping.

Example usage:

>>> from coola.utils.format import str_mapping
>>> print(str_mapping({"key1": "abc", "key2": "something\nelse"}))
(key1): abc
(key2): something
  else

coola.utils.str_sequence

str_sequence(
    sequence: Sequence, num_spaces: int = 2
) -> str

Compute a string representation of a sequence.

Parameters:

Name Type Description Default
sequence Sequence

Specifies the sequence.

required
num_spaces int

Specifies the number of spaces used for the indentation.

2

Returns:

Type Description
str

The string representation of the sequence.

Example usage:

>>> from coola.utils.format import str_sequence
>>> print(str_sequence(["abc", "something\nelse"]))
(0): abc
(1): something
  else