From 9784ba8e85ff0c21d2686a15178cf1960fded9ab Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Mon, 5 Aug 2024 10:46:39 +0200
Subject: [PATCH] [py] Ruff auto-fixes.

---
 .pre-commit-config.yaml                       |  4 ++--
 .../MFront/ModCamClay_ShearTest.py            | 22 +++++++++----------
 .../SeabedResponse/Stationary_waves.ipynb     |  8 +++----
 .../Linear/PythonHertzContact/post.py         |  4 ++--
 .../Kregime_Propagating_jupyter.ipynb         |  1 -
 .../Kregime_Static_jupyter.ipynb              | 12 +++++-----
 .../surfing_pyvista.ipynb                     |  4 ++--
 7 files changed, 26 insertions(+), 29 deletions(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 4a4e2b3c215..35d1d5e6435 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -80,7 +80,7 @@ repos:
       - id: vale
         args: [--output=line, --minAlertLevel=error]
   - repo: https://github.com/astral-sh/ruff-pre-commit
-    rev: "v0.4.2"
+    rev: "v0.5.0"
     hooks:
       - id: ruff
         types_or: [python, pyi, jupyter]
@@ -90,7 +90,7 @@ repos:
   # Run this hook (and all other manual hooks if any) with:
   # pre-commit run --hook-stage manual
   - repo: https://github.com/astral-sh/ruff-pre-commit
-    rev: "v0.4.2"
+    rev: "v0.5.0"
     hooks:
       - id: ruff
         types_or: [python, pyi, jupyter]
diff --git a/MaterialLib/SolidModels/MFront/ModCamClay_ShearTest.py b/MaterialLib/SolidModels/MFront/ModCamClay_ShearTest.py
index c97d9db8e18..fd328879308 100644
--- a/MaterialLib/SolidModels/MFront/ModCamClay_ShearTest.py
+++ b/MaterialLib/SolidModels/MFront/ModCamClay_ShearTest.py
@@ -143,7 +143,7 @@ fig, ax = plt.subplots()
 ax.set_title("Total volume/solid volume over time")
 for k in range(runs):
     ax.plot(
-        ltime, results[2][k], label=prelabel + "%.2f" % (valueList[k])
+        ltime, results[2][k], label=prelabel + f"{valueList[k]:.2f}"
     )  # OCR: pc0/valueList[k]
 ax.set_xlabel("$t$ / s")
 ax.set_ylabel("volume ratio")
@@ -155,7 +155,7 @@ fig.savefig("ModCamClay_ParamStudy_VolumeRatio.pdf")
 fig, ax = plt.subplots()
 ax.set_title("Porosity over time")
 for k in range(runs):
-    ax.plot(ltime, results[3][k], label=prelabel + "%.2f" % (valueList[k]))
+    ax.plot(ltime, results[3][k], label=prelabel + f"{valueList[k]:.2f}")
 ax.set_xlabel("$t$ / s")
 ax.set_ylabel(r"$\phi$")
 ax.grid()
@@ -165,7 +165,7 @@ fig.savefig("ModCamClay_ParamStudy_Porosity.pdf")
 fig, ax = plt.subplots()
 # ax.set_title('Shear stress over shear strain')
 for k in range(runs):
-    ax.plot(results[4][k], results[5][k], label=prelabel + "%.2f" % (valueList[k]))
+    ax.plot(results[4][k], results[5][k], label=prelabel + f"{valueList[k]:.2f}")
 ax.set_xlabel(r"$\epsilon_{xy}$")
 ax.set_ylabel(r"$\sigma_{xy}$ / MPa")
 ax.grid()
@@ -175,9 +175,7 @@ fig.savefig("ModCamClay_ParamStudy_ShearCurves.pdf")
 fig, ax = plt.subplots()
 # ax.set_title('Plastic volumetric strain over shear strain')
 for k in range(runs):
-    ax.plot(
-        results[4][k], results[7][k] * 100, label=prelabel + "%.2f" % (valueList[k])
-    )
+    ax.plot(results[4][k], results[7][k] * 100, label=prelabel + f"{valueList[k]:.2f}")
 ax.set_xlabel(r"$\epsilon_{xy}$")
 ax.set_ylabel(r"${\epsilon}_p^V$ / %")
 ax.grid()
@@ -187,11 +185,11 @@ fig.savefig("ModCamClay_ParamStudy_eplVCurves.pdf")
 fig, ax = plt.subplots()
 ax.set_title("Hydrostatic pressure and von-Mises stress over time")
 for k in range(runs):
-    ax.plot(ltime, results[0][k], label=prelabel + "%.2f" % (valueList[k]))
+    ax.plot(ltime, results[0][k], label=prelabel + f"{valueList[k]:.2f}")
     ax.plot(
         ltime,
         results[1][k],
-        label=prelabel + "%.2f" % (valueList[k]),
+        label=prelabel + f"{valueList[k]:.2f}",
         linestyle="dashed",
     )
 ax.set_xlabel("$t$ / s")
@@ -205,10 +203,10 @@ for k in range(runs):
     ax.plot(
         ltime,
         results[1][k],
-        label=prelabel + "%.2f" % (valueList[k]),
+        label=prelabel + f"{valueList[k]:.2f}",
         linestyle="dashed",
     )
-    ax.plot(ltime, results[6][k], label=prelabel + "%.2f" % (valueList[k]))
+    ax.plot(ltime, results[6][k], label=prelabel + f"{valueList[k]:.2f}")
 ax.set_xlabel("$t$ / s")
 ax.set_ylabel("$p, p_c$ / MPa")
 ax.grid()
@@ -221,9 +219,9 @@ ax.plot(pRange, qFunct, color="grey", label="Initial")
 ax.plot(pRange, M * pRange, color="black", label="CSL")
 for k in range(runs):
     # final yield surface
-    ax.scatter(pRangeFinal[k], qFunctFinal[k], label=prelabel + "%.2f" % (valueList[k]))
+    ax.scatter(pRangeFinal[k], qFunctFinal[k], label=prelabel + f"{valueList[k]:.2f}")
     # stress trajectory
-    ax.plot(results[1][k], results[0][k], label=prelabel + "%.2f" % (valueList[k]))
+    ax.plot(results[1][k], results[0][k], label=prelabel + f"{valueList[k]:.2f}")
 ax.set_xlabel("$p$ / MPa")
 ax.set_ylabel("$q$ / MPa")
 ax.grid()
diff --git a/Tests/Data/HydroMechanics/SeabedResponse/Stationary_waves.ipynb b/Tests/Data/HydroMechanics/SeabedResponse/Stationary_waves.ipynb
index 6b545dde586..23ec8088474 100644
--- a/Tests/Data/HydroMechanics/SeabedResponse/Stationary_waves.ipynb
+++ b/Tests/Data/HydroMechanics/SeabedResponse/Stationary_waves.ipynb
@@ -341,7 +341,7 @@
     "        compute_pressure_and_stresses(t, 0, y)[0],\n",
     "        -y_rel,\n",
     "        color=colors[t],\n",
-    "        label=\"t = %.1f s\" % t,\n",
+    "        label=f\"t = {t:.1f} s\",\n",
     "    )\n",
     "\n",
     "t = 2.5\n",
@@ -1082,7 +1082,7 @@
     "        compute_pressure_and_stresses(t, x, y)[0],\n",
     "        -y_rel,\n",
     "        color=colors[t],\n",
-    "        label=\"analytical, t = %.1f s\" % t,\n",
+    "        label=f\"analytical, t = {t:.1f} s\",\n",
     "    )\n",
     "\n",
     "ax[1][0].plot(\n",
@@ -1125,12 +1125,12 @@
     "            \"o\",\n",
     "            markevery=10,\n",
     "            color=colors[t_num],\n",
-    "            label=\"numerical, t = %.1f s\" % t_num,\n",
+    "            label=f\"numerical, t = {t_num:.1f} s\",\n",
     "        )\n",
     "        ax[0][0].set_xlabel(\"$p$ / $\\\\tilde{p}$\")\n",
     "\n",
     "        ax[0][1].plot(\n",
-    "            f_abs_pressure, depth / 100, color=colors[t_num], label=\"t = %.1f s\" % t_num\n",
+    "            f_abs_pressure, depth / 100, color=colors[t_num], label=f\"t = {t_num:.1f} s\"\n",
     "        )\n",
     "        ax[0][1].set_xlabel(\"$\\\\Delta p /\\\\tilde{p}$\")\n",
     "\n",
diff --git a/Tests/Data/Mechanics/Linear/PythonHertzContact/post.py b/Tests/Data/Mechanics/Linear/PythonHertzContact/post.py
index c4bba3cbd6a..6d24a97d3ef 100755
--- a/Tests/Data/Mechanics/Linear/PythonHertzContact/post.py
+++ b/Tests/Data/Mechanics/Linear/PythonHertzContact/post.py
@@ -177,7 +177,7 @@ for t, fn in zip(ts, fns):
             except:
                 print(max(xis), r_contact)
                 raise
-            avg_stress[i] = 1.0 / rho_max * np.trapz(x=rhos, y=stress_int(xis))
+            avg_stress[i] = 1.0 / rho_max * np.trapezoid(x=rhos, y=stress_int(xis))
         # avg_stress[-1] = 0.0
 
         return rs_int, avg_stress
@@ -187,7 +187,7 @@ for t, fn in zip(ts, fns):
         rs_int = np.linspace(min(rs), max(rs), max(len(rs), 200))
         stress_int = interp1d(rs, stress, bounds_error=False, fill_value=0.0)
 
-        return 2.0 * np.pi * np.trapz(x=rs_int, y=rs_int * stress_int(rs_int))
+        return 2.0 * np.pi * np.trapezoid(x=rs_int, y=rs_int * stress_int(rs_int))
 
     if t > 0:
         plane.SetOrigin(0, y_at_r_contact, 0)
diff --git a/Tests/Data/PhaseField/Kregime_Propagating_jupyter_notebook/Kregime_Propagating_jupyter.ipynb b/Tests/Data/PhaseField/Kregime_Propagating_jupyter_notebook/Kregime_Propagating_jupyter.ipynb
index d376a7ef7ba..14f9651f5b2 100644
--- a/Tests/Data/PhaseField/Kregime_Propagating_jupyter_notebook/Kregime_Propagating_jupyter.ipynb
+++ b/Tests/Data/PhaseField/Kregime_Propagating_jupyter_notebook/Kregime_Propagating_jupyter.ipynb
@@ -133,7 +133,6 @@
    },
    "outputs": [],
    "source": [
-    "import argparse\n",
     "import math\n",
     "import os\n",
     "import re\n",
diff --git a/Tests/Data/PhaseField/kregime_jupyter_notebook/Kregime_Static_jupyter.ipynb b/Tests/Data/PhaseField/kregime_jupyter_notebook/Kregime_Static_jupyter.ipynb
index a79093c6081..f6374abafe7 100644
--- a/Tests/Data/PhaseField/kregime_jupyter_notebook/Kregime_Static_jupyter.ipynb
+++ b/Tests/Data/PhaseField/kregime_jupyter_notebook/Kregime_Static_jupyter.ipynb
@@ -303,7 +303,7 @@
     "    # mesh properties\n",
     "    ls = 2 * h\n",
     "    # generate prefix from properties\n",
-    "    filename = \"results_h_%0.4f\" % h\n",
+    "    filename = f\"results_h_{h:0.4f}\"\n",
     "    mesh_generation(0.1, h)\n",
     "    # Convert GMSH (.msh) meshes to VTU meshes appropriate for OGS simulation.\n",
     "    input_file = f\"{out_dir}/\" + meshname + \".msh\"  # noqa: F841\n",
@@ -429,7 +429,7 @@
     "        slice_mesh = mesh.slice(normal=normal, origin=point)\n",
     "        y_slice = slice_mesh.points[:, 1]\n",
     "        Wnode_slice = slice_mesh.point_data[\"Wnode\"]\n",
-    "        width_i = np.trapz(Wnode_slice, x=y_slice)\n",
+    "        width_i = np.trapz(Wnode_slice, x=y_slice)  # noqa: NPY201\n",
     "        if width_i >= 0:\n",
     "            width_line[i] = width_i\n",
     "        r_i[i] = (\n",
@@ -507,7 +507,7 @@
     "for j, h_j in enumerate(h_list):\n",
     "    r_i_num = []\n",
     "    width_line_num = []\n",
-    "    filename = \"results_h_%0.4f\" % h_j\n",
+    "    filename = f\"results_h_{h_j:0.4f}\"\n",
     "    print(filename)\n",
     "    width_calculation(filename)\n",
     "    r_i_num = width_calculation(filename)[0]\n",
@@ -524,7 +524,7 @@
     "        fillstyle=\"none\",\n",
     "        markersize=0,\n",
     "        linewidth=lineWIDTH[0],\n",
-    "        label=\"Closed form solution - $h= $%0.4f\" % h_j,\n",
+    "        label=f\"Closed form solution - $h= ${h_j:0.4f}\",\n",
     "    )\n",
     "    plt.plot(\n",
     "        np.array(r_i_num[:]),\n",
@@ -533,7 +533,7 @@
     "        fillstyle=\"none\",\n",
     "        markersize=6,\n",
     "        linewidth=lineWIDTH[1],\n",
-    "        label=\"VPF - $h =$%0.4f\" % h_j,\n",
+    "        label=f\"VPF - $h =${h_j:0.4f}\",\n",
     "    )\n",
     "    # ------------------------------------------------------------------------\n",
     "plt.rcParams[\"figure.figsize\"] = [40, 20]\n",
@@ -541,7 +541,7 @@
     "plt.ylabel(\"$w_n$ [m]\", fontsize=14)\n",
     "plt.xlabel(\"$r$ [m]\", fontsize=14)\n",
     "plt.grid(linestyle=\"dashed\")\n",
-    "plt.title(\"%s\" % phasefield_model)\n",
+    "plt.title(f\"{phasefield_model}\")\n",
     "\n",
     "legend = plt.legend(bbox_to_anchor=(1.04, 1), loc=\"upper left\")\n",
     "plt.show()"
diff --git a/Tests/Data/PhaseField/surfing_jupyter_notebook/surfing_pyvista.ipynb b/Tests/Data/PhaseField/surfing_jupyter_notebook/surfing_pyvista.ipynb
index a3ca95f9575..63066739bb3 100644
--- a/Tests/Data/PhaseField/surfing_jupyter_notebook/surfing_pyvista.ipynb
+++ b/Tests/Data/PhaseField/surfing_jupyter_notebook/surfing_pyvista.ipynb
@@ -633,7 +633,7 @@
     "    \"-ob\",\n",
     "    fillstyle=\"none\",\n",
     "    linewidth=1.5,\n",
-    "    label=\"Phase-field %s\" % phasefield_model,\n",
+    "    label=f\"Phase-field {phasefield_model}\",\n",
     ")\n",
     "plt.plot(\n",
     "    G_theta_time[:, 0],\n",
@@ -659,7 +659,7 @@
     "    \"-ob\",\n",
     "    fillstyle=\"none\",\n",
     "    linewidth=1.5,\n",
-    "    label=\"Phase-field %s\" % phasefield_model,\n",
+    "    label=f\"Phase-field {phasefield_model}\",\n",
     ")\n",
     "plt.grid(linestyle=\"dashed\")\n",
     "plt.xlim(-0.05, 0.8)\n",
-- 
GitLab