Skip to content

Commit 6232559

Browse files
format codes with ruff (#448)
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 683180d commit 6232559

109 files changed

Lines changed: 382 additions & 425 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ repos:
2121
rev: 23.3.0
2222
hooks:
2323
- id: black-jupyter
24-
- repo: https://github.com/PyCQA/isort
25-
rev: 5.12.0
24+
- repo: https://github.com/charliermarsh/ruff-pre-commit
25+
# Ruff version.
26+
rev: v0.0.243
2627
hooks:
27-
- id: isort
28-
files: \.py$
28+
- id: ruff
29+
args: ["--fix"]
2930
# numpydoc
3031
- repo: https://github.com/Carreau/velin
3132
rev: 0.0.12

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Configuration file for the Sphinx documentation builder.
43
#

docs/make_format.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import csv
22
from collections import defaultdict
3-
from typing import Any
43

54
# ensure all plugins are loaded!
6-
import dpdata.plugins
75
from dpdata.driver import Driver, Minimizer
86
from dpdata.format import Format
9-
from dpdata.system import get_cls_name
107

118

129
def get_formats() -> dict:
@@ -31,7 +28,7 @@ def get_minimizer() -> dict:
3128

3229

3330
def detect_overridden(cls: Format, method: str) -> bool:
34-
"""Check whether a method is override
31+
"""Check whether a method is override.
3532
3633
Parameters
3734
----------
@@ -61,7 +58,9 @@ def get_cls_link(cls: object) -> str:
6158
str
6259
the link of a class
6360
"""
64-
return ":class:`%s <%s>`" % (cls.__name__, ".".join([cls.__module__, cls.__name__]))
61+
return ":class:`{} <{}>`".format(
62+
cls.__name__, ".".join([cls.__module__, cls.__name__])
63+
)
6564

6665

6766
def check_supported(fmt: Format):
@@ -112,7 +111,7 @@ def check_supported(fmt: Format):
112111
writer.writerow(
113112
{
114113
"Class": get_cls_link(kk),
115-
"Alias": "\n".join(("``%s``" % vvv for vvv in vv)),
114+
"Alias": "\n".join("``%s``" % vvv for vvv in vv),
116115
"Supported Functions": "\n".join(
117116
method_links[mtd] for mtd in check_supported(kk)
118117
),
@@ -132,7 +131,7 @@ def check_supported(fmt: Format):
132131
writer.writerow(
133132
{
134133
"Class": get_cls_link(kk),
135-
"Alias": "\n".join(("``%s``" % vvv for vvv in vv)),
134+
"Alias": "\n".join("``%s``" % vvv for vvv in vv),
136135
}
137136
)
138137

@@ -149,6 +148,6 @@ def check_supported(fmt: Format):
149148
writer.writerow(
150149
{
151150
"Class": get_cls_link(kk),
152-
"Alias": "\n".join(("``%s``" % vvv for vvv in vv)),
151+
"Alias": "\n".join("``%s``" % vvv for vvv in vv),
153152
}
154153
)

dpdata/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@
99
# BondOrder System has dependency on rdkit
1010
try:
1111
# prevent conflict with dpdata.rdkit
12-
import rdkit as _
12+
import rdkit as _ # noqa: F401
1313

1414
USE_RDKIT = True
1515
except ModuleNotFoundError:
1616
USE_RDKIT = False
1717

1818
if USE_RDKIT:
1919
from .bond_order_system import BondOrderSystem
20+
21+
__all__ = [
22+
"__version__",
23+
"lammps",
24+
"md",
25+
"vasp",
26+
"System",
27+
"LabeledSystem",
28+
"MultiSystems",
29+
"BondOrderSystem",
30+
]

dpdata/abacus/md.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import os
22
import re
3-
import sys
43
import warnings
5-
from ast import dump
64

75
import numpy as np
86

97
from .scf import (
108
bohr2ang,
11-
get_block,
129
get_cell,
1310
get_coords,
1411
get_geometry_in,
1512
kbar2evperang3,
16-
ry2ev,
1713
)
1814

1915
# Read in geometries from an ABACUS MD trajectory.
@@ -166,12 +162,12 @@ def get_frame(fname):
166162
path_in = os.path.join(fname, "INPUT")
167163
else:
168164
raise RuntimeError("invalid input")
169-
with open(path_in, "r") as fp:
165+
with open(path_in) as fp:
170166
inlines = fp.read().split("\n")
171167
geometry_path_in = get_geometry_in(fname, inlines) # base dir of STRU
172168
path_out = get_path_out(fname, inlines)
173169

174-
with open(geometry_path_in, "r") as fp:
170+
with open(geometry_path_in) as fp:
175171
geometry_inlines = fp.read().split("\n")
176172
celldm, cell = get_cell(geometry_inlines)
177173
atom_names, natoms, types, coords = get_coords(
@@ -182,11 +178,11 @@ def get_frame(fname):
182178
# ndump = int(os.popen("ls -l %s | grep 'md_pos_' | wc -l" %path_out).readlines()[0])
183179
# number of dumped geometry files
184180
# coords = get_coords_from_cif(ndump, dump_freq, atom_names, natoms, types, path_out, cell)
185-
with open(os.path.join(path_out, "MD_dump"), "r") as fp:
181+
with open(os.path.join(path_out, "MD_dump")) as fp:
186182
dumplines = fp.read().split("\n")
187183
coords, cells, force, stress = get_coords_from_dump(dumplines, natoms)
188184
ndump = np.shape(coords)[0]
189-
with open(os.path.join(path_out, "running_md.log"), "r") as fp:
185+
with open(os.path.join(path_out, "running_md.log")) as fp:
190186
outlines = fp.read().split("\n")
191187
energy = get_energy(outlines, ndump, dump_freq)
192188

@@ -201,7 +197,7 @@ def get_frame(fname):
201197
unconv_stru += "%d " % i
202198
ndump = len(energy)
203199
if unconv_stru != "":
204-
warnings.warn(f"Structure %s are unconverged and not collected!" % unconv_stru)
200+
warnings.warn("Structure %s are unconverged and not collected!" % unconv_stru)
205201

206202
for iframe in range(ndump):
207203
stress[iframe] *= np.linalg.det(cells[iframe, :, :].reshape([3, 3]))

dpdata/abacus/relax.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import sys
32

43
import numpy as np
54

@@ -16,19 +15,18 @@ def get_log_file(fname, inlines):
1615
suffix = line.split()[1]
1716
elif "calculation" in line and "calculation" == line.split()[0]:
1817
calculation = line.split()[1]
19-
logf = os.path.join(fname, "OUT.%s/running_%s.log" % (suffix, calculation))
18+
logf = os.path.join(fname, f"OUT.{suffix}/running_{calculation}.log")
2019
return logf
2120

2221

2322
def get_coords_from_log(loglines, natoms):
24-
"""
25-
NOTICE: unit of coords and cells is Angstrom
23+
"""NOTICE: unit of coords and cells is Angstrom
2624
order:
2725
coordinate
2826
cell (no output if cell is not changed)
2927
energy (no output, if SCF is not converged)
3028
force (no output, if cal_force is not setted or abnormal ending)
31-
stress (no output, if set cal_stress is not setted or abnormal ending)
29+
stress (no output, if set cal_stress is not setted or abnormal ending).
3230
"""
3331
natoms_log = 0
3432
for line in loglines:
@@ -175,10 +173,10 @@ def get_frame(fname):
175173
path_in = os.path.join(fname, "INPUT")
176174
else:
177175
raise RuntimeError("invalid input")
178-
with open(path_in, "r") as fp:
176+
with open(path_in) as fp:
179177
inlines = fp.read().split("\n")
180178
geometry_path_in = get_geometry_in(fname, inlines) # base dir of STRU
181-
with open(geometry_path_in, "r") as fp:
179+
with open(geometry_path_in) as fp:
182180
geometry_inlines = fp.read().split("\n")
183181
celldm, cell = get_cell(geometry_inlines)
184182
atom_names, natoms, types, coord_tmp = get_coords(

dpdata/abacus/scf.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import re
3-
import sys
43

54
import numpy as np
65

@@ -172,17 +171,17 @@ def get_frame(fname):
172171
if not CheckFile(path_in):
173172
return data
174173

175-
with open(path_in, "r") as fp:
174+
with open(path_in) as fp:
176175
inlines = fp.read().split("\n")
177176

178177
geometry_path_in = get_geometry_in(fname, inlines)
179178
path_out = get_path_out(fname, inlines)
180179
if not (CheckFile(geometry_path_in) and CheckFile(path_out)):
181180
return data
182181

183-
with open(geometry_path_in, "r") as fp:
182+
with open(geometry_path_in) as fp:
184183
geometry_inlines = fp.read().split("\n")
185-
with open(path_out, "r") as fp:
184+
with open(path_out) as fp:
186185
outlines = fp.read().split("\n")
187186

188187
celldm, cell = get_cell(geometry_inlines)
@@ -257,7 +256,7 @@ def get_nele_from_stru(geometry_inlines):
257256

258257
def get_frame_from_stru(fname):
259258
assert type(fname) == str
260-
with open(fname, "r") as fp:
259+
with open(fname) as fp:
261260
geometry_inlines = fp.read().split("\n")
262261
nele = get_nele_from_stru(geometry_inlines)
263262
inlines = ["ntype %d" % nele]

dpdata/amber/mask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""Amber mask"""
1+
"""Amber mask."""
22
try:
33
import parmed
44
except ImportError:
55
pass
66

77

88
def pick_by_amber_mask(param, maskstr, coords=None):
9-
"""Pick atoms by amber masks
9+
"""Pick atoms by amber masks.
1010
1111
Parameters
1212
----------

dpdata/amber/md.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def read_amber_traj(
3030
* mdfrc, NetCDF format, stores forces
3131
* mden (optional), text format, stores energies
3232
* mdout (optional), text format, may store energies if there is no mden_file
33-
* parm7, text format, stores types
33+
* parm7, text format, stores types.
3434
3535
Parameters
3636
----------
@@ -41,8 +41,9 @@ def read_amber_traj(
4141
instead of amber types. For example, a ligand will use C, H, O, N, and so on
4242
instead of h1, hc, o, os, and so on.
4343
IF use_element_symbols is str, it will be considered as Amber mask.
44+
labeled : bool
45+
Whether to return labeled data
4446
"""
45-
4647
flag_atom_type = False
4748
flag_atom_numb = False
4849
amber_types = []

dpdata/amber/sqm.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414

1515
def parse_sqm_out(fname):
16-
"""
17-
Read atom symbols, charges and coordinates from ambertools sqm.out file
18-
"""
16+
"""Read atom symbols, charges and coordinates from ambertools sqm.out file."""
1917
atom_symbols = []
2018
coords = []
2119
charges = []

0 commit comments

Comments
 (0)