diff --git a/pymatgen/core/__init__.py b/pymatgen/core/__init__.py index e015ca10ce3..b5210639b71 100644 --- a/pymatgen/core/__init__.py +++ b/pymatgen/core/__init__.py @@ -10,6 +10,7 @@ import os import warnings +from typing import Any from ruamel.yaml import YAML @@ -32,13 +33,14 @@ OLD_SETTINGS_FILE = os.path.join(os.path.expanduser("~"), ".pmgrc.yaml") -def _load_pmg_settings() -> dict[str, str]: - settings = {} +def _load_pmg_settings() -> dict[str, Any]: + settings: dict[str, Any] = {} + # Load .pmgrc.yaml file yaml = YAML() try: with open(SETTINGS_FILE) as yml_file: - settings = yaml.load(yml_file) + settings = yaml.load(yml_file) or {} except FileNotFoundError: try: with open(OLD_SETTINGS_FILE) as yml_file: diff --git a/pymatgen/core/tests/test_settings.py b/pymatgen/core/tests/test_settings.py new file mode 100644 index 00000000000..4034aa064ca --- /dev/null +++ b/pymatgen/core/tests/test_settings.py @@ -0,0 +1,39 @@ +from pathlib import Path + +from pytest import MonkeyPatch + +from pymatgen.core import _load_pmg_settings + +__author__ = "Janosh Riebesell" +__date__ = "2022-10-21" +__email__ = "janosh@lbl.gov" + + +def test_load_settings(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: + """Test .pmgrc.yaml file is loaded correctly and env vars take precedence.""" + monkeypatch.setattr("os.environ", {}) # reset outer env vars + + settings_file = tmp_path / ".pmgrc.yaml" + monkeypatch.setattr("pymatgen.core.SETTINGS_FILE", settings_file) + + # should return empty dict if file doesn't exist + assert _load_pmg_settings() == {} + + # should return empty dict if file is empty + settings_file.write_text("") + assert _load_pmg_settings() == {} + + settings_file.write_text("PMG_VASP_PSP_DIR: /path/to/psp") + assert _load_pmg_settings() == {"PMG_VASP_PSP_DIR": "/path/to/psp"} + + settings_file.write_text("PMG_MAPI_KEY: FOOBAR") + assert _load_pmg_settings() == {"PMG_MAPI_KEY": "FOOBAR"} + + # env vars should override .pmgrc.yaml + with monkeypatch.context() as ctx: + ctx.setenv("PMG_MAPI_KEY", "BAZ") + assert _load_pmg_settings() == {"PMG_MAPI_KEY": "BAZ"} + + # should return empty dict if file is invalid + settings_file.write_text("---") + assert _load_pmg_settings() == {} diff --git a/pymatgen/symmetry/tests/test_analyzer.py b/pymatgen/symmetry/tests/test_analyzer.py index a39b6930daa..2664c5048cf 100644 --- a/pymatgen/symmetry/tests/test_analyzer.py +++ b/pymatgen/symmetry/tests/test_analyzer.py @@ -6,6 +6,7 @@ import unittest import numpy as np +import pytest from pymatgen.core.sites import PeriodicSite from pymatgen.core.structure import Molecule, Structure @@ -43,8 +44,8 @@ def setUp(self): def test_primitive(self): s = Structure.from_spacegroup("Fm-3m", np.eye(3) * 3, ["Cu"], [[0, 0, 0]]) a = SpacegroupAnalyzer(s) - self.assertEqual(len(s), 4) - self.assertEqual(len(a.find_primitive()), 1) + assert len(s) == 4 + assert len(a.find_primitive()) == 1 def test_is_laue(self): struct = Structure.from_spacegroup("Fm-3m", np.eye(3) * 3, ["Cu"], [[0, 0, 0]]) @@ -57,7 +58,7 @@ def test_is_laue(self): def test_magnetic(self): lfp = PymatgenTest.get_structure("LiFePO4") sg = SpacegroupAnalyzer(lfp, 0.1) - self.assertEqual(sg.get_space_group_symbol(), "Pnma") + assert sg.get_space_group_symbol() == "Pnma" magmoms = [0] * len(lfp) magmoms[4] = 1 magmoms[5] = -1 @@ -65,26 +66,26 @@ def test_magnetic(self): magmoms[7] = -1 lfp.add_site_property("magmom", magmoms) sg = SpacegroupAnalyzer(lfp, 0.1) - self.assertEqual(sg.get_space_group_symbol(), "Pnma") + assert sg.get_space_group_symbol() == "Pnma" def test_get_space_symbol(self): - self.assertEqual(self.sg.get_space_group_symbol(), "Pnma") - self.assertEqual(self.disordered_sg.get_space_group_symbol(), "P4_2/nmc") - self.assertEqual(self.sg3.get_space_group_symbol(), "Pnma") - self.assertEqual(self.sg4.get_space_group_symbol(), "P6_3/mmc") + assert self.sg.get_space_group_symbol() == "Pnma" + assert self.disordered_sg.get_space_group_symbol() == "P4_2/nmc" + assert self.sg3.get_space_group_symbol() == "Pnma" + assert self.sg4.get_space_group_symbol() == "P6_3/mmc" def test_get_space_number(self): - self.assertEqual(self.sg.get_space_group_number(), 62) - self.assertEqual(self.disordered_sg.get_space_group_number(), 137) - self.assertEqual(self.sg4.get_space_group_number(), 194) + assert self.sg.get_space_group_number() == 62 + assert self.disordered_sg.get_space_group_number() == 137 + assert self.sg4.get_space_group_number() == 194 def test_get_hall(self): - self.assertEqual(self.sg.get_hall(), "-P 2ac 2n") - self.assertEqual(self.disordered_sg.get_hall(), "P 4n 2n -1n") + assert self.sg.get_hall() == "-P 2ac 2n" + assert self.disordered_sg.get_hall() == "P 4n 2n -1n" def test_get_pointgroup(self): - self.assertEqual(self.sg.get_point_group_symbol(), "mmm") - self.assertEqual(self.disordered_sg.get_point_group_symbol(), "4/mmm") + assert self.sg.get_point_group_symbol() == "mmm" + assert self.disordered_sg.get_point_group_symbol() == "4/mmm" def test_get_symmetry_operations(self): @@ -102,14 +103,14 @@ def test_get_symmetry_operations(self): for site in structure: new_frac = fop.operate(site.frac_coords) new_cart = op.operate(site.coords) - self.assertTrue(np.allclose(structure.lattice.get_fractional_coords(new_cart), new_frac)) + assert np.allclose(structure.lattice.get_fractional_coords(new_cart), new_frac) found = False newsite = PeriodicSite(site.species, new_cart, structure.lattice, coords_are_cartesian=True) for testsite in structure: if newsite.is_periodic_image(testsite, 1e-3): found = True break - self.assertTrue(found) + assert found # Make sure this works for any position, not just the atomic # ones. @@ -117,69 +118,69 @@ def test_get_symmetry_operations(self): random_ccoord = structure.lattice.get_cartesian_coords(random_fcoord) new_frac = fop.operate(random_fcoord) new_cart = op.operate(random_ccoord) - self.assertTrue(np.allclose(structure.lattice.get_fractional_coords(new_cart), new_frac)) + assert np.allclose(structure.lattice.get_fractional_coords(new_cart), new_frac) def test_get_symmetry_dataset(self): ds = self.sg.get_symmetry_dataset() - self.assertEqual(ds["international"], "Pnma") + assert ds["international"] == "Pnma" def test_get_crystal_system(self): crystal_system = self.sg.get_crystal_system() - self.assertEqual("orthorhombic", crystal_system) - self.assertEqual("tetragonal", self.disordered_sg.get_crystal_system()) + assert "orthorhombic" == crystal_system + assert "tetragonal" == self.disordered_sg.get_crystal_system() orig_spg = self.sg._space_group_data["number"] self.sg._space_group_data["number"] = 0 try: crystal_system = self.sg.get_crystal_system() except ValueError as exc: - self.assertEqual(str(exc), "Received invalid space group 0") + assert str(exc) == "Received invalid space group 0" finally: self.sg._space_group_data["number"] = orig_spg def test_get_refined_structure(self): for a in self.sg.get_refined_structure().lattice.angles: - self.assertEqual(a, 90) + assert a == 90 refined = self.disordered_sg.get_refined_structure() for a in refined.lattice.angles: - self.assertEqual(a, 90) - self.assertEqual(refined.lattice.a, refined.lattice.b) + assert a == 90 + assert refined.lattice.a == refined.lattice.b structure = self.get_structure("Li2O") structure.add_site_property("magmom", [1.0] * len(structure)) sg = SpacegroupAnalyzer(structure, 0.01) refined_struct = sg.get_refined_structure(keep_site_properties=True) - self.assertEqual(refined_struct.site_properties["magmom"], [1.0] * len(refined_struct)) + assert refined_struct.site_properties["magmom"] == [1.0] * len(refined_struct) structure = self.get_structure("Li2O") structure.add_site_property("magmom", [1.0] * len(structure)) sg = SpacegroupAnalyzer(structure, 0.01) refined_struct = sg.get_refined_structure(keep_site_properties=False) - self.assertEqual(refined_struct.site_properties.get("magmom", None), None) + assert refined_struct.site_properties.get("magmom", None) is None def test_get_symmetrized_structure(self): symm_struct = self.sg.get_symmetrized_structure() for a in symm_struct.lattice.angles: - self.assertEqual(a, 90) - self.assertEqual(len(symm_struct.equivalent_sites), 5) + assert a == 90 + assert len(symm_struct.equivalent_sites) == 5 symm_struct = self.disordered_sg.get_symmetrized_structure() - self.assertEqual(len(symm_struct.equivalent_sites), 8) - self.assertEqual([len(i) for i in symm_struct.equivalent_sites], [16, 4, 8, 4, 2, 8, 8, 8]) + assert len(symm_struct.equivalent_sites) == 8 + assert [len(i) for i in symm_struct.equivalent_sites] == [16, 4, 8, 4, 2, 8, 8, 8] s1 = symm_struct.equivalent_sites[1][1] s2 = symm_struct[symm_struct.equivalent_indices[1][1]] - self.assertEqual(s1, s2) - self.assertEqual(self.sg4.get_symmetrized_structure()[0].magmom, 0.1) - self.assertEqual(symm_struct.wyckoff_symbols[0], "16h") + assert s1 == s2 + assert self.sg4.get_symmetrized_structure()[0].magmom == 0.1 + assert symm_struct.wyckoff_symbols[0] == "16h" # Check copying - self.assertEqual(symm_struct.copy(), symm_struct) + assert symm_struct.copy() == symm_struct d = symm_struct.as_dict() from pymatgen.symmetry.structure import SymmetrizedStructure ss = SymmetrizedStructure.from_dict(d) - self.assertEqual(ss.wyckoff_symbols[0], "16h") - self.assertIn("SymmetrizedStructure", str(ss)) + assert ss.wyckoff_symbols[0] == "16h" + assert "SymmetrizedStructure" in str(ss) def test_find_primitive(self): """F m -3 m Li2O testing of converting to primitive cell.""" @@ -187,245 +188,245 @@ def test_find_primitive(self): structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure) primitive_structure = s.find_primitive() - self.assertEqual(primitive_structure.formula, "Li2 O1") - self.assertTrue(primitive_structure.site_properties.get("magmom", None) is None) + assert primitive_structure.formula == "Li2 O1" + assert primitive_structure.site_properties.get("magmom", None) is None # This isn't what is expected. All the angles should be 60 - self.assertAlmostEqual(primitive_structure.lattice.alpha, 60) - self.assertAlmostEqual(primitive_structure.lattice.beta, 60) - self.assertAlmostEqual(primitive_structure.lattice.gamma, 60) - self.assertAlmostEqual(primitive_structure.lattice.volume, structure.lattice.volume / 4.0) + assert primitive_structure.lattice.alpha == pytest.approx(60) + assert primitive_structure.lattice.beta == pytest.approx(60) + assert primitive_structure.lattice.gamma == pytest.approx(60) + assert primitive_structure.lattice.volume == pytest.approx(structure.lattice.volume / 4.0) structure = parser.get_structures(False)[0] structure.add_site_property("magmom", [1.0] * len(structure)) s = SpacegroupAnalyzer(structure) primitive_structure = s.find_primitive(keep_site_properties=True) - self.assertEqual(primitive_structure.site_properties["magmom"], [1.0] * len(primitive_structure)) + assert primitive_structure.site_properties["magmom"] == [1.0] * len(primitive_structure) structure = parser.get_structures(False)[0] structure.add_site_property("magmom", [1.0] * len(structure)) s = SpacegroupAnalyzer(structure) primitive_structure = s.find_primitive(keep_site_properties=False) - self.assertEqual(primitive_structure.site_properties.get("magmom", None), None) + assert primitive_structure.site_properties.get("magmom", None) is None def test_get_ir_reciprocal_mesh(self): grid = self.sg.get_ir_reciprocal_mesh() - self.assertEqual(len(grid), 216) - self.assertAlmostEqual(grid[1][0][0], 0.1) - self.assertAlmostEqual(grid[1][0][1], 0.0) - self.assertAlmostEqual(grid[1][0][2], 0.0) - self.assertAlmostEqual(grid[1][1], 2) + assert len(grid) == 216 + assert grid[1][0][0] == pytest.approx(0.1) + assert grid[1][0][1] == pytest.approx(0.0) + assert grid[1][0][2] == pytest.approx(0.0) + assert grid[1][1] == pytest.approx(2) def test_get_ir_reciprocal_mesh_map(self): mesh = (6, 6, 6) grid = self.sg.get_ir_reciprocal_mesh(mesh=mesh) full_grid, mapping = self.sg.get_ir_reciprocal_mesh_map(mesh=mesh) - self.assertEqual(len(np.unique(mapping)), len(grid)) + assert len(np.unique(mapping)) == len(grid) for _, i in enumerate(np.unique(mapping)): - self.assertAlmostEqual(full_grid[i][0], grid[_][0][0]) - self.assertAlmostEqual(full_grid[i][1], grid[_][0][1]) - self.assertAlmostEqual(full_grid[i][2], grid[_][0][2]) + assert full_grid[i][0] == pytest.approx(grid[_][0][0]) + assert full_grid[i][1] == pytest.approx(grid[_][0][1]) + assert full_grid[i][2] == pytest.approx(grid[_][0][2]) def test_get_conventional_standard_structure(self): parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "bcc_1927.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 90) - self.assertAlmostEqual(conv.lattice.beta, 90) - self.assertAlmostEqual(conv.lattice.gamma, 90) - self.assertAlmostEqual(conv.lattice.a, 9.1980270633769461) - self.assertAlmostEqual(conv.lattice.b, 9.1980270633769461) - self.assertAlmostEqual(conv.lattice.c, 9.1980270633769461) + assert conv.lattice.alpha == pytest.approx(90) + assert conv.lattice.beta == pytest.approx(90) + assert conv.lattice.gamma == pytest.approx(90) + assert conv.lattice.a == pytest.approx(9.1980270633769461) + assert conv.lattice.b == pytest.approx(9.1980270633769461) + assert conv.lattice.c == pytest.approx(9.1980270633769461) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "btet_1915.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 90) - self.assertAlmostEqual(conv.lattice.beta, 90) - self.assertAlmostEqual(conv.lattice.gamma, 90) - self.assertAlmostEqual(conv.lattice.a, 5.0615106678044235) - self.assertAlmostEqual(conv.lattice.b, 5.0615106678044235) - self.assertAlmostEqual(conv.lattice.c, 4.2327080177761687) + assert conv.lattice.alpha == pytest.approx(90) + assert conv.lattice.beta == pytest.approx(90) + assert conv.lattice.gamma == pytest.approx(90) + assert conv.lattice.a == pytest.approx(5.0615106678044235) + assert conv.lattice.b == pytest.approx(5.0615106678044235) + assert conv.lattice.c == pytest.approx(4.2327080177761687) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "orci_1010.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 90) - self.assertAlmostEqual(conv.lattice.beta, 90) - self.assertAlmostEqual(conv.lattice.gamma, 90) - self.assertAlmostEqual(conv.lattice.a, 2.9542233922299999) - self.assertAlmostEqual(conv.lattice.b, 4.6330325651443296) - self.assertAlmostEqual(conv.lattice.c, 5.373703587040775) + assert conv.lattice.alpha == pytest.approx(90) + assert conv.lattice.beta == pytest.approx(90) + assert conv.lattice.gamma == pytest.approx(90) + assert conv.lattice.a == pytest.approx(2.9542233922299999) + assert conv.lattice.b == pytest.approx(4.6330325651443296) + assert conv.lattice.c == pytest.approx(5.373703587040775) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "orcc_1003.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 90) - self.assertAlmostEqual(conv.lattice.beta, 90) - self.assertAlmostEqual(conv.lattice.gamma, 90) - self.assertAlmostEqual(conv.lattice.a, 4.1430033493799998) - self.assertAlmostEqual(conv.lattice.b, 31.437979757624728) - self.assertAlmostEqual(conv.lattice.c, 3.99648651) + assert conv.lattice.alpha == pytest.approx(90) + assert conv.lattice.beta == pytest.approx(90) + assert conv.lattice.gamma == pytest.approx(90) + assert conv.lattice.a == pytest.approx(4.1430033493799998) + assert conv.lattice.b == pytest.approx(31.437979757624728) + assert conv.lattice.c == pytest.approx(3.99648651) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "orac_632475.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 90) - self.assertAlmostEqual(conv.lattice.beta, 90) - self.assertAlmostEqual(conv.lattice.gamma, 90) - self.assertAlmostEqual(conv.lattice.a, 3.1790663399999999) - self.assertAlmostEqual(conv.lattice.b, 9.9032878699999998) - self.assertAlmostEqual(conv.lattice.c, 3.5372412099999999) + assert conv.lattice.alpha == pytest.approx(90) + assert conv.lattice.beta == pytest.approx(90) + assert conv.lattice.gamma == pytest.approx(90) + assert conv.lattice.a == pytest.approx(3.1790663399999999) + assert conv.lattice.b == pytest.approx(9.9032878699999998) + assert conv.lattice.c == pytest.approx(3.5372412099999999) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "monoc_1028.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 90) - self.assertAlmostEqual(conv.lattice.beta, 117.53832420192903) - self.assertAlmostEqual(conv.lattice.gamma, 90) - self.assertAlmostEqual(conv.lattice.a, 14.033435583000625) - self.assertAlmostEqual(conv.lattice.b, 3.96052850731) - self.assertAlmostEqual(conv.lattice.c, 6.8743926325200002) + assert conv.lattice.alpha == pytest.approx(90) + assert conv.lattice.beta == pytest.approx(117.53832420192903) + assert conv.lattice.gamma == pytest.approx(90) + assert conv.lattice.a == pytest.approx(14.033435583000625) + assert conv.lattice.b == pytest.approx(3.96052850731) + assert conv.lattice.c == pytest.approx(6.8743926325200002) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "hex_1170.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 90) - self.assertAlmostEqual(conv.lattice.beta, 90) - self.assertAlmostEqual(conv.lattice.gamma, 120) - self.assertAlmostEqual(conv.lattice.a, 3.699919902005897) - self.assertAlmostEqual(conv.lattice.b, 3.699919902005897) - self.assertAlmostEqual(conv.lattice.c, 6.9779585500000003) + assert conv.lattice.alpha == pytest.approx(90) + assert conv.lattice.beta == pytest.approx(90) + assert conv.lattice.gamma == pytest.approx(120) + assert conv.lattice.a == pytest.approx(3.699919902005897) + assert conv.lattice.b == pytest.approx(3.699919902005897) + assert conv.lattice.c == pytest.approx(6.9779585500000003) structure = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "tric_684654.json")) s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure() - self.assertAlmostEqual(conv.lattice.alpha, 74.09581916308757) - self.assertAlmostEqual(conv.lattice.beta, 75.72817279281173) - self.assertAlmostEqual(conv.lattice.gamma, 63.63234318667333) - self.assertAlmostEqual(conv.lattice.a, 3.741372924048738) - self.assertAlmostEqual(conv.lattice.b, 3.9883228679270686) - self.assertAlmostEqual(conv.lattice.c, 7.288495840048958) + assert conv.lattice.alpha == pytest.approx(74.09581916308757) + assert conv.lattice.beta == pytest.approx(75.72817279281173) + assert conv.lattice.gamma == pytest.approx(63.63234318667333) + assert conv.lattice.a == pytest.approx(3.741372924048738) + assert conv.lattice.b == pytest.approx(3.9883228679270686) + assert conv.lattice.c == pytest.approx(7.288495840048958) structure = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "tric_684654.json")) structure.add_site_property("magmom", [1.0] * len(structure)) s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure(keep_site_properties=True) - self.assertEqual(conv.site_properties["magmom"], [1.0] * len(conv)) + assert conv.site_properties["magmom"] == [1.0] * len(conv) structure = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "tric_684654.json")) structure.add_site_property("magmom", [1.0] * len(structure)) s = SpacegroupAnalyzer(structure, symprec=1e-2) conv = s.get_conventional_standard_structure(keep_site_properties=False) - self.assertEqual(conv.site_properties.get("magmom", None), None) + assert conv.site_properties.get("magmom", None) is None def test_get_primitive_standard_structure(self): parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "bcc_1927.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 109.47122063400001) - self.assertAlmostEqual(prim.lattice.beta, 109.47122063400001) - self.assertAlmostEqual(prim.lattice.gamma, 109.47122063400001) - self.assertAlmostEqual(prim.lattice.a, 7.9657251015812145) - self.assertAlmostEqual(prim.lattice.b, 7.9657251015812145) - self.assertAlmostEqual(prim.lattice.c, 7.9657251015812145) + assert prim.lattice.alpha == pytest.approx(109.47122063400001) + assert prim.lattice.beta == pytest.approx(109.47122063400001) + assert prim.lattice.gamma == pytest.approx(109.47122063400001) + assert prim.lattice.a == pytest.approx(7.9657251015812145) + assert prim.lattice.b == pytest.approx(7.9657251015812145) + assert prim.lattice.c == pytest.approx(7.9657251015812145) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "btet_1915.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 105.015053349) - self.assertAlmostEqual(prim.lattice.beta, 105.015053349) - self.assertAlmostEqual(prim.lattice.gamma, 118.80658411899999) - self.assertAlmostEqual(prim.lattice.a, 4.1579321075608791) - self.assertAlmostEqual(prim.lattice.b, 4.1579321075608791) - self.assertAlmostEqual(prim.lattice.c, 4.1579321075608791) + assert prim.lattice.alpha == pytest.approx(105.015053349) + assert prim.lattice.beta == pytest.approx(105.015053349) + assert prim.lattice.gamma == pytest.approx(118.80658411899999) + assert prim.lattice.a == pytest.approx(4.1579321075608791) + assert prim.lattice.b == pytest.approx(4.1579321075608791) + assert prim.lattice.c == pytest.approx(4.1579321075608791) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "orci_1010.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 134.78923546600001) - self.assertAlmostEqual(prim.lattice.beta, 105.856239333) - self.assertAlmostEqual(prim.lattice.gamma, 91.276341676000001) - self.assertAlmostEqual(prim.lattice.a, 3.8428217771014852) - self.assertAlmostEqual(prim.lattice.b, 3.8428217771014852) - self.assertAlmostEqual(prim.lattice.c, 3.8428217771014852) + assert prim.lattice.alpha == pytest.approx(134.78923546600001) + assert prim.lattice.beta == pytest.approx(105.856239333) + assert prim.lattice.gamma == pytest.approx(91.276341676000001) + assert prim.lattice.a == pytest.approx(3.8428217771014852) + assert prim.lattice.b == pytest.approx(3.8428217771014852) + assert prim.lattice.c == pytest.approx(3.8428217771014852) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "orcc_1003.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 90) - self.assertAlmostEqual(prim.lattice.beta, 90) - self.assertAlmostEqual(prim.lattice.gamma, 164.985257335) - self.assertAlmostEqual(prim.lattice.a, 15.854897098324196) - self.assertAlmostEqual(prim.lattice.b, 15.854897098324196) - self.assertAlmostEqual(prim.lattice.c, 3.99648651) + assert prim.lattice.alpha == pytest.approx(90) + assert prim.lattice.beta == pytest.approx(90) + assert prim.lattice.gamma == pytest.approx(164.985257335) + assert prim.lattice.a == pytest.approx(15.854897098324196) + assert prim.lattice.b == pytest.approx(15.854897098324196) + assert prim.lattice.c == pytest.approx(3.99648651) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "orac_632475.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 90) - self.assertAlmostEqual(prim.lattice.beta, 90) - self.assertAlmostEqual(prim.lattice.gamma, 144.40557588533386) - self.assertAlmostEqual(prim.lattice.a, 5.2005185662155391) - self.assertAlmostEqual(prim.lattice.b, 5.2005185662155391) - self.assertAlmostEqual(prim.lattice.c, 3.5372412099999999) + assert prim.lattice.alpha == pytest.approx(90) + assert prim.lattice.beta == pytest.approx(90) + assert prim.lattice.gamma == pytest.approx(144.40557588533386) + assert prim.lattice.a == pytest.approx(5.2005185662155391) + assert prim.lattice.b == pytest.approx(5.2005185662155391) + assert prim.lattice.c == pytest.approx(3.5372412099999999) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "monoc_1028.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 63.579155761999999) - self.assertAlmostEqual(prim.lattice.beta, 116.42084423747779) - self.assertAlmostEqual(prim.lattice.gamma, 148.47965136208569) - self.assertAlmostEqual(prim.lattice.a, 7.2908007159612325) - self.assertAlmostEqual(prim.lattice.b, 7.2908007159612325) - self.assertAlmostEqual(prim.lattice.c, 6.8743926325200002) + assert prim.lattice.alpha == pytest.approx(63.579155761999999) + assert prim.lattice.beta == pytest.approx(116.42084423747779) + assert prim.lattice.gamma == pytest.approx(148.47965136208569) + assert prim.lattice.a == pytest.approx(7.2908007159612325) + assert prim.lattice.b == pytest.approx(7.2908007159612325) + assert prim.lattice.c == pytest.approx(6.8743926325200002) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "hex_1170.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 90) - self.assertAlmostEqual(prim.lattice.beta, 90) - self.assertAlmostEqual(prim.lattice.gamma, 120) - self.assertAlmostEqual(prim.lattice.a, 3.699919902005897) - self.assertAlmostEqual(prim.lattice.b, 3.699919902005897) - self.assertAlmostEqual(prim.lattice.c, 6.9779585500000003) + assert prim.lattice.alpha == pytest.approx(90) + assert prim.lattice.beta == pytest.approx(90) + assert prim.lattice.gamma == pytest.approx(120) + assert prim.lattice.a == pytest.approx(3.699919902005897) + assert prim.lattice.b == pytest.approx(3.699919902005897) + assert prim.lattice.c == pytest.approx(6.9779585500000003) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "rhomb_3478_conv.cif")) structure = parser.get_structures(False)[0] s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure() - self.assertAlmostEqual(prim.lattice.alpha, 28.049186140546812) - self.assertAlmostEqual(prim.lattice.beta, 28.049186140546812) - self.assertAlmostEqual(prim.lattice.gamma, 28.049186140546812) - self.assertAlmostEqual(prim.lattice.a, 5.9352627428399982) - self.assertAlmostEqual(prim.lattice.b, 5.9352627428399982) - self.assertAlmostEqual(prim.lattice.c, 5.9352627428399982) + assert prim.lattice.alpha == pytest.approx(28.049186140546812) + assert prim.lattice.beta == pytest.approx(28.049186140546812) + assert prim.lattice.gamma == pytest.approx(28.049186140546812) + assert prim.lattice.a == pytest.approx(5.9352627428399982) + assert prim.lattice.b == pytest.approx(5.9352627428399982) + assert prim.lattice.c == pytest.approx(5.9352627428399982) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "rhomb_3478_conv.cif")) structure = parser.get_structures(False)[0] structure.add_site_property("magmom", [1.0] * len(structure)) s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure(keep_site_properties=True) - self.assertEqual(prim.site_properties["magmom"], [1.0] * len(prim)) + assert prim.site_properties["magmom"] == [1.0] * len(prim) parser = CifParser(os.path.join(PymatgenTest.TEST_FILES_DIR, "rhomb_3478_conv.cif")) structure = parser.get_structures(False)[0] structure.add_site_property("magmom", [1.0] * len(structure)) s = SpacegroupAnalyzer(structure, symprec=1e-2) prim = s.get_primitive_standard_structure(keep_site_properties=False) - self.assertEqual(prim.site_properties.get("magmom", None), None) + assert prim.site_properties.get("magmom", None) is None def test_tricky_structure(self): # for some reason this structure kills spglib1.9 @@ -448,11 +449,11 @@ def setUp(self): def test_are_symmetrically_equivalent(self): sites1 = [self.structure[i] for i in [0, 1]] sites2 = [self.structure[i] for i in [2, 3]] - self.assertTrue(self.sg1.are_symmetrically_equivalent(sites1, sites2, 1e-3)) + assert self.sg1.are_symmetrically_equivalent(sites1, sites2, 1e-3) sites1 = [self.structure[i] for i in [0, 1]] sites2 = [self.structure[i] for i in [0, 2]] - self.assertFalse(self.sg1.are_symmetrically_equivalent(sites1, sites2, 1e-3)) + assert not self.sg1.are_symmetrically_equivalent(sites1, sites2, 1e-3) H2O2 = Molecule( @@ -536,17 +537,17 @@ def test_are_symmetrically_equivalent(self): class PointGroupAnalyzerTest(PymatgenTest): def test_spherical(self): a = PointGroupAnalyzer(CH4) - self.assertEqual(a.sch_symbol, "Td") - self.assertEqual(len(a.get_pointgroup()), 24) - self.assertEqual(a.get_rotational_symmetry_number(), 12) + assert a.sch_symbol == "Td" + assert len(a.get_pointgroup()) == 24 + assert a.get_rotational_symmetry_number() == 12 a = PointGroupAnalyzer(H2O) - self.assertEqual(a.get_rotational_symmetry_number(), 2) + assert a.get_rotational_symmetry_number() == 2 a = PointGroupAnalyzer(PF6) - self.assertEqual(a.sch_symbol, "Oh") - self.assertEqual(len(a.get_pointgroup()), 48) + assert a.sch_symbol == "Oh" + assert len(a.get_pointgroup()) == 48 m = Molecule.from_file(os.path.join(test_dir_mol, "c60.xyz")) a = PointGroupAnalyzer(m) - self.assertEqual(a.sch_symbol, "Ih") + assert a.sch_symbol == "Ih" cube_species = ["C", "C", "C", "C", "C", "C", "C", "C"] cube_coords = [ @@ -562,12 +563,12 @@ def test_spherical(self): m = Molecule(cube_species, cube_coords) a = PointGroupAnalyzer(m, 0.1) - self.assertEqual(a.sch_symbol, "Oh") + assert a.sch_symbol == "Oh" def test_tricky(self): m = Molecule.from_file(os.path.join(test_dir_mol, "dh.xyz")) a = PointGroupAnalyzer(m, 0.1) - self.assertEqual(a.sch_symbol, "D*h") + assert a.sch_symbol == "D*h" def test_linear(self): coords = [ @@ -577,10 +578,10 @@ def test_linear(self): ] mol = Molecule(["C", "H", "H"], coords) a = PointGroupAnalyzer(mol) - self.assertEqual(a.sch_symbol, "D*h") + assert a.sch_symbol == "D*h" mol = Molecule(["C", "H", "N"], coords) a = PointGroupAnalyzer(mol) - self.assertEqual(a.sch_symbol, "C*v") + assert a.sch_symbol == "C*v" def test_asym_top(self): coords = [ @@ -593,8 +594,8 @@ def test_asym_top(self): mol = Molecule(["C", "H", "F", "Br", "Cl"], coords) a = PointGroupAnalyzer(mol) - self.assertEqual(a.sch_symbol, "C1") - self.assertEqual(len(a.get_pointgroup()), 1) + assert a.sch_symbol == "C1" + assert len(a.get_pointgroup()) == 1 coords = [ [0.000000, 0.000000, 1.08], [1.026719, 0.000000, -0.363000], @@ -603,36 +604,36 @@ def test_asym_top(self): ] cs_mol = Molecule(["H", "F", "Cl", "Cl"], coords) a = PointGroupAnalyzer(cs_mol) - self.assertEqual(a.sch_symbol, "Cs") - self.assertEqual(len(a.get_pointgroup()), 2) + assert a.sch_symbol == "Cs" + assert len(a.get_pointgroup()) == 2 a = PointGroupAnalyzer(C2H2F2Br2) - self.assertEqual(a.sch_symbol, "Ci") - self.assertEqual(len(a.get_pointgroup()), 2) + assert a.sch_symbol == "Ci" + assert len(a.get_pointgroup()) == 2 def test_cyclic(self): a = PointGroupAnalyzer(H2O2) - self.assertEqual(a.sch_symbol, "C2") - self.assertEqual(len(a.get_pointgroup()), 2) + assert a.sch_symbol == "C2" + assert len(a.get_pointgroup()) == 2 a = PointGroupAnalyzer(H2O) - self.assertEqual(a.sch_symbol, "C2v") - self.assertEqual(len(a.get_pointgroup()), 4) + assert a.sch_symbol == "C2v" + assert len(a.get_pointgroup()) == 4 a = PointGroupAnalyzer(NH3) - self.assertEqual(a.sch_symbol, "C3v") - self.assertEqual(len(a.get_pointgroup()), 6) + assert a.sch_symbol == "C3v" + assert len(a.get_pointgroup()) == 6 cs2 = Molecule.from_file(os.path.join(test_dir_mol, "Carbon_Disulfide.xyz")) a = PointGroupAnalyzer(cs2, eigen_tolerance=0.001) - self.assertEqual(a.sch_symbol, "C2v") + assert a.sch_symbol == "C2v" def test_dihedral(self): a = PointGroupAnalyzer(C2H4) - self.assertEqual(a.sch_symbol, "D2h") - self.assertEqual(len(a.get_pointgroup()), 8) + assert a.sch_symbol == "D2h" + assert len(a.get_pointgroup()) == 8 a = PointGroupAnalyzer(BF3) - self.assertEqual(a.sch_symbol, "D3h") - self.assertEqual(len(a.get_pointgroup()), 12) + assert a.sch_symbol == "D3h" + assert len(a.get_pointgroup()) == 12 m = Molecule.from_file(os.path.join(test_dir_mol, "b12h12.xyz")) a = PointGroupAnalyzer(m) - self.assertEqual(a.sch_symbol, "Ih") + assert a.sch_symbol == "Ih" def test_symmetrize_molecule1(self): np.random.seed(77) @@ -642,26 +643,26 @@ def test_symmetrize_molecule1(self): eq = iterative_symmetrize(dist_mol, max_n=100, epsilon=1e-7) sym_mol, eq_sets, ops = eq["sym_mol"], eq["eq_sets"], eq["sym_ops"] - self.assertTrue({0, 1} in eq_sets.values()) - self.assertTrue({2, 3, 4, 5} in eq_sets.values()) + assert {0, 1} in eq_sets.values() + assert {2, 3, 4, 5} in eq_sets.values() coords = sym_mol.cart_coords for i, eq_set in eq_sets.items(): for j in eq_set: _ = np.dot(ops[i][j], coords[i]) - self.assertTrue(np.allclose(np.dot(ops[i][j], coords[i]), coords[j])) + assert np.allclose(np.dot(ops[i][j], coords[i]), coords[j]) def test_symmetrize_molecule2(self): np.random.seed(77) distortion = np.random.randn(len(C2H2F2Br2), 3) / 20 dist_mol = Molecule(C2H2F2Br2.species, C2H2F2Br2.cart_coords + distortion) PA1 = PointGroupAnalyzer(C2H2F2Br2, tolerance=0.1) - self.assertTrue(PA1.get_pointgroup().sch_symbol == "Ci") + assert PA1.get_pointgroup().sch_symbol == "Ci" PA2 = PointGroupAnalyzer(dist_mol, tolerance=0.1) - self.assertTrue(PA2.get_pointgroup().sch_symbol == "C1") + assert PA2.get_pointgroup().sch_symbol == "C1" eq = iterative_symmetrize(dist_mol, tolerance=0.3) PA3 = PointGroupAnalyzer(eq["sym_mol"], tolerance=0.1) - self.assertTrue(PA3.get_pointgroup().sch_symbol == "Ci") + assert PA3.get_pointgroup().sch_symbol == "Ci" def test_get_kpoint_weights(self): for name in ["SrTiO3", "LiFePO4", "Graphite"]: @@ -671,7 +672,7 @@ def test_get_kpoint_weights(self): weights = [i[1] for i in ir_mesh] weights = np.array(weights) / sum(weights) for i, w in zip(weights, a.get_kpoint_weights([i[0] for i in ir_mesh])): - self.assertAlmostEqual(i, w) + assert i == pytest.approx(w) for name in ["SrTiO3", "LiFePO4", "Graphite"]: s = PymatgenTest.get_structure(name) @@ -680,27 +681,28 @@ def test_get_kpoint_weights(self): weights = [i[1] for i in ir_mesh] weights = np.array(weights) / sum(weights) for i, w in zip(weights, a.get_kpoint_weights([i[0] for i in ir_mesh])): - self.assertAlmostEqual(i, w) + assert i == pytest.approx(w) v = Vasprun(os.path.join(PymatgenTest.TEST_FILES_DIR, "vasprun.xml")) a = SpacegroupAnalyzer(v.final_structure) wts = a.get_kpoint_weights(v.actual_kpoints) for w1, w2 in zip(v.actual_kpoints_weights, wts): - self.assertAlmostEqual(w1, w2) + assert w1 == pytest.approx(w2) kpts = [[0, 0, 0], [0.15, 0.15, 0.15], [0.2, 0.2, 0.2]] - self.assertRaises(ValueError, a.get_kpoint_weights, kpts) + with pytest.raises(ValueError): + a.get_kpoint_weights(kpts) class FuncTest(unittest.TestCase): def test_cluster_sites(self): o, c = cluster_sites(CH4, 0.1) - self.assertEqual(o.specie.symbol, "C") - self.assertEqual(len(c), 1) + assert o.specie.symbol == "C" + assert len(c) == 1 o, c = cluster_sites(C2H2F2Br2.get_centered_molecule(), 0.1) - self.assertIsNone(o) - self.assertEqual(len(c), 4) + assert o is None + assert len(c) == 4 if __name__ == "__main__":