lorapop / code /work /edit_refid.py
bryan7264's picture
Add files using upload-large-folder tool
a4054b7 verified
Raw History Blame Contribute Delete
2.03 kB
# MIT License, Bryan Cheng, 2026. D-039: content-addressed reference-unit ids (a plan change can never reuse a reference
# file whose combos differ).
p = "src/run/intervene.py"
s = open(p).read()
s = s.replace(''' units = [{"id": f"__ref__{hw + '__' if hw else ''}{i // ref_size:03d}", "kind": "ref", "module": "", "combos": zero[i:i + ref_size]}
for i in range(0, len(zero), ref_size)]''', ''' units = [{"id": f"__ref__{hw + '__' if hw else ''}{i // ref_size:03d}_{combo_hash(zero[i:i + ref_size])}", "kind": "ref",
"module": "", "combos": zero[i:i + ref_size]} for i in range(0, len(zero), ref_size)]''')
s = s.replace('''def make_units(plan, shared_zero, ref_size=48, hw=""):''', '''def combo_hash(combos):
"""8-hex content hash of a reference unit's (background, dose) list (D-039)."""
return hashlib.sha1(json.dumps([[b, float(c)] for b, c in combos]).encode()).hexdigest()[:8]
def make_units(plan, shared_zero, ref_size=48, hw=""):''')
s = s.replace("import argparse\nimport json\n", "import argparse\nimport hashlib\nimport json\n")
assert "combo_hash(zero[i:i + ref_size])" in s and "import hashlib" in s
open(p, "w").write(s)
p = "tests/test_intervene_units.py"
s = open(p).read()
s = s.replace(''' assert [u["id"] for u in refs] == ["__ref__000", "__ref__001"]''', ''' assert [u["id"][:10] for u in refs] == ["__ref__000", "__ref__001"]
assert all(len(u["id"]) == 19 for u in refs) # __ref__NNN_<8 hex>''')
s = s.replace(''' assert [u["id"] for u in U if u["kind"] == "ref"] == ["__ref__H200_b128g256__000", "__ref__H200_b128g256__001"]''',
''' assert [u["id"][:-9] for u in U if u["kind"] == "ref"] == ["__ref__H200_b128g256__000", "__ref__H200_b128g256__001"]
# a different plan (other backgrounds) gives different ids for the same position
other = {**PLAN, "backgrounds": ["a", "b", "z"]}
assert make_units(other, True, ref_size=3, hw="H200_b128g256")[0]["id"] != U[0]["id"]''')
open(p, "w").write(s)
print("refid ok")