Files
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

118 lines
3.8 KiB
Python

from collections import Counter
import pytest
from rowing_stats.parsing import (
extract_distances,
extract_distances_by_method,
extract_times,
extract_times_by_method,
find_distances,
find_times,
image_base_name,
pick_best,
)
@pytest.mark.parametrize("text, expected", [
("30:06", ["30:06"]),
("30:06 29\n7283", ["30:06"]),
("30:06\n40:05", ["30:06", "40:05"]),
("30:06 30:06", ["30:06", "30:06"]), # each occurrence is a vote
("", []),
("no digits here", []),
("2:04", []), # a pace: one-digit minutes
("02:04", []), # under the 10-minute floor
("09:59", []),
("10:00", ["10:00"]),
("30:60", []), # seconds out of range
("99:00", ["99:00"]),
("130:06", ["30:06"]), # a stray leading digit is dropped
])
def test_find_times_mm_ss(text, expected):
assert find_times(text) == expected
@pytest.mark.parametrize("text, expected", [
("1:00:08", ["60:08"]), # normalised to total minutes
("1:30:00", ["90:00"]),
("2:00:00", ["120:00"]),
("2:00:01", ["120:01"]), # bound is on minutes only
("2:01:00", []), # over two hours
("0:05:00", []), # under ten minutes
("0:30:06", ["30:06"]),
("1:00:08 30:06", ["60:08", "30:06"]),
])
def test_find_times_h_mm_ss(text, expected):
assert find_times(text) == expected
def test_find_times_does_not_reread_the_tail_of_an_hour_time():
# "1:00:08" must not also produce "00:08", nor "1:30:45" a second "30:45"
assert find_times("1:30:45") == ["90:45"]
# ...even when the hour-format time itself was rejected
assert find_times("2:30:45") == []
@pytest.mark.parametrize("text, expected", [
("7283", ["7283"]),
("13879 26911", ["13879", "26911"]),
("999", []), # too short
("1000 50000", ["1000", "50000"]), # bounds are inclusive
("50001", []),
("0999", []), # four digits, but under 1000 m
("07337", ["07337"]), # a leading zero is kept as read
("123456", []), # six digits is not a distance
("30:06", []),
("72.83", []),
("", []),
])
def test_find_distances(text, expected):
assert find_distances(text) == expected
LABELED = [
("s2_otsu", "30:06 29\n7283"),
("s2_otsu_inv", "30:06\n7288"),
("s3_otsu", "38:06\n7283"),
("s3_otsu_inv", ""),
]
def test_votes_are_counted_across_variants():
assert extract_times(LABELED) == Counter({"30:06": 2, "38:06": 1})
assert extract_distances(LABELED) == Counter({"7283": 2, "7288": 1})
def test_majority_wins():
assert pick_best(extract_times(LABELED)) == "30:06"
assert pick_best(extract_distances(LABELED)) == "7283"
def test_pick_best_empty_is_none():
assert pick_best(Counter()) is None
def test_pick_best_tie_goes_to_first_seen():
assert pick_best(extract_times([("a", "30:06"), ("b", "31:06")])) == "30:06"
assert pick_best(extract_times([("a", "31:06"), ("b", "30:06")])) == "31:06"
def test_by_method_groups_and_shows_abstentions():
assert extract_times_by_method(LABELED) == {
"s2_otsu": {"30:06"}, "s2_otsu_inv": {"30:06"},
"s3_otsu": {"38:06"}, "s3_otsu_inv": set(),
}
assert extract_distances_by_method(LABELED)["s3_otsu_inv"] == set()
assert extract_distances_by_method(LABELED)["s2_otsu_inv"] == {"7288"}
@pytest.mark.parametrize("filename, expected", [
("IMG_5413_screen.jpg", "IMG_5413"),
("IMG_5413_screen (1).jpg", "IMG_5413"),
("IMG_5413_screen (12).jpg", "IMG_5413"),
("IMG_5413.JPEG", "IMG_5413"),
])
def test_image_base_name(filename, expected):
assert image_base_name(filename) == expected