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]>
This commit is contained in:
2026-10-09 12:08:01 +01:00
co-authored by Claude Opus 5.5
parent c447cc41ea
commit 7b19d8f3e9
31 changed files with 2572 additions and 1086 deletions
+90
View File
@@ -0,0 +1,90 @@
import pytest
from conftest import REPO
from rowing_stats.errors import RowingStatsError
from rowing_stats.results import (
CSV_COLUMNS,
WorkoutRow,
calories,
format_pace,
format_time,
load_ground_truth,
make_row,
pace_per_500m,
parse_time,
write_csv,
)
@pytest.mark.parametrize("seconds, expected", [
(124.0, "2:04.0"),
(123.14, "2:03.1"),
(119.5, "1:59.5"),
(119.97, "2:00.0"), # rounds up into the next minute, not "1:60.0"
(60.0, "1:00.0"),
])
def test_format_pace(seconds, expected):
assert format_pace(seconds) == expected
def test_time_round_trip():
assert format_time(30, 6) == "30:06"
assert format_time(60, 33) == "60:33" # minutes run past the hour
assert parse_time("30:06") == 1806
assert parse_time("60:33") == 3633
def test_derived_metrics_match_the_committed_results():
# First row of rowing_results.csv: 30:06, 7283 m -> 2:04.0, 486 kcal
assert format_pace(pace_per_500m(1806, 7283)) == "2:04.0"
assert round(calories(7283)) == 486
def test_make_row_derives_pace_and_calories():
assert make_row("IMG_5413.JPEG", "30:06", 7283, "2025-11-05 12:08:00") == WorkoutRow(
image="IMG_5413.JPEG", time="30:06", distance_m=7283,
pace_per_500m="2:04.0", calories=486, date_taken="2025-11-05 12:08:00",
)
def test_make_row_leaves_gaps_for_missing_fields():
assert make_row("a", "30:06", None) == WorkoutRow(image="a", time="30:06")
assert make_row("a", None, 7283) == WorkoutRow(image="a", distance_m=7283, calories=486)
assert make_row("a", None, None) == WorkoutRow(image="a")
def test_csv_round_trip(tmp_path):
path = tmp_path / "out.csv"
write_csv([make_row("IMG_1.JPEG", "30:06", 7283), make_row("IMG_2.JPEG", "40:05", 9671)],
path)
header, first, _ = path.read_text().splitlines()
assert header.split(",") == CSV_COLUMNS
assert first == "IMG_1.JPEG,30:06,7283,2:04.0,486,"
assert load_ground_truth(path) == {
"IMG_1": {"time": "30:06", "distance_m": 7283},
"IMG_2": {"time": "40:05", "distance_m": 9671},
}
def test_load_ground_truth_reads_the_committed_reference():
truth = load_ground_truth(REPO / "rowing_results.csv")
assert len(truth) == 34
assert truth["IMG_5413"] == {"time": "30:06", "distance_m": 7283}
def test_load_ground_truth_missing_file(tmp_path):
with pytest.raises(RowingStatsError, match="could not read"):
load_ground_truth(tmp_path / "nope.csv")
@pytest.mark.parametrize("content", [
"name,value\nx,1\n", # wrong columns
"image,time,distance_m\nIMG_1.JPEG,30:06,far\n", # distance not a number
"image,time,distance_m\nIMG_1.JPEG,30:06\n", # short row
])
def test_load_ground_truth_rejects_other_csvs(tmp_path, content):
path = tmp_path / "bad.csv"
path.write_text(content)
with pytest.raises(RowingStatsError, match="not a results CSV"):
load_ground_truth(path)