From 1f357d9a7a6d739d07c949c0610b4fe8b442ce21 Mon Sep 17 00:00:00 2001 From: Matthew Horton Date: Fri, 27 Sep 2024 17:04:48 -0700 Subject: [PATCH 1/6] Improve error handling in `SymmetryData` --- emmet-core/emmet/core/symmetry.py | 42 +++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/emmet-core/emmet/core/symmetry.py b/emmet-core/emmet/core/symmetry.py index 4c27155efb..5daf5a6d8c 100644 --- a/emmet-core/emmet/core/symmetry.py +++ b/emmet-core/emmet/core/symmetry.py @@ -3,7 +3,7 @@ from pydantic import BaseModel, Field from pymatgen.core import Structure from pymatgen.core.structure import Molecule -from pymatgen.symmetry.analyzer import PointGroupAnalyzer, SpacegroupAnalyzer, spglib +from pymatgen.symmetry.analyzer import PointGroupAnalyzer, SpacegroupAnalyzer, spglib, SymmetryUndeterminedError from emmet.core.settings import EmmetSettings from emmet.core.utils import ValueEnum @@ -139,29 +139,49 @@ class SymmetryData(BaseModel): symprec: Optional[float] = Field( None, title="Symmetry Finding Precision", - description="The precision given to spglib to determine the symmetry of this lattice.", + description="The precision provide to spglib to determine the symmetry of this structure.", ) - version: Optional[str] = Field(None, title="SPGLib version") + angle_tolerance: Optional[float] = Field( + None, + title="Angle Tolerance", + description="Angle tolerance provided to spglib to determine the symmetry of this structure." + ) + + version: Optional[str] = Field(None, title="spglib version") @classmethod def from_structure(cls, structure: Structure) -> "SymmetryData": - symprec = SETTINGS.SYMPREC - sg = SpacegroupAnalyzer(structure, symprec=symprec) - symmetry: Dict[str, Any] = {"symprec": symprec} - if not sg.get_symmetry_dataset(): - sg = SpacegroupAnalyzer(structure, 1e-3, 1) - symmetry["symprec"] = 1e-3 + + symmetry: Dict[str, Any] = { + "source": "spglib", + "symbol": None, + "number": None, + "point_group": None, + "crystal_system": None, + "hall": None, + "version": spglib.__version__, + "symprec": SETTINGS.SYMPREC, + "angle_tolerance": SETTINGS.ANGLE_TOL + } + + try: + sg = SpacegroupAnalyzer(structure, symprec=symmetry["symprec"], angle_tolerance=symmetry["angle_tolerance"]) + except SymmetryUndeterminedError: + try: + symmetry["symprec"] = 1e-3 + symmetry["angle_tolerance"] = 1 + sg = SpacegroupAnalyzer(structure, symmetry["symprec"], 1) + except SymmetryUndeterminedError: + return SymmetryData(**symmetry) symmetry.update( { - "source": "spglib", "symbol": sg.get_space_group_symbol(), "number": sg.get_space_group_number(), "point_group": sg.get_point_group_symbol(), "crystal_system": CrystalSystem(sg.get_crystal_system().title()), "hall": sg.get_hall(), - "version": spglib.__version__, } ) From 047463c9c8c4aa3974284f5c2e1be96d82306b6f Mon Sep 17 00:00:00 2001 From: Matthew Horton Date: Fri, 27 Sep 2024 17:07:04 -0700 Subject: [PATCH 2/6] Add minimum `pymatgen` version --- emmet-core/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/setup.py b/emmet-core/setup.py index b0d8a3fdd5..7b989a6a1a 100644 --- a/emmet-core/setup.py +++ b/emmet-core/setup.py @@ -29,7 +29,7 @@ include_package_data=True, install_requires=[ "numpy<2", - "pymatgen", + "pymatgen>=2024.6.10", "monty>=2024.2.2", "pydantic>=2.0", "pydantic-settings>=2.0", From e461bbecd5957d1c6204beb9cc9505685067e92a Mon Sep 17 00:00:00 2001 From: Matthew Horton Date: Fri, 27 Sep 2024 17:39:20 -0700 Subject: [PATCH 3/6] Set `angle_tolerance` explicitly --- emmet-core/emmet/core/symmetry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/symmetry.py b/emmet-core/emmet/core/symmetry.py index 5daf5a6d8c..ee841f1361 100644 --- a/emmet-core/emmet/core/symmetry.py +++ b/emmet-core/emmet/core/symmetry.py @@ -171,7 +171,7 @@ def from_structure(cls, structure: Structure) -> "SymmetryData": try: symmetry["symprec"] = 1e-3 symmetry["angle_tolerance"] = 1 - sg = SpacegroupAnalyzer(structure, symmetry["symprec"], 1) + sg = SpacegroupAnalyzer(structure, symprec=symmetry["symprec"], angle_tolerance=symmetry["angle_tolerance"]) except SymmetryUndeterminedError: return SymmetryData(**symmetry) From ee3201d1a416585ed56be7b0fd7f2973991b9ed6 Mon Sep 17 00:00:00 2001 From: Matthew Horton Date: Fri, 27 Sep 2024 17:47:23 -0700 Subject: [PATCH 4/6] Lint with `black` --- emmet-core/emmet/core/symmetry.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/emmet-core/emmet/core/symmetry.py b/emmet-core/emmet/core/symmetry.py index ee841f1361..e7e89cc4fe 100644 --- a/emmet-core/emmet/core/symmetry.py +++ b/emmet-core/emmet/core/symmetry.py @@ -3,7 +3,12 @@ from pydantic import BaseModel, Field from pymatgen.core import Structure from pymatgen.core.structure import Molecule -from pymatgen.symmetry.analyzer import PointGroupAnalyzer, SpacegroupAnalyzer, spglib, SymmetryUndeterminedError +from pymatgen.symmetry.analyzer import ( + PointGroupAnalyzer, + SpacegroupAnalyzer, + spglib, + SymmetryUndeterminedError, +) from emmet.core.settings import EmmetSettings from emmet.core.utils import ValueEnum @@ -145,7 +150,7 @@ class SymmetryData(BaseModel): angle_tolerance: Optional[float] = Field( None, title="Angle Tolerance", - description="Angle tolerance provided to spglib to determine the symmetry of this structure." + description="Angle tolerance provided to spglib to determine the symmetry of this structure.", ) version: Optional[str] = Field(None, title="spglib version") @@ -162,16 +167,24 @@ def from_structure(cls, structure: Structure) -> "SymmetryData": "hall": None, "version": spglib.__version__, "symprec": SETTINGS.SYMPREC, - "angle_tolerance": SETTINGS.ANGLE_TOL + "angle_tolerance": SETTINGS.ANGLE_TOL, } try: - sg = SpacegroupAnalyzer(structure, symprec=symmetry["symprec"], angle_tolerance=symmetry["angle_tolerance"]) + sg = SpacegroupAnalyzer( + structure, + symprec=symmetry["symprec"], + angle_tolerance=symmetry["angle_tolerance"], + ) except SymmetryUndeterminedError: try: symmetry["symprec"] = 1e-3 symmetry["angle_tolerance"] = 1 - sg = SpacegroupAnalyzer(structure, symprec=symmetry["symprec"], angle_tolerance=symmetry["angle_tolerance"]) + sg = SpacegroupAnalyzer( + structure, + symprec=symmetry["symprec"], + angle_tolerance=symmetry["angle_tolerance"], + ) except SymmetryUndeterminedError: return SymmetryData(**symmetry) From 4e7c17be7f30325ad795f596ff221d857c5935a0 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 27 Sep 2024 17:52:01 -0700 Subject: [PATCH 5/6] pre-commit --- emmet-core/emmet/core/symmetry.py | 1 - 1 file changed, 1 deletion(-) diff --git a/emmet-core/emmet/core/symmetry.py b/emmet-core/emmet/core/symmetry.py index e7e89cc4fe..718d708c82 100644 --- a/emmet-core/emmet/core/symmetry.py +++ b/emmet-core/emmet/core/symmetry.py @@ -157,7 +157,6 @@ class SymmetryData(BaseModel): @classmethod def from_structure(cls, structure: Structure) -> "SymmetryData": - symmetry: Dict[str, Any] = { "source": "spglib", "symbol": None, From 2daa179e2517431f70d053a419d4f85c5299d354 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 27 Sep 2024 17:52:47 -0700 Subject: [PATCH 6/6] typo provide -> provided --- emmet-core/emmet/core/symmetry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/symmetry.py b/emmet-core/emmet/core/symmetry.py index 718d708c82..b76f0c3bd1 100644 --- a/emmet-core/emmet/core/symmetry.py +++ b/emmet-core/emmet/core/symmetry.py @@ -144,7 +144,7 @@ class SymmetryData(BaseModel): symprec: Optional[float] = Field( None, title="Symmetry Finding Precision", - description="The precision provide to spglib to determine the symmetry of this structure.", + description="The precision provided to spglib to determine the symmetry of this structure.", ) angle_tolerance: Optional[float] = Field(