Skip to content

Commit 4eb70cb

Browse files
authored
Fix TestMPScanStaticSet.test_as_from_dict() (#3266)
* fix TestMPScanStaticSet test-as_from_dict() * rename Kpoint private attrs _ccoords->_cart_coords, _fcoords->_frac_coords * delete test_surface.py's get_path() and refactor test path construction
1 parent 6dacfff commit 4eb70cb

File tree

8 files changed

+74
-109
lines changed

8 files changed

+74
-109
lines changed

pymatgen/alchemy/transmuters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ def batch_write_vasp_input(
346346
formula = re.sub(r"\s+", "", struct.final_structure.formula)
347347
if subfolder is not None:
348348
subdir = subfolder(struct)
349-
dirname = os.path.join(output_dir, subdir, f"{formula}_{idx}")
349+
dirname = f"{output_dir}/{subdir}/{formula}_{idx}"
350350
else:
351-
dirname = os.path.join(output_dir, f"{formula}_{idx}")
351+
dirname = f"{output_dir}/{formula}_{idx}"
352352
struct.write_vasp_input(vasp_input_set, dirname, create_directory=create_directory, **kwargs)
353353
if include_cif:
354354
from pymatgen.io.cif import CifWriter

pymatgen/analysis/elasticity/strain.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ class Deformation(SquareTensor):
3838

3939
def __new__(cls, deformation_gradient):
4040
"""
41-
Create a Deformation object. Note that the constructor uses __new__
42-
rather than __init__ according to the standard method of subclassing
43-
numpy ndarrays.
41+
Create a Deformation object. Note that the constructor uses __new__ rather than
42+
__init__ according to the standard method of subclassing numpy ndarrays.
4443
4544
Args:
4645
deformation_gradient (3x3 array-like): the 3x3 array-like

pymatgen/electronic_structure/bandstructure.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def __init__(
5454
label: the label of the kpoint if any (None by default).
5555
"""
5656
self._lattice = lattice
57-
self._fcoords = lattice.get_fractional_coords(coords) if coords_are_cartesian else coords
57+
self._frac_coords = lattice.get_fractional_coords(coords) if coords_are_cartesian else coords
5858
self._label = label
5959

6060
if to_unit_cell:
61-
for i, fc in enumerate(self._fcoords):
62-
self._fcoords[i] -= math.floor(fc)
61+
for idx, fc in enumerate(self._frac_coords):
62+
self._frac_coords[idx] -= math.floor(fc)
6363

64-
self._ccoords = lattice.get_cartesian_coords(self._fcoords)
64+
self._cart_coords = lattice.get_cartesian_coords(self._frac_coords)
6565

6666
@property
6767
def lattice(self):
@@ -78,27 +78,27 @@ def label(self):
7878
@property
7979
def frac_coords(self):
8080
"""The fractional coordinates of the kpoint as a numpy array."""
81-
return np.copy(self._fcoords)
81+
return np.copy(self._frac_coords)
8282

8383
@property
8484
def cart_coords(self):
8585
"""The Cartesian coordinates of the kpoint as a numpy array."""
86-
return np.copy(self._ccoords)
86+
return np.copy(self._cart_coords)
8787

8888
@property
8989
def a(self):
9090
"""Fractional a coordinate of the kpoint."""
91-
return self._fcoords[0]
91+
return self._frac_coords[0]
9292

9393
@property
9494
def b(self):
9595
"""Fractional b coordinate of the kpoint."""
96-
return self._fcoords[1]
96+
return self._frac_coords[1]
9797

9898
@property
9999
def c(self):
100100
"""Fractional c coordinate of the kpoint."""
101-
return self._fcoords[2]
101+
return self._frac_coords[2]
102102

103103
def __str__(self):
104104
"""Returns a string with fractional, Cartesian coordinates and label."""

tests/core/test_lattice.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,9 @@ def test_lll_basis(self):
459459
l1 = Lattice([a, b, c])
460460
l2 = Lattice([a + b, b + c, c])
461461

462-
ccoords = np.array([[1, 1, 2], [2, 2, 1.5]])
463-
l1_fcoords = l1.get_fractional_coords(ccoords)
464-
l2_fcoords = l2.get_fractional_coords(ccoords)
462+
cart_coords = np.array([[1, 1, 2], [2, 2, 1.5]])
463+
l1_fcoords = l1.get_fractional_coords(cart_coords)
464+
l2_fcoords = l2.get_fractional_coords(cart_coords)
465465

466466
assert_allclose(l1.matrix, l2.lll_matrix)
467467
assert_allclose(np.dot(l2.lll_mapping, l2.matrix), l1.matrix)

tests/core/test_surface.py

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,16 @@
2929
from pymatgen.util.testing import TEST_FILES_DIR, PymatgenTest
3030

3131

32-
def get_path(path_str):
33-
return os.path.join(TEST_FILES_DIR, "surface_tests", path_str)
34-
35-
3632
class TestSlab(PymatgenTest):
3733
def setUp(self):
38-
zno1 = Structure.from_file(get_path("ZnO-wz.cif"), primitive=False)
34+
zno1 = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/ZnO-wz.cif", primitive=False)
3935
zno55 = SlabGenerator(zno1, [1, 0, 0], 5, 5, lll_reduce=False, center_slab=False).get_slab()
4036

4137
Ti = Structure(
4238
Lattice.hexagonal(4.6, 2.82),
4339
["Ti", "Ti", "Ti"],
4440
[
45-
[0.000000, 0.000000, 0.000000],
41+
[0, 0, 0],
4642
[0.333333, 0.666667, 0.500000],
4743
[0.666667, 0.333333, 0.500000],
4844
],
@@ -52,10 +48,10 @@ def setUp(self):
5248
Lattice.cubic(4.06),
5349
["Ag", "Ag", "Ag", "Ag"],
5450
[
55-
[0.000000, 0.000000, 0.000000],
56-
[0.000000, 0.500000, 0.500000],
57-
[0.500000, 0.000000, 0.500000],
58-
[0.500000, 0.500000, 0.000000],
51+
[0, 0, 0],
52+
[0, 0.500000, 0.500000],
53+
[0.500000, 0, 0.500000],
54+
[0.500000, 0.500000, 0],
5955
],
6056
)
6157

@@ -68,7 +64,7 @@ def setUp(self):
6864
self.agfcc = Ag_fcc
6965
self.zno1 = zno1
7066
self.zno55 = zno55
71-
self.nonlaue = non_laue
67+
self.non_laue = non_laue
7268
self.h = Structure(Lattice.cubic(3), ["H"], [[0, 0, 0]])
7369
self.libcc = Structure(Lattice.cubic(3.51004), ["Li", "Li"], [[0, 0, 0], [0.5, 0.5, 0.5]])
7470

@@ -235,7 +231,7 @@ def test_symmetrization(self):
235231
assert symmetric_count == len(slabs)
236232

237233
# Check if we can generate symmetric slabs from bulk with no inversion
238-
all_non_laue_slabs = generate_all_slabs(self.nonlaue, 1, 15, 15, symmetrize=True)
234+
all_non_laue_slabs = generate_all_slabs(self.non_laue, 1, 15, 15, symmetrize=True)
239235
assert len(all_non_laue_slabs) > 0
240236

241237
def test_get_symmetric_sites(self):
@@ -393,26 +389,7 @@ def test_get_slab(self):
393389
and (
394390
sg.symbol.endswith("H")
395391
or sg.int_number
396-
in [
397-
143,
398-
144,
399-
145,
400-
147,
401-
149,
402-
150,
403-
151,
404-
152,
405-
153,
406-
154,
407-
156,
408-
157,
409-
158,
410-
159,
411-
162,
412-
163,
413-
164,
414-
165,
415-
]
392+
in [143, 144, 145, 147, 149, 150, 151, 152, 153, 154, 156, 157, 158, 159, 162, 163, 164, 165]
416393
)
417394
):
418395
latt = Lattice.hexagonal(5, 10)
@@ -484,7 +461,7 @@ def test_get_slabs(self):
484461
# slabs is of sites in LiFePO4 unit cell - 2 + 1.
485462
assert len(gen.get_slabs(tol=1e-4, ftol=1e-4)) == 15
486463

487-
LiCoO2 = Structure.from_file(get_path("icsd_LiCoO2.cif"), primitive=False)
464+
LiCoO2 = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/icsd_LiCoO2.cif", primitive=False)
488465
gen = SlabGenerator(LiCoO2, [0, 0, 1], 10, 10)
489466
lco = gen.get_slabs(bonds={("Co", "O"): 3})
490467
assert len(lco) == 1
@@ -518,14 +495,14 @@ def test_triclinic_TeI(self):
518495
# in other Miller indices can cause some ambiguity when choosing a
519496
# higher tolerance.
520497
numb_slabs = {(0, 0, 1): 5, (0, 1, 0): 3, (1, 0, 0): 7}
521-
TeI = Structure.from_file(get_path("icsd_TeI.cif"), primitive=False)
498+
TeI = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/icsd_TeI.cif", primitive=False)
522499
for k, v in numb_slabs.items():
523500
trclnc_TeI = SlabGenerator(TeI, k, 10, 10)
524501
TeI_slabs = trclnc_TeI.get_slabs()
525502
assert v == len(TeI_slabs)
526503

527504
def test_get_orthogonal_c_slab(self):
528-
TeI = Structure.from_file(get_path("icsd_TeI.cif"), primitive=False)
505+
TeI = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/icsd_TeI.cif", primitive=False)
529506
trclnc_TeI = SlabGenerator(TeI, (0, 0, 1), 10, 10)
530507
TeI_slabs = trclnc_TeI.get_slabs()
531508
slab = TeI_slabs[0]
@@ -534,7 +511,7 @@ def test_get_orthogonal_c_slab(self):
534511
assert norm_slab.lattice.angles[1] == approx(90)
535512

536513
def test_get_orthogonal_c_slab_site_props(self):
537-
TeI = Structure.from_file(get_path("icsd_TeI.cif"), primitive=False)
514+
TeI = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/icsd_TeI.cif", primitive=False)
538515
trclnc_TeI = SlabGenerator(TeI, (0, 0, 1), 10, 10)
539516
TeI_slabs = trclnc_TeI.get_slabs()
540517
slab = TeI_slabs[0]
@@ -719,7 +696,7 @@ def test_previous_reconstructions(self):
719696
el = self.Si[0].species_string
720697

721698
slabs = rec.build_slabs()
722-
struct = Structure.from_file(get_path(os.path.join("reconstructions", el + "_" + n + ".cif")))
699+
struct = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/reconstructions/{el}_{n}.cif")
723700
assert any(len(m.group_structures([struct, slab])) == 1 for slab in slabs)
724701

725702

@@ -730,8 +707,8 @@ def setUp(self):
730707
mglatt = Lattice.from_parameters(3.2, 3.2, 5.13, 90, 90, 120)
731708
self.Mg = Structure(mglatt, ["Mg", "Mg"], [[1 / 3, 2 / 3, 1 / 4], [2 / 3, 1 / 3, 3 / 4]])
732709
self.lifepo4 = self.get_structure("LiFePO4")
733-
self.tei = Structure.from_file(get_path("icsd_TeI.cif"), primitive=False)
734-
self.LiCoO2 = Structure.from_file(get_path("icsd_LiCoO2.cif"), primitive=False)
710+
self.tei = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/icsd_TeI.cif", primitive=False)
711+
self.LiCoO2 = Structure.from_file(f"{TEST_FILES_DIR}/surface_tests/icsd_LiCoO2.cif", primitive=False)
735712

736713
self.p1 = Structure(
737714
Lattice.from_parameters(3, 4, 5, 31, 43, 50),

tests/files/.pytest-split-durations

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@
15701570
"tests/io/exciting/test_inputs.py::TestExcitingInput::test_fromfile": 0.0033374580089002848,
15711571
"tests/io/exciting/test_inputs.py::TestExcitingInput::test_paramdict": 0.008810124010778964,
15721572
"tests/io/exciting/test_inputs.py::TestExcitingInput::test_writebandstr": 0.009053541987668723,
1573-
"tests/io/exciting/test_inputs.py::TestExcitingInput::test_writestring": 0.01415829302277416,
1573+
"tests/io/exciting/test_inputs.py::TestExcitingInput::test_write_string": 0.01415829302277416,
15741574
"tests/io/feff/test_inputs.py::TestFeffAtoms::test_absorber_line": 0.007966249017044902,
15751575
"tests/io/feff/test_inputs.py::TestFeffAtoms::test_absorbing_atom": 0.0037107079406268895,
15761576
"tests/io/feff/test_inputs.py::TestFeffAtoms::test_as_dict_and_from_dict": 0.025775165995582938,

tests/io/exciting/test_inputs.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ def test_fromfile(self):
2626
# Test for the import of a structure directly from an exciting
2727
# input file
2828
filepath = f"{TEST_FILES_DIR}/input_exciting1.xml"
29-
excin = ExcitingInput.from_file(filepath)
29+
exc_input = ExcitingInput.from_file(filepath)
3030
lattice = [[0.0, 2.81, 2.81], [2.81, 0.0, 2.81], [2.81, 2.81, 0.0]]
3131
atoms = ["Na", "Cl"]
32-
fraccoords = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]]
33-
assert_allclose(lattice, excin.structure.lattice.matrix.tolist())
34-
assert atoms == [site.specie.symbol for site in excin.structure]
35-
assert fraccoords == [site.frac_coords.tolist() for site in excin.structure]
32+
frac_coords = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]]
33+
assert_allclose(lattice, exc_input.structure.lattice.matrix.tolist())
34+
assert atoms == [site.specie.symbol for site in exc_input.structure]
35+
assert frac_coords == [site.frac_coords.tolist() for site in exc_input.structure]
3636

37-
def test_writestring(self):
38-
# Test for the string export of s atructure into the exciting input xml schema
37+
def test_write_string(self):
38+
# Test for the string export of structure into the exciting input xml schema
3939
input_string = (
4040
'<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
4141
'xsi:noNamespaceSchemaLocation="http://xml.exciting-code.org/excitinginput'

0 commit comments

Comments
 (0)