tools/bc_feature: validate block dtype and guard smoothness mip-tail edge shapes #1861

Merged
kirigayakazima merged 1 commit from upstream-pr/1861 into main 2026-11-28 17:57:29 +00:00
kirigayakazima commented 2026-11-25 13:22:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-25T13:22:58Z.


Claimed in the coordination issue as: BC-FEATURE-SMOOTHNESS-EDGE-AND-DTYPE-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163663715)

What and why

  • Enforce dtype == np.uint8 in bc1_average() and block_average() in tools/bc_feature.py so wider integer or floating-point arrays fail fast with ValueError instead of misinterpreting byte bit-patterns.
  • Validate img.ndim >= 2 and img.size > 0 in smoothness(), reject 1D and empty inputs with ValueError, guard axis differences when height or width is 1 (1-row, 1-col, and 1x1 mip tails) so no RuntimeWarning or NaN is produced, and cast non-floating arrays to np.float32 before np.diff to avoid unsigned integer underflow wraparound.
  • Expand tests/test_bc_feature_validation.py with regression tests covering non-uint8 block inputs, 1D/empty rejection, degenerate mip-tail warning-free finiteness, and uint8 step parity with float32.

Tested

  • Patched: python -I tests/test_bc_feature_validation.py -v -> Ran 8 tests in 0.002s (OK); python -I tests/test_bc_feature.py -v -> Ran 15 tests in 0.082s (OK).
  • Baseline check (upstream/main 98c2f777 with updated tests/test_bc_feature_validation.py copied into detached worktree): Ran 8 tests in 0.016s (FAILED (failures=11)).
  • Repo gates: python -I tests/test_ci_registration.py (Ran 16 tests in 4.628s, OK), python -I tests/test_text_write_line_endings.py (Ran 3 tests in 12.538s, OK), python -I scripts/check_no_game_data.py (exit 0), python -I -m compileall -q (exit 0), git diff --check (exit 0).

GPUs tested: n/a (numpy array validation and feature-metric unit tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5492168252 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1861). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-25T13:22:58Z. --- Claimed in the coordination issue as: BC-FEATURE-SMOOTHNESS-EDGE-AND-DTYPE-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163663715) ### What and why - Enforce `dtype == np.uint8` in `bc1_average()` and `block_average()` in `tools/bc_feature.py` so wider integer or floating-point arrays fail fast with `ValueError` instead of misinterpreting byte bit-patterns. - Validate `img.ndim >= 2` and `img.size > 0` in `smoothness()`, reject 1D and empty inputs with `ValueError`, guard axis differences when height or width is `1` (1-row, 1-col, and 1x1 mip tails) so no `RuntimeWarning` or `NaN` is produced, and cast non-floating arrays to `np.float32` before `np.diff` to avoid unsigned integer underflow wraparound. - Expand `tests/test_bc_feature_validation.py` with regression tests covering non-`uint8` block inputs, 1D/empty rejection, degenerate mip-tail warning-free finiteness, and `uint8` step parity with `float32`. ### Tested - Patched: `python -I tests/test_bc_feature_validation.py -v` -> `Ran 8 tests in 0.002s` (`OK`); `python -I tests/test_bc_feature.py -v` -> `Ran 15 tests in 0.082s` (`OK`). - Baseline check (`upstream/main` `98c2f777` with updated `tests/test_bc_feature_validation.py` copied into detached worktree): `Ran 8 tests in 0.016s` (`FAILED (failures=11)`). - Repo gates: `python -I tests/test_ci_registration.py` (`Ran 16 tests in 4.628s`, `OK`), `python -I tests/test_text_write_line_endings.py` (`Ran 3 tests in 12.538s`, `OK`), `python -I scripts/check_no_game_data.py` (exit 0), `python -I -m compileall -q` (exit 0), `git diff --check` (exit 0). GPUs tested: n/a (numpy array validation and feature-metric unit tests) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
GeorgiyDemo commented 2026-11-25 14:05:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-25T14:05:42Z.


Tested 4e6a9c419 on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 71a769d7d (Python texture tool; no GPU):

  • pytest 9.2.0 on tests/test_bc_feature_validation.py: 8 passed, 20 subtests passed at this head.
  • The same file on main: 11 failed, 8 passed (10 warnings), 9 subtests — the uint8 dtype enforcement, img.ndim validation and the smoothness mip-tail guard are new.
  • Not run: real BC-packed textures; the tests use synthetic arrays.

AI-assisted; synthetic fixtures only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164748183 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1861#issuecomment-7164748183). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-25T14:05:42Z. --- Tested `4e6a9c419` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `71a769d7d` (Python texture tool; no GPU): - pytest 9.2.0 on `tests/test_bc_feature_validation.py`: **8 passed, 20 subtests passed** at this head. - The same file on `main`: **11 failed**, 8 passed (10 warnings), 9 subtests — the `uint8` dtype enforcement, `img.ndim` validation and the smoothness mip-tail guard are new. - Not run: real BC-packed textures; the tests use synthetic arrays. AI-assisted; synthetic fixtures only, no game data.
DeadlyChirp commented 2026-11-25 15:32:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-25T15:32:14Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_bc_feature_validation.py:

  • PR: 8 passed, 20 subtests passed. main: 4 passed, 8 subtests passed. The new tests pass on Windows.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7166995660 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1861#issuecomment-7166995660). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-25T15:32:14Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_bc_feature_validation.py`: - PR: 8 passed, 20 subtests passed. main: 4 passed, 8 subtests passed. The new tests pass on Windows. (AI-assisted test run; output checked by hand.)
MSedgeMC commented 2026-11-25 17:22:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MSedgeMC on 2026-11-25T17:22:03Z.

Review: APPROVED


Verified on the PR branch: python3 -m unittest tests.test_bc_feature_validation -v -> 8 tests OK, including the new non-uint8 dtype rejection, 1d/empty smoothness rejection, and degenerate mip-shape cases without RuntimeWarnings. These guards prevent silent garbage from malformed inputs in a texture-feature tool. Python/numpy only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6061980183 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1861#pullrequestreview-6061980183). Originally posted by [MSedgeMC](https://github.com/MSedgeMC) on 2026-11-25T17:22:03Z. **Review:** APPROVED --- Verified on the PR branch: python3 -m unittest tests.test_bc_feature_validation -v -> 8 tests OK, including the new non-uint8 dtype rejection, 1d/empty smoothness rejection, and degenerate mip-shape cases without RuntimeWarnings. These guards prevent silent garbage from malformed inputs in a texture-feature tool. Python/numpy only, no game data.
MoonEyes commented 2026-11-28 17:57:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:57:27Z.


Red/green: tests/test_bc_feature_validation.py fails 11 assertions on main (non-uint8 dtype accepted, 1-D/empty smoothness input, degenerate mip-tail shapes) and passes with the PR.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243417677 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1861#issuecomment-7243417677). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:57:27Z. --- Red/green: tests/test_bc_feature_validation.py fails 11 assertions on main (non-uint8 dtype accepted, 1-D/empty smoothness input, degenerate mip-tail shapes) and passes with the PR. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1861
No description provided.