Skip to content

Commit 2207e12

Browse files
authored
add cli (#265)
* add cli * fix tests * fix the typo in doc * fix tests
1 parent df9b686 commit 2207e12

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

dpdata/cli.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Command line interface for dpdata."""
2+
import argparse
3+
4+
from . import __version__
5+
from .system import System, LabeledSystem, MultiSystems
6+
7+
8+
def dpdata_cli():
9+
"""dpdata cli.
10+
11+
Examples
12+
--------
13+
.. code-block:: bash
14+
15+
$ dpdata -iposcar POSCAR -odeepmd/npy -O data -n
16+
"""
17+
parser = argparse.ArgumentParser(
18+
description="dpdata: Manipulating multiple atomic simulation data formats",
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
20+
)
21+
22+
parser.add_argument("from_file", type=str, help="read data from a file")
23+
parser.add_argument("--to_file", "-O", type=str, help="dump data to a file")
24+
parser.add_argument("--from_format", "-i", type=str, default="auto", help="the format of from_file")
25+
parser.add_argument("--to_format", "-o", type=str, help="the format of to_file")
26+
parser.add_argument("--no-labeled", "-n", action="store_true", help="labels aren't provided")
27+
parser.add_argument("--multi", "-m", action="store_true", help="the system contains multiple directories")
28+
parser.add_argument("--type-map", "-t", type=str, nargs="+", help="type map")
29+
30+
parser.add_argument('--version', action='version', version='dpdata v%s' % __version__)
31+
32+
parsed_args = parser.parse_args()
33+
convert(**vars(parsed_args))
34+
35+
36+
def convert(*,
37+
from_file: str,
38+
from_format: str = "auto",
39+
to_file: str = None,
40+
to_format: str = None,
41+
no_labeled: bool = False,
42+
multi: bool = False,
43+
type_map: list = None,
44+
**kwargs):
45+
"""Convert files from one format to another one.
46+
47+
Parameters
48+
----------
49+
from_file : str
50+
read data from a file
51+
from_format : str
52+
the format of from_file
53+
to_file : str
54+
dump data to a file
55+
to_format : str
56+
the format of to_file
57+
no_labeled : bool
58+
labels aren't provided
59+
multi : bool
60+
the system contains multiple directories
61+
type_map : list
62+
type map
63+
"""
64+
if multi:
65+
s = MultiSystems.from_file(from_file, fmt=from_format, type_map=type_map, labeled=not no_labeled)
66+
elif not no_labeled:
67+
s = LabeledSystem(from_file, fmt=from_format, type_map=type_map)
68+
else:
69+
s = System(from_file, fmt=from_format, type_map=type_map)
70+
if to_format is not None:
71+
out = s.to(to_format, to_file)
72+
if isinstance(out, str):
73+
print(out)
74+
else:
75+
print(s)

dpdata/system.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,21 @@ def to_fmt_obj(self, fmtobj, directory, *args, **kwargs):
10821082
for fn, ss in zip(fmtobj.to_multi_systems(self.systems.keys(), directory, **kwargs), self.systems.values()):
10831083
ss.to_fmt_obj(fmtobj, fn, *args, **kwargs)
10841084
return self
1085+
1086+
def to(self, fmt: str, *args, **kwargs) -> "MultiSystems":
1087+
"""Dump systems to the specific format.
1088+
1089+
Parameters
1090+
----------
1091+
fmt : str
1092+
format
1093+
1094+
Returns
1095+
-------
1096+
MultiSystems
1097+
self
1098+
"""
1099+
return self.to_fmt_obj(load_format(fmt), *args, **kwargs)
10851100

10861101
def __getitem__(self, key):
10871102
"""Returns proerty stored in System by key or by idx"""

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
'amber': ['parmed'],
5656
'pymatgen': ['pymatgen'],
5757
'docs': ['sphinx', 'recommonmark', 'sphinx_rtd_theme>=1.0.0rc1', 'numpydoc', 'm2r2', 'deepmodeling-sphinx'],
58-
}
58+
},
59+
entry_points={"console_scripts": ["dpdata = dpdata.cli:dpdata_cli"]},
5960
)
6061

tests/test_cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from context import dpdata
3+
from poscars.poscar_ref_oh import TestPOSCARoh
4+
import subprocess as sp
5+
6+
7+
class TestCli(unittest.TestCase, TestPOSCARoh):
8+
9+
@classmethod
10+
def setUpClass(cls) -> None:
11+
sp.check_output(["dpdata", "poscars/conf.lmp", "--type-map", "O", "H", "-olammps/lmp", "-O", "tmp.lmp", "--no-labeled"])
12+
cls.system = dpdata.System('tmp.lmp', fmt='lammps/lmp',
13+
type_map = ['O', 'H'])
14+
15+
@classmethod
16+
def tearDownClass(cls) -> None:
17+
cls.system = None

0 commit comments

Comments
 (0)