Skip to content

Commit

Permalink
Merge pull request #904 from minrk/nbqa
Browse files Browse the repository at this point in the history
run ruff via nbqa on notebooks
  • Loading branch information
minrk authored Oct 29, 2024
2 parents 067c417 + 05dc87c commit d7713f6
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 93 deletions.
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ repos:
- id: ruff
args:
- "--fix"
exclude_types:
- jupyter
- id: ruff-format
exclude_types:
- jupyter
# run ruff via nbqa, which has better and configurable handling of cell magics
# than ruff itself
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.9.0
hooks:
- id: nbqa-ruff-format
- id: nbqa-ruff-check
args:
- --fix
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
Expand Down
3 changes: 1 addition & 2 deletions docs/source/examples/Futures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"%%px --local --block\n",
"import os\n",
"import time\n",
"\n",
"import numpy as np\n",
"from numpy.linalg import norm"
]
Expand Down Expand Up @@ -186,8 +187,6 @@
}
],
"source": [
"import os\n",
"\n",
"f = rc[-1].apply(os.getpid)\n",
"f.add_done_callback(lambda _: print(\"I got PID: %i\" % _.result()))\n",
"f.result()"
Expand Down
14 changes: 8 additions & 6 deletions docs/source/examples/Monitoring an MPI Simulation - 1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@
"source": [
"%%px --block\n",
"# MPI initialization, library imports and sanity checks on all engines\n",
"from mpi4py import MPI\n",
"import numpy as np\n",
"import time\n",
"\n",
"from mpi4py import MPI\n",
"\n",
"mpi = MPI.COMM_WORLD\n",
"bcast = mpi.bcast\n",
"barrier = mpi.barrier\n",
"rank = mpi.rank\n",
"print(\"MPI rank: %i/%i\" % (mpi.rank,mpi.size))"
"print(\"MPI rank: %i/%i\" % (mpi.rank, mpi.size))"
]
},
{
Expand Down Expand Up @@ -229,7 +229,7 @@
" )\n",
" X, Y = np.meshgrid(Xax, Yax)\n",
" f = freqs[j]\n",
" Z = np.cos(f * (X ** 2 + Y ** 2))\n",
" Z = np.cos(f * (X**2 + Y**2))\n",
" # We add a small delay to simulate that a real-world computation\n",
" # would take much longer, and we ensure all nodes are synchronized\n",
" time.sleep(delay)\n",
Expand Down Expand Up @@ -533,7 +533,9 @@
],
"source": [
"%%px --target 0 --block\n",
"from ipyparallel import bind_kernel; bind_kernel()\n",
"from ipyparallel import bind_kernel\n",
"\n",
"bind_kernel()\n",
"%connect_info"
]
},
Expand Down Expand Up @@ -564,7 +566,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.9"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
11 changes: 6 additions & 5 deletions docs/source/examples/Monitoring an MPI Simulation - 2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,18 @@
"source": [
"%%px --block\n",
"# MPI initialization, library imports and sanity checks on all engines\n",
"import time\n",
"\n",
"from mpi4py import MPI\n",
"\n",
"# Load data publication API so engines can send data to notebook client\n",
"from ipyparallel.datapub import publish_data\n",
"import numpy as np\n",
"import time\n",
"\n",
"mpi = MPI.COMM_WORLD\n",
"bcast = mpi.bcast\n",
"barrier = mpi.barrier\n",
"rank = mpi.rank\n",
"print(\"MPI rank: %i/%i\" % (mpi.rank,mpi.size))"
"print(\"MPI rank: %i/%i\" % (mpi.rank, mpi.size))"
]
},
{
Expand Down Expand Up @@ -196,7 +197,7 @@
" )\n",
" X, Y = np.meshgrid(Xax, Yax)\n",
" f = freqs[j]\n",
" Z = np.cos(f * (X ** 2 + Y ** 2))\n",
" Z = np.cos(f * (X**2 + Y**2))\n",
"\n",
" # We are now going to publish data to the clients. We take advantage of fast\n",
" # MPI communications and gather the Z mesh at the rank 0 node in the Zcat variable:\n",
Expand Down Expand Up @@ -419,7 +420,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.9"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
23 changes: 13 additions & 10 deletions docs/source/examples/Parallel Magics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@
"source": [
"%%px\n",
"import psutil\n",
"\n",
"psutil.Process().nice(20 if psutil.POSIX else psutil.IDLE_PRIORITY_CLASS)"
]
},
Expand All @@ -578,8 +579,8 @@
"outputs": [],
"source": [
"%%px\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
Expand Down Expand Up @@ -619,10 +620,10 @@
],
"source": [
"%%px --noblock\n",
"x = np.linspace(0,np.pi,1000)\n",
"for n in range(id,12, stride):\n",
"x = np.linspace(0, np.pi, 1000)\n",
"for n in range(id, 12, stride):\n",
" print(n)\n",
" plt.plot(x,np.sin(n*x))\n",
" plt.plot(x, np.sin(n * x))\n",
"plt.title(\"Plot %i\" % id)"
]
},
Expand Down Expand Up @@ -1318,11 +1319,11 @@
],
"source": [
"%%px --group-outputs=engine\n",
"x = np.linspace(0,np.pi,1000)\n",
"for n in range(id+1,12, stride):\n",
"x = np.linspace(0, np.pi, 1000)\n",
"for n in range(id + 1, 12, stride):\n",
" print(n)\n",
" plt.figure()\n",
" plt.plot(x,np.sin(n*x))\n",
" plt.plot(x, np.sin(n * x))\n",
" plt.title(\"Plot %i\" % n)"
]
},
Expand Down Expand Up @@ -3261,6 +3262,7 @@
"source": [
"%%px\n",
"from numpy.random import random\n",
"\n",
"A = random((100, 100, 'invalid shape'))"
]
},
Expand Down Expand Up @@ -3343,10 +3345,10 @@
"source": [
"%%px\n",
"import time\n",
"\n",
"if rank == 0:\n",
" raise RuntimeError(\"rank 0 failed!\")\n",
"time.sleep(10)\n",
" "
"time.sleep(10)"
]
},
{
Expand Down Expand Up @@ -3469,6 +3471,7 @@
"source": [
"%%px --local\n",
"import os\n",
"\n",
"thispid = os.getpid()\n",
"print(thispid)"
]
Expand Down
6 changes: 3 additions & 3 deletions docs/source/examples/Using MPI with IPython Parallel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@
"rank = comm.Get_rank()\n",
"\n",
"if rank == 0:\n",
" data = [(i+1)**2 for i in range(size)]\n",
" data = [(i + 1) ** 2 for i in range(size)]\n",
"else:\n",
" data = None\n",
" data = None\n",
"data = comm.scatter(data, root=0)\n",
"\n",
"assert data == (rank+1)**2, 'data=%s, rank=%s' % (data, rank)\n",
"assert data == (rank + 1) ** 2, f'data={data}, rank={rank}'\n",
"{\n",
" 'data': data,\n",
" 'rank': rank,\n",
Expand Down
7 changes: 4 additions & 3 deletions docs/source/examples/broadcast/MPI Broadcast.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
}
],
"source": [
"%%time \n",
"%%time\n",
"ar = eall.push({'data': data}, block=False)\n",
"ar.wait_interactive()"
]
Expand Down Expand Up @@ -2568,7 +2568,8 @@
"source": [
"%%px\n",
"import numpy as np\n",
"np.linalg.norm(data, 2)\n"
"\n",
"np.linalg.norm(data, 2)"
]
}
],
Expand All @@ -2588,7 +2589,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.9"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
11 changes: 8 additions & 3 deletions docs/source/examples/progress.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -353,19 +353,22 @@
"n_steps = 10\n",
"\n",
"progress = W.IntProgress(max=n_steps, description=f\"rank {rank}: 0\")\n",
"\n",
"\n",
"def _set_color(change):\n",
" if change.new == progress.max:\n",
" progress.bar_style = \"success\"\n",
" else:\n",
" progress.bar_style = \"\"\n",
"\n",
"\n",
"progress.observe(_set_color, \"value\")\n",
"display(progress)\n",
"\n",
"for i in range(n_steps):\n",
" time.sleep(random.random())\n",
" progress.value += 1\n",
" progress.description = f\"rank {rank}: {progress.value}/{progress.max}\"\n",
"\n"
" progress.description = f\"rank {rank}: {progress.value}/{progress.max}\""
]
},
{
Expand Down Expand Up @@ -448,11 +451,13 @@
"\n",
"if rank == 0:\n",
" progress = W.IntProgress(max=n_steps, description=\"Step 0\")\n",
"\n",
" def _set_color(change):\n",
" if change.new == progress.max:\n",
" progress.bar_style = \"success\"\n",
" else:\n",
" progress.bar_style = \"\"\n",
"\n",
" progress.observe(_set_color, \"value\")\n",
" display(progress)\n",
"\n",
Expand Down Expand Up @@ -585,7 +590,7 @@
],
"source": [
"%%px\n",
"button = W.Button(description=f\"step\")\n",
"button = W.Button(description=\"step\")\n",
"progress = W.IntProgress(max=5, description=\"0 / 5\")\n",
"count = 0\n",
"\n",
Expand Down
Loading

0 comments on commit d7713f6

Please sign in to comment.