diff --git a/src/material_hasher/hasher/__init__.py b/src/material_hasher/hasher/__init__.py index f360aad..760edca 100644 --- a/src/material_hasher/hasher/__init__.py +++ b/src/material_hasher/hasher/__init__.py @@ -1,11 +1,7 @@ # Copyright 2025 Entalpic -import warnings - from material_hasher.hasher.bawl import BAWLHasher, ShortBAWLHasher from material_hasher.hasher.pdd import PointwiseDistanceDistributionHasher -warnings.filterwarnings("always") - __all__ = ["BAWLHasher"] HASHERS = { @@ -16,11 +12,12 @@ try: - from material_hasher.hasher.slices import SLICESHasher + import warnings + + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=ImportWarning) + from material_hasher.hasher.slices import SLICESHasher HASHERS.update({"SLICES": SLICESHasher}) except ImportError: - warnings.warn( - "Failed to import SLICES. If you would like to use this module, please consider running uv pip install -r requirements_slices.txt", - ImportWarning, - ) + pass diff --git a/src/material_hasher/hasher/slices.py b/src/material_hasher/hasher/slices.py index 69ddc3c..79a19ad 100644 --- a/src/material_hasher/hasher/slices.py +++ b/src/material_hasher/hasher/slices.py @@ -5,7 +5,14 @@ from pymatgen.core.structure import Structure -from slices.core import SLICES + +try: + from slices.core import SLICES +except ImportError: + raise ImportError( + "Failed to import SLICES. If you would like to use this module, please consider running uv pip install -r requirements_slices.txt" + ) + from material_hasher.hasher.base import HasherBase @@ -32,4 +39,3 @@ def get_material_hash(self, structure: Structure) -> str: The SLICES string representation of the structure. """ return self.backend.structure2SLICES(structure) - diff --git a/src/material_hasher/similarity/__init__.py b/src/material_hasher/similarity/__init__.py index f06c452..7111d00 100644 --- a/src/material_hasher/similarity/__init__.py +++ b/src/material_hasher/similarity/__init__.py @@ -1,6 +1,4 @@ # Copyright 2025 Entalpic -import logging - from .structure_matchers import PymatgenStructureSimilarity __all__ = ["PymatgenStructureSimilarity"] @@ -10,15 +8,13 @@ } try: - from .eqv2 import EquiformerV2Similarity + import warnings + + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=ImportWarning) + from .eqv2 import EquiformerV2Similarity - __all__.append("EquiformerV2Similarity") - SIMILARITY_MATCHERS["eqv2"] = EquiformerV2Similarity # type: ignore -except ImportError as e: - logging.warning( - "EquiformerV2Similarity is not available. You need to install fairchem-core and its dependencies. " - "This issue is known to affect MacOS systems. " - "If you're not using MacOS, please ensure the optional dependencies required for this feature are installed. uv sync --extra fairchem and uv sync --extra geometric" - "For more information, refer to issue #4: https://github.com/Entalpic/material-hasher/issues/4", - f"Error: {e}", - ) + __all__.append("EquiformerV2Similarity") + SIMILARITY_MATCHERS["eqv2"] = EquiformerV2Similarity # type: ignore +except ImportError: + pass diff --git a/src/material_hasher/similarity/eqv2.py b/src/material_hasher/similarity/eqv2.py index e3b31be..c6a378a 100644 --- a/src/material_hasher/similarity/eqv2.py +++ b/src/material_hasher/similarity/eqv2.py @@ -9,7 +9,18 @@ import yaml from ase.filters import FrechetCellFilter from ase.optimize import FIRE -from fairchem.core import OCPCalculator + +# fmt: off +try: + from fairchem.core import OCPCalculator +except ImportError: + raise ImportError( + "EquiformerV2Similarity is not available. You need to install fairchem-core and its dependencies. " + "Please ensure the optional dependencies required for this feature are installed. uv sync --extra fairchem and uv sync --extra geometric" + "For more information, refer to issue #4: https://github.com/Entalpic/material-hasher/issues/4", + ) +# fmt: on + from huggingface_hub import hf_hub_download from pymatgen.core import Structure from pymatgen.io.ase import AseAtomsAdaptor