Skip to content

Commit 5fd64ab

Browse files
committed
skip prototype test if pybtex is not available
1 parent 3e55250 commit 5fd64ab

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/pymatgen/analysis/prototypes.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,27 @@ class AflowPrototypeMatcher:
4848
https://doi.org/10.1016/j.commatsci.2017.01.017
4949
"""
5050

51-
def __init__(self, initial_ltol=0.2, initial_stol=0.3, initial_angle_tol=5):
51+
def __init__(
52+
self,
53+
initial_ltol: float = 0.2,
54+
initial_stol: float = 0.3,
55+
initial_angle_tol: float = 5,
56+
) -> None:
5257
"""
5358
Tolerances as defined in StructureMatcher. Tolerances will be
5459
gradually decreased until only a single match is found (if possible).
5560
5661
Args:
57-
initial_ltol: fractional length tolerance
58-
initial_stol: site tolerance
59-
initial_angle_tol: angle tolerance
62+
initial_ltol (float): fractional length tolerance.
63+
initial_stol (float): site tolerance.
64+
initial_angle_tol (float): angle tolerance.
6065
"""
6166
self.initial_ltol = initial_ltol
6267
self.initial_stol = initial_stol
6368
self.initial_angle_tol = initial_angle_tol
6469

6570
# Preprocess AFLOW prototypes
66-
self._aflow_prototype_library = []
71+
self._aflow_prototype_library: list[tuple[Structure, dict]] = []
6772
for dct in AFLOW_PROTOTYPE_LIBRARY:
6873
structure: Structure = dct["snl"].structure
6974
reduced_structure = self._preprocess_structure(structure)
@@ -73,7 +78,11 @@ def __init__(self, initial_ltol=0.2, initial_stol=0.3, initial_angle_tol=5):
7378
def _preprocess_structure(structure: Structure) -> Structure:
7479
return structure.get_reduced_structure(reduction_algo="niggli").get_primitive_structure()
7580

76-
def _match_prototype(self, structure_matcher: StructureMatcher, reduced_structure: Structure):
81+
def _match_prototype(
82+
self,
83+
structure_matcher: StructureMatcher,
84+
reduced_structure: Structure,
85+
) -> list[dict]:
7786
tags = []
7887
for aflow_reduced_structure, dct in self._aflow_prototype_library:
7988
# Since both structures are already reduced, we can skip the structure reduction step
@@ -84,7 +93,7 @@ def _match_prototype(self, structure_matcher: StructureMatcher, reduced_structur
8493
tags.append(dct)
8594
return tags
8695

87-
def _match_single_prototype(self, structure: Structure):
96+
def _match_single_prototype(self, structure: Structure) -> list[dict]:
8897
sm = StructureMatcher(
8998
ltol=self.initial_ltol,
9099
stol=self.initial_stol,
@@ -102,23 +111,23 @@ def _match_single_prototype(self, structure: Structure):
102111
break
103112
return tags
104113

105-
def get_prototypes(self, structure: Structure) -> list | None:
114+
def get_prototypes(self, structure: Structure) -> list[dict] | None:
106115
"""Get prototype(s) structures for a given input structure. If you use this method in
107116
your work, please cite the appropriate AFLOW publication:
108117
109-
Mehl, M. J., Hicks, D., Toher, C., Levy, O., Hanson, R. M., Hart, G., & Curtarolo,
110-
S. (2017). The AFLOW library of crystallographic prototypes: part 1. Computational
111-
Materials Science, 136, S1-S828. https://doi.org/10.1016/j.commatsci.2017.01.017
118+
Mehl, M. J., Hicks, D., Toher, C., Levy, O., Hanson, R. M., Hart, G., & Curtarolo,
119+
S. (2017). The AFLOW library of crystallographic prototypes: part 1. Computational
120+
Materials Science, 136, S1-S828. https://doi.org/10.1016/j.commatsci.2017.01.017
112121
113122
Args:
114-
structure: structure to match
123+
structure (Structure): structure to match
115124
116125
Returns:
117-
list | None: A list of dicts with keys 'snl' for the matched prototype and
118-
'tags', a dict of tags ('mineral', 'strukturbericht' and 'aflow') of that
126+
list[dict] | None: A list of dicts with keys "snl" for the matched prototype and
127+
"tags", a dict of tags ("mineral", "strukturbericht" and "aflow") of that
119128
prototype. This should be a list containing just a single entry, but it is
120129
possible a material can match multiple prototypes.
121130
"""
122-
tags = self._match_single_prototype(structure)
131+
tags: list[dict] = self._match_single_prototype(structure)
123132

124133
return tags or None

src/pymatgen/util/provenance.py

+2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ def __init__(
225225
# Check that references are valid BibTeX
226226
if not isinstance(references, str):
227227
raise TypeError("Invalid format for SNL reference! Should be empty string or BibTeX string.")
228+
228229
if references and not is_valid_bibtex(references):
229230
raise ValueError("Invalid format for SNL reference! Should be BibTeX string.")
231+
230232
if len(references) > MAX_BIBTEX_CHARS:
231233
raise ValueError(
232234
f"The BibTeX string must be fewer than {MAX_BIBTEX_CHARS} chars, you have {len(references)}"

tests/analysis/test_prototypes.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from __future__ import annotations
22

3-
from pymatgen.analysis.prototypes import AflowPrototypeMatcher
3+
import pytest
4+
5+
try:
6+
from pymatgen.analysis.prototypes import AflowPrototypeMatcher
7+
except RuntimeError:
8+
pytest.skip("pybtex is not available", allow_module_level=True)
9+
410
from pymatgen.util.testing import PymatgenTest
511

612

0 commit comments

Comments
 (0)