Skip to content

utils

Helper functions for plotting.

Colormap

Very simple colormap class.

Source code in src/pathpyG/visualisations/utils.py
class Colormap:
    """Very simple colormap class."""

    def __call__(
        self,
        values: list,
        alpha: Optional[float] = None,
        bytes: bool = False,
    ) -> list:
        """Return color value."""
        vmin, vmax = min(values), max(values)
        if vmin == vmax:
            vmin -= 1
            vmax += 1
        return [
            self.color_tuple(v)
            for v in ((x - vmin) / (vmax - vmin) * 100 for x in values)
        ]

    @staticmethod
    def color_tuple(n: float) -> tuple:
        """Return color ramp from green to red."""
        return (int((255 * n) * 0.01), int((255 * (100 - n)) * 0.01), 0, 255)

__call__

Return color value.

Source code in src/pathpyG/visualisations/utils.py
def __call__(
    self,
    values: list,
    alpha: Optional[float] = None,
    bytes: bool = False,
) -> list:
    """Return color value."""
    vmin, vmax = min(values), max(values)
    if vmin == vmax:
        vmin -= 1
        vmax += 1
    return [
        self.color_tuple(v)
        for v in ((x - vmin) / (vmax - vmin) * 100 for x in values)
    ]

color_tuple staticmethod

Return color ramp from green to red.

Source code in src/pathpyG/visualisations/utils.py
@staticmethod
def color_tuple(n: float) -> tuple:
    """Return color ramp from green to red."""
    return (int((255 * n) * 0.01), int((255 * (100 - n)) * 0.01), 0, 255)

hex_to_rgb

Convert hex string to rgb color tuple.

Source code in src/pathpyG/visualisations/utils.py
def hex_to_rgb(value: str) -> tuple:
    """Convert hex string to rgb color tuple."""
    value = value.lstrip("#")
    _l = len(value)
    return tuple(int(value[i : i + _l // 3], 16) for i in range(0, _l, _l // 3))

rgb_to_hex

Convert rgb color tuple to hex string.

Source code in src/pathpyG/visualisations/utils.py
def rgb_to_hex(rgb: tuple) -> str:
    """Convert rgb color tuple to hex string."""
    return "#%02x%02x%02x" % rgb