Skip to content

Commit 94d8099

Browse files
committed
fix: delegate legacy data collection
Fixes #1894 Coding-Agent: Codex Codex-Version: codex-cli 0.149.0 Model: gpt-5.6-sol Reasoning-Effort: xhigh
1 parent d5ce577 commit 94d8099

2 files changed

Lines changed: 44 additions & 84 deletions

File tree

dpgen/tools/collect_data.py

Lines changed: 19 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,33 @@
11
#!/usr/bin/env python3
22

33
import argparse
4-
import glob
5-
import json
6-
import os
7-
import subprocess as sp
4+
import warnings
85

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
157

168

179
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,
2224
)
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_)
9225

9326

9427
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+
)
9631
parser.add_argument("JOB_DIR", type=str, help="the directory of the DP-GEN job")
9732
parser.add_argument("OUTPUT", type=str, help="the output directory of data")
9833
parser.add_argument(

tests/tools/test_collect_data.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
from unittest.mock import patch
3+
4+
from dpgen.tools.collect_data import collect_data
5+
6+
7+
class TestLegacyCollectData(unittest.TestCase):
8+
@patch("dpgen.tools.collect_data.collect_current_data", return_value="result")
9+
def test_delegates_to_maintained_collector(self, current_collect_data):
10+
with self.assertWarnsRegex(DeprecationWarning, "dpgen collect"):
11+
result = collect_data("job", "param.json", "output", verbose=False)
12+
13+
self.assertEqual(result, "result")
14+
current_collect_data.assert_called_once_with(
15+
"job",
16+
"param.json",
17+
"output",
18+
verbose=False,
19+
shuffle=True,
20+
merge=False,
21+
)
22+
23+
24+
if __name__ == "__main__":
25+
unittest.main()

0 commit comments

Comments
 (0)