OSCR

Predicting post-stroke functional outcome using explainable machine learning and integrated data.

Code ↔ Paper

8 matches between paragraphs of the paper and lines of its authors' code, computed by the harvester (lexical-v1). Click a colored paragraph or line to see its counterpart.

The 8 matches
  1. [1] § Methods › Computational workflow › Machine learning workflow ↔ manuscript/supplement.qmd, lines 34–145 · score 0.91 · exponential linear unit, hidden layers, activation functions, developed empirically, MLP architecture, multilayer perceptron
  2. [2] § Methods › Data source and study population ↔ manuscript/supplement.qmd, lines 34–145 · score 0.80 · acute phase, Clinical Chemistry, clinical routine, Plasma, growth, serum
  3. [3] § Methods › Computational workflow › Data preprocessing ↔ code/ModelComparer.py, lines 184–227 · score 0.74 · hot encoded, Numerical features, kNN, neighbors, imputed, binary
  4. [4] § Methods › Computational workflow › Machine learning workflow ↔ code/ModelComparer.py, lines 229–289 · score 0.70 · cross validation, training folds, fold iteration, shuffled, stratified, CV
  5. [5] § Methods › Computational workflow › Machine learning workflow ↔ code/ModelComparer.py, lines 393–441 · score 0.61 · randomized search, XGBoost, trees, tuned, classification, boosted
  6. [6] § Methods › Computational workflow › Machine learning workflow ↔ code/ModelComparer.py, lines 334–391 · score 0.60 · Logistic regression, L1, L2, penalty, Ridge, tuned
  7. [7] § Methods › Computational workflow › Machine learning workflow ↔ code/ModelComparer.py, lines 334–391 · score 0.58 · Hyperparameter tuning, logistic regression, ridge, LASSO, CV, training
  8. [8] § Methods › Computational workflow › Feature importance ↔ code/ModelComparer.py, lines 680–774 · score 0.50 · logistic regression, XGBoost, Ridge, LASSO, SAGE, MLP

Paper

Loaded from Europe PMC by your browser, not stored by OSCR: doi.org · Europe PMC

The paper is loaded when this pane is shown.

The authors' code

Python · 848 lines · 30 KB · MIT · 6 matches

  1. import os
  2. import pandas as pd
  3. import numpy as np
  4. import xgboost as xgb
  5. import sage
  6. import torch
  7. import torch.nn as nn
  8. import torch.optim as optim
  9. from pathlib import Path
  10. from datetime import datetime
  11. from copy import deepcopy
  12. from torch.utils.data import DataLoader, TensorDataset
  13. from boruta import BorutaPy
  14. from sklearn.model_selection import (
  15. StratifiedKFold,
  16. GridSearchCV,
  17. RandomizedSearchCV,
  18. train_test_split,
  19. )
  20. from sklearn.preprocessing import (
  21. StandardScaler,
  22. OneHotEncoder,
  23. )
  24. from sklearn.impute import KNNImputer, SimpleImputer
  25. from sklearn.linear_model import LogisticRegression
  26. from sklearn.ensemble import RandomForestClassifier
  27. from sklearn.metrics import (
  28. accuracy_score,
  29. precision_score,
  30. recall_score,
  31. f1_score,
  32. roc_auc_score,
  33. average_precision_score,
  34. cohen_kappa_score,
  35. confusion_matrix,
  36. )
  37. from sklearn.pipeline import Pipeline
  38. from sklearn.compose import ColumnTransformer
  39. # User-defined functions
  40. from MLPClassifier import MLPClassifier
  41. BASE_DIR = Path(os.path.abspath(__file__)).parent.parent
  42. class ModelComparer:
  43. def __init__(
  44. self,
  45. data_path="data",
  46. results_path=None,
  47. export_prepared_data=True,
  48. calc_feature_importance=True,
  49. exclude_vars=[],
  50. num_folds=5,
  51. num_repeats=5,
  52. perform_feature_selection=True,
  53. hyperparam_scoring="neg_log_loss",
  54. boruta_max_iter=100,
  55. seed=2025,
  56. ):
  57. self.seed = seed
  58. self.repeat_seeds = [seed + i for i in range(num_repeats)]
  59. np.random.seed(seed)
  60. torch.manual_seed(seed)
  61. self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  62. self.data_path = data_path
  63. self.export_prepared_data = export_prepared_data # Whether to export prepared data (before preprocessing)
  64. self.results_path = (
  65. results_path
  66. if results_path
  67. else os.path.join(
  68. BASE_DIR, f"results/exp_{datetime.now().strftime('%Y%m%d-%H%M%S')}"
  69. )
  70. )
  71. self.num_folds = num_folds
  72. self.num_repeats = num_repeats
  73. self.calc_feature_importance = calc_feature_importance
  74. self.perform_feature_selection = perform_feature_selection
  75. self.boruta_max_iter = boruta_max_iter
  76. self.hyperparam_scoring = hyperparam_scoring
  77. self.exclude_vars = [] if exclude_vars is None else exclude_vars
  78. assert self.num_repeats > 0, "Number of repeats must be at least 1."
  79. # Create results directory if it doesn't exist
  80. os.makedirs(self.results_path, exist_ok=True)
  81. # Initialize results storage
  82. self.results_summary = []
  83. self.fold_results = [] # Model performance results for each fold
  84. def load_and_prepare_data(self):
  85. """Load and prepare data similar to R script"""
  86. print("Loading and preparing data...")
  87. # Load data
  88. self.raw_data = pd.read_csv(f"{self.data_path}/sahlsis.csv", index_col=0)
  89. self.metadata = pd.read_csv(f"{self.data_path}/sahlsis_meta.csv")
  90. # Identify variable types
  91. scalar_vars = self.metadata[self.metadata["type"] == "scalar"]["name"].tolist()
  92. categorical_vars = self.metadata[self.metadata["type"] == "categorical"][
  93. "name"
  94. ].tolist()
  95. binary_vars = self.metadata[self.metadata["type"] == "binary"]["name"].tolist()
  96. non_predictors = [
  97. "MRS",
  98. "MRS_dict",
  99. "Stroke",
  100. ] # Outcome and recurrence variables
  101. filter_vars = non_predictors + self.exclude_vars
  102. # Convert factor features
  103. for var in categorical_vars + binary_vars:
  104. if var in self.raw_data.columns:
  105. self.raw_data[var] = self.raw_data[var].astype("category")
  106. for var in scalar_vars:
  107. if var in self.raw_data.columns:
  108. self.raw_data[var] = pd.to_numeric(self.raw_data[var], errors="coerce")
  109. self.raw_data[var] = self.raw_data[var].astype("float64")
  110. # Remove rows and columns with too many missing values
  111. row_cutoff = 0.2
  112. col_cutoff = 0.3
  113. row_missing_rate = (
  114. self.raw_data.loc[self.raw_data["MRS_dict"].notna()].isna().mean(axis=1)
  115. )
  116. col_missing_rate = (
  117. self.raw_data.loc[self.raw_data["MRS_dict"].notna()].isna().mean(axis=0)
  118. )
  119. print(
  120. f"Removing {sum(self.raw_data['MRS_dict'].isna())} cases with missing mRS."
  121. )
  122. print(
  123. f"Removing {sum(row_missing_rate > row_cutoff)} cases with more than {row_cutoff:.0%} missing values."
  124. )
  125. print(
  126. f"Removing {sum(self.raw_data['Stroke'] == 1)} cases with recurrent stroke."
  127. )
  128. print(
  129. f"Removing {sum(col_missing_rate > col_cutoff)} features with more than {col_cutoff:.0%} missing values."
  130. )
  131. self.data = (
  132. self.raw_data.loc[self.raw_data["MRS_dict"].notna()]
  133. .loc[row_missing_rate <= row_cutoff]
  134. .loc[:, col_missing_rate <= col_cutoff]
  135. .loc[self.raw_data["Stroke"] != 1]
  136. )
  137. # Handle target variable
  138. target_map = {"Favorable": 0, "Unfavorable": 1}
  139. self.data["MRS_dict"] = self.data["MRS_dict"].map(target_map).astype(int)
  140. # Prepare features and target
  141. if self.exclude_vars != []:
  142. print(f"Filtering features: {self.exclude_vars}")
  143. self.X = self.data.drop(filter_vars, axis=1)
  144. self.y = self.data["MRS_dict"]
  145. print(f"Data shape after cleaning: {self.X.shape}")
  146. print(
  147. f"Class distribution: {self.y.value_counts(normalize=True).round(2).to_dict()}"
  148. )
  149. print("Data preparation complete.")
  150. def get_prepared_data(self):
  151. """Get prepared features and target"""
  152. return self.X, self.y
  153. def export_prepared_dataset(
  154. self, return_X_y=False, export_path=None, save_csv=True
  155. ):
  156. """Export prepared dataset before preprocessing"""
  157. if not export_path:
  158. export_path = os.path.join(self.results_path, "prepared_data.csv")
  159. prepared_data = self.data.copy()
  160. if save_csv:
  161. prepared_data.to_csv(export_path, index=True)
  162. print(f"Prepared dataset exported to {export_path}\n")
  163. if return_X_y:
  164. return self.X, self.y
  165. def create_preprocessor(self):
  166. """Create preprocessing pipeline"""
  167. # Identify numeric and categorical columns
  168. numeric_features = self.X.select_dtypes(include=[np.number]).columns.tolist()
  169. factor_features = self.X.select_dtypes(include=["category"]).columns.tolist()
  170. imp_num = KNNImputer(n_neighbors=5)
  171. # Create preprocessing pipelines
  172. numeric_transformer = Pipeline(
  173. steps=[
  174. ("scaler", StandardScaler()),
  175. ("imputer", imp_num),
  176. ]
  177. )
  178. def feature_name_combiner(input_feature, category):
  179. return f"{input_feature}::{category}"
  180. # For categorical features: impute missing with most frequent, then OneHotEncode and scale
  181. imp_cat = SimpleImputer(strategy="most_frequent")
  182. factor_transformer = Pipeline(
  183. steps=[
  184. ("imputer", imp_cat),
  185. (
  186. "onehot",
  187. OneHotEncoder(
  188. drop="if_binary",
  189. handle_unknown="ignore",
  190. sparse_output=False,
  191. feature_name_combiner=feature_name_combiner,
  192. ),
  193. ),
  194. ("scaler", StandardScaler()),
  195. ]
  196. )
  197. # Combine preprocessing steps
  198. preprocessor = ColumnTransformer(
  199. transformers=[
  200. ("num", numeric_transformer, numeric_features),
  201. ("cat", factor_transformer, factor_features),
  202. ]
  203. )
  204. return preprocessor
  205. def prepare_folds(self):
  206. """Prepare repeated cross-validation folds with preprocessing."""
  207. print("Preparing repeated cross-validation folds...")
  208. self.fold_data = []
  209. for repeat_idx, seed in enumerate(self.repeat_seeds):
  210. cv = StratifiedKFold(
  211. n_splits=self.num_folds, shuffle=True, random_state=seed
  212. )
  213. for fold_idx, (train_idx, test_idx) in enumerate(cv.split(self.X, self.y)):
  214. X_train_fold = self.X.iloc[train_idx]
  215. X_test_fold = self.X.iloc[test_idx]
  216. y_train_fold = self.y.iloc[train_idx]
  217. y_test_fold = self.y.iloc[test_idx]
  218. preprocessor = self.create_preprocessor()
  219. X_train_processed = preprocessor.fit_transform(X_train_fold)
  220. X_test_processed = preprocessor.transform(X_test_fold)
  221. # Get feature names
  222. feature_names = []
  223. numeric_features = X_train_fold.select_dtypes(
  224. include=[np.number]
  225. ).columns.tolist()
  226. feature_names.extend(numeric_features)
  227. factor_features = X_train_fold.select_dtypes(
  228. include=["category", "object"]
  229. ).columns.tolist()
  230. cat_pipe = preprocessor.named_transformers_["cat"]
  231. cat_encoder = cat_pipe.named_steps["onehot"]
  232. cat_feature_names_out = cat_encoder.get_feature_names_out(
  233. factor_features
  234. )
  235. feature_names.extend(cat_feature_names_out)
  236. X_train_processed = pd.DataFrame(
  237. X_train_processed, columns=feature_names, index=X_train_fold.index
  238. )
  239. X_test_processed = pd.DataFrame(
  240. X_test_processed, columns=feature_names, index=X_test_fold.index
  241. )
  242. self.fold_data.append(
  243. {
  244. "X_train": X_train_processed,
  245. "X_test": X_test_processed,
  246. "y_train": y_train_fold.values,
  247. "y_test": y_test_fold.values,
  248. }
  249. )
  250. self.unique_features = np.unique(
  251. np.concatenate(
  252. [fold["X_train"].columns.tolist() for fold in self.fold_data]
  253. )
  254. ).tolist()
  255. print(
  256. f"Prepared {self.num_folds} folds across {len(self.repeat_seeds)} repeats (total {len(self.fold_data)} fold iterations).\n"
  257. )
  258. def run_feature_selection(self):
  259. """Run Boruta feature selection on each fold"""
  260. print("Starting Boruta feature selection...")
  261. print(f"Running for a maximum of {self.boruta_max_iter} iterations per fold.")
  262. self.selected_features = []
  263. self.selected_indices = []
  264. for fold_idx in range(len(self.fold_data)):
  265. fold = self.fold_data[fold_idx]
  266. X_train = fold["X_train"]
  267. y_train = fold["y_train"]
  268. rf = RandomForestClassifier(n_jobs=-1, random_state=self.seed)
  269. boruta_selector = BorutaPy(
  270. rf,
  271. n_estimators="auto",
  272. verbose=0,
  273. random_state=self.seed,
  274. max_iter=self.boruta_max_iter,
  275. )
  276. boruta_selector.fit(X_train, y_train)
  277. selected_features = [
  278. X_train.columns[i]
  279. for i in range(len(X_train.columns))
  280. if boruta_selector.support_[i] or boruta_selector.support_weak_[i]
  281. ]
  282. self.selected_features.append(selected_features)
  283. selected_indices = [
  284. X_train.columns.get_loc(feature) for feature in selected_features
  285. ]
  286. self.selected_indices.append(selected_indices)
  287. print(
  288. f"Boruta selected {len(selected_features)} features for fold {fold_idx + 1}"
  289. )
  290. print(
  291. f"{sum(boruta_selector.support_)} confirmed, {sum(boruta_selector.support_weak_)} tentative."
  292. )
  293. self.unique_features = np.unique(
  294. np.concatenate(self.selected_features)
  295. ).tolist()
  296. print("Boruta feature selection complete.")
  297. def run_logistic_regression(
  298. self, alpha=1.0, selected_features=None, model_name="Logistic"
  299. ):
  300. """Run logistic regression (LASSO/Ridge based on alpha)"""
  301. print(f"Running {model_name}...")
  302. y_pred_list = []
  303. fitted_models = []
  304. for fold_idx in range(len(self.fold_data)):
  305. fold = self.fold_data[fold_idx]
  306. X_train = fold["X_train"]
  307. X_test = fold["X_test"]
  308. y_train = fold["y_train"]
  309. # Feature selection if provided
  310. if selected_features is not None:
  311. X_train = X_train.loc[:, selected_features[fold_idx]]
  312. X_test = X_test.loc[:, selected_features[fold_idx]]
  313. # Model with cross-validation for C parameter
  314. if alpha == 1.0: # LASSO
  315. model = LogisticRegression(
  316. penalty="l1",
  317. solver="liblinear",
  318. random_state=self.seed,
  319. max_iter=1000,
  320. )
  321. elif alpha == 0.0: # Ridge
  322. model = LogisticRegression(
  323. penalty="l2",
  324. random_state=self.seed,
  325. max_iter=1000,
  326. )
  327. else: # Elastic Net
  328. model = LogisticRegression(
  329. penalty="elasticnet",
  330. l1_ratio=alpha,
  331. solver="saga",
  332. random_state=self.seed,
  333. max_iter=1000,
  334. )
  335. # Hyperparameter tuning
  336. param_grid = {"C": np.logspace(-4, 4, 100)}
  337. grid_search = GridSearchCV(
  338. model,
  339. param_grid,
  340. cv=5,
  341. scoring=self.hyperparam_scoring,
  342. n_jobs=-1,
  343. )
  344. grid_search.fit(X_train.values, y_train)
  345. fitted_models.append(grid_search.best_estimator_)
  346. # Predict
  347. y_pred_proba = grid_search.predict_proba(X_test.values)[:, 1]
  348. y_pred_list.append(y_pred_proba)
  349. return y_pred_list, fitted_models
  350. def run_xgboost(self, selected_features=None):
  351. """Run XGBoost"""
  352. print("Running XGBoost...")
  353. y_pred_list = []
  354. fitted_models = []
  355. for fold_idx in range(len(self.fold_data)):
  356. fold = self.fold_data[fold_idx]
  357. X_train = fold["X_train"]
  358. X_test = fold["X_test"]
  359. y_train = fold["y_train"]
  360. # Feature selection if provided
  361. if selected_features is not None:
  362. X_train = X_train.loc[:, selected_features[fold_idx]]
  363. X_test = X_test.loc[:, selected_features[fold_idx]]
  364. # Hyperparameter tuning
  365. param_grid = {
  366. "n_estimators": [100, 200, 300, 500],
  367. "max_depth": [3, 5, 7],
  368. "learning_rate": [0.05, 0.1, 0.2, 0.3],
  369. "subsample": [0.8, 0.9, 1.0],
  370. "colsample_bytree": [0.8, 0.9, 1.0],
  371. "reg_lambda": [0, 0.5, 1.0, 5.0, 10.0],
  372. "min_child_weight": [1, 3, 5, 7, 10],
  373. }
  374. xgb_model = xgb.XGBClassifier(
  375. tree_method="hist",
  376. seed=self.seed,
  377. eval_metric="logloss",
  378. # max_delta_step=1,
  379. )
  380. grid_search = RandomizedSearchCV(
  381. xgb_model,
  382. param_grid,
  383. n_iter=150,
  384. cv=5,
  385. scoring=self.hyperparam_scoring,
  386. random_state=self.seed,
  387. )
  388. grid_search.fit(X_train, y_train)
  389. fitted_models.append(grid_search.best_estimator_)
  390. # Predict
  391. y_pred_proba = grid_search.predict_proba(X_test)[:, 1]
  392. y_pred_list.append(y_pred_proba)
  393. return y_pred_list, fitted_models
  394. def run_nn(self, selected_features=None):
  395. """Run Neural Network"""
  396. print("Running Neural Network...")
  397. torch.cuda.empty_cache()
  398. dropout_prob = 0.5
  399. lr = 0.001
  400. weight_decay = 0.0001
  401. num_epochs = 100
  402. patience = 7
  403. min_delta = 1e-6
  404. batch_size = 32
  405. y_pred_list = []
  406. fitted_models = []
  407. for fold_idx in range(len(self.fold_data)):
  408. fold = self.fold_data[fold_idx]
  409. X_train = fold["X_train"]
  410. X_test = fold["X_test"]
  411. y_train = fold["y_train"]
  412. # Feature selection if provided
  413. if selected_features is not None:
  414. X_train = X_train.loc[:, selected_features[fold_idx]]
  415. X_test = X_test.loc[:, selected_features[fold_idx]]
  416. # Prepare dataset
  417. X_train, X_val, y_train, y_val = train_test_split(
  418. X_train, y_train, test_size=0.1, random_state=self.seed,
  419. # stratify=y_train # NOTE: Not present in submitted results
  420. )
  421. train_dataset = TensorDataset(
  422. torch.tensor(X_train.values, dtype=torch.float32),
  423. torch.tensor(y_train, dtype=torch.float32),
  424. )
  425. train_loader = DataLoader(
  426. train_dataset, batch_size=batch_size, shuffle=True
  427. )
  428. X_val_tensor = torch.tensor(X_val.values, dtype=torch.float32)
  429. y_val_tensor = torch.tensor(y_val, dtype=torch.float32)
  430. X_test_tensor = torch.tensor(X_test.values, dtype=torch.float32)
  431. model = MLPClassifier(
  432. input_size=X_train.shape[1], dropout_prob=dropout_prob
  433. )
  434. model.to(self.device)
  435. criterion = nn.BCEWithLogitsLoss()
  436. optimizer = optim.AdamW(
  437. model.parameters(), lr=lr, weight_decay=weight_decay
  438. )
  439. # For early stopping
  440. best_val_loss = float("inf")
  441. patience_counter = 0
  442. best_model_state = None
  443. train_losses = []
  444. val_losses = []
  445. # Training loop
  446. for epoch in range(num_epochs):
  447. model.train()
  448. train_epoch_loss = 0.0
  449. for inputs, labels in train_loader:
  450. inputs = inputs.to(self.device)
  451. labels = labels.to(self.device)
  452. optimizer.zero_grad()
  453. outputs = model(inputs).squeeze()
  454. loss = criterion(outputs, labels)
  455. loss.backward()
  456. optimizer.step()
  457. train_epoch_loss += loss.item()
  458. train_epoch_loss /= len(train_loader)
  459. train_losses.append(train_epoch_loss)
  460. model.eval()
  461. val_outputs = model(X_val_tensor.to(self.device)).squeeze()
  462. val_loss = criterion(val_outputs, y_val_tensor.to(self.device))
  463. val_losses.append(val_loss.item())
  464. if val_loss < best_val_loss - min_delta:
  465. best_val_loss = val_loss
  466. patience_counter = 0
  467. best_model_state = deepcopy(model.state_dict())
  468. else:
  469. patience_counter += 1
  470. if patience_counter >= patience:
  471. break
  472. # Load best model
  473. model.load_state_dict(best_model_state)
  474. # Predict
  475. model.eval()
  476. with torch.no_grad():
  477. y_pred_proba = (
  478. torch.sigmoid(model(X_test_tensor.to(self.device)))
  479. .squeeze()
  480. .cpu()
  481. .numpy()
  482. )
  483. y_pred_list.append(y_pred_proba)
  484. fitted_models.append(model.cpu())
  485. return y_pred_list, fitted_models
  486. def evaluate_metrics(self, y_true, y_pred_proba, threshold=0.5):
  487. """Calculate comprehensive evaluation metrics"""
  488. y_pred = (y_pred_proba >= threshold).astype(int)
  489. # Calculate metrics
  490. accuracy = accuracy_score(y_true, y_pred)
  491. sensitivity = recall_score(y_true, y_pred)
  492. tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
  493. specificity = tn / (tn + fp) if (tn + fp) > 0 else 0
  494. ppv = precision_score(y_true, y_pred, zero_division=0)
  495. npv = tn / (tn + fn) if (tn + fn) > 0 else 0
  496. kappa = cohen_kappa_score(y_true, y_pred)
  497. f1 = f1_score(y_true, y_pred)
  498. auroc = roc_auc_score(y_true, y_pred_proba)
  499. auprc = average_precision_score(y_true, y_pred_proba)
  500. return {
  501. "Accuracy": accuracy,
  502. "Sensitivity": sensitivity,
  503. "Specificity": specificity,
  504. "PPV": ppv,
  505. "NPV": npv,
  506. "F1": f1,
  507. "Kappa": kappa,
  508. "AUROC": auroc,
  509. "AUPRC": auprc,
  510. }
  511. def calculate_fold_metrics(
  512. self, y_pred_list, model_name
  513. ): # TODO: Add train metrics
  514. """Calculate metrics across all folds"""
  515. fold_metrics = []
  516. for fold_idx in range(len(self.fold_data)):
  517. y_true = self.fold_data[fold_idx]["y_test"]
  518. y_pred_proba = y_pred_list[fold_idx]
  519. metrics = self.evaluate_metrics(y_true, y_pred_proba)
  520. self.fold_results.append(
  521. {
  522. "Fold": fold_idx,
  523. "Model": model_name,
  524. **metrics,
  525. }
  526. )
  527. fold_metrics.append(metrics)
  528. # Calculate mean and std across folds
  529. metrics_df = pd.DataFrame(fold_metrics)
  530. summary = {}
  531. summary["Model"] = model_name
  532. for metric in metrics_df.columns:
  533. summary[f"{metric}_mean"] = metrics_df[metric].mean()
  534. summary[f"{metric}_sd"] = metrics_df[metric].std()
  535. return summary
  536. def compute_SAGE_values_across_folds(
  537. self, model_name, fitted_models, use_test_data=True
  538. ):
  539. """Compute SAGE values across all folds"""
  540. sage_data = []
  541. if self.perform_feature_selection:
  542. selected_indices = self.selected_indices
  543. present_features = self.unique_features
  544. else:
  545. selected_indices = None
  546. present_features = self.unique_features
  547. for fold_idx in range(len(self.fold_data)):
  548. fold = self.fold_data[fold_idx]
  549. if self.perform_feature_selection:
  550. current_features = fold["X_train"].columns[selected_indices[fold_idx]]
  551. else:
  552. current_features = fold["X_train"].columns
  553. X_bg = (
  554. fold["X_train"]
  555. if not selected_indices
  556. else fold["X_train"].loc[:, current_features]
  557. )
  558. if use_test_data:
  559. X_exp = (
  560. fold["X_test"]
  561. if not selected_indices
  562. else fold["X_test"].loc[:, current_features]
  563. )
  564. y_exp = fold["y_test"]
  565. else:
  566. X_exp = (
  567. fold["X_train"]
  568. if not selected_indices
  569. else fold["X_train"].loc[:, current_features]
  570. )
  571. y_exp = fold["y_train"]
  572. if model_name == "MLP":
  573. torch.cuda.empty_cache()
  574. model = fitted_models[fold_idx]
  575. model.sigmoid_output = True
  576. model.to(self.device)
  577. model.eval()
  578. n_jobs = 4
  579. else:
  580. model = fitted_models[fold_idx]
  581. n_jobs = -2
  582. sage_imputer = sage.MarginalImputer(fitted_models[fold_idx], X_bg.values)
  583. sage_estimator = sage.PermutationEstimator(
  584. sage_imputer, n_jobs=n_jobs, random_state=self.seed
  585. )
  586. sage_values = sage_estimator(X_exp.values, y_exp, bar=False, verbose=False)
  587. sage_series = pd.Series(sage_values.values, index=X_bg.columns)
  588. sage_series_reindexed = sage_series.reindex(present_features, fill_value=0)
  589. if model_name == "MLP":
  590. model.cpu()
  591. for feature, value in sage_series_reindexed.items():
  592. sage_data.append(
  593. {
  594. "Fold": fold_idx,
  595. "Model": model_name,
  596. "Feature": feature,
  597. "SAGE": value,
  598. }
  599. )
  600. return pd.DataFrame(sage_data)
  601. def run_all_models(self):
  602. """Run all models with all feature selection methods"""
  603. print(f"{'=' * 10} Running all models {'=' * 10}")
  604. # Define all model configurations
  605. model_configs = [
  606. # No feature selection
  607. (
  608. "LASSO",
  609. "None",
  610. lambda: self.run_logistic_regression(alpha=1.0, model_name="LASSO"),
  611. ),
  612. (
  613. "Ridge",
  614. "None",
  615. lambda: self.run_logistic_regression(alpha=0.0, model_name="Ridge"),
  616. ),
  617. ("XGBoost", "None", lambda: self.run_xgboost()),
  618. ("MLP", "None", lambda: self.run_nn()),
  619. # Boruta feature selection
  620. (
  621. "LASSO",
  622. "Boruta",
  623. lambda: self.run_logistic_regression(
  624. alpha=1.0,
  625. selected_features=self.selected_features,
  626. model_name="LASSO",
  627. ),
  628. ),
  629. (
  630. "Ridge",
  631. "Boruta",
  632. lambda: self.run_logistic_regression(
  633. alpha=0.0,
  634. selected_features=self.selected_features,
  635. model_name="Ridge",
  636. ),
  637. ),
  638. (
  639. "XGBoost",
  640. "Boruta",
  641. lambda: self.run_xgboost(
  642. selected_features=self.selected_features
  643. ),
  644. ),
  645. (
  646. "MLP",
  647. "Boruta",
  648. lambda: self.run_nn(selected_features=self.selected_features),
  649. ),
  650. ]
  651. all_sage_results = []
  652. # Run models
  653. if self.perform_feature_selection:
  654. model_config_filtered = [
  655. m_config for m_config in model_configs if m_config[1] != "None"
  656. ]
  657. else:
  658. model_config_filtered = [
  659. m_config for m_config in model_configs if m_config[1] == "None"
  660. ]
  661. for model_name, _, model_func in model_config_filtered:
  662. y_pred_list, fitted_models = model_func()
  663. metrics = self.calculate_fold_metrics(y_pred_list, model_name)
  664. self.results_summary.append(metrics)
  665. if self.calc_feature_importance and self.perform_feature_selection:
  666. sage_df = self.compute_SAGE_values_across_folds(
  667. model_name,
  668. fitted_models,
  669. use_test_data=True,
  670. )
  671. all_sage_results.append(sage_df)
  672. print(f"Completed: {model_name}")
  673. # Convert to DataFrame
  674. self.results_summary_df = pd.DataFrame(self.results_summary)
  675. self.fold_results_df = pd.DataFrame(self.fold_results)
  676. if self.calc_feature_importance:
  677. self.sage_results_df = pd.concat(all_sage_results, ignore_index=True)
  678. # Save results
  679. self.results_summary_df.to_csv(
  680. f"{self.results_path}/model_performance_summary.csv", index=False
  681. )
  682. self.fold_results_df.to_csv(
  683. f"{self.results_path}/fold_results.csv", index=False
  684. )
  685. if self.calc_feature_importance:
  686. self.sage_results_df.to_csv(
  687. f"{self.results_path}/sage_values.csv", index=False
  688. )
  689. print("Results saved to CSV.")
  690. def export_selected_features(self):
  691. """Export selected features for each feature selection method"""
  692. print("Exporting selected features...")
  693. if self.perform_feature_selection:
  694. df_selected_features = pd.DataFrame(self.selected_features).T
  695. df_selected_features.columns = [
  696. f"Fold {i + 1}" for i in range(self.num_folds * self.num_repeats)
  697. ]
  698. df_selected_features.to_csv(
  699. f"{self.results_path}/selected_features.csv", index=False
  700. )
  701. print("Selected features exported to CSV.")
  702. def run_complete_analysis(self):
  703. """Run the complete analysis pipeline"""
  704. print("Starting complete ML analysis pipeline...")
  705. print(f"Data Path: {self.data_path}")
  706. print(f"Results Path: {self.results_path}")
  707. (
  708. print(f"Seed: {self.seed}")
  709. if self.num_repeats == 1
  710. else print(f"Seeds: {self.repeat_seeds}")
  711. )
  712. print(
  713. f"Will {'not ' if not self.perform_feature_selection else ''}perform feature selection"
  714. )
  715. print(
  716. f"Will {'not ' if not self.calc_feature_importance else ''}calculate feature importance"
  717. )
  718. print()
  719. # Load and prepare data
  720. self.load_and_prepare_data()
  721. if self.export_prepared_data:
  722. self.export_prepared_dataset()
  723. # Prepare CV folds
  724. self.prepare_folds()
  725. # Run feature selection
  726. if self.perform_feature_selection:
  727. self.run_feature_selection()
  728. # Export selected features
  729. if self.perform_feature_selection:
  730. self.export_selected_features()
  731. # Run all models
  732. self.run_all_models()
  733. # Create summary table
  734. best_models = self.results_summary_df.reset_index(drop=True).sort_values(
  735. by="AUPRC_mean", ascending=False
  736. )
  737. print("\nResults:")
  738. print(
  739. best_models[
  740. [
  741. "Model",
  742. "AUPRC_mean",
  743. "AUROC_mean",
  744. "Accuracy_mean",
  745. "Sensitivity_mean",
  746. "Specificity_mean",
  747. "Kappa_mean",
  748. "F1_mean",
  749. ]
  750. ]
  751. )
  752. print("=== Analysis Complete ===")
  753. return self.results_summary_df

ModelComparer.py at commit 6e5adc5, under MIT · at the source

Overview

Authors: Jesper Olsson1, Tara M. Stanne1,2, Björn Andersson3, Christina Jern1,2
ORCID iDs: Jesper Olsson
  1. Department of Laboratory Medicine, Institute of Biomedicine, Sahlgrenska Academy, University of Gothenburg,Box 440, 405 30 Gothenburg, Sweden
  2. Department of Clinical Genetics and Genomics, Sahlgrenska University Hospital,Gothenburg, Region Västra Götaland Sweden
  3. Bioinformatics and Data Centre, University of Gothenburg,Gothenburg, Sweden
Journal: Scientific reports, volume 16, issue 1, article 12462
Dates: received 18 December 2025; accepted 3 April 2026; published online 15 April 2026
Type: Research article · Language: English
License: CC BY
Identifiers: DOI 10.1038/s41598-026-47814-x · PMID 41986523 · PMCID PMC13083973 · OpenAlex W7154504234
Open access: gold, a free copy (OpenAlex)
Status: code verified
Categories: human (organism), stroke (population), clinical / translational (subfield)
Methods: Statistics, Machine learning
Keywords: Biomarkers, Medical research, Neurology, Neuroscience
MeSH: Ischemic Stroke*, Machine Learning*, Stroke*, Adolescent, Adult, Aged, Biomarkers, Boosting Machine Learning Algorithms, Female, Humans, Male, Middle Aged, Prediction Algorithms, Predictive Learning Models, Prognosis, Young Adult (* major topic)
Topic: Acute Ischemic Stroke Management (Epidemiology, Medicine), according to OpenAlex
Funding: University of Gothenburg
Citations: not cited yet (Europe PMC); 45 references in the paper

Abstract

Functional outcome after acute ischemic stroke (AIS) varies widely, and existing prognostic scores may not capture complex relationships. We evaluated a diverse set of clinical characteristics and blood biomarkers with multiple machine learning models to predict 3-month functional outcome after AIS, and used explainable artificial intelligence to identify key drivers of performance. Models were trained on 506 patients aged 18–69 years with AIS enrolled at four stroke units in western Sweden. We compared extreme gradient boosting, multilayer perceptron (MLP), and L1- and L2-regularized logistic regression. Feature importance was assessed with Shapley additive global explanations. Of the 506 patients, 105 had an unfavorable outcome (modified Rankin Scale score > 2). All models showed high area under the curve (AUROC, 0.900–0.906). The MLP achieved the highest precision–recall performance (AUPRC, 0.773 ± 0.080) and sensitivity (0.655 ± 0.096), though with lower specificity (0.920 ± 0.035). Stroke severity (NIH Stroke Scale score) was the dominant predictor across models. Among biomarkers, brain-derived tau (BD-tau) was most informative, followed by inflammation-related plasma proteins. In conclusion, machine learning accurately predicted functional outcome after AIS. BD-tau and inflammation-related proteins contributed predictive information above stroke severity, suggesting a potential for blood biomarkers to enhance individualized prognostication after AIS.

Supplementary Information: The online version contains supplementary material available at 10.1038/s41598-026-47814-x.

Reproduced under the paper's license (CC BY), from the paper cited above.

Repository

Its files are read in the Code ↔ Paper reader above, with 8 matches between paragraphs and lines of code.

jespeols/predicting_mRS_xML_integrated_data

License: MIT
State: the link answers, verified on 29 September 2026
Evidence: files inventoried
Commit: 6e5adc52e7fd25f6e09eccd0a6510f6569b87c57, 21 April 2026
Languages: Python (3), Quarto (3), Jupyter (1), R (1)
Size: 24 files, 8 scripts
Software Heritage: not archived
Found in: “Code availability”
Holds: README, license file, environment (environment.yml), 4 notebooks
Not found: CITATION.cff, tests, continuous integration, documentation
Tools: PyTorch (3 files), NumPy (2 files), pandas (2 files), tidyverse (2 files), Matplotlib (1 file), scikit-learn (1 file), XGBoost (1 file)
Availability: 1 check, the latest on 29 September 2026: the link answers
  • 29 September 2026: the link answers
10 files

Code availability

Source code for this article is available in the following Github repository: https://github.com/jespeols/predicting_mRS_xML_integrated_data.

Reproduced under the paper's license (CC BY), from the paper cited above.

Tracing map

Proposed by the machine: these links were found in the paper and verified at the source, without human review. The map will receive a Zenodo DOI once one of the paper's authors has validated it with their ORCID.

What the map holds:

  • 1 repository of the authors' code, each at its verified commit, with its license and how the link was found in the paper;
  • 8 scripts, each with its path and the digest of its content;
  • 8 matches between paragraphs of the paper and lines of the code (method lexical-v1);
  • neither the text of the paper nor the code itself.

Its JSON (tracing-map.json) is deposited on Zenodo with its DOI once the map is validated.

Data

No dataset and no data link were found in the paper.

Data availability

Anonymized data will be shared upon reasonable request, provided data transfer agrees with EU legislation on the general data protection regulation and with decisions by the Ethical Review Board of Sweden and the University of Gothenburg, the latter which should be regulated in a data transfer agreement.

Reproduced under the paper's license (CC BY), from the paper cited above.

Versions

The history of this record: each version stored by the harvester or made by a correction of its authors or of the maintainers of its code, and what changed in its facts. The texts of the paper (its abstract, its availability statements) are not part of it; versions that changed only those are not listed.

Version 1, 29 September 2026: the first record

Recorded: type, language, journal, volume, issue, pages, dates, 4 authors, 4 keywords, 16 MeSH terms, 1 funder, 43 references.

Cite

This paper

Olsson, J., Stanne, T. M., Andersson, B., & Jern, C. (2026). Predicting post-stroke functional outcome using explainable machine learning and integrated data. Scientific reports, 16(1), 12462. https://doi.org/10.1038/s41598-026-47814-x

BibTeX

@article{olsson2026predicting,
author = {Olsson, Jesper and Stanne, Tara M. and Andersson, Björn and Jern, Christina},
title = {{Predicting post-stroke functional outcome using explainable machine learning and integrated data}},
journal = {Scientific reports},
year = {2026},
month = apr,
volume = {16},
number = {1},
pages = {12462},
publisher = {Nature Publishing Group},
issn = {2045-2322},
doi = {10.1038/s41598-026-47814-x},
url = {https://doi.org/10.1038/s41598-026-47814-x},
pmid = {41986523},
pmcid = {PMC13083973}
}

RIS

TY - JOUR
AU - Olsson, Jesper
AU - Stanne, Tara M.
AU - Andersson, Björn
AU - Jern, Christina
TI - Predicting post-stroke functional outcome using explainable machine learning and integrated data
T2 - Scientific reports
J2 - Sci Rep
PY - 2026
DA - 2026/04/15
VL - 16
IS - 1
SP - 12462
SN - 2045-2322
PB - Nature Publishing Group
DO - 10.1038/s41598-026-47814-x
UR - https://doi.org/10.1038/s41598-026-47814-x
LA - en
ER -

CSL-JSON

{
"id": "10.1038/s41598-026-47814-x",
"type": "article-journal",
"title": "Predicting post-stroke functional outcome using explainable machine learning and integrated data",
"container-title": "Scientific reports",
"author": [
{
"family": "Olsson",
"given": "Jesper"
},
{
"family": "Stanne",
"given": "Tara M."
},
{
"family": "Andersson",
"given": "Björn"
},
{
"family": "Jern",
"given": "Christina"
}
],
"container-title-short": "Sci Rep",
"volume": "16",
"issue": "1",
"page": "12462",
"DOI": "10.1038/s41598-026-47814-x",
"PMID": "41986523",
"PMCID": "PMC13083973",
"ISSN": "2045-2322",
"publisher": "Nature Publishing Group",
"URL": "https://doi.org/10.1038/s41598-026-47814-x",
"language": "en",
"issued": {
"date-parts": [
[
2026,
4,
15
]
]
}
}

The tracing map gets a citation of its own once an author has validated it and it has a DOI.

Similar papers

The papers with a page that share the most with this one: the tools found in their code, their categories, datasets, cited references and authors, the rarest counting most.

[1] doi:10.1186/s13073-026-01704-z [code]
Gene expression profiling enables refined parcellation of cortical layers in the heterogeneous human cerebral cortex.
Journal: Genome medicine
In common: XGBoost, PyTorch, tidyverse, 4 other tools, 1 reference
[2] doi:10.3390/e28050562 [code]
A Comparative Analysis of Explainable AI (XAI) Techniques for Transparent and Reliable Image Classification.
Journal: Entropy (Basel, Switzerland)
In common: PyTorch, scikit-learn, pandas, 2 other tools, 2 references
[3] doi:10.1038/s41514-026-00456-9 [code]
Exploring the link between body physiology and cognition: the role of the brain and aging.
Journal: npj aging
In common: XGBoost, tidyverse, scikit-learn, 3 other tools, 1 reference
[4] doi:10.1038/s42003-026-10957-8 [code]
Brain defence by the extracellular matrix protein Cochlin.
Journal: Communications biology
In common: XGBoost, PyTorch, tidyverse, 4 other tools
[5] doi:10.1038/s41467-026-76837-1 [code]
Drug screen and machine learning predict neuroprotective agents in a preclinical human model of childhood dementia.
Journal: Nature communications
In common: XGBoost, PyTorch, tidyverse, 4 other tools
[6] doi:10.1186/s13059-026-04125-8 [code]
MLMarker: a machine learning framework for tissue inference and biomarker discovery.
Journal: Genome biology
In common: XGBoost, PyTorch, tidyverse, 4 other tools
[7] doi:10.1371/journal.pgen.1012242 [code]
Wiz regulates clustered protocadherin genes by restricting CTCF/cohesin loop extrusion in a genomic-distance biased manner.
Journal: PLoS genetics
In common: XGBoost, PyTorch, scikit-learn, 3 other tools, 1 reference
[8] doi:10.1016/j.phro.2026.101056 [code]
Toward uncertainty-aware manual delineation of brain tumours using eye-tracking and image-derived features.
Journal: Physics and imaging in radiation oncology
In common: XGBoost, PyTorch, tidyverse, 4 other tools
[9] doi:10.1038/s42003-026-10462-y [code]
SpaDC enables sequence-based integrative analysis and regulatory inference of spatial chromatin accessibility data.
Journal: Communications biology
In common: XGBoost, PyTorch, tidyverse, 4 other tools
[10] doi:10.1038/s41598-026-47942-4 [code]
CT-based radiomic markers to predict late-onset seizures after traumatic brain injury.
Journal: Scientific reports
In common: XGBoost, scikit-learn, pandas, 2 other tools, clinical / translational, 1 reference

Contribute

The authors of this paper can claim it, correct its record and validate its tracing map, and the maintainers of its code (its owner, or a public member of its organization) correct what it says of their repository; anyone signed in can ask for its removal. Every request goes to OSCR's own machine, which answers it; your account page follows them.

Sign in with ORCID to claim this paper as one of its authors, correct its record or validate its tracing map: when the paper's metadata lists your ORCID iD, you are recognized at once. Maintainers of its code: sign in with GitHub, then claim the repository on your account page.

Request its removal

To ask OSCR to remove this record, the copies of its authors' scripts or its tracing map, use the removal request page: signed in, you say who you are, what to remove and why, then review and confirm the request. Published rules decide every request (how).

Discussion, reproductions, activity

Discussion: questions and error reports about this paper and its code, from signed-in readers and its authors. It opens with sign-in.

Reproductions: reports from readers who ran the authors' code: what they reproduced, with which environment, commit and data. It opens with sign-in.

Activity: what happens around this paper: new versions of its record, its map's validation, discussions and reproductions. It opens with sign-in.