-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_to_sqlite.py
More file actions
176 lines (143 loc) · 5.31 KB
/
Copy pathmigrate_to_sqlite.py
File metadata and controls
176 lines (143 loc) · 5.31 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
"""
Migrate JSON snapshots to SQLite.
Pass 1: Scan all snap_*.json, collect unique nuclei (synset → word + anchor).
Pass 2: For each snap+delta pair chronologically, insert snapshot + observations.
Pass 3: Import positions from nucleus_positions.json.
Commits per snapshot for resumability. Skips already-imported timestamps.
"""
import json
import re
import sys
from pathlib import Path
from db import (
get_connection, ensure_schema, pack_anchor, parse_wall_time,
)
BASE_DIR = Path(__file__).parent
SNAPSHOTS_DIR = BASE_DIR / 'snapshots'
POSITIONS_PATH = BASE_DIR / 'nucleus_positions.json'
def find_all_timestamps():
"""Find all snapshot timestamps in chronological order."""
timestamps = []
for p in SNAPSHOTS_DIR.glob('snap_*.json'):
m = re.match(r'snap_(\d{8}_\d{6})\.json', p.name)
if m:
timestamps.append(m.group(1))
timestamps.sort()
return timestamps
def load_json(path):
with open(path) as f:
return json.load(f)
def main():
conn = get_connection()
ensure_schema(conn)
timestamps = find_all_timestamps()
print(f"Found {len(timestamps)} snapshots to migrate")
# Check which timestamps are already imported
existing = set(
row[0] for row in conn.execute("SELECT timestamp FROM snapshots").fetchall()
)
to_import = [ts for ts in timestamps if ts not in existing]
print(f" {len(existing)} already imported, {len(to_import)} remaining")
if not to_import:
print("Nothing to do!")
# Still import positions in case they're missing
_import_positions(conn)
conn.close()
return
# Pass 1+2 combined: process each snapshot chronologically
nuclei_seen = set(
row[0] for row in conn.execute("SELECT synset FROM nuclei").fetchall()
)
for i, ts in enumerate(to_import):
snap_path = SNAPSHOTS_DIR / f"snap_{ts}.json"
delta_path = SNAPSHOTS_DIR / f"delta_{ts}.json"
try:
snapshot = load_json(snap_path)
except Exception as e:
print(f" [{i+1}/{len(to_import)}] SKIP {ts}: {e}")
continue
delta = {}
if delta_path.exists():
try:
delta = load_json(delta_path)
except Exception:
pass
wall_time = parse_wall_time(ts)
# Insert new nuclei
new_nuclei = []
for synset, data in snapshot.items():
if synset not in nuclei_seen and 'anchor' in data:
new_nuclei.append((synset, data['word'], pack_anchor(data['anchor'])))
nuclei_seen.add(synset)
if new_nuclei:
conn.executemany(
"INSERT OR IGNORE INTO nuclei (synset, word, anchor) VALUES (?, ?, ?)",
new_nuclei,
)
# Insert snapshot row
conn.execute(
"INSERT INTO snapshots (timestamp, wall_time) VALUES (?, ?)",
(ts, wall_time),
)
snap_id = conn.execute(
"SELECT id FROM snapshots WHERE timestamp = ?", (ts,)
).fetchone()[0]
# Insert observations
rows = []
for synset, data in snapshot.items():
if data.get('update_count', 0) > 0:
rows.append((
snap_id,
synset,
data['update_count'],
data.get('exemplar_count', 0),
data.get('uncertainty', 1.0),
delta.get(synset, 0),
))
if rows:
conn.executemany(
"INSERT INTO observations "
"(snapshot_id, synset, update_count, exemplar_count, uncertainty, delta) "
"VALUES (?, ?, ?, ?, ?, ?)",
rows,
)
conn.commit()
if (i + 1) % 100 == 0 or i == len(to_import) - 1:
print(f" [{i+1}/{len(to_import)}] {ts} "
f"({len(rows)} obs, {len(new_nuclei)} new nuclei)")
# Pass 3: import positions
_import_positions(conn)
# Summary
n_nuclei = conn.execute("SELECT COUNT(*) FROM nuclei").fetchone()[0]
n_snaps = conn.execute("SELECT COUNT(*) FROM snapshots").fetchone()[0]
n_obs = conn.execute("SELECT COUNT(*) FROM observations").fetchone()[0]
n_pos = conn.execute("SELECT COUNT(*) FROM positions").fetchone()[0]
db_path = conn.execute("PRAGMA database_list").fetchone()[2]
db_size_mb = Path(db_path).stat().st_size / (1024 * 1024)
print(f"\nMigration complete!")
print(f" {n_nuclei} nuclei, {n_snaps} snapshots, {n_obs} observations, {n_pos} positions")
print(f" Database size: {db_size_mb:.1f} MB")
conn.close()
def _import_positions(conn):
if not POSITIONS_PATH.exists():
print("No positions file found, skipping")
return
positions = load_json(POSITIONS_PATH)
existing = set(
row[0] for row in conn.execute("SELECT synset FROM positions").fetchall()
)
new_pos = [
(synset, pos[0], pos[1])
for synset, pos in positions.items()
if synset not in existing
]
if new_pos:
conn.executemany(
"INSERT OR IGNORE INTO positions (synset, x, y) VALUES (?, ?, ?)",
new_pos,
)
conn.commit()
print(f" Positions: {len(new_pos)} new, {len(existing)} existing")
if __name__ == '__main__':
main()