Download code/07_toxicity.py from HarriziSaad/BULMA: direct link, hf CLI and curl.
- Browser
- Download file 1.94 kB
-
https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/07_toxicity.py
- Command line
-
hf download hf://datasets/HarriziSaad/BULMA/code/07_toxicity.py
-
curl -L -o 07_toxicity.py https://huggingface.co/datasets/HarriziSaad/BULMA/resolve/main/code/07_toxicity.py
1.94 kB
| import numpy as np | |
| import pandas as pd | |
| from scipy import stats | |
| from common import RES, hom, axes, centre, effects | |
| Z = hom() | |
| ag, ox, os_, ref = axes(Z.columns) | |
| Zc = centre(Z) | |
| E_ox, E_os = effects(Zc, ox, os_) | |
| cd = (E_ox - E_os).abs() | |
| sev = (Z > 2).sum() / Z.notna().sum() | |
| sd = Z.std() | |
| pct = sev.rank(pct=True) * 100 | |
| C = pd.DataFrame({"agent": ag, "class": np.where(ox, "oxidative", np.where(os_, "osmotic", "baseline-only")), | |
| "severity_frac_z_gt2": sev.values, "severity_percentile": pct.values, "sd": sd.values}, index=Z.columns) | |
| idx = np.where(ox | os_)[0] | |
| rows = [] | |
| for j in idx: | |
| keep_ox = ox.copy() | |
| keep_os = os_.copy() | |
| keep_ox[j] = False | |
| keep_os[j] = False | |
| a, b = effects(Zc, keep_ox, keep_os) | |
| d = (a - b).abs() | |
| m = cd.notna() & d.notna() | |
| top = cd.rank(ascending=False) <= np.ceil(0.1 * m.sum()) | |
| top2 = d.rank(ascending=False) <= np.ceil(0.1 * m.sum()) | |
| rows.append({"left_out": Z.columns[j].split(":")[0], "agent": ag[j], "severity": sev.iloc[j], | |
| "rho_vs_full": stats.spearmanr(cd[m], d[m]).statistic, | |
| "top_decile_retained": (top & top2).sum() / top.sum()}) | |
| L = pd.DataFrame(rows) | |
| L.to_csv(RES / "leave_one_condition_out.csv", index=False) | |
| r = stats.spearmanr(L.severity, L.rho_vs_full) | |
| w = 1 / sd.values | |
| Zw = Zc * w | |
| a_w, b_w = effects(Zw, ox, os_) | |
| cd_w = (a_w - b_w).abs() | |
| m = cd.notna() & cd_w.notna() | |
| pd.DataFrame({"cd_condition_standardised": cd_w}).round(4).rename_axis("gene").to_csv(RES / "cd_condition_standardised.csv") | |
| pd.DataFrame([ | |
| {"statistic": "severity of omitted condition vs rho_vs_full (Spearman)", "value": r.statistic, "p": r.pvalue, "n": len(L)}, | |
| {"statistic": "condition-standardised vs unweighted |cd| (Spearman)", "value": stats.spearmanr(cd[m], cd_w[m]).statistic, | |
| "p": np.nan, "n": int(m.sum())}, | |
| ]).to_csv(RES / "toxicity_summary.csv", index=False) | |