{
"rows": [
{
"leg": "out",
"kind": "Launch",
"t": 63.7,
"epoch": 1796007579.0,
"value": 0.142,
"unit": "g",
"detail": "speed 10 s later 2 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 67.5,
"epoch": 1796007582.8,
"value": 0.141,
"unit": "g",
"detail": "speed 10 s later 1 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 70.7,
"epoch": 1796007586.0,
"value": 0.141,
"unit": "g",
"detail": "speed 10 s later 1 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 83.6,
"epoch": 1796007598.9,
"value": 0.135,
"unit": "g",
"detail": "speed 10 s later 18 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 210.8,
"epoch": 1796007726.1,
"value": 0.142,
"unit": "g",
"detail": "speed 10 s later 25 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 715.6,
"epoch": 1796008230.9,
"value": 0.142,
"unit": "g",
"detail": "speed 10 s later 4 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 721.8,
"epoch": 1796008237.1,
"value": 0.142,
"unit": "g",
"detail": "speed 10 s later 4 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 785.3,
"epoch": 1796008300.6,
"value": 0.226,
"unit": "g",
"detail": "speed 10 s later 48 km/h"
},
{
"leg": "out",
"kind": "Launch",
"t": 866.5,
"epoch": 1796008381.8,
"value": 0.17,
"unit": "g",
"detail": "speed 10 s later 15 km/h"
},
{
"leg": "out",
"kind": "Boost",
"t": 259.3,
"epoch": 1796007774.5,
"value": 11.17,
"unit": "psi",
"detail": "5.6 s, 2579 rpm at peak, 85 km/h"
},
{
"leg": "out",
"kind": "Boost",
"t": 336.3,
"epoch": 1796007851.5,
"value": 9.86,
"unit": "psi",
"detail": "1.4 s, 2444 rpm at peak, 100 km/h"
},
{
"leg": "out",
"kind": "Boost",
"t": 426.1,
"epoch": 1796007941.4,
"value": 2.18,
"unit": "psi",
"detail": "0.0 s, 2922 rpm at peak, 118 km/h"
},
{
"leg": "out",
"kind": "Boost",
"t": 494.5,
"epoch": 1796008009.7,
"value": 2.03,
"unit": "psi",
"detail": "0.2 s, 2663 rpm at peak, 108 km/h"
},
{
"leg": "out",
"kind": "Boost",
"t": 631.9,
"epoch": 1796008147.2,
"value": 4.64,
"unit": "psi",
"detail": "3.2 s, 1749 rpm at peak, 103 km/h"
},
{
"leg": "out",
"kind": "Boost",
"t": 736.9,
"epoch": 1796008252.2,
"value": 5.95,
"unit": "psi",
"detail": "1.2 s, 2086 rpm at peak, 43 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 69.6,
"epoch": 1796010873.5,
"value": 0.142,
"unit": "g",
"detail": "speed 10 s later 2 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 84.3,
"epoch": 1796010888.2,
"value": 0.144,
"unit": "g",
"detail": "speed 10 s later 10 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 111.4,
"epoch": 1796010915.4,
"value": 0.177,
"unit": "g",
"detail": "speed 10 s later 34 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 145.6,
"epoch": 1796010949.6,
"value": 0.283,
"unit": "g",
"detail": "speed 10 s later 58 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 264.3,
"epoch": 1796011068.2,
"value": 0.198,
"unit": "g",
"detail": "speed 10 s later 35 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 327.0,
"epoch": 1796011131.0,
"value": 0.17,
"unit": "g",
"detail": "speed 10 s later 28 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 349.9,
"epoch": 1796011153.9,
"value": 0.17,
"unit": "g",
"detail": "speed 10 s later 19 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 687.5,
"epoch": 1796011491.4,
"value": 0.212,
"unit": "g",
"detail": "speed 10 s later 47 km/h"
},
{
"leg": "home",
"kind": "Launch",
"t": 885.4,
"epoch": 1796011689.4,
"value": 0.196,
"unit": "g",
"detail": "speed 10 s later 33 km/h"
},
{
"leg": "home",
"kind": "Boost",
"t": 376.5,
"epoch": 1796011180.4,
"value": 11.02,
"unit": "psi",
"detail": "3.8 s, 1826 rpm at peak, 72 km/h"
},
{
"leg": "home",
"kind": "Boost",
"t": 397.0,
"epoch": 1796011200.9,
"value": 3.48,
"unit": "psi",
"detail": "0.4 s, 1887 rpm at peak, 95 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 126.3,
"epoch": 1796010930.3,
"value": 8.7,
"unit": "s",
"detail": "from 45 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 188.2,
"epoch": 1796010992.2,
"value": 8.8,
"unit": "s",
"detail": "from 65 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 223.0,
"epoch": 1796011027.0,
"value": 0.0,
"unit": "s",
"detail": "from 59 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 236.5,
"epoch": 1796011040.5,
"value": 15.5,
"unit": "s",
"detail": "from 70 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 279.9,
"epoch": 1796011083.9,
"value": 1.2,
"unit": "s",
"detail": "from 39 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 300.9,
"epoch": 1796011104.8,
"value": 1.0,
"unit": "s",
"detail": "from 61 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 308.5,
"epoch": 1796011112.5,
"value": 9.1,
"unit": "s",
"detail": "from 57 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 400.8,
"epoch": 1796011204.8,
"value": 1.4,
"unit": "s",
"detail": "from 98 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 544.6,
"epoch": 1796011348.6,
"value": 0.4,
"unit": "s",
"detail": "from 118 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 552.1,
"epoch": 1796011356.0,
"value": 0.2,
"unit": "s",
"detail": "from 116 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 574.6,
"epoch": 1796011378.6,
"value": 0.8,
"unit": "s",
"detail": "from 115 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 611.6,
"epoch": 1796011415.6,
"value": 1.8,
"unit": "s",
"detail": "from 99 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 635.9,
"epoch": 1796011439.9,
"value": 3.2,
"unit": "s",
"detail": "from 115 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 643.2,
"epoch": 1796011447.1,
"value": 10.5,
"unit": "s",
"detail": "from 113 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 663.7,
"epoch": 1796011467.7,
"value": 9.9,
"unit": "s",
"detail": "from 85 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 698.9,
"epoch": 1796011502.9,
"value": 4.2,
"unit": "s",
"detail": "from 46 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 734.1,
"epoch": 1796011538.1,
"value": 17.1,
"unit": "s",
"detail": "from 66 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 779.4,
"epoch": 1796011583.3,
"value": 4.2,
"unit": "s",
"detail": "from 71 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 798.5,
"epoch": 1796011602.5,
"value": 8.3,
"unit": "s",
"detail": "from 54 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 825.7,
"epoch": 1796011629.6,
"value": 8.7,
"unit": "s",
"detail": "from 62 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 864.9,
"epoch": 1796011668.9,
"value": 8.5,
"unit": "s",
"detail": "from 44 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 916.0,
"epoch": 1796011720.0,
"value": 7.6,
"unit": "s",
"detail": "from 58 km/h"
},
{
"leg": "home",
"kind": "Fuel cut",
"t": 948.4,
"epoch": 1796011752.4,
"value": 7.9,
"unit": "s",
"detail": "from 51 km/h"
},
{
"leg": "start",
"kind": "Engine start",
"t": 15.6,
"epoch": 1796006894.2,
"value": 9.176,
"unit": "V",
"detail": "resting 12.1 V before cranking, 21.0 \u00b0C coolant"
}
],
"meta": [
{
"label": "segment.py",
"value": "\"\"\"Deduce what the car was doing from the samples alone: no typed marks.\n\nSegments (per drive):\n  Stop     speed 0 for 5 s or more with the engine running\n  Freeway  speed held at 80 km/h (50 mph) or more; short dips stay freeway\n  City     any other moving stretch (stop-and-go, 25-50 mph roads)\nStretches shorter than 30 s are merged into the stretch before them, so a\nbrief slowdown on the freeway does not become \"city\".\n\nEvents:\n  Engine start  RPM goes from 0 to above 400; lowest voltage in the 3 s around it\n  Launch        pulling away from a stop; peak acceleration in the next 10 s\n  Boost         manifold pressure above outside air by 2 psi (14 kPa) or more\n  Fuel cut      wideband lambda at 1.9 or more (injectors off while coasting)\n\"\"\"\nimport csv, json, statistics as st\n\nBARO_KPA = 97          # outside air pressure the car reported in both health checks\nFREEWAY_KMH = 80\nMIN_STOP_S = 5\nMIN_SEG_S = 30\n\nrows = list(csv.DictReader(open('/home/claude/g70/v2/samples.csv')))\ndef f(r, k):\n    v = r.get(k, '')\n    return None if v == '' else float(v)\n\ndef stats(seg, leg):\n    sp = [f(r, 'speed_kmh') for r in seg]\n    t = [f(r, 't') for r in seg]\n    dist = sum((t[i] - t[i - 1]) * (sp[i - 1] or 0) / 3600 for i in range(1, len(seg)))\n    def col(k):\n        return [f(r, k) for r in seg if f(r, k) is not None]\n    def mean(k):\n        v = col(k); return round(st.mean(v), 2) if v else None\n    def mx(k):\n        v = col(k); return round(max(v), 2) if v else None\n    def mn(k):\n        v = col(k); return round(min(v), 2) if v else None\n    trim1 = [f(r, 'stft1') + f(r, 'ltft1') for r in seg if f(r, 'stft1') is not None and f(r, 'ltft1') is not None]\n    trim2 = [f(r, 'stft2') + f(r, 'ltft2') for r in seg if f(r, 'stft2') is not None and f(r, 'ltft2') is not None]\n    stops = sum(1 for i in range(1, len(sp)) if sp[i - 1] > 0 and sp[i] == 0)\n    return {\n        'start_s': round(t[0], 1), 'end_s': round(t[-1], 1), 'duration_s': round(t[-1] - t[0], 1),\n        'start_epoch': round(f(seg[0], 'epoch'), 1),\n        'distance_km': round(dist, 2), 'avg_speed_kmh': round(dist / ((t[-1] - t[0]) / 3600), 1) if t[-1] > t[0] else 0,\n        'max_speed_kmh': mx('speed_kmh'), 'avg_rpm': mean('rpm'), 'max_rpm': mx('rpm'),\n        'avg_load': mean('load'), 'max_load': mx('load'), 'max_map_kpa': mx('map_kpa'),\n        'coolant_start_c': f(seg[0], 'coolant_c'), 'coolant_end_c': f(seg[-1], 'coolant_c'), 'max_coolant_c': mx('coolant_c'),\n        'avg_iat_c': mean('iat_c'), 'min_volts': mn('volts'), 'avg_volts': mean('volts'),\n        'trim_b1': round(st.mean(trim1), 2) if trim1 else None, 'trim_b2': round(st.mean(trim2), 2) if trim2 else None,\n        'max_cat_c': mx('cat1_c'), 'brief_stops': stops,\n    }\n\nsegments, events = [], []\nfor leg in ['out', 'home']:\n    r = [x for x in rows if x['leg'] == leg]\n    # 1. per-sample class\n    cls = []\n    for x in r:\n        s = f(x, 'speed_kmh') or 0\n        cls.append('Stop' if s == 0 else ('Freeway' if s >= FREEWAY_KMH else 'City'))\n    # 2. runs\n    runs = []\n    for i, c in enumerate(cls):\n        if runs and runs[-1][0] == c: runs[-1][2] = i\n        else: runs.append([c, i, i])\n    dur = lambda run: f(r[run[2]], 't') - f(r[run[1]], 't')\n    # 3. short stops are part of the drive around them\n    for run in runs:\n        if run[0] == 'Stop' and dur(run) < MIN_STOP_S: run[0] = 'City'\n    # 4. merge short moving runs into the previous run (or next, at the start)\n    changed = True\n    while changed:\n        changed = False\n        merged = []\n        for run in runs:\n            if merged and merged[-1][0] == run[0]:\n                merged[-1][2] = run[2]; changed = True\n            else:\n                merged.append(run)\n        runs = merged\n        for i, run in enumerate(runs):\n            if run[0] != 'Stop' and dur(run) < MIN_SEG_S and len(runs) > 1:\n                nb = runs[i - 1] if i > 0 else runs[i + 1]\n                if nb[0] != 'Stop':\n                    run[0] = nb[0]; changed = True\n                    break\n    # 5. name first/last stops\n    for i, run in enumerate(runs):\n        seg = r[run[1]:run[2] + 1]\n        name = run[0]\n        if run[0] == 'Stop' and i == 0: name = 'Idle before pulling out'\n        elif run[0] == 'Stop' and i == len(runs) - 1: name = 'Final stop'\n        row = {'id': f'{leg}-{i + 1:02d}', 'leg': leg, 'kind': run[0], 'label': name}\n        row.update(stats(seg, leg))\n        segments.append(row)\n\n    # events\n    t = [f(x, 't') for x in r]\n    sp = [f(x, 'speed_kmh') or 0 for x in r]\n    for i in range(1, len(r)):\n        if sp[i - 1] == 0 and sp[i] > 0:\n            j = i; peak = 0; at = t[i]\n            while j < len(r) and t[j] - t[i] <= 10:\n                k = max(i - 1, j - 5)\n                if t[j] > t[k]:\n                    a = (sp[j] - sp[k]) / 3.6 / (t[j] - t[k])\n                    if a > peak: peak, at = a, t[j]\n                j += 1\n            events.append({'leg': leg, 'kind': 'Launch', 't': round(t[i], 1), 'epoch': round(f(r[i], 'epoch'), 1),\n                           'value': round(peak / 9.81, 3), 'unit': 'g', 'detail': f'speed 10 s later {round(sp[min(j, len(r)) - 1])} km/h'})\n    i = 0\n    while i < len(r):\n        m = f(r[i], 'map_kpa')\n        if m is not None and m - BARO_KPA >= 14:\n            j = i; pk = m; pki = i\n            while j < len(r) and (f(r[j], 'map_kpa') or 0) - BARO_KPA >= 14:\n                if f(r[j], 'map_kpa') > pk: pk, pki = f(r[j], 'map_kpa'), j\n                j += 1\n            events.append({'leg': leg, 'kind': 'Boost', 't': round(t[i], 1), 'epoch': round(f(r[i], 'epoch'), 1),\n                           'value': round((pk - BARO_KPA) * 0.145038, 2), 'unit': 'psi',\n                           'detail': f'{round(t[j - 1] - t[i], 1)} s, {round(f(r[pki], \"rpm\"))} rpm at peak, {round(sp[pki])} km/h'})\n            i = j\n        else:\n            i += 1\n    i = 0\n    while i < len(r):\n        l = f(r[i], 'lambda1')\n        if l is not None and l >= 1.9:\n            j = i\n            while j < len(r) and (f(r[j], 'lambda1') or 0) >= 1.9: j += 1\n            events.append({'leg': leg, 'kind': 'Fuel cut', 't': round(t[i], 1), 'epoch': round(f(r[i], 'epoch'), 1),\n                           'value': round(t[j - 1] - t[i], 1), 'unit': 's', 'detail': f'from {round(sp[i])} km/h'})\n            i = j\n        else:\n            i += 1\n\n# engine start in the start capture\nr = [x for x in rows if x['leg'] == 'start']\nseen_off = False\nfor i in range(1, len(r)):\n    if (f(r[i - 1], 'rpm') or 0) == 0: seen_off = True\n    if seen_off and (f(r[i], 'rpm') or 0) > 400:\n        t0 = f(r[i], 't')\n        win = [x for x in r if abs(f(x, 't') - t0) <= 3 and f(x, 'volts') is not None]\n        vmin = min(win, key=lambda x: f(x, 'volts'))\n        rest = [f(x, 'volts') for x in r if f(x, 't') < t0 - 1 and f(x, 'volts') is not None]\n        events.append({'leg': 'start', 'kind': 'Engine start', 't': round(t0, 1), 'epoch': round(f(r[i], 'epoch'), 1),\n                       'value': f(vmin, 'volts'), 'unit': 'V',\n                       'detail': f'resting {round(st.mean(rest), 2) if rest else \"?\"} V before cranking, {f(r[i], \"coolant_c\")} \u00b0C coolant'})\n        break\n\ncode = open(__file__).read()\nmeta = [{'label': 'segment.py', 'value': code, 'type': 'code', 'lang': 'python'}, {'label': 'Input', 'value': 'samples.csv'}]\njson.dump({'rows': segments, 'meta': meta}, open('/home/claude/g70/v2/segments.json', 'w'), indent=0)\njson.dump({'rows': events, 'meta': meta}, open('/home/claude/g70/v2/events.json', 'w'), indent=0)\nfor s in segments:\n    print(s['id'], s['label'], s['duration_s'], s['distance_km'], s['max_speed_kmh'], s['trim_b1'])\nfrom collections import Counter\nprint(Counter((e['leg'], e['kind']) for e in events))\nfor e in events:\n    if e['kind'] in ('Engine start',) or (e['kind'] == 'Launch' and e['value'] > 0.25) or (e['kind'] == 'Boost' and e['value'] > 8): print(e)\n",
"type": "code",
"lang": "python"
},
{
"label": "Input",
"value": "samples.csv"
}
]
}