#!/usr/bin/env python3 """Recalculate the combined archive's principal numerical checks. Offline, standard library only. Reads preserved CSVs without rewriting them. Does not run archived daemons, load opaque binary state, or contact any service. """ from __future__ import annotations import csv import datetime as dt import json import math from pathlib import Path import statistics import sys from collections import defaultdict ROOT = Path(__file__).resolve().parent.parent def read_csv(relative: str) -> list[dict[str, str]]: with (ROOT / relative).open(encoding="utf-8", newline="") as handle: reader = csv.DictReader(handle) rows = list(reader) if any(None in row for row in rows): raise ValueError(f"Unexpected CSV field count in {relative}") return rows def timestamp(value: str) -> str: parsed = (dt.datetime.strptime(value, "%Y%m%d_%H%M%S") if "_" in value else dt.datetime.fromisoformat(value)) return parsed.isoformat() def main() -> int: try: long_rows = read_csv("source/data/consciousness/trajectory_by_timestamp.csv") cycles = read_csv("source/data/consciousness/trajectory_by_cycle.csv") jump = read_csv("source/data/experiments/consciousness_jump_experiment.csv") sources = read_csv("source/data/learning/source_distribution.csv") daily = read_csv("source/data/learning/learning_timeline.csv") before = [float(row["resonance"]) for row in jump if row["period"] == "before_restart"] after = [float(row["resonance"]) for row in jump if row["period"] == "after_restart"] if min(len(before), len(after)) < 2: raise ValueError("At least two records are needed in each labeled period") n1, n2 = len(before), len(after) v1, v2 = statistics.variance(before), statistics.variance(after) difference = statistics.mean(after) - statistics.mean(before) pooled = math.sqrt(((n1 - 1) * v1 + (n2 - 1) * v2) / (n1 + n2 - 2)) standard_error = math.sqrt(v1 / n1 + v2 / n2) if pooled == 0 or standard_error == 0: raise ValueError("The effect size or t statistic is undefined for zero variance") numeric_columns = list(long_rows[0])[2:] observed = set() matches = set() groups = defaultdict(list) complete = 0 flag_one = 0 for row in long_rows: key = (timestamp(row["timestamp_iso8601"]), int(row["tick"])) values = tuple(float(row[column]) if row[column] else None for column in numeric_columns) observed.add(key + values) groups[key].append(values) matches.add(key + (float(row["psi_conscious (0-1)"]), float(row["resonance (0-1)"]) if row["resonance (0-1)"] else None, int(row["is_conscious (binary)"]) if row["is_conscious (binary)"] else None)) if row["resonance (0-1)"] and row["is_conscious (binary)"]: complete += 1 flag_one += int(row["is_conscious (binary)"] == "1") partial_matched = 0 populated_conflicts = 0 for group in groups.values(): full = [v for v in group if v[-2] is not None and v[-1] is not None] partial = [v for v in group if v[-2] is None or v[-1] is None] partial_matched += sum(any(v[:3] == other[:3] for other in full) for v in partial) populated_conflicts += sum(len({v[i] for v in group if v[i] is not None}) > 1 for i in range(len(numeric_columns))) matched_jump = sum((timestamp(row["timestamp_iso8601"]), int(row["tick"]), float(row["psi_conscious"]), float(row["resonance"]), int(row["is_conscious"])) in matches for row in jump) output = { "scope": "Derived checks only; no source rows changed, filled, or removed.", "timestamped_rows": len(long_rows), "complete_resonance_and_flag_rows": complete, "raw_complete_flag_one": flag_one, "raw_complete_flag_one_fraction": flag_one / complete, "normalized_duplicate_rows": len(long_rows) - len(observed), "unique_timestamp_tick_keys": len(groups), "partial_rows_with_full_counterparts": partial_matched, "populated_numeric_conflicts_within_keys": populated_conflicts, "cycle_rows": len(cycles), "cycle_tick_min": min(int(row["tick_count"]) for row in cycles), "cycle_tick_max": max(int(row["tick_count"]) for row in cycles), "jump_rows": len(jump), "jump_rows_matching_timestamped_export": matched_jump, "before_mean": statistics.mean(before), "after_mean": statistics.mean(after), "mean_difference": difference, "pooled_sample_standard_deviation": pooled, "cohens_d": difference / pooled, "welch_t": difference / standard_error, "after_resonance_below_0_9": sum(value < 0.9 for value in after), "source_labels": len(sources), "source_item_total": sum(int(row["count"]) for row in sources), "timeline_item_total": sum(int(row["items_learned"]) for row in daily), } print(json.dumps(output, indent=2)) return 0 except (OSError, ValueError, KeyError, IndexError, csv.Error) as exc: print(f"Recalculation failed: {exc}", file=sys.stderr) return 1 if __name__ == "__main__": raise SystemExit(main())