from dataclasses import dataclass from typing import Any, Mapping CORE_TELEMETRY_FEATURES = frozenset({ "Speed_Mean", "RPM_Mean", "Throttle_Mean", "TyreLife", "Position", }) MIN_COVERAGE_THRESHOLD = 0.25 # Below 25% is almost certainly an incomplete out-lap @dataclass(frozen=True, slots=True) class FeatureQualityReport: total_features: int present_features: int coverage: float core_present: int core_total: int confidence: str # "high", "medium", "low", "provisional" is_usable: bool reason: str | None = None def evaluate_feature_coverage(row: Mapping[str, Any]) -> FeatureQualityReport: """Analyze a 39-feature row to determine completeness and prediction confidence.""" total = len(row) if total == 0: return FeatureQualityReport( total_features=0, present_features=0, coverage=0.0, core_present=0, core_total=len(CORE_TELEMETRY_FEATURES), confidence="provisional", is_usable=False, reason="Empty feature row", ) present = sum( 1 for value in row.values() if value is not None and value != "UNKNOWN" and str(value).strip() != "" ) coverage = round(present / total, 4) core_present = sum( 1 for feat in CORE_TELEMETRY_FEATURES if row.get(feat) is not None ) core_total = len(CORE_TELEMETRY_FEATURES) # Determine confidence level if coverage >= 0.70 and core_present >= 4: confidence = "high" is_usable = True reason = None elif coverage >= 0.40 and core_present >= 2: confidence = "medium" is_usable = True reason = None elif coverage >= MIN_COVERAGE_THRESHOLD and core_present >= 2: confidence = "low" is_usable = True reason = "Partial telemetry; provisional prediction" else: confidence = "provisional" is_usable = False reason = f"Coverage too low ({coverage:.1%}); out-lap or pit lane idle" return FeatureQualityReport( total_features=total, present_features=present, coverage=coverage, core_present=core_present, core_total=core_total, confidence=confidence, is_usable=is_usable, reason=reason, )