-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add LRU cache to structure matcher (#4036)
* add LRU cache to _get_reduced_structure computations * pre-commit auto-fixes * make structure hashable via as_dict * make structure hash recursive as_dict, change structure test to check hashability * precommit * moved computation using lru_cache out of class method to avoid memory leakage issue * pre-commit auto-fixes * fix structure matcher caching, fix a few tests (mcsqs wrong file destination and missing pytest approx in TestBSPlot) * precommit * add suggested SiteOrderedIStructure from @kbuma * pre-commit auto-fixes * add cast in eq for SiteOrderedIStructure to make mypy happy * pre-commit auto-fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: esoteric-ephemera <[email protected]>
- Loading branch information
1 parent
54cdebc
commit 51ea7de
Showing
3 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,13 @@ | |
|
||
import abc | ||
import itertools | ||
from typing import TYPE_CHECKING | ||
from functools import lru_cache | ||
from typing import TYPE_CHECKING, cast | ||
|
||
import numpy as np | ||
from monty.json import MSONable | ||
|
||
from pymatgen.core import Composition, Lattice, Structure, get_el_sp | ||
from pymatgen.core import SETTINGS, Composition, IStructure, Lattice, Structure, get_el_sp | ||
from pymatgen.optimization.linear_assignment import LinearAssignment | ||
from pymatgen.util.coord import lattice_points_in_supercell | ||
from pymatgen.util.coord_cython import is_coord_subset_pbc, pbc_shortest_vectors | ||
|
@@ -29,6 +30,31 @@ | |
__email__ = "[email protected]" | ||
__status__ = "Production" | ||
__date__ = "Dec 3, 2012" | ||
LRU_CACHE_SIZE = SETTINGS.get("STRUCTURE_MATCHER_CACHE_SIZE", 300) | ||
|
||
|
||
class SiteOrderedIStructure(IStructure): | ||
""" | ||
Imutable structure where the order of sites matters. | ||
In caching reduced structures (see `StructureMatcher._get_reduced_structure`) | ||
the order of input sites can be important. | ||
In general, the order of sites in a structure does not matter, but when | ||
a method like `StructureMatcher.get_s2_like_s1` tries to put s2's sites in | ||
the same order as s1, the site order matters. | ||
""" | ||
|
||
def __eq__(self, other: object) -> bool: | ||
"""Check for IStructure equality and same site order.""" | ||
if not super().__eq__(other): | ||
return False | ||
other = cast(SiteOrderedIStructure, other) # make mypy happy | ||
|
||
return list(self.sites) == list(other.sites) | ||
|
||
def __hash__(self) -> int: | ||
"""Use the composition hash for now.""" | ||
return super().__hash__() | ||
|
||
|
||
class AbstractComparator(MSONable, abc.ABC): | ||
|
@@ -939,16 +965,26 @@ def _anonymous_match( | |
break | ||
return matches | ||
|
||
@classmethod | ||
def _get_reduced_structure(cls, struct: Structure, primitive_cell: bool = True, niggli: bool = True) -> Structure: | ||
"""Helper method to find a reduced structure.""" | ||
@staticmethod | ||
@lru_cache(maxsize=LRU_CACHE_SIZE) | ||
def _get_reduced_istructure( | ||
struct: SiteOrderedIStructure, primitive_cell: bool = True, niggli: bool = True | ||
) -> SiteOrderedIStructure: | ||
"""Helper method to find a reduced imutable structure.""" | ||
reduced = struct.copy() | ||
if niggli: | ||
reduced = reduced.get_reduced_structure(reduction_algo="niggli") | ||
if primitive_cell: | ||
reduced = reduced.get_primitive_structure() | ||
return reduced | ||
|
||
@classmethod | ||
def _get_reduced_structure(cls, struct: Structure, primitive_cell: bool = True, niggli: bool = True) -> Structure: | ||
"""Helper method to find a reduced structure.""" | ||
return Structure.from_sites( | ||
cls._get_reduced_istructure(SiteOrderedIStructure.from_sites(struct), primitive_cell, niggli) | ||
) | ||
|
||
def get_rms_anonymous(self, struct1, struct2): | ||
""" | ||
Performs an anonymous fitting, which allows distinct species in one | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters