File size: 2,423 Bytes
267266e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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,
    )