From 644168dc8414199af615f594bb45bac3f38ba8ca Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 10 Apr 2023 20:48:01 -0400 Subject: [PATCH 1/9] format codes with ruff Signed-off-by: Jinzhe Zeng --- .pre-commit-config.yaml | 9 +- docs/conf.py | 1 - docs/make_format.py | 15 +- dpdata/__init__.py | 13 +- dpdata/abacus/md.py | 14 +- dpdata/abacus/relax.py | 12 +- dpdata/abacus/scf.py | 9 +- dpdata/amber/mask.py | 4 +- dpdata/amber/md.py | 5 +- dpdata/amber/sqm.py | 4 +- dpdata/bond_order_system.py | 41 ++--- dpdata/cli.py | 4 +- dpdata/cp2k/cell.py | 11 +- dpdata/cp2k/output.py | 12 +- dpdata/deepmd/hdf5.py | 2 +- dpdata/deepmd/mixed.py | 10 +- dpdata/fhi_aims/output.py | 2 +- dpdata/format.py | 19 ++- dpdata/gaussian/gjf.py | 18 +-- dpdata/lammps/dump.py | 2 +- dpdata/lammps/lmp.py | 2 +- dpdata/md/rdf.py | 3 +- dpdata/md/water.py | 3 - dpdata/periodic_table.py | 2 +- dpdata/plugins/3dmol.py | 2 + dpdata/plugins/abacus.py | 8 +- dpdata/plugins/amber.py | 13 +- dpdata/plugins/ase.py | 14 +- dpdata/plugins/cp2k.py | 4 +- dpdata/plugins/deepmd.py | 24 ++- dpdata/plugins/gaussian.py | 2 +- dpdata/plugins/gromacs.py | 14 +- dpdata/plugins/lammps.py | 5 +- dpdata/plugins/list.py | 4 +- dpdata/plugins/pwmat.py | 9 +- dpdata/plugins/pymatgen.py | 8 +- dpdata/plugins/rdkit.py | 4 +- dpdata/plugins/vasp.py | 16 +- dpdata/plugins/xyz.py | 2 +- dpdata/pymatgen/molecule.py | 2 - dpdata/qe/scf.py | 4 +- dpdata/qe/traj.py | 3 +- dpdata/rdkit/sanitize.py | 43 +++--- dpdata/rdkit/utils.py | 1 - dpdata/siesta/aiMD_output.py | 10 +- dpdata/siesta/output.py | 6 +- dpdata/system.py | 142 ++++++++---------- dpdata/unit.py | 30 ++-- dpdata/utils.py | 14 +- dpdata/vasp/poscar.py | 8 +- dpdata/vasp/xml.py | 22 ++- dpdata/xyz/quip_gap_xyz.py | 10 +- pyproject.toml | 30 ++++ tests/comp_sys.py | 2 +- tests/context.py | 6 - tests/poscars/test_lammps_dump_s_su.py | 1 - tests/test_abacus_stru_dump.py | 1 - tests/test_amber_md.py | 2 +- tests/test_amber_sqm.py | 6 +- tests/test_ase_traj.py | 6 +- tests/test_bond_order_system.py | 1 - tests/test_cell_to_low_triangle.py | 1 - tests/test_corr.py | 4 +- tests/test_cp2k_aimd_output.py | 2 - tests/test_cp2k_output.py | 2 - tests/test_deepmd_comp.py | 2 +- tests/test_deepmd_mixed.py | 3 +- tests/test_elements_index.py | 2 - tests/test_empty.py | 1 - tests/test_fhi_output.py | 1 - tests/test_gaussian_log.py | 1 - tests/test_gromacs_gro.py | 1 - tests/test_json.py | 2 - tests/test_lammps_dump_shift_origin.py | 2 - tests/test_lammps_dump_to_system.py | 1 - tests/test_lammps_dump_unfold.py | 1 - tests/test_lammps_lmp_dump.py | 1 - tests/test_lammps_lmp_to_system.py | 1 - tests/test_msd.py | 1 - tests/test_multisystems.py | 3 +- tests/test_perturb.py | 9 +- tests/test_pick_atom_idx.py | 5 +- tests/test_predict.py | 2 +- tests/test_pwmat_config_dump.py | 1 - tests/test_pwmat_movement.py | 1 - tests/test_pymatgen_molecule.py | 2 +- tests/test_qe_cp_traj.py | 1 - tests/test_qe_pw_scf.py | 1 - ...test_qe_pw_scf_crystal_atomic_positions.py | 1 - tests/test_qe_pw_scf_energy_bug.py | 2 - tests/test_quip_gap_xyz.py | 2 - tests/test_remove_outlier.py | 1 - tests/test_remove_pbc.py | 1 - tests/test_replace.py | 5 +- tests/test_replicate.py | 2 - tests/test_siesta_aiMD_output.py | 1 - tests/test_siesta_output.py | 1 - tests/test_system_append.py | 3 +- tests/test_system_apply_pbc.py | 1 - tests/test_system_set_type.py | 1 - tests/test_to_ase.py | 4 +- tests/test_to_list.py | 2 - tests/test_to_pymatgen.py | 3 - tests/test_to_pymatgen_entry.py | 4 - tests/test_vasp_outcar.py | 1 - tests/test_vasp_poscar_dump.py | 1 - tests/test_vasp_unconverged_outcar.py | 1 - tests/test_vasp_xml.py | 1 - tests/test_water_ions.py | 4 - 109 files changed, 366 insertions(+), 426 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fb478b13b..cf83ade3b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,11 +21,12 @@ repos: rev: 23.3.0 hooks: - id: black-jupyter -- repo: https://github.com/PyCQA/isort - rev: 5.12.0 +- repo: https://github.com/charliermarsh/ruff-pre-commit + # Ruff version. + rev: v0.0.243 hooks: - - id: isort - files: \.py$ + - id: ruff + args: ["--fix"] # numpydoc - repo: https://github.com/Carreau/velin rev: 0.0.12 diff --git a/docs/conf.py b/docs/conf.py index d09cf3fd5..e60f18fcb 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Configuration file for the Sphinx documentation builder. # diff --git a/docs/make_format.py b/docs/make_format.py index a7962208b..a208b21ed 100644 --- a/docs/make_format.py +++ b/docs/make_format.py @@ -1,12 +1,9 @@ import csv from collections import defaultdict -from typing import Any # ensure all plugins are loaded! -import dpdata.plugins from dpdata.driver import Driver, Minimizer from dpdata.format import Format -from dpdata.system import get_cls_name def get_formats() -> dict: @@ -31,7 +28,7 @@ def get_minimizer() -> dict: def detect_overridden(cls: Format, method: str) -> bool: - """Check whether a method is override + """Check whether a method is override. Parameters ---------- @@ -61,7 +58,9 @@ def get_cls_link(cls: object) -> str: str the link of a class """ - return ":class:`%s <%s>`" % (cls.__name__, ".".join([cls.__module__, cls.__name__])) + return ":class:`{} <{}>`".format( + cls.__name__, ".".join([cls.__module__, cls.__name__]) + ) def check_supported(fmt: Format): @@ -112,7 +111,7 @@ def check_supported(fmt: Format): writer.writerow( { "Class": get_cls_link(kk), - "Alias": "\n".join(("``%s``" % vvv for vvv in vv)), + "Alias": "\n".join("``%s``" % vvv for vvv in vv), "Supported Functions": "\n".join( method_links[mtd] for mtd in check_supported(kk) ), @@ -132,7 +131,7 @@ def check_supported(fmt: Format): writer.writerow( { "Class": get_cls_link(kk), - "Alias": "\n".join(("``%s``" % vvv for vvv in vv)), + "Alias": "\n".join("``%s``" % vvv for vvv in vv), } ) @@ -149,6 +148,6 @@ def check_supported(fmt: Format): writer.writerow( { "Class": get_cls_link(kk), - "Alias": "\n".join(("``%s``" % vvv for vvv in vv)), + "Alias": "\n".join("``%s``" % vvv for vvv in vv), } ) diff --git a/dpdata/__init__.py b/dpdata/__init__.py index d160717c3..036643c0d 100644 --- a/dpdata/__init__.py +++ b/dpdata/__init__.py @@ -9,7 +9,7 @@ # BondOrder System has dependency on rdkit try: # prevent conflict with dpdata.rdkit - import rdkit as _ + import rdkit as _ # noqa: F401 USE_RDKIT = True except ModuleNotFoundError: @@ -17,3 +17,14 @@ if USE_RDKIT: from .bond_order_system import BondOrderSystem + +__all__ = [ + "__version__", + "lammps", + "md", + "vasp", + "System", + "LabeledSystem", + "MultiSystems", + "BondOrderSystem", +] diff --git a/dpdata/abacus/md.py b/dpdata/abacus/md.py index 7532b7b2f..33cfa78b6 100644 --- a/dpdata/abacus/md.py +++ b/dpdata/abacus/md.py @@ -1,19 +1,15 @@ import os import re -import sys import warnings -from ast import dump import numpy as np from .scf import ( bohr2ang, - get_block, get_cell, get_coords, get_geometry_in, kbar2evperang3, - ry2ev, ) # Read in geometries from an ABACUS MD trajectory. @@ -166,12 +162,12 @@ def get_frame(fname): path_in = os.path.join(fname, "INPUT") else: raise RuntimeError("invalid input") - with open(path_in, "r") as fp: + with open(path_in) as fp: inlines = fp.read().split("\n") geometry_path_in = get_geometry_in(fname, inlines) # base dir of STRU path_out = get_path_out(fname, inlines) - with open(geometry_path_in, "r") as fp: + with open(geometry_path_in) as fp: geometry_inlines = fp.read().split("\n") celldm, cell = get_cell(geometry_inlines) atom_names, natoms, types, coords = get_coords( @@ -182,11 +178,11 @@ def get_frame(fname): # ndump = int(os.popen("ls -l %s | grep 'md_pos_' | wc -l" %path_out).readlines()[0]) # number of dumped geometry files # coords = get_coords_from_cif(ndump, dump_freq, atom_names, natoms, types, path_out, cell) - with open(os.path.join(path_out, "MD_dump"), "r") as fp: + with open(os.path.join(path_out, "MD_dump")) as fp: dumplines = fp.read().split("\n") coords, cells, force, stress = get_coords_from_dump(dumplines, natoms) ndump = np.shape(coords)[0] - with open(os.path.join(path_out, "running_md.log"), "r") as fp: + with open(os.path.join(path_out, "running_md.log")) as fp: outlines = fp.read().split("\n") energy = get_energy(outlines, ndump, dump_freq) @@ -201,7 +197,7 @@ def get_frame(fname): unconv_stru += "%d " % i ndump = len(energy) if unconv_stru != "": - warnings.warn(f"Structure %s are unconverged and not collected!" % unconv_stru) + warnings.warn("Structure %s are unconverged and not collected!" % unconv_stru) for iframe in range(ndump): stress[iframe] *= np.linalg.det(cells[iframe, :, :].reshape([3, 3])) diff --git a/dpdata/abacus/relax.py b/dpdata/abacus/relax.py index 7d0da0fa7..5dd77030b 100644 --- a/dpdata/abacus/relax.py +++ b/dpdata/abacus/relax.py @@ -1,5 +1,4 @@ import os -import sys import numpy as np @@ -16,19 +15,18 @@ def get_log_file(fname, inlines): suffix = line.split()[1] elif "calculation" in line and "calculation" == line.split()[0]: calculation = line.split()[1] - logf = os.path.join(fname, "OUT.%s/running_%s.log" % (suffix, calculation)) + logf = os.path.join(fname, f"OUT.{suffix}/running_{calculation}.log") return logf def get_coords_from_log(loglines, natoms): - """ - NOTICE: unit of coords and cells is Angstrom + """NOTICE: unit of coords and cells is Angstrom order: coordinate cell (no output if cell is not changed) energy (no output, if SCF is not converged) force (no output, if cal_force is not setted or abnormal ending) - stress (no output, if set cal_stress is not setted or abnormal ending) + stress (no output, if set cal_stress is not setted or abnormal ending). """ natoms_log = 0 for line in loglines: @@ -175,10 +173,10 @@ def get_frame(fname): path_in = os.path.join(fname, "INPUT") else: raise RuntimeError("invalid input") - with open(path_in, "r") as fp: + with open(path_in) as fp: inlines = fp.read().split("\n") geometry_path_in = get_geometry_in(fname, inlines) # base dir of STRU - with open(geometry_path_in, "r") as fp: + with open(geometry_path_in) as fp: geometry_inlines = fp.read().split("\n") celldm, cell = get_cell(geometry_inlines) atom_names, natoms, types, coord_tmp = get_coords( diff --git a/dpdata/abacus/scf.py b/dpdata/abacus/scf.py index e82089ad1..1816913e5 100644 --- a/dpdata/abacus/scf.py +++ b/dpdata/abacus/scf.py @@ -1,6 +1,5 @@ import os import re -import sys import numpy as np @@ -172,7 +171,7 @@ def get_frame(fname): if not CheckFile(path_in): return data - with open(path_in, "r") as fp: + with open(path_in) as fp: inlines = fp.read().split("\n") geometry_path_in = get_geometry_in(fname, inlines) @@ -180,9 +179,9 @@ def get_frame(fname): if not (CheckFile(geometry_path_in) and CheckFile(path_out)): return data - with open(geometry_path_in, "r") as fp: + with open(geometry_path_in) as fp: geometry_inlines = fp.read().split("\n") - with open(path_out, "r") as fp: + with open(path_out) as fp: outlines = fp.read().split("\n") celldm, cell = get_cell(geometry_inlines) @@ -257,7 +256,7 @@ def get_nele_from_stru(geometry_inlines): def get_frame_from_stru(fname): assert type(fname) == str - with open(fname, "r") as fp: + with open(fname) as fp: geometry_inlines = fp.read().split("\n") nele = get_nele_from_stru(geometry_inlines) inlines = ["ntype %d" % nele] diff --git a/dpdata/amber/mask.py b/dpdata/amber/mask.py index 34eba90bb..109cdf9c1 100644 --- a/dpdata/amber/mask.py +++ b/dpdata/amber/mask.py @@ -1,4 +1,4 @@ -"""Amber mask""" +"""Amber mask.""" try: import parmed except ImportError: @@ -6,7 +6,7 @@ def pick_by_amber_mask(param, maskstr, coords=None): - """Pick atoms by amber masks + """Pick atoms by amber masks. Parameters ---------- diff --git a/dpdata/amber/md.py b/dpdata/amber/md.py index 28b4535c9..ef0588289 100644 --- a/dpdata/amber/md.py +++ b/dpdata/amber/md.py @@ -30,7 +30,7 @@ def read_amber_traj( * mdfrc, NetCDF format, stores forces * mden (optional), text format, stores energies * mdout (optional), text format, may store energies if there is no mden_file - * parm7, text format, stores types + * parm7, text format, stores types. Parameters ---------- @@ -41,8 +41,9 @@ def read_amber_traj( instead of amber types. For example, a ligand will use C, H, O, N, and so on instead of h1, hc, o, os, and so on. IF use_element_symbols is str, it will be considered as Amber mask. + labeled : bool + Whether to return labeled data """ - flag_atom_type = False flag_atom_numb = False amber_types = [] diff --git a/dpdata/amber/sqm.py b/dpdata/amber/sqm.py index 9b4505017..5dcbf9955 100644 --- a/dpdata/amber/sqm.py +++ b/dpdata/amber/sqm.py @@ -13,9 +13,7 @@ def parse_sqm_out(fname): - """ - Read atom symbols, charges and coordinates from ambertools sqm.out file - """ + """Read atom symbols, charges and coordinates from ambertools sqm.out file.""" atom_symbols = [] coords = [] charges = [] diff --git a/dpdata/bond_order_system.py b/dpdata/bond_order_system.py index 55ec0fe28..e2449ee66 100644 --- a/dpdata/bond_order_system.py +++ b/dpdata/bond_order_system.py @@ -6,15 +6,14 @@ from rdkit.Chem import Conformer import dpdata.rdkit.utils -from dpdata.rdkit.sanitize import SanitizeError, Sanitizer -from dpdata.system import Axis, DataType, LabeledSystem, System, load_format +from dpdata.rdkit.sanitize import Sanitizer +from dpdata.system import Axis, DataType, System # import dpdata.rdkit.mol2 class BondOrderSystem(System): - """ - The system with chemical bond and formal charges information + """The system with chemical bond and formal charges information. For example, a labeled methane system named `d_example` has one molecule (5 atoms, 4 bonds) and `n_frames` frames. The bond order and formal charge information can be accessed by - `d_example['bonds']` : a numpy array of size 4 x 3, and @@ -44,8 +43,7 @@ def __init__( verbose=False, **kwargs, ): - """ - Constructor + """Constructor. Parameters ---------- @@ -73,8 +71,9 @@ def __init__( whether to raise an Exception if sanitization procedure fails. verbose : bool whether to print information in the sanitization procedure. + **kwargs : dict + Additional arguments for the format. """ - System.__init__(self) self.sanitizer = Sanitizer(sanitize_level, raise_errors, verbose) @@ -112,9 +111,7 @@ def to_fmt_obj(self, fmtobj, *args, **kwargs): return fmtobj.to_bond_order_system(self.data, self.rdkit_mol, *args, **kwargs) def __str__(self): - """ - A brief summary of the system - """ + """A brief summary of the system.""" ret = "Data Summary" ret += "\nBondOrder System" ret += "\n-------------------" @@ -128,33 +125,23 @@ def __str__(self): return ret def get_nbonds(self): - """ - Return the number of bonds - """ + """Return the number of bonds.""" return len(self.data["bonds"]) def get_charge(self): - """ - Return the total formal charge of the moleclue - """ + """Return the total formal charge of the moleclue.""" return sum(self.data["formal_charges"]) def get_mol(self): - """ - Return the rdkit.Mol object - """ + """Return the rdkit.Mol object.""" return self.rdkit_mol def get_bond_order(self, begin_atom_idx, end_atom_idx): - """ - Return the bond order between given atoms - """ + """Return the bond order between given atoms.""" return self.data["bond_dict"][f"{int(begin_atom_idx)}-{int(end_atom_idx)}"] def get_formal_charges(self): - """ - Return the formal charges on each atom - """ + """Return the formal charges on each atom.""" return self.data["formal_charges"] def copy(self): @@ -178,9 +165,7 @@ def __add__(self, other): # raise RuntimeError(f"Unsupported data structure: {type(other)}") def from_rdkit_mol(self, rdkit_mol): - """ - Initialize from a rdkit.Chem.rdchem.Mol object - """ + """Initialize from a rdkit.Chem.rdchem.Mol object.""" rdkit_mol = self.sanitizer.sanitize(rdkit_mol) self.data = dpdata.rdkit.utils.mol_to_system_data(rdkit_mol) self.data["bond_dict"] = dict( diff --git a/dpdata/cli.py b/dpdata/cli.py index 6bf7e5713..04850b80b 100644 --- a/dpdata/cli.py +++ b/dpdata/cli.py @@ -43,7 +43,7 @@ def dpdata_parser() -> argparse.ArgumentParser: def dpdata_cli(): - """dpdata cli. + """Dpdata cli. Examples -------- @@ -85,6 +85,8 @@ def convert( the system contains multiple directories type_map : list type map + **kwargs : dict + Additional arguments for the format. """ if multi: s = MultiSystems.from_file( diff --git a/dpdata/cp2k/cell.py b/dpdata/cp2k/cell.py index 0cd6151e3..7f5f3ef0e 100644 --- a/dpdata/cp2k/cell.py +++ b/dpdata/cp2k/cell.py @@ -1,13 +1,10 @@ # %% -import re -from collections import OrderedDict import numpy as np def cell_to_low_triangle(A, B, C, alpha, beta, gamma): - """ - Convert cell to low triangle matrix. + """Convert cell to low triangle matrix. Parameters ---------- @@ -51,11 +48,11 @@ def cell_to_low_triangle(A, B, C, alpha, beta, gamma): ) ) if not A > 0.2: - raise RuntimeError("A=={}, must be greater than 0.2".format(A)) + raise RuntimeError(f"A=={A}, must be greater than 0.2") if not B > 0.2: - raise RuntimeError("B=={}, must be greater than 0.2".format(B)) + raise RuntimeError(f"B=={B}, must be greater than 0.2") if not C > 0.2: - raise RuntimeError("C=={}, must be greater than 0.2".format(C)) + raise RuntimeError(f"C=={C}, must be greater than 0.2") lx = A xy = B * np.cos(gamma) diff --git a/dpdata/cp2k/output.py b/dpdata/cp2k/output.py index 04bd000da..13d5be7c7 100644 --- a/dpdata/cp2k/output.py +++ b/dpdata/cp2k/output.py @@ -26,14 +26,12 @@ avail_patterns.append(re.compile(r"^ ENSEMBLE TYPE")) -class Cp2kSystems(object): - """ - deal with cp2k outputfile - """ +class Cp2kSystems: + """deal with cp2k outputfile.""" def __init__(self, log_file_name, xyz_file_name, restart=False): - self.log_file_object = open(log_file_name, "r") - self.xyz_file_object = open(xyz_file_name, "r") + self.log_file_object = open(log_file_name) + self.xyz_file_object = open(xyz_file_name) self.log_block_generator = self.get_log_block_generator() self.xyz_block_generator = self.get_xyz_block_generator() self.restart_flag = restart @@ -333,7 +331,7 @@ def handle_single_xyz_frame(self, lines): atom_num, len(lines) ) ) - data_format_line = lines[1].strip("\n").strip() + str(" ") + data_format_line = lines[1].strip("\n").strip() + " " prop_pattern = re.compile(r"(?P\w+)\s*=\s*(?P.*?)[, ]") prop_dict = dict(prop_pattern.findall(data_format_line)) diff --git a/dpdata/deepmd/hdf5.py b/dpdata/deepmd/hdf5.py index 69b993abd..e7e1abe6a 100644 --- a/dpdata/deepmd/hdf5.py +++ b/dpdata/deepmd/hdf5.py @@ -101,7 +101,7 @@ def to_system_data( nframes = dd.shape[0] all_data.append(np.reshape(dd, (nframes, *prop["shape"]))) elif prop["required"]: - raise RuntimeError("%s/%s/%s not found" % (folder, ii, fn)) + raise RuntimeError(f"{folder}/{ii}/{fn} not found") if len(all_data) > 0: data[dt] = np.concatenate(all_data, axis=0) diff --git a/dpdata/deepmd/mixed.py b/dpdata/deepmd/mixed.py index fff8e930e..e8abce21b 100644 --- a/dpdata/deepmd/mixed.py +++ b/dpdata/deepmd/mixed.py @@ -19,12 +19,8 @@ def load_type(folder): def formula(atom_names, atom_numbs): - """ - Return the formula of this system, like C3H5O2 - """ - return "".join( - ["{}{}".format(symbol, numb) for symbol, numb in zip(atom_names, atom_numbs)] - ) + """Return the formula of this system, like C3H5O2.""" + return "".join([f"{symbol}{numb}" for symbol, numb in zip(atom_names, atom_numbs)]) def _cond_load_data(fname): @@ -225,6 +221,8 @@ def mix_system(*system, type_map, **kwargs): The systems to mix type_map : list of str Maps atom type to name + **kwargs : dict + Other parameters Returns ------- diff --git a/dpdata/fhi_aims/output.py b/dpdata/fhi_aims/output.py index 17526c182..c4cd92573 100755 --- a/dpdata/fhi_aims/output.py +++ b/dpdata/fhi_aims/output.py @@ -41,7 +41,7 @@ def get_info(lines, type_idx_zero=False): _atom_names.append(ii[0]) atom_names = [] for ii in _atom_names: - if not ii in atom_names: + if ii not in atom_names: atom_names.append(ii) atom_numbs = [_atom_names.count(ii) for ii in atom_names] diff --git a/dpdata/format.py b/dpdata/format.py index c6ba91b7b..149f2d9c6 100644 --- a/dpdata/format.py +++ b/dpdata/format.py @@ -1,7 +1,6 @@ """Implement the format plugin system.""" import os from abc import ABC -from collections import abc from .plugin import Plugin @@ -47,12 +46,14 @@ def decorator(object): return decorator def from_system(self, file_name, **kwargs): - """System.from + """System.from. Parameters ---------- file_name : str file name + **kwargs : dict + other parameters Returns ------- @@ -64,12 +65,16 @@ def from_system(self, file_name, **kwargs): ) def to_system(self, data, *args, **kwargs): - """System.to + """System.to. Parameters ---------- data : dict system data + *args : list + other parameters + **kwargs : dict + other parameters """ raise NotImplementedError( "%s doesn't support System.to" % (self.__class__.__name__) @@ -94,7 +99,7 @@ def to_bond_order_system(self, data, rdkit_mol, *args, **kwargs): class MultiModes: """File mode for MultiSystems 0 (default): not implemented - 1: every directory under the top-level directory is a system + 1: every directory under the top-level directory is a system. """ NotImplemented = 0 @@ -103,12 +108,14 @@ class MultiModes: MultiMode = MultiModes.NotImplemented def from_multi_systems(self, directory, **kwargs): - """MultiSystems.from + """MultiSystems.from. Parameters ---------- directory : str directory of system + **kwargs : dict + other parameters Returns ------- @@ -141,6 +148,8 @@ def mix_system(self, *system, type_map, **kwargs): The systems to mix type_map : list of str Maps atom type to name + **kwargs : dict + other parameters Returns ------- diff --git a/dpdata/gaussian/gjf.py b/dpdata/gaussian/gjf.py index 354187f01..c2051761c 100644 --- a/dpdata/gaussian/gjf.py +++ b/dpdata/gaussian/gjf.py @@ -217,23 +217,23 @@ def make_gaussian_input( buff = [] # keywords, e.g., force b3lyp/6-31g** if use_fragment_guesses: - keywords[0] = "{} guess=fragment={}".format(keywords[0], frag_numb) + keywords[0] = f"{keywords[0]} guess=fragment={frag_numb}" chkkeywords = [] if len(keywords) > 1: - chkkeywords.append("%chk={}.chk".format(str(uuid.uuid1()))) + chkkeywords.append(f"%chk={str(uuid.uuid1())}.chk") - nprockeywords = "%nproc={:d}".format(nproc) + nprockeywords = f"%nproc={nproc:d}" # use formula as title titlekeywords = "".join( - ["{}{}".format(symbol, numb) for symbol, numb in zip(atom_names, atom_numbs)] + [f"{symbol}{numb}" for symbol, numb in zip(atom_names, atom_numbs)] ) - chargekeywords = "{} {}".format(charge, multiplicity) + chargekeywords = f"{charge} {multiplicity}" buff = [ *chkkeywords, nprockeywords, - "#{}".format(keywords[0]), + f"#{keywords[0]}", "", titlekeywords, "", @@ -246,13 +246,13 @@ def make_gaussian_input( "%s(Fragment=%d) %f %f %f" % (symbol, frag_index[ii] + 1, *coordinate) ) else: - buff.append("%s %f %f %f" % (symbol, *coordinate)) + buff.append("{} {:f} {:f} {:f}".format(symbol, *coordinate)) if not sys_data.get("nopbc", False): # PBC condition cell = sys_data["cells"][0] for ii in range(3): # use TV as atomic symbol, see https://gaussian.com/pbc/ - buff.append("TV %f %f %f" % (*cell[ii],)) + buff.append("TV {:f} {:f} {:f}".format(*cell[ii])) if basis_set is not None: # custom basis set buff.extend(["", basis_set, ""]) @@ -262,7 +262,7 @@ def make_gaussian_input( "\n--link1--", *chkkeywords, nprockeywords, - "#{}".format(kw), + f"#{kw}", "", titlekeywords, "", diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index 2e4dd5611..cdc28bdcd 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -196,7 +196,7 @@ def system_data(lines, type_map=None, type_idx_zero=True, unwrap=False): system = {} system["atom_numbs"] = get_natoms_vec(lines) system["atom_names"] = [] - if type_map == None: + if type_map is None: for ii in range(len(system["atom_numbs"])): system["atom_names"].append("TYPE_%d" % ii) else: diff --git a/dpdata/lammps/lmp.py b/dpdata/lammps/lmp.py index 7f80fcc0b..317b30ed4 100644 --- a/dpdata/lammps/lmp.py +++ b/dpdata/lammps/lmp.py @@ -151,7 +151,7 @@ def system_data(lines, type_map=None, type_idx_zero=True): system = {} system["atom_numbs"] = get_natoms_vec(lines) system["atom_names"] = [] - if type_map == None: + if type_map is None: for ii in range(len(system["atom_numbs"])): system["atom_names"].append("Type_%d" % ii) else: diff --git a/dpdata/md/rdf.py b/dpdata/md/rdf.py index 4dcbece63..219543eb3 100644 --- a/dpdata/md/rdf.py +++ b/dpdata/md/rdf.py @@ -2,8 +2,7 @@ def rdf(sys, sel_type=[None, None], max_r=5, nbins=100): - """ - compute the rdf of a system + """Compute the rdf of a system. Parameters ---------- diff --git a/dpdata/md/water.py b/dpdata/md/water.py index 42e27243b..0cb82cc99 100644 --- a/dpdata/md/water.py +++ b/dpdata/md/water.py @@ -5,9 +5,6 @@ def compute_bonds(box, posis, atype, oh_sel=[0, 1], max_roh=1.3, uniq_hbond=True): try: - import ase - import ase.neighborlist - # nlist implemented by ase return compute_bonds_ase(box, posis, atype, oh_sel, max_roh, uniq_hbond) except ImportError: diff --git a/dpdata/periodic_table.py b/dpdata/periodic_table.py index 30ffb1be9..e6f16bc97 100644 --- a/dpdata/periodic_table.py +++ b/dpdata/periodic_table.py @@ -1,6 +1,6 @@ from pathlib import Path -from monty.serialization import dumpfn, loadfn +from monty.serialization import loadfn fpdt = str(Path(__file__).absolute().parent / "periodic_table.json") _pdt = loadfn(fpdt) diff --git a/dpdata/plugins/3dmol.py b/dpdata/plugins/3dmol.py index 68c3769b0..ec994dd9b 100644 --- a/dpdata/plugins/3dmol.py +++ b/dpdata/plugins/3dmol.py @@ -33,6 +33,8 @@ def to_system( (width, height) of the widget style : dict style of 3DMol. Read 3DMol documentation for details. + **kwargs : dict + other parameters Examples -------- diff --git a/dpdata/plugins/abacus.py b/dpdata/plugins/abacus.py index db983b7db..754221be0 100644 --- a/dpdata/plugins/abacus.py +++ b/dpdata/plugins/abacus.py @@ -11,11 +11,12 @@ def from_system(self, file_name, **kwargs): return dpdata.abacus.scf.get_frame_from_stru(file_name) def to_system(self, data, file_name, frame_idx=0, **kwargs): - """ - Dump the system into ABACUS STRU format file. + """Dump the system into ABACUS STRU format file. Parameters ---------- + data : dict + System data file_name : str The output file name frame_idx : int @@ -28,8 +29,9 @@ def to_system(self, data, file_name, frame_idx=0, **kwargs): List of atomic masses numerical_descriptor : str, optional numerical descriptor file + **kwargs : dict + other parameters """ - pp_file = kwargs.get("pp_file") numerical_orbital = kwargs.get("numerical_orbital") mass = kwargs.get("mass") diff --git a/dpdata/plugins/amber.py b/dpdata/plugins/amber.py index 4d6a22290..cdc92a30b 100644 --- a/dpdata/plugins/amber.py +++ b/dpdata/plugins/amber.py @@ -60,15 +60,11 @@ def from_labeled_system( @Format.register("sqm/out") class SQMOutFormat(Format): def from_system(self, fname, **kwargs): - """ - Read from ambertools sqm.out - """ + """Read from ambertools sqm.out.""" return dpdata.amber.sqm.parse_sqm_out(fname) def from_labeled_system(self, fname, **kwargs): - """ - Read from ambertools sqm.out - """ + """Read from ambertools sqm.out.""" data = dpdata.amber.sqm.parse_sqm_out(fname) assert "forces" in list(data.keys()), f"No forces in {fname}" return data @@ -77,8 +73,7 @@ def from_labeled_system(self, fname, **kwargs): @Format.register("sqm/in") class SQMINFormat(Format): def to_system(self, data, fname=None, frame_idx=0, **kwargs): - """ - Generate input files for semi-emperical calculation in sqm software + """Generate input files for semi-emperical calculation in sqm software. Parameters ---------- @@ -88,6 +83,8 @@ def to_system(self, data, fname=None, frame_idx=0, **kwargs): output file name frame_idx : int, default=0 index of frame to write + **kwargs : dict + other parameters Other Parameters ---------------- diff --git a/dpdata/plugins/ase.py b/dpdata/plugins/ase.py index f8dc4f8b2..1e68bf71e 100644 --- a/dpdata/plugins/ase.py +++ b/dpdata/plugins/ase.py @@ -34,6 +34,8 @@ def from_system(self, atoms: "ase.Atoms", **kwargs) -> dict: ---------- atoms : ase.Atoms an ASE Atoms, containing a structure + **kwargs : dict + other parameters Returns ------- @@ -67,6 +69,8 @@ def from_labeled_system(self, atoms: "ase.Atoms", **kwargs) -> dict: ---------- atoms : ase.Atoms an ASE Atoms, containing a structure + **kwargs : dict + other parameters Returns ------- @@ -124,6 +128,8 @@ def from_multi_systems( frame index step ase_fmt : str, optional ASE format. See the ASE documentation about supported formats + **kwargs : dict + other parameters Yields ------ @@ -131,14 +137,10 @@ def from_multi_systems( ASE atoms in the file """ frames = ase.io.read(file_name, format=ase_fmt, index=slice(begin, end, step)) - for atoms in frames: - yield atoms + yield from frames def to_system(self, data, **kwargs): - """ - convert System to ASE Atom obj - - """ + """convert System to ASE Atom obj.""" from ase import Atoms structures = [] diff --git a/dpdata/plugins/cp2k.py b/dpdata/plugins/cp2k.py index e1df43e58..1f09adaef 100644 --- a/dpdata/plugins/cp2k.py +++ b/dpdata/plugins/cp2k.py @@ -8,8 +8,8 @@ @Format.register("cp2k/aimd_output") class CP2KAIMDOutputFormat(Format): def from_labeled_system(self, file_name, restart=False, **kwargs): - xyz_file = sorted(glob.glob("{}/*pos*.xyz".format(file_name)))[0] - log_file = sorted(glob.glob("{}/*.log".format(file_name)))[0] + xyz_file = sorted(glob.glob(f"{file_name}/*pos*.xyz"))[0] + log_file = sorted(glob.glob(f"{file_name}/*.log"))[0] return tuple(Cp2kSystems(log_file, xyz_file, restart)) diff --git a/dpdata/plugins/deepmd.py b/dpdata/plugins/deepmd.py index 770d2f9ba..38eab85fd 100644 --- a/dpdata/plugins/deepmd.py +++ b/dpdata/plugins/deepmd.py @@ -22,7 +22,7 @@ def from_system(self, file_name, type_map=None, **kwargs): ) def to_system(self, data, file_name, **kwargs): - """Dump the system in deepmd raw format to directory `file_name`""" + """Dump the system in deepmd raw format to directory `file_name`.""" dpdata.deepmd.raw.dump(file_name, data) def from_labeled_system(self, file_name, type_map=None, **kwargs): @@ -42,8 +42,7 @@ def from_system(self, file_name, type_map=None, **kwargs): ) def to_system(self, data, file_name, set_size=5000, prec=np.float64, **kwargs): - """ - Dump the system in deepmd compressed format (numpy binary) to `folder`. + """Dump the system in deepmd compressed format (numpy binary) to `folder`. The frames are firstly split to sets, then dumped to seperated subfolders named as `folder/set.000`, `folder/set.001`, .... @@ -60,6 +59,8 @@ def to_system(self, data, file_name, set_size=5000, prec=np.float64, **kwargs): The size of each set. prec : {numpy.float32, numpy.float64} The floating point precision of the compressed data + **kwargs : dict + other parameters """ dpdata.deepmd.comp.dump(file_name, data, set_size=set_size, comp_prec=prec) @@ -95,8 +96,7 @@ def from_system_mix(self, file_name, type_map=None, **kwargs): ) def to_system(self, data, file_name, prec=np.float64, **kwargs): - """ - Dump the system in deepmd mixed type format (numpy binary) to `folder`. + """Dump the system in deepmd mixed type format (numpy binary) to `folder`. The frames were already split to different systems, so these frames can be dumped to one single subfolders named as `folder/set.000`, containing less than `set_size` frames. @@ -109,6 +109,8 @@ def to_system(self, data, file_name, prec=np.float64, **kwargs): The output folder prec : {numpy.float32, numpy.float64} The floating point precision of the compressed data + **kwargs : dict + other parameters """ dpdata.deepmd.mixed.dump(file_name, data, comp_prec=prec) @@ -126,6 +128,8 @@ def mix_system(self, *system, type_map, **kwargs): The systems to mix type_map : list of str Maps atom type to name + **kwargs : dict + other parameters Returns ------- @@ -216,6 +220,8 @@ def from_system( hashtag is used to split path to the HDF5 file and the HDF5 group type_map : dict[str] type map + **kwargs : dict + other parameters Returns ------- @@ -244,6 +250,8 @@ def from_labeled_system( hashtag is used to split path to the HDF5 file and the HDF5 group type_map : dict[str] type map + **kwargs : dict + other parameters Returns ------- @@ -278,6 +286,8 @@ def to_system( set size comp_prec : np.dtype data precision + **kwargs : dict + other parameters """ if isinstance(file_name, (h5py.Group, h5py.File)): dpdata.deepmd.hdf5.dump( @@ -301,6 +311,8 @@ def from_multi_systems(self, directory: str, **kwargs) -> h5py.Group: ---------- directory : str HDF5 file name + **kwargs : dict + other parameters Yields ------ @@ -322,6 +334,8 @@ def to_multi_systems( formulas of MultiSystems directory : str HDF5 file name + **kwargs : dict + other parameters Yields ------ diff --git a/dpdata/plugins/gaussian.py b/dpdata/plugins/gaussian.py index 416c61ad0..1ea097cc0 100644 --- a/dpdata/plugins/gaussian.py +++ b/dpdata/plugins/gaussian.py @@ -25,7 +25,7 @@ def from_labeled_system(self, file_name, **kwargs): @Format.register("gaussian/gjf") class GaussiaGJFFormat(Format): - """Gaussian input file""" + """Gaussian input file.""" def to_system(self, data: dict, file_name: str, **kwargs): """Generate Gaussian input file. diff --git a/dpdata/plugins/gromacs.py b/dpdata/plugins/gromacs.py index 6f19a27f0..20e508355 100644 --- a/dpdata/plugins/gromacs.py +++ b/dpdata/plugins/gromacs.py @@ -6,28 +6,34 @@ @Format.register("gromacs/gro") class GromacsGroFormat(Format): def from_system(self, file_name, format_atom_name=True, **kwargs): - """ - Load gromacs .gro file + """Load gromacs .gro file. Parameters ---------- file_name : str The input file name + format_atom_name : bool + Whether to format the atom name + **kwargs : dict + other parameters """ return dpdata.gromacs.gro.file_to_system_data( file_name, format_atom_name=format_atom_name, **kwargs ) def to_system(self, data, file_name=None, frame_idx=-1, **kwargs): - """ - Dump the system in gromacs .gro format + """Dump the system in gromacs .gro format. Parameters ---------- + data : dict + System data file_name : str or None The output file name. If None, return the file content as a string frame_idx : int The index of the frame to dump + **kwargs : dict + other parameters """ assert frame_idx < len(data["coords"]) if frame_idx == -1: diff --git a/dpdata/plugins/lammps.py b/dpdata/plugins/lammps.py index bd041d959..be89be9d0 100644 --- a/dpdata/plugins/lammps.py +++ b/dpdata/plugins/lammps.py @@ -13,8 +13,7 @@ def from_system(self, file_name, type_map=None, **kwargs): return dpdata.lammps.lmp.to_system_data(lines, type_map) def to_system(self, data, file_name, frame_idx=0, **kwargs): - """ - Dump the system in lammps data format + """Dump the system in lammps data format. Parameters ---------- @@ -24,6 +23,8 @@ def to_system(self, data, file_name, frame_idx=0, **kwargs): The output file name frame_idx : int The index of the frame to dump + **kwargs : dict + other parameters """ assert frame_idx < len(data["coords"]) w_str = dpdata.lammps.lmp.from_system_data(data, frame_idx) diff --git a/dpdata/plugins/list.py b/dpdata/plugins/list.py index f7880fa08..eb510da29 100644 --- a/dpdata/plugins/list.py +++ b/dpdata/plugins/list.py @@ -4,9 +4,7 @@ @Format.register("list") class ListFormat(Format): def to_system(self, data, **kwargs): - """ - convert system to list, usefull for data collection - """ + """convert system to list, usefull for data collection.""" from dpdata import LabeledSystem, System if "forces" in data: diff --git a/dpdata/plugins/pwmat.py b/dpdata/plugins/pwmat.py index 4d123f1e8..11257c4d0 100644 --- a/dpdata/plugins/pwmat.py +++ b/dpdata/plugins/pwmat.py @@ -51,15 +51,20 @@ def from_system(self, file_name, **kwargs): return dpdata.pwmat.atomconfig.to_system_data(lines) def to_system(self, data, file_name, frame_idx=0, *args, **kwargs): - """ - Dump the system in pwmat atom.config format + """Dump the system in pwmat atom.config format. Parameters ---------- + data : dict + The system data file_name : str The output file name frame_idx : int The index of the frame to dump + *args : list + other parameters + **kwargs : dict + other parameters """ assert frame_idx < len(data["coords"]) w_str = dpdata.pwmat.atomconfig.from_system_data(data, frame_idx) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 36efc12cf..f3aa6b274 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -7,7 +7,7 @@ @Format.register("pymatgen/structure") class PyMatgenStructureFormat(Format): def to_system(self, data, **kwargs): - """convert System to Pymatgen Structure obj""" + """convert System to Pymatgen Structure obj.""" structures = [] try: from pymatgen.core import Structure @@ -33,14 +33,14 @@ class PyMatgenMoleculeFormat(Format): @Format.post("remove_pbc") def from_system(self, file_name, **kwargs): try: - from pymatgen.core import Molecule + pass except ModuleNotFoundError as e: raise ImportError("No module pymatgen.Molecule") from e return dpdata.pymatgen.molecule.to_system_data(file_name) def to_system(self, data, **kwargs): - """convert System to Pymatgen Molecule obj""" + """convert System to Pymatgen Molecule obj.""" molecules = [] try: from pymatgen.core import Molecule @@ -61,7 +61,7 @@ def to_system(self, data, **kwargs): @Format.register_to("to_pymatgen_ComputedStructureEntry") class PyMatgenCSEFormat(Format): def to_labeled_system(self, data, *args, **kwargs): - """convert System to Pymagen ComputedStructureEntry obj""" + """convert System to Pymagen ComputedStructureEntry obj.""" try: from pymatgen.entries.computed_entries import ComputedStructureEntry except ModuleNotFoundError as e: diff --git a/dpdata/plugins/rdkit.py b/dpdata/plugins/rdkit.py index 64a2f343d..ff7638cb6 100644 --- a/dpdata/plugins/rdkit.py +++ b/dpdata/plugins/rdkit.py @@ -23,9 +23,7 @@ def to_bond_order_system(self, data, mol, file_name, frame_idx=0, **kwargs): @Format.register("sdf_file") class SdfFormat(Format): def from_bond_order_system(self, file_name, **kwargs): - """ - Note that it requires all molecules in .sdf file must be of the same topology - """ + """Note that it requires all molecules in .sdf file must be of the same topology.""" mols = [ m for m in rdkit.Chem.SDMolSupplier(file_name, sanitize=False, removeHs=False) diff --git a/dpdata/plugins/vasp.py b/dpdata/plugins/vasp.py index 6e2fe5f0a..76d34b9d5 100644 --- a/dpdata/plugins/vasp.py +++ b/dpdata/plugins/vasp.py @@ -4,7 +4,7 @@ import dpdata.vasp.poscar import dpdata.vasp.xml from dpdata.format import Format -from dpdata.utils import sort_atom_names, uniq_atom_names +from dpdata.utils import uniq_atom_names @Format.register("poscar") @@ -21,15 +21,18 @@ def from_system(self, file_name, **kwargs): return data def to_system(self, data, file_name, frame_idx=0, **kwargs): - """ - Dump the system in vasp POSCAR format + """Dump the system in vasp POSCAR format. Parameters ---------- + data : dict + The system data file_name : str The output file name frame_idx : int The index of the frame to dump + **kwargs : dict + other parameters """ w_str = VASPStringFormat().to_system(data, frame_idx=frame_idx) with open(file_name, "w") as fp: @@ -39,13 +42,16 @@ def to_system(self, data, file_name, frame_idx=0, **kwargs): @Format.register("vasp/string") class VASPStringFormat(Format): def to_system(self, data, frame_idx=0, **kwargs): - """ - Dump the system in vasp POSCAR format string + """Dump the system in vasp POSCAR format string. Parameters ---------- + data : dict + The system data frame_idx : int The index of the frame to dump + **kwargs : dict + other parameters """ assert frame_idx < len(data["coords"]) return dpdata.vasp.poscar.from_system_data(data, frame_idx) diff --git a/dpdata/plugins/xyz.py b/dpdata/plugins/xyz.py index 69cca61b4..fdb5bf3b1 100644 --- a/dpdata/plugins/xyz.py +++ b/dpdata/plugins/xyz.py @@ -23,7 +23,7 @@ def to_system(self, data, file_name, **kwargs): fp.write("\n".join(buff)) def from_system(self, file_name, **kwargs): - with open(file_name, "r") as fp: + with open(file_name) as fp: coords, types = xyz_to_coord(fp.read()) atom_names, atom_types, atom_numbs = np.unique( types, return_inverse=True, return_counts=True diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index 25e13c29f..13d4046c1 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -6,8 +6,6 @@ pass from collections import Counter -import dpdata - def to_system_data(file_name, protect_layer=9): mol = Molecule.from_file(file_name) diff --git a/dpdata/qe/scf.py b/dpdata/qe/scf.py index 72202358d..659b18458 100755 --- a/dpdata/qe/scf.py +++ b/dpdata/qe/scf.py @@ -140,9 +140,9 @@ def get_frame(fname): path_out = fname[1] else: raise RuntimeError("invalid input") - with open(path_out, "r") as fp: + with open(path_out) as fp: outlines = fp.read().split("\n") - with open(path_in, "r") as fp: + with open(path_in) as fp: inlines = fp.read().split("\n") cell = get_cell(inlines) atom_names, natoms, types, coords = get_coords(inlines, cell) diff --git a/dpdata/qe/traj.py b/dpdata/qe/traj.py index 700f2ebcf..7aa3b20b4 100644 --- a/dpdata/qe/traj.py +++ b/dpdata/qe/traj.py @@ -3,7 +3,6 @@ import numpy as np -import dpdata from ..unit import ( EnergyConversion, @@ -134,7 +133,7 @@ def load_data(fname, natoms, begin=0, step=1, convert=1.0): with open(fname) as fp: while True: blk, ss = _load_pos_block(fp, natoms) - if blk == None: + if blk is None: break else: if cc >= begin and (cc - begin) % step == 0: diff --git a/dpdata/rdkit/sanitize.py b/dpdata/rdkit/sanitize.py index 6a5a1c34e..061de3d9d 100644 --- a/dpdata/rdkit/sanitize.py +++ b/dpdata/rdkit/sanitize.py @@ -3,7 +3,7 @@ from copy import deepcopy from rdkit import Chem -from rdkit.Chem.rdchem import Atom, Bond, BondType, Mol +from rdkit.Chem.rdchem import BondType # openbabel try: @@ -31,9 +31,7 @@ def get_explicit_valence(atom, verbose=False): def regularize_formal_charges(mol, sanitize=True, verbose=False): - """ - Regularize formal charges of atoms - """ + """Regularize formal charges of atoms.""" assert isinstance(mol, Chem.rdchem.Mol) for atom in mol.GetAtoms(): assign_formal_charge_for_atom(atom, verbose) @@ -48,9 +46,7 @@ def regularize_formal_charges(mol, sanitize=True, verbose=False): def assign_formal_charge_for_atom(atom, verbose=False): - """ - assigen formal charge according to 8-electron rule for element B,C,N,O,S,P,As - """ + """Assigen formal charge according to 8-electron rule for element B,C,N,O,S,P,As.""" assert isinstance(atom, Chem.rdchem.Atom) valence = get_explicit_valence(atom, verbose) if atom.GetSymbol() == "B": @@ -630,19 +626,24 @@ def super_sanitize_mol(mol, name=None, verbose=True): return None -class Sanitizer(object): +class Sanitizer: def __init__(self, level="medium", raise_errors=True, verbose=False): - """ - Set up sanitizer. - -------- - Parameters: - level : 'low', 'medium' or 'high'. - `low` - use rdkit.Chem.SanitizeMol() to sanitize - `medium` - before using rdkit, assign formal charges of each atom first, which requires - the rightness of bond order information - `high` - try to regularize bond order of nitro, phosphate, sulfate, nitrine, guanidine, - pyridine-oxide function groups and aromatic heterocycles. If failed, the program - will call obabel to pre-process the mol object and re-try the procedure. + """Set up sanitizer. + --------. + + Parameters + ---------- + level : 'low', 'medium' or 'high'. + `low` - use rdkit.Chem.SanitizeMol() to sanitize + `medium` - before using rdkit, assign formal charges of each atom first, which requires + the rightness of bond order information + `high` - try to regularize bond order of nitro, phosphate, sulfate, nitrine, guanidine, + pyridine-oxide function groups and aromatic heterocycles. If failed, the program + will call obabel to pre-process the mol object and re-try the procedure. + raise_errors : bool, default=True + If True, raise SanitizeError when failed. + verbose : bool, default=False + If True, print error information when failed. """ self._check_level(level) self.level = level @@ -667,9 +668,7 @@ def _handle_exception(self, error_info): print(error_info) def sanitize(self, mol): - """ - Sanitize mol according to `self.level`. If failed, return None. - """ + """Sanitize mol according to `self.level`. If failed, return None.""" if self.level == "low": try: Chem.SanitizeMol(mol) diff --git a/dpdata/rdkit/utils.py b/dpdata/rdkit/utils.py index e5d1c7a8b..25cf97cd1 100644 --- a/dpdata/rdkit/utils.py +++ b/dpdata/rdkit/utils.py @@ -1,6 +1,5 @@ try: from rdkit import Chem - from rdkit.Chem import AllChem except ModuleNotFoundError: pass import numpy as np diff --git a/dpdata/siesta/aiMD_output.py b/dpdata/siesta/aiMD_output.py index c76ff8895..4e1890ecc 100644 --- a/dpdata/siesta/aiMD_output.py +++ b/dpdata/siesta/aiMD_output.py @@ -8,7 +8,7 @@ #############################read output##################################### def get_single_line_tail(fin, keyword, num=1): - file = open(fin, "r") + file = open(fin) part_res = [] for value in file: if keyword in value: @@ -32,7 +32,7 @@ def extract_keyword( is_repeated_read, column_num, ): - file = open(fout, "r") + file = open(fout) ret = [] part_ret = [] flag = 0 @@ -73,7 +73,7 @@ def extract_keyword( def obtain_nframe(fname): - fp = open(fname, "r") + fp = open(fname) flag = False idx = 0 temp = 0 @@ -82,7 +82,7 @@ def obtain_nframe(fname): flag = True continue if flag: - if not "siesta: Pressure (static):" in ii: + if "siesta: Pressure (static):" not in ii: if len(ii.split()) == 3: temp += 1 if temp == 3: @@ -106,7 +106,7 @@ def get_atom_types(fout, atomnums): def get_atom_name(fout): - file = open(fout, "r") + file = open(fout) ret = [] for value in file: if "Species number:" in value: diff --git a/dpdata/siesta/output.py b/dpdata/siesta/output.py index b70fe55db..7418d5433 100644 --- a/dpdata/siesta/output.py +++ b/dpdata/siesta/output.py @@ -8,7 +8,7 @@ #############################read output##################################### def get_single_line_tail(fin, keyword, num=1): - file = open(fin, "r") + file = open(fin) res = [] for value in file: if keyword in value: @@ -23,7 +23,7 @@ def get_single_line_tail(fin, keyword, num=1): ## begin_column: begin column num ## column_num: read column num def extract_keyword(fout, keyword, down_line_num, begin_column, column_num): - file = open(fout, "r") + file = open(fout) ret = [] flag = 0 idx = 0 @@ -63,7 +63,7 @@ def get_atom_types(fout, atomnums): def get_atom_name(fout): - file = open(fout, "r") + file = open(fout) ret = [] for value in file: if "Species number:" in value: diff --git a/dpdata/system.py b/dpdata/system.py index 29c7a2e30..9408e4cca 100644 --- a/dpdata/system.py +++ b/dpdata/system.py @@ -1,6 +1,5 @@ # %% import glob -import inspect import os from copy import deepcopy from enum import Enum, unique @@ -18,7 +17,6 @@ from dpdata.amber.mask import load_param_file, pick_by_amber_mask from dpdata.driver import Driver, Minimizer from dpdata.format import Format -from dpdata.periodic_table import Element from dpdata.plugin import Plugin from dpdata.utils import add_atom_names, elements_index_map, remove_pbc, sort_atom_names @@ -29,7 +27,9 @@ def load_format(fmt): if fmt in formats: return formats[fmt]() raise NotImplementedError( - "Unsupported data format %s. Supported formats: %s" % (fmt, " ".join(formats)) + "Unsupported data format {}. Supported formats: {}".format( + fmt, " ".join(formats) + ) ) @@ -142,8 +142,7 @@ def check(self, system: "System"): class System(MSONable): - """ - The data System + """The data System. A data System (a concept used by `deepmd-kit `_) contains frames (e.g. produced by an MD simulation) that has the same number of atoms of the same type. @@ -196,8 +195,7 @@ def __init__( convergence_check=True, **kwargs, ): - """ - Constructor + """Constructor. Parameters ---------- @@ -265,6 +263,8 @@ def __init__( The raw data of System class. convergence_check : boolean Whether to request a convergence check. + **kwargs : dict + other parameters """ self.data = {} self.data["atom_numbs"] = [] @@ -340,6 +340,10 @@ def to(self, fmt: str, *args, **kwargs) -> "System": ---------- fmt : str format + *args + arguments + **kwargs + keyword arguments Returns ------- @@ -367,17 +371,17 @@ def __str__(self): return ret def __getitem__(self, key): - """Returns proerty stored in System by key or by idx""" + """Returns proerty stored in System by key or by idx.""" if isinstance(key, (int, slice, list, np.ndarray)): return self.sub_system(key) return self.data[key] def __len__(self): - """Returns number of frames in the system""" + """Returns number of frames in the system.""" return self.get_nframes() def __add__(self, others): - """magic method "+" operation""" + """magic method "+" operation.""" self_copy = self.copy() if isinstance(others, System): other_copy = others.copy() @@ -392,12 +396,11 @@ def __add__(self, others): return self.__class__.from_dict({"data": self_copy.data}) def dump(self, filename, indent=4): - """dump .json or .yaml file""" + """dump .json or .yaml file.""" dumpfn(self.as_dict(), filename, indent=indent) def map_atom_types(self, type_map=None) -> np.ndarray: - """ - Map the atom types of the system + """Map the atom types of the system. Parameters ---------- @@ -436,11 +439,11 @@ def map_atom_types(self, type_map=None) -> np.ndarray: @staticmethod def load(filename): - """rebuild System obj. from .json or .yaml file""" + """rebuild System obj. from .json or .yaml file.""" return loadfn(filename) def as_dict(self): - """Returns data dict of System instance""" + """Returns data dict of System instance.""" d = { "@module": self.__class__.__module__, "@class": self.__class__.__name__, @@ -449,23 +452,23 @@ def as_dict(self): return d def get_atom_names(self): - """Returns name of atoms""" + """Returns name of atoms.""" return self.data["atom_names"] def get_atom_types(self): - """Returns type of atoms""" + """Returns type of atoms.""" return self.data["atom_types"] def get_atom_numbs(self): - """Returns number of atoms""" + """Returns number of atoms.""" return self.data["atom_numbs"] def get_nframes(self): - """Returns number of frames in the system""" + """Returns number of frames in the system.""" return len(self.data["cells"]) def get_natoms(self): - """Returns total number of atoms in the system""" + """Returns total number of atoms in the system.""" return len(self.data["atom_types"]) def get_ntypes(self) -> int: @@ -477,8 +480,7 @@ def copy(self): return self.__class__.from_dict({"data": deepcopy(self.data)}) def sub_system(self, f_idx): - """ - Construct a subsystem from the system + """Construct a subsystem from the system. Parameters ---------- @@ -509,8 +511,7 @@ def sub_system(self, f_idx): return tmp def append(self, system): - """ - Append a system to this system + """Append a system to this system. Parameters ---------- @@ -563,8 +564,7 @@ def append(self, system): return True def convert_to_mixed_type(self, type_map=None): - """ - Convert the data dict to mixed type format structure, in order to append systems + """Convert the data dict to mixed type format structure, in order to append systems with different formula but the same number of atoms. Change the 'atom_names' to one placeholder type 'MIXED_TOKEN' and add 'real_atom_types' to store the real type vectors according to the given type_map. @@ -590,8 +590,7 @@ def convert_to_mixed_type(self, type_map=None): self.data["atom_names"] = ["MIXED_TOKEN"] def sort_atom_names(self, type_map=None): - """ - Sort atom_names of the system and reorder atom_numbs and atom_types accoarding + """Sort atom_names of the system and reorder atom_numbs and atom_types accoarding to atom_names. If type_map is not given, atom_names will be sorted by alphabetical order. If type_map is given, atom_names will be type_map. @@ -603,8 +602,7 @@ def sort_atom_names(self, type_map=None): self.data = sort_atom_names(self.data, type_map=type_map) def check_type_map(self, type_map): - """ - Assign atom_names to type_map if type_map is given and different from + """Assign atom_names to type_map if type_map is given and different from atom_names. Parameters @@ -643,12 +641,10 @@ def sort_atom_types(self) -> np.ndarray: @property def formula(self): - """ - Return the formula of this system, like C3H5O2 - """ + """Return the formula of this system, like C3H5O2.""" return "".join( [ - "{}{}".format(symbol, numb) + f"{symbol}{numb}" for symbol, numb in zip( self.data["atom_names"], self.data["atom_numbs"] ) @@ -657,14 +653,13 @@ def formula(self): @property def uniq_formula(self): - """ - Return the uniq_formula of this system. + """Return the uniq_formula of this system. The uniq_formula sort the elements in formula by names. Systems with the same uniq_formula can be append together. """ return "".join( [ - "{}{}".format(symbol, numb) + f"{symbol}{numb}" for symbol, numb in sorted( zip(self.data["atom_names"], self.data["atom_numbs"]) ) @@ -672,30 +667,25 @@ def uniq_formula(self): ) def extend(self, systems): - """ - Extend a system list to this system + """Extend a system list to this system. Parameters ---------- systems : [System1, System2, System3 ] The list to extend """ - for system in systems: self.append(system.copy()) def apply_pbc(self): - """ - Append periodic boundary condition - """ + """Append periodic boundary condition.""" ncoord = dpdata.md.pbc.dir_coord(self.data["coords"], self.data["cells"]) ncoord = ncoord % 1 self.data["coords"] = np.matmul(ncoord, self.data["cells"]) @post_funcs.register("remove_pbc") def remove_pbc(self, protect_layer=9): - """ - This method does NOT delete the definition of the cells, it + """This method does NOT delete the definition of the cells, it (1) revises the cell to a cubic cell and ensures that the cell boundary to any atom in the system is no less than `protect_layer` (2) translates the system such that the center-of-geometry of the system @@ -745,14 +735,11 @@ def rot_frame_lower_triangular(self, f_idx=0): return np.matmul(qq, rot) def add_atom_names(self, atom_names): - """ - Add atom_names that do not exist. - """ + """Add atom_names that do not exist.""" self.data = add_atom_names(self.data, atom_names) def replicate(self, ncopy): - """ - Replicate the each frame in the system in 3 dimensions. + """Replicate the each frame in the system in 3 dimensions. Each frame in the system will become a supercell. Parameters @@ -862,8 +849,7 @@ def replace(self, initial_atom_type, end_atom_type, replace_num): def perturb( self, pert_num, cell_pert_fraction, atom_pert_distance, atom_pert_style="normal" ): - """ - Perturb each frame in the system randomly. + """Perturb each frame in the system randomly. The cell will be deformed randomly, and atoms will be displaced by a random distance in random direction. Parameters @@ -939,8 +925,7 @@ def shuffle(self): return idx def predict(self, *args: Any, driver: str = "dp", **kwargs: Any) -> "LabeledSystem": - """ - Predict energies and forces by a driver. + """Predict energies and forces by a driver. Parameters ---------- @@ -991,7 +976,7 @@ def minimize( return LabeledSystem(data=data) def pick_atom_idx(self, idx, nopbc=None): - """Pick atom index + """Pick atom index. Parameters ---------- @@ -1053,7 +1038,7 @@ def remove_atom_names(self, atom_names): return new_sys def pick_by_amber_mask(self, param, maskstr, pass_coords=False, nopbc=None): - """Pick atoms by amber mask + """Pick atoms by amber mask. Parameters ---------- @@ -1120,15 +1105,12 @@ def get_atom_perturb_vector(atom_pert_distance, atom_pert_style="normal"): random_unit_vector = e / np.linalg.norm(e) random_vector = atom_pert_distance * random_unit_vector else: - raise RuntimeError( - "unsupported options atom_pert_style={}".format(atom_pert_style) - ) + raise RuntimeError(f"unsupported options atom_pert_style={atom_pert_style}") return random_vector class LabeledSystem(System): - """ - The labeled data System + """The labeled data System. For example, a labeled water system named `d_example` has two molecules (6 atoms) and `nframes` frames. The labels can be accessed by - `d_example['energies']` : a numpy array of size nframes @@ -1139,8 +1121,8 @@ class LabeledSystem(System): - The order of frames stored in `'energies'`, `'forces'` and `'virials'` should be consistent with `'atom_types'`, `'cells'` and `'coords'`. - The order of atoms in **every** frame of `'forces'` should be consistent with `'coords'` and `'atom_types'`. - Parameters - ---------- + Parameters + ---------- file_name : str The file to load the system fmt : str @@ -1211,7 +1193,7 @@ def __str__(self): return ret def __add__(self, others): - """magic method "+" operation""" + """magic method "+" operation.""" self_copy = self.copy() if isinstance(others, LabeledSystem): other_copy = others.copy() @@ -1315,8 +1297,7 @@ class MultiSystems: """A set containing several systems.""" def __init__(self, *systems, type_map=None): - """ - Parameters + """Parameters ---------- *systems : System The systems contained @@ -1378,6 +1359,10 @@ def to(self, fmt: str, *args, **kwargs) -> "MultiSystems": ---------- fmt : str format + *args : list + arguments + **kwargs : dict + keyword arguments Returns ------- @@ -1387,7 +1372,7 @@ def to(self, fmt: str, *args, **kwargs) -> "MultiSystems": return self.to_fmt_obj(load_format(fmt), *args, **kwargs) def __getitem__(self, key): - """Returns proerty stored in System by key or by idx""" + """Returns proerty stored in System by key or by idx.""" if isinstance(key, int): return list(self.systems.values())[key] return self.systems[key] @@ -1404,7 +1389,7 @@ def __str__(self): ) def __add__(self, others): - """magic method "+" operation""" + """magic method "+" operation.""" self_copy = deepcopy(self) if isinstance(others, System) or isinstance(others, MultiSystems): return self.__class__(self, others) @@ -1422,7 +1407,7 @@ def from_file(cls, file_name, fmt, **kwargs): def from_dir(cls, dir_name, file_name, fmt="auto", type_map=None): multi_systems = cls() target_file_list = sorted( - glob.glob("./{}/**/{}".format(dir_name, file_name), recursive=True) + glob.glob(f"./{dir_name}/**/{file_name}", recursive=True) ) for target_file in target_file_list: multi_systems.append( @@ -1435,16 +1420,15 @@ def load_systems_from_file(self, file_name=None, fmt=None, **kwargs): return self.from_fmt_obj(load_format(fmt), file_name, **kwargs) def get_nframes(self): - """Returns number of frames in all systems""" + """Returns number of frames in all systems.""" return sum(len(system) for system in self.systems.values()) def append(self, *systems): - """ - Append systems or MultiSystems to systems + """Append systems or MultiSystems to systems. Parameters ---------- - system : System + *systems : System The system to append """ for system in systems: @@ -1467,9 +1451,7 @@ def __append(self, system): self.systems[formula] = system.copy() def check_atom_names(self, system): - """ - Make atom_names in all systems equal, prevent inconsistent atom_types. - """ + """Make atom_names in all systems equal, prevent inconsistent atom_types.""" # new_in_system = set(system["atom_names"]) - set(self.atom_names) # new_in_self = set(self.atom_names) - set(system["atom_names"]) new_in_system = [e for e in system["atom_names"] if e not in self.atom_names] @@ -1490,8 +1472,7 @@ def check_atom_names(self, system): system.sort_atom_names(type_map=self.atom_names) def predict(self, *args: Any, driver="dp", **kwargs: Any) -> "MultiSystems": - """ - Predict energies and forces by a driver. + """Predict energies and forces by a driver. Parameters ---------- @@ -1517,8 +1498,7 @@ def predict(self, *args: Any, driver="dp", **kwargs: Any) -> "MultiSystems": def minimize( self, *args: Any, minimizer: Union[str, Minimizer], **kwargs: Any ) -> "MultiSystems": - """ - Minimize geometry by a minimizer. + """Minimize geometry by a minimizer. Parameters ---------- @@ -1550,7 +1530,7 @@ def minimize( return new_multisystems def pick_atom_idx(self, idx, nopbc=None): - """Pick atom index + """Pick atom index. Parameters ---------- diff --git a/dpdata/unit.py b/dpdata/unit.py index fa0d1e246..eba07b412 100644 --- a/dpdata/unit.py +++ b/dpdata/unit.py @@ -43,14 +43,16 @@ def check_unit(unit): class Conversion(ABC): def __init__(self, unitA, unitB, check=True): - """ - Parent class for unit conversion + """Parent class for unit conversion. Parameters ---------- - unitA : str, unit to be converted - unitB : str, unit which unitA is converted to, i.e. `1 unitA = self._value unitB` - check : bool, whether to check unit validity + unitA : str + unit to be converted + unitB : str + unit which unitA is converted to, i.e. `1 unitA = self._value unitB` + check : bool + whether to check unit validity Examples -------- @@ -83,8 +85,7 @@ def __str__(self): class EnergyConversion(Conversion): def __init__(self, unitA, unitB): - """ - Class for energy conversion + """Class for energy conversion. Examples -------- @@ -98,8 +99,7 @@ def __init__(self, unitA, unitB): class LengthConversion(Conversion): def __init__(self, unitA, unitB): - """ - Class for length conversion + """Class for length conversion. Examples -------- @@ -113,12 +113,12 @@ def __init__(self, unitA, unitB): class ForceConversion(Conversion): def __init__(self, unitA, unitB): - """ - Class for force conversion + """Class for force conversion. Parameters ---------- - unitA, unitB : str, in format of "energy_unit/length_unit" + unitA, unitB : str + in format of "energy_unit/length_unit" Examples -------- @@ -134,12 +134,12 @@ def __init__(self, unitA, unitB): class PressureConversion(Conversion): def __init__(self, unitA, unitB): - """ - Class for pressure conversion + """Class for pressure conversion. Parameters ---------- - unitA, unitB : str, in format of "energy_unit/length_unit^3", or in `["Pa", "pa", "kPa", "kpa", "bar", "kbar"]` + unitA, unitB : str + in format of "energy_unit/length_unit^3", or in `["Pa", "pa", "kPa", "kpa", "bar", "kbar"]` Examples -------- diff --git a/dpdata/utils.py b/dpdata/utils.py index c461d8c09..55cf41ef5 100644 --- a/dpdata/utils.py +++ b/dpdata/utils.py @@ -32,22 +32,21 @@ def remove_pbc(system, protect_layer=9): def add_atom_names(data, atom_names): - """ - Add atom_names that do not exist. - """ + """Add atom_names that do not exist.""" data["atom_names"].extend(atom_names) data["atom_numbs"].extend([0 for _ in atom_names]) return data def sort_atom_names(data, type_map=None): - """ - Sort atom_names of the system and reorder atom_numbs and atom_types accoarding + """Sort atom_names of the system and reorder atom_numbs and atom_types accoarding to atom_names. If type_map is not given, atom_names will be sorted by alphabetical order. If type_map is given, atom_names will be type_map. Parameters ---------- + data : dict + system data type_map : list type_map """ @@ -76,9 +75,8 @@ def sort_atom_names(data, type_map=None): def uniq_atom_names(data): - """ - Make the atom names uniq. For example - ['O', 'H', 'O', 'H', 'O'] -> ['O', 'H'] + """Make the atom names uniq. For example + ['O', 'H', 'O', 'H', 'O'] -> ['O', 'H']. Parameters ---------- diff --git a/dpdata/vasp/poscar.py b/dpdata/vasp/poscar.py index 1100eb852..95f9c15ed 100644 --- a/dpdata/vasp/poscar.py +++ b/dpdata/vasp/poscar.py @@ -4,9 +4,7 @@ def _to_system_data_lower(lines, cartesian=True): - """ - Treat as cartesian poscar - """ + """Treat as cartesian poscar.""" system = {} system["atom_names"] = [str(ii) for ii in lines[5].split()] system["atom_numbs"] = [int(ii) for ii in lines[6].split()] @@ -44,7 +42,7 @@ def to_system_data(lines): lines.pop(7) is_cartesian = lines[7][0] in ["C", "c", "K", "k"] if not is_cartesian: - if not (lines[7][0] in ["d", "D"]): + if lines[7][0] not in ["d", "D"]: raise RuntimeError( "seem not to be a valid POSCAR of vasp 5.x, may be a POSCAR of vasp 4.x?" ) @@ -84,7 +82,7 @@ def from_system_data(system, f_idx=0, skip_zeros=True): posis = posis[sort_idx] posi_list = [] for ii in posis: - posi_list.append("%15.10f %15.10f %15.10f" % (ii[0], ii[1], ii[2])) + posi_list.append(f"{ii[0]:15.10f} {ii[1]:15.10f} {ii[2]:15.10f}") posi_list.append("") ret += "\n".join(posi_list) return ret diff --git a/dpdata/vasp/xml.py b/dpdata/vasp/xml.py index 9d4cb8033..062a8f28a 100755 --- a/dpdata/vasp/xml.py +++ b/dpdata/vasp/xml.py @@ -8,7 +8,7 @@ def check_name(item, name): assert ( item.attrib["name"] == name - ), "item attrib '%s' dose not math required '%s'" % (item.attrib["name"], name) + ), "item attrib '{}' dose not math required '{}'".format(item.attrib["name"], name) def get_varray(varray): @@ -23,11 +23,11 @@ def analyze_atominfo(atominfo_xml): eles = [] types = [] for ii in atominfo_xml.find("array").find("set"): - eles.append((ii.findall("c")[0].text.strip())) + eles.append(ii.findall("c")[0].text.strip()) types.append(int(ii.findall("c")[1].text)) uniq_ele = [] for ii in eles: - if not (ii in uniq_ele): + if ii not in uniq_ele: uniq_ele.append(ii) return uniq_ele, types @@ -63,12 +63,12 @@ def formulate_config(eles, types, posi, cell, ener, forc, strs_): ret += " " + ii ret += "\n" ret += "##\n" - ret += "#X %13.8f %13.8f %13.8f\n" % (cell[0][0], cell[0][1], cell[0][2]) - ret += "#Y %13.8f %13.8f %13.8f\n" % (cell[1][0], cell[1][1], cell[1][2]) - ret += "#Z %13.8f %13.8f %13.8f\n" % (cell[2][0], cell[2][1], cell[2][2]) + ret += f"#X {cell[0][0]:13.8f} {cell[0][1]:13.8f} {cell[0][2]:13.8f}\n" + ret += f"#Y {cell[1][0]:13.8f} {cell[1][1]:13.8f} {cell[1][2]:13.8f}\n" + ret += f"#Z {cell[2][0]:13.8f} {cell[2][1]:13.8f} {cell[2][2]:13.8f}\n" ret += "#W 1.0\n" ret += "#E %.10f\n" % (ener / natoms) - ret += "#S %.9e %.9e %.9e %.9e %.9e %.9e\n" % ( + ret += "#S {:.9e} {:.9e} {:.9e} {:.9e} {:.9e} {:.9e}\n".format( strs[0][0], strs[1][1], strs[2][2], @@ -80,16 +80,14 @@ def formulate_config(eles, types, posi, cell, ener, forc, strs_): for ii in range(natoms): sp = np.matmul(cell.T, posi[ii]) ret += "%d" % (types[ii] - 1) - ret += " %12.6f %12.6f %12.6f" % (sp[0], sp[1], sp[2]) - ret += " %12.6f %12.6f %12.6f" % (forc[ii][0], forc[ii][1], forc[ii][2]) + ret += f" {sp[0]:12.6f} {sp[1]:12.6f} {sp[2]:12.6f}" + ret += f" {forc[ii][0]:12.6f} {forc[ii][1]:12.6f} {forc[ii][2]:12.6f}" ret += "\n" return ret def analyze(fname, type_idx_zero=False, begin=0, step=1): - """ - can deal with broken xml file - """ + """Deal with broken xml file.""" all_posi = [] all_cell = [] all_ener = [] diff --git a/dpdata/xyz/quip_gap_xyz.py b/dpdata/xyz/quip_gap_xyz.py index dceeade20..c2d4118c4 100644 --- a/dpdata/xyz/quip_gap_xyz.py +++ b/dpdata/xyz/quip_gap_xyz.py @@ -6,13 +6,11 @@ import numpy as np -class QuipGapxyzSystems(object): - """ - deal with QuipGapxyzFile - """ +class QuipGapxyzSystems: + """deal with QuipGapxyzFile.""" def __init__(self, file_name): - self.file_object = open(file_name, "r") + self.file_object = open(file_name) self.block_generator = self.get_block_generator() def __iter__(self): @@ -53,7 +51,7 @@ def handle_single_xyz_frame(lines): atom_num, len(lines) ) ) - data_format_line = lines[1].strip("\n").strip() + str(" ") + data_format_line = lines[1].strip("\n").strip() + " " field_value_pattern = re.compile( r"(?P\S+)=(?P[\'\"]?)(?P.*?)(?P=quote)\s+" ) diff --git a/pyproject.toml b/pyproject.toml index cdb7a25d1..d55befa09 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,3 +67,33 @@ write_to = "dpdata/_version.py" [tool.isort] profile = "black" + +[tool.ruff] +target-version = "py37" +select = [ + "E", # errors + "F", # pyflakes + "D", # pydocstyle + "UP", # pyupgrade +] +ignore = [ + "E501", # line too long + "F841", # local variable is assigned to but never used + "E741", # ambiguous variable name + "E402", # module level import not at top of file + "D413", # missing blank line after last section + "D416", # section name should end with a colon + "D203", # 1 blank line required before class docstring + "D107", # missing docstring in __init__ + "D213", # multi-line docstring summary should start at the second line + "D100", # TODO: missing docstring in public module + "D101", # TODO: missing docstring in public class + "D102", # TODO: missing docstring in public method + "D103", # TODO: missing docstring in public function + "D104", # TODO: missing docstring in public package + "D105", # TODO: missing docstring in magic method + "D205", # 1 blank line required between summary line and description + "D401", # TODO: first line should be in imperative mood + "D404", # TODO: first word of the docstring should not be This +] +ignore-init-module-imports = true diff --git a/tests/comp_sys.py b/tests/comp_sys.py index 6618cdee6..426df412e 100644 --- a/tests/comp_sys.py +++ b/tests/comp_sys.py @@ -89,7 +89,7 @@ def test_virial(self): # if len(self.system_1['virials']) == 0: # self.assertEqual(len(self.system_1['virials']), 0) # return - if not "virials" in self.system_1: + if "virials" not in self.system_1: self.assertFalse("virials" in self.system_2) return np.testing.assert_almost_equal( diff --git a/tests/context.py b/tests/context.py index 10348a8d6..1406b7757 100644 --- a/tests/context.py +++ b/tests/context.py @@ -2,9 +2,3 @@ import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) -import dpdata -import dpdata.gaussian.gjf -import dpdata.md.msd -import dpdata.md.water -import dpdata.stat -import dpdata.system diff --git a/tests/poscars/test_lammps_dump_s_su.py b/tests/poscars/test_lammps_dump_s_su.py index 4b40cf35b..28673dfc7 100644 --- a/tests/poscars/test_lammps_dump_s_su.py +++ b/tests/poscars/test_lammps_dump_s_su.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata from poscars.poscar_ref_oh import TestPOSCARoh diff --git a/tests/test_abacus_stru_dump.py b/tests/test_abacus_stru_dump.py index 78fcd0880..46cb5de6a 100644 --- a/tests/test_abacus_stru_dump.py +++ b/tests/test_abacus_stru_dump.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata from test_vasp_poscar_dump import myfilecmp diff --git a/tests/test_amber_md.py b/tests/test_amber_md.py index 8d8304ccd..a6197c272 100644 --- a/tests/test_amber_md.py +++ b/tests/test_amber_md.py @@ -6,7 +6,7 @@ from context import dpdata try: - import parmed + pass except ModuleNotFoundError: skip_parmed_related_test = True else: diff --git a/tests/test_amber_sqm.py b/tests/test_amber_sqm.py index 99c1a2857..ee98dc180 100644 --- a/tests/test_amber_sqm.py +++ b/tests/test_amber_sqm.py @@ -6,7 +6,7 @@ from context import dpdata try: - from dpdata import BondOrderSystem + pass except ImportError: skip_bond_order_system = True else: @@ -67,12 +67,12 @@ def setUp(self): self.system = dpdata.BondOrderSystem( "amber/methane.mol", fmt="mol", type_map=["H", "C"] ) - with open("amber/sqm.in", "r") as f: + with open("amber/sqm.in") as f: self.sqm_in = f.read() def test_sqm_in(self): self.system.to("sqm/in", "amber/sqm_test.in") - with open("amber/sqm_test.in", "r") as f: + with open("amber/sqm_test.in") as f: self.sqm_in_test = f.read() self.assertEqual(self.sqm_in, self.sqm_in_test) diff --git a/tests/test_ase_traj.py b/tests/test_ase_traj.py index 2a3e92679..8f7516f76 100644 --- a/tests/test_ase_traj.py +++ b/tests/test_ase_traj.py @@ -1,12 +1,10 @@ -import os import unittest -import numpy as np from comp_sys import CompLabeledSys, IsPBC from context import dpdata try: - import ase + pass except ModuleNotFoundError: skip_ase = True else: @@ -28,7 +26,7 @@ def setUp(self): @unittest.skipIf(skip_ase, "skip ase related test. install ase to fix") -class TestASEtraj1(unittest.TestCase, CompLabeledSys, IsPBC): +class TestASEtraj2(unittest.TestCase, CompLabeledSys, IsPBC): def setUp(self): self.system_temp0 = dpdata.MultiSystems.from_file( file_name="ase_traj/HeAlO.traj", fmt="ase/structure" diff --git a/tests/test_bond_order_system.py b/tests/test_bond_order_system.py index 52f259efa..41a167fbc 100644 --- a/tests/test_bond_order_system.py +++ b/tests/test_bond_order_system.py @@ -8,7 +8,6 @@ from rdkit import Chem from rdkit.Chem import AllChem - from dpdata import BondOrderSystem except ImportError: skip_bond_order_system = True else: diff --git a/tests/test_cell_to_low_triangle.py b/tests/test_cell_to_low_triangle.py index d3121e38e..c080c8e5f 100644 --- a/tests/test_cell_to_low_triangle.py +++ b/tests/test_cell_to_low_triangle.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_corr.py b/tests/test_corr.py index 3224c950c..463c99af9 100644 --- a/tests/test_corr.py +++ b/tests/test_corr.py @@ -20,7 +20,7 @@ def setUp(self): ep = r"@%EP" target = ":1" cutoff = 6.0 - interactwith = "(%s)<:%f&!%s" % (target, cutoff, ep) + interactwith = f"({target})<:{cutoff:f}&!{ep}" s_ll = dpdata.LabeledSystem("amber/corr/dp_ll", fmt="deepmd/npy") s_hl = dpdata.LabeledSystem("amber/corr/dp_hl", fmt="deepmd/npy") self.system_1 = s_ll.correction(s_hl) @@ -31,7 +31,7 @@ def setUp(self): self.v_places = 6 -class TestCorr(unittest.TestCase, CompLabeledSys, IsPBC): +class TestCorrMulti(unittest.TestCase, CompLabeledSys, IsPBC): """Make a test to get a correction of two MultiSystems.""" def setUp(self): diff --git a/tests/test_cp2k_aimd_output.py b/tests/test_cp2k_aimd_output.py index 8e9932d11..bce242500 100644 --- a/tests/test_cp2k_aimd_output.py +++ b/tests/test_cp2k_aimd_output.py @@ -1,8 +1,6 @@ # %% -import os import unittest -import numpy as np from comp_sys import CompLabeledSys from context import dpdata diff --git a/tests/test_cp2k_output.py b/tests/test_cp2k_output.py index a7af46969..0e4b153dc 100644 --- a/tests/test_cp2k_output.py +++ b/tests/test_cp2k_output.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from comp_sys import CompLabeledSys from context import dpdata diff --git a/tests/test_deepmd_comp.py b/tests/test_deepmd_comp.py index 616caa468..46f8e7414 100644 --- a/tests/test_deepmd_comp.py +++ b/tests/test_deepmd_comp.py @@ -42,7 +42,7 @@ def tearDown(self): shutil.rmtree("tmp.deepmd.npy") -class TestDeepmdCompNoLabels(unittest.TestCase, CompSys, IsPBC): +class TestDeepmdCompNoLabels2(unittest.TestCase, CompSys, IsPBC): def setUp(self): self.dir_name = "tmp.deepmd.npy.nol" natoms = 3 diff --git a/tests/test_deepmd_mixed.py b/tests/test_deepmd_mixed.py index 19c46e727..6311410ef 100644 --- a/tests/test_deepmd_mixed.py +++ b/tests/test_deepmd_mixed.py @@ -2,10 +2,9 @@ import shutil import unittest from glob import glob -from itertools import permutations import numpy as np -from comp_sys import CompLabeledSys, CompSys, IsNoPBC, MultiSystems +from comp_sys import CompLabeledSys, IsNoPBC, MultiSystems from context import dpdata diff --git a/tests/test_elements_index.py b/tests/test_elements_index.py index 17a7ea91f..9e9e50661 100644 --- a/tests/test_elements_index.py +++ b/tests/test_elements_index.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from dpdata.system import elements_index_map diff --git a/tests/test_empty.py b/tests/test_empty.py index 3892ab3b1..8787f9543 100644 --- a/tests/test_empty.py +++ b/tests/test_empty.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_fhi_output.py b/tests/test_fhi_output.py index acbe2aac9..067e5f699 100644 --- a/tests/test_fhi_output.py +++ b/tests/test_fhi_output.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_gaussian_log.py b/tests/test_gaussian_log.py index 67e13910e..109efe713 100644 --- a/tests/test_gaussian_log.py +++ b/tests/test_gaussian_log.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_gromacs_gro.py b/tests/test_gromacs_gro.py index 27d2742ef..2971755f1 100644 --- a/tests/test_gromacs_gro.py +++ b/tests/test_gromacs_gro.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata diff --git a/tests/test_json.py b/tests/test_json.py index 09fc46dd7..545e5db8c 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from comp_sys import CompLabeledSys, IsPBC from context import dpdata diff --git a/tests/test_lammps_dump_shift_origin.py b/tests/test_lammps_dump_shift_origin.py index 60eb94c04..4ecd6f873 100644 --- a/tests/test_lammps_dump_shift_origin.py +++ b/tests/test_lammps_dump_shift_origin.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from comp_sys import CompSys, IsPBC from context import dpdata diff --git a/tests/test_lammps_dump_to_system.py b/tests/test_lammps_dump_to_system.py index b2061c099..af9748a51 100644 --- a/tests/test_lammps_dump_to_system.py +++ b/tests/test_lammps_dump_to_system.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata from poscars.poscar_ref_oh import TestPOSCARoh diff --git a/tests/test_lammps_dump_unfold.py b/tests/test_lammps_dump_unfold.py index 3e5ca47db..1e78d9756 100644 --- a/tests/test_lammps_dump_unfold.py +++ b/tests/test_lammps_dump_unfold.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata from poscars.poscar_ref_oh import TestPOSCARoh diff --git a/tests/test_lammps_lmp_dump.py b/tests/test_lammps_lmp_dump.py index e083fc8db..8e9cfb328 100644 --- a/tests/test_lammps_lmp_dump.py +++ b/tests/test_lammps_lmp_dump.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata from poscars.poscar_ref_oh import TestPOSCARoh diff --git a/tests/test_lammps_lmp_to_system.py b/tests/test_lammps_lmp_to_system.py index 80909def7..19e133121 100644 --- a/tests/test_lammps_lmp_to_system.py +++ b/tests/test_lammps_lmp_to_system.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata from poscars.poscar_ref_oh import TestPOSCARoh diff --git a/tests/test_msd.py b/tests/test_msd.py index f102ea9db..52b1ce935 100644 --- a/tests/test_msd.py +++ b/tests/test_msd.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_multisystems.py b/tests/test_multisystems.py index 3df15192b..172c2ad48 100644 --- a/tests/test_multisystems.py +++ b/tests/test_multisystems.py @@ -2,8 +2,7 @@ import unittest from itertools import permutations -import numpy as np -from comp_sys import CompLabeledSys, CompSys, IsNoPBC, MultiSystems +from comp_sys import CompLabeledSys, IsNoPBC, MultiSystems from context import dpdata diff --git a/tests/test_perturb.py b/tests/test_perturb.py index 162f17280..b89a8c7f2 100644 --- a/tests/test_perturb.py +++ b/tests/test_perturb.py @@ -1,13 +1,12 @@ -import os import unittest -from unittest.mock import MagicMock, Mock, patch +from unittest.mock import patch import numpy as np from comp_sys import CompSys, IsPBC from context import dpdata -class NormalGenerator(object): +class NormalGenerator: def __init__(self): self.randn_generator = self.get_randn_generator() self.rand_generator = self.get_rand_generator() @@ -44,7 +43,7 @@ def get_rand_generator(): ) -class UniformGenerator(object): +class UniformGenerator: def __init__(self): self.randn_generator = self.get_randn_generator() self.rand_generator = self.get_rand_generator() @@ -97,7 +96,7 @@ def get_rand_generator(): count += 1 -class ConstGenerator(object): +class ConstGenerator: def __init__(self): self.randn_generator = self.get_randn_generator() self.rand_generator = self.get_rand_generator() diff --git a/tests/test_pick_atom_idx.py b/tests/test_pick_atom_idx.py index 1de7ad321..0dc069911 100644 --- a/tests/test_pick_atom_idx.py +++ b/tests/test_pick_atom_idx.py @@ -1,12 +1,9 @@ import unittest -import numpy as np from comp_sys import CompSys, IsNoPBC from context import dpdata try: - import parmed - exist_module = True except Exception: exist_module = False @@ -33,7 +30,7 @@ def setUp(self): ep = r"@%EP" target = ":1" cutoff = 6.0 - interactwith = "(%s)<:%f&!%s" % (target, cutoff, ep) + interactwith = f"({target})<:{cutoff:f}&!{ep}" self.places = 6 self.e_places = 6 self.f_places = 6 diff --git a/tests/test_predict.py b/tests/test_predict.py index 1de04d895..c9df70ca5 100644 --- a/tests/test_predict.py +++ b/tests/test_predict.py @@ -5,7 +5,7 @@ from context import dpdata try: - import ase + pass except ModuleNotFoundError: skip_ase = True else: diff --git a/tests/test_pwmat_config_dump.py b/tests/test_pwmat_config_dump.py index db79a923f..9389c7a97 100644 --- a/tests/test_pwmat_config_dump.py +++ b/tests/test_pwmat_config_dump.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from pwmat.config_ref_oh import Testconfigoh import dpdata diff --git a/tests/test_pwmat_movement.py b/tests/test_pwmat_movement.py index bba4f2cae..e32c0f315 100644 --- a/tests/test_pwmat_movement.py +++ b/tests/test_pwmat_movement.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py index 4000480a0..3695e3ff1 100644 --- a/tests/test_pymatgen_molecule.py +++ b/tests/test_pymatgen_molecule.py @@ -5,7 +5,7 @@ from context import dpdata try: - import pymatgen + pass except ModuleNotFoundError: skip_pymatgen = True else: diff --git a/tests/test_qe_cp_traj.py b/tests/test_qe_cp_traj.py index e947a0f46..6a9631064 100644 --- a/tests/test_qe_cp_traj.py +++ b/tests/test_qe_cp_traj.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_qe_pw_scf.py b/tests/test_qe_pw_scf.py index 4d3032da4..2d96d7fe2 100644 --- a/tests/test_qe_pw_scf.py +++ b/tests/test_qe_pw_scf.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_qe_pw_scf_crystal_atomic_positions.py b/tests/test_qe_pw_scf_crystal_atomic_positions.py index e335c51ad..01c4df21f 100644 --- a/tests/test_qe_pw_scf_crystal_atomic_positions.py +++ b/tests/test_qe_pw_scf_crystal_atomic_positions.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_qe_pw_scf_energy_bug.py b/tests/test_qe_pw_scf_energy_bug.py index 49ff0e34f..8360a7a92 100644 --- a/tests/test_qe_pw_scf_energy_bug.py +++ b/tests/test_qe_pw_scf_energy_bug.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from context import dpdata diff --git a/tests/test_quip_gap_xyz.py b/tests/test_quip_gap_xyz.py index 03f69d955..b383bd2f4 100644 --- a/tests/test_quip_gap_xyz.py +++ b/tests/test_quip_gap_xyz.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from comp_sys import CompLabeledSys, IsPBC from context import dpdata diff --git a/tests/test_remove_outlier.py b/tests/test_remove_outlier.py index 192f4d8f0..b2cb52fcf 100644 --- a/tests/test_remove_outlier.py +++ b/tests/test_remove_outlier.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_remove_pbc.py b/tests/test_remove_pbc.py index 28310088d..d5befd771 100644 --- a/tests/test_remove_pbc.py +++ b/tests/test_remove_pbc.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_replace.py b/tests/test_replace.py index eaef289ca..b16c388b5 100644 --- a/tests/test_replace.py +++ b/tests/test_replace.py @@ -1,13 +1,12 @@ -import os import unittest -from unittest.mock import MagicMock, Mock, patch +from unittest.mock import patch import numpy as np from comp_sys import CompSys, IsPBC from context import dpdata -class ConstGenerator(object): +class ConstGenerator: def __init__(self): self.choice_generator = self.get_choice_generator() diff --git a/tests/test_replicate.py b/tests/test_replicate.py index fc41e62d9..99104c3ca 100644 --- a/tests/test_replicate.py +++ b/tests/test_replicate.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from comp_sys import CompSys, IsPBC from context import dpdata diff --git a/tests/test_siesta_aiMD_output.py b/tests/test_siesta_aiMD_output.py index e7f61641c..a1ba31b6d 100644 --- a/tests/test_siesta_aiMD_output.py +++ b/tests/test_siesta_aiMD_output.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_siesta_output.py b/tests/test_siesta_output.py index 0c51ae21d..9ff0167a0 100644 --- a/tests/test_siesta_output.py +++ b/tests/test_siesta_output.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_system_append.py b/tests/test_system_append.py index 32107a4c4..a2c30b238 100644 --- a/tests/test_system_append.py +++ b/tests/test_system_append.py @@ -1,8 +1,7 @@ -import os import unittest import numpy as np -from comp_sys import CompLabeledSys, CompSys, IsNoPBC, IsPBC +from comp_sys import CompLabeledSys, IsNoPBC, IsPBC from context import dpdata diff --git a/tests/test_system_apply_pbc.py b/tests/test_system_apply_pbc.py index c9f14d924..9cf44ae08 100644 --- a/tests/test_system_apply_pbc.py +++ b/tests/test_system_apply_pbc.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_system_set_type.py b/tests/test_system_set_type.py index edcddc646..4bb14b621 100644 --- a/tests/test_system_set_type.py +++ b/tests/test_system_set_type.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_to_ase.py b/tests/test_to_ase.py index 2440962c2..60dc931d9 100644 --- a/tests/test_to_ase.py +++ b/tests/test_to_ase.py @@ -1,12 +1,10 @@ import os import unittest -import numpy as np from comp_sys import CompSys, IsPBC from context import dpdata try: - from ase import Atoms from ase.io import write except ModuleNotFoundError: exist_module = False @@ -32,7 +30,7 @@ def setUp(self): @unittest.skipIf(not exist_module, "skip test_ase") class TestFromASE(unittest.TestCase, CompSys, IsPBC): - """Test ASEStructureFormat.from_system""" + """Test ASEStructureFormat.from_system.""" def setUp(self): system_1 = dpdata.System() diff --git a/tests/test_to_list.py b/tests/test_to_list.py index 68c98bf61..d559ffce2 100644 --- a/tests/test_to_list.py +++ b/tests/test_to_list.py @@ -1,7 +1,5 @@ -import os import unittest -import numpy as np from comp_sys import CompLabeledSys, IsPBC from context import dpdata diff --git a/tests/test_to_pymatgen.py b/tests/test_to_pymatgen.py index e2b91e24d..b38551aed 100644 --- a/tests/test_to_pymatgen.py +++ b/tests/test_to_pymatgen.py @@ -1,13 +1,10 @@ import os import unittest -import numpy as np from comp_sys import CompSys, IsPBC from context import dpdata try: - from pymatgen import Structure - exist_module = True except Exception: exist_module = False diff --git a/tests/test_to_pymatgen_entry.py b/tests/test_to_pymatgen_entry.py index 9510224b1..f1c11c1cc 100644 --- a/tests/test_to_pymatgen_entry.py +++ b/tests/test_to_pymatgen_entry.py @@ -1,14 +1,10 @@ import os import unittest -import numpy as np -from comp_sys import CompSys, IsPBC from context import dpdata from monty.serialization import loadfn try: - from pymatgen.entries.computed_entries import ComputedStructureEntry - exist_module = True except Exception: exist_module = False diff --git a/tests/test_vasp_outcar.py b/tests/test_vasp_outcar.py index d7e38867e..fb2ec1c94 100644 --- a/tests/test_vasp_outcar.py +++ b/tests/test_vasp_outcar.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_vasp_poscar_dump.py b/tests/test_vasp_poscar_dump.py index c67f42fcb..a81cbe94b 100644 --- a/tests/test_vasp_poscar_dump.py +++ b/tests/test_vasp_poscar_dump.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata from poscars.poscar_ref_oh import TestPOSCARoh diff --git a/tests/test_vasp_unconverged_outcar.py b/tests/test_vasp_unconverged_outcar.py index 4e48a71ab..7e1b35353 100644 --- a/tests/test_vasp_unconverged_outcar.py +++ b/tests/test_vasp_unconverged_outcar.py @@ -1,7 +1,6 @@ import os import unittest -import numpy as np from context import dpdata diff --git a/tests/test_vasp_xml.py b/tests/test_vasp_xml.py index d5df49dad..c21947870 100644 --- a/tests/test_vasp_xml.py +++ b/tests/test_vasp_xml.py @@ -1,4 +1,3 @@ -import os import unittest import numpy as np diff --git a/tests/test_water_ions.py b/tests/test_water_ions.py index 8b70dc736..1f30e1c8c 100644 --- a/tests/test_water_ions.py +++ b/tests/test_water_ions.py @@ -1,13 +1,9 @@ import os import unittest -import numpy as np from context import dpdata try: - import ase - import ase.neighborlist - exist_ase = True except Exception: exist_ase = False From f92660f534a21d3cb03d440dad6210a7b91e3bde Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 10 Apr 2023 20:56:25 -0400 Subject: [PATCH 2/9] noqa Signed-off-by: Jinzhe Zeng --- dpdata/plugins/pymatgen.py | 2 +- tests/test_amber_md.py | 2 +- tests/test_amber_sqm.py | 2 +- tests/test_predict.py | 2 +- tests/test_pymatgen_molecule.py | 2 +- tests/test_to_pymatgen.py | 2 ++ tests/test_to_pymatgen_entry.py | 2 ++ tests/test_water_ions.py | 3 +++ 8 files changed, 12 insertions(+), 5 deletions(-) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index f3aa6b274..531473603 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -33,7 +33,7 @@ class PyMatgenMoleculeFormat(Format): @Format.post("remove_pbc") def from_system(self, file_name, **kwargs): try: - pass + from pymatgen.core import Molecule # noqa: F401 except ModuleNotFoundError as e: raise ImportError("No module pymatgen.Molecule") from e diff --git a/tests/test_amber_md.py b/tests/test_amber_md.py index a6197c272..3995371ee 100644 --- a/tests/test_amber_md.py +++ b/tests/test_amber_md.py @@ -6,7 +6,7 @@ from context import dpdata try: - pass + import parmed # noqa: F401 except ModuleNotFoundError: skip_parmed_related_test = True else: diff --git a/tests/test_amber_sqm.py b/tests/test_amber_sqm.py index ee98dc180..7f14ff84c 100644 --- a/tests/test_amber_sqm.py +++ b/tests/test_amber_sqm.py @@ -6,7 +6,7 @@ from context import dpdata try: - pass + from dpdata import BondOrderSystem # noqa: F401 except ImportError: skip_bond_order_system = True else: diff --git a/tests/test_predict.py b/tests/test_predict.py index c9df70ca5..ad85464a2 100644 --- a/tests/test_predict.py +++ b/tests/test_predict.py @@ -5,7 +5,7 @@ from context import dpdata try: - pass + import ase # noqa: F401 except ModuleNotFoundError: skip_ase = True else: diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py index 3695e3ff1..231bd97ff 100644 --- a/tests/test_pymatgen_molecule.py +++ b/tests/test_pymatgen_molecule.py @@ -5,7 +5,7 @@ from context import dpdata try: - pass + import pymatgen # noqa: F401 except ModuleNotFoundError: skip_pymatgen = True else: diff --git a/tests/test_to_pymatgen.py b/tests/test_to_pymatgen.py index b38551aed..d58153962 100644 --- a/tests/test_to_pymatgen.py +++ b/tests/test_to_pymatgen.py @@ -5,6 +5,8 @@ from context import dpdata try: + from pymatgen import Structure # noqa: F401 + exist_module = True except Exception: exist_module = False diff --git a/tests/test_to_pymatgen_entry.py b/tests/test_to_pymatgen_entry.py index f1c11c1cc..fd8f40fcf 100644 --- a/tests/test_to_pymatgen_entry.py +++ b/tests/test_to_pymatgen_entry.py @@ -5,6 +5,8 @@ from monty.serialization import loadfn try: + from pymatgen.entries.computed_entries import ComputedStructureEntry # noqa: F401 + exist_module = True except Exception: exist_module = False diff --git a/tests/test_water_ions.py b/tests/test_water_ions.py index 1f30e1c8c..788030f38 100644 --- a/tests/test_water_ions.py +++ b/tests/test_water_ions.py @@ -4,6 +4,9 @@ from context import dpdata try: + import ase # noqa: F401 + import ase.neighborlist # noqa: F401 + exist_ase = True except Exception: exist_ase = False From cb6a0fc7473407a919eb5c25cf44b9b5a8ae196a Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 10 Apr 2023 20:57:34 -0400 Subject: [PATCH 3/9] prevent F401 Signed-off-by: Jinzhe Zeng --- tests/context.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/context.py b/tests/context.py index 1406b7757..16834cf5e 100644 --- a/tests/context.py +++ b/tests/context.py @@ -2,3 +2,18 @@ import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) +import dpdata +import dpdata.gaussian.gjf +import dpdata.md.msd +import dpdata.md.water +import dpdata.stat +import dpdata.system + +__all__ = [ + "dpdata", + "dpdata.gaussian.gjf", + "dpdata.md.msd", + "dpdata.md.water", + "dpdata.stat", + "dpdata.system", +] From 74b0efd1e215f6c1eb51a8650364cfbe340d87c5 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 10 Apr 2023 21:01:50 -0400 Subject: [PATCH 4/9] noqa Signed-off-by: Jinzhe Zeng --- tests/test_ase_traj.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ase_traj.py b/tests/test_ase_traj.py index 8f7516f76..8fe08e9a7 100644 --- a/tests/test_ase_traj.py +++ b/tests/test_ase_traj.py @@ -4,7 +4,7 @@ from context import dpdata try: - pass + import ase # noqa: F401 except ModuleNotFoundError: skip_ase = True else: From ae1765a885f5a89881838dd6531c49b15f971b78 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 10 Apr 2023 21:02:58 -0400 Subject: [PATCH 5/9] noqa Signed-off-by: Jinzhe Zeng --- tests/context.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/context.py b/tests/context.py index 16834cf5e..77a7557d3 100644 --- a/tests/context.py +++ b/tests/context.py @@ -3,17 +3,12 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) import dpdata -import dpdata.gaussian.gjf -import dpdata.md.msd -import dpdata.md.water -import dpdata.stat -import dpdata.system +import dpdata.gaussian.gjf # noqa: F401 +import dpdata.md.msd # noqa: F401 +import dpdata.md.water # noqa: F401 +import dpdata.stat # noqa: F401 +import dpdata.system # noqa: F401 __all__ = [ "dpdata", - "dpdata.gaussian.gjf", - "dpdata.md.msd", - "dpdata.md.water", - "dpdata.stat", - "dpdata.system", ] From 00a7e32c75e34b0e38485d1ef3b95565dd441046 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 10 Apr 2023 21:08:00 -0400 Subject: [PATCH 6/9] fix wrong format name --- tests/test_ase_traj.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ase_traj.py b/tests/test_ase_traj.py index 8fe08e9a7..8b9ad1f43 100644 --- a/tests/test_ase_traj.py +++ b/tests/test_ase_traj.py @@ -15,7 +15,7 @@ class TestASEtraj1(unittest.TestCase, CompLabeledSys, IsPBC): def setUp(self): self.multi_systems = dpdata.MultiSystems.from_file( - "ase_traj/HeAlO.traj", fmt="ase_traj/structure" + "ase_traj/HeAlO.traj", fmt="ase/structure" ) self.system_1 = self.multi_systems.systems["Al0He4O0"] self.system_2 = dpdata.LabeledSystem("ase_traj/Al0He4O0", fmt="deepmd") From afdbe228cb865522dc46b246c41e8c754e4137e5 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 10 Apr 2023 21:13:30 -0400 Subject: [PATCH 7/9] skip TestASEtraj1 which was broken --- tests/test_ase_traj.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_ase_traj.py b/tests/test_ase_traj.py index 8b9ad1f43..593d96f09 100644 --- a/tests/test_ase_traj.py +++ b/tests/test_ase_traj.py @@ -12,6 +12,7 @@ @unittest.skipIf(skip_ase, "skip ase related test. install ase to fix") +@unittest.skip("This test is broken") class TestASEtraj1(unittest.TestCase, CompLabeledSys, IsPBC): def setUp(self): self.multi_systems = dpdata.MultiSystems.from_file( From 0605302269a789910a9d96e0a910698f9288887e Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 11 Apr 2023 18:19:20 -0400 Subject: [PATCH 8/9] add isort rule set --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index d55befa09..5b555ad59 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,6 +75,7 @@ select = [ "F", # pyflakes "D", # pydocstyle "UP", # pyupgrade + "I", # isort ] ignore = [ "E501", # line too long From a0b89a35243e094bb875ecff2ec3580e49fcc367 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Apr 2023 22:19:57 +0000 Subject: [PATCH 9/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/qe/traj.py | 1 - tests/test_elements_index.py | 1 - 2 files changed, 2 deletions(-) diff --git a/dpdata/qe/traj.py b/dpdata/qe/traj.py index 7aa3b20b4..ab1a790b5 100644 --- a/dpdata/qe/traj.py +++ b/dpdata/qe/traj.py @@ -3,7 +3,6 @@ import numpy as np - from ..unit import ( EnergyConversion, ForceConversion, diff --git a/tests/test_elements_index.py b/tests/test_elements_index.py index 9e9e50661..45408b4d8 100644 --- a/tests/test_elements_index.py +++ b/tests/test_elements_index.py @@ -1,6 +1,5 @@ import unittest - from dpdata.system import elements_index_map