From 24ee42591367bf02955e24d49920ee1cac6d6edf Mon Sep 17 00:00:00 2001 From: Ali Ramlaoui Date: Thu, 24 Apr 2025 00:03:58 +0200 Subject: [PATCH 1/2] chore: Remove warnings from unsuccessful imports in main --- src/material_hasher/hasher/__init__.py | 15 ++++++--------- src/material_hasher/hasher/slices.py | 13 +++++++++++-- src/material_hasher/similarity/__init__.py | 22 +++++++++------------- src/material_hasher/similarity/eqv2.py | 16 +++++++++++++++- 4 files changed, 41 insertions(+), 25 deletions(-) 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..b5478d7 100644 --- a/src/material_hasher/hasher/slices.py +++ b/src/material_hasher/hasher/slices.py @@ -5,7 +5,17 @@ from pymatgen.core.structure import Structure -from slices.core import SLICES + +try: + from slices.core import SLICES +except ImportError: + import warnings + + 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, + ) + from material_hasher.hasher.base import HasherBase @@ -32,4 +42,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..e4eae5d 100644 --- a/src/material_hasher/similarity/eqv2.py +++ b/src/material_hasher/similarity/eqv2.py @@ -9,7 +9,21 @@ 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: + import warnings + warnings.warn( + "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", + ImportWarning, + ) +# fmt: on + from huggingface_hub import hf_hub_download from pymatgen.core import Structure from pymatgen.io.ase import AseAtomsAdaptor From e3c231dd347382d2cf5a2d6da269be2c11651a17 Mon Sep 17 00:00:00 2001 From: Ali Ramlaoui Date: Tue, 6 May 2025 04:18:10 +0200 Subject: [PATCH 2/2] fix: raise import errors on modules not installed --- src/material_hasher/hasher/slices.py | 7 ++----- src/material_hasher/similarity/eqv2.py | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/material_hasher/hasher/slices.py b/src/material_hasher/hasher/slices.py index b5478d7..79a19ad 100644 --- a/src/material_hasher/hasher/slices.py +++ b/src/material_hasher/hasher/slices.py @@ -9,11 +9,8 @@ try: from slices.core import SLICES except ImportError: - import warnings - - 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, + raise ImportError( + "Failed to import SLICES. If you would like to use this module, please consider running uv pip install -r requirements_slices.txt" ) diff --git a/src/material_hasher/similarity/eqv2.py b/src/material_hasher/similarity/eqv2.py index e4eae5d..c6a378a 100644 --- a/src/material_hasher/similarity/eqv2.py +++ b/src/material_hasher/similarity/eqv2.py @@ -14,13 +14,10 @@ try: from fairchem.core import OCPCalculator except ImportError: - import warnings - warnings.warn( + raise ImportError( "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" + "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", - ImportWarning, ) # fmt: on