Skip to content

Commit 87ddad1

Browse files
authored
Switch to Ruff for formatting and linting (#544)
* configure ruff and settings * update pre-commit to use ruff * Refactor MDIO accessor for type hints, style, and clarity * Remove unnecessary blank lines in __init__.py files * Set line-length to 100 in Ruff configuration * Refactor CLI code for improved type hinting and formatting * Remove unnecessary blank line in constants.py * Remove redundant `pass` statements from exception classes * Refactor tuple comprehensions and exception raising * Refactor SEG-Y utility functions * Refactor SEG-Y workers for readability * Refactor SEGY parser for clarity and type consistency * Refactor SEG-Y geometry logic and improve logging formatting * Add type annotations to exception class constructors * Refactor file handling to use `Path` objects consistently * Refactor and reformat compat.py * Refactor SEG-Y blocked I/O * Refactor type annotations in utils_write.py * Refactor error handling and simplify conditional branches * Refactor ChunkIterator and added examples. * Refactor Grid class and enhanced type annotations * Refactor `Dimension` class and typing improvements * Refactor SEG-Y to MDIO converter for readability * Refactor `numpy_to_mdio` * Refactor MDIO to SEG-Y conversion with minor improvements * Refactor exception constructors with type annotations. * Refactor SEG-Y command for formatting * Simplify and clean up `info.py` code formatting. * Refactor MDIO copy function docstring * Refactor `copy` function docstring and suppress linter warning * Refactor type hints and optimize imports in io_utils.py * Improve error messages for unsupported serialization formats * Refactor serialization tests with type hints and stricter checks * Add return type annotation to test_create_empty_like * Refactor test functions to add type annotations. * Add explicit return type annotations to test functions * Refactor code and type annotations in tests * Refactor test_accessor.py to improve type annotations * Refactor unit test fixtures for clarity and typing support * Enable E501 ignore for specific integration test file * Refactor imports and type hints in SEG-Y test module * Fix function call in unsupported format serializer test * Update typing annotations and cleanup formatting in tests * Update segy_to_mdio to support Path objects * Enable per-file linting ignore for notebook tutorials * Update linting tools and clarify CVE comment in noxfile * Remove unnecessary blank line in helpers_segy.py * Use `datetime.UTC` instead of `timezone.utc`. * Refactor tests to use `Path` over `os.path` for file handling * Fix comment for query size validation in accessor.py * Remove .flake8 configuration file * Add flake8-future-annotations * lint and format notebooks * Update docstyle dependencies and tooling configurations * lint docs and add more details * Update `setup-uv` action to v6 and adjust parameters * Update bump-my-version to 1.1.2 * Fix SEG-Y sparsity check bug I introduced. * Fix environment variable check for ignoring grid sparsity * Refactor grid sparsity checks and environment variable handling. * Update environment variable names for grid sparsity checks * fix sparsity exception bug * Update test CLI argument handling for consistent type usage * revert unintended python version matrix change
1 parent abcec1d commit 87ddad1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1334
-1734
lines changed

.flake8

Lines changed: 0 additions & 15 deletions
This file was deleted.

.github/workflows/constraints.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
bump-my-version==1.1.1
1+
bump-my-version==1.1.2
22
nox==2025.2.9

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
fetch-depth: 2
1818

1919
- name: Install the pinned version of uv
20-
uses: astral-sh/setup-uv@v5
20+
uses: astral-sh/setup-uv@v6
2121
with:
2222
python-version: 3.13
23-
pyproject-file: "${{ github.workspace }}/pyproject.toml"
23+
working-directory: ${{ github.workspace }}
2424

2525
- name: Install bumpversion
2626
run: |

.github/workflows/tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ jobs:
4343
python-version: ${{ matrix.python }}
4444

4545
- name: Install the pinned version of uv
46-
uses: astral-sh/setup-uv@v5
46+
uses: astral-sh/setup-uv@v6
4747
with:
4848
python-version: ${{ matrix.python }}
49-
pyproject-file: ${{ github.workspace }}/pyproject.toml
49+
working-directory: ${{ github.workspace }}
5050

5151
- name: Install Nox
5252
run: |
@@ -105,10 +105,10 @@ jobs:
105105
uses: actions/checkout@v4
106106

107107
- name: Install the pinned version of uv
108-
uses: astral-sh/setup-uv@v5
108+
uses: astral-sh/setup-uv@v6
109109
with:
110110
python-version: 3.13
111-
pyproject-file: "${{ github.workspace }}/pyproject.toml"
111+
working-directory: ${{ github.workspace }}
112112

113113
- name: Install Nox
114114
run: |

.pre-commit-config.yaml

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
repos:
2+
# should be replaced in the future ref https://github.com/astral-sh/ruff/issues/458
3+
- repo: https://github.com/jsh9/pydoclint
4+
rev: 0.6.6
5+
hooks:
6+
- id: pydoclint
27
- repo: local
38
hooks:
4-
- id: black
5-
name: black
6-
entry: black
9+
- id: ruff-format
10+
name: Format code with Ruff
11+
entry: ruff format
12+
language: system
13+
types_or: [python, pyi, jupyter]
14+
- id: ruff
15+
name: Lint code with Ruff
16+
entry: ruff check
717
language: system
8-
types: [python]
9-
require_serial: true
18+
types_or: [python, pyi, jupyter]
19+
args: [--fix]
1020
- id: check-added-large-files
1121
name: Check for added large files
1222
entry: check-added-large-files
@@ -22,39 +32,12 @@ repos:
2232
entry: check-yaml
2333
language: system
2434
types: [yaml]
25-
- id: darglint
26-
name: darglint
27-
entry: darglint
28-
language: system
29-
types: [python]
30-
stages: [manual]
3135
- id: end-of-file-fixer
3236
name: Fix End of Files
3337
entry: end-of-file-fixer
3438
language: system
3539
types: [text]
3640
stages: [pre-commit, pre-push, manual]
37-
- id: flake8
38-
name: flake8
39-
entry: flake8
40-
language: system
41-
types: [python]
42-
require_serial: true
43-
args: [--darglint-ignore-regex, .*]
44-
- id: isort
45-
name: isort
46-
entry: isort
47-
require_serial: true
48-
language: system
49-
types_or: [cython, pyi, python]
50-
args: ["--filter-files"]
51-
- id: pyupgrade
52-
name: pyupgrade
53-
description: Automatically upgrade syntax for newer versions.
54-
entry: pyupgrade
55-
language: system
56-
types: [python]
57-
args: [--py38-plus]
5841
- id: trailing-whitespace
5942
name: Trim Trailing Whitespace
6043
entry: trailing-whitespace-fixer
@@ -63,6 +46,6 @@ repos:
6346
stages: [pre-commit, pre-push, manual]
6447
args: [--markdown-linebreak-ext=md]
6548
- repo: https://github.com/pre-commit/mirrors-prettier
66-
rev: v2.6.0
49+
rev: v2.7.1
6750
hooks:
6851
- id: prettier

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
project = "MDIO"
66
author = "TGS"
7-
copyright = "2023, TGS"
7+
copyright = "2023, TGS" # noqa: A001
88

99
# -- General configuration ---------------------------------------------------
1010

docs/tutorials/compression.ipynb

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"metadata": {},
4545
"outputs": [],
4646
"source": [
47-
"from mdio import segy_to_mdio, MDIOReader"
47+
"from mdio import MDIOReader\n",
48+
"from mdio import segy_to_mdio"
4849
]
4950
},
5051
{
@@ -263,7 +264,7 @@
263264
"ascii": false,
264265
"bar_format": null,
265266
"colour": null,
266-
"elapsed": 0.014499902725219727,
267+
"elapsed": 0.014499902725219728,
267268
"initial": 0,
268269
"n": 0,
269270
"ncols": null,
@@ -366,28 +367,27 @@
366367
}
367368
],
368369
"source": [
369-
"import os\n",
370+
"from pathlib import Path\n",
370371
"\n",
371372
"\n",
372-
"def get_dir_size(path: str) -> int:\n",
373+
"def get_dir_size(path: Path) -> int:\n",
373374
" \"\"\"Get size of a directory recursively.\"\"\"\n",
374375
" total = 0\n",
375-
" with os.scandir(path) as it:\n",
376-
" for entry in it:\n",
377-
" if entry.is_file():\n",
378-
" total += entry.stat().st_size\n",
379-
" elif entry.is_dir():\n",
380-
" total += get_dir_size(entry.path)\n",
376+
" for entry in path.iterdir():\n",
377+
" if entry.is_file():\n",
378+
" total += entry.stat().st_size\n",
379+
" elif entry.is_dir():\n",
380+
" total += get_dir_size(entry)\n",
381381
" return total\n",
382382
"\n",
383383
"\n",
384-
"def get_size(path: str) -> int:\n",
384+
"def get_size(path: Path) -> int:\n",
385385
" \"\"\"Get size of a folder or a file.\"\"\"\n",
386-
" if os.path.isfile(path):\n",
387-
" return os.path.getsize(path)\n",
388-
"\n",
389-
" elif os.path.isdir(path):\n",
386+
" if path.is_file():\n",
387+
" return path.stat().st_size\n",
388+
" if path.is_dir():\n",
390389
" return get_dir_size(path)\n",
390+
" return 0 # Handle case where path is neither file nor directory\n",
391391
"\n",
392392
"\n",
393393
"print(f\"SEG-Y:\\t{get_size('volve.segy') / 1024 / 1024:.2f} MB\")\n",
@@ -466,29 +466,35 @@
466466
],
467467
"source": [
468468
"import matplotlib.pyplot as plt\n",
469+
"from matplotlib.axes import Axes\n",
470+
"from matplotlib.image import AxesImage\n",
469471
"from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
472+
"from numpy.typing import NDArray\n",
470473
"\n",
471474
"vmin = -3 * std\n",
472475
"vmax = 3 * std\n",
473476
"\n",
474-
"imshow_kw = dict(vmin=vmin, vmax=vmax, cmap=\"gray_r\", interpolation=\"bilinear\")\n",
477+
"imshow_kw = {\"vmin\": -3 * std, \"vmax\": 3 * std, \"cmap\": \"gray_r\", \"interpolation\": \"bilinear\"}\n",
475478
"\n",
476479
"\n",
477-
"def attach_colorbar(image, axis):\n",
480+
"def attach_colorbar(image: AxesImage, axis: Axes) -> None:\n",
481+
" \"\"\"Attach a colorbar to an axis.\"\"\"\n",
478482
" divider = make_axes_locatable(axis)\n",
479483
" cax = divider.append_axes(\"top\", size=\"2%\", pad=0.05)\n",
480484
" plt.colorbar(image, cax=cax, orientation=\"horizontal\")\n",
481485
" cax.xaxis.set_ticks_position(\"top\")\n",
482486
" cax.tick_params(labelsize=8)\n",
483487
"\n",
484488
"\n",
485-
"def plot_image_and_cbar(data, axis, title):\n",
489+
"def plot_image_and_cbar(data: NDArray, axis: Axes, title: str) -> None:\n",
490+
" \"\"\"Plot an image with a colorbar.\"\"\"\n",
486491
" image = axis.imshow(data.T, **imshow_kw)\n",
487492
" attach_colorbar(image, axis)\n",
488493
" axis.set_title(title, y=-0.15)\n",
489494
"\n",
490495
"\n",
491-
"def plot_inlines_with_diff(orig, compressed, title):\n",
496+
"def plot_inlines_with_diff(orig: NDArray, compressed: NDArray, title: str) -> None:\n",
497+
" \"\"\"Plot lossless and lossy inline with their differences.\"\"\"\n",
492498
" fig, ax = plt.subplots(1, 4, sharey=\"all\", sharex=\"all\", figsize=(12, 5))\n",
493499
"\n",
494500
" diff = orig[200] - compressed[200]\n",
@@ -545,19 +551,19 @@
545551
}
546552
],
547553
"source": [
548-
"import skimage\n",
549-
"\n",
550-
"\n",
551-
"def get_metrics(image_true, image_test):\n",
552-
" \"\"\"Get four metrics\"\"\"\n",
553-
" psnr = skimage.metrics.peak_signal_noise_ratio(\n",
554-
" image_true[200], image_test[200], data_range=max_ - min_\n",
555-
" )\n",
556-
" ssim = skimage.metrics.structural_similarity(\n",
557-
" image_true[200], image_test[200], data_range=max_ - min_\n",
558-
" )\n",
559-
" mse = skimage.metrics.mean_squared_error(image_true[200], image_test[200])\n",
560-
" nrmse = skimage.metrics.normalized_root_mse(image_true[200], image_test[200])\n",
554+
"from numpy.typing import NDArray\n",
555+
"from skimage.metrics import mean_squared_error\n",
556+
"from skimage.metrics import normalized_root_mse\n",
557+
"from skimage.metrics import peak_signal_noise_ratio\n",
558+
"from skimage.metrics import structural_similarity\n",
559+
"\n",
560+
"\n",
561+
"def get_metrics(image_true: NDArray, image_test: NDArray) -> tuple[float, float, float, float]:\n",
562+
" \"\"\"Get four image quality metrics.\"\"\"\n",
563+
" psnr = peak_signal_noise_ratio(image_true[200], image_test[200], data_range=max_ - min_)\n",
564+
" ssim = structural_similarity(image_true[200], image_test[200], data_range=max_ - min_)\n",
565+
" mse = mean_squared_error(image_true[200], image_test[200])\n",
566+
" nrmse = normalized_root_mse(image_true[200], image_test[200])\n",
561567
"\n",
562568
" return psnr, ssim, mse, nrmse\n",
563569
"\n",

0 commit comments

Comments
 (0)