"""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}