"""Random access to 32-book continuous timelines; model consumes first twelve.""" from collections import OrderedDict import hashlib,io,json,sqlite3 import numpy as np from common import * class Data: def __init__(self,pilot=False,path=None): if pilot:raise RuntimeError('No partial-corpus production mode') self.root=RUN/'large';self.ready=json.loads((self.root/'DATA_READY.json').read_text()) self.db=sqlite3.connect('file:'+str(path or self.root/'mix.sqlite')+'?mode=ro&immutable=1',uri=True) self.db.row_factory=sqlite3.Row;self.files=OrderedDict();self.cache=OrderedDict() self.count=self.db.execute('SELECT count(*) FROM presentations').fetchone()[0] def episode(self,uid): if uid not in self.cache: r=self.db.execute('SELECT * FROM episodes WHERE uid=?',(uid,)).fetchone() if not r:raise KeyError(uid) if r['tar'] not in self.files: self.files[r['tar']]=open(r['tar'],'rb') if len(self.files)>16:self.files.popitem(last=False)[1].close() f=self.files[r['tar']];f.seek(r['code_offset']);blob=f.read(r['code_size']) if hashlib.sha256(blob).hexdigest()!=r['code_sha']:raise RuntimeError('Continuous codec timeline hash changed') codes=np.load(io.BytesIO(blob),allow_pickle=False) if codes.ndim!=2 or codes.shape[1]!=32 or codes.dtype.kind not in 'iu' or codes.min()<0 or codes.max()>1023:raise ValueError('Invalid podcast codec array') self.cache[uid]=codes if len(self.cache)>8:self.cache.popitem(last=False) self.cache.move_to_end(uid);return self.cache[uid] def row(self,key,condition='reference'): r=self.db.execute('SELECT * FROM samples WHERE key=?',(int(key),)).fetchone() if not r:raise KeyError(key) ex=json.loads(r['example_json']);codes=self.episode(r['episode']) a,b=ex['target_frame_start'],ex['target_frame_end'];ca,cb=ex['context_frame_start'],ex['context_frame_end'] if not 0<=ca<=cb<=a750:raise ValueError('Future context or invalid boundaries') refs={} for speaker,q in ex['speaker_references'].items(): ra,rb=q['start_frame'],q['end_frame'] if not 0<=ra=? AND pos