Guide lattice search with spacing spectrum

This commit is contained in:
Meta Vens 2026-04-25 02:02:59 +01:00
commit 918e57ba87
3 changed files with 260 additions and 11 deletions

View file

@ -98,7 +98,11 @@ def test_infer_lattice_emits_search_progress_events(monkeypatch) -> None:
cuda = _FakeCuda()
monkeypatch.setattr(inference_module, "_require_torch", lambda: (_FakeTorch(), object()))
monkeypatch.setattr(inference_module, "_estimate_lattice_spacing", lambda rgba: ((None, 0.0), (None, 0.0)))
monkeypatch.setattr(
inference_module,
"_estimate_lattice_spacing_details",
lambda rgba: (inference_module.SpacingEstimate(None, 0.0, None, 0.0, (), ()),) * 2,
)
monkeypatch.setattr(inference_module, "_hint_target_sizes_from_spacing", lambda width, height, spacing_x, spacing_y: [])
monkeypatch.setattr(inference_module, "_estimate_lattice_prior_details", lambda rgba, **kwargs: (4.0, 4.0, 0.5))
monkeypatch.setattr(

View file

@ -1,6 +1,7 @@
from __future__ import annotations
from repixelizer.inference import (
SpacingEstimate,
_axis_prior_from_estimates,
_candidate_dims,
_combine_axis_priors,
@ -14,6 +15,35 @@ from repixelizer.types import InferenceCandidate
from repixelizer.synthetic import fake_pixelize, make_emblem
def _make_spacing_estimate(
*,
best_cell: int,
confidence: float,
peaks: dict[int, float],
start: int = 2,
stop: int = 24,
) -> SpacingEstimate:
candidate_cells = tuple(float(cell) for cell in range(start, stop + 1))
candidate_scores: list[float] = []
for cell in range(start, stop + 1):
score = 0.16
for peak_cell, peak_score in peaks.items():
distance = abs(cell - peak_cell)
if distance == 0:
score = max(score, peak_score)
elif distance == 1:
score = max(score, peak_score - 0.14)
candidate_scores.append(float(score))
return SpacingEstimate(
spacing=float(best_cell),
confidence=confidence,
best_cell=float(best_cell),
best_score=float(peaks[best_cell]),
candidate_cells=candidate_cells,
candidate_scores=tuple(candidate_scores),
)
def test_axis_prior_stays_close_to_spacing_when_autocorr_hits_large_multiple() -> None:
prior, reliability = _axis_prior_from_estimates(8.0, 0.75, 32.0)
assert prior < 10.0
@ -52,7 +82,7 @@ def test_strong_spacing_signal_can_collapse_candidate_search_to_single_size() ->
assert sizes == {128}
def test_weak_spacing_signal_keeps_broad_candidate_search() -> None:
def test_weak_spacing_signal_without_spectrum_keeps_broad_candidate_search() -> None:
dims = _resolve_candidate_dims_from_spacing(
1024,
1024,
@ -67,6 +97,30 @@ def test_weak_spacing_signal_keeps_broad_candidate_search() -> None:
assert len(sizes) > 10
def test_weak_spacing_signal_uses_spectrum_modes_to_keep_candidate_search_compact() -> None:
spectrum = _make_spacing_estimate(
best_cell=8,
confidence=0.18,
peaks={4: 0.78, 8: 0.74, 16: 0.71},
)
dims = _resolve_candidate_dims_from_spacing(
1024,
1024,
None,
hinted_sizes=[128],
spacing_x=(8.0, 0.18),
spacing_y=(8.1, 0.12),
prior_reliability=0.24,
spacing_x_estimate=spectrum,
spacing_y_estimate=spectrum,
)
sizes = {width for width, _ in dims}
assert sizes
assert len(sizes) <= 12
assert any(abs(size - 128) <= 1 for size in sizes)
assert all(any(abs(size - target) <= 2 for target in (64, 128, 256)) for size in sizes)
def test_top_candidates_are_diversified_by_size() -> None:
candidates = [
InferenceCandidate(target_width=113, target_height=113, phase_x=0.0, phase_y=0.0, score=0.90, breakdown={}),