Skip to content

liblaf.melon.utils

Small utility helpers used by Melon internals and workflows.

Functions:

filter_kwargs

filter_kwargs(
    func: Callable[..., Any], kwargs: Mapping[str, Any]
) -> Mapping[str, Any]

Filter keyword arguments to those accepted by func.

Functions with **kwargs receive the original mapping unchanged.

Parameters:

  • func (Callable[..., Any]) –

    Callable whose signature should be respected.

  • kwargs (Mapping[str, Any]) –

    Candidate keyword arguments.

Returns:

Source code in src/liblaf/melon/utils/_toolz.py
def filter_kwargs(
    func: Callable[..., Any], kwargs: Mapping[str, Any]
) -> Mapping[str, Any]:
    """Filter keyword arguments to those accepted by `func`.

    Functions with `**kwargs` receive the original mapping unchanged.

    Args:
        func: Callable whose signature should be respected.
        kwargs: Candidate keyword arguments.

    Returns:
        Supported keyword arguments.
    """
    from inspect import Parameter

    signature: inspect.Signature = inspect.signature(func)
    filtered: dict[str, Any] = {}
    for p in signature.parameters.values():
        match p.kind:
            case Parameter.POSITIONAL_OR_KEYWORD | Parameter.KEYWORD_ONLY:
                if p.name in kwargs:
                    filtered[p.name] = kwargs[p.name]
            case Parameter.VAR_KEYWORD:
                return kwargs
    return filtered

pick

pick[K, V](
    allowlist: Container[K], dictionary: Mapping[K, V]
) -> dict[K, V]

Return dictionary items whose keys are in allowlist.

Parameters:

  • allowlist (Container[K]) –

    Keys to keep.

  • dictionary (Mapping[K, V]) –

    Source mapping.

Returns:

  • dict[K, V]

    A plain dictionary containing only allowed keys.

Source code in src/liblaf/melon/utils/_toolz.py
def pick[K, V](allowlist: Container[K], dictionary: Mapping[K, V]) -> dict[K, V]:
    """Return dictionary items whose keys are in `allowlist`.

    Args:
        allowlist: Keys to keep.
        dictionary: Source mapping.

    Returns:
        A plain dictionary containing only allowed keys.
    """
    return tlz.keyfilter(lambda k: k in allowlist, dictionary)

temporary_array

temporary_array(
    attributes: DataSetAttributes,
    data: Float[ArrayLike, ...] | None = None,
    name: str = "",
    length: int = 8,
) -> Generator[str]

Attach a temporary PyVista data array and remove it on exit.

Parameters:

  • attributes (DataSetAttributes) –

    Point, cell, or field data container to mutate.

  • data (Float[ArrayLike, ...] | None, default: None ) –

    Optional array value to store under the generated name.

  • name (str, default: '' ) –

    Prefix for the generated array name.

  • length (int, default: 8 ) –

    Number of random suffix characters to append.

Yields:

Source code in src/liblaf/melon/utils/_attributes.py
@contextlib.contextmanager
def temporary_array(
    attributes: pv.DataSetAttributes,
    data: Float[ArrayLike, "..."] | None = None,
    name: str = "",
    length: int = 8,
) -> Generator[str]:
    """Attach a temporary PyVista data array and remove it on exit.

    Args:
        attributes: Point, cell, or field data container to mutate.
        data: Optional array value to store under the generated name.
        name: Prefix for the generated array name.
        length: Number of random suffix characters to append.

    Yields:
        The generated array name.
    """
    suffix: str = "".join(random.choices(_ALPHABET, k=length))  # noqa: S311
    name: str = f"{name}{suffix}"
    if data is not None:
        attributes[name] = data
    try:
        yield name
    finally:
        attributes.pop(name, None)

warp_stream_from_torch

warp_stream_from_torch() -> Stream | None
Source code in src/liblaf/melon/utils/_warp.py
def warp_stream_from_torch() -> wp.Stream | None:
    if not torch.cuda.is_available():
        return None
    torch_stream: torch.cuda.Stream = torch.cuda.current_stream()
    warp_stream: wp.Stream = wp.stream_from_torch(torch_stream)
    return warp_stream