"""Load original histories and reconstruct published evaluation windows. Download this module or copy its functions. No model/API credentials are needed. """ import gzip import json def load_users(repo_id='WZDavid/MemoryVCD'): from huggingface_hub import hf_hub_download path = hf_hub_download(repo_id, 'users/users_12domains.jsonl.gz', repo_type='dataset') with gzip.open(path, 'rt') as f: return [json.loads(line) for line in f if line.strip()] def split_window(user, window): """Return memory and targets for one evaluation_windows row. Time windows use target-domain chronological prefixes. Domain windows use strictly earlier timestamps and concatenate sorted source-domain segments. Model-specific memory truncation/retrieval/compression is applied afterwards. """ domain = window['domain'] by_domain = user['interactions'] ordered = sorted(by_domain[domain], key=lambda x: x.get('timestamp', 0)) targets = [ordered[i] for i in window['test_indices']] if window['axis'] == 'time': memory = ordered[:window['memory_prefix_length']] else: memory = [] for source in window['source_domains']: rows = by_domain.get(source, []) if source == domain: rows = [x for x in rows if x not in targets] rows = sorted((x for x in rows if int(x.get('timestamp', 0)) < window['memory_before_timestamp']), key=lambda x: x.get('timestamp', 0)) memory.extend(dict(x, domain_category=source) for x in rows) return memory, targets def restore_ranking_input(users, candidates, source_file): """Rebuild a saved ranking input from the named source's candidate rows. Returns only users represented in that candidate source, in original order. Preserves other fields and replaces negatives only at specified positions. """ import copy selected = [r for r in candidates if r['source_file'] == source_file and r['task'] == 'item_ranking'] wanted = {r['user_id'] for r in selected} result = [copy.deepcopy(u) for u in users if u['user_id'] in wanted] lookup = {u['user_id']:u for u in result} for r in selected: ordered = sorted(lookup[r['user_id']]['interactions'][r['domain']], key=lambda x:x.get('timestamp',0)) target = ordered[r['interaction_index']] assert target['parent_asin'] == r['pos_asin'] target['negative_parent_asins'] = r['neg_asins'] return result