diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 332d0e8f..a5d41652 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,7 +21,7 @@ repos: # Python - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.7.4 + rev: v0.8.1 hooks: - id: ruff args: ["--fix"] diff --git a/dpdata/abacus/md.py b/dpdata/abacus/md.py index 8b5ac21e..1b597165 100644 --- a/dpdata/abacus/md.py +++ b/dpdata/abacus/md.py @@ -68,7 +68,7 @@ def get_coords_from_dump(dumplines, natoms): else: nframes_dump = int(nlines / (total_natoms + 9)) assert nframes_dump > 0, ( - "Number of lines in MD_dump file = %d. Number of atoms = %d. The MD_dump file is incomplete." + "Number of lines in MD_dump file = %d. Number of atoms = %d. The MD_dump file is incomplete." # noqa: UP031 % (nlines, total_natoms) ) cells = np.zeros([nframes_dump, 3, 3]) @@ -125,7 +125,7 @@ def get_coords_from_dump(dumplines, natoms): ) iframe += 1 assert iframe == nframes_dump, ( - "iframe=%d, nframe_dump=%d. Number of frames does not match number of lines in MD_dump." + "iframe=%d, nframe_dump=%d. Number of frames does not match number of lines in MD_dump." # noqa: UP031 % (iframe, nframes_dump) ) stresses *= kbar2evperang3 @@ -145,7 +145,7 @@ def get_energy(outlines, ndump, dump_freq): energy.append(np.nan) nenergy += 1 assert ndump == len(energy), ( - "Number of total energies in running_md.log = %d. Number of frames in MD_dump = %d. Please check." + "Number of total energies in running_md.log = %d. Number of frames in MD_dump = %d. Please check." # noqa: UP031 % (len(energy), ndump) ) energy = np.array(energy) @@ -191,7 +191,7 @@ def get_frame(fname): force = np.delete(force, i - ndump, axis=0) stress = np.delete(stress, i - ndump, axis=0) energy = np.delete(energy, i - ndump, axis=0) - unconv_stru += "%d " % i + unconv_stru += "%d " % i # noqa: UP031 ndump = len(energy) if unconv_stru != "": warnings.warn(f"Structure {unconv_stru} are unconverged and not collected!") diff --git a/dpdata/abacus/relax.py b/dpdata/abacus/relax.py index ec27fe4f..8f90040e 100644 --- a/dpdata/abacus/relax.py +++ b/dpdata/abacus/relax.py @@ -47,7 +47,7 @@ def get_coords_from_log(loglines, natoms): natoms_log += int(line.split()[-1]) assert natoms_log > 0 and natoms_log == natoms, ( - "ERROR: detected atom number in log file is %d" % natoms + "ERROR: detected atom number in log file is %d" % natoms # noqa: UP031 ) energy = [] @@ -76,7 +76,7 @@ def get_coords_from_log(loglines, natoms): list(map(lambda x: float(x) * a0, loglines[i + k].split()[1:4])) ) else: - assert False, "Unrecongnized coordinate type, %s, line:%d" % ( + assert False, "Unrecongnized coordinate type, %s, line:%d" % ( # noqa: UP031 loglines[i].split()[0], i, ) diff --git a/dpdata/data_type.py b/dpdata/data_type.py index 851a65c9..8cea28c1 100644 --- a/dpdata/data_type.py +++ b/dpdata/data_type.py @@ -122,7 +122,7 @@ def check(self, system: System): elif isinstance(data, list): if len(shape) and shape[0] != len(data): raise DataError( - "Length of %s is %d, but expected %d" + "Length of %s is %d, but expected %d" # noqa: UP031 % (self.name, len(data), shape[0]) ) else: diff --git a/dpdata/deepmd/comp.py b/dpdata/deepmd/comp.py index 67eddd9f..410d789e 100644 --- a/dpdata/deepmd/comp.py +++ b/dpdata/deepmd/comp.py @@ -119,7 +119,7 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True): for ii in range(nsets): set_stt = ii * set_size set_end = (ii + 1) * set_size - set_folder = os.path.join(folder, "set.%03d" % ii) + set_folder = os.path.join(folder, "set.%03d" % ii) # noqa: UP031 os.makedirs(set_folder) try: os.remove(os.path.join(folder, "nopbc")) @@ -158,5 +158,5 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True): for ii in range(nsets): set_stt = ii * set_size set_end = (ii + 1) * set_size - set_folder = os.path.join(folder, "set.%03d" % ii) + set_folder = os.path.join(folder, "set.%03d" % ii) # noqa: UP031 np.save(os.path.join(set_folder, dtype.deepmd_name), ddata[set_stt:set_end]) diff --git a/dpdata/deepmd/hdf5.py b/dpdata/deepmd/hdf5.py index cbdaa632..c2b3bd42 100644 --- a/dpdata/deepmd/hdf5.py +++ b/dpdata/deepmd/hdf5.py @@ -59,7 +59,7 @@ def to_system_data( else: my_type_map = [] for ii in range(ntypes): - my_type_map.append("Type_%d" % ii) + my_type_map.append("Type_%d" % ii) # noqa: UP031 assert len(my_type_map) >= len(data["atom_numbs"]) for ii in range(len(data["atom_numbs"])): data["atom_names"].append(my_type_map[ii]) @@ -217,7 +217,7 @@ def dump( for ii in range(nsets): set_stt = ii * set_size set_end = (ii + 1) * set_size - set_folder = g.create_group("set.%03d" % ii) + set_folder = g.create_group("set.%03d" % ii) # noqa: UP031 for dt, prop in data_types.items(): if dt in reshaped_data: set_folder.create_dataset( diff --git a/dpdata/deepmd/raw.py b/dpdata/deepmd/raw.py index 81b01cc0..50dc5afd 100644 --- a/dpdata/deepmd/raw.py +++ b/dpdata/deepmd/raw.py @@ -27,7 +27,7 @@ def load_type(folder, type_map=None): else: my_type_map = [] for ii in range(ntypes): - my_type_map.append("Type_%d" % ii) + my_type_map.append("Type_%d" % ii) # noqa: UP031 data["atom_names"] = my_type_map data["atom_numbs"] = [] for ii, _ in enumerate(data["atom_names"]): diff --git a/dpdata/gaussian/gjf.py b/dpdata/gaussian/gjf.py index b83dad1c..419ec354 100644 --- a/dpdata/gaussian/gjf.py +++ b/dpdata/gaussian/gjf.py @@ -186,8 +186,8 @@ def make_gaussian_input( mult_frags.append(detect_multiplicity(np.array(symbols)[idx])) if use_fragment_guesses: multiplicity = sum(mult_frags) - frag_numb + 1 - charge % 2 - chargekeywords_frag = "%d %d" % (charge, multiplicity) + "".join( - [" %d %d" % (charge, mult_frag) for mult_frag in mult_frags] + chargekeywords_frag = "%d %d" % (charge, multiplicity) + "".join( # noqa: UP031 + [" %d %d" % (charge, mult_frag) for mult_frag in mult_frags] # noqa: UP031 ) else: multi_frags = np.array(mult_frags) @@ -239,10 +239,10 @@ def make_gaussian_input( for ii, (symbol, coordinate) in enumerate(zip(symbols, coordinates)): if use_fragment_guesses: buff.append( - "%s(Fragment=%d) %f %f %f" % (symbol, frag_index[ii] + 1, *coordinate) + "%s(Fragment=%d) %f %f %f" % (symbol, frag_index[ii] + 1, *coordinate) # noqa: UP031 ) else: - buff.append("{} {:f} {:f} {:f}".format(symbol, *coordinate)) + buff.append("{} {:f} {:f} {:f}".format(symbol, *coordinate)) # noqa: UP031 if not sys_data.get("nopbc", False): # PBC condition cell = sys_data["cells"][0] diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index 91f0c48d..fe549b95 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -289,7 +289,7 @@ def system_data( system["atom_names"] = [] if type_map is None: for ii in range(len(system["atom_numbs"])): - system["atom_names"].append("TYPE_%d" % ii) + system["atom_names"].append("TYPE_%d" % ii) # noqa: UP031 else: assert len(type_map) >= len(system["atom_numbs"]) for ii in range(len(system["atom_numbs"])): diff --git a/dpdata/lammps/lmp.py b/dpdata/lammps/lmp.py index 04620166..d9f2f1d5 100644 --- a/dpdata/lammps/lmp.py +++ b/dpdata/lammps/lmp.py @@ -167,7 +167,7 @@ def system_data(lines, type_map=None, type_idx_zero=True): system["atom_names"] = [] if type_map is None: for ii in range(len(system["atom_numbs"])): - system["atom_names"].append("Type_%d" % ii) + system["atom_names"].append("Type_%d" % ii) # noqa: UP031 else: assert len(type_map) >= len(system["atom_numbs"]) for ii in range(len(system["atom_numbs"])): @@ -198,27 +198,27 @@ def from_system_data(system, f_idx=0): ret += "\n" natoms = sum(system["atom_numbs"]) ntypes = len(system["atom_numbs"]) - ret += "%d atoms\n" % natoms - ret += "%d atom types\n" % ntypes + ret += "%d atoms\n" % natoms # noqa: UP031 + ret += "%d atom types\n" % ntypes # noqa: UP031 ret += (ptr_float_fmt + " " + ptr_float_fmt + " xlo xhi\n") % ( 0, system["cells"][f_idx][0][0], - ) + ) # noqa: UP031 ret += (ptr_float_fmt + " " + ptr_float_fmt + " ylo yhi\n") % ( 0, system["cells"][f_idx][1][1], - ) + ) # noqa: UP031 ret += (ptr_float_fmt + " " + ptr_float_fmt + " zlo zhi\n") % ( 0, system["cells"][f_idx][2][2], - ) + ) # noqa: UP031 ret += ( ptr_float_fmt + " " + ptr_float_fmt + " " + ptr_float_fmt + " xy xz yz\n" ) % ( system["cells"][f_idx][1][0], system["cells"][f_idx][2][0], system["cells"][f_idx][2][1], - ) + ) # noqa: UP031 ret += "\n" ret += "Atoms # atomic\n" ret += "\n" @@ -233,7 +233,7 @@ def from_system_data(system, f_idx=0): + " " + ptr_float_fmt + "\n" - ) + ) # noqa: UP031 if "spins" in system: coord_fmt = ( @@ -247,7 +247,7 @@ def from_system_data(system, f_idx=0): + " " + ptr_float_fmt + "\n" - ) + ) # noqa: UP031 spins_norm = np.linalg.norm(system["spins"][f_idx], axis=1) for ii in range(natoms): if "spins" in system: @@ -262,7 +262,7 @@ def from_system_data(system, f_idx=0): system["spins"][f_idx][ii][1] / spins_norm[ii], system["spins"][f_idx][ii][2] / spins_norm[ii], spins_norm[ii], - ) + ) # noqa: UP031 else: ret += coord_fmt % ( ii + 1, @@ -274,7 +274,7 @@ def from_system_data(system, f_idx=0): system["spins"][f_idx][ii][1], system["spins"][f_idx][ii][2] + 1, spins_norm[ii], - ) + ) # noqa: UP031 else: ret += coord_fmt % ( ii + 1, @@ -282,7 +282,7 @@ def from_system_data(system, f_idx=0): system["coords"][f_idx][ii][0] - system["orig"][0], system["coords"][f_idx][ii][1] - system["orig"][1], system["coords"][f_idx][ii][2] - system["orig"][2], - ) + ) # noqa: UP031 return ret diff --git a/dpdata/plugins/amber.py b/dpdata/plugins/amber.py index 361e0d8a..c51af346 100644 --- a/dpdata/plugins/amber.py +++ b/dpdata/plugins/amber.py @@ -136,8 +136,8 @@ def label(self, data: dict) -> dict: labeled_system = dpdata.LabeledSystem() with tempfile.TemporaryDirectory() as d: for ii, ss in enumerate(ori_system): - inp_fn = os.path.join(d, "%d.in" % ii) - out_fn = os.path.join(d, "%d.out" % ii) + inp_fn = os.path.join(d, "%d.in" % ii) # noqa: UP031 + out_fn = os.path.join(d, "%d.out" % ii) # noqa: UP031 ss.to("sqm/in", inp_fn, **self.kwargs) try: sp.check_output( diff --git a/dpdata/plugins/gaussian.py b/dpdata/plugins/gaussian.py index 55bee5a4..9cba4598 100644 --- a/dpdata/plugins/gaussian.py +++ b/dpdata/plugins/gaussian.py @@ -109,8 +109,8 @@ def label(self, data: dict) -> dict: labeled_system = dpdata.LabeledSystem() with tempfile.TemporaryDirectory() as d: for ii, ss in enumerate(ori_system): - inp_fn = os.path.join(d, "%d.gjf" % ii) - out_fn = os.path.join(d, "%d.log" % ii) + inp_fn = os.path.join(d, "%d.gjf" % ii) # noqa: UP031 + out_fn = os.path.join(d, "%d.log" % ii) # noqa: UP031 ss.to("gaussian/gjf", inp_fn, **self.kwargs) try: sp.check_output([*self.gaussian_exec.split(), inp_fn]) diff --git a/dpdata/pwmat/atomconfig.py b/dpdata/pwmat/atomconfig.py index 62eff77c..5f01c840 100644 --- a/dpdata/pwmat/atomconfig.py +++ b/dpdata/pwmat/atomconfig.py @@ -54,7 +54,7 @@ def to_system_data(lines): def from_system_data(system, f_idx=0, skip_zeros=True): ret = "" natoms = sum(system["atom_numbs"]) - ret += "%d" % natoms + ret += "%d" % natoms # noqa: UP031 ret += "\n" ret += "LATTICE" ret += "\n" @@ -83,7 +83,7 @@ def from_system_data(system, f_idx=0, skip_zeros=True): posi_list = [] for jj, ii in zip(atomic_numbers, posis): ii = np.matmul(ii, np.linalg.inv(system["cells"][0])) - posi_list.append("%d %15.10f %15.10f %15.10f 1 1 1" % (jj, ii[0], ii[1], ii[2])) + posi_list.append("%d %15.10f %15.10f %15.10f 1 1 1" % (jj, ii[0], ii[1], ii[2])) # noqa: UP031 for kk in range(len(posi_list)): min = kk for jj in range(kk, len(posi_list)): diff --git a/dpdata/qe/traj.py b/dpdata/qe/traj.py index b4be303a..4cb09550 100644 --- a/dpdata/qe/traj.py +++ b/dpdata/qe/traj.py @@ -78,7 +78,7 @@ def load_atom_names(lines, ntypes): def load_celldm(lines): celldm = np.zeros(6) for ii in range(6): - key = "celldm(%d)" % (ii + 1) + key = "celldm(%d)" % (ii + 1) # noqa: UP031 val = load_key(lines, key) if val is not None: celldm[ii] = float(val) @@ -169,7 +169,7 @@ def load_energy(fname, begin=0, step=1): data = np.loadtxt(fname) steps = [] for ii in data[begin::step, 0]: - steps.append("%d" % ii) + steps.append("%d" % ii) # noqa: UP031 with open_file(fname) as fp: while True: line = fp.readline() diff --git a/dpdata/rdkit/sanitize.py b/dpdata/rdkit/sanitize.py index 2b0d7663..1477918d 100644 --- a/dpdata/rdkit/sanitize.py +++ b/dpdata/rdkit/sanitize.py @@ -367,10 +367,10 @@ def sanitize_mol(mol, verbose=False): # copy from FEprep def mol_edit_log(mol, i, j): if not mol.HasProp("edit"): - mol.SetProp("edit", "%d_%d" % (i, j)) + mol.SetProp("edit", "%d_%d" % (i, j)) # noqa: UP031 else: edited = mol.GetProp("edit") - mol.SetProp("edit", edited + ",%d_%d" % (i, j)) + mol.SetProp("edit", edited + ",%d_%d" % (i, j)) # noqa: UP031 def kekulize_aromatic_heterocycles(mol_in, assign_formal_charge=True, sanitize=True): diff --git a/dpdata/system.py b/dpdata/system.py index 08136cb9..abe0a755 100644 --- a/dpdata/system.py +++ b/dpdata/system.py @@ -226,7 +226,7 @@ def check_data(self): dd.check(self) if sum(self.get_atom_numbs()) != self.get_natoms(): raise DataError( - "Sum of atom_numbs (%d) is not equal to natoms (%d)." + "Sum of atom_numbs (%d) is not equal to natoms (%d)." # noqa: UP031 % (sum(self.get_atom_numbs()), self.get_natoms()) ) @@ -281,8 +281,8 @@ def __str__(self): ret = "Data Summary" ret += "\nUnlabeled System" ret += "\n-------------------" - ret += "\nFrame Numbers : %d" % self.get_nframes() - ret += "\nAtom Numbers : %d" % self.get_natoms() + ret += "\nFrame Numbers : %d" % self.get_nframes() # noqa: UP031 + ret += "\nAtom Numbers : %d" % self.get_natoms() # noqa: UP031 ret += "\nElement List :" ret += "\n-------------------" ret += "\n" + " ".join(map(str, self.get_atom_names())) @@ -1243,8 +1243,8 @@ def __str__(self): ret = "Data Summary" ret += "\nLabeled System" ret += "\n-------------------" - ret += "\nFrame Numbers : %d" % self.get_nframes() - ret += "\nAtom Numbers : %d" % self.get_natoms() + ret += "\nFrame Numbers : %d" % self.get_nframes() # noqa: UP031 + ret += "\nAtom Numbers : %d" % self.get_natoms() # noqa: UP031 status = "Yes" if self.has_virial() else "No" ret += f"\nIncluding Virials : {status}" ret += "\nElement List :" diff --git a/dpdata/vasp/poscar.py b/dpdata/vasp/poscar.py index 30073e2b..78b8dbbe 100644 --- a/dpdata/vasp/poscar.py +++ b/dpdata/vasp/poscar.py @@ -79,7 +79,7 @@ def from_system_data(system, f_idx=0, skip_zeros=True): for ii, name in zip(system["atom_numbs"], system["atom_names"]): if ii == 0: continue - ret += "%s%d " % (name, ii) + ret += "%s%d " % (name, ii) # noqa: UP031 ret += "\n" ret += "1.0\n" for ii in system["cells"][f_idx]: @@ -94,7 +94,7 @@ def from_system_data(system, f_idx=0, skip_zeros=True): for ii in system["atom_numbs"]: if ii == 0: continue - ret += "%d " % ii + ret += "%d " % ii # noqa: UP031 ret += "\n" move = system.get("move", None) if move is not None and len(move) > 0: diff --git a/dpdata/vasp/xml.py b/dpdata/vasp/xml.py index 352b107e..c99f9d32 100755 --- a/dpdata/vasp/xml.py +++ b/dpdata/vasp/xml.py @@ -58,7 +58,7 @@ def formulate_config(eles, types, posi, cell, ener, forc, strs_): natoms = len(types) ntypes = len(eles) ret = "" - ret += "#N %d %d\n" % (natoms, ntypes - 1) + ret += "#N %d %d\n" % (natoms, ntypes - 1) # noqa: UP031 ret += "#C " for ii in eles: ret += " " + ii @@ -73,7 +73,7 @@ def formulate_config(eles, types, posi, cell, ener, forc, strs_): ret += "#F\n" for ii in range(natoms): sp = np.matmul(cell.T, posi[ii]) - ret += "%d" % (types[ii] - 1) + ret += "%d" % (types[ii] - 1) # noqa: UP031 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"