From b41cbc9b5dc0aa382d43c8a23b83f4ed20b180bb Mon Sep 17 00:00:00 2001 From: "Alexander J. Pfleger" <70842573+AJPfleger@users.noreply.github.com> Date: Sat, 22 Jun 2024 11:16:43 +0200 Subject: [PATCH] ci: extend black formatting to jupyter notebooks (#3304) blocked by: - https://github.com/acts-project/acts/pull/3305 --- .github/workflows/checks.yml | 2 +- .pre-commit-config.yaml | 7 ++- Tests/IntegrationTests/Magfield.ipynb | 85 +++++++++++++++------------ docs/SolenoidField.ipynb | 20 +++++-- 4 files changed, 68 insertions(+), 46 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 5ec5f1a88a5..dfbf1fd5d0b 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -35,7 +35,7 @@ jobs: with: python-version: '3.12' - name: Install black - run: pip install black==24.4.2 + run: pip install black[jupyter]==24.4.2 - name: Run black format check run: black --check . --extend-exclude ".*thirdparty.*" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e98889960ed..b9b73432170 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,8 +14,9 @@ repos: - id: check-yaml - id: check-added-large-files - # Using this mirror lets us use mypyc-compiled black, which is about 2x faster - - repo: https://github.com/psf/black-pre-commit-mirror + + # Using this mirror lets us use mypyc-compiled black, which is about 2x faster + - repo: https://github.com/psf/black-pre-commit-mirror rev: 24.4.2 hooks: - - id: black + - id: black-jupyter diff --git a/Tests/IntegrationTests/Magfield.ipynb b/Tests/IntegrationTests/Magfield.ipynb index d57f71d63d8..de79afb5fe5 100644 --- a/Tests/IntegrationTests/Magfield.ipynb +++ b/Tests/IntegrationTests/Magfield.ipynb @@ -14,8 +14,9 @@ "import numpy as np\n", "import scipy\n", "import scipy.stats\n", - "plt.rcParams[\"figure.figsize\"] = (12,7)\n", - "#plt.rc('text', usetex=True)\n", + "\n", + "plt.rcParams[\"figure.figsize\"] = (12, 7)\n", + "# plt.rc('text', usetex=True)\n", "from pathlib import Path" ] }, @@ -260,7 +261,7 @@ } ], "source": [ - "df = pd.read_csv(bp/\"solenoid.csv\", delimiter=\";\")\n", + "df = pd.read_csv(bp / \"solenoid.csv\", delimiter=\";\")\n", "df[\"B\"] = np.sqrt(df.B_x**2 + df.B_y**2 + df.B_z**2)\n", "display(df.head())\n", "display(df.tail())" @@ -285,7 +286,7 @@ } ], "source": [ - "fig, ax = plt.subplots(figsize=(8,5))\n", + "fig, ax = plt.subplots(figsize=(8, 5))\n", "ax.quiver(df.z, df.x, df.B_z, df.B_x, df.B, pivot=\"mid\")\n", "ax.set_ylim(bottom=0)\n", "ax.set_xlabel(\"z [mm]\")\n", @@ -503,17 +504,17 @@ } ], "source": [ - "dfmap = pd.read_csv(bp/\"solenoidmap.csv\", delimiter=\";\")\n", - "dfmap[\"B\"] = np.sqrt(dfmap.B_r**2 + dfmap.B_z**2)#*1e16\n", - "#df.i = df.i.astype(int)\n", - "#df.j = df.j.astype(int)\n", + "dfmap = pd.read_csv(bp / \"solenoidmap.csv\", delimiter=\";\")\n", + "dfmap[\"B\"] = np.sqrt(dfmap.B_r**2 + dfmap.B_z**2) # *1e16\n", + "# df.i = df.i.astype(int)\n", + "# df.j = df.j.astype(int)\n", "nBinsR = dfmap.i.max()\n", "nBinsZ = dfmap.j.max()\n", "rmin = dfmap.r.min()\n", "rmax = dfmap.r.max()\n", "zmin = dfmap.z.min()\n", "zmax = dfmap.z.max()\n", - "rmax -= rmax/nBinsR\n", + "rmax -= rmax / nBinsR\n", "print(nBinsR, nBinsZ)\n", "print(rmin, rmax, zmin, zmax)\n", "display(dfmap.head())\n", @@ -539,22 +540,22 @@ } ], "source": [ - "#hist, xedges, yedges = np.histogram2d([], [], bins=(nBinsZ, nBinsR), range=((zmin, zmax), (rmin, rmax))) \n", - "#extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]\n", + "# hist, xedges, yedges = np.histogram2d([], [], bins=(nBinsZ, nBinsR), range=((zmin, zmax), (rmin, rmax)))\n", + "# extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]\n", "\n", "\n", - "hist = np.zeros((nBinsR-1, nBinsZ))\n", + "hist = np.zeros((nBinsR - 1, nBinsZ))\n", "\n", "for index, row in dfmap.iterrows():\n", - " i, j = int(row.i-1), int(row.j-1)\n", - " if i 100], bins=bins, range=(0, 5), label=r\"$r>100$\");\n", - "plt.hist(df.B_ratio[df.r < 100], histtype='step', bins=bins, range=(0, 5), fill=False, linewidth=3, label=r\"$r<100$\");\n", + "plt.hist(df.B_ratio[df.r > 100], bins=bins, range=(0, 5), label=r\"$r>100$\")\n", + "plt.hist(\n", + " df.B_ratio[df.r < 100],\n", + " histtype=\"step\",\n", + " bins=bins,\n", + " range=(0, 5),\n", + " fill=False,\n", + " linewidth=3,\n", + " label=r\"$r<100$\",\n", + ")\n", "plt.legend()\n", - "#df.Berr[df.Berr > -2].min()" + "# df.Berr[df.Berr > -2].min()" ] }, { @@ -773,10 +785,11 @@ " x = np.copy(x)\n", " step = abs(x[1] - x[0])\n", " xmin, xmax = x.min(), x.max()\n", - " #print(x)\n", - " x += step/2\n", - " #print(x)\n", - " plt.hist(x[:-1], bins=len(x)-1, weights=y, range=(xmin, xmax), **kwargs)\n", + " # print(x)\n", + " x += step / 2\n", + " # print(x)\n", + " plt.hist(x[:-1], bins=len(x) - 1, weights=y, range=(xmin, xmax), **kwargs)\n", + "\n", "\n", "plt.subplot(211)\n", "hist, bin_edges_r, _ = scipy.stats.binned_statistic(df.r, df.B, bins=50)\n", diff --git a/docs/SolenoidField.ipynb b/docs/SolenoidField.ipynb index 5d61f686724..1c830f450d0 100644 --- a/docs/SolenoidField.ipynb +++ b/docs/SolenoidField.ipynb @@ -47,10 +47,12 @@ "outputs": [], "source": [ "from subprocess import check_output\n", + "\n", + "\n", "def asciitex(eq, lc=\"/// \"):\n", " out = check_output([\"asciitex\", eq]).decode(\"utf-8\").rstrip()\n", " out = \"\\n\".join([lc + l for l in out.split(\"\\n\")])\n", - " print(out,\"\\n\")" + " print(out, \"\\n\")" ] }, { @@ -59,16 +61,22 @@ "metadata": {}, "outputs": [], "source": [ - "asciitex(r\"E_1(k^2) = \\int_0^{\\pi/2} \\left( 1 - k^2 \\sin^2{\\theta} \\right)^{-1/2} d\\theta\")\n", + "asciitex(\n", + " r\"E_1(k^2) = \\int_0^{\\pi/2} \\left( 1 - k^2 \\sin^2{\\theta} \\right)^{-1/2} d\\theta\"\n", + ")\n", "asciitex(r\"E_2(k^2) = \\int_0^{\\pi/2}\\sqrt{1 - k^2 \\sin^2{\\theta}} d\\theta\")\n", "\n", "asciitex(r\"k^2 = \\frac{4Rr}{(R+r)^2 + z^2}\")\n", "\n", - "asciitex(r\"B_r(r, z) = \\frac{\\mu_0 I}{4\\pi} \\frac{kz}{\\sqrt{Rr^3}} \"\n", - " r\"\\left[ \\left(\\frac{2-k^2}{2-2k^2}\\right)E_2(k^2) - E_1(k^2) \\right]\")\n", + "asciitex(\n", + " r\"B_r(r, z) = \\frac{\\mu_0 I}{4\\pi} \\frac{kz}{\\sqrt{Rr^3}} \"\n", + " r\"\\left[ \\left(\\frac{2-k^2}{2-2k^2}\\right)E_2(k^2) - E_1(k^2) \\right]\"\n", + ")\n", "\n", - "asciitex(r\"B_z(r,z) = \\frac{\\mu_0 I}{4\\pi} \\frac{k}{\\sqrt{Rr}} \"\n", - " r\"\\left[ \\left( \\frac{(R+r)k^2-2r}{2r(1-k^2)} \\right) E_2(k^2) + E_1(k^2) \\right]\")" + "asciitex(\n", + " r\"B_z(r,z) = \\frac{\\mu_0 I}{4\\pi} \\frac{k}{\\sqrt{Rr}} \"\n", + " r\"\\left[ \\left( \\frac{(R+r)k^2-2r}{2r(1-k^2)} \\right) E_2(k^2) + E_1(k^2) \\right]\"\n", + ")" ] }, {