"""E2c (extension): does the real fly wiring classify better than random wiring? python experiments/e2c_connectome.py data/demo_calibration --group date python experiments/e2c_connectome.py data/demo_banana --group batch Three mushroom bodies, everything else identical: 隨機接線 textbook model, 5 different random seeds 連結體接線 data/connectome/kc_wiring.npy (run connectome_wiring.py first) 打亂連結體 same synapse counts per KC, sent to random glomeruli (5 seeds) If 連結體 beats 打亂連結體, the specific fly wiring matters, not just its statistics. """ from __future__ import annotations import argparse import sys from pathlib import Path import numpy as np import pandas as pd from sklearn.model_selection import LeaveOneGroupOut, cross_val_score sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from enose.common import load_table, output_dir # noqa: E402 from enose.connectome import shuffled_wiring # noqa: E402 from enose.mushroom import MushroomBodyClassifier # noqa: E402 WIRING = Path(__file__).resolve().parents[1] / "data" / "connectome" / "kc_wiring.npy" SEEDS: tuple[int, ...] = (0, 1, 2, 3, 4) def main() -> None: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("folder", type=Path) parser.add_argument("--group", default="date") arguments = parser.parse_args() table, features, labels, _ = load_table(arguments.folder / "features.csv") if "rh_level" in table: keep = (table["rh_level"] == "mid").to_numpy() table, features, labels = table[keep], features[keep], labels[keep] groups = table[arguments.group].to_numpy() wiring = np.load(WIRING) def score(model: MushroomBodyClassifier) -> float: return cross_val_score(model, features, labels, groups=groups, cv=LeaveOneGroupOut()).mean() rows = [{"接線": "連結體接線", "seed": "-", "準確率": score(MushroomBodyClassifier(connectivity=wiring))}] for seed in SEEDS: rows.append({"接線": "隨機接線", "seed": seed, "準確率": score(MushroomBodyClassifier(kenyon_cells=wiring.shape[0], random_state=seed))}) rows.append({"接線": "打亂連結體", "seed": seed, "準確率": score(MushroomBodyClassifier(connectivity=shuffled_wiring(wiring, seed)))}) result = pd.DataFrame(rows) result.to_csv(output_dir(arguments.folder / "results") / "e2c_connectome.csv", index=False) print(result.groupby("接線")["準確率"].agg(["mean", "std", "count"]).round(3).to_string()) print("\n注意:模擬資料的差異不代表真實結果;隨機與打亂組的標準差代表「換一種接線」本身造成的差異。") if __name__ == "__main__": main()