Spaces:
Running
Running
File size: 10,315 Bytes
7bc5fde | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | import os
import pandas as pd
import numpy as np
from sklearn.model_selection import GroupShuffleSplit, train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer, KNNImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
class PPMIDataPreprocessor:
"""
Clean, leak-proof preprocessing pipeline for PPMI dataset.
Handles patient-level splitting, imputation, encoding, scaling.
"""
def __init__(self):
# -------------------------
# Selected Features
# -------------------------
self.selected_features = [
# Demographics
"age", "SEX", "EDUCYRS", "race", "BMI",
# Family History
"fampd", "fampd_bin",
# Motor symptoms
"sym_tremor", "sym_rigid", "sym_brady", "sym_posins",
# Non-motor
"rem", "ess", "gds", "stai",
# Cognitive
"moca", "clockdraw", "bjlot",
# --- EXTENDED MEDICAL FEATURES ---
# Olfactory
"upsit", "upsit_pctl",
# UPDRS (Gold standard for PD)
"updrs1_score", "updrs2_score", "updrs3_score", "updrs4_score", "updrs_totscore",
# DatScan (Imaging)
"mean_caudate", "mean_putamen",
# Biomarkers (CSF)
"abeta", "tau", "ptau",
# Target
"COHORT",
# Patient ID
"PATNO"
]
# Features to impute using KNN (to preserve correlations)
self.knn_cols = [
"moca", "clockdraw", "bjlot", "rem", "gds", "ess", "stai",
"upsit", "upsit_pctl",
"updrs1_score", "updrs2_score", "updrs3_score", "updrs4_score", "updrs_totscore",
"mean_caudate", "mean_putamen",
"abeta", "tau", "ptau"
]
# Categorical
self.categorical_cols = ["SEX", "race", "fampd"]
# Numeric
self.numeric_cols = [
"age", "EDUCYRS", "BMI",
"fampd_bin",
"sym_tremor", "sym_rigid", "sym_brady", "sym_posins",
# New Medical Features (added for enhancement)
"updrs1_score", "updrs2_score", "updrs3_score", "updrs4_score", "updrs_totscore",
"mean_caudate", "mean_putamen",
"abeta", "tau", "ptau",
"upsit", "upsit_pctl"
]
self.preprocessor = None
# ------------------------------------------------------------
def _clean_biomarker(self, x):
"""Clean biomarker strings like '>1700' or '<200' to floats."""
if pd.isna(x):
return np.nan
if isinstance(x, str):
x = x.replace(">", "").replace("<", "")
try:
return float(x)
except ValueError:
return np.nan
def load_data(self, file_path):
"""Load and filter only necessary columns."""
df = pd.read_csv(file_path, low_memory=False)
# Clean biomarker columns if they exist
for col in ["abeta", "tau", "ptau"]:
if col in df.columns:
df[col] = df[col].apply(self._clean_biomarker)
# Select available features (intersection with file columns)
available = [c for c in self.selected_features if c in df.columns]
# Always require COHORT/PATNO
if "COHORT" not in available or "PATNO" not in available:
raise ValueError("Dataset missing required COHORT or PATNO columns")
df = df[available].dropna(subset=["COHORT", "PATNO"])
return df
# ------------------------------------------------------------
def patient_split(self, df, test_size=0.2):
"""Split such that the same patient never appears in both sets."""
gss = GroupShuffleSplit(test_size=test_size, n_splits=1, random_state=42)
train_idx, test_idx = next(gss.split(df, groups=df["PATNO"]))
train_df = df.iloc[train_idx].reset_index(drop=True)
test_df = df.iloc[test_idx].reset_index(drop=True)
return train_df, test_df
# ------------------------------------------------------------
def build_preprocessor(self):
"""Builds leak-proof transformers."""
numeric_transformer = Pipeline(steps=[
("imputer_mean", SimpleImputer(strategy="mean")),
("scaler", StandardScaler())
])
knn_transformer = Pipeline(steps=[
("imputer_knn", KNNImputer(n_neighbors=5)),
("scaler", StandardScaler())
])
categorical_transformer = Pipeline(steps=[
("imputer_mode", SimpleImputer(strategy="most_frequent")),
("encoder", OneHotEncoder(handle_unknown="ignore"))
])
self.preprocessor = ColumnTransformer(
transformers=[
("num", numeric_transformer,
[c for c in self.numeric_cols if c not in self.knn_cols]),
("knn", knn_transformer, self.knn_cols),
("cat", categorical_transformer, self.categorical_cols),
],
remainder="drop"
)
return self.preprocessor
# ------------------------------------------------------------
def prepare(self, file_path):
"""
COMPLETE DATA PREPARATION PIPELINE
Returns X_train, X_test, y_train, y_test
"""
df = self.load_data(file_path)
train_df, test_df = self.patient_split(df)
X_train = train_df.drop(["COHORT", "PATNO"], axis=1)
y_train = train_df["COHORT"]
X_test = test_df.drop(["COHORT", "PATNO"], axis=1)
y_test = test_df["COHORT"]
# Build processor & fit only on training data
pre = self.build_preprocessor()
X_train_processed = pre.fit_transform(X_train)
X_test_processed = pre.transform(X_test)
return X_train_processed, X_test_processed, y_train.values, y_test.values
class DataPreprocessor:
"""Backwards-compatible wrapper around the new PPMI pipeline."""
def __init__(self):
self.core = PPMIDataPreprocessor()
self.feature_names_ = None
self.class_mapping_ = None
self.preprocessor_ = None
self.train_df_ = None
self.test_df_ = None
@staticmethod
def _to_python_scalar(value):
if isinstance(value, np.generic):
return value.item()
return value
def _load_all_files(self, file_paths):
if isinstance(file_paths, str):
file_paths = [file_paths]
dataframes = []
for path in file_paths:
if not path:
continue
if not os.path.exists(path):
print(f"[WARN] DataPreprocessor: '{path}' not found, skipping.")
continue
df = self.core.load_data(path)
dataframes.append(df)
if not dataframes:
raise FileNotFoundError("No valid CSV files were provided to DataPreprocessor.")
combined = pd.concat(dataframes, ignore_index=True)
combined = combined.drop_duplicates().reset_index(drop=True)
return combined
def prepare_data(self, file_paths, test_size=0.2, use_patient_split=True):
"""Expose the legacy API expected by the training scripts."""
df = self._load_all_files(file_paths)
if use_patient_split:
train_df, test_df = self.core.patient_split(df, test_size=test_size)
else:
train_df, test_df = train_test_split(
df,
test_size=test_size,
random_state=42,
stratify=df["COHORT"],
)
self.train_df_ = train_df.reset_index(drop=True)
self.test_df_ = test_df.reset_index(drop=True)
classes_sorted = np.sort(df["COHORT"].unique())
self.class_mapping_ = {
self._to_python_scalar(original): int(idx)
for idx, original in enumerate(classes_sorted)
}
X_train = train_df.drop(["COHORT", "PATNO"], axis=1)
y_train = train_df["COHORT"].map(self.class_mapping_).values
X_test = test_df.drop(["COHORT", "PATNO"], axis=1)
y_test = test_df["COHORT"].map(self.class_mapping_).values
pre = self.core.build_preprocessor()
X_train_processed = pre.fit_transform(X_train)
X_test_processed = pre.transform(X_test)
self.preprocessor_ = pre
try:
self.feature_names_ = pre.get_feature_names_out(X_train.columns).tolist()
except AttributeError:
self.feature_names_ = None
return X_train_processed, X_test_processed, y_train, y_test
def get_feature_names(self):
if self.feature_names_ is None:
raise ValueError("Feature names are unavailable. Call prepare_data() first.")
return self.feature_names_
def get_preprocessor(self):
if self.preprocessor_ is None:
raise ValueError("Preprocessor is unavailable. Call prepare_data() first.")
return self.preprocessor_
def get_class_mapping(self):
if self.class_mapping_ is None:
raise ValueError("Class mapping is unavailable. Call prepare_data() first.")
return self.class_mapping_
def get_split_frames(self):
if self.train_df_ is None or self.test_df_ is None:
raise ValueError("Split frames are unavailable. Call prepare_data() first.")
return self.train_df_.copy(), self.test_df_.copy()
# -------------------------------------------------------------------
# Script example (not executed when imported)
# -------------------------------------------------------------------
if __name__ == "__main__":
file_path = "PPMI_Curated_Data.csv"
prep = PPMIDataPreprocessor()
X_train, X_test, y_train, y_test = prep.prepare(file_path)
print("Train:", X_train.shape)
print("Test:", X_test.shape)
|