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

Open
opened 2026-11-26 21:19:14 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1861
Author: @kirigayakazima
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/bc-feature-safety


📝 Commits (1)

  • 4e6a9c4 Harden bc_feature dtype checks and smoothness mip-tail handling

📊 Changes

2 files changed (+61 additions, -2 deletions)

View changed files

📝 tests/test_bc_feature_validation.py (+48 -0)
📝 tools/bc_feature.py (+13 -2)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1861 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/bc-feature-safety` --- ### 📝 Commits (1) - [`4e6a9c4`](https://github.com/ubiquitarianism/leonida/commit/4e6a9c419e4f8be83bab95bab654a858b853c609) Harden bc_feature dtype checks and smoothness mip-tail handling ### 📊 Changes **2 files changed** (+61 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_bc_feature_validation.py` (+48 -0) 📝 `tools/bc_feature.py` (+13 -2) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1871
No description provided.