File size: 4,810 Bytes
97b0f0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
"""Recompute published numerical comparisons from non-media derivative tables.

Python 3.9+, standard library only. This verifies arithmetic and event pairing;
it cannot re-establish video semantics or reproduce sensor analyses without raw IMU.
"""
import csv
import json
import math
from pathlib import Path
from statistics import mean, median, pstdev

DATA = Path(__file__).resolve().parents[1] / 'data'
def rows(name):
    with (DATA / name).open(newline='') as handle:
        return list(csv.DictReader(handle))

def compare(a, b):
    a, b = sorted(a), sorted(b)
    gaps = [y-x for series in (a,b) for x,y in zip(series,series[1:])]
    limit = min(1., .45*median(gaps)) if gaps else .5
    n,m = len(a),len(b)
    dp = [[0.]*(m+1) for _ in range(n+1)]
    move = [[0]*(m+1) for _ in range(n+1)]
    for i in range(1,n+1): dp[i][0],move[i][0] = i,1
    for j in range(1,m+1): dp[0][j],move[0][j] = j,2
    for i in range(1,n+1):
        for j in range(1,m+1):
            delta = abs(a[i-1]-b[j-1])
            pair = dp[i-1][j-1]+delta/limit if delta<=limit else math.inf
            value = min(pair,dp[i-1][j]+1,dp[i][j-1]+1)
            dp[i][j] = value
            move[i][j] = 3 if value==pair else 1 if value==dp[i-1][j]+1 else 2
    i,j = n,m
    differences=[]
    while i or j:
        if move[i][j]==3:
            differences.append(b[j-1]-a[i-1]);i-=1;j-=1
        elif move[i][j]==1:i-=1
        else:j-=1
    absolute = [abs(v) for v in differences]
    return dict(matched=len(absolute),unmatched_first=n-len(absolute),unmatched_second=m-len(absolute),
                pairing_limit_seconds=limit,
                median_absolute_seconds=median(absolute) if absolute else None,
                maximum_absolute_seconds=max(absolute) if absolute else None)

def main():
    events = rows('review_events.csv')
    series = {name:[float(r['video_seconds']) for r in events if r['source']==name]
              for name in ('human_A','human_B','AI')}
    comparisons = {'human_A_vs_B':compare(series['human_A'],series['human_B']),
                   'AI_candidates_vs_A':compare(series['AI'],series['human_A']),
                   'AI_candidates_vs_B':compare(series['AI'],series['human_B'])}
    expected = [(10,.101950,.933567),(9,.263633,.499767),(10,.301250,.365067)]
    for result,(count,med,max_gap) in zip(comparisons.values(),expected):
        assert result['matched']==count
        assert math.isclose(result['median_absolute_seconds'],med,abs_tol=1e-9)
        assert math.isclose(result['maximum_absolute_seconds'],max_gap,abs_tol=1e-9)
    assert len(events)==30
    assert all(e['status']=='uncertain' for e in events if e['source']=='AI')
    anchors=rows('sync_anchors.csv')
    x=[float(r['video_seconds']) for r in anchors];y=[float(r['watch_seconds']) for r in anchors]
    mx,my=mean(x),mean(y)
    scale=sum((a-mx)*(b-my) for a,b in zip(x,y))/sum((a-mx)**2 for a in x)
    offset=my-scale*mx
    residual=[b-offset-scale*a for a,b in zip(x,y)]
    rms=1000*math.sqrt(mean(v*v for v in residual))
    maximum=1000*max(abs(v) for v in residual)
    assert len(anchors)==6 and math.isclose(rms,20.368108909930335,abs_tol=1e-9)
    assert math.isclose(maximum,40.81906035135319,abs_tol=1e-9)
    control=rows('randomized_control.csv')
    actual=[float(r['mean_correlation']) for r in control if r['condition']=='actual']
    null=[float(r['mean_correlation']) for r in control if r['condition']=='randomized']
    assert len(actual)==1 and len(null)==20
    assert (round(actual[0],3),round(mean(null),3),round(pstdev(null),3))==(.526,.525,.024)
    acf=rows('autocorrelation_summary.csv')
    assert len(acf)==7 and sum(int(r['meets_source_criterion']) for r in acf)==4
    # Rounded period columns do not redefine the source's full-precision criterion.
    timing=rows('session_timing.csv');legacy=timing[:6]
    assert [r['session_id'] for r in timing]==['H01','H02','H03','H04','H05','H06','H07','V01']
    assert sum(int(r['samples']) for r in legacy)==43563
    assert sum(int(r['shortIntervalsBelowHalfExpected']) for r in legacy)==2383
    assert sum(int(r['longIntervalsAboveOnePointFiveExpected']) for r in legacy)==2087
    assert math.isclose(float(timing[6]['secondsFromLastCaptureMarkToLastSample']),-2.238,abs_tol=1e-12)
    print(json.dumps(dict(comparisons=comparisons,
        synchronization=dict(scale=scale,offset_seconds=offset,rms_ms=rms,max_ms=maximum),
        randomized_control=dict(actual=actual[0],randomized_mean=mean(null),randomized_population_sd=pstdev(null),trials=len(null)),
        autocorrelation_direct_matches=4,legacy_median_interval_cv_percent=median(float(r['intervalJitterCvPercent']) for r in legacy),
        checked='five derivative CSV tables; no media or raw-sensor replication'),indent=2))

if __name__=='__main__':main()