|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import argparse |
4 | | -import glob |
5 | | -import json |
6 | | -import os |
7 | | -import subprocess as sp |
| 4 | +import warnings |
8 | 5 |
|
9 | | - |
10 | | -def file_len(fname): |
11 | | - with open(fname) as f: |
12 | | - for i, l in enumerate(f): |
13 | | - pass |
14 | | - return i + 1 |
| 6 | +from dpgen.collect.collect import collect_data as collect_current_data |
15 | 7 |
|
16 | 8 |
|
17 | 9 | def collect_data(target_folder, param_file, output, verbose=True): |
18 | | - target_folder = os.path.abspath(target_folder) |
19 | | - output = os.path.abspath(output) |
20 | | - tool_path = os.path.join( |
21 | | - os.path.dirname(os.path.realpath(__file__)), "..", "template" |
| 10 | + """Delegate the legacy helper to the maintained collection implementation.""" |
| 11 | + warnings.warn( |
| 12 | + "dpgen.tools.collect_data is deprecated; use `dpgen collect` or " |
| 13 | + "`dpgen.collect.collect.collect_data` instead.", |
| 14 | + DeprecationWarning, |
| 15 | + stacklevel=2, |
| 16 | + ) |
| 17 | + return collect_current_data( |
| 18 | + target_folder, |
| 19 | + param_file, |
| 20 | + output, |
| 21 | + verbose=verbose, |
| 22 | + shuffle=True, |
| 23 | + merge=False, |
22 | 24 | ) |
23 | | - command_cvt_2_raw = os.path.join(tool_path, "tools.vasp", "convert2raw.py") |
24 | | - command_cvt_2_raw += " data.configs" |
25 | | - command_shuffle_raw = os.path.join(tool_path, "tools.raw", "shuffle_raw.py") |
26 | | - command_raw_2_set = os.path.join(tool_path, "tools.raw", "raw_to_set.sh") |
27 | | - # goto input |
28 | | - cwd = os.getcwd() |
29 | | - os.chdir(target_folder) |
30 | | - jdata = json.load(open(param_file)) |
31 | | - sys = jdata["sys_configs"] |
32 | | - if verbose: |
33 | | - max_str_len = max([len(str(ii)) for ii in sys]) |
34 | | - ptr_fmt = "%%%ds %%6d" % (max_str_len + 5) # noqa: UP031 |
35 | | - # collect systems from iter dirs |
36 | | - coll_sys = [[] for ii in sys] |
37 | | - numb_sys = len(sys) |
38 | | - iters = glob.glob("iter.[0-9]*[0-9]") |
39 | | - iters.sort() |
40 | | - for ii in iters: |
41 | | - iter_data = glob.glob(os.path.join(ii, "02.fp", "data.[0-9]*[0-9]")) |
42 | | - iter_data.sort() |
43 | | - for jj in iter_data: |
44 | | - sys_idx = int(os.path.basename(jj).split(".")[-1]) |
45 | | - coll_sys[sys_idx].append(jj) |
46 | | - # create output dir |
47 | | - os.makedirs(output, exist_ok=True) |
48 | | - # loop over systems |
49 | | - for idx, ii in enumerate(coll_sys): |
50 | | - if len(ii) == 0: |
51 | | - continue |
52 | | - # link iter data dirs |
53 | | - out_sys_path = os.path.join(output, "system.%03d" % idx) # noqa: UP031 |
54 | | - os.makedirs(out_sys_path, exist_ok=True) |
55 | | - cwd_ = os.getcwd() |
56 | | - os.chdir(out_sys_path) |
57 | | - for jj in ii: |
58 | | - in_sys_path = os.path.join(target_folder, jj) |
59 | | - in_iter = in_sys_path.split("/")[-3] |
60 | | - in_base = in_sys_path.split("/")[-1] |
61 | | - out_file = in_iter + "." + in_base |
62 | | - if os.path.exists(out_file): |
63 | | - os.remove(out_file) |
64 | | - os.symlink(in_sys_path, out_file) |
65 | | - # cat data.configs |
66 | | - data_configs = glob.glob( |
67 | | - os.path.join("iter.[0-9]*[0-9].data.[0-9]*[0-9]", "orig", "data.configs") |
68 | | - ) |
69 | | - data_configs.sort() |
70 | | - os.makedirs("orig", exist_ok=True) |
71 | | - with open(os.path.join("orig", "data.configs"), "w") as outfile: |
72 | | - for fname in data_configs: |
73 | | - with open(fname) as infile: |
74 | | - outfile.write(infile.read()) |
75 | | - # convert to raw |
76 | | - os.chdir("orig") |
77 | | - sp.check_call(command_cvt_2_raw, shell=True) |
78 | | - os.chdir("..") |
79 | | - # shuffle raw |
80 | | - sp.check_call(command_shuffle_raw + " orig " + " . > /dev/null", shell=True) |
81 | | - if os.path.exists("type.raw"): |
82 | | - os.remove("type.raw") |
83 | | - os.symlink(os.path.join("orig", "type.raw"), "type.raw") |
84 | | - # raw to sets |
85 | | - sp.check_call(command_raw_2_set + " > /dev/null", shell=True) |
86 | | - # print summary |
87 | | - if verbose: |
88 | | - ndata = file_len("box.raw") |
89 | | - print(ptr_fmt % (str(sys[idx]), ndata)) |
90 | | - # ch dir |
91 | | - os.chdir(cwd_) |
92 | 25 |
|
93 | 26 |
|
94 | 27 | def _main(): |
95 | | - parser = argparse.ArgumentParser(description="Collect data from DP-GEN iterations") |
| 28 | + parser = argparse.ArgumentParser( |
| 29 | + description="Deprecated wrapper for `dpgen collect`" |
| 30 | + ) |
96 | 31 | parser.add_argument("JOB_DIR", type=str, help="the directory of the DP-GEN job") |
97 | 32 | parser.add_argument("OUTPUT", type=str, help="the output directory of data") |
98 | 33 | parser.add_argument( |
|
0 commit comments