shadowcollecter's picture
Add files using upload-large-folder tool
904ca52 verified
#!/usr/bin/env python3
"""Shift all vertex IDs by -1 in-place for the avazu hypergraph to match
MaxEmbed's `num < c` invariant (max must be < num_vertices).
Applied to both avazu.bin (binary) and avazu_hypergraph.txt (text)."""
import numpy as np
import os
import time
import struct
txt_path = '/home/sadoo/research_data/processed/avazu/maxembed_paper_scale/avazu_hypergraph.txt'
bin_path = '/home/sadoo/research_data/processed/avazu/maxembed_paper_scale/avazu.bin'
# --- Binary: shift pins by 1 ---
t0 = time.time()
print(f'Shifting binary {bin_path}')
with open(bin_path, 'rb') as f:
query_cnt = struct.unpack('<Q', f.read(8))[0]
node_cnt = struct.unpack('<Q', f.read(8))[0]
print(f' query_cnt={query_cnt:,} node_cnt={node_cnt:,}')
offsets = np.frombuffer(f.read((query_cnt+1) * 8), dtype=np.uint64).copy()
pins = np.fromfile(f, dtype=np.uint32)
print(f' loaded pins: {len(pins):,} max={pins.max()} min={pins.min()} (elapsed {time.time()-t0:.0f}s)')
pins -= 1
print(f' after shift: max={pins.max()} min={pins.min()}')
tmp_path = bin_path + '.tmp'
with open(tmp_path, 'wb') as f:
f.write(np.uint64(query_cnt).tobytes())
f.write(np.uint64(node_cnt).tobytes())
f.write(offsets.tobytes())
pins.tofile(f)
os.rename(tmp_path, bin_path)
print(f' binary rewritten in {time.time()-t0:.0f}s, size={os.path.getsize(bin_path):,}')
# --- Text: rewrite with IDs shifted by 1 using numpy-backed CSR reconstruction ---
# We already have offsets + pins in memory (shifted), just re-emit text from them.
t0 = time.time()
print(f'Shifting text {txt_path}')
tmp_path = txt_path + '.tmp'
# Header: keep same (num_vertices was already correct = 9,449,205)
# but now vertex IDs are 0-indexed, so max=9449204 < num_vertices=9449205.
with open(tmp_path, 'w') as fout:
fout.write(f'0 {int(node_cnt)} {int(query_cnt)} {len(pins)}\n')
CHUNK = 262144
# Use buffered writes via join of per-row strings
cursor = 0
for start in range(0, int(query_cnt), CHUNK):
end = min(start + CHUNK, int(query_cnt))
# per-row slicing
buf = []
for i in range(start, end):
lo = int(offsets[i])
hi = int(offsets[i+1])
row = pins[lo:hi]
buf.append(' '.join(map(str, row.tolist())))
fout.write('\n'.join(buf))
fout.write('\n')
if start % (CHUNK * 40) == 0:
print(f' rows written {end:,}/{int(query_cnt):,} elapsed={time.time()-t0:.0f}s')
os.rename(tmp_path, txt_path)
print(f' text rewritten in {time.time()-t0:.0f}s size={os.path.getsize(txt_path):,}')
print('Done')