# LTP from field recordings, knockout vs wild-type: fEPSP slope as percent of each slice's baseline, the mean of minutes
# 51-60 after HFS, with slices or mice as n, and a t-test at every post-HFS minute. Python 3.13, NumPy, pandas, SciPy.
import numpy as np, pandas as pd
from scipy import stats
d = pd.read_csv("fepsp.csv")                         # one row per slice and minute: genotype, mouse, slice, minute (-19 to 60), slope
d["sl"] = d.genotype + "-" + d.mouse.astype(str) + "-" + d.slice.astype(str)
base = d[d.minute <= 0].groupby("sl").slope.mean()
d["pct"] = 100 * d.slope / d.sl.map(base)            # percent of the slice's own 20-min baseline
s = d[(d.minute >= 51) & (d.minute <= 60)].groupby(["genotype", "mouse", "sl"]).pct.mean().reset_index()   # LTP per slice
m = s.groupby(["genotype", "mouse"]).pct.mean().reset_index()                                               # LTP per mouse
for g, x in m.groupby("genotype"):
    print(f"{g}: {len(x)} mice, {int((s.genotype == g).sum())} slices; LTP (minutes 51-60) {x.pct.mean():.0f}% of baseline (mouse range {x.pct.min():.0f} to {x.pct.max():.0f}%)")
k, w = s.pct[s.genotype == "knockout"], s.pct[s.genotype == "wild-type"]; t = stats.ttest_ind(k, w, equal_var=False)
print(f"A. Slices as n ({len(k)} vs {len(w)}), Welch t-test: difference {k.mean() - w.mean():.1f} points, p = {t.pvalue:.2g}")
post = d[d.minute > 0].groupby(["genotype", "sl", "minute"]).pct.mean().unstack()
p = np.array([stats.ttest_ind(post.loc["knockout", c], post.loc["wild-type", c], equal_var=False).pvalue for c in post.columns])
hits = int((p < 0.05).sum())
print(f"B. A Welch t-test at each of {len(p)} post-HFS minutes, slices as n: {hits} with p < 0.05; smallest p = {p.min():.2g}")
k, w = m.pct[m.genotype == "knockout"], m.pct[m.genotype == "wild-type"]; t = stats.ttest_ind(k, w, equal_var=False); ci = t.confidence_interval()
print(f"C. Mice as n ({len(k)} vs {len(w)}), mean of each mouse's slices, Welch t-test: difference {k.mean() - w.mean():.1f} points (95% CI {ci.low:.1f} to {ci.high:.1f}), p = {t.pvalue:.2g}")
