# 査読（2026-10-20）への応答の集計: ① N=200・Df 2.8 で次数（lmax 1/2）と波長の刻み（4点/7点）を一つずつ変える
# ② N=1000 の向き（seed 2/3/4）ごとの b* と、その平均
import json, numpy as np
from collections import defaultdict
from tm_color import color, L7
V = lambda N: N * np.pi * 30 ** 3 / 6
def ks(rows, N, lams):
    Ls = np.array(sorted(lams))
    K = np.interp(L7, Ls, [2 * rows[l]["Cabs"] / V(N) for l in Ls])
    S = np.interp(L7, Ls, [0.75 * rows[l]["Csca"] * (1 - rows[l]["g"]) / V(N) for l in Ls])
    return K, S
base = defaultdict(dict); extra = defaultdict(dict)
for l in open("tm_results.jsonl"):
    d = json.loads(l); base[(d["N"], d["Df"], d.get("lmax", 2), 2)][d["lam"]] = d
for l in open("tm_extra.jsonl"):
    d = json.loads(l); extra[(d["N"], d["Df"], d["lmax"], d["seed"])][d["lam"]] = d
r2 = base[(200, 2.8, 2, 2)]; r1 = dict(base[(200, 2.8, 1, 2)]); r1.update(extra[(200, 2.8, 1, 2)])
F4 = (400, 500, 600, 700); F7 = tuple(range(400, 701, 50))
print("N=200 Df2.8          b*      ΔE00")
for name, r, lams in (("lmax2・7点", r2, F7), ("lmax2・4点", r2, F4), ("lmax1・7点", r1, F7), ("lmax1・4点", r1, F4)):
    if all(l in r for l in lams):
        c = color(*ks(r, 200, lams)); print(f"{name:12s}  {c[2]:+6.2f}  {c[3]:5.2f}")
print("\nN=1000（lmax1・4点）  向きごとの b*")
for Df in (2.8, 1.8):
    vals = []
    for seed in (2, 3, 4):
        r = base[(1000, Df, 1, 2)] if seed == 2 else extra[(1000, Df, 1, seed)]
        if all(l in r for l in F4):
            c = color(*ks(r, 1000, F4)); vals.append(c[2]); print(f"Df{Df} seed{seed}: {c[2]:+6.2f}")
    if len(vals) > 1:
        rs = [base[(1000, Df, 1, 2)]] + [extra[(1000, Df, 1, s)] for s in (3, 4) if all(l in extra[(1000, Df, 1, s)] for l in F4)]
        avg = {l: {k: np.mean([x[l][k] for x in rs]) for k in ("Cabs", "Csca", "g")} for l in F4}
        c = color(*ks(avg, 1000, F4))
        print(f"Df{Df} 向き{len(rs)}つの断面積を平均: b* {c[2]:+6.2f}  幅 {min(vals):+.2f}〜{max(vals):+.2f}")
