Files
digital-drosophila/models/male-cns-lif/kc-drive-audit-v1/finalize.py
T
2026-09-30 09:59:18 +08:00

155 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Aggregate and seal descriptive accounting without learning promotion."""
import numpy as np
import pandas as pd
from context import ROOT, MODEL, EXP, AG, AG_MODEL, read, save, digest, check, integrity, seal
from accounting import bit_equal
def summarize():
inventory = read(EXP / 'protocol/inventory.json')
expected_inventory = [(name, 0, 'fixture') for name in read(AG_MODEL / 'config.json')['fixtures']]
for row in read(AG / 'protocol/calibration-cases.json'):
expected_inventory.append((row['name'], 0, 'calibration'))
if row['seed'] == 51101:
expected_inventory.append((row['name'], 1, 'calibration'))
identities = [(r['case'], r['repeat'], r['kind']) for r in inventory]
assert len(identities) == len(set(identities)) == 80 and identities == expected_inventory
reports = []
modes = {}
for item in inventory:
folder = EXP / 'trials' / (item['case'] + '-r' + str(item['repeat']))
result = read(folder / 'summary.json')
assert result['pass_all'] and all(result['exact'].values())
assert result['ledger_roundoff_bound_pass'] and result['steps'] > 0
for name, sha in result['input_sha256'].items():
assert digest(ROOT / item['source'] / name) == sha
# Re-read saved numeric evidence; a successful receipt alone is not proof.
spec = read(AG / 'cases' / item['case'] / 'spec.json')
assert (result['case'], result['repeat'], result['kind']) == (item['case'], item['repeat'], item['kind'])
assert result['steps'] == spec['steps'] and result['duration_ms'] == spec['duration_ms']
assert result['mode'] == spec.get('mode') and result['seed'] == spec.get('seed')
assert result['odor_id'] == spec.get('odor_id')
nodes = np.load(spec['graph_dir'] + '/nodes.npy', mmap_mode='r')
kc = np.load(AG / 'apl-bundle/kc_globals.npy', mmap_mode='r')
targets, positions, _ = np.intersect1d(nodes, kc, assume_unique=True, return_indices=True)
table = pd.read_parquet(folder / 'per-kc.parquet')
with np.load(folder / 'replay-final.npz') as actual, np.load(ROOT / item['source'] / 'state.npz') as expected:
assert bit_equal(actual['target_globals'], targets)
for key in ('v', 'g', 'ref', 'counts'):
assert bit_equal(actual[key], expected[key][positions]), key
event_mask = np.isin(expected['neuron_idx'], targets)
for key in ('step', 'neuron_idx'):
assert bit_equal(actual[key], expected[key][event_mask]), key
assert bit_equal(table.neuron_idx.to_numpy(), targets)
assert bit_equal(table.spike_count.to_numpy(), actual['counts'])
assert bit_equal(table.first_spike_tick.to_numpy(), actual['first_spike'])
assert np.isfinite(table.select_dtypes(include='number').to_numpy()).all()
assert (table.g_ledger_residual_V.abs() <= table.g_ledger_bound_V).all()
reports.append(result)
if item['kind'] != 'calibration' or item['repeat'] != 0:
continue
modes.setdefault(result['mode'], []).append(result)
aggregate = {}
for mode, rows in modes.items():
assert len(rows) == 30
group_totals = {}
for group in rows[0]['strata']['all']['source_groups']:
group_totals[group] = {metric: sum(r['strata']['all']['source_groups'][group][metric] for r in rows)
for metric in rows[0]['strata']['all']['source_groups'][group]}
active = np.array([r['KC_active'] for r in rows], dtype=np.int64)
strata = {}
for stratum in ('all', 'ever_active', 'never_active'):
strata[stratum] = {metric: sum(r['strata'][stratum]['metrics_sum'][metric] for r in rows)
for metric in rows[0]['strata'][stratum]['metrics_sum']}
strata[stratum].update(neuron_trial_count=sum(r['strata'][stratum]['neurons'] for r in rows),
no_accepted_exc_neuron_trials=sum(r['strata'][stratum]['no_accepted_exc_neurons'] for r in rows),
no_accepted_ALPN_exc_neuron_trials=sum(r['strata'][stratum]['no_accepted_ALPN_exc_neurons'] for r in rows),
has_exc_but_never_active_neuron_trials=sum(r['strata'][stratum]['has_exc_but_never_active'] for r in rows))
aggregate[mode] = dict(trials=30, KC_active_mean=float(active.mean()),
KC_active_min=int(active.min()), KC_active_max=int(active.max()),
KC_active_fraction_mean=float(active.mean() / 4064), source_groups=group_totals,
strata=strata,
input_totals={key: sum(r['input'][key] for r in rows) for key in
('requests', 'accepted', 'rejected', 'design_target_PN_spikes', 'non_design_ALPN_spikes')},
min_max_design_targets=[min(r['input']['odor_design_target_count'] for r in rows),
max(r['input']['odor_design_target_count'] for r in rows)],
neuron_trial_not_unique_neuron_counts=True)
flat = []
for row in reports:
if row['kind'] == 'calibration' and row['repeat'] == 0:
flat.append(dict(case=row['case'], mode=row['mode'], seed=row['seed'], odor_id=row['odor_id'],
KC_active=row['KC_active'], KC_spikes=row['KC_spikes'],
requests=row['input']['requests'], accepted_requests=row['input']['accepted'],
ALPN_actual_spikes=row['input']['actual_spikes_by_source_group']['ALPN'],
no_exc_neurons=row['strata']['all']['no_accepted_exc_neurons'],
no_ALPN_exc_neurons=row['strata']['all']['no_accepted_ALPN_exc_neurons'],
APL_sum_SI=row['strata']['all']['metrics_sum']['APL_write_SI'],
APL_ref_sum_SI=row['strata']['all']['metrics_sum']['APL_write_ref_SI'],
maximum_ledger_error_V=row['ledger_max_abs_error_V'],
source_shadow_max_residual_V=row['source_shadow_max_residual_V']))
assert len(reports) == 80 and len(flat) == 60 and len(aggregate) == 2
return reports, aggregate, flat
def main():
check()
assert not (EXP / 'audit/failure.json').exists(), 'Preserved failure cannot be promoted'
completed = read(EXP / 'audit/completed.json')
assert completed['pass_all'] and len(completed['trials']) == 80 and len(completed['repeat_checks']) == 12
inventory = read(EXP / 'protocol/inventory.json')
expected_ids = {(r['case'], r['repeat']) for r in inventory}
actual_ids = [(r['case'], r['repeat']) for r in completed['trials']]
assert len(actual_ids) == len(set(actual_ids)) == 80 and set(actual_ids) == expected_ids
assert all(r['pass_all'] for r in completed['trials'])
repeat_ids = [r['case'] for r in completed['repeat_checks']]
assert len(repeat_ids) == len(set(repeat_ids)) == 12
assert set(repeat_ids) == {r['case'] for r in inventory if r['repeat'] == 1}
assert all(r['pass_all'] and all(r['checks'].values()) for r in completed['repeat_checks'])
reports, aggregate, flat = summarize()
pd.DataFrame(flat).to_csv(EXP / 'audit/calibration-drive-summary.csv', index=False)
after = integrity()
save(EXP / 'regression/old-integrity-after.json', after)
prior_five = read(AG / 'regression/frozen-five.json')
assert prior_five['pass_all']
result = dict(status='PASS-CONDITIONAL-KC-DRIVE-AUDIT', reconstructed_trials=80,
fixture_full_target_history_trials=8, CAL_final_and_event_trials=72, exact_accounting_repeats=12,
modes=aggregate, max_ledger_residual_V=max(r['ledger_max_abs_error_V'] for r in reports),
max_source_shadow_residual_V=max(r['source_shadow_max_residual_V'] for r in reports),
max_source_area_residual_V_ms=max(r['source_area_max_residual_V_ms'] for r in reports),
CPU_accounting_wall_sum_seconds=sum(r['CPU_audit_wall_seconds'] for r in reports),
old_integrity_pass=all(r['pass'] for r in after.values()),
frozen_five_reexecuted_this_audit=False, previous_sealed_five_reference_pass=True,
previous_five_reference_sha256=digest(AG / 'regression/frozen-five.json'),
previous_CAL_failure_not_changed=True, new_brain_simulation=False, GPU_used=False,
training=False, heldout_access=False, candidate_generated=False, learning_achieved=False,
APL_causal_failure_claim=False, counterfactual_tested=False,
limitations=['Logged upstream and APL are conditioning inputs, not independent components',
'CAL has no independently saved per-tick KC state histories; only events and final states checked',
'Source state lanes are non-feedback linear bookkeeping under observed reset/availability',
'First-spike APL/ref mechanisms cannot establish whole-network counterfactual causality',
'Counts pooled across trials are neuron-trial counts, not distinct neuron identities'],
payload_bytes_before_manifest=sum(p.stat().st_size for p in EXP.rglob('*') if p.is_file()))
assert result['payload_bytes_before_manifest'] < 2 * 1024 ** 3
save(EXP / 'audit/final-status.json', result)
lines = ['# Phase 4AH — conditional KC drive audit', '', 'Status: `'+result['status']+'`.', '',
'80 existing records reconstructed; 8 fixture pre/post histories and 72 CAL event/final states exact.',
'12 repeated CAL records produced identical accounting. No new brain simulation, GPU use, tuning or learning.',
'Full prior frozen integrity passed. Five reference simulations were not rerun; the sealed prior PASS is referenced by SHA.',
'Previous CAL failure remains unchanged. Conditional replay is not an APL-removal counterfactual.', '',
'|Mode|Mean active KC fraction|Requests|Accepted requests|ALPN accepted excitatory increment sum (V)|APL write sum (V)|',
'|---|---:|---:|---:|---:|---:|']
for mode, row in aggregate.items():
lines.append(f"|{mode}|{row['KC_active_fraction_mean']:.8f}|{row['input_totals']['requests']}|{row['input_totals']['accepted']}|{row['source_groups']['ALPN']['accepted_exc_SI']:.9g}|{row['strata']['all']['APL_write_SI']:.9g}|")
lines += ['', 'Increment sums span 30 trials × 4064 KC × time; not instantaneous conductance or causal percentages.',
'See audit/final-status.json, audit/calibration-drive-summary.csv and trials/*/per-kc.parquet for detailed evidence.']
with (EXP / 'REPORT.md').open('x') as f:
f.write('\n'.join(lines) + '\n')
check()
seal(MODEL)
seal(EXP)
print(result['status'], flush=True)
if __name__ == '__main__':
main()