Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Add Flake8 linting to notebooks #2484

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ concurrency:
cancel-in-progress: true

on:
# Trigger the workflow on push or pull request,
# but only for the master branch
push:
branches:
- master
pull_request:
branches:
- master
# Trigger the workflow on push or pull request,
# but only for the master branch
push:
branches:
- master
pull_request:
branches:
- master

jobs:
flake8:
Expand All @@ -21,14 +21,18 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.12
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
pip install nbqa
- name: Lint with flake8
run: |
flake8 --builtins=ArgumentError .
- name: Lint notebooks with flake8
run: |
nbqa flake8 examples/
38 changes: 20 additions & 18 deletions examples/cfd/01_convection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Create field and assign initial conditions\n",
"u = np.empty((nx, ny))\n",
Expand Down Expand Up @@ -116,18 +116,19 @@
"for n in range(nt + 1):\n",
" # Copy previous result into a new buffer\n",
" un = u.copy()\n",
" \n",
"\n",
" # Update the new result with a 3-point stencil\n",
" u[1:, 1:] = (un[1:, 1:] - (c * dt / dy * (un[1:, 1:] - un[1:, :-1])) -\n",
" (c * dt / dx * (un[1:, 1:] - un[:-1, 1:])))\n",
"\n",
" # Apply boundary conditions. \n",
" # Apply boundary conditions.\n",
" u[0, :] = 1. # left\n",
" u[-1, :] = 1. # right\n",
" u[-1, :] = 1. # right\n",
" u[:, 0] = 1. # bottom\n",
" u[:, -1] = 1. # top\n",
" # Note that in the above expressions the NumPy index -1 corresponds to the final point of the array along the indexed dimension, \n",
" # i.e. here u[-1, :] is equivalent to u[80, :].\n"
" u[:, -1] = 1. # top\n",
" # Note that in the above expressions the NumPy index -1 corresponds to the\n",
" # final point of the array along the indexed dimension, i.e. here u[-1, :]\n",
" # is equivalent to u[80, :].\n"
]
},
{
Expand All @@ -147,7 +148,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# A small sanity check for auto-testing\n",
"assert (u[45:55, 45:55] > 1.8).all()\n",
Expand Down Expand Up @@ -197,8 +198,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction # noqa\n",
"\n",
"grid = Grid(shape=(nx, ny), extent=(2., 2.))\n",
"u = TimeFunction(name='u', grid=grid)\n",
Expand Down Expand Up @@ -230,7 +231,7 @@
}
],
"source": [
"from devito import Eq\n",
"from devito import Eq # noqa\n",
"\n",
"# Specify the `interior` flag so that the stencil is only\n",
"# applied to the interior of the domain.\n",
Expand Down Expand Up @@ -264,8 +265,8 @@
}
],
"source": [
"from devito import solve\n",
"from sympy import nsimplify, pprint\n",
"from devito import solve # noqa\n",
"from sympy import nsimplify, pprint # noqa\n",
"\n",
"stencil = solve(eq, u.forward)\n",
"\n",
Expand Down Expand Up @@ -306,8 +307,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator # noqa\n",
"\n",
"# Reset our initial condition in both buffers.\n",
"# This is required to avoid 0s propagating into\n",
Expand Down Expand Up @@ -368,7 +369,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Reset our data field and ICs in both buffers\n",
"init_hat(field=u.data[0], dx=dx, dy=dy, value=2.)\n",
Expand All @@ -386,7 +387,8 @@
"# Now combine the BC expressions with the stencil to form operator\n",
"expressions = [Eq(u.forward, stencil)]\n",
"expressions += [bc_left, bc_right, bc_top, bc_bottom]\n",
"op = Operator(expressions=expressions, opt=None, openmp=False) # <-- Turn off performance optimisations\n",
"# `opt=None, openmp=False` turns off performance optimisations\n",
"op = Operator(expressions=expressions, opt=None, openmp=False)\n",
"op(time=nt, dt=dt)\n",
"\n",
"plot_field(u.data[0])\n",
Expand Down Expand Up @@ -507,7 +509,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
"version": "3.11.5"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
Expand Down
45 changes: 23 additions & 22 deletions examples/cfd/01_convection_revisited.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"outputs": [],
"source": [
"from examples.cfd import plot_field, init_hat, init_smooth\n",
"from examples.cfd import plot_field, init_smooth\n",
"import numpy as np\n",
"%matplotlib inline\n",
"\n",
Expand Down Expand Up @@ -79,7 +79,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Create field and assign initial conditions\n",
"u = np.empty((nx, ny))\n",
Expand Down Expand Up @@ -110,17 +110,17 @@
"for n in range(nt + 1):\n",
" # Copy previous result into a new buffer\n",
" un = u.copy()\n",
" \n",
"\n",
" # Update the new result with a 3-point stencil\n",
" u[1:, 1:] = (un[1:, 1:] - (c * dt / dy * (un[1:, 1:] - un[1:, :-1])) -\n",
" (c * dt / dx * (un[1:, 1:] - un[:-1, 1:])))\n",
" \n",
" # Apply boundary conditions. \n",
"\n",
" # Apply boundary conditions.\n",
" # Note: -1 here is the last index in the array, not the one at x=-1 or y=-1.\n",
" u[0, :] = 1. # left\n",
" u[-1, :] = 1. # right\n",
" u[-1, :] = 1. # right\n",
" u[:, 0] = 1. # bottom\n",
" u[:, -1] = 1. # top"
" u[:, -1] = 1. # top"
]
},
{
Expand All @@ -140,7 +140,7 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# A small sanity check for auto-testing\n",
"assert (u[45:55, 45:55] > 1.8).all()\n",
Expand Down Expand Up @@ -181,8 +181,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Grid, TimeFunction # noqa\n",
"\n",
"grid = Grid(shape=(nx, ny), extent=(2., 2.))\n",
"u = TimeFunction(name='u', grid=grid)\n",
Expand Down Expand Up @@ -212,7 +212,7 @@
}
],
"source": [
"from devito import Eq\n",
"from devito import Eq # noqa\n",
"\n",
"eq = Eq(u.dt + c*u.dxl + c*u.dyl)\n",
"\n",
Expand Down Expand Up @@ -242,8 +242,8 @@
}
],
"source": [
"from devito import solve \n",
"from sympy import nsimplify, pprint\n",
"from devito import solve # noqa\n",
"from sympy import nsimplify, pprint # noqa\n",
"\n",
"stencil = solve(eq, u.forward)\n",
"\n",
Expand Down Expand Up @@ -281,8 +281,8 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator\n",
"# NBVAL_IGNORE_OUTPUT\n",
"from devito import Operator # noqa\n",
"\n",
"# Reset our initial condition in both buffers.\n",
"# This is required to avoid 0s propagating into\n",
Expand All @@ -291,13 +291,13 @@
"init_smooth(field=u.data[1], dx=dx, dy=dy)\n",
"\n",
"# Apply boundary conditions.\n",
"# Note that as the u.data method is from numpy, we can use the \n",
"# -1 syntax to represent the last item in the array.\n",
"# Note that as the u.data method is from numpy, we can use the\n",
"# -1 syntax to represent the last item in the array.\n",
"u.data[:, 0, :] = 1.\n",
"u.data[:, -1, :] = 1.\n",
"u.data[:, :, 0] = 1.\n",
"u.data[:, :, -1] = 1.\n",
" \n",
"\n",
"# Create an Operator that updates the forward stencil\n",
"# point in the interior subdomain only.\n",
"op = Operator(Eq(u.forward, stencil, subdomain=grid.interior))\n",
Expand Down Expand Up @@ -343,15 +343,15 @@
}
],
"source": [
"#NBVAL_IGNORE_OUTPUT\n",
"# NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Reset our data field and ICs in both buffers\n",
"init_smooth(field=u.data[0], dx=dx, dy=dy)\n",
"init_smooth(field=u.data[1], dx=dx, dy=dy)\n",
"\n",
"# For defining BCs, we generally to explicitly set rows/columns\n",
"# in our field using an expression. We can use Devito's \"indexed\" \n",
"# notation to do this. A u in this instance is a sympy function \n",
"# in our field using an expression. We can use Devito's \"indexed\"\n",
"# notation to do this. A u in this instance is a sympy function\n",
"# we cannot use the numpy shortcut u[-1] to refer to the last location:\n",
"x, y = grid.dimensions\n",
"t = grid.stepping_dim\n",
Expand All @@ -363,7 +363,8 @@
"# Now combine the BC expressions with the stencil to form operator.\n",
"expressions = [Eq(u.forward, stencil, subdomain=grid.interior)]\n",
"expressions += [bc_left, bc_right, bc_top, bc_bottom]\n",
"op = Operator(expressions=expressions, opt=None, openmp=False) # <-- Turn off performance optimisations\n",
"# `opt=None, openmp=False` turns off performance optimisations\n",
"op = Operator(expressions=expressions, opt=None, openmp=False)\n",
"op(time=nt, dt=dt)\n",
"\n",
"plot_field(u.data[0])\n",
Expand Down
Loading
Loading