Files
myGarminApp/tools/compare_csv.py
Riaan Doorduin 1075698321 Phase 0: mygarmin package — library, importers, dedupe, dashboard, exports, CLI
Generalised from personal scripts: SQLite library with untouched originals, importers for FIT, TCX,
GPX, AlpineQuest .trk, Strava and Garmin Connect exports, the Garmin Connect API and a USB watch;
cross-source de-duplication; time zones from GPS; data-driven dashboard and GPX/.trk exports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 12:35:08 +02:00

33 lines
2.4 KiB
Python

"""Compare a library with an all_activities.csv written by the original scripts (the reference for phase 0).
usage: python tools/compare_csv.py <library> <all_activities.csv>"""
import collections, csv, sys
from datetime import datetime
sys.path.insert(0, 'src')
from mygarmin.library import Library
from mygarmin.timeutil import parse_utc
lib = Library(sys.argv[1])
SHOWN = (None, 'suspect GPS', 'summary only') # flags that still appear in the dashboard
new = [r for r in lib.activities() if not r['dup_of'] and r['flag'] in SHOWN]
old = list(csv.DictReader(open(sys.argv[2], encoding='utf-8')))
print('per source old -> new'); co, cn = collections.Counter(r['src'] for r in old), collections.Counter(r['source'] for r in new)
for s in sorted(set(co) | set(cn)): print(f' {s:12s} {co[s]:5d} {cn[s]:5d}')
key = lambda t: parse_utc(t).timestamp()
idx = sorted((key(r['start_utc']), i) for i, r in enumerate(new))
used, miss, diffs = set(), [], collections.Counter(); ex = collections.defaultdict(list)
for o in old:
t = key(o['start_utc'])
c = [i for tt, i in idx if abs(tt - t) < 120 and i not in used]
if not c: miss.append(o); continue
n = new[c[0]]; used.add(c[0])
if n['source'] != o['src']: diffs['source'] += 1; ex['source'].append((o['start_local'], o['src'], n['source']))
if abs((n['dist_m'] or 0) / 1000 - float(o['dist_km'] or 0)) > 0.05: diffs['dist'] += 1; ex['dist'].append((o['start_local'], o['src'], o['dist_km'], round((n['dist_m'] or 0) / 1000, 2)))
if abs((n['moving_s'] or 0) / 60 - float(o['moving_min'] or 0)) > 0.5: diffs['moving'] += 1; ex['moving'].append((o['start_local'], o['src'], o['moving_min'], round((n['moving_s'] or 0) / 60, 1)))
if n['sport'] != o['sport']: diffs['sport'] += 1; ex['sport'].append((o['start_local'], o['src'], o['sport'], n['sport']))
if n['start_local'][:16] != o['start_local'][:16]: diffs['local time'] += 1; ex['local time'].append((o['start_local'], o['src'], n['start_local']))
print(f'matched {len(used)}; only in old {len(miss)}; only in new {len(new) - len(used)}')
for k, v in diffs.items(): print(f' differs in {k}: {v}', ex[k][:6])
print('only old:', [(o['start_local'], o['src'], o['sport'], o['dist_km']) for o in miss[:15]])
print('only new:', [(n['start_local'], n['source'], n['sport'], round((n['dist_m'] or 0) / 1000, 2), n['flag']) for i, n in enumerate(new) if i not in used][:15])