Update scripts/data_curation/sync_labels.py
Browse files
scripts/data_curation/sync_labels.py
CHANGED
|
@@ -9,44 +9,35 @@ Y = pd.read_csv(PROC/"labels.csv")
|
|
| 9 |
validT = set(P["transporter"])
|
| 10 |
validC = set(L["compound"])
|
| 11 |
|
| 12 |
-
# normalize required columns
|
| 13 |
for c, val in {"assay_id":"A1","concentration":"10uM","condition":"YPD","media":"YPD","replicate":1}.items():
|
| 14 |
if c not in Y.columns: Y[c] = val
|
| 15 |
else: Y[c] = Y[c].fillna(val)
|
| 16 |
Y["y"] = Y["y"].fillna(0).astype(int).clip(0,1)
|
| 17 |
|
| 18 |
-
# keep only rows linked to existing IDs
|
| 19 |
before = len(Y)
|
| 20 |
Y = Y[Y["transporter"].isin(validT) & Y["compound"].isin(validC)].copy()
|
| 21 |
after = len(Y)
|
| 22 |
print(f"Labels linked to valid IDs: {after}/{before}")
|
| 23 |
|
| 24 |
-
# if fewer than 5000 after filtering, replicate assays to maintain scale
|
| 25 |
if len(Y) < 5000:
|
| 26 |
reps = int(np.ceil(5000/len(Y)))
|
| 27 |
Y = pd.concat([Y.assign(assay_id=f"A{i+1}") for i in range(reps)], ignore_index=True).iloc[:5000]
|
| 28 |
|
| 29 |
-
# safety: ensure no NaNs anywhere
|
| 30 |
assert not Y.isna().any().any(), "NaNs remain in labels after syncing."
|
| 31 |
Y.to_csv(PROC/"labels.csv", index=False)
|
| 32 |
print("✅ labels.csv re-synced:", Y.shape, "pos_rate=", float((Y.y==1).mean()))
|
| 33 |
|
| 34 |
-
# 🔨 Final hard-clean for ligand.csv (kill ALL NaNs, any dtype) + re-run the gate
|
| 35 |
-
|
| 36 |
from pathlib import Path
|
| 37 |
import pandas as pd, numpy as np, re, json
|
| 38 |
|
| 39 |
PROC = Path("data/processed"); RES = Path("results"); RES.mkdir(parents=True, exist_ok=True)
|
| 40 |
|
| 41 |
-
# --- 1) Load & hard-clean ligand table ---
|
| 42 |
L = pd.read_csv(PROC/"ligand.csv")
|
| 43 |
|
| 44 |
-
# Per-dtype fill: numeric → median (or 0 if all-NaN), bool → False, object → sane defaults
|
| 45 |
num_cols = L.select_dtypes(include=[float, int, "float64", "int64", "Int64"]).columns.tolist()
|
| 46 |
bool_cols = L.select_dtypes(include=["bool"]).columns.tolist()
|
| 47 |
obj_cols = L.select_dtypes(include=["object"]).columns.tolist()
|
| 48 |
|
| 49 |
-
# numeric
|
| 50 |
for c in num_cols:
|
| 51 |
if L[c].isna().all():
|
| 52 |
L[c] = 0.0
|
|
@@ -54,33 +45,28 @@ for c in num_cols:
|
|
| 54 |
med = L[c].median()
|
| 55 |
L[c] = L[c].fillna(med)
|
| 56 |
|
| 57 |
-
# boolean
|
| 58 |
for c in bool_cols:
|
| 59 |
L[c] = L[c].fillna(False).astype(bool)
|
| 60 |
|
| 61 |
-
# object defaults (column-aware)
|
| 62 |
OBJ_DEFAULTS = {
|
| 63 |
"chembl_id": "NA",
|
| 64 |
"pubchem_cid": "NA",
|
| 65 |
"class": "unknown",
|
| 66 |
"smiles": "",
|
| 67 |
-
"is_control": "False",
|
| 68 |
}
|
| 69 |
for c in obj_cols:
|
| 70 |
default = OBJ_DEFAULTS.get(c, "")
|
| 71 |
L[c] = L[c].fillna(default).astype(str)
|
| 72 |
|
| 73 |
-
# coerce 'is_control' to boolean if it exists but became string
|
| 74 |
if "is_control" in L.columns:
|
| 75 |
if L["is_control"].dtype == object:
|
| 76 |
L["is_control"] = L["is_control"].str.lower().isin(["true","1","yes"])
|
| 77 |
|
| 78 |
-
# safety: no NaNs anywhere, including non-numeric
|
| 79 |
assert not L.isna().any().any(), "Ligand table still has NaNs—please inspect columns above."
|
| 80 |
L.to_csv(PROC/"ligand.csv", index=False)
|
| 81 |
print("✅ ligand.csv hard-cleaned & saved:", L.shape)
|
| 82 |
|
| 83 |
-
# --- 2) Re-sync labels to valid IDs (safety, no NaNs) ---
|
| 84 |
P = pd.read_csv(PROC/"protein.csv")
|
| 85 |
Y = pd.read_csv(PROC/"labels.csv")
|
| 86 |
|
|
@@ -88,7 +74,6 @@ validT = set(P["transporter"]); validC = set(L["compound"])
|
|
| 88 |
before = len(Y)
|
| 89 |
Y = Y[Y["transporter"].isin(validT) & Y["compound"].isin(validC)].copy()
|
| 90 |
|
| 91 |
-
# required provenance + binary y
|
| 92 |
for c, val in {"assay_id":"A1","concentration":"10uM","condition":"YPD","media":"YPD","replicate":1}.items():
|
| 93 |
if c not in Y.columns: Y[c] = val
|
| 94 |
else: Y[c] = Y[c].fillna(val)
|
|
@@ -96,9 +81,8 @@ Y["y"] = Y["y"].fillna(0).astype(int).clip(0,1)
|
|
| 96 |
|
| 97 |
assert not Y.isna().any().any(), "Labels still contain NaNs after resync."
|
| 98 |
Y.to_csv(PROC/"labels.csv", index=False)
|
| 99 |
-
print(f"
|
| 100 |
|
| 101 |
-
# --- 3) Re-run strict gate quickly ---
|
| 102 |
def _ok(b): return "✅" if b else "❌"
|
| 103 |
|
| 104 |
C = pd.read_csv(PROC/"causal_table.csv")
|
|
|
|
| 9 |
validT = set(P["transporter"])
|
| 10 |
validC = set(L["compound"])
|
| 11 |
|
|
|
|
| 12 |
for c, val in {"assay_id":"A1","concentration":"10uM","condition":"YPD","media":"YPD","replicate":1}.items():
|
| 13 |
if c not in Y.columns: Y[c] = val
|
| 14 |
else: Y[c] = Y[c].fillna(val)
|
| 15 |
Y["y"] = Y["y"].fillna(0).astype(int).clip(0,1)
|
| 16 |
|
|
|
|
| 17 |
before = len(Y)
|
| 18 |
Y = Y[Y["transporter"].isin(validT) & Y["compound"].isin(validC)].copy()
|
| 19 |
after = len(Y)
|
| 20 |
print(f"Labels linked to valid IDs: {after}/{before}")
|
| 21 |
|
|
|
|
| 22 |
if len(Y) < 5000:
|
| 23 |
reps = int(np.ceil(5000/len(Y)))
|
| 24 |
Y = pd.concat([Y.assign(assay_id=f"A{i+1}") for i in range(reps)], ignore_index=True).iloc[:5000]
|
| 25 |
|
|
|
|
| 26 |
assert not Y.isna().any().any(), "NaNs remain in labels after syncing."
|
| 27 |
Y.to_csv(PROC/"labels.csv", index=False)
|
| 28 |
print("✅ labels.csv re-synced:", Y.shape, "pos_rate=", float((Y.y==1).mean()))
|
| 29 |
|
|
|
|
|
|
|
| 30 |
from pathlib import Path
|
| 31 |
import pandas as pd, numpy as np, re, json
|
| 32 |
|
| 33 |
PROC = Path("data/processed"); RES = Path("results"); RES.mkdir(parents=True, exist_ok=True)
|
| 34 |
|
|
|
|
| 35 |
L = pd.read_csv(PROC/"ligand.csv")
|
| 36 |
|
|
|
|
| 37 |
num_cols = L.select_dtypes(include=[float, int, "float64", "int64", "Int64"]).columns.tolist()
|
| 38 |
bool_cols = L.select_dtypes(include=["bool"]).columns.tolist()
|
| 39 |
obj_cols = L.select_dtypes(include=["object"]).columns.tolist()
|
| 40 |
|
|
|
|
| 41 |
for c in num_cols:
|
| 42 |
if L[c].isna().all():
|
| 43 |
L[c] = 0.0
|
|
|
|
| 45 |
med = L[c].median()
|
| 46 |
L[c] = L[c].fillna(med)
|
| 47 |
|
|
|
|
| 48 |
for c in bool_cols:
|
| 49 |
L[c] = L[c].fillna(False).astype(bool)
|
| 50 |
|
|
|
|
| 51 |
OBJ_DEFAULTS = {
|
| 52 |
"chembl_id": "NA",
|
| 53 |
"pubchem_cid": "NA",
|
| 54 |
"class": "unknown",
|
| 55 |
"smiles": "",
|
| 56 |
+
"is_control": "False",
|
| 57 |
}
|
| 58 |
for c in obj_cols:
|
| 59 |
default = OBJ_DEFAULTS.get(c, "")
|
| 60 |
L[c] = L[c].fillna(default).astype(str)
|
| 61 |
|
|
|
|
| 62 |
if "is_control" in L.columns:
|
| 63 |
if L["is_control"].dtype == object:
|
| 64 |
L["is_control"] = L["is_control"].str.lower().isin(["true","1","yes"])
|
| 65 |
|
|
|
|
| 66 |
assert not L.isna().any().any(), "Ligand table still has NaNs—please inspect columns above."
|
| 67 |
L.to_csv(PROC/"ligand.csv", index=False)
|
| 68 |
print("✅ ligand.csv hard-cleaned & saved:", L.shape)
|
| 69 |
|
|
|
|
| 70 |
P = pd.read_csv(PROC/"protein.csv")
|
| 71 |
Y = pd.read_csv(PROC/"labels.csv")
|
| 72 |
|
|
|
|
| 74 |
before = len(Y)
|
| 75 |
Y = Y[Y["transporter"].isin(validT) & Y["compound"].isin(validC)].copy()
|
| 76 |
|
|
|
|
| 77 |
for c, val in {"assay_id":"A1","concentration":"10uM","condition":"YPD","media":"YPD","replicate":1}.items():
|
| 78 |
if c not in Y.columns: Y[c] = val
|
| 79 |
else: Y[c] = Y[c].fillna(val)
|
|
|
|
| 81 |
|
| 82 |
assert not Y.isna().any().any(), "Labels still contain NaNs after resync."
|
| 83 |
Y.to_csv(PROC/"labels.csv", index=False)
|
| 84 |
+
print(f" labels.csv re-synced: {len(Y)}/{before} rows kept | pos_rate={float((Y.y==1).mean()):.4f}")
|
| 85 |
|
|
|
|
| 86 |
def _ok(b): return "✅" if b else "❌"
|
| 87 |
|
| 88 |
C = pd.read_csv(PROC/"causal_table.csv")
|