Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply extra Ruff rules #84

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/ruff.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
extend = "../pyproject.toml"

[lint]
ignore = ["D"]
extend-ignore = [
"A001",
"D",
"ERA001",
"INP001",
]
7 changes: 3 additions & 4 deletions examples/1INFILTR/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
"""

import itertools
import os
import sys
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np

r_unit = "cm"
t_unit = "h"

_filename = os.path.join(sys.path[0], "Nod_Inf.out")
_filename = Path(__file__).parent / "Nod_Inf.out"


t = []
r = None
theta = []
velocity = []

with open(_filename) as file:
with _filename.open() as file:
for i, line in enumerate(file):
if i <= 10:
continue # Skip time 0
Expand Down
5 changes: 2 additions & 3 deletions examples/HF135/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

"""

import os
import sys
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np

_filename = os.path.join(sys.path[0], "groundwaterFoam_results.csv")
_filename = Path(__file__).parent / "groundwaterFoam_results.csv"

name = "porousMultiphaseFoam"

Expand Down
85 changes: 54 additions & 31 deletions fronts/D.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
is the simplest supported function.
"""
if D0 <= 0:
raise ValueError("D0 must be positive")
msg = "D0 must be positive"
raise ValueError(msg)

Check warning on line 45 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L44-L45

Added lines #L44 - L45 were not covered by tests

def D(_, derivatives=0):
if derivatives == 0:
Expand Down Expand Up @@ -108,10 +109,12 @@
elif not free:
return constant(float(expr))
else:
raise ValueError("expression cannot contain more than one variable")
msg = "expression cannot contain more than one variable"
raise ValueError(msg)

Check warning on line 113 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L112-L113

Added lines #L112 - L113 were not covered by tests

if max_derivatives not in {0, 1, 2}:
raise ValueError("max_derivatives must be 0, 1 or 2")
msg = "max_derivatives must be 0, 1 or 2"
raise ValueError(msg)

Check warning on line 117 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L116-L117

Added lines #L116 - L117 were not covered by tests

if max_derivatives == 0:
func = sympy.lambdify(theta, expr, modules=np)
Expand Down Expand Up @@ -152,9 +155,8 @@
if derivatives == 2 and max_derivatives == 2:
return funcs[0](theta), funcs[1](theta), funcs[2](theta)

raise ValueError(
f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
)
msg = f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
raise ValueError(msg)

Check warning on line 159 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L158-L159

Added lines #L158 - L159 were not covered by tests

else:
f0v = sympy.lambdify(theta, exprs[0], modules=np)
Expand All @@ -179,9 +181,8 @@
if derivatives == 2 and max_derivatives == 2:
return (*f01(theta), f2(theta))

raise ValueError(
f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
)
msg = f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
raise ValueError(msg)

Check warning on line 185 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L184-L185

Added lines #L184 - L185 were not covered by tests

return D

Expand Down Expand Up @@ -240,7 +241,8 @@
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2

raise ValueError("derivatives must be 0, 1, or 2")
msg = "derivatives must be 0, 1, or 2"
raise ValueError(msg)

Check warning on line 245 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L244-L245

Added lines #L244 - L245 were not covered by tests

return D

Expand Down Expand Up @@ -278,18 +280,23 @@
"""
if Ks is not None:
if k is not None:
raise TypeError("cannot pass both Ks and k")
msg = "cannot pass both Ks and k"
raise TypeError(msg)

Check warning on line 284 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L283-L284

Added lines #L283 - L284 were not covered by tests
if Ks <= 0:
raise ValueError("Ks must be positive")
msg = "Ks must be positive"
raise ValueError(msg)

Check warning on line 287 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L286-L287

Added lines #L286 - L287 were not covered by tests
return Ks

if k is not None:
if k <= 0:
raise ValueError("k must be positive")
msg = "k must be positive"
raise ValueError(msg)

Check warning on line 293 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L292-L293

Added lines #L292 - L293 were not covered by tests
if nu <= 0:
raise ValueError("nu must be positive")
msg = "nu must be positive"
raise ValueError(msg)

Check warning on line 296 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L295-L296

Added lines #L295 - L296 were not covered by tests
if g <= 0:
raise ValueError("g must be positive")
msg = "g must be positive"
raise ValueError(msg)

Check warning on line 299 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L298-L299

Added lines #L298 - L299 were not covered by tests
return g * k / nu

return 1
Expand Down Expand Up @@ -373,12 +380,14 @@
Papers, Colorado State University, 1964, vol. 24, p. 37.
"""
if alpha <= 0:
raise ValueError("alpha must be positive")
msg = "alpha must be positive"
raise ValueError(msg)

Check warning on line 384 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L383-L384

Added lines #L383 - L384 were not covered by tests

Ks = _as_Ks(Ks=Ks, k=k, nu=nu, g=g)

if theta_range[1] <= theta_range[0]:
raise ValueError("theta_range[1] must be greater than theta_range[0]")
msg = "theta_range[1] must be greater than theta_range[0]"
raise ValueError(msg)

Check warning on line 390 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L389-L390

Added lines #L389 - L390 were not covered by tests

# - Code generated with functionstr() from ../symbolic/generate.py - #
# Source: ../symbolic/brooks_and_corey.py
Expand All @@ -398,7 +407,8 @@
d2D_dtheta2 = x4 * (-3 * x3 + 2 + x2**2 / n**2) / x0**3
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

Check warning on line 411 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L410-L411

Added lines #L410 - L411 were not covered by tests

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -498,24 +508,30 @@
"""
if n is not None:
if m is not None:
raise TypeError("cannot pass both n and m")
msg = "cannot pass both n and m"
raise TypeError(msg)

Check warning on line 512 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L511-L512

Added lines #L511 - L512 were not covered by tests
if n <= 1:
raise ValueError("n must be greater than 1.0")
msg = "n must be greater than 1.0"
raise ValueError(msg)

Check warning on line 515 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L514-L515

Added lines #L514 - L515 were not covered by tests
m = 1 - 1 / n

elif m is None:
raise TypeError("either n or m must be given")
msg = "either n or m must be given"
raise TypeError(msg)

Check warning on line 520 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L519-L520

Added lines #L519 - L520 were not covered by tests

if not (0 < m < 1):
raise ValueError("m must be strictly between 0.0 and 1.0")
msg = "m must be strictly between 0.0 and 1.0"
raise ValueError(msg)

Check warning on line 524 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L523-L524

Added lines #L523 - L524 were not covered by tests

if alpha <= 0:
raise ValueError("alpha must be positive")
msg = "alpha must be positive"
raise ValueError(msg)

Check warning on line 528 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L527-L528

Added lines #L527 - L528 were not covered by tests

Ks = _as_Ks(Ks=Ks, k=k, nu=nu, g=g)

if theta_range[1] - theta_range[0] <= 0:
raise ValueError("theta_range[1] must be greater than theta_range[0]")
msg = "theta_range[1] must be greater than theta_range[0]"
raise ValueError(msg)

Check warning on line 534 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L533-L534

Added lines #L533 - L534 were not covered by tests

# - Code generated with functionstr() from ../symbolic/generate.py - #
# Source: ../symbolic/van_genuchten.py
Expand Down Expand Up @@ -578,7 +594,8 @@
)
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

Check warning on line 598 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L597-L598

Added lines #L597 - L598 were not covered by tests

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -825,7 +842,8 @@
)
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

Check warning on line 846 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L845-L846

Added lines #L845 - L846 were not covered by tests

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -919,7 +937,8 @@
)
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

Check warning on line 941 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L940-L941

Added lines #L940 - L941 were not covered by tests

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -1014,7 +1033,8 @@
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2

raise ValueError("derivatives must be 0, 1, or 2")
msg = "derivatives must be 0, 1, or 2"
raise ValueError(msg)

Check warning on line 1037 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L1036-L1037

Added lines #L1036 - L1037 were not covered by tests

return D

Expand Down Expand Up @@ -1046,15 +1066,18 @@
try:
D_, dD_dtheta = D(theta, 1)
except (ValueError, ArithmeticError) as e:
raise ValueError(f"D({theta}, 1) failed with {e.__class__.__name__}") from e
msg = f"D({theta}, 1) failed with {e.__class__.__name__}"
raise ValueError(msg) from e

Check warning on line 1070 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L1069-L1070

Added lines #L1069 - L1070 were not covered by tests

try:
D_ = float(D_)
dD_dtheta = float(dD_dtheta)
except TypeError as e:
raise ValueError(f"D({theta}, 1) returned wrong type") from e
msg = f"D({theta}, 1) returned wrong type"
raise ValueError(msg) from e

Check warning on line 1077 in fronts/D.py

View check run for this annotation

Codecov / codecov/patch

fronts/D.py#L1076-L1077

Added lines #L1076 - L1077 were not covered by tests

if not np.isfinite(D_) or D_ <= 0 or not np.isfinite(dD_dtheta):
raise ValueError(f"D({theta}, 1) returned invalid value")
msg = f"D({theta}, 1) returned invalid value"
raise ValueError(msg)

return D_
11 changes: 6 additions & 5 deletions fronts/_boltzmann.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@
"""
if o is not None:
if r is not None or t is not None:
raise TypeError("must pass either r and t, or just o")
msg = "must pass either r and t, or just o"
raise TypeError(msg)

Check warning on line 198 in fronts/_boltzmann.py

View check run for this annotation

Codecov / codecov/patch

fronts/_boltzmann.py#L197-L198

Added lines #L197 - L198 were not covered by tests
return o

if r is None or t is None:
raise TypeError("must pass either r and t, or just o")
msg = "must pass either r and t, or just o"
raise TypeError(msg)

Check warning on line 203 in fronts/_boltzmann.py

View check run for this annotation

Codecov / codecov/patch

fronts/_boltzmann.py#L202-L203

Added lines #L202 - L203 were not covered by tests
return _o(r, t)


Expand Down Expand Up @@ -312,9 +314,8 @@
try:
k = _k[radial]
except KeyError:
raise ValueError(
f"radial must be one of {{{', '.join(repr(key) for key in _k)}}}"
) from None
msg = f"radial must be one of {{{', '.join(repr(key) for key in _k)}}}"
raise ValueError(msg) from None

Check warning on line 318 in fronts/_boltzmann.py

View check run for this annotation

Codecov / codecov/patch

fronts/_boltzmann.py#L317-L318

Added lines #L317 - L318 were not covered by tests

def fun(o, y):
theta, dtheta_do = y
Expand Down
13 changes: 8 additions & 5 deletions fronts/_inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
first derivative at ``theta``
* ``D(theta, 2)`` returns the value of :math:`D`, its first
derivative, and its second derivative at ``theta``

In all cases, the argument ``theta`` may be a single float or a NumPy
array.

Expand All @@ -66,20 +66,22 @@
[1] GERLERO, G. S.; BERLI, C. L. A.; KLER, P. A. Open-source
high-performance software packages for direct and inverse solving of
horizontal capillary flow. Capillarity, 2023, vol. 6, no. 2, pp. 31-40.

[2] BRUCE, R. R.; KLUTE, A. The measurement of soil moisture diffusivity.
Soil Science Society of America Journal, 1956, vol. 20, no. 4, pp. 458-462.
"""
o = np.asarray(o)

if not np.all(np.diff(o) > 0):
raise ValueError("o must be strictly increasing")
msg = "o must be strictly increasing"
raise ValueError(msg)

Check warning on line 77 in fronts/_inverse.py

View check run for this annotation

Codecov / codecov/patch

fronts/_inverse.py#L76-L77

Added lines #L76 - L77 were not covered by tests

samples = np.asarray(samples)

dsamples = np.diff(samples)
if not (np.all(dsamples >= -1e-12) or np.all(dsamples <= 1e-12)):
raise ValueError("samples must be monotonic")
msg = "samples must be monotonic"
raise ValueError(msg)

Check warning on line 84 in fronts/_inverse.py

View check run for this annotation

Codecov / codecov/patch

fronts/_inverse.py#L83-L84

Added lines #L83 - L84 were not covered by tests

i = samples[-1]

Expand Down Expand Up @@ -118,7 +120,8 @@
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2

raise ValueError("derivatives must be 0, 1, or 2")
msg = "derivatives must be 0, 1, or 2"
raise ValueError(msg)

Check warning on line 124 in fronts/_inverse.py

View check run for this annotation

Codecov / codecov/patch

fronts/_inverse.py#L123-L124

Added lines #L123 - L124 were not covered by tests

return D

Expand Down
Loading