Files
rowing_stats/tests/test_tune.py
T
adamfandClaude Opus 5.5 7b19d8f3e9 Restructure scripts into the rowing_stats package
Move the five top-level scripts into src/rowing_stats, one module per
pipeline stage, behind a single 'rowing-stats' command with a subcommand
each (crop, classify, ocr, claude, tune). Add type hints throughout,
packaging metadata, pinned requirements files, and a test suite.

Failures are now typed exceptions under RowingStatsError instead of
prints, None returns and sys.exit: unreadable or missing images, photos
with no detectable screen, a missing tesseract binary, tesseract
failures, unusable Claude replies, and missing optional dependencies,
model weights or API credentials. Each command reports a bad file and
carries on, stops once on a setup problem, and returns a non-zero exit
code when something failed.

Crops, classifier labels and OCR votes are unchanged. Other changes:
- crop.find_screen takes a CropParams, so the tuner searches the real
  detection code instead of a copy of it.
- OCR validation scores only images that have a reference reading;
  images without one were counted as wrong.
- 'classify train --data-dir train/' works with the repository layout
  (it looked for train/train), and the CNN weights path defaults to the
  committed screen_classifier_model.pth instead of a missing model.pth.
- The CNN is loaded once per run rather than once per image.
- ocr and claude can write their readings with --output.
- crop no longer defaults to /mnt/user-data paths.
- Pure vote-counting moved to parsing.py; the unused 'expected' argument
  that let a caller steer the vote is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-09 12:08:01 +01:00

49 lines
2.0 KiB
Python

import pytest
from rowing_stats import tune
from rowing_stats.crop import CropParams
def test_load_images_skips_unreadable_files(photo_path, corrupt_path, capsys):
images = tune.load_images(photo_path.parent)
assert [path.name for path, _ in images] == ["IMG_0001.jpg"]
assert "corrupt.jpg" in capsys.readouterr().err
def test_count_rowing_crops(photo_path, photo, tmp_path, monkeypatch):
seen = []
def fake_predict(path, model_path):
seen.append(path)
assert path.is_file() # the crop exists while it is classified
return 1, 0.9
monkeypatch.setattr(tune, "cnn_predict", fake_predict)
images = [(photo_path, photo), (photo_path, photo)]
assert tune.count_rowing_crops(images, CropParams(), tmp_path, tmp_path / "m.pth") == 2
assert not any(path.exists() for path in seen) # and is removed afterwards
# Parameters that find no screen classify nothing
assert tune.count_rowing_crops(images, CropParams(area_ratio_min=0.1), tmp_path,
tmp_path / "m.pth") == 0
assert len(seen) == 2
def test_objective_rejects_inverted_ranges(photo_path, photo, tmp_path, monkeypatch):
optuna = pytest.importorskip("optuna")
monkeypatch.setattr(tune, "cnn_predict", lambda path, model_path: (1, 0.9))
objective = tune.make_objective([(photo_path, photo)], tmp_path, tmp_path / "m.pth")
defaults = dict(vars(CropParams()))
assert objective(optuna.trial.FixedTrial(defaults)) == 1
assert objective(optuna.trial.FixedTrial({**defaults, "thresh_min": 160,
"thresh_max": 160})) == 0
assert objective(optuna.trial.FixedTrial({**defaults, "canny_low": 100,
"canny_high": 100})) == 0
def test_cli_missing_weights(tmp_path, capsys):
pytest.importorskip("torchvision")
assert tune.main(["--model-path", str(tmp_path / "nope.pth"), "--n-trials", "1"]) == 1
assert "CNN weights not found" in capsys.readouterr().err