From 70414bba092413f44b3ce3479341f99d12ac1879 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:37:40 -0700 Subject: [PATCH] move robocrys class + obj init's to convenience util func --- emmet-core/emmet/core/robocrys.py | 30 ++++++++++++++++-------------- emmet-core/emmet/core/utils.py | 20 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/emmet-core/emmet/core/robocrys.py b/emmet-core/emmet/core/robocrys.py index 05da9f35ee..81085f29b2 100644 --- a/emmet-core/emmet/core/robocrys.py +++ b/emmet-core/emmet/core/robocrys.py @@ -1,10 +1,12 @@ -from typing import Union, Optional +from typing import Optional, Union from pydantic import BaseModel, Field from pymatgen.core.structure import Structure +from robocrys.condense.mineral import MineralMatcher from emmet.core.material_property import PropertyDoc from emmet.core.mpid import MPID +from emmet.core.utils import generate_robocrys_condensed_struct_and_description class MineralData(BaseModel): @@ -73,25 +75,25 @@ class RobocrystallogapherDoc(PropertyDoc): ) @classmethod - def from_structure(cls, structure: Structure, material_id: MPID, **kwargs): # type: ignore[override] - try: - from robocrys import StructureCondenser, StructureDescriber - from robocrys import __version__ as __robocrys_version__ - except ImportError: - raise ImportError( - "robocrys needs to be installed to generate RobocrystallographerDoc" + def from_structure( + cls, + structure: Structure, + material_id: MPID, + robocrys_version: str, + mineral_matcher: MineralMatcher | None = None, + **kwargs + ): + condensed_structure, description = ( + generate_robocrys_condensed_struct_and_description( + structure=structure, mineral_matcher=mineral_matcher ) - - condensed_structure = StructureCondenser().condense_structure(structure) - description = StructureDescriber( - describe_symmetry_labels=False, fmt="unicode", return_parts=False - ).describe(condensed_structure=condensed_structure) + ) return super().from_structure( meta_structure=structure, material_id=material_id, condensed_structure=condensed_structure, description=description, - robocrys_version=__robocrys_version__, + robocrys_version=robocrys_version, **kwargs ) diff --git a/emmet-core/emmet/core/utils.py b/emmet-core/emmet/core/utils.py index 798876e052..e92f8024d4 100644 --- a/emmet-core/emmet/core/utils.py +++ b/emmet-core/emmet/core/utils.py @@ -5,8 +5,6 @@ from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np -from emmet.core.mpid import MPculeID -from emmet.core.settings import EmmetSettings from monty.json import MSONable from pydantic import BaseModel from pymatgen.analysis.elasticity.strain import Deformation @@ -23,6 +21,11 @@ DeformStructureTransformation, ) from pymatgen.util.graph_hashing import weisfeiler_lehman_graph_hash +from robocrys import StructureCondenser, StructureDescriber +from robocrys.condense.mineral import MineralMatcher + +from emmet.core.mpid import MPculeID +from emmet.core.settings import EmmetSettings try: import bson @@ -105,6 +108,19 @@ def undeform_structure(structure: Structure, transformations: Dict) -> Structure return structure +def generate_robocrys_condensed_struct_and_description( + structure: Structure, mineral_matcher: MineralMatcher | None = None +) -> tuple[dict[Any], str]: + condenser = StructureCondenser(mineral_matcher=mineral_matcher) + describer = StructureDescriber( + describe_symmetry_labels=False, fmt="unicode", return_parts=False + ) + condensed_structure = condenser.condense_structure(structure) + description = describer.describe(condensed_structure) + + return condensed_structure, description + + def group_molecules(molecules: List[Molecule]): """ Groups molecules according to composition, charge, and equality