From 1baf3dea5e01c3415dc06c38f3413be4ba4caaee Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Wed, 3 Apr 2024 00:04:12 +0800 Subject: [PATCH 01/10] PyMatgenStructureFormat with from_system method --- dpdata/plugins/pymatgen.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 82b64e715..e25023e52 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -2,10 +2,45 @@ import dpdata.pymatgen.molecule from dpdata.format import Format - +from pymatgen.core import Structure @Format.register("pymatgen/structure") class PyMatgenStructureFormat(Format): + + def from_system(self, structure: Structure, **kwargs) -> dict: + """Convert pymatgen.core.Structure to System. + + Parameters + ---------- + structure : pymatgen.core.Structure + a Pymatgen Structure, containing a structure + **kwargs : dict + other parameters + + Returns + ------- + dict + data dict + """ + symbols = [site.species_string for site in structure] + atom_names = list(structure.symbol_set) + atom_numbs = [symbols.count(symbol) for symbol in atom_names] + atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype(int) + coords = structure.cart_coords + cells = structure.lattice.matrix + nopbc = not np.any(structure.pbc) + + info_dict = { + "atom_names": atom_names, + "atom_numbs": atom_numbs, + "atom_types": atom_types, + "coords": np.array([coords]), + "cells": np.array([cells]), + "nopbc": nopbc, + } + return info_dict + + def to_system(self, data, **kwargs): """Convert System to Pymatgen Structure obj.""" structures = [] From 8ef24b8d5cba4959f691e0816a859126c56dc4e4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:20:29 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/plugins/pymatgen.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index e25023e52..167b58daa 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -1,12 +1,12 @@ import numpy as np +from pymatgen.core import Structure import dpdata.pymatgen.molecule from dpdata.format import Format -from pymatgen.core import Structure + @Format.register("pymatgen/structure") class PyMatgenStructureFormat(Format): - def from_system(self, structure: Structure, **kwargs) -> dict: """Convert pymatgen.core.Structure to System. @@ -25,7 +25,9 @@ def from_system(self, structure: Structure, **kwargs) -> dict: symbols = [site.species_string for site in structure] atom_names = list(structure.symbol_set) atom_numbs = [symbols.count(symbol) for symbol in atom_names] - atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype(int) + atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype( + int + ) coords = structure.cart_coords cells = structure.lattice.matrix nopbc = not np.any(structure.pbc) @@ -40,7 +42,6 @@ def from_system(self, structure: Structure, **kwargs) -> dict: } return info_dict - def to_system(self, data, **kwargs): """Convert System to Pymatgen Structure obj.""" structures = [] From 541dfbb666626c23d11c91ada100930bc5ed7852 Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Wed, 3 Apr 2024 01:02:49 +0800 Subject: [PATCH 03/10] Updata pyamtgen_from_method --- dpdata/plugins/pymatgen.py | 24 +++--------------------- dpdata/pymatgen/structure.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 dpdata/pymatgen/structure.py diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 167b58daa..e7e527ff7 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -1,13 +1,13 @@ import numpy as np -from pymatgen.core import Structure import dpdata.pymatgen.molecule +import dpdata.pymatgen.structure from dpdata.format import Format @Format.register("pymatgen/structure") class PyMatgenStructureFormat(Format): - def from_system(self, structure: Structure, **kwargs) -> dict: + def from_system(self, structure, **kwargs) -> dict: """Convert pymatgen.core.Structure to System. Parameters @@ -22,25 +22,7 @@ def from_system(self, structure: Structure, **kwargs) -> dict: dict data dict """ - symbols = [site.species_string for site in structure] - atom_names = list(structure.symbol_set) - atom_numbs = [symbols.count(symbol) for symbol in atom_names] - atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype( - int - ) - coords = structure.cart_coords - cells = structure.lattice.matrix - nopbc = not np.any(structure.pbc) - - info_dict = { - "atom_names": atom_names, - "atom_numbs": atom_numbs, - "atom_types": atom_types, - "coords": np.array([coords]), - "cells": np.array([cells]), - "nopbc": nopbc, - } - return info_dict + return dpdata.pymatgen.structure.from_system_data(structure) def to_system(self, data, **kwargs): """Convert System to Pymatgen Structure obj.""" diff --git a/dpdata/pymatgen/structure.py b/dpdata/pymatgen/structure.py new file mode 100644 index 000000000..26c6f390f --- /dev/null +++ b/dpdata/pymatgen/structure.py @@ -0,0 +1,27 @@ +import numpy as np + +try: + from pymatgen.core import Structure +except ImportError: + pass + +def from_system_data(structure: Structure): + symbols = [site.species_string for site in structure] + atom_names = list(structure.symbol_set) + atom_numbs = [symbols.count(symbol) for symbol in atom_names] + atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype( + int + ) + coords = structure.cart_coords + cells = structure.lattice.matrix + nopbc = not np.any(structure.pbc) + + info_dict = { + "atom_names": atom_names, + "atom_numbs": atom_numbs, + "atom_types": atom_types, + "coords": np.array([coords]), + "cells": np.array([cells]), + "nopbc": nopbc, + } + return info_dict \ No newline at end of file From 455fd7c63c892faac17cf21daa95702cb76d26a0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 2 Apr 2024 17:03:04 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/pymatgen/structure.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dpdata/pymatgen/structure.py b/dpdata/pymatgen/structure.py index 26c6f390f..6625c2e27 100644 --- a/dpdata/pymatgen/structure.py +++ b/dpdata/pymatgen/structure.py @@ -5,13 +5,12 @@ except ImportError: pass + def from_system_data(structure: Structure): symbols = [site.species_string for site in structure] atom_names = list(structure.symbol_set) atom_numbs = [symbols.count(symbol) for symbol in atom_names] - atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype( - int - ) + atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype(int) coords = structure.cart_coords cells = structure.lattice.matrix nopbc = not np.any(structure.pbc) @@ -24,4 +23,4 @@ def from_system_data(structure: Structure): "cells": np.array([cells]), "nopbc": nopbc, } - return info_dict \ No newline at end of file + return info_dict From 7c2722a9cb38d04d0c16eca9a411bea6e06ceba6 Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Wed, 3 Apr 2024 12:55:51 +0800 Subject: [PATCH 05/10] Update unittest for pymatgen from system method --- dpdata/pymatgen/structure.py | 2 +- tests/test_from_pymatgen.py | 39 ++++++++++++++++++++++++++++++++++++ tests/test_to_pymatgen.py | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_from_pymatgen.py diff --git a/dpdata/pymatgen/structure.py b/dpdata/pymatgen/structure.py index 6625c2e27..90a323373 100644 --- a/dpdata/pymatgen/structure.py +++ b/dpdata/pymatgen/structure.py @@ -6,7 +6,7 @@ pass -def from_system_data(structure: Structure): +def from_system_data(structure): symbols = [site.species_string for site in structure] atom_names = list(structure.symbol_set) atom_numbs = [symbols.count(symbol) for symbol in atom_names] diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py new file mode 100644 index 000000000..7470aad00 --- /dev/null +++ b/tests/test_from_pymatgen.py @@ -0,0 +1,39 @@ +import os +import unittest + +from context import dpdata + +try: + from pymatgen.core import Structure # noqa: F401 + + exist_module = True +except Exception: + exist_module = False + + +@unittest.skipIf(not exist_module, "skip pymatgen") +class TestFormPytmatgen(unittest.TestCase): + def setUp(self): + structure= Structure.from_file(os.path.join('poscars','POSCAR.P42nmc')) + self.system_1 = dpdata.System(structure,fmt='pymatgen/structure') + self.system_2 = dpdata.System(os.path.join('poscars','POSCAR.P42nmc'),fmt='poscar') + self.system_1_cell = self.system_1['cells'][0] + self.system_2_cell = self.system_2['cells'][0] + self.system_1_coords = self.system_1['coords'][0] + self.system_2_coords = self.system_2['coords'][0] + self.places = 6 + + def test_all(self): + self.assertEqual(self.system_1['atom_names'], self.system_2['atom_names']) + self.assertAlmostEqual(self.system_1['atom_numbs'], self.system_2['atom_numbs']) + for i in range(len(self.system_1['atom_types'])): + self.assertEqual(self.system_1['atom_types'][i], self.system_2['atom_types'][i]) + for i in range(self.system_2_cell.shape[0]): + for j in range(self.system_2_cell.shape[1]): + self.assertAlmostEqual(self.system_1_cell[i][j], self.system_2_cell[i][j]) + for row in range(self.system_2_coords.shape[0]): + for col in range(self.system_2_coords.shape[1]): + self.assertAlmostEqual(self.system_1_coords[row][col], self.system_2_coords[row][col]) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/tests/test_to_pymatgen.py b/tests/test_to_pymatgen.py index d58153962..651445457 100644 --- a/tests/test_to_pymatgen.py +++ b/tests/test_to_pymatgen.py @@ -5,7 +5,7 @@ from context import dpdata try: - from pymatgen import Structure # noqa: F401 + from pymatgen.core import Structure # noqa: F401 exist_module = True except Exception: From c1691fb6ae46db9cd6a103e6e72b166910f5421d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Apr 2024 04:58:33 +0000 Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_from_pymatgen.py | 39 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py index 7470aad00..3bd4d4174 100644 --- a/tests/test_from_pymatgen.py +++ b/tests/test_from_pymatgen.py @@ -14,26 +14,35 @@ @unittest.skipIf(not exist_module, "skip pymatgen") class TestFormPytmatgen(unittest.TestCase): def setUp(self): - structure= Structure.from_file(os.path.join('poscars','POSCAR.P42nmc')) - self.system_1 = dpdata.System(structure,fmt='pymatgen/structure') - self.system_2 = dpdata.System(os.path.join('poscars','POSCAR.P42nmc'),fmt='poscar') - self.system_1_cell = self.system_1['cells'][0] - self.system_2_cell = self.system_2['cells'][0] - self.system_1_coords = self.system_1['coords'][0] - self.system_2_coords = self.system_2['coords'][0] + structure = Structure.from_file(os.path.join("poscars", "POSCAR.P42nmc")) + self.system_1 = dpdata.System(structure, fmt="pymatgen/structure") + self.system_2 = dpdata.System( + os.path.join("poscars", "POSCAR.P42nmc"), fmt="poscar" + ) + self.system_1_cell = self.system_1["cells"][0] + self.system_2_cell = self.system_2["cells"][0] + self.system_1_coords = self.system_1["coords"][0] + self.system_2_coords = self.system_2["coords"][0] self.places = 6 - + def test_all(self): - self.assertEqual(self.system_1['atom_names'], self.system_2['atom_names']) - self.assertAlmostEqual(self.system_1['atom_numbs'], self.system_2['atom_numbs']) - for i in range(len(self.system_1['atom_types'])): - self.assertEqual(self.system_1['atom_types'][i], self.system_2['atom_types'][i]) + self.assertEqual(self.system_1["atom_names"], self.system_2["atom_names"]) + self.assertAlmostEqual(self.system_1["atom_numbs"], self.system_2["atom_numbs"]) + for i in range(len(self.system_1["atom_types"])): + self.assertEqual( + self.system_1["atom_types"][i], self.system_2["atom_types"][i] + ) for i in range(self.system_2_cell.shape[0]): for j in range(self.system_2_cell.shape[1]): - self.assertAlmostEqual(self.system_1_cell[i][j], self.system_2_cell[i][j]) + self.assertAlmostEqual( + self.system_1_cell[i][j], self.system_2_cell[i][j] + ) for row in range(self.system_2_coords.shape[0]): for col in range(self.system_2_coords.shape[1]): - self.assertAlmostEqual(self.system_1_coords[row][col], self.system_2_coords[row][col]) + self.assertAlmostEqual( + self.system_1_coords[row][col], self.system_2_coords[row][col] + ) + if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() From 15f7b6b8e5afa554a9aaf4efeb8b7f0b73df417d Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Wed, 3 Apr 2024 13:15:36 +0800 Subject: [PATCH 07/10] update test for pymatgen --- tests/test_from_pymatgen.py | 22 +++++----------------- tests/test_to_pymatgen.py | 2 +- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py index 7470aad00..2c2aeebc6 100644 --- a/tests/test_from_pymatgen.py +++ b/tests/test_from_pymatgen.py @@ -2,6 +2,7 @@ import unittest from context import dpdata +from comp_sys import CompSys, IsPBC try: from pymatgen.core import Structure # noqa: F401 @@ -12,28 +13,15 @@ @unittest.skipIf(not exist_module, "skip pymatgen") -class TestFormPytmatgen(unittest.TestCase): +class TestFormPytmatgen(unittest.TestCase, CompSys, IsPBC): def setUp(self): structure= Structure.from_file(os.path.join('poscars','POSCAR.P42nmc')) self.system_1 = dpdata.System(structure,fmt='pymatgen/structure') self.system_2 = dpdata.System(os.path.join('poscars','POSCAR.P42nmc'),fmt='poscar') - self.system_1_cell = self.system_1['cells'][0] - self.system_2_cell = self.system_2['cells'][0] - self.system_1_coords = self.system_1['coords'][0] - self.system_2_coords = self.system_2['coords'][0] self.places = 6 - - def test_all(self): - self.assertEqual(self.system_1['atom_names'], self.system_2['atom_names']) - self.assertAlmostEqual(self.system_1['atom_numbs'], self.system_2['atom_numbs']) - for i in range(len(self.system_1['atom_types'])): - self.assertEqual(self.system_1['atom_types'][i], self.system_2['atom_types'][i]) - for i in range(self.system_2_cell.shape[0]): - for j in range(self.system_2_cell.shape[1]): - self.assertAlmostEqual(self.system_1_cell[i][j], self.system_2_cell[i][j]) - for row in range(self.system_2_coords.shape[0]): - for col in range(self.system_2_coords.shape[1]): - self.assertAlmostEqual(self.system_1_coords[row][col], self.system_2_coords[row][col]) + self.e_places = 6 + self.f_places = 6 + self.v_places = 6 if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/tests/test_to_pymatgen.py b/tests/test_to_pymatgen.py index 651445457..34e6646c3 100644 --- a/tests/test_to_pymatgen.py +++ b/tests/test_to_pymatgen.py @@ -19,7 +19,7 @@ def setUp(self): system_1.from_lammps_lmp( os.path.join("poscars", "conf.lmp"), type_map=["O", "H"] ) - system_1.to_pymatgen_structure()[0].to("poscar", "tmp.POSCAR") + system_1.to_pymatgen_structure()[0].to("tmp.POSCAR", "poscar") self.system_1 = system_1 self.system_2 = dpdata.System("tmp.POSCAR") self.places = 6 From 45c5a2fdf04d8ab083ab329e28c74d20a283d10c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Apr 2024 05:17:37 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_from_pymatgen.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py index 33a1c0da5..fed21c16a 100644 --- a/tests/test_from_pymatgen.py +++ b/tests/test_from_pymatgen.py @@ -1,8 +1,8 @@ import os import unittest -from context import dpdata from comp_sys import CompSys, IsPBC +from context import dpdata try: from pymatgen.core import Structure # noqa: F401 @@ -15,13 +15,16 @@ @unittest.skipIf(not exist_module, "skip pymatgen") class TestFormPytmatgen(unittest.TestCase, CompSys, IsPBC): def setUp(self): - structure= Structure.from_file(os.path.join('poscars','POSCAR.P42nmc')) - self.system_1 = dpdata.System(structure,fmt='pymatgen/structure') - self.system_2 = dpdata.System(os.path.join('poscars','POSCAR.P42nmc'),fmt='poscar') + structure = Structure.from_file(os.path.join("poscars", "POSCAR.P42nmc")) + self.system_1 = dpdata.System(structure, fmt="pymatgen/structure") + self.system_2 = dpdata.System( + os.path.join("poscars", "POSCAR.P42nmc"), fmt="poscar" + ) self.places = 6 self.e_places = 6 self.f_places = 6 self.v_places = 6 + if __name__ == "__main__": unittest.main() From 1a34fc92975b88debc878994acb1d64822ff58fa Mon Sep 17 00:00:00 2001 From: MoseyQAQ Date: Wed, 3 Apr 2024 14:07:45 +0800 Subject: [PATCH 09/10] update unittest --- dpdata/pymatgen/structure.py | 8 +++----- tests/test_from_pymatgen.py | 2 +- tests/test_to_pymatgen.py | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/dpdata/pymatgen/structure.py b/dpdata/pymatgen/structure.py index 90a323373..fa7099634 100644 --- a/dpdata/pymatgen/structure.py +++ b/dpdata/pymatgen/structure.py @@ -1,26 +1,24 @@ import numpy as np try: - from pymatgen.core import Structure + from pymatgen.core import Structure # noqa: F401 except ImportError: pass -def from_system_data(structure): +def from_system_data(structure) -> dict: symbols = [site.species_string for site in structure] atom_names = list(structure.symbol_set) atom_numbs = [symbols.count(symbol) for symbol in atom_names] atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype(int) coords = structure.cart_coords cells = structure.lattice.matrix - nopbc = not np.any(structure.pbc) info_dict = { "atom_names": atom_names, "atom_numbs": atom_numbs, "atom_types": atom_types, "coords": np.array([coords]), - "cells": np.array([cells]), - "nopbc": nopbc, + "cells": np.array([cells]) } return info_dict diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py index fed21c16a..0e5740fb0 100644 --- a/tests/test_from_pymatgen.py +++ b/tests/test_from_pymatgen.py @@ -13,7 +13,7 @@ @unittest.skipIf(not exist_module, "skip pymatgen") -class TestFormPytmatgen(unittest.TestCase, CompSys, IsPBC): +class TestFormPytmatgen(unittest.TestCase, CompSys): def setUp(self): structure = Structure.from_file(os.path.join("poscars", "POSCAR.P42nmc")) self.system_1 = dpdata.System(structure, fmt="pymatgen/structure") diff --git a/tests/test_to_pymatgen.py b/tests/test_to_pymatgen.py index 34e6646c3..b55443d4d 100644 --- a/tests/test_to_pymatgen.py +++ b/tests/test_to_pymatgen.py @@ -19,7 +19,7 @@ def setUp(self): system_1.from_lammps_lmp( os.path.join("poscars", "conf.lmp"), type_map=["O", "H"] ) - system_1.to_pymatgen_structure()[0].to("tmp.POSCAR", "poscar") + system_1.to_pymatgen_structure()[0].to(filename="tmp.POSCAR", fmt="poscar") self.system_1 = system_1 self.system_2 = dpdata.System("tmp.POSCAR") self.places = 6 From e56d0b33b9db5bcc2c0901faa0e598a4c8401ff6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Apr 2024 06:07:59 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/pymatgen/structure.py | 4 ++-- tests/test_from_pymatgen.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dpdata/pymatgen/structure.py b/dpdata/pymatgen/structure.py index fa7099634..b6c148da3 100644 --- a/dpdata/pymatgen/structure.py +++ b/dpdata/pymatgen/structure.py @@ -1,7 +1,7 @@ import numpy as np try: - from pymatgen.core import Structure # noqa: F401 + from pymatgen.core import Structure # noqa: F401 except ImportError: pass @@ -19,6 +19,6 @@ def from_system_data(structure) -> dict: "atom_numbs": atom_numbs, "atom_types": atom_types, "coords": np.array([coords]), - "cells": np.array([cells]) + "cells": np.array([cells]), } return info_dict diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py index 0e5740fb0..d3ddbe3e9 100644 --- a/tests/test_from_pymatgen.py +++ b/tests/test_from_pymatgen.py @@ -1,7 +1,7 @@ import os import unittest -from comp_sys import CompSys, IsPBC +from comp_sys import CompSys from context import dpdata try: