File size: 4,369 Bytes
fbf5988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
"""Artifact-based source-frame timing checks; no third-party dependencies.

Bounds are inclusive, zero-based SOURCE frames. None means the last source
frame. The onset tolerance covers a tiny initial segment lost to encoding.
Scores are macro averages, so a long blank preamble cannot hide a missing trace.
"""


def active_interval(frame, config, last_frame):
    for interval in config['intervals']:
        end = interval.get('visible_end_frame')
        end = last_frame if end is None else end
        if interval['start_frame'] <= frame <= end:
            return interval
    return None


def score_intervals(rows, config, last_frame, min_pixels=3):
    tolerance = config.get('onset_tolerance_frames', 3)
    indexed = {}
    for row in rows:
        frame = row['source_reference_frame']
        indexed.setdefault(frame, []).append(row)

    def present(frame):
        samples = indexed.get(frame, [])
        return bool(samples) and all(
            not r.get('missing_candidate_frame') and r['actual_pixels'] > min_pixels
            for r in samples)

    def area(frame):
        samples = indexed.get(frame, [])
        return sum(r['actual_pixels'] for r in samples) / len(samples) if samples else 0

    def fraction(frames, predicate):
        return sum(bool(predicate(f)) for f in frames) / len(frames) if frames else 1.0

    intervals = []
    for interval in config['intervals']:
        start = interval['start_frame']
        end = interval.get('visible_end_frame')
        end = last_frame if end is None else end
        previous = [i for i in config['intervals'] if i['start_frame'] < start]
        earliest = start if previous else max(0, start - tolerance)
        candidates = range(earliest, min(last_frame, start + tolerance) + 1)
        onset = any(present(f) for f in candidates)
        required = list(range(start + tolerance, end + 1))
        visibility = fraction(required, present)
        components = {'onset': float(onset), 'visibility': visibility}
        stop = interval.get('growth_end_frame')
        if stop is not None and not interval.get('allow_growth_after_end', False):
            # Compare sustained growth to the visible footprint at the stop.
            # A 20% area band tolerates camera scale/codec noise. Disappearance
            # is checked independently by visibility and cannot earn hold credit.
            anchor_frames = range(max(start, stop - 2), min(end, stop + 2) + 1)
            values = sorted(area(f) for f in anchor_frames if present(f))
            anchor = values[len(values) // 2] if values else 0
            band = config.get('hold_area_tolerance_fraction', 0.20)
            hold_frames = list(range(stop + tolerance + 1, end + 1))
            components['hold'] = fraction(hold_frames, lambda f:
                anchor > min_pixels and present(f) and
                abs(area(f) - anchor) <= max(40, anchor * band))
        intervals.append(dict(interval, resolved_visible_end_frame=end,
                              components=components,
                              score=sum(components.values()) / len(components)))

    # Frames before an explicitly early-allowed start are optional. Tolerance
    # applies to onset only; between two tracers the documented reset is real.
    excluded = set()
    for interval in config['intervals']:
        start = interval['start_frame']
        if interval.get('allow_early_start', False):
            excluded.update(range(start))
        elif not any(i['start_frame'] < start for i in config['intervals']):
            excluded.update(range(max(0, start - tolerance), start))
    forbidden = [f for f in range(last_frame + 1)
                 if f not in excluded and active_interval(f, config, last_frame) is None]
    absence = fraction(forbidden, lambda f: bool(indexed.get(f)) and
                       all(not r.get('missing_candidate_frame') for r in indexed[f]) and
                       all(r['actual_pixels'] <= min_pixels for r in indexed[f]))
    parts = [x['score'] for x in intervals]
    if forbidden:
        parts.append(absence)
    return {'score': sum(parts) / len(parts), 'intervals': intervals,
            'outside_interval_absence': absence,
            'forbidden_frame_count': len(forbidden),
            'frame_index_base': 0, 'frame_domain': 'source',
            'onset_tolerance_frames': tolerance}