Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"""
Dilation, a morphological operation on binary images.

Dilation grows the white (True) regions of a binary image: a pixel becomes True if
the structuring element, placed over it, overlaps at least one True pixel. The
functions here convert an RGB image to gray, threshold it to binary, and dilate it.

Reference: https://en.wikipedia.org/wiki/Dilation_(morphology)
"""

from pathlib import Path

import numpy as np
Expand All @@ -6,7 +16,8 @@

def rgb_to_gray(rgb: np.ndarray) -> np.ndarray:
"""
Return gray image from rgb image
Return gray image from rgb image.

>>> rgb_to_gray(np.array([[[127, 255, 0]]]))
array([[187.6453]])
>>> rgb_to_gray(np.array([[[0, 0, 0]]]))
Expand All @@ -22,7 +33,8 @@ def rgb_to_gray(rgb: np.ndarray) -> np.ndarray:

def gray_to_binary(gray: np.ndarray) -> np.ndarray:
"""
Return binary image from gray image
Return binary image from gray image.

>>> gray_to_binary(np.array([[127, 255, 0]]))
array([[False, True, False]])
>>> gray_to_binary(np.array([[0]]))
Expand All @@ -39,7 +51,8 @@ def gray_to_binary(gray: np.ndarray) -> np.ndarray:

def dilation(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
"""
Return dilated image
Return dilated image.

>>> dilation(np.array([[True, False, True]]), np.array([[0, 1, 0]]))
array([[False, False, False]])
>>> dilation(np.array([[False, False, True]]), np.array([[1, 0, 1]]))
Expand Down
47 changes: 47 additions & 0 deletions work/R23-T057/REVIEW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# R23-T057: strict Ruff review of `digital_image_processing/morphological_operations/dilation_operation.py`

Tool: `ruff 0.16.9`. Commands (from the repo root):

ruff check digital_image_processing/morphological_operations/dilation_operation.py # repo config
ruff check --select ALL --output-format concise digital_image_processing/morphological_operations/dilation_operation.py

Raw outputs are saved next to this file (`ruff_*_before.txt`, `ruff_*_after.txt`).

## Result
| | repo config | `--select ALL` |
|---|---|---|
| before | All checks passed | **15 findings**, 7 codes |
| after | All checks passed | **6 findings**, 3 codes |

## `--select ALL` findings and decisions
| Code | Name | Count | Decision | Reason |
|---|---|---|---|---|
| D100 | undocumented-public-module | 1 | **Fixed** | CONTRIBUTING.md asks for docstrings with a source URL. Added a module docstring with a summary and the Wikipedia reference for dilation. |
| D205 | missing-blank-line-after-summary | 3 | **Fixed** | A blank line between the summary and the `>>>` examples makes the docstring easier to read; no behaviour change. |
| D400 | missing-trailing-period | 3 | **Fixed** | Summary lines now end with a period. |
| D415 | missing-terminal-punctuation | 3 | **Fixed** | Same lines as D400 (this rule also accepts `?`/`!`); fixed by the same edit. |
| D212 | multi-line-summary-first-line | 4 | **Not fixed** | Wants the summary on the first line (`"""Return ...`). CONTRIBUTING.md's own docstring example starts the text on the second line, which is the opposite rule (D213). Following D212 would contradict the contributing guide and the style used across the repo. (The count went from 3 to 4 because the new module docstring follows the same second-line style.) |
| COM812 | missing-trailing-comma | 1 | **Not fixed** | `pyproject.toml` lists `"COM"` as `-- DO NOT FIX`, and Ruff documents that COM812 can conflict with the formatter. The repo formatter already accepts the current code. |
| CPY001 | missing-copyright-notice | 1 | **Not fixed** | The repo uses no per-file copyright headers (the licence lives at the repository root), so adding one here would be inconsistent. |

4 codes fixed (D100, D205, D400, D415; 10 findings). No logic was changed, only documentation.

## Checks after the changes
- `ruff check` (repo config): All checks passed
- `ruff format --check`: already formatted
- `python -m pytest --doctest-modules digital_image_processing/morphological_operations/dilation_operation.py`: passed (10 doctests)
- The file uses CRLF line endings in the repo; they were kept so the diff only shows the real changes.

## Observation (not changed; outside the Ruff task)
`dilation()` only works for 3x3 kernels. It copies the image into the padded array with
`image_padded[kernel.shape[0] - 2 : -1, kernel.shape[1] - 2 : -1] = image`, which assumes a kernel
size of 3. For other sizes the slice can be empty and the copy silently does nothing.
Two existing doctests record the resulting all-False output:

dilation(np.array([[True, False, True]]), np.array([[0, 1, 0]])) # shown as [[False, False, False]]
dilation(np.array([[False, False, True]]), np.array([[1, 0, 1]])) # shown as [[False, False, False]]

A 1x3 kernel `[0, 1, 0]` is the identity for dilation, so the first result should be
`[[True, False, True]]`. With the normal 3x3 plus-shaped kernel the function is correct
(one True pixel grows into a plus shape). Left as is so the 10 existing doctests stay
unchanged; worth a separate fix.
10 changes: 10 additions & 0 deletions work/R23-T057/ruff_all_after.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: `incorrect-blank-line-before-class` (D203) and `blank-line-before-class` (D211) are incompatible. Ignoring `incorrect-blank-line-before-class`.
warning: `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible. Ignoring `multi-line-summary-second-line`.
digital_image_processing/morphological_operations/dilation_operation.py:1:1: D212 [*] Multi-line docstring summary should start at the first line
digital_image_processing/morphological_operations/dilation_operation.py:1:1: CPY001 Missing copyright notice at top of file
digital_image_processing/morphological_operations/dilation_operation.py:18:5: D212 [*] Multi-line docstring summary should start at the first line
digital_image_processing/morphological_operations/dilation_operation.py:35:5: D212 [*] Multi-line docstring summary should start at the first line
digital_image_processing/morphological_operations/dilation_operation.py:53:5: D212 [*] Multi-line docstring summary should start at the first line
digital_image_processing/morphological_operations/dilation_operation.py:63:85: COM812 [*] Trailing comma missing
Found 6 errors.
[*] 5 fixable with `--fix`.
19 changes: 19 additions & 0 deletions work/R23-T057/ruff_all_before.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
warning: `incorrect-blank-line-before-class` (D203) and `blank-line-before-class` (D211) are incompatible. Ignoring `incorrect-blank-line-before-class`.
warning: `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible. Ignoring `multi-line-summary-second-line`.
digital_image_processing/morphological_operations/dilation_operation.py:1:1: D100 Missing docstring in public module
digital_image_processing/morphological_operations/dilation_operation.py:1:1: CPY001 Missing copyright notice at top of file
digital_image_processing/morphological_operations/dilation_operation.py:8:5: D205 1 blank line required between summary line and description
digital_image_processing/morphological_operations/dilation_operation.py:8:5: D212 [*] Multi-line docstring summary should start at the first line
digital_image_processing/morphological_operations/dilation_operation.py:8:5: D400 First line should end with a period
digital_image_processing/morphological_operations/dilation_operation.py:8:5: D415 First line should end with a period, question mark, or exclamation point
digital_image_processing/morphological_operations/dilation_operation.py:24:5: D205 1 blank line required between summary line and description
digital_image_processing/morphological_operations/dilation_operation.py:24:5: D212 [*] Multi-line docstring summary should start at the first line
digital_image_processing/morphological_operations/dilation_operation.py:24:5: D400 First line should end with a period
digital_image_processing/morphological_operations/dilation_operation.py:24:5: D415 First line should end with a period, question mark, or exclamation point
digital_image_processing/morphological_operations/dilation_operation.py:41:5: D205 1 blank line required between summary line and description
digital_image_processing/morphological_operations/dilation_operation.py:41:5: D212 [*] Multi-line docstring summary should start at the first line
digital_image_processing/morphological_operations/dilation_operation.py:41:5: D400 First line should end with a period
digital_image_processing/morphological_operations/dilation_operation.py:41:5: D415 First line should end with a period, question mark, or exclamation point
digital_image_processing/morphological_operations/dilation_operation.py:50:85: COM812 [*] Trailing comma missing
Found 15 errors.
[*] 4 fixable with `--fix` (6 unsafe fixes available with `--fix --unsafe-fixes`).
1 change: 1 addition & 0 deletions work/R23-T057/ruff_repo_config_after.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All checks passed!
1 change: 1 addition & 0 deletions work/R23-T057/ruff_repo_config_before.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All checks passed!