Files
digital-drosophila/tools/phase4aj-publication/describe_phase4aj.py
T
2026-09-30 12:08:25 +08:00

256 lines
16 KiB
Python

"""Post-seal descriptive extraction; not another scientific gate or model run.
The four original CAL summary dictionaries are copied, not recalculated.
Origin totals use only the earliest-cohort/pre-first-KC audit window from
the 60 sealed AG-r2 control trials. No AI population/window share is imported.
"""
from __future__ import annotations
from collections import Counter
import hashlib
import importlib.util
import json
import math
from pathlib import Path
ROOT = Path('/data/digital-drosophila')
MODEL_RELATIVE = Path('models/male-cns-lif/apl-timing-lab-v1')
EXPERIMENT_RELATIVE = Path('experiments/phase4aj-apl-timing')
REGISTRY_RELATIVE = Path('registry/phase4aj-apl-timing')
HELPER_RELATIVE = Path('models/male-cns-lif/pn-recruitment-audit-v1/publication.py')
HELPER_SHA256 = '7a79c499098b74163e7bb16adabcbc54e7069dafa204d0b99c7f4c03d79f8016'
LEGACY_CALIBRATION_SHA256 = 'edb9cb2d890471fd07afd88c959ef3540b0cd40af490fac3f26396b5e7f5bf9c'
MODES = ('physiological', 'matched_energy')
ARMS = ('delay0', 'delay18')
CAL_MODE_FIELDS = frozenset(('KC_active_fraction_mean', 'KC_active_fraction_min', 'KC_active_fraction_max',
'same_odor_jaccard_mean', 'different_odor_jaccard_mean', 'same_odor_euclidean_mean',
'different_odor_euclidean_mean', 'different_odor_cosine_mean', 'different_odor_correlation_mean',
'within_variance', 'between_distance_mean', 'pairwise_separation_pass_count', 'pair_count',
'expected_event_count_cv', 'actual_event_count_cv', 'within_distance_count', 'between_distance_count',
'pairwise_baseline', 'pairs', 'conditions', 'gate'))
WINDOW = 'cohort_pre_KC_and_pre_first'
GROUP_FIELDS = ('events', 'positive_events', 'negative_events', 'positive_weight_SI', 'negative_abs_weight_SI')
OUTGOING_FIELDS = ('cohort_ALLN_outgoing_events', 'outgoing_events_with_since_reset_design_antecedent',
'outgoing_events_without_since_reset_design_antecedent', 'compact_join_pair_count')
RELATIONS = frozenset(('no_non_design_spike', 'KC_unobserved', 'before', 'same_tick', 'after'))
MAX_BYTES = 4 * 1024 ** 2
def require(ok, message):
if not ok:
raise ValueError(message)
def load_helpers(path):
path = Path(path)
require(path.is_absolute() and path.is_file() and not path.is_symlink(), 'Invalid frozen verification helper')
source = path.read_bytes()
require(hashlib.sha256(source).hexdigest() == HELPER_SHA256, 'Frozen AI helper hash mismatch')
spec = importlib.util.spec_from_file_location('phase4aj_describe_frozen_ai_helpers', path)
require(spec is not None and spec.loader is not None, 'Cannot define frozen helper module')
module = importlib.util.module_from_spec(spec)
# Exact pinned bytes; no pyc read/write in the old sealed model directory.
exec(compile(source, str(path), 'exec'), module.__dict__)
return module
def _boolean_fields(value):
return {key: item for key, item in value.items() if type(item) is bool}
def _nonnegative_integer(value, field):
require(type(value) is int and value >= 0, 'Invalid nonnegative count: ' + field)
return value
def aggregate_origin(trials):
"""Sum stored audit totals, retaining source labels and the fixed window.
These are descriptive sums of already aggregated nominal weights. They
have no arithmetic-error-bound, membrane-effect, or causal interpretation.
"""
groups = {}
relations = Counter()
outgoing = {field: 0 for field in OUTGOING_FIELDS}
before_trials = before_neurons = censored = cohort_occurrences = 0
per_trial = []
for trial in trials:
source = trial['window_source_accounting'][WINDOW]
require(isinstance(source, dict), 'Origin source groups must be a dictionary')
for label, values in source.items():
require(isinstance(label, str) and set(values) == set(GROUP_FIELDS), 'Unexpected origin source-group schema')
target = groups.setdefault(label, {key: [] for key in GROUP_FIELDS})
for key in GROUP_FIELDS[:3]:
target[key].append(_nonnegative_integer(values[key], key))
require(values['positive_events'] + values['negative_events'] <= values['events'], 'Origin sign count exceeds events')
for key in GROUP_FIELDS[3:]:
require(type(values[key]) in (int, float) and math.isfinite(values[key]) and values[key] >= 0,
'Invalid nominal source weight: ' + key)
target[key].append(float(values[key]))
relation = trial['cohort_relation_to_first_KC']
require(relation in RELATIONS, 'Unknown earliest-cohort/first-KC relation')
require(type(trial['cohort_before_first_KC']) is bool and
trial['cohort_before_first_KC'] == (relation == 'before'), 'Inconsistent saved first-KC relation')
require(type(trial['pre_kc_window_censored']) is bool, 'Invalid saved censoring flag')
count = _nonnegative_integer(trial['cohort_neurons'], 'cohort_neurons')
relations[relation] += 1
cohort_occurrences += count
before_trials += int(trial['cohort_before_first_KC'])
before_neurons += count if trial['cohort_before_first_KC'] else 0
censored += int(trial['pre_kc_window_censored'])
for key in OUTGOING_FIELDS:
outgoing[key] += _nonnegative_integer(trial[key], key)
require(trial[OUTGOING_FIELDS[1]] + trial[OUTGOING_FIELDS[2]] == trial[OUTGOING_FIELDS[0]],
'Saved ALLN outgoing partition is inconsistent')
per_trial.append({key: trial[key] for key in ('case', 'mode', 'odor_id', 'seed', 'pass_all',
'first_any_KC_tick', 'earliest_tick', 'cohort_neurons', 'cohort_before_first_KC',
'cohort_relation_to_first_KC', 'pre_kc_window_censored', *OUTGOING_FIELDS)} |
{'source_groups': source, 'exact': trial['exact']})
totals = {label: {key: sum(parts[key]) if key in GROUP_FIELDS[:3] else math.fsum(parts[key])
for key in GROUP_FIELDS} for label, parts in sorted(groups.items())}
direct_pn = {key: sum(totals.get(label, {}).get(key, 0) for label in ('design_ALPN', 'non_design_ALPN'))
if key in GROUP_FIELDS[:3] else math.fsum(totals.get(label, {}).get(key, 0.0)
for label in ('design_ALPN', 'non_design_ALPN')) for key in GROUP_FIELDS}
return dict(trial_count=len(trials), source_window=WINDOW, source_groups=totals,
direct_ALPN_combined=direct_pn, direct_ALPN_labels=['design_ALPN', 'non_design_ALPN'],
first_before_KC_trial_count=before_trials, cohort_neuron_occurrences_before_first_KC=before_neurons,
cohort_neuron_occurrences_across_trials=cohort_occurrences,
first_KC_window_censored_trial_count=censored, cohort_relation_trial_counts=dict(sorted(relations.items())),
**outgoing, per_trial=per_trial,
absent_group_semantics='Producer emits only observed groups; an absent group contributes zero accepted events in this window',
nominal_weight_units='SI volts; accepted edge-weight increments, not membrane effect or causal contribution',
aggregation='Integer counts and math.fsum of saved per-trial nominal float totals; descriptive, no new roundoff-bound claim',
pair_count_semantics='Existing compact temporal join pairs; shared antecedents can recur for multiple outgoing edges, not unique causal chains')
def describe(root=ROOT, helper_path=None):
"""Write two exclusive post-publication files; never alter a seal/receipt."""
root = Path(root)
require(root.is_absolute() and root.is_dir() and not root.is_symlink(), 'Invalid project root')
root = root.resolve(strict=True)
model, experiment, registry = (root / item for item in (MODEL_RELATIVE, EXPERIMENT_RELATIVE, REGISTRY_RELATIVE))
output, checksum = registry / 'interpretation-metrics.json', registry / 'interpretation-metrics.sha256'
require(registry.is_dir() and not registry.is_symlink(), 'Publish the sealed evidence registry first')
require(not output.exists() and not output.is_symlink() and not checksum.exists() and not checksum.is_symlink(),
'Interpretation output already exists; no overwrite/resume')
for target in (model, experiment, registry):
path = root
for part in target.relative_to(root).parts:
path /= part
require(not path.is_symlink(), 'Symlink in descriptive source/output path')
helper_path = root / HELPER_RELATIVE if helper_path is None else Path(helper_path)
helper = load_helpers(helper_path)
verified = {'model': helper.verify_seal(model), 'experiment': helper.verify_seal(experiment)}
inventories = {name: {row['path']: row for row in verification['files']} for name, verification in verified.items()}
inputs = {}
def sealed_json(relative):
require(relative in inventories['experiment'], 'Required descriptive input absent from seal: ' + relative)
path = helper._regular_file(experiment, relative)
row = inventories['experiment'][relative]
inputs[relative] = row['sha256']
return helper._json_bytes(path.read_bytes())
status = sealed_json('audit/final-status.json')
calibration = sealed_json('calibration/summary.json')
origin = sealed_json('origin-audit/summary.json')
inventory = sealed_json('protocol/calibration-cases.json')
frozen = sealed_json('regression/frozen-five.json')
integrity = sealed_json('regression/old-integrity-after.json')
require(isinstance(status.get('status'), str) and status['status'].startswith(('PASS-', 'FAIL-', 'INCOMPLETE-')),
'Missing finalized status; publication is not a substitute for finalization')
require(status.get('candidate_generated') is False and status.get('learning_achieved') is False and
status.get('shared_APL_arithmetic_oracle_limitation') is True, 'Unexpected finalized scope')
require(calibration.get('legacy_calibration_sha256') == LEGACY_CALIBRATION_SHA256,
'CAL summary does not identify the sealed original metric implementation')
require(set(calibration['arms']) == set(ARMS), 'Both CAL arm summaries are required')
cal_modes = {}
arm_flags = {}
for arm in ARMS:
value = calibration['arms'][arm]
require(set(value['modes']) == set(MODES), 'Both original mode summaries are required: ' + arm)
# Preserve the complete existing dictionaries, including pairs, CV,
# conditions, gate and the original pairwise baseline description.
cal_modes[arm] = {mode: value['modes'][mode] for mode in MODES}
require(all(isinstance(value['modes'][mode], dict) and CAL_MODE_FIELDS.issubset(value['modes'][mode])
for mode in MODES), 'Incomplete saved original CAL mode summary')
arm_flags[arm] = _boolean_fields(value)
require(len(inventory) == 60 and len({row['name'] for row in inventory}) == 60, 'Require the fixed 60 CAL r0 cases')
names = {row['name']: row for row in inventory}
require(Counter(row['mode'] for row in inventory) == Counter({mode: 30 for mode in MODES}), 'Origin mode coverage changed')
require(len(origin['trials']) == 60 and {row['case'] for row in origin['trials']} == set(names),
'Origin trial inventory must match all 60 fixed control cases')
trials = []
for row in sorted(origin['trials'], key=lambda value: value['case']):
name = row['case']
require(helper._relative_name(name).name == name, 'Origin case must be a single relative name')
relative = 'origin-audit/' + name + '/summary.json'
trial = sealed_json(relative)
require(row['summary_sha256'] == inputs[relative], 'Origin receipt does not bind its sealed trial summary')
require(trial['case'] == name and trial['repeat'] == 0 and all(
trial[key] == names[name][key] for key in ('mode', 'odor_id', 'seed')), 'Origin case identity mismatch')
trials.append(trial)
receipt_path = helper._regular_file(registry, 'publication-receipt.json')
receipt_sha = helper._digest(receipt_path)
receipt = helper._json_bytes(receipt_path.read_bytes())
require(receipt.get('publication_complete') is True and receipt.get('declared_audit_status') == status['status'] and
all(receipt.get(name + '_manifest_sha256') == verified[name]['manifest_sha256']
for name in ('model', 'experiment')), 'Existing publication receipt belongs to different seals/status')
for row in receipt['files']:
path = helper._regular_file(registry, row['path'])
require(path.stat().st_size == row['bytes'] and helper._digest(path) == row['sha256'], 'Previously published evidence changed')
source_hashes = {'describe_phase4aj.py': helper._digest(Path(__file__)),
'frozen_AI_publication.py': HELPER_SHA256,
'sealed_AG_calibration.py': LEGACY_CALIBRATION_SHA256}
for name in ('al_origin_audit.py', 'calibration.py'):
require(name in inventories['model'], 'Registered metric-producing source absent from model seal: ' + name)
source_hashes['AJ_model/' + name] = inventories['model'][name]['sha256']
result = dict(kind='postseal-descriptive-interpretation-metrics', declared_final_status=status['status'],
final_status_boolean_flags=_boolean_fields(status), calibration_boolean_flags=_boolean_fields(calibration),
calibration_arm_boolean_flags=arm_flags, original_CAL_mode_summaries=cal_modes,
finalized_numerical_evidence={key: status[key] for key in ('gates', 'comparisons', 'repeats', 'controls', 'components')},
frozen_reference_report=frozen, frozen_integrity_report=integrity,
origin_audit_declared_status=origin['status'], origin_audit_boolean_flags=_boolean_fields(origin),
origin_scope='60 sealed AG-r2 control CAL r0 trials, 30 per mode; not a new delay18-origin audit',
origin_window=WINDOW, origin_all_modes=aggregate_origin(trials),
origin_by_mode={mode: aggregate_origin([trial for trial in trials if trial['mode'] == mode]) for mode in MODES},
source_sha256=source_hashes, sealed_input_sha256=inputs,
model_manifest_sha256=verified['model']['manifest_sha256'],
experiment_manifest_sha256=verified['experiment']['manifest_sha256'],
model_checksums_sha256=verified['model']['checksums_sha256'],
experiment_checksums_sha256=verified['experiment']['checksums_sha256'],
publication_receipt_sha256_unchanged=receipt_sha,
postseal_addition_outside_original_receipt=True, source_and_receipt_files_modified=False,
original_metric_dicts_copied_without_recalculation=True, new_scientific_gate=False,
scientific_pass_inferred_from_description=False, new_brain_simulation=False, GPU_used=False,
parameters_changed=False, unique_cause_proven=False, necessity_proven=False, learning_achieved=False,
inherited_AI_other_population_or_window_share=False,
limitations=['Nominal accepted weight sums are not voltage attribution, necessity, or unique causation.',
'Origin cohorts include all ties at the earliest non-design ALPN spike, with strict pre-first-target and pre-first-any-KC cutoffs.',
'Counts of timed ALLN outgoing edge events and antecedent pairs are distinct and need not denote unique paths.',
'Post-seal aggregation changes no original scientific gate or PASS/FAIL/INCOMPLETE status.',
'Origin data are sealed control trajectories; no treatment-specific origin conclusion is inferred.'])
after = {'model': helper.verify_seal(model), 'experiment': helper.verify_seal(experiment)}
require(after == verified and helper._digest(helper_path) == HELPER_SHA256, 'A sealed source changed during description')
require(helper._digest(receipt_path) == receipt_sha, 'Existing publication receipt changed during description')
data = (json.dumps(result, indent=2, ensure_ascii=False, allow_nan=False) + '\n').encode('utf-8')
require(len(data) <= MAX_BYTES, 'Descriptive output exceeds the 4 MiB small-evidence limit')
with output.open('xb') as stream:
stream.write(data)
output_sha = hashlib.sha256(data).hexdigest()
with checksum.open('xb') as stream:
stream.write((output_sha + ' interpretation-metrics.json\n').encode('ascii'))
require(helper._digest(output) == output_sha and helper._digest(receipt_path) == receipt_sha,
'Output or original receipt changed while writing')
return dict(description_complete=True, declared_final_status=status['status'], output=str(output),
sha256=output_sha, new_scientific_gate=False, learning_achieved=False)
def main():
print(json.dumps(describe(), ensure_ascii=False, allow_nan=False), flush=True)
if __name__ == '__main__':
main()