Skip to content

Commit 7da8b79

Browse files
authored
Merge pull request #81 from pdebench/fix/linter_warnings
Fix ruff linter warnings
2 parents 9f3ca2b + 0cb1e07 commit 7da8b79

Some content is hidden

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

50 files changed

+575
-631
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ repos:
4040
args: [--prose-wrap=always]
4141

4242
- repo: https://github.com/astral-sh/ruff-pre-commit
43-
rev: "v0.1.14"
43+
rev: "v0.8.4"
4444
hooks:
4545
- id: ruff
4646
args: ["--fix", "--show-fixes"]
4747
- id: ruff-format
4848

49-
- repo: https://github.com/pre-commit/mirrors-mypy
50-
rev: "v1.8.0"
51-
hooks:
52-
- id: mypy
53-
files: pdebench|tests
54-
args: []
55-
additional_dependencies:
56-
- pytest
49+
# - repo: https://github.com/pre-commit/mirrors-mypy
50+
# rev: "v1.8.0"
51+
# hooks:
52+
# - id: mypy
53+
# files: pdebench|tests
54+
# args: []
55+
# additional_dependencies:
56+
# - pytest
5757

5858
- repo: https://github.com/codespell-project/codespell
5959
rev: "v2.2.6"
@@ -62,7 +62,7 @@ repos:
6262
exclude_types: [jupyter]
6363

6464
- repo: https://github.com/shellcheck-py/shellcheck-py
65-
rev: "v0.9.0.6"
65+
rev: "v0.10.0.1"
6666
hooks:
6767
- id: shellcheck
6868

noxfile.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from __future__ import annotations
2+
3+
import shutil
4+
from pathlib import Path
5+
6+
import nox
7+
8+
DIR = Path(__file__).parent.resolve()
9+
10+
nox.needs_version = ">=2024.3.2"
11+
nox.options.sessions = ["precommit", "pylint", "tests", "build"]
12+
nox.options.default_venv_backend = "uv|mamba|virtualenv"
13+
14+
15+
@nox.session(python=["3.10"])
16+
def precommit(session: nox.Session) -> None:
17+
"""
18+
Run the linter.
19+
"""
20+
session.install("pre-commit")
21+
session.run(
22+
"pre-commit", "run", "--all-files", "--show-diff-on-failure", *session.posargs
23+
)
24+
25+
26+
@nox.session(python=["3.10"])
27+
def pylint(session: nox.Session) -> None:
28+
"""
29+
Run PyLint.
30+
"""
31+
# This needs to be installed into the package environment, and is slower
32+
# than a pre-commit check
33+
session.install(".", "pylint>=3.2")
34+
session.run("pylint", "pdebench", *session.posargs)
35+
36+
37+
@nox.session(python=["3.10"])
38+
def tests(session: nox.Session) -> None:
39+
"""
40+
Run the unit and regular tests.
41+
"""
42+
session.install(".[test]")
43+
session.run("pytest", *session.posargs)
44+
45+
46+
@nox.session(python=["3.10"])
47+
def build(session: nox.Session) -> None:
48+
"""
49+
Build an SDist and wheel.
50+
"""
51+
52+
build_path = DIR.joinpath("build")
53+
if build_path.exists():
54+
shutil.rmtree(build_path)
55+
56+
session.install("build")
57+
session.run("python", "-m", "build")

pdebench/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
2020
2121
"""
22+
2223
from __future__ import annotations
2324

25+
import logging
26+
27+
_logger = logging.getLogger(__name__)
28+
_logger.propagate = False
29+
2430
__version__ = "0.0.1"
2531
__author__ = "Makoto Takamoto, Timothy Praditia, Raphael Leiteritz, Dan MacKinlay, Francesco Alesiani, Dirk Pflüger, Mathias Niepert"
26-
__credits__ = "NEC labs Europe, University of Stuttgart, CSIRO" "s Data61"
32+
__credits__ = "NEC labs Europe, University of Stuttgart, CSIRO's Data61"

pdebench/data_gen/data_gen_NLE/AdvectionEq/advection_exact_Hydra.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
"""
32
<NAME OF THE PROGRAM THIS FILE BELONGS TO>
43
@@ -144,8 +143,10 @@
144143
145144
THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
146145
"""
146+
147147
from __future__ import annotations
148148

149+
import logging
149150
import time
150151
from math import ceil
151152

@@ -157,11 +158,13 @@
157158
# Hydra
158159
from omegaconf import DictConfig
159160

161+
logger = logging.getLogger(__name__)
162+
160163

161164
# Init arguments with Hydra
162165
@hydra.main(config_path="config")
163166
def main(cfg: DictConfig) -> None:
164-
print(f"advection velocity: {cfg.args.beta}")
167+
logger.info("advection velocity: %f", cfg.args.beta)
165168

166169
# cell edge coordinate
167170
xe = jnp.linspace(cfg.args.xL, cfg.args.xR, cfg.args.nx + 1)
@@ -181,14 +184,14 @@ def evolve(u):
181184
uu = uu.at[0].set(u)
182185

183186
while t < cfg.args.fin_time:
184-
print(f"save data at t = {t:.3f}")
187+
logger.info("save data at t = %f", t)
185188
u = set_function(xc, t, cfg.args.beta)
186189
uu = uu.at[i_save].set(u)
187190
t += cfg.args.dt_save
188191
i_save += 1
189192

190193
tm_fin = time.time()
191-
print(f"total elapsed time is {tm_fin - tm_ini} sec")
194+
logger.info("total elapsed time is %f sec", tm_fin - tm_ini)
192195
uu = uu.at[-1].set(u)
193196
return uu, t
194197

@@ -199,9 +202,9 @@ def set_function(x, t, beta):
199202
u = set_function(xc, t=0, beta=cfg.args.beta)
200203
u = device_put(u) # putting variables in GPU (not necessary??)
201204
uu, t = evolve(u)
202-
print(f"final time is: {t:.3f}")
205+
logger.info("final time is: %f", t)
203206

204-
print("data saving...")
207+
logger.info("data saving...")
205208
cwd = hydra.utils.get_original_cwd() + "/"
206209
jnp.save(cwd + cfg.args.save + "/Advection_beta" + str(cfg.args.beta), uu)
207210
jnp.save(cwd + cfg.args.save + "/x_coordinate", xe)

pdebench/data_gen/data_gen_NLE/AdvectionEq/advection_multi_solution_Hydra.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
"""
32
<NAME OF THE PROGRAM THIS FILE BELONGS TO>
43
@@ -144,22 +143,29 @@
144143
145144
THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
146145
"""
146+
147147
from __future__ import annotations
148148

149149
import random
150+
import sys
151+
152+
# Hydra
153+
from math import ceil, exp, log
150154
from pathlib import Path
151155

152156
import hydra
153157
import jax
154158
import jax.numpy as jnp
155159
from jax import device_put, lax
156-
157-
# Hydra
158160
from omegaconf import DictConfig
159161

160162
sys.path.append("..")
163+
import logging
164+
161165
from utils import Courant, bc, init_multi, limiting
162166

167+
logger = logging.getLogger(__name__)
168+
163169

164170
def _pass(carry):
165171
return carry
@@ -192,7 +198,7 @@ def main(cfg: DictConfig) -> None:
192198
else:
193199
beta = cfg.multi.beta
194200

195-
print("beta: ", beta)
201+
logger.info("beta: %f", beta)
196202

197203
@jax.jit
198204
def evolve(u):
@@ -204,7 +210,8 @@ def evolve(u):
204210
uu = jnp.zeros([it_tot, u.shape[0]])
205211
uu = uu.at[0].set(u)
206212

207-
cond_fun = lambda x: x[0] < fin_time
213+
def cond_fun(x):
214+
return x[0] < fin_time
208215

209216
def _body_fun(carry):
210217
def _show(_carry):
@@ -226,9 +233,7 @@ def _show(_carry):
226233

227234
carry = t, tsave, steps, i_save, dt, u, uu
228235
t, tsave, steps, i_save, dt, u, uu = lax.while_loop(cond_fun, _body_fun, carry)
229-
uu = uu.at[-1].set(u)
230-
231-
return uu
236+
return uu.at[-1].set(u)
232237

233238
@jax.jit
234239
def simulation_fn(i, carry):
@@ -265,12 +270,11 @@ def flux(u):
265270
fL = uL * beta
266271
fR = uR * beta
267272
# upwind advection scheme
268-
f_upwd = 0.5 * (
273+
return 0.5 * (
269274
fR[1 : cfg.multi.nx + 2]
270275
+ fL[2 : cfg.multi.nx + 3]
271276
- jnp.abs(beta) * (uL[2 : cfg.multi.nx + 3] - uR[1 : cfg.multi.nx + 2])
272277
)
273-
return f_upwd
274278

275279
u = init_multi(xc, numbers=cfg.multi.numbers, k_tot=4, init_key=cfg.multi.init_key)
276280
u = device_put(u) # putting variables in GPU (not necessary??)
@@ -285,7 +289,7 @@ def flux(u):
285289
# reshape before saving
286290
uu = uu.reshape((-1, *uu.shape[2:]))
287291

288-
print("data saving...")
292+
logger.info("data saving...")
289293
cwd = hydra.utils.get_original_cwd() + "/"
290294
Path(cwd + cfg.multi.save).mkdir(parents=True, exist_ok=True)
291295
jnp.save(cwd + cfg.multi.save + "1D_Advection_Sols_beta" + str(beta)[:5], uu)

pdebench/data_gen/data_gen_NLE/BurgersEq/burgers_Hydra.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
"""
32
<NAME OF THE PROGRAM THIS FILE BELONGS TO>
43
@@ -144,8 +143,10 @@
144143
145144
THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
146145
"""
146+
147147
from __future__ import annotations
148148

149+
import logging
149150
import sys
150151
import time
151152
from math import ceil
@@ -161,6 +162,8 @@
161162
sys.path.append("..")
162163
from utils import Courant, Courant_diff, bc, init, limiting
163164

165+
logger = logging.getLogger(__name__)
166+
164167

165168
def _pass(carry):
166169
return carry
@@ -201,7 +204,8 @@ def evolve(u):
201204

202205
tm_ini = time.time()
203206

204-
cond_fun = lambda x: x[0] < fin_time
207+
def cond_fun(x):
208+
return x[0] < fin_time
205209

206210
def _body_fun(carry):
207211
def _save(_carry):
@@ -227,7 +231,7 @@ def _save(_carry):
227231
uu = uu.at[-1].set(u)
228232

229233
tm_fin = time.time()
230-
print(f"total elapsed time is {tm_fin - tm_ini} sec")
234+
logger.info("total elapsed time is %f sec", tm_fin - tm_ini)
231235
return uu, t
232236

233237
@jax.jit
@@ -285,9 +289,9 @@ def flux(u):
285289
u = init(xc=xc, mode=cfg.args.init_mode, u0=cfg.args.u0, du=cfg.args.du)
286290
u = device_put(u) # putting variables in GPU (not necessary??)
287291
uu, t = evolve(u)
288-
print(f"final time is: {t:.3f}")
292+
logger.info("final time is: %.3f", t)
289293

290-
print("data saving...")
294+
logger.info("data saving...")
291295
cwd = hydra.utils.get_original_cwd() + "/"
292296
if cfg.args.init_mode == "sinsin":
293297
jnp.save(

pdebench/data_gen/data_gen_NLE/BurgersEq/burgers_multi_solution_Hydra.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
"""
32
<NAME OF THE PROGRAM THIS FILE BELONGS TO>
43
@@ -144,8 +143,10 @@
144143
145144
THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
146145
"""
146+
147147
from __future__ import annotations
148148

149+
import logging
149150
import random
150151
import sys
151152
from math import ceil, exp, log
@@ -162,6 +163,8 @@
162163
sys.path.append("..")
163164
from utils import Courant, Courant_diff, bc, init_multi, limiting
164165

166+
logger = logging.getLogger(__name__)
167+
165168

166169
def _pass(carry):
167170
return carry
@@ -191,7 +194,7 @@ def main(cfg: DictConfig) -> None:
191194
) # uniform number between 0.01 to 100
192195
else:
193196
epsilon = cfg.multi.epsilon
194-
print("epsilon: ", epsilon)
197+
logger.info("epsilon: %f", epsilon)
195198
# t-coordinate
196199
it_tot = ceil((fin_time - ini_time) / dt_save) + 1
197200
tc = jnp.arange(it_tot + 1) * dt_save
@@ -206,7 +209,8 @@ def evolve(u):
206209
uu = jnp.zeros([it_tot, u.shape[0]])
207210
uu = uu.at[0].set(u)
208211

209-
cond_fun = lambda x: x[0] < fin_time
212+
def cond_fun(x):
213+
return x[0] < fin_time
210214

211215
def _body_fun(carry):
212216
def _show(_carry):
@@ -228,9 +232,7 @@ def _show(_carry):
228232

229233
carry = t, tsave, steps, i_save, dt, u, uu
230234
t, tsave, steps, i_save, dt, u, uu = lax.while_loop(cond_fun, _body_fun, carry)
231-
uu = uu.at[-1].set(u)
232-
233-
return uu
235+
return uu.at[-1].set(u)
234236

235237
@jax.jit
236238
def simulation_fn(i, carry):
@@ -301,7 +303,7 @@ def flux(u):
301303
# reshape before saving
302304
uu = uu.reshape((-1, *uu.shape[2:]))
303305

304-
print("data saving...")
306+
logger.info("data saving...")
305307
cwd = hydra.utils.get_original_cwd() + "/"
306308
Path(cwd + cfg.multi.save).mkdir(parents=True, exist_ok=True)
307309
jnp.save(cwd + cfg.multi.save + "1D_Burgers_Sols_Nu" + str(epsilon)[:5], uu)

0 commit comments

Comments
 (0)