# MIT License
#
# Copyright (c) 2024-2026 Inverse Materials Design Group
#
# Author: Ihor Radchenko <yantar92@posteo.net>
#
# This file is a part of MatRiFFlow package
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Common routines used across the sub-libraries."""
import logging
from collections.abc import Callable, Sequence
from contextlib import AbstractContextManager
from pathlib import Path
from typing import Any
logger = logging.getLogger(__name__)
[docs]
def short_path(path: str | Path) -> str:
"""Return the final path component for compact log messages.
gorun working directories live under long cluster mount points
(e.g. ``/net/storage/.../generations.Li/.../relax.2``). Repeating
the full path on every line drowns out the useful information, so
per-step messages only show the final component. The working
directory is announced once at the start of a run.
"""
name = Path(path).name
return name if name else str(path)
[docs]
def open_progress_bar(
total: int | None,
**options: Any) -> AbstractContextManager[Any]:
"""Open an alive-progress context."""
import alive_progress
# FIXME: remove the Any handle and suppression when upstream typing is released.
# https://github.com/rsalmei/alive-progress/issues/306
return alive_progress.alive_bar(total, **options) # pyright: ignore[reportUnknownVariableType]
[docs]
def suppress_spglib_warnings():
"""Silence spglib's raw C-level warnings.
spglib prints internal retry diagnostics directly to stderr when
symmetry detection needs several attempts (e.g. ``"spglib:
ssm_get_exact_positions failed."``, ``"spglib: No point group was
found"``). They are harmless and duplicate the structured warnings
surfaced by matrifflow's own diagnostics, but they interleave with
rich tables and corrupt the display.
spglib reads ``SPGLIB_WARNING`` on every call (see its
``warning_enabled``), so setting it at runtime is enough. We use
``setdefault`` so a user who has already chosen a value keeps
control; only the absent-key case is forced to ``OFF``.
"""
import os
os.environ.setdefault("SPGLIB_WARNING", "OFF")
[docs]
def groupby_cmp(
lst: Sequence[Any],
cmp_eq: Callable[[Any, Any], bool],
title_function: Callable[[Any], str] | None = None) -> list[list[Any]]:
"""Group elements of a list by pairwise equality.
Each item is compared only against the first member of every existing
group, so the number of ``cmp_eq`` calls is O(n * g), where ``g`` is
the number of groups produced, instead of O(n^2) over group members.
This is exact when ``cmp_eq`` is an equivalence relation (reflexive,
symmetric, and transitive). A non-transitive ``cmp_eq`` still works,
but it is only compared against the first member of each group, so
items that would match a later member but not the representative may
be split into separate groups.
Args:
lst: List of elements to group.
cmp_eq: Function that takes two elements and returns True if they
should belong to the same group. Ideally an equivalence
relation; see above for the behavior when it is not
transitive.
title_function: Optional function that takes an element and returns
a string used in log messages. When None, "??" is logged.
Returns:
list[list]: List of grouped lists. Each sublist contains elements
that are pairwise equivalent according to cmp_eq.
"""
groups: list[list[Any]] = []
for item in lst:
title = title_function(item) if title_function is not None else "??"
for group in groups:
if cmp_eq(item, group[0]):
logger.debug('Appending %s to existing group', title)
group.append(item)
break
else:
logger.debug('Creating a new group for %s', title)
groups.append([item])
return groups
[docs]
def deprecated_aliases(
module_globals: dict[str, Any],
**aliases: str) -> Callable[[str], Any]:
"""Return a module-level ``__getattr__`` resolving deprecated names.
``aliases`` maps each legacy name to the current name present in
``module_globals``. Accessing a legacy name emits a
:class:`DeprecationWarning` and returns the current object, so old
``IMD*`` / ``IMDG*`` class names keep working after the MatRiFFlow
rename.
Usage (at the bottom of a module)::
__getattr__ = deprecated_aliases(globals(), IMDGVaspDir="MRFVaspDir")
"""
import warnings
def __getattr__(name: str) -> Any:
if name in aliases:
warnings.warn(
f"{name} is deprecated; use {aliases[name]} instead.",
DeprecationWarning,
stacklevel=2,
)
return module_globals[aliases[name]]
raise AttributeError(
f"module {module_globals['__name__']!r} has no attribute {name!r}"
)
return __getattr__