Download code/04_reproducibility.py from HarriziSaad/BULMA: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/04_reproducibility.py
- Command line
-
hf download hf://datasets/HarriziSaad/BULMA/code/04_reproducibility.py
-
curl -L -o 04_reproducibility.py https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/04_reproducibility.py
3.37 kB
| import itertools | |
| import numpy as np | |
| import pandas as pd | |
| from scipy import stats | |
| from common import RES, SEED, hom, axes, centre, effects | |
| N_SPLITS = 1000 | |
| Z = hom() | |
| ag, ox, os_, ref = axes(Z.columns) | |
| Zc = centre(Z) | |
| E_ox, E_os = effects(Zc, ox, os_) | |
| ox_idx, os_idx = np.where(ox)[0], np.where(os_)[0] | |
| X = Zc.values | |
| def halves(o_a, s_a, o_b, s_b): | |
| a_ox, a_os = np.nanmean(X[:, o_a], axis=1), np.nanmean(X[:, s_a], axis=1) | |
| b_ox, b_os = np.nanmean(X[:, o_b], axis=1), np.nanmean(X[:, s_b], axis=1) | |
| m = ~np.isnan(a_ox + a_os + b_ox + b_os) | |
| r = lambda x, y: stats.spearmanr(x[m], y[m]).statistic | |
| return {"cd_signed": r(a_ox - a_os, b_ox - b_os), "cd_abs": r(np.abs(a_ox - a_os), np.abs(b_ox - b_os)), | |
| "oxidative": r(a_ox, b_ox), "osmotic": r(a_os, b_os)} | |
| rows = [] | |
| rows.append({"split": "odd/even within axis (published)", | |
| **halves(ox_idx[0::2], os_idx[0::2], ox_idx[1::2], os_idx[1::2])}) | |
| rng = np.random.default_rng(SEED) | |
| rand = [] | |
| h2o2 = set(np.where(ag == "h2o2")[0]) | |
| for _ in range(N_SPLITS): | |
| po, ps = rng.permutation(ox_idx), rng.permutation(os_idx) | |
| r = halves(po[:len(po) // 2], ps[:len(ps) // 2], po[len(po) // 2:], ps[len(ps) // 2:]) | |
| r["h2o2_replicates_separated"] = len(set(po[:len(po) // 2]) & h2o2) == 1 | |
| rand.append(r) | |
| R = pd.DataFrame(rand) | |
| R.to_csv(RES / "splithalf_random.csv", index=False) | |
| for q, lab in ((0.5, "median"), (0.025, "2.5%"), (0.975, "97.5%")): | |
| rows.append({"split": f"random within-axis splits ({N_SPLITS}), {lab}", | |
| **R.drop(columns="h2o2_replicates_separated").quantile(q).to_dict()}) | |
| OXA, OSA = ["h2o2", "paraquat", "4nqo"], ["nacl", "sorbitol"] | |
| for k in (1,): | |
| for oa in itertools.combinations(OXA, k): | |
| for sa in OSA: | |
| ob = [a for a in OXA if a not in oa] | |
| sb = [a for a in OSA if a != sa] | |
| o_a = np.where(np.isin(ag, oa))[0] | |
| o_b = np.where(np.isin(ag, ob))[0] | |
| s_a = np.where(ag == sa)[0] | |
| s_b = np.where(np.isin(ag, sb))[0] | |
| rows.append({"split": f"agent-blocked: {'+'.join(oa)} & {sa} | {'+'.join(ob)} & {'+'.join(sb)}", | |
| **halves(o_a, s_a, o_b, s_b)}) | |
| scanner = np.array([c.split(":")[9] if len(c.split(":")) > 9 else "" for c in Z.columns]) | |
| new_os = np.where(os_ & (scanner == "new scanner"))[0] | |
| old_os = np.where(os_ & (scanner == "old scanner"))[0] | |
| cd_full = (E_ox - E_os).abs() | |
| for name, idx in (("osmotic restricted to 2004 batch (new scanner)", new_os), | |
| ("osmotic restricted to 2001 batch (old scanner)", old_os)): | |
| e = np.nanmean(X[:, idx], axis=1) | |
| c2 = np.abs(E_ox.values - e) | |
| m = ~np.isnan(c2) & cd_full.notna().values | |
| rows.append({"split": f"{name}: |cd| vs full (n conditions {len(idx)})", | |
| "cd_abs": stats.spearmanr(cd_full.values[m], c2[m]).statistic, | |
| "osmotic": stats.spearmanr(E_os.values[m], e[m]).statistic}) | |
| e_new, e_old = np.nanmean(X[:, new_os], axis=1), np.nanmean(X[:, old_os], axis=1) | |
| m = ~np.isnan(e_new + e_old) | |
| rows.append({"split": "osmotic 2001 batch vs 2004 batch (effect agreement)", | |
| "osmotic": stats.spearmanr(e_new[m], e_old[m]).statistic}) | |
| out = pd.DataFrame(rows) | |
| out.to_csv(RES / "reproducibility.csv", index=False) | |
| pd.set_option("display.width", 220) | |