Day 81: Size recommendation logic: measurements → "M, 91%"
From centimeters to a size
The engine's headline output is a size and confidence — 'M, 91%'. This maps measurements to a brand's size chart: compare the person's chest/shoulder/etc. against each size's ranges and pick the best fit. Confidence comes from how cleanly the measurements fall inside one size (deep in M's range → high confidence) versus straddling a boundary (between M and L → lower confidence), combined with the measurement confidences you've propagated all stage.
def recommend_size(measurements, size_chart):
scores = {}
for size, ranges in size_chart.items():
fit = 0.0
for dim, (lo, hi) in ranges.items():
cm = measurements[dim]["cm"]
center = (lo + hi) / 2
# 1.0 dead-center, decaying toward and past the range edges
fit += max(0.0, 1.0 - abs(cm - center) / ((hi - lo) / 2 + 1e-6))
scores[size] = fit / len(ranges)
best = max(scores, key=scores.get)
measure_conf = min(m["confidence"] for m in measurements.values())
size_conf = round(scores[best] * measure_conf, 2)
return {"size": best, "confidence": size_conf}The confidence must earn its place
'91%' should mean something: the measurements confidently place the person inside size M. If measurements are shaky (occlusion, off-angle) or the person straddles M/L, the number must drop — and the UI can then suggest 'M or L, try both'. A confidence that's always high is a confidence that's lying; make it move with real uncertainty.
Key terms
- Size chart
- A brand's mapping from body measurement ranges to garment sizes (S/M/L...).
- Fit score
- A measure of how well a set of measurements falls within a given size's ranges.
- Boundary case
- Measurements straddling two sizes, warranting lower confidence and a two-size suggestion.
A person's measurements fall right on the boundary between size M and size L. What should the size confidence do?