74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
"""Phase 4AH: read-only accounting of sealed Phase 4AG trajectories."""
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
|
|
ROOT = Path('/data/digital-drosophila')
|
|
MODEL = Path(__file__).resolve().parent
|
|
EXP = ROOT / 'experiments/phase4ah-kc-drive-audit'
|
|
AG_MODEL = ROOT / 'models/male-cns-lif/author-apl-lab-v2'
|
|
AG = ROOT / 'experiments/phase4ag-author-apl-r2'
|
|
META = ROOT / 'datasets/male-cns-v1.0/processed/neuron-only/neuron-index/unified-metadata.parquet'
|
|
COEFF = ROOT / 'experiments/phase4af-author-fullgraph/oracle/no_input/coefficients.json'
|
|
|
|
|
|
def digest(path):
|
|
h = hashlib.sha256()
|
|
with Path(path).open('rb') as f:
|
|
for b in iter(lambda: f.read(8 << 20), b''):
|
|
h.update(b)
|
|
return h.hexdigest()
|
|
|
|
|
|
def save(path, value):
|
|
path = Path(path)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
with path.open('x', encoding='utf-8') as f:
|
|
json.dump(value, f, indent=2, ensure_ascii=False, allow_nan=False)
|
|
f.write('\n')
|
|
|
|
|
|
def sources():
|
|
return {p.name: digest(p) for p in sorted(MODEL.iterdir()) if p.is_file()
|
|
and p.name not in ('manifest.json', 'checksums.sha256')}
|
|
|
|
|
|
def read(path):
|
|
return json.loads(Path(path).read_text(encoding='utf-8'))
|
|
|
|
|
|
def integrity():
|
|
spec = importlib.util.spec_from_file_location('sealed_ag_context', AG_MODEL / 'context.py')
|
|
previous = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(previous)
|
|
result = previous.integrity()
|
|
import sys
|
|
sys.path.insert(0, str(ROOT / 'models/male-cns-lif/terminal-scope-diagnostic-v1'))
|
|
from diagnostic import manifest_check
|
|
for p in (AG_MODEL, AG):
|
|
result[str(p.relative_to(ROOT))] = manifest_check(p, digest)
|
|
assert all(r['pass'] for r in result.values())
|
|
return result
|
|
|
|
|
|
def check():
|
|
registered = read(EXP / 'protocol/preregistered.json')
|
|
assert registered['sources'] == sources(), 'Registered audit source changed'
|
|
for row in registered['inputs']:
|
|
assert digest(ROOT / row['path']) == row['sha256'], row['path']
|
|
if (EXP / 'protocol/prepared-hashes.json').exists():
|
|
for row in read(EXP / 'protocol/prepared-hashes.json'):
|
|
assert digest(EXP / row['path']) == row['sha256'], row['path']
|
|
return registered
|
|
|
|
|
|
def seal(root):
|
|
rows = [dict(path=str(p.relative_to(root)), bytes=p.stat().st_size, sha256=digest(p))
|
|
for p in sorted(root.rglob('*')) if p.is_file() and '__pycache__' not in p.parts]
|
|
save(root / 'manifest.json', dict(root=str(root), files=rows))
|
|
with (root / 'checksums.sha256').open('x') as f:
|
|
for row in rows:
|
|
f.write(row['sha256'] + ' ' + row['path'] + '\n')
|
|
f.write(digest(root / 'manifest.json') + ' manifest.json\n')
|