Skip to content

Commit

Permalink
ci: extend black formatting to jupyter notebooks (#3304)
Browse files Browse the repository at this point in the history
blocked by:
- #3305
  • Loading branch information
AJPfleger authored Jun 22, 2024
1 parent 5ecd0ac commit b41cbc9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"

Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
85 changes: 49 additions & 36 deletions Tests/IntegrationTests/Magfield.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down Expand Up @@ -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())"
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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<nBinsR-1:\n",
" i, j = int(row.i - 1), int(row.j - 1)\n",
" if i < nBinsR - 1:\n",
" hist[i][j] = row.B\n",
" \n",
" #hist[int(row.i-1), int(row.j-1)] = math.sqrt(row.B_r**2 + row.B_z**2)\n",
"\n",
"#print(hist)\n",
" # hist[int(row.i-1), int(row.j-1)] = math.sqrt(row.B_r**2 + row.B_z**2)\n",
"\n",
"# print(hist)\n",
"fig, ax = plt.subplots()\n",
"ax.imshow(hist, origin='lower', extent=(zmin, zmax, rmin, rmax), aspect=2)\n",
"ax.imshow(hist, origin=\"lower\", extent=(zmin, zmax, rmin, rmax), aspect=2)\n",
"fig.savefig(Path.home() / \"dev/acts-core/solenoid.png\")"
]
},
Expand All @@ -564,8 +565,8 @@
"metadata": {},
"outputs": [],
"source": [
"#z, r = np.mgrid[-4.:4.:400j,-2.:2.:200j]\n",
"#z, r"
"# z, r = np.mgrid[-4.:4.:400j,-2.:2.:200j]\n",
"# z, r"
]
},
{
Expand All @@ -574,17 +575,19 @@
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"/Users/pagessin/dev/acts-core/build/magfield_lookup.csv\", delimiter=\";\")\n",
"df = pd.read_csv(\n",
" \"/Users/pagessin/dev/acts-core/build/magfield_lookup.csv\", delimiter=\";\"\n",
")\n",
"df[\"r\"] = np.sqrt(df.x**2 + df.y**2)\n",
"df[\"B_r\"] = np.sqrt(df.B_x**2 + df.B_y**2)\n",
"df[\"B\"] = np.sqrt(df.B_x**2 + df.B_y**2 + df.B_z**2)\n",
"df[\"Bm_r\"] = np.sqrt(df.Bm_x**2 + df.Bm_y**2)\n",
"df[\"Bm\"] = np.sqrt(df.Bm_x**2 + df.Bm_y**2 + df.Bm_z**2)\n",
"df[\"Berr\"] = (df.B - df.Bm)/df.B\n",
"df[\"Babserr\"] = np.abs(df.B - df.Bm)/df.B\n",
"df[\"Berr\"] = (df.B - df.Bm) / df.B\n",
"df[\"Babserr\"] = np.abs(df.B - df.Bm) / df.B\n",
"df[\"B_ratio\"] = df.Bm / df.B\n",
"#display(df.head())\n",
"#display(df.tail())"
"# display(df.head())\n",
"# display(df.tail())"
]
},
{
Expand All @@ -593,7 +596,7 @@
"metadata": {},
"outputs": [],
"source": [
"#df = df[df[\"r\"] < 1220]"
"# df = df[df[\"r\"] < 1220]"
]
},
{
Expand Down Expand Up @@ -672,9 +675,10 @@
"source": [
"def hist(x, y, v, bins, extent):\n",
" hist, *_ = scipy.stats.binned_statistic_2d(x, y, v, bins=bins)\n",
" plt.imshow(hist, origin=\"lower\", extent=extent, aspect='auto')\n",
" plt.imshow(hist, origin=\"lower\", extent=extent, aspect=\"auto\")\n",
"\n",
"bins = (50, 25) \n",
"\n",
"bins = (50, 25)\n",
"\n",
"hist(df.r, df.z, df.B, bins=bins, extent=(zmin, zmax, rmin, rmax))\n",
"plt.colorbar()\n",
Expand Down Expand Up @@ -721,14 +725,22 @@
"source": [
"plt.semilogy()\n",
"bins = 200\n",
"#print(plt.xlabel)\n",
"# print(plt.xlabel)\n",
"plt.title(\"Mapped field value over 'true' field value\")\n",
"plt.xlabel(r\"$B_{m}/B$\")\n",
"plt.ylabel(\"count\")\n",
"plt.hist(df.B_ratio[df.r > 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()"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down
20 changes: 14 additions & 6 deletions docs/SolenoidField.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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\")"
]
},
{
Expand All @@ -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",
")"
]
},
{
Expand Down

0 comments on commit b41cbc9

Please sign in to comment.