Skip to content

Commit

Permalink
Patch lint (#197)
Browse files Browse the repository at this point in the history
* test lint

* test lint

* test lint
  • Loading branch information
jmmshn authored May 21, 2024
1 parent 8a44d47 commit a50a35b
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 85 deletions.
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
exclude: ^(tests)

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
Expand Down
4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ def v_N_GaN(test_dir):


@pytest.fixture(scope="session")
def basic_fed(
data_Mg_Ga, defect_entries_and_plot_data_Mg_Ga, stable_entries_Mg_Ga_N
):
def basic_fed(data_Mg_Ga, defect_entries_and_plot_data_Mg_Ga, stable_entries_Mg_Ga_N):
bulk_vasprun = data_Mg_Ga["bulk_sc"]["vasprun"]
bulk_bs = bulk_vasprun.get_band_structure()
vbm = bulk_bs.get_vbm()["energy"]
Expand Down
17 changes: 9 additions & 8 deletions tests/plotting/test_thermo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import pytest
from pymatgen.analysis.defects.plotting.thermo import plot_formation_energy_diagrams, plot_chempot_2d
from pymatgen.analysis.defects.plotting.thermo import (
plot_chempot_2d,
plot_formation_energy_diagrams,
)
from pymatgen.core import Element

def test_fed_plot(basic_fed):
fig = plot_formation_energy_diagrams([basic_fed])
assert {d_.name for d_ in fig.data} == {'Mg_Ga', 'Mg_Ga:slope'}

def test_chempot_plot(basic_fed):
plot_chempot_2d(basic_fed, x_element=Element("Mg"), y_element=Element("Ga"))

def test_fed_plot(basic_fed) -> None:
fig = plot_formation_energy_diagrams([basic_fed])
assert {d_.name for d_ in fig.data} == {"Mg_Ga", "Mg_Ga:slope"}


def test_chempot_plot(basic_fed) -> None:
plot_chempot_2d(basic_fed, x_element=Element("Mg"), y_element=Element("Ga"))
12 changes: 12 additions & 0 deletions tests/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.ruff.format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"
22 changes: 12 additions & 10 deletions tests/test_ccd.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def hd1(v_ga):
assert pytest.approx(hd1.omega_eV) == 0.03341323356861477
return hd1

def test_defect_band_raises(v_ga):

def test_defect_band_raises(v_ga) -> None:
vaspruns = v_ga[(0, -1)]["vaspruns"]
procar = v_ga[(0, -1)]["procar"]
hd0 = HarmonicDefect.from_vaspruns(
Expand All @@ -55,15 +56,16 @@ def test_defect_band_raises(v_ga):
)
# mis-matched defect band
hd0.defect_band = [(138, 0, 1), (139, 1, 1)]
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
assert hd0.defect_band_index

# mis-matched defect spin
hd0.defect_band = [(138, 0, 1), (138, 1, 0)]
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
assert hd0.spin_index == 1

def test_HarmonicDefect(hd0, v_ga, test_dir):

def test_HarmonicDefect(hd0, v_ga, test_dir) -> None:
# test other basic reading functions for HarmonicDefect
vaspruns = v_ga[(0, -1)]["vaspruns"]
procar = v_ga[(0, -1)]["procar"]
Expand Down Expand Up @@ -100,7 +102,7 @@ def test_HarmonicDefect(hd0, v_ga, test_dir):
assert "Spin index" in str(e.value)


def test_wswq(hd0, test_dir):
def test_wswq(hd0, test_dir) -> None:
wswq_dir = test_dir / "v_Ga" / "ccd_0_-1" / "wswqs"

# check for ValueError when you have mis-matched distortions and wswqs
Expand All @@ -119,7 +121,7 @@ def test_wswq(hd0, test_dir):
assert np.linalg.norm(elph_me[..., 139]) > 0


def test_wswq_slope():
def test_wswq_slope() -> None:
# Make sure the the slope is automatically defined as the sign of the distoration changes.
mats = [np.ones((3, 5)), np.zeros((3, 5)), np.ones((3, 5))]
FakeWSWQ = namedtuple("FakeWSWQ", ["data"])
Expand All @@ -132,7 +134,7 @@ def test_wswq_slope():
assert np.allclose(res, np.ones((3, 5)) * 1)


def test_SRHCapture(hd0, hd1, test_dir):
def test_SRHCapture(hd0, hd1, test_dir) -> None:
from pymatgen.analysis.defects.ccd import get_SRH_coefficient

hd0.read_wswqs(test_dir / "v_Ga" / "ccd_0_-1" / "wswqs")
Expand All @@ -159,7 +161,7 @@ def test_SRHCapture(hd0, hd1, test_dir):
assert "WSWQ" in str(e.value)


def test_dielectric_func(test_dir):
def test_dielectric_func(test_dir) -> None:
dir0_opt = test_dir / "v_Ga" / "ccd_0_-1" / "optics"
hd0 = HarmonicDefect.from_directories(
directories=[dir0_opt],
Expand Down Expand Up @@ -187,5 +189,5 @@ def test_dielectric_func(test_dir):
assert df.iloc[5]["jb"] == 100


def test_plot_pes(hd0):
def test_plot_pes(hd0) -> None:
plot_pes(hd0)
12 changes: 6 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pymatgen.core.periodic_table import Element, Specie


def test_vacancy(gan_struct):
def test_vacancy(gan_struct) -> None:
s = gan_struct.copy()
vac = Vacancy(s, s.sites[0])
vac2 = Vacancy(s, s.sites[1])
Expand All @@ -29,7 +29,7 @@ def test_vacancy(gan_struct):
assert vac.latex_name == r"v$_{\rm Ga}$"


def test_substitution(gan_struct):
def test_substitution(gan_struct) -> None:
s = gan_struct.copy()
n_site = s.sites[3]
assert n_site.specie.symbol == "N"
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_substitution(gan_struct):
assert n_ga.get_charge_states() == [-100, 102]


def test_interstitial(gan_struct):
def test_interstitial(gan_struct) -> None:
s = gan_struct.copy()
inter_fpos = [0, 0, 0.75]
n_site = PeriodicSite(Specie("N"), inter_fpos, s.lattice)
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_interstitial(gan_struct):
assert inter2.get_charge_states() == [-100, 102]


def test_adsorbate(gan_struct):
def test_adsorbate(gan_struct) -> None:
s = gan_struct.copy()
ads_fpos = [0, 0, 0.75]
n_site = PeriodicSite(Specie("N"), ads_fpos, s.lattice)
Expand All @@ -152,7 +152,7 @@ def test_adsorbate(gan_struct):
assert str(ads) == "N adsorbate site at [0.00,0.00,0.75]"


def test_complex(gan_struct):
def test_complex(gan_struct) -> None:
s = gan_struct.copy()
o_site = PeriodicSite(Specie("O"), s[3].frac_coords, s.lattice)
sub = Substitution(s, o_site) # O substituted on N site
Expand All @@ -177,7 +177,7 @@ def test_complex(gan_struct):
assert dc2 != dc


def test_parsing_and_grouping_NamedDefects(test_dir):
def test_parsing_and_grouping_NamedDefects(test_dir) -> None:
bulk_dir = test_dir / "Mg_Ga" / "bulk_sc"
defect_dir = test_dir / "Mg_Ga" / "q=0"
bulk_struct = Structure.from_file(bulk_dir / "CONTCAR.gz")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
)


def test_freysoldt(data_Mg_Ga):
def test_freysoldt(data_Mg_Ga) -> None:
"""Older basic test for Freysoldt correction."""
bulk_locpot = data_Mg_Ga["bulk_sc"]["locpot"]
defect_locpot = data_Mg_Ga["q=0"]["locpot"]
Expand Down Expand Up @@ -44,7 +44,7 @@ def test_freysoldt(data_Mg_Ga):
)


def test_freysoldt_sxdefect_compare(v_N_GaN):
def test_freysoldt_sxdefect_compare(v_N_GaN) -> None:
"""More detailed test for Freysoldt correction.
Compare against results from the sxdefectalign tool from SPHInX.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_finder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_defect_finder(test_dir):
def test_defect_finder(test_dir) -> None:
from pymatgen.analysis.defects.finder import DefectSiteFinder
from pymatgen.core import IStructure

Expand Down
14 changes: 7 additions & 7 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)


def test_vacancy_generators(gan_struct):
def test_vacancy_generators(gan_struct) -> None:
vacancy_generator = VacancyGenerator().get_defects(gan_struct)
for defect in vacancy_generator:
assert isinstance(defect, Vacancy)
Expand All @@ -29,7 +29,7 @@ def test_vacancy_generators(gan_struct):
)


def test_substitution_generators(gan_struct):
def test_substitution_generators(gan_struct) -> None:
sub_generator = SubstitutionGenerator().get_defects(
gan_struct, {"Ga": ["Mg", "Ca"]}
)
Expand All @@ -49,13 +49,13 @@ def test_substitution_generators(gan_struct):
}


def test_antisite_generator(gan_struct):
def test_antisite_generator(gan_struct) -> None:
anti_gen = AntiSiteGenerator().get_defects(gan_struct)
def_names = [defect.name for defect in anti_gen]
assert sorted(def_names) == ["Ga_N", "N_Ga"]


def test_interstitial_generator(gan_struct):
def test_interstitial_generator(gan_struct) -> None:
gen = InterstitialGenerator().get_defects(
gan_struct, insertions={"Mg": [[0, 0, 0]]}
)
Expand All @@ -71,7 +71,7 @@ def test_interstitial_generator(gan_struct):
assert len(l_gen) == 1


def test_charge_interstitial_generator(chgcar_fe3o4):
def test_charge_interstitial_generator(chgcar_fe3o4) -> None:
gen = ChargeInterstitialGenerator().get_defects(chgcar_fe3o4, {"Ga"})
cnt = 0
for defect in gen:
Expand All @@ -81,7 +81,7 @@ def test_charge_interstitial_generator(chgcar_fe3o4):
assert cnt == 2


def test_voronoi_interstitial_generator(chgcar_fe3o4):
def test_voronoi_interstitial_generator(chgcar_fe3o4) -> None:
gen = VoronoiInterstitialGenerator().get_defects(chgcar_fe3o4.structure, {"Li"})
cnt = 0
for defect in gen:
Expand All @@ -91,7 +91,7 @@ def test_voronoi_interstitial_generator(chgcar_fe3o4):
assert cnt == 4


def test_generate_all_native_defects(chgcar_fe3o4):
def test_generate_all_native_defects(chgcar_fe3o4) -> None:
gen = generate_all_native_defects(chgcar_fe3o4)
assert len(list(gen)) == 14

Expand Down
12 changes: 6 additions & 6 deletions tests/test_recombination.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)


def test_boltzmann():
def test_boltzmann() -> None:
ref_results = [
0.9791034813819097,
0.020459854127734073,
Expand All @@ -27,7 +27,7 @@ def test_boltzmann():
assert np.allclose(results2[:, 1], ref_results, rtol=1e-3)


def test_get_vibronic_matrix_elements():
def test_get_vibronic_matrix_elements() -> None:
# precompute values of the overlap
dQ, omega_i, omega_f = 0, 0.2, 0.2
Ni, Nf = 5, 5
Expand All @@ -42,7 +42,7 @@ def test_get_vibronic_matrix_elements():
assert np.allclose(matel, ref_result)


def test_pchip_eval():
def test_pchip_eval() -> None:
x_c = np.linspace(0, 2, 5)
y_c = np.sin(x_c) + 1
xx = np.linspace(-3, 3, 1000)
Expand All @@ -52,7 +52,7 @@ def test_pchip_eval():
assert int_val == pytest.approx(int_ref, rel=1e-3)


def test_get_SRH_coef():
def test_get_SRH_coef() -> None:
ref_res = [4.64530153e-14, 4.64752885e-14, 4.75265302e-14]
res = get_SRH_coef(
T=[100, 200, 300],
Expand All @@ -67,8 +67,8 @@ def test_get_SRH_coef():
assert np.allclose(res, ref_res)


def test_get_Rad_coef():
res = get_Rad_coef(
def test_get_Rad_coef() -> None:
get_Rad_coef(
T=[100, 200, 300],
dQ=1.0,
dE=1.0,
Expand Down
25 changes: 13 additions & 12 deletions tests/test_supercells.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import numpy as np
import pytest
from monty.serialization import loadfn
from pymatgen.analysis.defects.generators import VacancyGenerator
from pymatgen.analysis.defects.supercells import (
_ase_cubic,
get_closest_sc_mat,
get_matched_structure_mapping,
get_sc_fromstruct,
get_closest_sc_mat
)
import pytest


def test_supercells(gan_struct):
def test_supercells(gan_struct) -> None:
uc = gan_struct.copy()
sc_mat = get_sc_fromstruct(uc)
sc = uc * sc_mat
Expand All @@ -24,7 +24,7 @@ def test_supercells(gan_struct):
) # the sc_mat can be reconstructed from the sc


def test_ase_supercells(gan_struct):
def test_ase_supercells(gan_struct) -> None:
sc_mat = _ase_cubic(gan_struct, min_atoms=4, max_atoms=8, min_length=1.0)
sc = gan_struct * sc_mat
assert 4 <= sc.num_sites <= 8
Expand All @@ -34,27 +34,28 @@ def test_ase_supercells(gan_struct):
_ase_cubic(gan_struct, min_atoms=4, max_atoms=8, min_length=10)


def test_closest_sc_mat(test_dir):
def test_closest_sc_mat(test_dir) -> None:
si_o_structs = loadfn(test_dir / "Si-O_structs.json")
ref_sc_mat = [[2,1,2], [2,0,3], [2,1,1]]
ref_sc_mat = [[2, 1, 2], [2, 0, 3], [2, 1, 1]]

vg = VacancyGenerator()

def get_vac(s, sc_mat):
vac = next(vg.generate(s, rm_species=["O"]))
return vac.get_supercell_structure(sc_mat=sc_mat)
def check_uc(uc_struct, sc_mat):

def check_uc(uc_struct, sc_mat) -> None:
vac_sc = get_vac(uc_struct, sc_mat)
sorted_results = get_closest_sc_mat(uc_struct, vac_sc, debug=True)
min_dist = sorted_results[0][0]
close_mats = [r[2] for r in sorted_results if r[0] < min_dist*1.1]
close_mats = [r[2] for r in sorted_results if r[0] < min_dist * 1.1]
is_matched = [np.allclose(ref_sc_mat, x) for x in close_mats]
assert any(is_matched)

for s in si_o_structs:
check_uc(s, ref_sc_mat)

uc_struct = si_o_structs[0]
vac_struct = get_vac(uc_struct, ref_sc_mat)
res = get_closest_sc_mat(uc_struct=uc_struct, sc_struct=vac_struct, debug=False)
assert np.allclose(res, ref_sc_mat)
assert np.allclose(res, ref_sc_mat)
Loading

0 comments on commit a50a35b

Please sign in to comment.