pwdata 0.2.15__tar.gz → 0.2.16__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. {pwdata-0.2.15 → pwdata-0.2.16}/PKG-INFO +1 -1
  2. pwdata-0.2.16/pwdata/datasets_saver.py +188 -0
  3. pwdata-0.2.16/pwdata/extendedxyz.py +101 -0
  4. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/image.py +2 -1
  5. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/main.py +3 -3
  6. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata.egg-info/PKG-INFO +1 -1
  7. {pwdata-0.2.15 → pwdata-0.2.16}/setup.py +1 -1
  8. pwdata-0.2.15/pwdata/datasets_saver.py +0 -120
  9. pwdata-0.2.15/pwdata/extendedxyz.py +0 -24
  10. {pwdata-0.2.15 → pwdata-0.2.16}/LICENSE +0 -0
  11. {pwdata-0.2.15 → pwdata-0.2.16}/README.md +0 -0
  12. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/__init__.py +0 -0
  13. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/atomconfig.py +0 -0
  14. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/build/__init__.py +0 -0
  15. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/build/cell.py +0 -0
  16. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/build/geometry.py +0 -0
  17. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/build/supercells.py +0 -0
  18. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/build/write_struc.py +0 -0
  19. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/calculators/__init__.py +0 -0
  20. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/calculators/const.py +0 -0
  21. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/calculators/unitconvert_lmps.py +0 -0
  22. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/calculators/units.py +0 -0
  23. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/cp2kdata.py +0 -0
  24. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/deepmd.py +0 -0
  25. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/dump.py +0 -0
  26. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/lammpsdata.py +0 -0
  27. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/lmps.py +0 -0
  28. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/movement.py +0 -0
  29. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/movement_saver.py +0 -0
  30. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/outcar.py +0 -0
  31. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/pertub/__init__.py +0 -0
  32. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/pertub/perturbation.py +0 -0
  33. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/pertub/scale.py +0 -0
  34. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/poscar.py +0 -0
  35. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata/pwmlff.py +0 -0
  36. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata.egg-info/SOURCES.txt +0 -0
  37. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata.egg-info/dependency_links.txt +0 -0
  38. {pwdata-0.2.15 → pwdata-0.2.16}/pwdata.egg-info/top_level.txt +0 -0
  39. {pwdata-0.2.15 → pwdata-0.2.16}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pwdata
3
- Version: 0.2.15
3
+ Version: 0.2.16
4
4
  Summary: pwdata is a data pre-processing tool for PWMLFF, which can be used to extract features and labels. It also provides convenient interfaces for data conversion between different software.
5
5
  Home-page: https://github.com/LonxunQuantum/pwdata
6
6
  Author: LonxunQuantum
@@ -0,0 +1,188 @@
1
+ import numpy as np
2
+ import os
3
+ from collections import Counter
4
+ from math import ceil
5
+ from collections import defaultdict
6
+ from pwdata.calculators.const import elements
7
+
8
+ def save_to_dataset(image_data: list, datasets_path = "./PWdata", train_data_path = "train", valid_data_path = "valid",
9
+ train_ratio = None, random = True, seed = 2024, retain_raw = False, data_name = None):
10
+
11
+ if image_data[0].format == 'extxyz':
12
+ image_lists = defaultdict(list)
13
+ for image in image_data:
14
+ key = (image.formula, tuple(image.pbc))
15
+ image_lists[key].append(image)
16
+
17
+ for key, image_data in image_lists.items():
18
+ data_name = key[0]
19
+ suffix = 0
20
+ while os.path.exists(os.path.join(datasets_path, data_name)):
21
+ suffix += 1
22
+ data_name = key[0] + "_" + str(suffix)
23
+ """ Get and process the data from the input file. """
24
+ lattice, position, energies, ei, forces, virials, atom_type, atom_types_image, image_nums = get_pw(image_data)
25
+
26
+ labels_path = os.path.join(datasets_path, data_name)
27
+ if not os.path.exists(datasets_path):
28
+ os.makedirs(datasets_path, exist_ok=True)
29
+ if not os.path.exists(labels_path):
30
+ os.makedirs(labels_path, exist_ok=True)
31
+
32
+ if seed:
33
+ np.random.seed(seed)
34
+ indices = np.arange(image_nums) # 0, 1, 2, ..., image_nums-1
35
+ if random:
36
+ np.random.shuffle(indices) # shuffle the indices
37
+ assert train_ratio is not None, "train_ratio must be set"
38
+ train_size = ceil(image_nums * train_ratio)
39
+ train_indices = indices[:train_size]
40
+ val_indices = indices[train_size:]
41
+ # image_nums = [image_nums]
42
+ atom_types_image = atom_types_image.reshape(1, -1)
43
+
44
+ train_data = [lattice[train_indices], position[train_indices], energies[train_indices],
45
+ forces[train_indices], atom_types_image, atom_type,
46
+ ei[train_indices]]
47
+ val_data = [lattice[val_indices], position[val_indices], energies[val_indices],
48
+ forces[val_indices], atom_types_image, atom_type,
49
+ ei[val_indices]]
50
+
51
+ if len(virials) != 0:
52
+ train_data.append(virials[train_indices])
53
+ val_data.append(virials[val_indices])
54
+ else:
55
+ train_data.append([])
56
+ val_data.append([])
57
+
58
+ if train_ratio == 1.0 or len(val_indices) == 0:
59
+ labels_path = os.path.join(labels_path, train_data_path)
60
+ if not os.path.exists(labels_path):
61
+ os.makedirs(labels_path)
62
+ if retain_raw:
63
+ save_to_raw(train_data, train_data_path)
64
+ save_to_npy(train_data, labels_path)
65
+ else:
66
+ train_path = os.path.join(labels_path, train_data_path)
67
+ val_path = os.path.join(labels_path, valid_data_path)
68
+ if not os.path.exists(train_path):
69
+ os.makedirs(train_path)
70
+ if not os.path.exists(val_path):
71
+ os.makedirs(val_path)
72
+ if retain_raw:
73
+ save_to_raw(train_data, train_path)
74
+ save_to_raw(val_data, val_path)
75
+ save_to_npy(train_data, train_path)
76
+ save_to_npy(val_data, val_path)
77
+ else:
78
+ """ Get and process the data from the input file. """
79
+ lattice, position, energies, ei, forces, virials, atom_type, atom_types_image, image_nums = get_pw(image_data)
80
+
81
+ if data_name is None:
82
+ sc = Counter(atom_types_image) # a list sc of (symbol, count) pairs
83
+ temp_data_name = ''.join([elements[key] + str(count) for key, count in sc.items()])
84
+ data_name = temp_data_name
85
+ suffix = 0
86
+ while os.path.exists(os.path.join(datasets_path, data_name)):
87
+ suffix += 1
88
+ data_name = temp_data_name + "_" + str(suffix)
89
+ else:
90
+ pass
91
+
92
+ labels_path = os.path.join(datasets_path, data_name)
93
+ if not os.path.exists(datasets_path):
94
+ os.makedirs(datasets_path, exist_ok=True)
95
+ if not os.path.exists(labels_path):
96
+ os.makedirs(labels_path, exist_ok=True)
97
+
98
+ if seed:
99
+ np.random.seed(seed)
100
+ indices = np.arange(image_nums) # 0, 1, 2, ..., image_nums-1
101
+ if random:
102
+ np.random.shuffle(indices) # shuffle the indices
103
+ assert train_ratio is not None, "train_ratio must be set"
104
+ train_size = ceil(image_nums * train_ratio)
105
+ train_indices = indices[:train_size]
106
+ val_indices = indices[train_size:]
107
+ # image_nums = [image_nums]
108
+ atom_types_image = atom_types_image.reshape(1, -1)
109
+
110
+ train_data = [lattice[train_indices], position[train_indices], energies[train_indices],
111
+ forces[train_indices], atom_types_image, atom_type,
112
+ ei[train_indices]]
113
+ val_data = [lattice[val_indices], position[val_indices], energies[val_indices],
114
+ forces[val_indices], atom_types_image, atom_type,
115
+ ei[val_indices]]
116
+
117
+ if len(virials) != 0:
118
+ train_data.append(virials[train_indices])
119
+ val_data.append(virials[val_indices])
120
+ else:
121
+ train_data.append([])
122
+ val_data.append([])
123
+
124
+ if train_ratio == 1.0 or len(val_indices) == 0:
125
+ labels_path = os.path.join(labels_path, train_data_path)
126
+ if not os.path.exists(labels_path):
127
+ os.makedirs(labels_path)
128
+ if retain_raw:
129
+ save_to_raw(train_data, train_data_path)
130
+ save_to_npy(train_data, labels_path)
131
+ else:
132
+ train_path = os.path.join(labels_path, train_data_path)
133
+ val_path = os.path.join(labels_path, valid_data_path)
134
+ if not os.path.exists(train_path):
135
+ os.makedirs(train_path)
136
+ if not os.path.exists(val_path):
137
+ os.makedirs(val_path)
138
+ if retain_raw:
139
+ save_to_raw(train_data, train_path)
140
+ save_to_raw(val_data, val_path)
141
+ save_to_npy(train_data, train_path)
142
+ save_to_npy(val_data, val_path)
143
+
144
+ def save_to_raw(data, directory):
145
+ filenames = ["lattice.dat", "position.dat", "energies.dat", "forces.dat", "image_type.dat", "atom_type.dat", "ei.dat", "virials.dat"]
146
+ formats = ["%.8f", "%.16f", "%.8f", "%.16f", "%d", "%d", "%.8f", "%.8f"]
147
+ # for i in tqdm(range(len(data)), desc="Saving to raw files"):
148
+ for i in range(len(data)):
149
+ if i != 7 or (i == 7 and len(data[7]) != 0):
150
+ np.savetxt(os.path.join(directory, filenames[i]), data[i], fmt=formats[i])
151
+
152
+ def save_to_npy(data, directory):
153
+ filenames = ["lattice.npy", "position.npy", "energies.npy", "forces.npy", "image_type.npy", "atom_type.npy", "ei.npy", "virials.npy"]
154
+ # for i in tqdm(range(len(data)), desc="Saving to npy files"):
155
+ for i in range(len(data)):
156
+ if i != 7 or (i == 7 and len(data[7]) != 0):
157
+ np.save(os.path.join(directory, filenames[i]), data[i])
158
+
159
+ def get_pw(image_data):
160
+ # Initialize variables to store data
161
+ all_lattices = []
162
+ all_postions = []
163
+ all_energies = []
164
+ all_ei = []
165
+ all_forces = []
166
+ all_virials = []
167
+ for image in image_data:
168
+ if image.cartesian:
169
+ image.position = image.get_scaled_positions(wrap=False) # get the positions in direct coordinates, because the positions in direct coordinates are used in the MLFF model (find_neighbore)
170
+ image.cartesian = False
171
+ all_lattices.append(image.lattice)
172
+ all_postions.append(image.position)
173
+ all_energies.append(image.Ep)
174
+ all_forces.append(image.force)
175
+ all_ei.append(image.atomic_energy)
176
+ if len(image.stress) != 0:
177
+ all_virials.append(image.stress)
178
+ image_nums = len(image_data)
179
+ atom_type = np.array(image.atom_type).reshape(1, -1)
180
+ atom_types_image = np.array(image.atom_types_image)
181
+ all_lattices = np.array(all_lattices).reshape(image_nums, 9)
182
+ all_postions = np.array(all_postions).reshape(image_nums, -1)
183
+ all_energies = np.array(all_energies).reshape(image_nums, 1)
184
+ all_forces = np.array(all_forces).reshape(image_nums, -1)
185
+ all_ei = np.array(all_ei).reshape(image_nums, -1)
186
+ if len(all_virials) != 0:
187
+ all_virials = np.array(all_virials).reshape(image_nums, -1)
188
+ return all_lattices, all_postions, all_energies, all_ei, all_forces, all_virials, atom_type, atom_types_image, image_nums
@@ -0,0 +1,101 @@
1
+ import os, re
2
+ import numpy as np
3
+ from tqdm import tqdm
4
+ from pwdata.calculators.const import elements, ELEMENTTABLE
5
+ from pwdata.image import Image
6
+ from ase.io import read
7
+
8
+ class EXTXYZ(object):
9
+ def __init__(self, xyz_file, index) -> None:
10
+ self.image_list:list[Image] = []
11
+ self.number_pattern = re.compile(r"[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?")
12
+ self.load_xyz_file(xyz_file, index)
13
+
14
+ def get(self):
15
+ return self.image_list
16
+
17
+ def load_xyz_file(self, xyz_file, index):
18
+ Atoms = read(xyz_file, index=index, format='extxyz')
19
+ if isinstance(Atoms, list):
20
+ for i in tqdm(range(len(Atoms)), desc="Converting images"):
21
+ image = Image()
22
+ image.formula = Atoms[i].symbols.formula._formula
23
+ image.pbc = Atoms[i].pbc
24
+ image.atom_nums = Atoms[i].get_global_number_of_atoms()
25
+ image.atom_type = [ELEMENTTABLE[type] for type in Atoms[i].symbols.formula._count.keys()]
26
+ image.atom_type_num = [num for num in Atoms[i].symbols.formula._count.values()]
27
+ image.atom_types_image = Atoms[i].numbers
28
+ # image.atom_types_image = Atoms[i].get_chemical_symbols()
29
+ image.lattice = Atoms[i].cell.array
30
+ image.position = Atoms[i].positions
31
+ image.cartesian = True
32
+ image.force = Atoms[i].get_forces()
33
+ image.Ep = Atoms[i].info['energy']
34
+ # If Atomic-Energy is not in the file, calculate it from the Ep
35
+ atomic_energy, _, _, _ = np.linalg.lstsq([image.atom_type_num], np.array([image.Ep]), rcond=1e-3)
36
+ atomic_energy = np.repeat(atomic_energy, image.atom_type_num)
37
+ image.atomic_energy = atomic_energy.tolist()
38
+ try:
39
+ stress = Atoms[i].get_stress(voigt=False)
40
+ except AttributeError:
41
+ pass
42
+ else:
43
+ # virial = -stress * volume
44
+ image.stress = - Atoms[i].get_volume() * stress
45
+ image.format = 'extxyz'
46
+ self.image_list.append(image)
47
+ else:
48
+ image = Image()
49
+ image.formula = Atoms.get_chemical_formula()
50
+ image.pbc = Atoms.pbc
51
+ image.atom_nums = Atoms.get_global_number_of_atoms()
52
+ image.atom_type = [ELEMENTTABLE[type] for type in Atoms.symbols.formula._count.keys()]
53
+ image.atom_type_num = [num for num in Atoms.symbols.formula._count.values()]
54
+ image.atom_types_image = Atoms.numbers
55
+ # image.atom_types_image = Atoms.get_chemical_symbols()
56
+ image.lattice = Atoms.cell.array
57
+ image.position = Atoms.positions
58
+ image.cartesian = True
59
+ image.force = Atoms.get_forces()
60
+ image.Ep = Atoms.info['energy']
61
+ # If Atomic-Energy is not in the file, calculate it from the Ep
62
+ atomic_energy, _, _, _ = np.linalg.lstsq([image.atom_type_num], np.array([image.Ep]), rcond=1e-3)
63
+ atomic_energy = np.repeat(atomic_energy, image.atom_type_num)
64
+ image.atomic_energy = atomic_energy.tolist()
65
+ try:
66
+ stress = Atoms.get_stress(voigt=False)
67
+ except AttributeError:
68
+ pass
69
+ else:
70
+ image.stress = - Atoms.get_volume() * stress
71
+ image.format = 'extxyz'
72
+ self.image_list.append(image)
73
+
74
+
75
+ def save_to_extxyz(image_data_all: list, output_path: str, data_name: str, write_patthen='w'):
76
+ data_name = open(os.path.join(output_path, data_name), write_patthen)
77
+ for i in range(len(image_data_all)):
78
+ image_data = image_data_all[i]
79
+ if not image_data.cartesian:
80
+ image_data._set_cartesian()
81
+ data_name.write("%d\n" % image_data.atom_nums)
82
+ # data_name.write("Iteration: %s\n" % image_data.iteration)
83
+ output_head = 'Lattice="%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f" Properties=species:S:1:pos:R:3:forces:R:3:local_energy:R:1 pbc="T T T" energy={} '.format(image_data.Ep)
84
+ output_extended = (image_data.lattice[0][0], image_data.lattice[0][1], image_data.lattice[0][2],
85
+ image_data.lattice[1][0], image_data.lattice[1][1], image_data.lattice[1][2],
86
+ image_data.lattice[2][0], image_data.lattice[2][1], image_data.lattice[2][2])
87
+ if image_data.stress is not None:
88
+ output_head += 'stress="%.8f %.8f %.8f %.8f %.8f %.8f %.8f %.8f %.8f"'
89
+ output_extended += (image_data.stress[0][0], image_data.stress[0][1], image_data.stress[0][2],
90
+ image_data.stress[1][0], image_data.stress[1][1], image_data.stress[1][2],
91
+ image_data.stress[2][0], image_data.stress[2][1], image_data.stress[2][2])
92
+ output_head += '\n'
93
+ data_name.write(output_head % output_extended)
94
+ for j in range(image_data.atom_nums):
95
+ properties_format = "%s %14.8f %14.8f %14.8f %14.8f %14.8f %14.8f %14.8f\n"
96
+ properties = (elements[image_data.atom_types_image[j]], image_data.position[j][0], image_data.position[j][1], image_data.position[j][2],
97
+ image_data.force[j][0], image_data.force[j][1], image_data.force[j][2],
98
+ image_data.atomic_energy[j])
99
+ data_name.write(properties_format % properties)
100
+ data_name.close()
101
+ print("Convert to %s successfully!" % data_name)
@@ -8,7 +8,7 @@ from pwdata.build.cell import scaled_positions
8
8
  from pwdata.lmps import Box2l
9
9
  # 1. initial the image class
10
10
  class Image(object):
11
- def __init__(self,
11
+ def __init__(self, formula = None,
12
12
  atom_type = None, atom_type_num = None, atom_nums = None, atom_types_image = None,
13
13
  iteration = None, Etot = None, Ep = None, Ek = None, scf = None, lattice = None,
14
14
  stress = None, position = None, force = None, atomic_energy = None,
@@ -35,6 +35,7 @@ class Image(object):
35
35
  image_nums (int): The number of images.
36
36
  pbc (list): three bool, Periodic boundary conditions flags. Examples: [True, True, False] or [1, 1, 0]. True (1) means periodic, False (0) means non-periodic. Default: [False, False, False].
37
37
  """
38
+ self.formula = formula
38
39
  self.atom_nums = atom_nums
39
40
  self.iteration = iteration
40
41
  self.atom_type = atom_type
@@ -16,7 +16,7 @@ from pwdata.cp2kdata import CP2KMD, CP2KSCF
16
16
  from pwdata.deepmd import DPNPY, DPRAW
17
17
  from pwdata.pwmlff import PWNPY
18
18
  from pwdata.movement_saver import save_to_movement
19
- from pwdata.extendedxyz import save_to_extxyz
19
+ from pwdata.extendedxyz import EXTXYZ, save_to_extxyz
20
20
  from pwdata.datasets_saver import save_to_dataset, get_pw, save_to_raw, save_to_npy
21
21
  from pwdata.build.write_struc import write_config, write_vasp, write_lammps
22
22
 
@@ -47,7 +47,7 @@ class Save_Data(object):
47
47
  elif format.lower() == "vasp/outcar":
48
48
  self.image_data = OUTCAR(data_path)
49
49
  elif format.lower() == "extxyz":
50
- pass
50
+ self.image_data = EXTXYZ(data_path)
51
51
  elif format.lower() == "vasp/xml":
52
52
  pass
53
53
  elif format.lower() == 'cp2k/md':
@@ -175,7 +175,7 @@ class Config(object):
175
175
  elif format.lower() == "vasp/outcar":
176
176
  image = OUTCAR(data_path).image_list[index]
177
177
  elif format.lower() == "extxyz":
178
- image = None
178
+ image = EXTXYZ(data_path, index).image_list[index]
179
179
  elif format.lower() == "vasp/xml":
180
180
  image = None
181
181
  elif format.lower() == 'cp2k/md':
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pwdata
3
- Version: 0.2.15
3
+ Version: 0.2.16
4
4
  Summary: pwdata is a data pre-processing tool for PWMLFF, which can be used to extract features and labels. It also provides convenient interfaces for data conversion between different software.
5
5
  Home-page: https://github.com/LonxunQuantum/pwdata
6
6
  Author: LonxunQuantum
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name="pwdata",
8
- version="0.2.15",
8
+ version="0.2.16",
9
9
  author="LonxunQuantum",
10
10
  author_email="lonxun@pwmat.com",
11
11
  description="pwdata is a data pre-processing tool for PWMLFF, which can be used to extract features and labels. It also provides convenient interfaces for data conversion between different software.",
@@ -1,120 +0,0 @@
1
- import numpy as np
2
- import os
3
- from collections import Counter
4
- from math import ceil
5
- from pwdata.calculators.const import elements
6
-
7
- def save_to_dataset(image_data: list, datasets_path = "./PWdata", train_data_path = "train", valid_data_path = "valid",
8
- train_ratio = None, random = True, seed = 2024, retain_raw = False, data_name = None):
9
-
10
- """ Get and process the data from the input file. """
11
- lattice, position, energies, ei, forces, virials, atom_type, atom_types_image, image_nums = get_pw(image_data)
12
-
13
- if data_name is None:
14
- sc = Counter(atom_types_image) # a list sc of (symbol, count) pairs
15
- temp_data_name = ''.join([elements[key] + str(count) for key, count in sc.items()])
16
- data_name = temp_data_name
17
- suffix = 0
18
- while os.path.exists(os.path.join(datasets_path, data_name)):
19
- suffix += 1
20
- data_name = temp_data_name + "_" + str(suffix)
21
- else:
22
- pass
23
-
24
- labels_path = os.path.join(datasets_path, data_name)
25
- if not os.path.exists(datasets_path):
26
- os.makedirs(datasets_path, exist_ok=True)
27
- if not os.path.exists(labels_path):
28
- os.makedirs(labels_path, exist_ok=True)
29
-
30
- if seed:
31
- np.random.seed(seed)
32
- indices = np.arange(image_nums) # 0, 1, 2, ..., image_nums-1
33
- if random:
34
- np.random.shuffle(indices) # shuffle the indices
35
- assert train_ratio is not None, "train_ratio must be set"
36
- train_size = ceil(image_nums * train_ratio)
37
- train_indices = indices[:train_size]
38
- val_indices = indices[train_size:]
39
- # image_nums = [image_nums]
40
- atom_types_image = atom_types_image.reshape(1, -1)
41
-
42
- train_data = [lattice[train_indices], position[train_indices], energies[train_indices],
43
- forces[train_indices], atom_types_image, atom_type,
44
- ei[train_indices]]
45
- val_data = [lattice[val_indices], position[val_indices], energies[val_indices],
46
- forces[val_indices], atom_types_image, atom_type,
47
- ei[val_indices]]
48
-
49
- if len(virials) != 0:
50
- train_data.append(virials[train_indices])
51
- val_data.append(virials[val_indices])
52
- else:
53
- train_data.append([])
54
- val_data.append([])
55
-
56
- if train_ratio == 1.0 or len(val_indices) == 0:
57
- labels_path = os.path.join(labels_path, train_data_path)
58
- if not os.path.exists(labels_path):
59
- os.makedirs(labels_path)
60
- if retain_raw:
61
- save_to_raw(train_data, train_data_path)
62
- save_to_npy(train_data, labels_path)
63
- else:
64
- train_path = os.path.join(labels_path, train_data_path)
65
- val_path = os.path.join(labels_path, valid_data_path)
66
- if not os.path.exists(train_path):
67
- os.makedirs(train_path)
68
- if not os.path.exists(val_path):
69
- os.makedirs(val_path)
70
- if retain_raw:
71
- save_to_raw(train_data, train_path)
72
- save_to_raw(val_data, val_path)
73
- save_to_npy(train_data, train_path)
74
- save_to_npy(val_data, val_path)
75
-
76
- def save_to_raw(data, directory):
77
- filenames = ["lattice.dat", "position.dat", "energies.dat", "forces.dat", "image_type.dat", "atom_type.dat", "ei.dat", "virials.dat"]
78
- formats = ["%.8f", "%.16f", "%.8f", "%.16f", "%d", "%d", "%.8f", "%.8f"]
79
- # for i in tqdm(range(len(data)), desc="Saving to raw files"):
80
- for i in range(len(data)):
81
- if i != 7 or (i == 7 and len(data[7]) != 0):
82
- np.savetxt(os.path.join(directory, filenames[i]), data[i], fmt=formats[i])
83
-
84
- def save_to_npy(data, directory):
85
- filenames = ["lattice.npy", "position.npy", "energies.npy", "forces.npy", "image_type.npy", "atom_type.npy", "ei.npy", "virials.npy"]
86
- # for i in tqdm(range(len(data)), desc="Saving to npy files"):
87
- for i in range(len(data)):
88
- if i != 7 or (i == 7 and len(data[7]) != 0):
89
- np.save(os.path.join(directory, filenames[i]), data[i])
90
-
91
- def get_pw(image_data):
92
- # Initialize variables to store data
93
- all_lattices = []
94
- all_postions = []
95
- all_energies = []
96
- all_ei = []
97
- all_forces = []
98
- all_virials = []
99
- for image in image_data:
100
- if image.cartesian:
101
- image.position = image.get_scaled_positions(wrap=False) # get the positions in direct coordinates, because the positions in direct coordinates are used in the MLFF model (find_neighbore)
102
- image.cartesian = False
103
- all_lattices.append(image.lattice)
104
- all_postions.append(image.position)
105
- all_energies.append(image.Ep)
106
- all_forces.append(image.force)
107
- all_ei.append(image.atomic_energy)
108
- if len(image.stress) != 0:
109
- all_virials.append(image.stress)
110
- image_nums = len(image_data)
111
- atom_type = np.array(image.atom_type).reshape(1, -1)
112
- atom_types_image = np.array(image.atom_types_image)
113
- all_lattices = np.array(all_lattices).reshape(image_nums, 9)
114
- all_postions = np.array(all_postions).reshape(image_nums, -1)
115
- all_energies = np.array(all_energies).reshape(image_nums, 1)
116
- all_forces = np.array(all_forces).reshape(image_nums, -1)
117
- all_ei = np.array(all_ei).reshape(image_nums, -1)
118
- if len(all_virials) != 0:
119
- all_virials = np.array(all_virials).reshape(image_nums, -1)
120
- return all_lattices, all_postions, all_energies, all_ei, all_forces, all_virials, atom_type, atom_types_image, image_nums
@@ -1,24 +0,0 @@
1
- import os
2
- from pwdata.calculators.const import elements
3
-
4
- def save_to_extxyz(image_data_all: list, output_path: str, data_name: str, write_patthen='w'):
5
- data_name = open(os.path.join(output_path, data_name), write_patthen)
6
- for i in range(len(image_data_all)):
7
- image_data = image_data_all[i]
8
- if not image_data.cartesian:
9
- image_data._set_cartesian()
10
- data_name.write("%d\n" % image_data.atom_nums)
11
- # data_name.write("Iteration: %s\n" % image_data.iteration)
12
- output_head = 'Lattice="%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f" Properties=species:S:1:pos:R:3:forces:R:3:local_energy:R:1 pbc="T T T" energy={}\n'.format(image_data.Ep)
13
- output_extended = (image_data.lattice[0][0], image_data.lattice[0][1], image_data.lattice[0][2],
14
- image_data.lattice[1][0], image_data.lattice[1][1], image_data.lattice[1][2],
15
- image_data.lattice[2][0], image_data.lattice[2][1], image_data.lattice[2][2])
16
- data_name.write(output_head % output_extended)
17
- for j in range(image_data.atom_nums):
18
- properties_format = "%s %14.8f %14.8f %14.8f %14.8f %14.8f %14.8f %14.8f\n"
19
- properties = (elements[image_data.atom_types_image[j]], image_data.position[j][0], image_data.position[j][1], image_data.position[j][2],
20
- image_data.force[j][0], image_data.force[j][1], image_data.force[j][2],
21
- image_data.atomic_energy[j])
22
- data_name.write(properties_format % properties)
23
- data_name.close()
24
- print("Convert to %s successfully!" % data_name)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes