diff --git a/pymatgen/core/lattice.py b/pymatgen/core/lattice.py index 40b8a9db91c..c2501ca9c04 100644 --- a/pymatgen/core/lattice.py +++ b/pymatgen/core/lattice.py @@ -971,7 +971,7 @@ def as_dict(self, verbosity: int = 0) -> dict: verbosity (int): Verbosity level. Default of 0 only includes the matrix representation. Set to 1 for more details. """ - d = { + dct = { "@module": type(self).__module__, "@class": type(self).__name__, "matrix": self._matrix.tolist(), @@ -979,9 +979,9 @@ def as_dict(self, verbosity: int = 0) -> dict: } if verbosity > 0: keys = ["a", "b", "c", "alpha", "beta", "gamma", "volume"] - d.update(dict(zip(keys, [*self.parameters, self.volume]))) + dct.update(dict(zip(keys, [*self.parameters, self.volume]))) - return d + return dct def find_all_mappings( self, diff --git a/pymatgen/core/sites.py b/pymatgen/core/sites.py index 1590d797db5..83512c8c878 100644 --- a/pymatgen/core/sites.py +++ b/pymatgen/core/sites.py @@ -599,13 +599,13 @@ def as_dict(self, verbosity: int = 0) -> dict: """ species_list = [] for spec, occu in self._species.items(): - d = spec.as_dict() - del d["@module"] - del d["@class"] - d["occu"] = occu - species_list.append(d) + dct = spec.as_dict() + del dct["@module"] + del dct["@class"] + dct["occu"] = occu + species_list.append(dct) - d = { + dct = { "species": species_list, "abc": [float(c) for c in self._frac_coords], # type: ignore "lattice": self._lattice.as_dict(verbosity=verbosity), @@ -614,12 +614,12 @@ def as_dict(self, verbosity: int = 0) -> dict: } if verbosity > 0: - d["xyz"] = [float(c) for c in self.coords] - d["label"] = self.species_string + dct["xyz"] = [float(c) for c in self.coords] + dct["label"] = self.species_string - d["properties"] = self.properties + dct["properties"] = self.properties - return d + return dct @classmethod def from_dict(cls, d, lattice=None) -> PeriodicSite: diff --git a/pymatgen/core/structure.py b/pymatgen/core/structure.py index 3bf86607cc1..174afd711c9 100644 --- a/pymatgen/core/structure.py +++ b/pymatgen/core/structure.py @@ -2357,7 +2357,7 @@ def get_orderings(self, mode: Literal["enum", "sqs"] = "enum", **kwargs) -> list return [run_mcsqs(self, **kwargs).bestsqs] raise ValueError("Invalid mode!") - def as_dict(self, verbosity=1, fmt=None, **kwargs): + def as_dict(self, verbosity=1, fmt=None, **kwargs) -> dict[str, Any]: """ Dict representation of Structure. @@ -2386,21 +2386,21 @@ def as_dict(self, verbosity=1, fmt=None, **kwargs): latt_dict = self._lattice.as_dict(verbosity=verbosity) del latt_dict["@module"] del latt_dict["@class"] - - d = { + sites = [] + dct = { "@module": type(self).__module__, "@class": type(self).__name__, "charge": self.charge, "lattice": latt_dict, - "sites": [], } for site in self: - site_dict = site.as_dict(verbosity=verbosity) + site_dict = site.as_dict(verbosity=verbosity) # type: ignore[call-arg] del site_dict["lattice"] del site_dict["@module"] del site_dict["@class"] - d["sites"].append(site_dict) - return d + sites.append(site_dict) + dct["sites"] = sites + return dct def as_dataframe(self): """ diff --git a/pymatgen/entries/compatibility.py b/pymatgen/entries/compatibility.py index 9fc8811ca8d..46eb097dc5d 100644 --- a/pymatgen/entries/compatibility.py +++ b/pymatgen/entries/compatibility.py @@ -790,7 +790,13 @@ def __init__( correct_peroxide: Specify whether peroxide/superoxide/ozonide corrections are to be applied or not. check_potcar_hash (bool): Use potcar hash to verify potcars are correct. + silence_deprecation (bool): Silence deprecation warning. Defaults to False. """ + warnings.warn( # added by @janosh on 2023-05-25 + "MaterialsProjectCompatibility is deprecated, Materials Project formation energies " + "use the newer MaterialsProject2020Compatibility scheme.", + DeprecationWarning, + ) self.compat_type = compat_type self.correct_peroxide = correct_peroxide self.check_potcar_hash = check_potcar_hash diff --git a/pymatgen/symmetry/structure.py b/pymatgen/symmetry/structure.py index 873979611fc..0e328999902 100644 --- a/pymatgen/symmetry/structure.py +++ b/pymatgen/symmetry/structure.py @@ -4,13 +4,16 @@ from __future__ import annotations -from typing import Sequence +from typing import TYPE_CHECKING, Sequence import numpy as np from tabulate import tabulate from pymatgen.core.structure import PeriodicSite, Structure +if TYPE_CHECKING: + from pymatgen.symmetry.analyzer import SpacegroupOperations + class SymmetrizedStructure(Structure): """ @@ -27,17 +30,16 @@ class SymmetrizedStructure(Structure): def __init__( self, structure: Structure, - spacegroup, + spacegroup: SpacegroupOperations, equivalent_positions: Sequence[int], wyckoff_letters: Sequence[str], - ): + ) -> None: """ Args: structure (Structure): Original structure - spacegroup (SpacegroupOperations): An input SpacegroupOperations from - SpacegroupAnalyzer. - equivalent_positions: Equivalent positions from SpacegroupAnalyzer. - wyckoff_letters: Wyckoff letters + spacegroup (SpacegroupOperations): An input SpacegroupOperations from SpacegroupAnalyzer. + equivalent_positions (list[int]): Equivalent positions from SpacegroupAnalyzer. + wyckoff_letters (list[str]): Wyckoff letters """ self.spacegroup = spacegroup u, inv = np.unique(equivalent_positions, return_inverse=True) @@ -73,13 +75,16 @@ def copy(self): wyckoff_letters=self.wyckoff_letters, ) - def find_equivalent_sites(self, site) -> list[PeriodicSite]: + def find_equivalent_sites(self, site: PeriodicSite) -> list[PeriodicSite]: """ Finds all symmetrically equivalent sites for a particular site Args: site (PeriodicSite): A site in the structure + Raises: + ValueError: if site is not in the structure. + Returns: ([PeriodicSite]): List of all symmetrically equivalent sites. """ @@ -89,10 +94,10 @@ def find_equivalent_sites(self, site) -> list[PeriodicSite]: raise ValueError("Site not in structure") - def __repr__(self): + def __repr__(self) -> str: return str(self) - def __str__(self): + def __str__(self) -> str: outs = [ "SymmetrizedStructure", f"Full Formula ({self.composition.formula})", @@ -100,11 +105,11 @@ def __str__(self): f"Spacegroup: {self.spacegroup.int_symbol} ({self.spacegroup.int_number})", ] - def to_s(x): + def to_str(x): return f"{x:0.6f}" - outs.append("abc : " + " ".join(to_s(i).rjust(10) for i in self.lattice.abc)) - outs.append("angles: " + " ".join(to_s(i).rjust(10) for i in self.lattice.angles)) + outs.append("abc : " + " ".join(to_str(i).rjust(10) for i in self.lattice.abc)) + outs.append("angles: " + " ".join(to_str(i).rjust(10) for i in self.lattice.angles)) if self._charge: if self._charge >= 0: outs.append(f"Overall Charge: +{self._charge}") @@ -114,13 +119,13 @@ def to_s(x): data = [] props = self.site_properties keys = sorted(props) - for i, sites in enumerate(self.equivalent_sites): + for idx, sites in enumerate(self.equivalent_sites): site = sites[0] - row = [str(i), site.species_string] - row.extend([to_s(j) for j in site.frac_coords]) - row.append(self.wyckoff_symbols[i]) + row = [str(idx), site.species_string] + row.extend([to_str(j) for j in site.frac_coords]) + row.append(self.wyckoff_symbols[idx]) for k in keys: - row.append(props[k][i]) + row.append(props[k][idx]) data.append(row) outs.append( tabulate( diff --git a/pymatgen/symmetry/tests/test_analyzer.py b/pymatgen/symmetry/tests/test_analyzer.py index 0428c12b7bf..bb8a8446a8a 100644 --- a/pymatgen/symmetry/tests/test_analyzer.py +++ b/pymatgen/symmetry/tests/test_analyzer.py @@ -168,10 +168,9 @@ def test_get_refined_structure(self): refined_struct = sg.get_refined_structure(keep_site_properties=False) assert refined_struct.site_properties.get("magmom") is None - def test_get_symmetrized_structure(self): + def test_symmetrized_structure(self): symm_struct = self.sg.get_symmetrized_structure() - for a in symm_struct.lattice.angles: - assert a == 90 + assert symm_struct.lattice.angles == (90, 90, 90) assert len(symm_struct.equivalent_sites) == 5 symm_struct = self.disordered_sg.get_symmetrized_structure() @@ -190,7 +189,7 @@ def test_get_symmetrized_structure(self): ss = SymmetrizedStructure.from_dict(d) assert ss.wyckoff_symbols[0] == "16h" - assert "SymmetrizedStructure" in str(ss) + assert str(ss).startswith("SymmetrizedStructure\nFull Formula (Li20.2 Ge2.06 P3.94 S24)\nReduced Formula: ") def test_find_primitive(self): """F m -3 m Li2O testing of converting to primitive cell."""