Skip to content

Commit

Permalink
move robocrys class + obj init's to convenience util func
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmathis committed Sep 12, 2024
1 parent c3c62e7 commit 70414bb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
30 changes: 16 additions & 14 deletions emmet-core/emmet/core/robocrys.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
)
20 changes: 18 additions & 2 deletions emmet-core/emmet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 70414bb

Please sign in to comment.