Download code/05_sensitivity.py from HarriziSaad/BULMA: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/05_sensitivity.py
- Command line
-
hf download hf://datasets/HarriziSaad/BULMA/code/05_sensitivity.py
-
curl -L -o 05_sensitivity.py https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/05_sensitivity.py
3.44 kB
| import numpy as np | |
| import pandas as pd | |
| from scipy import stats | |
| from common import RES, OX, OS, hom, axes, centre, effects | |
| Z = hom() | |
| ag, ox, os_, ref = axes(Z.columns) | |
| a, b = effects(centre(Z), ox, os_) | |
| base = (a - b).abs() | |
| rows = [] | |
| for how in ("mean", "trim"): | |
| a2, b2 = effects(centre(Z, how), ox, os_) | |
| m = base.notna() & (a2 - b2).notna() | |
| rows.append(("centring", how, stats.spearmanr(base[m], (a2 - b2).abs()[m]).statistic, int(m.sum()))) | |
| Zc = centre(Z) | |
| for drop in sorted(OX | OS): | |
| keep = ag != drop | |
| a2, b2 = effects(Zc, ox & keep, os_ & keep) | |
| cd2 = (a2 - b2).abs() | |
| m = base.notna() & cd2.notna() | |
| rows.append(("leave-one-agent-out", drop, stats.spearmanr(base[m], cd2[m]).statistic, int(m.sum()))) | |
| naive_a, naive_b = Z.loc[:, ox].mean(axis=1), Z.loc[:, os_].mean(axis=1) | |
| bline = Z.median(axis=1) | |
| naive_cd = naive_a - naive_b | |
| signed = a - b | |
| m = base.notna() | |
| rows.append(("identity N_ox = E_ox + b", "max |N_ox - (E_ox + b)|", float((naive_a - (a + bline)).abs().max()), int(m.sum()))) | |
| rows.append(("identity N_osm = E_osm + b", "max |N_osm - (E_osm + b)|", float((naive_b - (b + bline)).abs().max()), int(m.sum()))) | |
| rows.append(("slow-growth invariance", "max |naive - deconfounded|", | |
| float((naive_cd[m] - signed[m]).abs().max()), int(m.sum()))) | |
| rows.append(("slow-growth invariance", "pearson r", | |
| stats.pearsonr(naive_cd[m], signed[m]).statistic, int(m.sum()))) | |
| rref = Zc.loc[:, ref].mean(axis=1) | |
| rows.append(("reference-contrast variant", "max |(E_ox - r) - (E_osm - r) - (E_ox - E_osm)|", | |
| float(((a - rref) - (b - rref) - signed)[m].abs().max()), int(m.sum()))) | |
| rows.append(("baseline b vs mean of reference conditions", "spearman", | |
| stats.spearmanr(bline, Z.loc[:, ref].mean(axis=1), nan_policy="omit").statistic, int(m.sum()))) | |
| rows.append(("per-stress effect: baseline vs reference-mean contrast", "spearman (oxidative)", | |
| stats.spearmanr(a, a - rref, nan_policy="omit").statistic, int(m.sum()))) | |
| cond = pd.read_csv(RES / "conditions.csv") | |
| batch = (cond.pool + ", " + cond.scanner).values | |
| X = Z.values | |
| B = np.full_like(X, np.nan) | |
| for bt in np.unique(batch): | |
| cols = np.where((batch == bt) & ref)[0] | |
| B[:, batch == bt] = np.nanmedian(X[:, cols], axis=1)[:, None] | |
| rows.append(("batch-matched baseline", f"non-stress conditions in batch {bt}", len(cols), | |
| int(((batch == bt) & (ox | os_)).sum()))) | |
| ab, bb = effects(pd.DataFrame(X - B, index=Z.index, columns=Z.columns), ox, os_) | |
| for name, x, y in (("spearman E_ox", a, ab), ("spearman E_osm", b, bb), ("spearman D", signed, ab - bb), | |
| ("spearman |D|", base, (ab - bb).abs())): | |
| rows.append(("batch-matched baseline", name, stats.spearmanr(x, y, nan_policy="omit").statistic, int(m.sum()))) | |
| sev = cond.severity_frac_z_gt2.values | |
| keep = ref & (sev < np.quantile(sev[ref], 0.9)) | |
| b_sev = np.nanmedian(X[:, keep], axis=1) | |
| as_, bs_ = Z.loc[:, ox].mean(axis=1) - b_sev, Z.loc[:, os_].mean(axis=1) - b_sev | |
| for name, x, y in (("spearman E_ox", a, as_), ("spearman E_osm", b, bs_)): | |
| rows.append(("baseline excluding the 10% most severe non-stress conditions", name, | |
| stats.spearmanr(x, y, nan_policy="omit").statistic, int(keep.sum()))) | |
| out = pd.DataFrame(rows, columns=["analysis", "variant", "value", "n"]) | |
| out.to_csv(RES / "sensitivity.csv", index=False) | |