Skip to content

Commit

Permalink
More suppression of (unnecessary) spglib deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kavanase committed Jun 11, 2024
1 parent 6e1e35d commit 209e72e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions doped/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def defect_from_structures(
Dictionary of bulk supercell Voronoi node information, for
further expedited site-matching.
"""
warnings.filterwarnings("ignore", "dict interface") # ignore spglib warning from v2.4.1
try:
def_type, comp_diff = get_defect_type_and_composition_diff(bulk_supercell, defect_supercell)
except RuntimeError as exc:
Expand Down
2 changes: 2 additions & 0 deletions doped/thermodynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ def group_defects_by_distance(
# would be independent of this... but challenging to setup without complex clustering approaches (
# for now this works very well as is, and this is a rare case and usually not a problem anyway as
# dist_tol can just be adjusted as needed)
warnings.filterwarnings("ignore", "dict interface") # ignore spglib warning from v2.4.1

# initial group by Defect.name (same nominal defect), then distance to equiv sites
# first make dictionary of nominal defect name: list of entries with that name
defect_name_dict = {}
Expand Down
24 changes: 14 additions & 10 deletions doped/utils/symmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,9 @@ def get_wyckoff(frac_coords, struct, symm_ops: Optional[list] = None, equiv_site
symm_dataset, unique_sites = _get_symm_dataset_of_struc_with_all_equiv_sites(
frac_coords, struct, symm_ops, symprec=symprec
)
conv_cell_factor = len(symm_dataset.std_positions) / len(symm_dataset.wyckoffs)
conv_cell_factor = len(symm_dataset["std_positions"]) / len(symm_dataset["wyckoffs"])
multiplicity = int(conv_cell_factor * len(unique_sites))
wyckoff_label = f"{multiplicity}{symm_dataset.wyckoffs[-1]}"
wyckoff_label = f"{multiplicity}{symm_dataset['wyckoffs'][-1]}"

return (wyckoff_label, unique_sites) if equiv_sites else wyckoff_label

Expand Down Expand Up @@ -1086,7 +1086,7 @@ def point_symmetry_from_defect(defect, symm_ops=None, symprec=0.01):
symm_dataset, _unique_sites = _get_symm_dataset_of_struc_with_all_equiv_sites(
defect.site.frac_coords, defect.structure, symm_ops=symm_ops, symprec=symprec
)
spglib_point_group_symbol = schoenflies_from_hermann(symm_dataset.site_symmetry_symbols[-1])
spglib_point_group_symbol = schoenflies_from_hermann(symm_dataset["site_symmetry_symbols"][-1])
if spglib_point_group_symbol is not None:
return spglib_point_group_symbol

Expand Down Expand Up @@ -1182,6 +1182,7 @@ def point_symmetry_from_defect_entry(
a boolean specifying if the supercell has been detected to break the crystal
periodicity).
"""
warnings.filterwarnings("ignore", "dict interface") # ignore spglib warning from v2.4.1
if symprec is None:
symprec = 0.1 if relaxed else 0.01 # relaxed structures likely have structural noise
# May need to adjust symprec (e.g. for Ag2Se, symprec of 0.2 is too large as we have very
Expand All @@ -1200,7 +1201,7 @@ def point_symmetry_from_defect_entry(
# then easy, can just be taken from symmetry dataset of defect structure
symm_dataset = _get_sga(defect_entry.defect.structure, symprec=symprec).get_symmetry_dataset()
return schoenflies_from_hermann(
symm_dataset.site_symmetry_symbols[defect_entry.defect.defect_site_index]
symm_dataset["site_symmetry_symbols"][defect_entry.defect.defect_site_index]
)

supercell = _get_defect_supercell(defect_entry) if relaxed else _get_bulk_supercell(defect_entry)
Expand Down Expand Up @@ -1304,15 +1305,15 @@ def point_symmetry_from_defect_entry(
# This issue is avoided for relaxed defect supercells as we take the symm_ops of our reduced
# symmetry cell rather than that of the bulk (so no chance of spurious symmetry upgrade from
# equivalent sites), and hence the max point symmetry is the point symmetry of the defect
spglib_point_group_symbol = schoenflies_from_hermann(symm_dataset.site_symmetry_symbols[-1])
spglib_point_group_symbol = schoenflies_from_hermann(symm_dataset["site_symmetry_symbols"][-1])

# Note that, if the supercell is non-periodicity-breaking, then the site symmetry can be simply
# determined using the point group of the unrelaxed defect structure:
# unrelaxed_defect_supercell = defect_entry.calculation_metadata.get(
# "unrelaxed_defect_structure", defect_supercell
# )
# return schoenflies_from_hermann(
# _get_sga(unrelaxed_defect_supercell, symprec).get_symmetry_dataset().pointgroup,
# _get_sga(unrelaxed_defect_supercell, symprec).get_symmetry_dataset()["pointgroup"],
# )
# But current approach works for all cases with unrelaxed defect structures

Expand All @@ -1321,15 +1322,15 @@ def point_symmetry_from_defect_entry(
# the defect (e.g. for split-interstitials, split-vacancies, swapped vacancies etc),
# so use 'pointgroup' output (in this case the reduced symmetry avoids the symmetry-upgrade
# possibility with the equivalent sites, as when relaxed=False)
spglib_point_group_symbol = schoenflies_from_hermann(symm_dataset.pointgroup)
spglib_point_group_symbol = schoenflies_from_hermann(symm_dataset["pointgroup"])

# This also works (at least for non-periodicity-breaking supercells) for relaxed defects in
# most cases, but is slightly less robust (more sensitive to ``symprec`` choice) than the
# approach above:
# schoenflies_from_hermann(
# _get_sga(
# defect_supercell, symprec=symprec
# ).get_symmetry_dataset().pointgroup
# ).get_symmetry_dataset()["pointgroup"]
# )

if spglib_point_group_symbol is not None:
Expand Down Expand Up @@ -1376,7 +1377,7 @@ def _check_relaxed_defect_symmetry_determination(

if unrelaxed_defect_structure is not None:
unrelaxed_spglib_point_group_symbol = schoenflies_from_hermann(
_get_sga(unrelaxed_defect_structure, symprec=symprec).get_symmetry_dataset().pointgroup,
_get_sga(unrelaxed_defect_structure, symprec=symprec).get_symmetry_dataset()["pointgroup"],
)

bulk_supercell = _get_bulk_supercell(defect_entry)
Expand All @@ -1388,7 +1389,9 @@ def _check_relaxed_defect_symmetry_determination(
symprec=symprec,
dist_tol=symprec,
)
bulk_spglib_point_group_symbol = schoenflies_from_hermann(symm_dataset.site_symmetry_symbols[-1])
bulk_spglib_point_group_symbol = schoenflies_from_hermann(
symm_dataset["site_symmetry_symbols"][-1]
)

if bulk_spglib_point_group_symbol != unrelaxed_spglib_point_group_symbol:
if verbose:
Expand Down Expand Up @@ -1485,6 +1488,7 @@ def point_symmetry(
a boolean specifying if the supercell has been detected to break the crystal
periodicity).
"""
warnings.filterwarnings("ignore", "dict interface") # ignore spglib warning from v2.4.1
if symprec is None:
symprec = 0.1 if relaxed else 0.01 # relaxed structures likely have structural noise

Expand Down
4 changes: 4 additions & 0 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,9 @@ def test_extrinsic_interstitial_parsing_and_kumagai(self):
# test symmetry determination (periodicity breaking does not affect F_i):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
warnings.filterwarnings("ignore", "dict interface") # ignore spglib warning from v2.4.1
relaxed_defect_name = get_defect_name_from_entry(int_F_minus1_ent)
print([warn.message for warn in w]) # for debugging
assert not w # this supercell is not periodicity breaking
assert relaxed_defect_name == "F_i_C4v_O2.67"
assert get_defect_name_from_entry(int_F_minus1_ent, relaxed=False) == "F_i_Cs_O2.67"
Expand Down Expand Up @@ -2243,7 +2245,9 @@ def test_extrinsic_substitution_parsing_and_freysoldt_and_kumagai(self):
# test symmetry determination (no warning here because periodicity breaking doesn't affect F_O):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
warnings.filterwarnings("ignore", "dict interface") # ignore spglib warning from v2.4.1
relaxed_defect_name = get_defect_name_from_entry(F_O_1_ent)
print([warn.message for warn in w]) # for debugging
assert len(w) == 0
assert relaxed_defect_name == "F_O_D4h_Ti1.79"
assert get_defect_name_from_entry(F_O_1_ent, relaxed=False) == "F_O_D4h_Ti1.79"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def _compare_attributes(obj1, obj2, exclude=None):
elif attr == "defects" and any(len(i.defect_structure) == 0 for i in val1["vacancies"]):
continue # StructureMatcher comparison breaks for empty structures, which we can have with
# our 1-atom primitive Cu input
elif isinstance(val1, (list, tuple)) and all(isinstance(i, np.ndarray) for i in val1):
assert all(np.array_equal(i, j) for i, j in zip(val1, val2)), "List of arrays do not match"
else:
assert val1 == val2

Expand Down
8 changes: 2 additions & 6 deletions tests/test_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import contextlib
import filecmp
import json
import locale
import os
import random
Expand All @@ -15,12 +14,11 @@
import numpy as np
import pytest
from ase.build import bulk, make_supercell
from monty.json import MontyEncoder
from monty.serialization import loadfn
from pymatgen.analysis.structure_matcher import ElementComparator, StructureMatcher
from pymatgen.core.structure import Structure
from pymatgen.io.vasp.inputs import BadIncarWarning, Incar, Kpoints, Poscar, Potcar
from test_generation import if_present_rm
from test_generation import _compare_attributes, if_present_rm

from doped.generation import DefectsGenerator
from doped.vasp import (
Expand Down Expand Up @@ -1597,9 +1595,7 @@ def _check_reloaded_defect_entry(filename, ref_defect_entry):
reloaded_defect_entry.sc_defect_frac_coords,
ref_defect_entry.sc_defect_frac_coords,
)
assert json.dumps(reloaded_defect_entry, sort_keys=True, cls=MontyEncoder) == json.dumps(
ref_defect_entry, sort_keys=True, cls=MontyEncoder
)
_compare_attributes(reloaded_defect_entry, ref_defect_entry)

_check_reloaded_defect_entry("Ag_Sb_Cs_Te2.90_-2/vasp_std/Ag_Sb_Cs_Te2.90_-2.json", defect_entry)

Expand Down

0 comments on commit 209e72e

Please sign in to comment.