Skip to content

Commit

Permalink
rename variable names for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Mar 27, 2024
1 parent 87cd846 commit 1bf618d
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 140 deletions.
44 changes: 22 additions & 22 deletions tests/analysis/structure_prediction/test_volume_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,49 @@ def test_predict(self):
nacl = PymatgenTest.get_structure("CsCl")
nacl.replace_species({"Cs": "Na"})
nacl.scale_lattice(184.384551033)
p = RLSVolumePredictor(radii_type="ionic")
assert p.predict(struct, nacl) == approx(342.84905395082535)
p = RLSVolumePredictor(radii_type="atomic")
assert p.predict(struct, nacl) == approx(391.884366481)
predictor = RLSVolumePredictor(radii_type="ionic")
assert predictor.predict(struct, nacl) == approx(342.84905395082535)
predictor = RLSVolumePredictor(radii_type="atomic")
assert predictor.predict(struct, nacl) == approx(391.884366481)
lif = PymatgenTest.get_structure("CsCl")
lif.replace_species({"Cs": "Li", "Cl": "F"})
p = RLSVolumePredictor(radii_type="ionic")
assert p.predict(lif, nacl) == approx(74.268402413690467)
p = RLSVolumePredictor(radii_type="atomic")
assert p.predict(lif, nacl) == approx(62.2808125839)
predictor = RLSVolumePredictor(radii_type="ionic")
assert predictor.predict(lif, nacl) == approx(74.268402413690467)
predictor = RLSVolumePredictor(radii_type="atomic")
assert predictor.predict(lif, nacl) == approx(62.2808125839)

lfpo = PymatgenTest.get_structure("LiFePO4")
lmpo = PymatgenTest.get_structure("LiFePO4")
lmpo.replace_species({"Fe": "Mn"})
p = RLSVolumePredictor(radii_type="ionic")
assert p.predict(lmpo, lfpo) == approx(310.08253254420134)
p = RLSVolumePredictor(radii_type="atomic")
assert p.predict(lmpo, lfpo) == approx(299.607967711)
predictor = RLSVolumePredictor(radii_type="ionic")
assert predictor.predict(lmpo, lfpo) == approx(310.08253254420134)
predictor = RLSVolumePredictor(radii_type="atomic")
assert predictor.predict(lmpo, lfpo) == approx(299.607967711)

sto = PymatgenTest.get_structure("SrTiO3")
scoo = PymatgenTest.get_structure("SrTiO3")
scoo.replace_species({"Ti4+": "Co4+"})
p = RLSVolumePredictor(radii_type="ionic")
assert p.predict(scoo, sto) == approx(56.162534974936463)
p = RLSVolumePredictor(radii_type="atomic")
assert p.predict(scoo, sto) == approx(57.4777835108)
predictor = RLSVolumePredictor(radii_type="ionic")
assert predictor.predict(scoo, sto) == approx(56.162534974936463)
predictor = RLSVolumePredictor(radii_type="atomic")
assert predictor.predict(scoo, sto) == approx(57.4777835108)

# Use Ag7P3S11 as a test case:

# (i) no oxidation states are assigned and CVP-atomic scheme is selected.
aps = Structure.from_file(f"{module_dir}/Ag7P3S11_mp-683910_primitive.cif")
apo = Structure.from_file(f"{module_dir}/Ag7P3S11_mp-683910_primitive.cif")
apo.replace_species({"S": "O"})
p = RLSVolumePredictor(radii_type="atomic", check_isostructural=False)
assert p.predict(apo, aps) == approx(1196.31384276)
predictor = RLSVolumePredictor(radii_type="atomic", check_isostructural=False)
assert predictor.predict(apo, aps) == approx(1196.31384276)

# (ii) Oxidation states are assigned.
apo.add_oxidation_state_by_element({"Ag": 1, "P": 5, "O": -2})
aps.add_oxidation_state_by_element({"Ag": 1, "P": 5, "S": -2})
p = RLSVolumePredictor(radii_type="ionic")
assert p.predict(apo, aps) == approx(1165.23259079)
p = RLSVolumePredictor(radii_type="atomic")
assert p.predict(apo, aps) == approx(1196.31384276)
predictor = RLSVolumePredictor(radii_type="ionic")
assert predictor.predict(apo, aps) == approx(1165.23259079)
predictor = RLSVolumePredictor(radii_type="atomic")
assert predictor.predict(apo, aps) == approx(1196.31384276)

def test_modes(self):
cs_cl = PymatgenTest.get_structure("CsCl")
Expand Down
6 changes: 3 additions & 3 deletions tests/analysis/test_adsorption.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ def test_generate_adsorption_structures(self):

def test_adsorb_both_surfaces(self):
# Test out for monatomic adsorption
o = Molecule("O", [[0, 0, 0]])
ad_slabs = self.asf_100.adsorb_both_surfaces(o)
ad_slabs_one = self.asf_100.generate_adsorption_structures(o)
oxi = Molecule("O", [[0, 0, 0]])
ad_slabs = self.asf_100.adsorb_both_surfaces(oxi)
ad_slabs_one = self.asf_100.generate_adsorption_structures(oxi)
assert len(ad_slabs) == len(ad_slabs_one)
for ad_slab in ad_slabs:
sg = SpacegroupAnalyzer(ad_slab)
Expand Down
20 changes: 10 additions & 10 deletions tests/apps/battery/test_conversion_battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def test_init(self):

assert len(c.get_sub_electrodes(adjacent_only=True)) == c.num_steps
assert len(c.get_sub_electrodes(adjacent_only=False)) == sum(range(1, c.num_steps + 1))
p = self.expected_properties[formula]
props = self.expected_properties[formula]

for k, v in p.items():
for k, v in props.items():
assert getattr(c, f"get_{k}")() == approx(v, abs=1e-2)

assert {*c.get_summary_dict(print_subelectrodes=True)} == {
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_init(self):
# try to create an electrode from a dict and test methods
dct = c.as_dict()
electrode = ConversionElectrode.from_dict(dct)
for k, v in p.items():
for k, v in props.items():
assert getattr(electrode, "get_" + k)() == approx(v, abs=1e-2)

def test_repr(self):
Expand All @@ -121,22 +121,22 @@ def test_repr(self):

def test_summary(self):
key_map = {"specific_energy": "energy_grav", "energy_density": "energy_vol"}
for f in self.formulas:
c = self.conversion_electrodes[f]["CE"]
dct = c.get_summary_dict()
p = self.expected_properties[f]
for k, v in p.items():
for formula in self.formulas:
conv_elec = self.conversion_electrodes[formula]["CE"]
dct = conv_elec.get_summary_dict()
props = self.expected_properties[formula]
for k, v in props.items():
summary_key = key_map.get(k, k)
assert dct[summary_key] == approx(v, abs=1e-2)

def test_composite(self):
# check entries in charged/discharged state
for formula in self.formulas:
CE = self.conversion_electrodes[formula]["CE"]
for step, vpair in enumerate(CE.voltage_pairs):
for step, volt_pair in enumerate(CE.voltage_pairs):
# entries_charge/entries_discharge attributes should return entries equal with the expected
composite_dict = self.expected_composite[formula]
for attr in ["entries_charge", "entries_discharge"]:
# composite at each discharge step, of which entry object is simplified to reduced formula
entries_formula_list = [entry.reduced_formula for entry in getattr(vpair, attr)]
entries_formula_list = [entry.reduced_formula for entry in getattr(volt_pair, attr)]
assert entries_formula_list == composite_dict[attr][step]
8 changes: 4 additions & 4 deletions tests/core/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ def test_xyz(self):
# update PymatgenTest for unittest2?
# self.assertWarns(UserWarning, self.op.as_xyz_str)

o = SymmOp.from_xyz_str("0.5+x, 0.25+y, 0.75+z")
assert_allclose(o.translation_vector, [0.5, 0.25, 0.75])
o = SymmOp.from_xyz_str("x + 0.5, y + 0.25, z + 0.75")
assert_allclose(o.translation_vector, [0.5, 0.25, 0.75])
symm_op = SymmOp.from_xyz_str("0.5+x, 0.25+y, 0.75+z")
assert_allclose(symm_op.translation_vector, [0.5, 0.25, 0.75])
symm_op = SymmOp.from_xyz_str("x + 0.5, y + 0.25, z + 0.75")
assert_allclose(symm_op.translation_vector, [0.5, 0.25, 0.75])


class TestMagSymmOp(PymatgenTest):
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ def test_from_str(self):

def test_pickle(self):
el1 = DummySpecies("X", 3)
o = pickle.dumps(el1)
assert el1 == pickle.loads(o)
pickled = pickle.dumps(el1)
assert el1 == pickle.loads(pickled)

def test_sort(self):
r = sorted([Element.Fe, DummySpecies("X")])
Expand Down
18 changes: 9 additions & 9 deletions tests/core/test_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ def test_get_slabs(self):
gen = SlabGenerator(self.get_structure("CsCl"), [0, 0, 1], 10, 10)

# Test orthogonality of some internal variables.
a, b, _c = gen.oriented_unit_cell.lattice.matrix
assert np.dot(a, gen._normal) == approx(0)
a_len, b, _c = gen.oriented_unit_cell.lattice.matrix
assert np.dot(a_len, gen._normal) == approx(0)
assert np.dot(b, gen._normal) == approx(0)

assert len(gen.get_slabs()) == 1
Expand All @@ -458,8 +458,8 @@ def test_get_slabs(self):
gen = SlabGenerator(LiCoO2, [0, 0, 1], 10, 10)
lco = gen.get_slabs(bonds={("Co", "O"): 3})
assert len(lco) == 1
a, b, _c = gen.oriented_unit_cell.lattice.matrix
assert np.dot(a, gen._normal) == approx(0)
a_len, b, _c = gen.oriented_unit_cell.lattice.matrix
assert np.dot(a_len, gen._normal) == approx(0)
assert np.dot(b, gen._normal) == approx(0)

scc = Structure.from_spacegroup("Pm-3m", Lattice.cubic(3), ["Fe"], [[0, 0, 0]])
Expand All @@ -473,13 +473,13 @@ def test_get_slabs(self):
# Test whether using units of hkl planes instead of Angstroms for
# min_slab_size and min_vac_size will give us the same number of atoms
n_atoms = []
for a in [1, 1.4, 2.5, 3.6]:
struct = Structure.from_spacegroup("Im-3m", Lattice.cubic(a), ["Fe"], [[0, 0, 0]])
for a_len in [1, 1.4, 2.5, 3.6]:
struct = Structure.from_spacegroup("Im-3m", Lattice.cubic(a_len), ["Fe"], [[0, 0, 0]])
slab_gen = SlabGenerator(struct, (1, 1, 1), 10, 10, in_unit_planes=True, max_normal_search=2)
n_atoms.append(len(slab_gen.get_slab()))
n = n_atoms[0]
for i in n_atoms:
assert n == i
# Check if the number of atoms in all slabs is the same
for n_a in n_atoms:
assert n_atoms[0] == n_a

def test_triclinic_TeI(self):
# Test case for a triclinic structure of TeI. Only these three
Expand Down
6 changes: 3 additions & 3 deletions tests/core/test_tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,9 @@ def test_get_scaled(self):
assert self.non_symm.get_scaled(10) == approx(SquareTensor([[1, 2, 3], [4, 5, 6], [2, 5, 5]]))

def test_polar_decomposition(self):
u, p = self.rand_sqtensor.polar_decomposition()
assert_allclose(np.dot(u, p), self.rand_sqtensor)
assert_allclose(np.eye(3), np.dot(u, np.conjugate(np.transpose(u))), atol=1e-9)
u_mat, p_mat = self.rand_sqtensor.polar_decomposition()
assert_allclose(np.dot(u_mat, p_mat), self.rand_sqtensor)
assert_allclose(np.eye(3), np.dot(u_mat, np.conjugate(np.transpose(u_mat))), atol=1e-9)

def test_serialization(self):
# Test base serialize-deserialize
Expand Down
12 changes: 6 additions & 6 deletions tests/io/cp2k/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ def test_basis_info(self):

def test_potential_info(self):
# Ensure potential metadata can be read from string
p = PotentialInfo.from_str("GTH-PBE-q1-NLCC")
assert p.potential_type == "GTH"
assert p.xc == "PBE"
assert p.nlcc
pot_info = PotentialInfo.from_str("GTH-PBE-q1-NLCC")
assert pot_info.potential_type == "GTH"
assert pot_info.xc == "PBE"
assert pot_info.nlcc

# Ensure one-way softmatching works
p2 = PotentialInfo.from_str("GTH-q1-NLCC")
assert p2.softmatch(p)
assert not p.softmatch(p2)
assert p2.softmatch(pot_info)
assert not pot_info.softmatch(p2)

def test_basis(self):
# Ensure cp2k formatted string can be read for data correctly
Expand Down
72 changes: 36 additions & 36 deletions tests/io/test_adf.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def test_option_operations(self):

def test_atom_block_key(self):
block = AdfKey("atoms")
o = Molecule.from_str(h2o_xyz, "xyz")
for site in o:
mol = Molecule.from_str(h2o_xyz, "xyz")
for site in mol:
block.add_subkey(AdfKey(str(site.specie), list(site.coords)))
assert str(block) == atoms_string

Expand Down Expand Up @@ -206,14 +206,14 @@ def test_energy(self):

def test_serialization(self):
task = AdfTask()
o = AdfTask.from_dict(task.as_dict())
assert task.title == o.title
assert task.basis_set == o.basis_set
assert task.scf == o.scf
assert task.geo == o.geo
assert task.operation == o.operation
assert task.units == o.units
assert str(task) == str(o)
adf_task = AdfTask.from_dict(task.as_dict())
assert task.title == adf_task.title
assert task.basis_set == adf_task.basis_set
assert task.scf == adf_task.scf
assert task.geo == adf_task.geo
assert task.operation == adf_task.operation
assert task.units == adf_task.units
assert str(task) == str(adf_task)


rhb18 = {
Expand Down Expand Up @@ -261,35 +261,35 @@ def test_main(self):
class TestAdfOutput:
def test_analytical_freq(self):
filename = f"{TEST_DIR}/adf/analytical_freq/adf.out"
o = AdfOutput(filename)
assert o.final_energy == approx(-0.54340325)
assert len(o.energies) == 4
assert len(o.structures) == 4
assert o.frequencies[0] == approx(1553.931)
assert o.frequencies[2] == approx(3793.086)
assert o.normal_modes[0][2] == approx(0.071)
assert o.normal_modes[0][6] == approx(0.000)
assert o.normal_modes[0][7] == approx(-0.426)
assert o.normal_modes[0][8] == approx(-0.562)
adf_out = AdfOutput(filename)
assert adf_out.final_energy == approx(-0.54340325)
assert len(adf_out.energies) == 4
assert len(adf_out.structures) == 4
assert adf_out.frequencies[0] == approx(1553.931)
assert adf_out.frequencies[2] == approx(3793.086)
assert adf_out.normal_modes[0][2] == approx(0.071)
assert adf_out.normal_modes[0][6] == approx(0.000)
assert adf_out.normal_modes[0][7] == approx(-0.426)
assert adf_out.normal_modes[0][8] == approx(-0.562)

def test_numerical_freq(self):
filename = f"{TEST_DIR}/adf/numerical_freq/adf.out"
o = AdfOutput(filename)
assert o.freq_type == "Numerical"
assert len(o.final_structure) == 4
assert len(o.frequencies) == 6
assert len(o.normal_modes) == 6
assert o.frequencies[0] == approx(938.21)
assert o.frequencies[3] == approx(3426.64)
assert o.frequencies[4] == approx(3559.35)
assert o.frequencies[5] == approx(3559.35)
assert o.normal_modes[1][0] == approx(0.067)
assert o.normal_modes[1][3] == approx(-0.536)
assert o.normal_modes[1][7] == approx(0.000)
assert o.normal_modes[1][9] == approx(-0.536)
adf_out = AdfOutput(filename)
assert adf_out.freq_type == "Numerical"
assert len(adf_out.final_structure) == 4
assert len(adf_out.frequencies) == 6
assert len(adf_out.normal_modes) == 6
assert adf_out.frequencies[0] == approx(938.21)
assert adf_out.frequencies[3] == approx(3426.64)
assert adf_out.frequencies[4] == approx(3559.35)
assert adf_out.frequencies[5] == approx(3559.35)
assert adf_out.normal_modes[1][0] == approx(0.067)
assert adf_out.normal_modes[1][3] == approx(-0.536)
assert adf_out.normal_modes[1][7] == approx(0.000)
assert adf_out.normal_modes[1][9] == approx(-0.536)

def test_single_point(self):
filename = f"{TEST_DIR}/adf/sp/adf.out"
o = AdfOutput(filename)
assert o.final_energy == approx(-0.74399276)
assert len(o.final_structure) == 4
adf_out = AdfOutput(filename)
assert adf_out.final_energy == approx(-0.74399276)
assert len(adf_out.final_structure) == 4
8 changes: 4 additions & 4 deletions tests/io/test_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,15 @@ def test_symmetrized(self):

def test_disordered(self):
si = Element("Si")
n = Element("N")
nitrogen = Element("N")
coords = []
coords.extend((np.array([0, 0, 0]), np.array([0.75, 0.5, 0.75])))
lattice = [
[3.8401979337, 0.00, 0.00],
[1.9200989668, 3.3257101909, 0.00],
[0.00, -2.2171384943, 3.1355090603],
]
struct = Structure(lattice, [si, {si: 0.5, n: 0.5}], coords)
struct = Structure(lattice, [si, {si: 0.5, nitrogen: 0.5}], coords)
writer = CifWriter(struct)
answer = """# generated using pymatgen
data_Si1.5N0.5
Expand Down Expand Up @@ -554,15 +554,15 @@ def test_cif_writer_without_refinement(self):
def test_specie_cif_writer(self):
si4 = Species("Si", 4)
si3 = Species("Si", 3)
n = DummySpecies("X", -3)
dummy_spec = DummySpecies("X", -3)
coords = []
coords.extend((np.array([0.5, 0.5, 0.5]), np.array([0.75, 0.5, 0.75]), np.array([0, 0, 0])))
lattice = [
[3.8401979337, 0.00, 0.00],
[1.9200989668, 3.3257101909, 0.00],
[0.00, -2.2171384943, 3.1355090603],
]
struct = Structure(lattice, [n, {si3: 0.5, n: 0.5}, si4], coords)
struct = Structure(lattice, [dummy_spec, {si3: 0.5, dummy_spec: 0.5}, si4], coords)
writer = CifWriter(struct)
answer = """# generated using pymatgen
data_X1.5Si1.5
Expand Down

0 comments on commit 1bf618d

Please sign in to comment.