Skip to content

Commit cf51156

Browse files
authored
generate formats table in doc (#269)
* generate formats table in doc Also fixes some typos * bugfix
1 parent 8196d9e commit cf51156

File tree

9 files changed

+124
-4
lines changed

9 files changed

+124
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ _version.py
2222
!tests/cp2k/aimd/cp2k.log
2323
!tests/cp2k/restart_aimd/ch4.log
2424
__pycache__
25+
docs/_build
26+
docs/formats.csv
27+
docs/api/

docs/conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515
import os
1616
import sys
17+
import subprocess as sp
1718
from datetime import date
1819
sys.path.insert(0, os.path.abspath('..'))
1920

@@ -171,8 +172,12 @@ def run_apidoc(_):
171172
module = os.path.join(cur_dir, "..", "dpdata")
172173
main(['-M', '--tocfile', 'api', '-H', 'API documentation', '-o', os.path.join(cur_dir, "api"), module, '--force'])
173174

175+
def run_formats(_):
176+
sp.check_output([sys.executable, "make_format.py"])
177+
174178
def setup(app):
175179
app.connect('builder-inited', run_apidoc)
180+
app.connect('builder-inited', run_formats)
176181

177182

178183
intersphinx_mapping = {

docs/formats.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Supported Formats
2+
=================
3+
4+
dpdata supports the following formats:
5+
6+
.. csv-table:: Supported Formats
7+
:file: formats.csv
8+
:header-rows: 1
9+

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Welcome to dpdata's documentation!
1010
:maxdepth: 2
1111
:caption: Contents:
1212

13+
formats
1314
api/api
1415

1516
.. mdinclude:: ../README.md

docs/make_format.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import csv
2+
from collections import defaultdict
3+
4+
# ensure all plugins are loaded!
5+
import dpdata.plugins
6+
from dpdata.format import Format
7+
from dpdata.system import get_cls_name
8+
9+
10+
def get_formats() -> dict:
11+
formats = defaultdict(list)
12+
for kk, ff in Format.get_formats().items():
13+
formats[ff].append(kk)
14+
return formats
15+
16+
def detect_overridden(cls: Format, method: str) -> bool:
17+
"""Check whether a method is override
18+
19+
Parameters
20+
----------
21+
cls : Format
22+
a format
23+
method : str
24+
method name
25+
26+
Returns
27+
-------
28+
bool
29+
whether a method is overridden
30+
"""
31+
return method in cls.__dict__
32+
33+
def get_cls_link(cls: object) -> str:
34+
"""Returns class link.
35+
36+
Parameters
37+
----------
38+
cls : object
39+
the class
40+
41+
Returns
42+
-------
43+
str
44+
the link of a class
45+
"""
46+
return ':class:`%s <%s>`' % (cls.__name__, ".".join([cls.__module__, cls.__name__]))
47+
48+
def check_supported(fmt: Format):
49+
methods = set()
50+
for mtd in [
51+
'from_system', 'to_system',
52+
'from_labeled_system', 'to_labeled_system',
53+
'from_bond_order_system', 'to_bond_order_system',
54+
'from_multi_systems', 'to_multi_systems',
55+
]:
56+
if detect_overridden(fmt, mtd):
57+
methods.add(mtd)
58+
if mtd == 'to_system':
59+
methods.add('to_labeled_system')
60+
if fmt.MultiMode != fmt.MultiModes.NotImplemented:
61+
methods.add('from_multi_systems')
62+
methods.add('to_multi_systems')
63+
return methods
64+
65+
method_links = {
66+
"from_system": ":func:`System() <dpdata.system.System>`",
67+
"to_system": ":func:`System.to() <dpdata.system.System.to>`",
68+
"from_labeled_system": ":func:`LabeledSystem() <dpdata.system.LabeledSystem>`",
69+
"to_labeled_system": ":func:`LabeledSystem.to() <dpdata.system.System.to>`",
70+
"from_bond_order_system": ":func:`BondOrderSystem() <dpdata.bond_order_system.BondOrderSystem>`",
71+
"to_bond_order_system": ":func:`BondOrderSystem.to() <dpdata.system.System.to>`",
72+
"from_multi_systems": ":func:`MultiSystems.load_systems_from_file() <dpdata.system.MultiSystems.load_systems_from_file>`",
73+
"to_multi_systems": ":func:`MultiSystems.to() <dpdata.system.MultiSystems.to>`",
74+
}
75+
76+
if __name__ == "__main__":
77+
formats = get_formats()
78+
with open('formats.csv', 'w', newline='') as csvfile:
79+
fieldnames = [
80+
'Class', 'Alias', 'Supported Functions',
81+
]
82+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
83+
84+
writer.writeheader()
85+
for kk, vv in formats.items():
86+
writer.writerow({
87+
'Class': get_cls_link(kk),
88+
'Alias': '\n'.join(('``%s``' % vvv for vvv in vv)),
89+
'Supported Functions': '\n'.join(method_links[mtd] for mtd in check_supported(kk)),
90+
})

dpdata/plugins/deepmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def from_labeled_system(self, file_name, type_map=None, **kwargs):
5858
MultiMode = Format.MultiModes.Directory
5959

6060
@Format.register("deepmd/hdf5")
61-
class DeePMDCompFormat(Format):
61+
class DeePMDHDF5Format(Format):
6262
"""HDF5 format for DeePMD-kit.
6363
6464
Examples

dpdata/plugins/qe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def from_labeled_system(self, file_name, begin = 0, step = 1, **kwargs):
2727
return data
2828

2929
@Format.register("qe/pw/scf")
30-
class QECPTrajFormat(Format):
30+
class QECPPWSCFFormat(Format):
3131
@Format.post("rot_lower_triangular")
3232
def from_labeled_system(self, file_name, **kwargs):
3333
data = {}

dpdata/plugins/siesta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def from_labeled_system(self, file_name, **kwargs):
3434

3535
@Format.register("siesta/aimd_output")
3636
@Format.register_from("from_siesta_aiMD_output")
37-
class SiestaOutputFormat(Format):
37+
class SiestaAIMDOutputFormat(Format):
3838
def from_system(self, file_name, **kwargs):
3939
data = {}
4040
data['atom_names'], \

dpdata/system.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,19 @@ def from_fmt_obj(self, fmtobj, file_name, **kwargs):
139139
self.post_funcs.get_plugin(post_f)(self)
140140
return self
141141

142-
def to(self, fmt, *args, **kwargs):
142+
def to(self, fmt: str, *args, **kwargs) -> 'System':
143+
"""Dump systems to the specific format.
144+
145+
Parameters
146+
----------
147+
fmt : str
148+
format
149+
150+
Returns
151+
-------
152+
System
153+
self
154+
"""
143155
return self.to_fmt_obj(load_format(fmt), *args, **kwargs)
144156

145157
def to_fmt_obj(self, fmtobj, *args, **kwargs):

0 commit comments

Comments
 (0)