Skip to content

arkas.utils

arkas.utils

Contain utility functions.

arkas.utils.factory

Contain a function to instantiate an object from its configuration.

arkas.utils.factory.setup_object

setup_object(obj_or_config: T | dict) -> T

Set up an object from its configuration.

Parameters:

Name Type Description Default
obj_or_config T | dict

The object or its configuration.

required

Returns:

Type Description
T

The instantiated object.

Example usage:

>>> from arkas.utils.factory import setup_object
>>> obj = setup_object({"_target_": "collections.deque", "iterable": [1, 2, 1, 3]})
>>> obj
deque([1, 2, 1, 3])
>>> setup_object(obj)  # Do nothing because the object is already instantiated
deque([1, 2, 1, 3])

arkas.utils.figure

Contain utility functions to manage matplotlib figures.

arkas.utils.figure.figure2html

figure2html(
    fig: Figure | None,
    reactive: bool = True,
    close_fig: bool = False,
) -> str

Convert a matplotlib figure to a string that can be used in a HTML file.

Parameters:

Name Type Description Default
fig Figure | None

The figure to convert.

required
reactive bool

If True, the generated is configured to be reactive to the screen size.

True
close_fig bool

If True, the figure is closed after it is converted to HTML format.

False

Returns:

Type Description
str

The converted figure to a string.

Example usage:

>>> from matplotlib import pyplot as plt
>>> from arkas.utils.figure import figure2html
>>> fig, ax = plt.subplots()
>>> string = figure2html(fig)

arkas.utils.imports

Implement some utility functions to manage optional dependencies.

arkas.utils.imports.check_colorlog

check_colorlog() -> None

Check if the colorlog package is installed.

Raises:

Type Description
RuntimeError

if the colorlog package is not installed.

Example usage:

>>> from arkas.utils.imports import check_colorlog
>>> check_colorlog()

arkas.utils.imports.check_hya

check_hya() -> None

Check if the hya package is installed.

Raises:

Type Description
RuntimeError

if the hya package is not installed.

Example usage:

>>> from arkas.utils.imports import check_hya
>>> check_hya()

arkas.utils.imports.check_hydra

check_hydra() -> None

Check if the hydra package is installed.

Raises:

Type Description
RuntimeError

if the hydra package is not installed.

Example usage:

>>> from arkas.utils.imports import check_hydra
>>> check_hydra()

arkas.utils.imports.check_markdown

check_markdown() -> None

Check if the markdown package is installed.

Raises:

Type Description
RuntimeError

if the markdown package is not installed.

Example usage:

>>> from arkas.utils.imports import check_markdown
>>> check_markdown()

arkas.utils.imports.check_matplotlib

check_matplotlib() -> None

Check if the matplotlib package is installed.

Raises:

Type Description
RuntimeError

if the matplotlib package is not installed.

Example usage:

>>> from arkas.utils.imports import check_matplotlib
>>> check_matplotlib()

arkas.utils.imports.check_omegaconf

check_omegaconf() -> None

Check if the omegaconf package is installed.

Raises:

Type Description
RuntimeError

if the omegaconf package is not installed.

Example usage:

>>> from arkas.utils.imports import check_omegaconf
>>> check_omegaconf()

arkas.utils.imports.check_plotly

check_plotly() -> None

Check if the plotly package is installed.

Raises:

Type Description
RuntimeError

if the plotly package is not installed.

Example usage:

>>> from arkas.utils.imports import check_plotly
>>> check_plotly()

arkas.utils.imports.check_scipy

check_scipy() -> None

Check if the scipy package is installed.

Raises:

Type Description
RuntimeError

if the scipy package is not installed.

Example usage:

>>> from arkas.utils.imports import check_scipy
>>> check_scipy()

arkas.utils.imports.colorlog_available

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

Implement a decorator to execute a function only if colorlog 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 colorlog package is installed, otherwise None.

Example usage:

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

arkas.utils.imports.hya_available

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

Implement a decorator to execute a function only if hya 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 hya package is installed, otherwise None.

Example usage:

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

arkas.utils.imports.hydra_available

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

Implement a decorator to execute a function only if hydra 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 hydra package is installed, otherwise None.

Example usage:

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

arkas.utils.imports.is_colorlog_available

is_colorlog_available() -> bool

Indicate if the colorlog package is installed or not.

Returns:

Type Description
bool

True if colorlog is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_colorlog_available
>>> is_colorlog_available()

arkas.utils.imports.is_hya_available

is_hya_available() -> bool

Indicate if the hya package is installed or not.

Returns:

Type Description
bool

True if hya is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_hya_available
>>> is_hya_available()

arkas.utils.imports.is_hydra_available

is_hydra_available() -> bool

Indicate if the hydra package is installed or not.

Returns:

Type Description
bool

True if hydra is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_hydra_available
>>> is_hydra_available()

arkas.utils.imports.is_markdown_available

is_markdown_available() -> bool

Indicate if the markdown package is installed or not.

Returns:

Type Description
bool

True if markdown is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_markdown_available
>>> is_markdown_available()

arkas.utils.imports.is_matplotlib_available

is_matplotlib_available() -> bool

Indicate if the matplotlib package is installed or not.

Returns:

Type Description
bool

True if matplotlib is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_matplotlib_available
>>> is_matplotlib_available()

arkas.utils.imports.is_omegaconf_available

is_omegaconf_available() -> bool

Indicate if the omegaconf package is installed or not.

Returns:

Type Description
bool

True if omegaconf is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_omegaconf_available
>>> is_omegaconf_available()

arkas.utils.imports.is_plotly_available

is_plotly_available() -> bool

Indicate if the plotly package is installed or not.

Returns:

Type Description
bool

True if plotly is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_plotly_available
>>> is_plotly_available()

arkas.utils.imports.is_scipy_available

is_scipy_available() -> bool

Indicate if the scipy package is installed or not.

Returns:

Type Description
bool

True if scipy is available otherwise False.

Example usage:

>>> from arkas.utils.imports import is_scipy_available
>>> is_scipy_available()

arkas.utils.imports.markdown_available

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

Implement a decorator to execute a function only if markdown 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 markdown package is installed, otherwise None.

Example usage:

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

arkas.utils.imports.matplotlib_available

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

Implement a decorator to execute a function only if matplotlib 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 matplotlib package is installed, otherwise None.

Example usage:

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

arkas.utils.imports.omegaconf_available

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

Implement a decorator to execute a function only if omegaconf 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 omegaconf package is installed, otherwise None.

Example usage:

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

arkas.utils.imports.plotly_available

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

Implement a decorator to execute a function only if plotly 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 plotly package is installed, otherwise None.

Example usage:

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

arkas.utils.imports.scipy_available

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

Implement a decorator to execute a function only if scipy 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 scipy package is installed, otherwise None.

Example usage:

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

arkas.utils.text

Contain text utility functions.

arkas.utils.text.markdown_to_html

markdown_to_html(
    text: str, ignore_error: bool = False
) -> str

Convert a markdown text to HTML text.

Parameters:

Name Type Description Default
text str

The markdown text to convert.

required
ignore_error bool

If False, an error is raised if markdown is not installed, otherwise the input text is returned.

False

Returns:

Type Description
str

The converted text if markdown is installed, otherwise the input text.

Example usage:

>>> from arkas.utils.text import markdown_to_html
>>> out = markdown_to_html("- a\n- b\n- c")
>>> print(out)
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>