import numpy as np, json, urllib.request, collections, sys SP="/private/tmp/claude-501/-Users-timlingo/82369039-a20e-4b5a-8a5e-28234a57b996/scratchpad" EV="/Users/timlingo/Development/neuron-technologies/_wt-assoc-leg/tools/retrieval-eval/" np.seterr(all='ignore') M=np.load(SP+'/emb.npy'); eids=open(SP+'/ids.txt',encoding='utf-8',errors='surrogateescape').read().split('\n') eidx={k:i for i,k in enumerate(eids)} d=json.load(open('/Users/timlingo/neuron-memory-backups/snapshot-pre-repair-20260806.json',encoding='utf-8',errors='surrogateescape')) N={n['id']:n for n in d['nodes']} STRUCT={"identity","contains","superseded_by","references","embodies","demonstrated_by","canonical-self","depends_on","currently_holds","activates"} adj=collections.defaultdict(list); hasstruct=set() for e in d['edges']: if e.get('relation') not in STRUCT: continue w=float(e.get('weight') or 0.0) adj[e['from_id']].append((e['to_id'],w)); adj[e['to_id']].append((e['from_id'],w)) hasstruct.add(e['from_id']); hasstruct.add(e['to_id']) del d gold={q['id']:q for q in json.load(open(EV+"gold_set.json"))['queries']} LEX={r['id']:r['returned'] for r in json.load(open(EV+"results-main.json"))['rows']} CACHE={} def emb(t): if t in CACHE: return CACHE[t] b=json.dumps({"model":"nomic-embed-text","prompt":t}).encode() r=urllib.request.Request("http://127.0.0.1:11434/api/embeddings",data=b,headers={"Content-Type":"application/json"}) v=np.array(json.load(urllib.request.urlopen(r,timeout=60))["embedding"],dtype=np.float32) v=v/(np.linalg.norm(v)+1e-9); CACHE[t]=v; return v FIRE=0.02; DECAY=0.7; DEPTH=2; SEED_MIN=0.60; ASSOC_MAX=64 def assoc(seeds, s): act={x:1.0 for x in seeds}; seen={x:2 for x in seeds} Q=[(x,0) for x in seeds]; h=0 while h=DEPTH: continue p=act[cur] for oid,w in adj.get(cur,()): n=N.get(oid) if not n or n.get('node_type') in ('Tag','InternalStateEvent'): continue na=p*w*DECAY*float(n.get('salience') or 0.0) if na=lim: break if si=lim: break if aiSEED_MIN] A=[] if mode!='hybrid': seeds=[x for x in L[:3] if x in N] if mode=='semseed': seeds=seeds+[eids[j] for j in ordr[:K] if eids[j] in N and eids[j] not in seeds] A=assoc(seeds,s) if seeds else [] res[qid]=inter3(L,S,A) return res def score(res,label): hits=0; det={} for qid,q in gold.items(): out=res[qid][:5] if q['category']=='nonsense': ok = (len(res[qid])==0) elif q['category']=='superseded': rel=q['relevant']; must=q.get('must_outrank') or {} ok=False for good,bad in (must.items() if isinstance(must,dict) else []): ok = good in res[qid] and (bad not in res[qid] or res[qid].index(good)