From a6f268ef13c548be2bee03829be06f5ab49b3546 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert Date: Mon, 20 Jul 2026 13:03:40 +0200 Subject: [PATCH] Format Python API with ruff --- .pre-commit-config.yaml | 9 + doc/guide/minimal-example/energy.py | 8 +- python/dftd3/__init__.py | 2 +- python/dftd3/ase.py | 27 ++- python/dftd3/library.py | 4 +- python/dftd3/qcschema.py | 6 +- python/dftd3/test_interface.py | 4 +- python/dftd3/test_qcschema.py | 279 +++++++++++++++++++++++----- test/validation/tester.py | 12 +- 9 files changed, 275 insertions(+), 76 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2048d2505..5143d498d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,15 @@ # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.15.10 + hooks: + # Run the linter. + - id: ruff + args: [--config, python/pyproject.toml, --fix] + # Run the formatter. + - id: ruff-format + args: [--config, python/pyproject.toml] - repo: https://github.com/PlasmaFAIR/fortitude-pre-commit rev: v0.9.0 hooks: diff --git a/doc/guide/minimal-example/energy.py b/doc/guide/minimal-example/energy.py index 87f4fee6f..6cc926310 100644 --- a/doc/guide/minimal-example/energy.py +++ b/doc/guide/minimal-example/energy.py @@ -4,11 +4,11 @@ num = np.array([6, 1, 1, 1, 1]) xyz = np.array( # coordinates in Bohr [ - [ 0.0000000, -0.0000000, 0.0000000], - [-1.1922080, 1.1922080, 1.1922080], - [ 1.1922080, -1.1922080, 1.1922080], + [0.0000000, -0.0000000, 0.0000000], + [-1.1922080, 1.1922080, 1.1922080], + [1.1922080, -1.1922080, 1.1922080], [-1.1922080, -1.1922080, -1.1922080], - [ 1.1922080, 1.1922080, -1.1922080], + [1.1922080, 1.1922080, -1.1922080], ] ) method = "PBE0" diff --git a/python/dftd3/__init__.py b/python/dftd3/__init__.py index f49df4137..b7bb52c36 100644 --- a/python/dftd3/__init__.py +++ b/python/dftd3/__init__.py @@ -16,6 +16,6 @@ """Python API for the DFT-D3 dispersion model""" # make sure we have a CFFI available -import cffi +import cffi # noqa __version__ = "1.4.0" diff --git a/python/dftd3/ase.py b/python/dftd3/ase.py index aa5d0a37c..09ac2dd39 100644 --- a/python/dftd3/ase.py +++ b/python/dftd3/ase.py @@ -99,13 +99,21 @@ [-0. , 0.00004060, -0.00004786]]) """ -try: - import ase -except ModuleNotFoundError: - raise ModuleNotFoundError("This submodule requires ASE installed") - from typing import List, Optional +try: + from ase.calculators.calculator import ( + Calculator, + InputError, + CalculationFailed, + all_changes, + ) + from ase.calculators.mixing import SumCalculator + from ase.atoms import Atoms + from ase.units import Hartree, Bohr +except ModuleNotFoundError as e: + raise ModuleNotFoundError("This submodule requires ASE installed") from e + from .interface import ( DispersionModel, DampingParam, @@ -117,15 +125,6 @@ CSODampingParam, ZDampingParam, ) -from ase.calculators.calculator import ( - Calculator, - InputError, - CalculationFailed, - all_changes, -) -from ase.calculators.mixing import SumCalculator -from ase.atoms import Atoms -from ase.units import Hartree, Bohr _damping_param = { diff --git a/python/dftd3/library.py b/python/dftd3/library.py index 9a90baf09..3a8b464d7 100644 --- a/python/dftd3/library.py +++ b/python/dftd3/library.py @@ -285,7 +285,9 @@ def new_z_damping( def load_z_damping(method: str, atm: bool) -> ParamHandle: """Load Z damping parameters from internal storage""" - return ParamHandle.with_gc(error_check(lib.dftd3_load_z_damping)(_char(method), atm)) + return ParamHandle.with_gc( + error_check(lib.dftd3_load_z_damping)(_char(method), atm) + ) def update_structure( diff --git a/python/dftd3/qcschema.py b/python/dftd3/qcschema.py index 7283dc47b..aace6379d 100644 --- a/python/dftd3/qcschema.py +++ b/python/dftd3/qcschema.py @@ -368,9 +368,7 @@ def run_qcschema(input_data): if schema_version == 1: return qcel_v1.AtomicResult(**ret_data) - + if "error" in ret_data: - return qcel_v2.FailedOperation( - input_data=atomic_input, error=ret_data["error"] - ) + return qcel_v2.FailedOperation(input_data=atomic_input, error=ret_data["error"]) return qcel_v2.AtomicResult(**ret_data) diff --git a/python/dftd3/test_interface.py b/python/dftd3/test_interface.py index def63e171..d8845df63 100644 --- a/python/dftd3/test_interface.py +++ b/python/dftd3/test_interface.py @@ -281,7 +281,9 @@ def test_b97d_d3_op(atm: bool, model: DispersionModel) -> None: def test_pbe_d3_z(atm: bool, model: DispersionModel) -> None: ref = -0.005741490224533363 if atm else -0.005841389688523762 - res = model.get_dispersion(ZDampingParam(a1=200770.0, s9=1.0 if atm else 0.0), grad=False) + res = model.get_dispersion( + ZDampingParam(a1=200770.0, s9=1.0 if atm else 0.0), grad=False + ) assert approx(res.get("energy")) == ref diff --git a/python/dftd3/test_qcschema.py b/python/dftd3/test_qcschema.py index 30baaa734..bd23eea5f 100644 --- a/python/dftd3/test_qcschema.py +++ b/python/dftd3/test_qcschema.py @@ -96,6 +96,7 @@ def get_molecule(name: str) -> dict: raise ValueError(f"Unknown molecule name: {name}") + def get_atomic_input( version: int, molecule: dict, @@ -147,10 +148,13 @@ def get_atomic_input( raise ValueError(f"Unsupported version: {version}") -@pytest.fixture(params=[pytest.param(1, marks=v1_available), pytest.param(2, marks=v2_available)]) +@pytest.fixture( + params=[pytest.param(1, marks=v1_available), pytest.param(2, marks=v2_available)] +) def qcsk_version(request): return request.param + @pytest.fixture(params=["D3", "D3ATM"]) def atm(request): return request.param == "D3ATM" @@ -229,47 +233,191 @@ def test_gradient_b97d_d3bj(atm: bool, qcsk_version: int) -> None: if atm: gradient = np.array( [ - [-2.2443259092095252e-4, -5.9115746657000033e-4, -2.3329260776706518e-4], - [+1.5024984250557624e-4, -2.8252993039799747e-4, -1.1514673689182528e-4], - [+6.4022428072682536e-4, -1.9311563977342141e-4, -8.5242235769601618e-5], - [-3.8718976343890316e-4, +3.3773870712256794e-4, +1.4637097756315060e-4], - [-1.5079749995767366e-4, +2.7785809307656525e-4, +1.3482408063774428e-4], - [-5.4334015754870673e-5, +4.8740178552571852e-4, +2.0162061388786579e-4], - [+4.2524309694851151e-4, -1.0353034250430949e-3, +5.4187321009509747e-4], - [+2.5238880483243874e-4, -4.7570815576830443e-4, -1.9406096040939865e-4], - [+5.0428650201581710e-4, +2.5654485601078660e-4, +9.9141715771808528e-5], - [+5.7124533738733385e-4, -3.7922753617903088e-5, -2.2970835136004695e-5], - [-5.2629396330476706e-4, -2.1821084773584023e-4, -7.7771997956860579e-5], - [-3.1447177050715551e-4, -4.4596903523484569e-4, -1.7470534972738137e-4], - [-4.7241980717575030e-4, +5.7789501566979232e-4, +6.3391340634065063e-4], - [-4.4295668215519951e-4, +4.4288999267499747e-5, +2.9913326488103452e-4], - [-5.5532764141529982e-5, +5.0128482842532645e-4, -2.3745756890447453e-4], - [+1.6636630001049617e-4, +2.9846431594745745e-4, -1.7829054133393723e-4], - [-4.6582228517614413e-5, +2.5574634046668398e-4, -4.7454953681660685e-4], - [-3.4993078552581617e-5, +2.4269431262900884e-4, -2.6338889846419609e-4], + [ + -2.2443259092095252e-4, + -5.9115746657000033e-4, + -2.3329260776706518e-4, + ], + [ + +1.5024984250557624e-4, + -2.8252993039799747e-4, + -1.1514673689182528e-4, + ], + [ + +6.4022428072682536e-4, + -1.9311563977342141e-4, + -8.5242235769601618e-5, + ], + [ + -3.8718976343890316e-4, + +3.3773870712256794e-4, + +1.4637097756315060e-4, + ], + [ + -1.5079749995767366e-4, + +2.7785809307656525e-4, + +1.3482408063774428e-4, + ], + [ + -5.4334015754870673e-5, + +4.8740178552571852e-4, + +2.0162061388786579e-4, + ], + [ + +4.2524309694851151e-4, + -1.0353034250430949e-3, + +5.4187321009509747e-4, + ], + [ + +2.5238880483243874e-4, + -4.7570815576830443e-4, + -1.9406096040939865e-4, + ], + [ + +5.0428650201581710e-4, + +2.5654485601078660e-4, + +9.9141715771808528e-5, + ], + [ + +5.7124533738733385e-4, + -3.7922753617903088e-5, + -2.2970835136004695e-5, + ], + [ + -5.2629396330476706e-4, + -2.1821084773584023e-4, + -7.7771997956860579e-5, + ], + [ + -3.1447177050715551e-4, + -4.4596903523484569e-4, + -1.7470534972738137e-4, + ], + [ + -4.7241980717575030e-4, + +5.7789501566979232e-4, + +6.3391340634065063e-4, + ], + [ + -4.4295668215519951e-4, + +4.4288999267499747e-5, + +2.9913326488103452e-4, + ], + [ + -5.5532764141529982e-5, + +5.0128482842532645e-4, + -2.3745756890447453e-4, + ], + [ + +1.6636630001049617e-4, + +2.9846431594745745e-4, + -1.7829054133393723e-4, + ], + [ + -4.6582228517614413e-5, + +2.5574634046668398e-4, + -4.7454953681660685e-4, + ], + [ + -3.4993078552581617e-5, + +2.4269431262900884e-4, + -2.6338889846419609e-4, + ], ] ) else: gradient = np.array( [ - [-2.2562967976217643e-04, -5.8711103078133340e-04, -2.3061961042068857e-04], - [+1.5075490579531149e-04, -2.8381505981386033e-04, -1.1485932659166404e-04], - [+6.3724554206790829e-04, -1.8986067913445720e-04, -8.2853482732288822e-05], - [-3.8832449621583123e-04, +3.3282825489958215e-04, +1.4481440153935071e-04], - [-1.5045846438610337e-04, +2.7794484580385330e-04, +1.3314407954925452e-04], - [-4.9560016743308596e-05, +4.8509857651717612e-04, +2.0112762761380736e-04], - [+4.2275101080035900e-04, -1.0281087399068071e-03, +5.3322479935030958e-04], - [+2.5240228222254599e-04, -4.7591532248547261e-04, -1.9359997368309395e-04], - [+5.0291675303562318e-04, +2.5554376917832927e-04, +9.9031452660504099e-05], - [+5.7163177819497945e-04, -4.0110024194130451e-05, -2.3133113964066147e-05], - [-5.2462065805044188e-04, -2.1790929154673013e-04, -7.7260813677796259e-05], - [-3.1304532783227779e-04, -4.4767282257326171e-04, -1.7470716818989833e-04], - [-4.7193012650844176e-04, +5.7610764389933421e-04, +6.3567846119653526e-04], - [-4.4301897860924111e-04, +4.4549998176073525e-05, +2.9945109087021988e-04], - [-5.8493469864357109e-05, +5.0096942382815647e-04, -2.4093109056512457e-04], - [+1.6568457442979896e-04, +2.9913845360379754e-04, -1.7797313385544664e-04], - [-4.4555263684831344e-05, +2.5438217773443301e-04, -4.6925288218547617e-04], - [-3.3750364889515759e-05, +2.4393982679531790e-04, -2.6128131691443809e-04], + [ + -2.2562967976217643e-04, + -5.8711103078133340e-04, + -2.3061961042068857e-04, + ], + [ + +1.5075490579531149e-04, + -2.8381505981386033e-04, + -1.1485932659166404e-04, + ], + [ + +6.3724554206790829e-04, + -1.8986067913445720e-04, + -8.2853482732288822e-05, + ], + [ + -3.8832449621583123e-04, + +3.3282825489958215e-04, + +1.4481440153935071e-04, + ], + [ + -1.5045846438610337e-04, + +2.7794484580385330e-04, + +1.3314407954925452e-04, + ], + [ + -4.9560016743308596e-05, + +4.8509857651717612e-04, + +2.0112762761380736e-04, + ], + [ + +4.2275101080035900e-04, + -1.0281087399068071e-03, + +5.3322479935030958e-04, + ], + [ + +2.5240228222254599e-04, + -4.7591532248547261e-04, + -1.9359997368309395e-04, + ], + [ + +5.0291675303562318e-04, + +2.5554376917832927e-04, + +9.9031452660504099e-05, + ], + [ + +5.7163177819497945e-04, + -4.0110024194130451e-05, + -2.3133113964066147e-05, + ], + [ + -5.2462065805044188e-04, + -2.1790929154673013e-04, + -7.7260813677796259e-05, + ], + [ + -3.1304532783227779e-04, + -4.4767282257326171e-04, + -1.7470716818989833e-04, + ], + [ + -4.7193012650844176e-04, + +5.7610764389933421e-04, + +6.3567846119653526e-04, + ], + [ + -4.4301897860924111e-04, + +4.4549998176073525e-05, + +2.9945109087021988e-04, + ], + [ + -5.8493469864357109e-05, + +5.0096942382815647e-04, + -2.4093109056512457e-04, + ], + [ + +1.6568457442979896e-04, + +2.9913845360379754e-04, + -1.7797313385544664e-04, + ], + [ + -4.4555263684831344e-05, + +2.5438217773443301e-04, + -4.6925288218547617e-04, + ], + [ + -3.3750364889515759e-05, + +2.4393982679531790e-04, + -2.6128131691443809e-04, + ], ] ) @@ -279,7 +427,6 @@ def test_gradient_b97d_d3bj(atm: bool, qcsk_version: int) -> None: assert approx(atomic_result.return_result, abs=thr) == gradient - @pytest.mark.skipif( qcel_v1 is None and qcel_v2 is None, reason="requires qcelemental models" ) @@ -455,19 +602,55 @@ def test_ghost_pbe_d3bj(atm: bool, qcsk_version: int) -> None: else: gradient = np.array( [ - [+3.6009409394390283e-11, +1.1526637296579583e-07, +0.0000000000000000e00], - [+5.3425212496378062e-05, -1.8891583140214365e-05, +0.0000000000000000e00], - [-2.6712623930671679e-05, -1.8891594924889618e-05, -4.6267592357667043e-05], - [-2.6712623930671679e-05, -1.8891594924889618e-05, +4.6267592357667043e-05], - [-6.4444409450829659e-13, +5.6559506617027801e-05, +0.0000000000000000e00], - [+0.0000000000000000e00, +0.0000000000000000e00, +0.0000000000000000e00], - [+0.0000000000000000e00, +0.0000000000000000e00, +0.0000000000000000e00], - [+0.0000000000000000e00, +0.0000000000000000e00, +0.0000000000000000e00], - [+0.0000000000000000e00, +0.0000000000000000e00, +0.0000000000000000e00], + [ + +3.6009409394390283e-11, + +1.1526637296579583e-07, + +0.0000000000000000e00, + ], + [ + +5.3425212496378062e-05, + -1.8891583140214365e-05, + +0.0000000000000000e00, + ], + [ + -2.6712623930671679e-05, + -1.8891594924889618e-05, + -4.6267592357667043e-05, + ], + [ + -2.6712623930671679e-05, + -1.8891594924889618e-05, + +4.6267592357667043e-05, + ], + [ + -6.4444409450829659e-13, + +5.6559506617027801e-05, + +0.0000000000000000e00, + ], + [ + +0.0000000000000000e00, + +0.0000000000000000e00, + +0.0000000000000000e00, + ], + [ + +0.0000000000000000e00, + +0.0000000000000000e00, + +0.0000000000000000e00, + ], + [ + +0.0000000000000000e00, + +0.0000000000000000e00, + +0.0000000000000000e00, + ], + [ + +0.0000000000000000e00, + +0.0000000000000000e00, + +0.0000000000000000e00, + ], ] ) atomic_result = run_qcschema(atomic_input) assert atomic_result.success - assert approx(atomic_result.return_result, abs=thr) == gradient \ No newline at end of file + assert approx(atomic_result.return_result, abs=thr) == gradient diff --git a/test/validation/tester.py b/test/validation/tester.py index cf5980fb3..ef4321e62 100644 --- a/test/validation/tester.py +++ b/test/validation/tester.py @@ -8,7 +8,10 @@ """ try: - import subprocess, sys, json, os + import subprocess + import sys + import json + import os except ImportError: exit(77) @@ -25,7 +28,10 @@ def __init__(self, value, abs): def __eq__(self, other): def compare(a, b, ctx): if isinstance(a, list) and isinstance(b, list): - return all(compare(x, y, f"{ctx}[{idx}]") for idx, (x, y) in enumerate(zip(a, b))) + return all( + compare(x, y, f"{ctx}[{idx}]") + for idx, (x, y) in enumerate(zip(a, b)) + ) if isinstance(a, dict) and isinstance(b, dict): try: @@ -57,7 +63,7 @@ def compare(a, b, ctx): outp = sys.argv[2] with open(sys.argv[3]) as fd: wdir = os.path.dirname(fd.name) - args = [arg.replace('$ORIGIN', wdir) for arg in fd.read().strip().split("\n")] + args = [arg.replace("$ORIGIN", wdir) for arg in fd.read().strip().split("\n")] stat = subprocess.call( [prog, *args, "--json", os.path.basename(outp)],