Skip to content

Commit 65f4a61

Browse files
authored
Test improvements (#3497)
* update ruff-pre-commit to v0.1.7 * fic doc str indent * improve test_get_neighbor_list * improve test_ebands_input * improve asserts in test_get_feff_pot * remove unused assignment inside pytest.raises * fix some unused var F841
1 parent 53aaa84 commit 65f4a61

File tree

15 files changed

+62
-50
lines changed

15 files changed

+62
-50
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ci:
88

99
repos:
1010
- repo: https://github.com/astral-sh/ruff-pre-commit
11-
rev: v0.1.6
11+
rev: v0.1.7
1212
hooks:
1313
- id: ruff
1414
args: [--fix]

pymatgen/analysis/adsorption.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ def substitute(site, i):
576576
# surface atoms, i.e. search for sites above (below) the bottom (top) surface
577577
sorted_sites = sorted(sym_slab, key=lambda site: site.frac_coords[2])
578578
if sorted_sites[0].surface_properties == "surface":
579-
d = sorted_sites[0].frac_coords[2] + dist_from_surf
579+
dist = sorted_sites[0].frac_coords[2] + dist_from_surf
580580
else:
581-
d = sorted_sites[-1].frac_coords[2] - dist_from_surf
581+
dist = sorted_sites[-1].frac_coords[2] - dist_from_surf
582582

583583
for idx, site in enumerate(sym_slab):
584-
if d - range_tol < site.frac_coords[2] < d + range_tol and (
584+
if dist - range_tol < site.frac_coords[2] < dist + range_tol and (
585585
target_species and site.species_string in target_species or not target_species
586586
):
587587
substituted_slabs.append(substitute(site, idx))

pymatgen/core/composition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def get_reduced_formula_and_factor(self, iupac_ordering: bool = False) -> tuple[
363363
if not all_int:
364364
return self.formula.replace(" ", ""), 1
365365
d = {k: int(round(v)) for k, v in self.get_el_amt_dict().items()}
366-
(formula, factor) = reduce_formula(d, iupac_ordering=iupac_ordering)
366+
formula, factor = reduce_formula(d, iupac_ordering=iupac_ordering)
367367

368368
if formula in Composition.special_formulas:
369369
formula = Composition.special_formulas[formula]

pymatgen/electronic_structure/dos.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,10 @@ def get_dos_fp(
10971097
n_bins: int = 256,
10981098
normalize: bool = True,
10991099
) -> NamedTuple:
1100-
"""Generates the DOS fingerprint based on work of
1100+
"""Generates the DOS fingerprint.
1101+
1102+
Based on work of:
1103+
11011104
F. Knoop, T. A. r Purcell, M. Scheffler, C. Carbogno, J. Open Source Softw. 2020, 5, 2671.
11021105
Source - https://gitlab.com/vibes-developers/vibes/-/tree/master/vibes/materials_fp
11031106
Copyright (c) 2020 Florian Knoop, Thomas A.R.Purcell, Matthias Scheffler, Christian Carbogno.
@@ -1117,7 +1120,7 @@ def get_dos_fp(
11171120
11181121
Returns:
11191122
Fingerprint(namedtuple) : The electronic density of states fingerprint
1120-
of format (energies, densities, type, n_bins)
1123+
of format (energies, densities, type, n_bins)
11211124
"""
11221125
fingerprint = namedtuple("fingerprint", "energies densities type n_bins bin_width")
11231126
energies = self.energies - self.efermi

pymatgen/symmetry/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ def not_on_axis(site):
11551155
return np.linalg.norm(v) > self.tol
11561156

11571157
valid_sets = []
1158-
origin_site, dist_el_sites = cluster_sites(self.centered_mol, self.tol)
1158+
_origin_site, dist_el_sites = cluster_sites(self.centered_mol, self.tol)
11591159
for test_set in dist_el_sites.values():
11601160
valid_set = list(filter(not_on_axis, test_set))
11611161
if len(valid_set) > 0:

pymatgen/symmetry/kpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,7 @@ def _get_max_cosine_labels(self, max_cosine_orbits_orig, key_points_inds_orbits,
21812181
max_cosine_orbits_copy = max_cosine_orbits_orig.copy()
21822182
max_cosine_label_inds = np.zeros(len(max_cosine_orbits_copy))
21832183
initial_max_cosine_label_inds = [max_cos_orb[0][0] for max_cos_orb in max_cosine_orbits_copy]
2184-
u, inds, counts = np.unique(initial_max_cosine_label_inds, return_index=True, return_counts=True)
2184+
_uniq_vals, inds, counts = np.unique(initial_max_cosine_label_inds, return_index=True, return_counts=True)
21852185
grouped_inds = [
21862186
[
21872187
i

tests/analysis/solar/test_slme.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ def test_slme_from_vasprun(self):
1414
abz = abz * 100.0
1515
eff = slme(en, abz, indir_gap, indir_gap, plot_current_voltage=False)
1616
assert eff == approx(27.729, abs=1e-2)
17+
assert dir_gap == approx(0.85389999, abs=1e-6)

tests/analysis/test_molecule_matcher.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def test_mismatched_atom_composition(self):
380380
mol_matcher = HungarianOrderMatcher(mol1)
381381

382382
with pytest.raises(ValueError, match="The number of the same species aren't matching"):
383-
_, rmsd = mol_matcher.fit(mol2)
383+
mol_matcher.fit(mol2)
384384

385385
def test_fit(self):
386386
mol1 = Molecule.from_file(f"{test_dir}/benzene1.xyz")
@@ -485,7 +485,7 @@ def test_mismatched_atom_composition(self):
485485
mol_matcher = GeneticOrderMatcher(mol1, threshold=0.3)
486486

487487
with pytest.raises(ValueError, match="The number of the same species aren't matching"):
488-
_, rmsd = mol_matcher.fit(mol2)[0]
488+
mol_matcher.fit(mol2)[0]
489489

490490
def test_fit(self):
491491
mol1 = Molecule.from_file(f"{test_dir}/benzene1.xyz")
@@ -562,7 +562,7 @@ def test_mismatched_atoms(self):
562562
with pytest.raises(
563563
ValueError, match="The order of the species aren't matching! Please try using PermInvMatcher"
564564
):
565-
_, rmsd = self.mol_matcher.fit(mol2)
565+
self.mol_matcher.fit(mol2)
566566

567567
def test_rotated_molecule(self):
568568
mol2 = Molecule.from_file(f"{test_dir}/Si_cluster_rotated.xyz")
@@ -600,7 +600,7 @@ def test_random_match(self):
600600
ValueError,
601601
match="The number of all possible permutations \\(20922789888000\\) is not feasible to run this method",
602602
):
603-
_, rmsd = self.mol_matcher.fit(mol2)
603+
self.mol_matcher.fit(mol2)
604604

605605

606606
class TestHungarianOrderMatcherSi(unittest.TestCase):
@@ -617,7 +617,7 @@ def test_to_and_from_dict(self):
617617
def test_mismatched_atoms(self):
618618
mol2 = Molecule.from_file(f"{test_dir}/Si2O_cluster_rotated.xyz")
619619
with pytest.raises(ValueError, match="The number of the same species aren't matching"):
620-
_, rmsd = self.mol_matcher.fit(mol2)
620+
self.mol_matcher.fit(mol2)
621621

622622
def test_rotated_molecule(self):
623623
# TODO: Checking the cause of the large deviation
@@ -690,7 +690,7 @@ def test_mismatched_atoms(self):
690690
with pytest.raises(
691691
ValueError, match="The order of the species aren't matching! Please try using PermInvMatcher"
692692
):
693-
_, rmsd = self.mol_matcher.fit(mol2)
693+
self.mol_matcher.fit(mol2)
694694

695695
def test_rotated_molecule(self):
696696
mol2 = Molecule.from_file(f"{test_dir}/Si2O_cluster_rotated.xyz")
@@ -709,7 +709,7 @@ def test_permuted_atoms_order(self):
709709
with pytest.raises(
710710
ValueError, match="The order of the species aren't matching! Please try using PermInvMatcher"
711711
):
712-
_, rmsd = self.mol_matcher.fit(mol2)
712+
self.mol_matcher.fit(mol2)
713713

714714

715715
class TestBruteForceOrderMatcherSi2O(unittest.TestCase):
@@ -721,7 +721,7 @@ def setUpClass(cls):
721721
def test_mismatched_atoms(self):
722722
mol2 = Molecule.from_file(f"{test_dir}/Si_cluster_rotated.xyz")
723723
with pytest.raises(ValueError, match="The number of the same species aren't matching"):
724-
_, rmsd = self.mol_matcher.fit(mol2)
724+
self.mol_matcher.fit(mol2)
725725

726726
def test_rotated_molecule(self):
727727
mol2 = Molecule.from_file(f"{test_dir}/Si2O_cluster_rotated.xyz")
@@ -753,7 +753,7 @@ def setUpClass(cls):
753753
def test_mismatched_atoms(self):
754754
mol2 = Molecule.from_file(f"{test_dir}/Si_cluster_rotated.xyz")
755755
with pytest.raises(ValueError, match="The number of the same species aren't matching"):
756-
_, rmsd = self.mol_matcher.fit(mol2)
756+
self.mol_matcher.fit(mol2)
757757

758758
def test_rotated_molecule(self):
759759
mol2 = Molecule.from_file(f"{test_dir}/Si2O_cluster_rotated.xyz")

tests/analysis/test_structure_matcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ def test_find_match1(self):
401401
s2 = Structure(latt, ["Si", "Si", "Ag"], [[0, 0.1, 0], [0, 0.1, -0.95], [0.7, 0.5, 0.375]])
402402

403403
s1, s2, fu, s1_supercell = sm._preprocess(s1, s2, niggli=False)
404+
assert s1_supercell is True
404405
match = sm._strict_match(s1, s2, fu, s1_supercell=True, use_rms=True, break_on_match=False)
405406
scale_matrix = match[2]
406407
s2.make_supercell(scale_matrix)

tests/core/test_structure.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,18 +648,17 @@ def test_get_all_neighbors_and_get_neighbors(self):
648648
assert len(neigh_sites) == 1
649649

650650
def test_get_neighbor_list(self):
651-
struct = self.struct
652-
c_indices1, c_indices2, c_offsets, c_distances = struct.get_neighbor_list(3)
653-
p_indices1, p_indices2, p_offsets, p_distances = struct._get_neighbor_list_py(3)
654-
assert_allclose(sorted(c_distances), sorted(p_distances))
655-
656651
# test mutable structure after applying strain (which makes lattice.matrix array no longer contiguous)
657652
# https://github.com/materialsproject/pymatgen/pull/3108
658-
mutable_struct = Structure.from_sites(struct)
653+
mutable_struct = Structure.from_sites(self.struct)
659654
mutable_struct.apply_strain(0.01)
660-
c_indices1, c_indices2, c_offsets, c_distances = mutable_struct.get_neighbor_list(3)
661-
p_indices1, p_indices2, p_offsets, p_distances = mutable_struct._get_neighbor_list_py(3)
662-
assert_allclose(sorted(c_distances), sorted(p_distances))
655+
for struct in (self.struct, mutable_struct):
656+
cy_indices1, cy_indices2, cy_offsets, cy_distances = struct.get_neighbor_list(3)
657+
py_indices1, py_indices2, py_offsets, py_distances = struct._get_neighbor_list_py(3)
658+
assert_allclose(cy_distances, py_distances)
659+
assert_allclose(cy_indices1, py_indices1)
660+
assert_allclose(cy_indices2, py_indices2)
661+
assert len(cy_offsets) == len(py_offsets)
663662

664663
# @skipIf(not os.getenv("CI"), "Only run this in CI tests")
665664
# def test_get_all_neighbors_crosscheck_old(self):
@@ -728,6 +727,8 @@ def test_get_symmetric_neighbor_list(self):
728727
# tetragonal group with all bonds related by symmetry
729728
struct = Structure.from_spacegroup(100, [[1, 0, 0], [0, 1, 0], [0, 0, 2]], ["Fe"], [[0.0, 0.0, 0.0]])
730729
c_indices, p_indices, offsets, distances, s_indices, sym_ops = struct.get_symmetric_neighbor_list(0.8, sg=100)
730+
assert len(c_indices) == len(p_indices) == len(offsets) == len(distances) == 8
731+
assert c_indices == pytest.approx([0, 1, 1, 1, 0, 0, 0, 0])
731732
assert len(np.unique(s_indices)) == 1
732733
assert s_indices[0] == 0
733734
assert all(~np.isnan(s_indices))
@@ -756,6 +757,7 @@ def test_get_symmetric_neighbor_list(self):
756757
)
757758
assert all(np.sort(np.array([c_indices3, p_indices3]).flatten()) == np.sort(c_indices2))
758759
assert all(np.sort(np.array([c_indices3, p_indices3]).flatten()) == np.sort(p_indices2))
760+
assert len(offsets3) == len(distances3) == len(s_indices3) == 24
759761

760762
def test_get_all_neighbors_outside_cell(self):
761763
struct = Structure(

0 commit comments

Comments
 (0)