pwdata 0.3.2__tar.gz → 0.3.3__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.
Potentially problematic release.
This version of pwdata might be problematic. Click here for more details.
- {pwdata-0.3.2 → pwdata-0.3.3}/PKG-INFO +1 -1
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/build/write_struc.py +1 -1
- pwdata-0.3.3/pwdata/check_envs.py +51 -0
- pwdata-0.3.2/pwdata/main.py → pwdata-0.3.3/pwdata/config.py +4 -61
- pwdata-0.3.3/pwdata/convert_files.py +348 -0
- pwdata-0.3.3/pwdata/datasets_saver.py +190 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/deepmd.py +3 -3
- pwdata-0.3.3/pwdata/extendedxyz.py +196 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/image.py +8 -2
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/lammpsdata.py +1 -1
- pwdata-0.3.3/pwdata/main.py +473 -0
- pwdata-0.3.3/pwdata/meta.py +121 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/movement_saver.py +1 -1
- pwdata-0.3.3/pwdata/test.py +164 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata.egg-info/PKG-INFO +1 -1
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata.egg-info/SOURCES.txt +5 -0
- pwdata-0.3.3/pwdata.egg-info/entry_points.txt +2 -0
- pwdata-0.3.3/pwdata.egg-info/requires.txt +8 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/setup.py +16 -1
- pwdata-0.3.2/pwdata/check_envs.py +0 -5
- pwdata-0.3.2/pwdata/datasets_saver.py +0 -188
- pwdata-0.3.2/pwdata/extendedxyz.py +0 -102
- pwdata-0.3.2/pwdata/test.py +0 -18
- {pwdata-0.3.2 → pwdata-0.3.3}/LICENSE +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/README.md +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/__init__.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/atomconfig.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/build/__init__.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/build/cell.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/build/geometry.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/build/supercells.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/calculators/__init__.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/calculators/const.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/calculators/unitconvert_lmps.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/calculators/units.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/cp2kdata.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/dump.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/lmps.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/movement.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/outcar.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/pertub/__init__.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/pertub/perturbation.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/pertub/scale.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/poscar.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata/pwmlff.py +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata.egg-info/dependency_links.txt +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/pwdata.egg-info/top_level.txt +0 -0
- {pwdata-0.3.2 → pwdata-0.3.3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: pwdata
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
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
|
|
@@ -20,7 +20,7 @@ def write_config(image,
|
|
|
20
20
|
positions scaled coordinates (Direct), and constraints
|
|
21
21
|
to file. fractional coordinates is default.
|
|
22
22
|
"""
|
|
23
|
-
assert direct==True, "PWmat only support direct coordinates"
|
|
23
|
+
# assert direct==True, "PWmat only support direct coordinates"
|
|
24
24
|
atom_nums = len(image) if image.atom_nums is None else image.atom_nums
|
|
25
25
|
if isinstance(image, (list, tuple)):
|
|
26
26
|
if atom_nums > 1:
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
def print_cmd():
|
|
2
|
+
cmd_infos()
|
|
3
|
+
|
|
4
|
+
def cmd_infos():
|
|
5
|
+
info = ""
|
|
6
|
+
info += "In functional development, please refer to the link\n\t\thttp://doc.lonxun.com/PWMLFF/Appendix-2/\n"
|
|
7
|
+
|
|
8
|
+
info += "scale_cell or scale:\n"
|
|
9
|
+
info += "\tScaling the structural lattice, the command example:\n"
|
|
10
|
+
info += "\tscale_cell -r 0.98 0.99 0.97 0.95 -i lisi_atom.config -f pwmat/config -s scale_atom.config -o pwmat/config\n"
|
|
11
|
+
info += "\t\nYou can also use 'pwdata scale_cell -h' to obtain a more detailed parameter list\n\n"
|
|
12
|
+
|
|
13
|
+
info += "super_cell or super:\n"
|
|
14
|
+
info += "\tConstruct a supercell based on the input original structure and supercell matrix, the command example:\n"
|
|
15
|
+
info += "\tpwdata super_cell -i input_file -f input_file_format -n output_format_name -o output_format\n"
|
|
16
|
+
info += "\tYou can also use 'pwdata super_cell -h' to obtain a more detailed parameter list\n\n"
|
|
17
|
+
|
|
18
|
+
info += "perturb:\n"
|
|
19
|
+
info += "\tDisturb the atomic positions and unit cells of the structure, the command example:\n"
|
|
20
|
+
info += "\tpwdata perturb -e 0.01 -d 0.04 -n 20 -i LiSi_POSCAR -f vasp/poscar -s perturb_lammps -o lammps/lmp -c\n\n"
|
|
21
|
+
info += "\tYou can also use 'pwdata perturb -h' to obtain a more detailed parameter list\n\n"
|
|
22
|
+
|
|
23
|
+
info += "convert_config or cvt_config:\n"
|
|
24
|
+
info += "\tTransferring structural files between different apps(PWmat\VASP\CP2K\Lammps), the command example:\n"
|
|
25
|
+
info += "\tpwdata cvt_config -i LiSi_POSCAR -f vasp/poscar -s cvtcnf_lammps.lmp -o lammps/lmp -c\n\n"
|
|
26
|
+
info += "\tYou can also use 'pwdata convert_config -h' to obtain a more detailed parameter list\n\n"
|
|
27
|
+
|
|
28
|
+
info += "convert_configs or cvt_configs:\n"
|
|
29
|
+
info += "\tExtract various trajectory files(movement of PWmat, outcar of Vasp, lammps/dump of Lammps, cp2k-mdfile of CP2K) or data sets (deepmd/npy, deepmd/raw, extxyz, meta aselmdb) into pwmlff/npy or extxyz format, the command example:\n"
|
|
30
|
+
info += "\tpwdata convert_images -i input_file1 input_file2 ... input_filen -f input_file_format -n output_format_name -o output_format\n\n"
|
|
31
|
+
|
|
32
|
+
info += "format suppport\n"
|
|
33
|
+
info += "__________________________________________________________________________________________|\n"
|
|
34
|
+
info += "| Software | file | multi-Image | label | format |\n"
|
|
35
|
+
info += "| ----------------- | ---------------- | ----------- | ----- | -------------------------- |\n"
|
|
36
|
+
info += "| PWmat | MOVEMENT | True | True | 'pwmat/movement' |\n"
|
|
37
|
+
info += "| PWmat | OUT.MLMD | False | True | 'pwmat/movement' |\n"
|
|
38
|
+
info += "| PWmat | atom.config | False | False | 'pwmat/config' |\n"
|
|
39
|
+
info += "| VASP | OUTCAR | True | True | 'vasp/outcar' |\n"
|
|
40
|
+
info += "| VASP | poscar | False | False | 'vasp/poscar' |\n"
|
|
41
|
+
info += "| LAMMPS | lmp.init | False | False | 'lammps/lmp' |\n"
|
|
42
|
+
info += "| LAMMPS | dump | True | False | 'lammps/dump' |\n"
|
|
43
|
+
info += "| CP2K | stdout, xyz, pdb | True | True | 'cp2k/md' |\n"
|
|
44
|
+
info += "| CP2K | stdout | False | True | 'cp2k/scf' |\n"
|
|
45
|
+
info += "| PWMLFF | \*.npy | True | True | 'pwmlff/npy' |\n"
|
|
46
|
+
info += "| DeepMD (read) | \*.npy, \*.raw | True | True | 'deepmd/npy', 'deepmd/raw' |\n"
|
|
47
|
+
info += "| \* (extended xyz) | \*.xyz | True | True | 'extxyz' |\n"
|
|
48
|
+
info += "| \* (meta aselmdb) | \*.aselmdb | True | True | 'meta' |\n"
|
|
49
|
+
info += "__________________________________________________________________________________________|\n\n"
|
|
50
|
+
|
|
51
|
+
print(info)
|
|
@@ -19,6 +19,7 @@ from pwdata.movement_saver import save_to_movement
|
|
|
19
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
|
+
from pwdata.meta import META
|
|
22
23
|
|
|
23
24
|
class Save_Data(object):
|
|
24
25
|
def __init__(self, data_path, datasets_path = "./PWdata", train_data_path = "train", valid_data_path = "valid",
|
|
@@ -188,6 +189,8 @@ class Config(object):
|
|
|
188
189
|
image = DPRAW(data_path).image_list[index]
|
|
189
190
|
elif format.lower() == 'pwmlff/npy':
|
|
190
191
|
image = PWNPY(data_path).image_list[index]
|
|
192
|
+
elif format.lower() == 'meta':
|
|
193
|
+
image = META(data_path, atom_names, **kwargs).image_list[index]
|
|
191
194
|
else:
|
|
192
195
|
raise Exception("Error! The format of the input file is not supported!")
|
|
193
196
|
return image
|
|
@@ -277,64 +280,4 @@ def string2index(string: str) -> Union[int, slice, str]:
|
|
|
277
280
|
else:
|
|
278
281
|
i.append(int(s))
|
|
279
282
|
i += (3 - len(i)) * [None]
|
|
280
|
-
return slice(*i)
|
|
281
|
-
|
|
282
|
-
def run_cmd():
|
|
283
|
-
from check_envs import print_cmd
|
|
284
|
-
if len(sys.argv) == 1 or "-h".upper() == sys.argv[1].upper() or \
|
|
285
|
-
"help".upper() == sys.argv[1].upper() or "-help".upper() == sys.argv[1].upper() or "--help".upper() == sys.argv[1].upper():
|
|
286
|
-
print_cmd()
|
|
287
|
-
|
|
288
|
-
elif "convert_config".upper() == sys.argv[1].upper():
|
|
289
|
-
pass
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
if __name__ == "__main__":
|
|
293
|
-
# import argparse
|
|
294
|
-
# SUPERCELL_MATRIX = [[2, 0, 0], [0, 2, 0], [0, 0, 2]]
|
|
295
|
-
# data_file = "/data/home/hfhuang/2_MLFF/2-DP/19-json-version/8-Si2/atom.config"
|
|
296
|
-
# # data_file = "/data/home/hfhuang/9_cp2k/1-SiO2/cp2k.out"
|
|
297
|
-
# # data_file = "/data/home/hfhuang/software/mlff/Si/Si64-vasprun.xml"
|
|
298
|
-
# # data_file = "/data/home/hfhuang/2_MLFF/3-outcar2movement/0/OUTCARC3N4"
|
|
299
|
-
# output_path = "/data/home/hfhuang/2_MLFF/2-DP/19-json-version/8-Si2/mlff/"
|
|
300
|
-
# output_file = "supercell.config"
|
|
301
|
-
# format = "config"
|
|
302
|
-
# pbc = [1, 1, 1]
|
|
303
|
-
# # config = Config.read(format, data_file, atom_names=["Si"], index=-1) # read dump
|
|
304
|
-
# config = Config.read(format, data_file)
|
|
305
|
-
# Config.to(file_path = output_path,
|
|
306
|
-
# file_name = output_file,
|
|
307
|
-
# file_format = 'config',
|
|
308
|
-
# direct = True,
|
|
309
|
-
# sort = False)
|
|
310
|
-
# parser = argparse.ArgumentParser(description='Convert and build structures.')
|
|
311
|
-
# parser.add_argument('--format', type=str, required=False, help='Format of the input file', default="outcar")
|
|
312
|
-
# parser.add_argument('--save_format', type=str, required=False, help='Format of the output file', default="config")
|
|
313
|
-
# parser.add_argument('--outcar_file', type=str, required=False, help='Path to the OUTCAR file')
|
|
314
|
-
# parser.add_argument('--movement_file', type=str, required=False, help='Path to the MOVEMENT file')
|
|
315
|
-
# parser.add_argument('--output_path', type=str, required=False, help='Path to the output directory', default="./")
|
|
316
|
-
# parser.add_argument('--output_file', type=str, required=False, help='Name of the output file', default="MOVEMENT")
|
|
317
|
-
# parser.add_argument('--supercell_matrix', type=list, required=False, help='Supercell matrix', default=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
|
318
|
-
# parser.add_argument('--pbc', type=list, required=False, help='Periodic boundary conditions flags', default=[1, 1, 1])
|
|
319
|
-
# parser.add_argument('--direct', type=bool, required=False, help='Whether to write the positions in direct (frac) coordinates', default=True)
|
|
320
|
-
# parser.add_argument('--sort', type=bool, required=False, help='Whether to sort the atoms by atomic number', default=True)
|
|
321
|
-
# parser.add_argument('--pert_num', type=int, required=False, help='Number of perturbed structures', default=50)
|
|
322
|
-
# parser.add_argument('--cell_pert_fraction', type=float, required=False, help='Fraction of the cell perturbation', default=0.03)
|
|
323
|
-
# parser.add_argument('--atom_pert_distance', type=float, required=False, help='Distance of the atom perturbation', default=0.01)
|
|
324
|
-
# parser.add_argument('--retain_raw', type=bool, required=False, help='Whether to retain raw data', default=False)
|
|
325
|
-
# parser.add_argument('--train_ratio', type=float, required=False, help='Ratio of training data', default=0.8)
|
|
326
|
-
# parser.add_argument('--random', type=bool, required=False, help='Whether to shuffle the data', default=True)
|
|
327
|
-
# parser.add_argument('--scale_factor', type=float, required=False, help='Scale factor of the lattice', default=1.0)
|
|
328
|
-
# parser.add_argument('--seed', type=int, required=False, help='Random seed', default=2024)
|
|
329
|
-
# parser.add_argument('--index', type=Union[int, slice, str], required=False, help='Index of the configuration', default=-1)
|
|
330
|
-
# parser.add_argument('--atom_names', type=list, required=False, help='Names of the atoms', default=["H"])
|
|
331
|
-
# parser.add_argument('--style', type=str, required=False, help='Style of the lammps input file', default="atomic")
|
|
332
|
-
|
|
333
|
-
# args = parser.parse_args()
|
|
334
|
-
|
|
335
|
-
run_cmd()
|
|
336
|
-
|
|
337
|
-
config = Config.read("vasp/outcar",
|
|
338
|
-
"/data/home/wuxingxing/datas/al_dir/si_4_vasp/init_bulk/temp_init_bulk_work/aimd/init_config_0/0.9_scale/3_aimd/OUTCAR")
|
|
339
|
-
print()
|
|
340
|
-
|
|
283
|
+
return slice(*i)
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import numpy as np
|
|
4
|
+
from pwdata.config import Config
|
|
5
|
+
from pwdata.build.supercells import make_supercell
|
|
6
|
+
from pwdata.pertub.perturbation import perturb_structure
|
|
7
|
+
from pwdata.pertub.scale import scale_cell
|
|
8
|
+
from pwdata.utils.constant import FORMAT, ELEMENTTABLE
|
|
9
|
+
from pwdata.utils.constant import get_atomic_name_from_number
|
|
10
|
+
from pwdata.image import Image
|
|
11
|
+
from collections import Counter
|
|
12
|
+
from ase.db.row import AtomsRow
|
|
13
|
+
from pwdata.fairchem.datasets.ase_datasets import AseDBDataset
|
|
14
|
+
from concurrent.futures import ProcessPoolExecutor, as_completed
|
|
15
|
+
import multiprocessing
|
|
16
|
+
|
|
17
|
+
def do_convert_config(input_file:str,
|
|
18
|
+
input_format:str,
|
|
19
|
+
atom_types:list[str],
|
|
20
|
+
savename:str,
|
|
21
|
+
output_format:str,
|
|
22
|
+
direct:bool = True): # True: save as fractional coordinates, False for cartesian coordinates
|
|
23
|
+
image = Config(data_path=input_file, format=input_format, atom_names=atom_types)
|
|
24
|
+
image.to(output_path = os.path.dirname(os.path.abspath(savename)),
|
|
25
|
+
data_name = os.path.basename(savename),
|
|
26
|
+
save_format = output_format,
|
|
27
|
+
direct = direct,
|
|
28
|
+
sort = True)
|
|
29
|
+
return os.path.abspath(savename)
|
|
30
|
+
|
|
31
|
+
def do_scale_cell(input_file:str,
|
|
32
|
+
input_format:str,
|
|
33
|
+
atom_types:list[str],
|
|
34
|
+
savename:str,
|
|
35
|
+
output_format:str,
|
|
36
|
+
scale_factor:list[float],
|
|
37
|
+
direct:bool = True): # True: save as fractional coordinates, False for cartesian coordinates
|
|
38
|
+
# for pwamt/movement movement or MLMD.OUT files
|
|
39
|
+
if not isinstance(scale_factor, list):
|
|
40
|
+
scale_factor = [scale_factor]
|
|
41
|
+
image = Config(data_path=input_file, format=input_format, atom_names=atom_types)
|
|
42
|
+
for idx, factor in enumerate(scale_factor):
|
|
43
|
+
scaled_structs = scale_cell(image, factor)
|
|
44
|
+
scaled_structs.to(output_path = os.path.dirname(os.path.abspath(savename)),
|
|
45
|
+
data_name = "{}_{}".format(factor, os.path.basename(savename)),
|
|
46
|
+
save_format = output_format,
|
|
47
|
+
direct = direct,
|
|
48
|
+
sort = True)
|
|
49
|
+
return os.path.abspath(savename)
|
|
50
|
+
|
|
51
|
+
def do_super_cell(input_file:str,
|
|
52
|
+
input_format:str,
|
|
53
|
+
atom_types:list[str],
|
|
54
|
+
savename:str,
|
|
55
|
+
output_format:str,
|
|
56
|
+
supercell_matrix:list[int],
|
|
57
|
+
direct:bool = True,
|
|
58
|
+
pbc:list =[1, 1, 1],
|
|
59
|
+
wrap=True,
|
|
60
|
+
tol=1e-5
|
|
61
|
+
): # True: save as fractional coordinates, False for cartesian coordinates
|
|
62
|
+
# for pwamt/movement movement or MLMD.OUT files
|
|
63
|
+
image = Config(data_path=input_file, format=input_format, atom_names=atom_types)
|
|
64
|
+
scaled_structs = make_supercell(image, supercell_matrix, pbc=pbc, wrap=wrap, tol=tol)
|
|
65
|
+
scaled_structs.to(output_path = os.path.dirname(os.path.abspath(savename)),
|
|
66
|
+
data_name = os.path.basename(savename),
|
|
67
|
+
save_format = output_format,
|
|
68
|
+
direct = direct,
|
|
69
|
+
sort = True)
|
|
70
|
+
return os.path.abspath(savename)
|
|
71
|
+
|
|
72
|
+
def do_perturb(input_file:str,
|
|
73
|
+
input_format:str,
|
|
74
|
+
atom_types:list[str],
|
|
75
|
+
save_path:str,
|
|
76
|
+
save_name_prefix:str,
|
|
77
|
+
output_format:str,
|
|
78
|
+
cell_pert_fraction:float,
|
|
79
|
+
atom_pert_distance:float,
|
|
80
|
+
pert_num:int,
|
|
81
|
+
direct:bool = True
|
|
82
|
+
): # True: save as fractional coordinates, False for cartesian coordinates
|
|
83
|
+
# for pwamt/movement movement or MLMD.OUT files
|
|
84
|
+
image = Config(data_path=input_file, format=input_format, atom_names=atom_types)
|
|
85
|
+
save_path = os.path.abspath(save_path)
|
|
86
|
+
perturbed_structs = perturb_structure(
|
|
87
|
+
image_data = image,
|
|
88
|
+
pert_num = pert_num,
|
|
89
|
+
cell_pert_fraction = cell_pert_fraction,
|
|
90
|
+
atom_pert_distance = atom_pert_distance)
|
|
91
|
+
|
|
92
|
+
perturb_files = []
|
|
93
|
+
for tmp_perturbed_idx, tmp_pertubed_struct in enumerate(perturbed_structs):
|
|
94
|
+
tmp_pertubed_struct.to(output_path = save_path,
|
|
95
|
+
data_name = "{}_{}".format(tmp_perturbed_idx, save_name_prefix),
|
|
96
|
+
save_format = output_format,
|
|
97
|
+
direct = direct,
|
|
98
|
+
sort = True)
|
|
99
|
+
perturb_files.append("{}_{}".format(tmp_perturbed_idx, save_name_prefix))
|
|
100
|
+
return perturb_files, perturbed_structs
|
|
101
|
+
|
|
102
|
+
def do_convert_images(
|
|
103
|
+
input:list[str],
|
|
104
|
+
input_format:str,
|
|
105
|
+
savepath, #'the/path/pwmlff-datas'
|
|
106
|
+
output_format,
|
|
107
|
+
train_valid_ratio,
|
|
108
|
+
data_shuffle,
|
|
109
|
+
gap,
|
|
110
|
+
atom_types:list[str]=None,
|
|
111
|
+
query:str=None,
|
|
112
|
+
cpu_nums:int=None,
|
|
113
|
+
merge:bool=True
|
|
114
|
+
):
|
|
115
|
+
data_files = search_images(input, input_format)
|
|
116
|
+
image_data = load_files(data_files, input_format, atom_types=atom_types, query=query, cpu_nums=cpu_nums)
|
|
117
|
+
save_images(savepath, image_data, output_format, train_valid_ratio, data_shuffle, merge)
|
|
118
|
+
|
|
119
|
+
'''
|
|
120
|
+
description:
|
|
121
|
+
save the image_datas to pwmlff/npy or extxyz format
|
|
122
|
+
for pwmlff/npy, the images will save to subdir accordding to the atom types and atom nums of each type, such as Pb20Te30, Pb21Te30, ...
|
|
123
|
+
for extxyz, the images will save to subdir accordding to the atom types, such as PbTe, PbTeG
|
|
124
|
+
param {*} savepath
|
|
125
|
+
param {*} image_data
|
|
126
|
+
param {*} output_format
|
|
127
|
+
param {*} train_valid_ratio
|
|
128
|
+
param {*} data_shuffle
|
|
129
|
+
return {*}
|
|
130
|
+
author: wuxingxing
|
|
131
|
+
'''
|
|
132
|
+
def save_images(savepath, image_data, output_format, train_valid_ratio=1, data_shuffle=False, merge=True):
|
|
133
|
+
if merge is True and output_format == FORMAT.extxyz:
|
|
134
|
+
save_dir = savepath
|
|
135
|
+
image_data.to(
|
|
136
|
+
output_path=save_dir,
|
|
137
|
+
save_format=output_format,
|
|
138
|
+
train_ratio = train_valid_ratio,
|
|
139
|
+
random=data_shuffle,
|
|
140
|
+
seed = 2024,
|
|
141
|
+
retain_raw = False,
|
|
142
|
+
write_patthen="a"
|
|
143
|
+
)
|
|
144
|
+
else:
|
|
145
|
+
save_dict = split_image_by_atomtype_nums(image_data, format=output_format)
|
|
146
|
+
for key, images in save_dict.items():
|
|
147
|
+
# print(len(images))
|
|
148
|
+
save_dir = os.path.join(savepath, key) if output_format==FORMAT.extxyz else savepath #pwmlff/npy will do split when saving images
|
|
149
|
+
image_data.images = images
|
|
150
|
+
image_data.to(
|
|
151
|
+
output_path=save_dir,
|
|
152
|
+
save_format=output_format,
|
|
153
|
+
train_ratio = train_valid_ratio,
|
|
154
|
+
random=data_shuffle,
|
|
155
|
+
seed = 2024,
|
|
156
|
+
retain_raw = False,
|
|
157
|
+
write_patthen="a"
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
def search_images(input_list:list[str], input_format:str):
|
|
161
|
+
res_list = set()
|
|
162
|
+
for workDir in input_list:
|
|
163
|
+
workDir = os.path.abspath(workDir)
|
|
164
|
+
if os.path.isfile(workDir):
|
|
165
|
+
res_list.add(workDir)
|
|
166
|
+
else:
|
|
167
|
+
if input_format == FORMAT.pwmlff_npy:
|
|
168
|
+
for root, dirs, files in os.walk(workDir):
|
|
169
|
+
if 'energies.npy' in files:
|
|
170
|
+
if "train" in os.path.basename(root):
|
|
171
|
+
res_list.add(os.path.dirname(root))
|
|
172
|
+
|
|
173
|
+
elif input_format == FORMAT.extxyz:
|
|
174
|
+
for path, dirList, fileList in os.walk(workDir, followlinks=True):
|
|
175
|
+
for _ in fileList:
|
|
176
|
+
if ".xyz" in _:
|
|
177
|
+
res_list.add(os.path.join(path, _))
|
|
178
|
+
|
|
179
|
+
elif input_format == FORMAT.deepmd_npy:
|
|
180
|
+
for root, dirs, files in os.walk(workDir):
|
|
181
|
+
if 'energy.npy' in files:
|
|
182
|
+
res_list.add(os.path.dirname(root))
|
|
183
|
+
|
|
184
|
+
elif input_format == FORMAT.deepmd_raw:
|
|
185
|
+
for root, dirs, files in os.walk(workDir):
|
|
186
|
+
if 'energy.raw' in files:
|
|
187
|
+
res_list.add(os.path.dirname(root))
|
|
188
|
+
|
|
189
|
+
elif input_format == FORMAT.meta:
|
|
190
|
+
for root, dirs, files in os.walk(workDir):
|
|
191
|
+
for file in files:
|
|
192
|
+
if '.aselmdb' in file:
|
|
193
|
+
res_list.add(root)
|
|
194
|
+
break
|
|
195
|
+
|
|
196
|
+
return list(res_list)
|
|
197
|
+
|
|
198
|
+
def load_files(input_list:list[str], input_format:str, atom_types:list[str]=None, query:str=None, cpu_nums=None):
|
|
199
|
+
image_data = None
|
|
200
|
+
if input_format == FORMAT.meta:
|
|
201
|
+
image_data = Config(input_format, input_list, atom_names=atom_types, query=query, cpu_nums=cpu_nums)
|
|
202
|
+
if not isinstance(image_data.images, list): # for the first pwmlff/npy dir only has one picture
|
|
203
|
+
image_data.images = [image_data.images]
|
|
204
|
+
else:
|
|
205
|
+
for data_path in input_list:
|
|
206
|
+
if image_data is not None:
|
|
207
|
+
tmp_config = Config(input_format, data_path, atom_names=atom_types, query=query, cpu_nums=cpu_nums)
|
|
208
|
+
image_data.append(tmp_config)
|
|
209
|
+
else:
|
|
210
|
+
image_data = Config(input_format, data_path, atom_names=atom_types, query=query, cpu_nums=cpu_nums)
|
|
211
|
+
if not isinstance(image_data.images, list): # for the first pwmlff/npy dir only has one picture
|
|
212
|
+
image_data.images = [image_data.images]
|
|
213
|
+
return image_data
|
|
214
|
+
|
|
215
|
+
def split_image_by_atomtype_nums(image_data, format=None):
|
|
216
|
+
key_dict = {}
|
|
217
|
+
for idx, image in enumerate(image_data.images):
|
|
218
|
+
element_counts = Counter(image.atom_types_image)
|
|
219
|
+
atom_type = list(element_counts.keys())
|
|
220
|
+
counts = list(element_counts.values())
|
|
221
|
+
tmp_key = ""
|
|
222
|
+
for element, count in zip(atom_type, counts):
|
|
223
|
+
tmp_key += "{}_{}_".format(element, count)
|
|
224
|
+
if tmp_key not in key_dict:
|
|
225
|
+
key_dict[tmp_key] = [image]
|
|
226
|
+
else:
|
|
227
|
+
key_dict[tmp_key].append(image)
|
|
228
|
+
|
|
229
|
+
new_split = {}
|
|
230
|
+
for key in key_dict.keys():
|
|
231
|
+
elements = key.split('_')[:-1]
|
|
232
|
+
new_array = [int(elements[i]) for i in range(0, len(elements), 2)]
|
|
233
|
+
type_nums = elements[1::2]
|
|
234
|
+
atom_list = get_atomic_name_from_number(new_array)
|
|
235
|
+
new_key = []
|
|
236
|
+
if format == FORMAT.extxyz:
|
|
237
|
+
new_key = "".join(atom_list)
|
|
238
|
+
if new_key not in new_split:
|
|
239
|
+
new_split[new_key] = key_dict[key]
|
|
240
|
+
else:
|
|
241
|
+
new_split[new_key].extend(key_dict[key])
|
|
242
|
+
else: # for pwmlff/npy
|
|
243
|
+
for atom, num in zip(atom_list, type_nums):
|
|
244
|
+
new_key.append(atom)
|
|
245
|
+
new_key.append(num)
|
|
246
|
+
new_key = "".join(new_key)
|
|
247
|
+
new_split[new_key] = key_dict[key]
|
|
248
|
+
return new_split
|
|
249
|
+
|
|
250
|
+
def make_query(elements:list[str]):
|
|
251
|
+
# 这个闭包函数将捕获 elements 参数
|
|
252
|
+
def query(row):
|
|
253
|
+
if sorted(set(row.symbols)) == sorted(elements):
|
|
254
|
+
return True
|
|
255
|
+
return False
|
|
256
|
+
return query
|
|
257
|
+
|
|
258
|
+
def do_meta_data(input,
|
|
259
|
+
savepath,
|
|
260
|
+
output_format,
|
|
261
|
+
train_valid_ratio,
|
|
262
|
+
split_rand,
|
|
263
|
+
atom_types:list[str]=None,
|
|
264
|
+
query_str:str=None,
|
|
265
|
+
cpu_nums:int=None):
|
|
266
|
+
image_data = load_meta_datas(input, atom_types, query_str, cpu_nums)
|
|
267
|
+
save_images(savepath, image_data, output_format, train_valid_ratio, split_rand)
|
|
268
|
+
|
|
269
|
+
def load_meta_datas(input, atom_types: list[str] = None, query_str: str = None, cpu_nums: int=None):
|
|
270
|
+
search_dict = {'src': input}
|
|
271
|
+
dataset = AseDBDataset(config=search_dict)
|
|
272
|
+
image_list = []
|
|
273
|
+
for ids, dbs in enumerate(dataset.dbs):
|
|
274
|
+
if query_str is None and atom_types is None:
|
|
275
|
+
atom_list = list(dbs.select())
|
|
276
|
+
elif query_str is None and atom_types is not None:
|
|
277
|
+
atom_list = list(dbs.select("".join(atom_types)))
|
|
278
|
+
elif query_str is not None and atom_types is not None:
|
|
279
|
+
atom_list = list(dbs.select(query_str,filter=make_query))
|
|
280
|
+
else:# query_str is not None and atom_types is None:
|
|
281
|
+
atom_list = list(dbs.select(query_str))
|
|
282
|
+
for Atoms in atom_list:
|
|
283
|
+
image = to_image(Atoms)
|
|
284
|
+
image_list.append(image)
|
|
285
|
+
image_data = Config()
|
|
286
|
+
image_data.images = image_list
|
|
287
|
+
return image_data
|
|
288
|
+
|
|
289
|
+
### ProcessPoolExecutor
|
|
290
|
+
def load_meta_datas_cpus(input, atom_types: list[str] = None, query_str: str = None, cpu_nums: int=None):
|
|
291
|
+
search_dict = {'src': input}
|
|
292
|
+
dataset = AseDBDataset(config=search_dict)
|
|
293
|
+
image_list = []
|
|
294
|
+
# 定义一个辅助函数,用于查询和转换每个数据库中的数据
|
|
295
|
+
def process_dbs(dbs):
|
|
296
|
+
# 根据条件构建 atom_list
|
|
297
|
+
if query_str is None and atom_types is None:
|
|
298
|
+
atom_list = list(dbs.select())
|
|
299
|
+
elif query_str is None and atom_types is not None:
|
|
300
|
+
atom_list = list(dbs.select("".join(atom_types)))
|
|
301
|
+
elif query_str is not None and atom_types is not None:
|
|
302
|
+
atom_list = list(dbs.select(query_str, filter=make_query))
|
|
303
|
+
else:
|
|
304
|
+
atom_list = list(dbs.select(query_str))
|
|
305
|
+
# 对每个原子对象进行转换并返回 image 列表
|
|
306
|
+
return [to_image(Atoms) for Atoms in atom_list]
|
|
307
|
+
|
|
308
|
+
# 使用多进程并行处理每个子数据库
|
|
309
|
+
if cpu_nums is None:
|
|
310
|
+
cpu_nums = multiprocessing.cpu_count()
|
|
311
|
+
with ProcessPoolExecutor(max_workers=cpu_nums) as executor:
|
|
312
|
+
futures = [executor.submit(process_dbs, dbs) for dbs in dataset.dbs]
|
|
313
|
+
for future in as_completed(futures):
|
|
314
|
+
image_list.extend(future.result())
|
|
315
|
+
|
|
316
|
+
image_data = Config()
|
|
317
|
+
image_data.images = image_list
|
|
318
|
+
return image_data
|
|
319
|
+
|
|
320
|
+
def to_image(Atoms):
|
|
321
|
+
image = Image()
|
|
322
|
+
image.formula = Atoms.formula
|
|
323
|
+
image.pbc = Atoms.pbc
|
|
324
|
+
image.atom_nums = Atoms.natoms
|
|
325
|
+
type_nums_dict = Counter(Atoms.numbers)
|
|
326
|
+
image.atom_type = np.array(list(type_nums_dict.keys()))
|
|
327
|
+
image.atom_type_num = np.array(list(type_nums_dict.values()))
|
|
328
|
+
image.atom_types_image = np.array(Atoms.numbers)
|
|
329
|
+
image.lattice = np.array(Atoms.cell)
|
|
330
|
+
image.position = Atoms.positions
|
|
331
|
+
image.cartesian = True
|
|
332
|
+
image.force = Atoms.forces
|
|
333
|
+
image.Ep = Atoms.energy
|
|
334
|
+
|
|
335
|
+
# 计算 Atomic-Energy
|
|
336
|
+
atomic_energy, _, _, _ = np.linalg.lstsq([image.atom_type_num], np.array([image.Ep]), rcond=1e-3)
|
|
337
|
+
atomic_energy = np.repeat(atomic_energy, image.atom_type_num)
|
|
338
|
+
image.atomic_energy = atomic_energy.tolist()
|
|
339
|
+
|
|
340
|
+
vol = Atoms.volume
|
|
341
|
+
virial = (-np.array(Atoms.stress) * vol)
|
|
342
|
+
image.virial = np.array([
|
|
343
|
+
[virial[0], virial[5], virial[4]],
|
|
344
|
+
[virial[5], virial[1], virial[3]],
|
|
345
|
+
[virial[4], virial[3], virial[2]]
|
|
346
|
+
])
|
|
347
|
+
image.format = 'metadata'
|
|
348
|
+
return image
|