Skip to content

Commit e4f0a97

Browse files
authored
Ditch nbqa and use ruff only to lint and format notebooks (#51)
Since `ruff` can run in either scripts and notebooks, we don't need to rely on `nbqa` to run style checks and autoformats to notebooks. Ditch `nbqa` from the `environment.yml` and update the Makefile and the Action.
1 parent e7bcb70 commit e4f0a97

File tree

8 files changed

+43
-30
lines changed

8 files changed

+43
-30
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
python-version: ${{ env.PYTHON }}
2626

2727
- name: Install required packages
28-
run: pip install ruff nbqa
28+
run: pip install ruff
2929

3030
- name: Check style of notebooks
3131
run: make check

Makefile

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
NOTEBOOKS_DIR=notebooks
22

3-
.PHONY: build run clean check format
3+
.PHONY: build run clean check check-style check-format format
44

55
help:
66
@echo "Commands:"
77
@echo ""
8-
@echo " build build Myst website (without running notebooks)"
9-
@echo " clean clean output of Myst website"
10-
@echo " run run all notebooks"
11-
@echo " check lint notebooks with nbqa and ruff"
12-
@echo " format autoformat notebooks with nbqa and ruff"
8+
@echo " build build Myst website (without running notebooks)"
9+
@echo " clean clean output of Myst website"
10+
@echo " run run all notebooks"
11+
@echo " check check notebooks style and format with ruff"
12+
@echo " check-style check notebooks style with ruff"
13+
@echo " check-format check notebooks format with ruff"
14+
@echo " format autoformat notebooks with ruff"
1315
@echo ""
1416

1517

@@ -22,8 +24,13 @@ clean:
2224
run:
2325
jupyter nbconvert --to notebook --execute --inplace "${NOTEBOOKS_DIR}/**/*.ipynb"
2426

25-
check:
26-
nbqa ruff "${NOTEBOOKS_DIR}"
27+
check: check-format check-style
28+
29+
check-style:
30+
ruff check "${NOTEBOOKS_DIR}"
31+
32+
check-format:
33+
ruff format --check "${NOTEBOOKS_DIR}"
2734

2835
format:
29-
nbqa ruff --fix "${NOTEBOOKS_DIR}"
36+
ruff format "${NOTEBOOKS_DIR}"

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,31 @@ jupyter nbconvert --to notebook --execute --inplace notebooks/**/*.ipynb
115115

116116
## Check style of notebooks
117117

118-
We can check the code style of our notebooks using [`ruff`][ruff] and
119-
[`nbqa`][nbqa]. Simply run the following command to check the style of the
120-
notebooks:
118+
We can check the code style of our notebooks using [`ruff`][ruff].
119+
Simply run the following command to check the style of the notebooks:
121120

122121
```bash
123-
nbqa ruff notebooks
122+
ruff check notebooks
123+
```
124+
125+
You can run the following that the notebooks are correctly formatted:
126+
127+
```bash
128+
ruff format --check notebooks
124129
```
125130

126131
And run this to autoformat them:
127132

128133
```bash
129-
nbqa ruff --fix notebooks
134+
ruff format --fix notebooks
130135
```
131136

132137
Alternatively, you can use the targets we have in the `Makefile`, like `make
133138
check` and `make format`. Read more information about the available targets
134139
by running `make help`.
135140

141+
[ruff]: https://astral.sh/ruff
142+
136143
## License
137144

138145
All text and figures are licensed under a

environment.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ dependencies:
1515
- choclo==0.3.*
1616
- scikit-learn
1717
- pymatsolver
18-
# Code quality
19-
- nbqa
18+
# Code style
2019
- ruff

notebooks/03-gravity/weighting_strategies.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
},
355355
"outputs": [],
356356
"source": [
357-
"data_object = data.Data(survey, dobs=dobs, standard_deviation=uncertainties)\n"
357+
"data_object = data.Data(survey, dobs=dobs, standard_deviation=uncertainties)"
358358
]
359359
},
360360
{
@@ -430,7 +430,7 @@
430430
"model_map = maps.IdentityMap(nP=nC) # model consists of a value for each active cell\n",
431431
"\n",
432432
"# Define and plot starting model\n",
433-
"starting_model = np.zeros(nC)\n"
433+
"starting_model = np.zeros(nC)"
434434
]
435435
},
436436
{
@@ -469,7 +469,7 @@
469469
"# residual between the observed data and the data predicted for a given model.\n",
470470
"# Within the data misfit, the residual between predicted and observed data are\n",
471471
"# normalized by the data's standard deviation.\n",
472-
"dmis = data_misfit.L2DataMisfit(data=data_object, simulation=simulation)\n"
472+
"dmis = data_misfit.L2DataMisfit(data=data_object, simulation=simulation)"
473473
]
474474
},
475475
{
@@ -1636,7 +1636,7 @@
16361636
" np.r_[35.0, 0.0, -40.0], 15.0, mesh.gridCC\n",
16371637
")\n",
16381638
"ind_sphere = ind_sphere[ind_active]\n",
1639-
"true_model[ind_sphere] = sphere_density\n"
1639+
"true_model[ind_sphere] = sphere_density"
16401640
]
16411641
},
16421642
{

notebooks/04-magnetics/fwd_magnetics_induced_3d.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@
245245
"receiver_list = [receiver_list]\n",
246246
"\n",
247247
"# Define the inducing field\n",
248-
"inclination = 90 # inclination [deg]\n",
249-
"declination = 0 # declination [deg]\n",
248+
"inclination = 90 # inclination [deg]\n",
249+
"declination = 0 # declination [deg]\n",
250250
"amplitude = 50000 # amplitude [nT]\n",
251251
"\n",
252252
"source_field = magnetics.sources.UniformBackgroundField(\n",
253253
" receiver_list=receiver_list,\n",
254254
" amplitude=amplitude,\n",
255255
" inclination=inclination,\n",
256-
" declination=declination\n",
256+
" declination=declination,\n",
257257
")\n",
258258
"\n",
259259
"# Define the survey\n",

notebooks/04-magnetics/fwd_magnetics_mvi_3d.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@
247247
"receiver_list = [receiver_list]\n",
248248
"\n",
249249
"# Define the inducing field\n",
250-
"field_inclination = 90 # inclination [deg]\n",
251-
"field_declination = 0 # declination [deg]\n",
250+
"field_inclination = 90 # inclination [deg]\n",
251+
"field_declination = 0 # declination [deg]\n",
252252
"field_amplitude = 50000 # amplitude [nT]\n",
253253
"\n",
254254
"source_field = magnetics.sources.UniformBackgroundField(\n",
255255
" receiver_list=receiver_list,\n",
256256
" amplitude=field_amplitude,\n",
257257
" inclination=field_inclination,\n",
258-
" declination=field_declination\n",
258+
" declination=field_declination,\n",
259259
")\n",
260260
"\n",
261261
"# Define the survey\n",

notebooks/04-magnetics/inv_magnetics_induced_3d.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,15 @@
367367
"receiver_list = [receiver_list]\n",
368368
"\n",
369369
"# Define the inducing field\n",
370-
"inclination = 90 # inclination [deg]\n",
371-
"declination = 0 # declination [deg]\n",
370+
"inclination = 90 # inclination [deg]\n",
371+
"declination = 0 # declination [deg]\n",
372372
"amplitude = 50000 # amplitude [nT]\n",
373373
"\n",
374374
"source_field = magnetics.sources.UniformBackgroundField(\n",
375375
" receiver_list=receiver_list,\n",
376376
" amplitude=amplitude,\n",
377377
" inclination=inclination,\n",
378-
" declination=declination\n",
378+
" declination=declination,\n",
379379
")\n",
380380
"\n",
381381
"# Define the survey\n",

0 commit comments

Comments
 (0)