Predicting post-stroke functional outcome using explainable machine learning and integrated data.
The 8 matches
- [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] § 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] § Methods › Computational workflow › Data preprocessing ↔ code/ModelComparer.py, lines 184–227 · score 0.74 · hot encoded, Numerical features, kNN, neighbors, imputed, binary
- [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] § Methods › Computational workflow › Machine learning workflow ↔ code/ModelComparer.py, lines 393–441 · score 0.61 · randomized search, XGBoost, trees, tuned, classification, boosted
- [6] § Methods › Computational workflow › Machine learning workflow ↔ code/ModelComparer.py, lines 334–391 · score 0.60 · Logistic regression, L1, L2, penalty, Ridge, tuned
- [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] § 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
- import os
- import pandas as pd
- import numpy as np
- import xgboost as xgb
- import sage
- import torch
- import torch.nn as nn
- import torch.optim as optim
- from pathlib import Path
- from datetime import datetime
- from copy import deepcopy
- from torch.utils.data import DataLoader, TensorDataset
- from boruta import BorutaPy
- from sklearn.model_selection import (
- StratifiedKFold,
- GridSearchCV,
- RandomizedSearchCV,
- train_test_split,
- )
- from sklearn.preprocessing import (
- StandardScaler,
- OneHotEncoder,
- )
- from sklearn.impute import KNNImputer, SimpleImputer
- from sklearn.linear_model import LogisticRegression
- from sklearn.ensemble import RandomForestClassifier
- from sklearn.metrics import (
- accuracy_score,
- precision_score,
- recall_score,
- f1_score,
- roc_auc_score,
- average_precision_score,
- cohen_kappa_score,
- confusion_matrix,
- )
- from sklearn.pipeline import Pipeline
- from sklearn.compose import ColumnTransformer
- # User-defined functions
- from MLPClassifier import MLPClassifier
- BASE_DIR = Path(os.path.abspath(__file__)).parent.parent
- class ModelComparer:
- def __init__(
- self,
- data_path="data",
- results_path=None,
- export_prepared_data=True,
- calc_feature_importance=True,
- exclude_vars=[],
- num_folds=5,
- num_repeats=5,
- perform_feature_selection=True,
- hyperparam_scoring="neg_log_loss",
- boruta_max_iter=100,
- seed=2025,
- ):
- self.seed = seed
- self.repeat_seeds = [seed + i for i in range(num_repeats)]
- np.random.seed(seed)
- torch.manual_seed(seed)
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
- self.data_path = data_path
- self.export_prepared_data = export_prepared_data # Whether to export prepared data (before preprocessing)
- self.results_path = (
- results_path
- if results_path
- else os.path.join(
- BASE_DIR, f"results/exp_{datetime.now().strftime('%Y%m%d-%H%M%S')}"
- )
- )
- self.num_folds = num_folds
- self.num_repeats = num_repeats
- self.calc_feature_importance = calc_feature_importance
- self.perform_feature_selection = perform_feature_selection
- self.boruta_max_iter = boruta_max_iter
- self.hyperparam_scoring = hyperparam_scoring
- self.exclude_vars = [] if exclude_vars is None else exclude_vars
- assert self.num_repeats > 0, "Number of repeats must be at least 1."
- # Create results directory if it doesn't exist
- os.makedirs(self.results_path, exist_ok=True)
- # Initialize results storage
- self.results_summary = []
- self.fold_results = [] # Model performance results for each fold
- def load_and_prepare_data(self):
- """Load and prepare data similar to R script"""
- print("Loading and preparing data...")
- # Load data
- self.raw_data = pd.read_csv(f"{self.data_path}/sahlsis.csv", index_col=0)
- self.metadata = pd.read_csv(f"{self.data_path}/sahlsis_meta.csv")
- # Identify variable types
- scalar_vars = self.metadata[self.metadata["type"] == "scalar"]["name"].tolist()
- categorical_vars = self.metadata[self.metadata["type"] == "categorical"][
- "name"
- ].tolist()
- binary_vars = self.metadata[self.metadata["type"] == "binary"]["name"].tolist()
- non_predictors = [
- "MRS",
- "MRS_dict",
- "Stroke",
- ] # Outcome and recurrence variables
- filter_vars = non_predictors + self.exclude_vars
- # Convert factor features
- for var in categorical_vars + binary_vars:
- if var in self.raw_data.columns:
- self.raw_data[var] = self.raw_data[var].astype("category")
- for var in scalar_vars:
- if var in self.raw_data.columns:
- self.raw_data[var] = pd.to_numeric(self.raw_data[var], errors="coerce")
- self.raw_data[var] = self.raw_data[var].astype("float64")
- # Remove rows and columns with too many missing values
- row_cutoff = 0.2
- col_cutoff = 0.3
- row_missing_rate = (
- self.raw_data.loc[self.raw_data["MRS_dict"].notna()].isna().mean(axis=1)
- )
- col_missing_rate = (
- self.raw_data.loc[self.raw_data["MRS_dict"].notna()].isna().mean(axis=0)
- )
- print(
- f"Removing {sum(self.raw_data['MRS_dict'].isna())} cases with missing mRS."
- )
- print(
- f"Removing {sum(row_missing_rate > row_cutoff)} cases with more than {row_cutoff:.0%} missing values."
- )
- print(
- f"Removing {sum(self.raw_data['Stroke'] == 1)} cases with recurrent stroke."
- )
- print(
- f"Removing {sum(col_missing_rate > col_cutoff)} features with more than {col_cutoff:.0%} missing values."
- )
- self.data = (
- self.raw_data.loc[self.raw_data["MRS_dict"].notna()]
- .loc[row_missing_rate <= row_cutoff]
- .loc[:, col_missing_rate <= col_cutoff]
- .loc[self.raw_data["Stroke"] != 1]
- )
- # Handle target variable
- target_map = {"Favorable": 0, "Unfavorable": 1}
- self.data["MRS_dict"] = self.data["MRS_dict"].map(target_map).astype(int)
- # Prepare features and target
- if self.exclude_vars != []:
- print(f"Filtering features: {self.exclude_vars}")
- self.X = self.data.drop(filter_vars, axis=1)
- self.y = self.data["MRS_dict"]
- print(f"Data shape after cleaning: {self.X.shape}")
- print(
- f"Class distribution: {self.y.value_counts(normalize=True).round(2).to_dict()}"
- )
- print("Data preparation complete.")
- def get_prepared_data(self):
- """Get prepared features and target"""
- return self.X, self.y
- def export_prepared_dataset(
- self, return_X_y=False, export_path=None, save_csv=True
- ):
- """Export prepared dataset before preprocessing"""
- if not export_path:
- export_path = os.path.join(self.results_path, "prepared_data.csv")
- prepared_data = self.data.copy()
- if save_csv:
- prepared_data.to_csv(export_path, index=True)
- print(f"Prepared dataset exported to {export_path}\n")
- if return_X_y:
- return self.X, self.y
- def create_preprocessor(self):
- """Create preprocessing pipeline"""
- # Identify numeric and categorical columns
- numeric_features = self.X.select_dtypes(include=[np.number]).columns.tolist()
- factor_features = self.X.select_dtypes(include=["category"]).columns.tolist()
- imp_num = KNNImputer(n_neighbors=5)
- # Create preprocessing pipelines
- numeric_transformer = Pipeline(
- steps=[
- ("scaler", StandardScaler()),
- ("imputer", imp_num),
- ]
- )
- def feature_name_combiner(input_feature, category):
- return f"{input_feature}::{category}"
- # For categorical features: impute missing with most frequent, then OneHotEncode and scale
- imp_cat = SimpleImputer(strategy="most_frequent")
- factor_transformer = Pipeline(
- steps=[
- ("imputer", imp_cat),
- (
- "onehot",
- OneHotEncoder(
- drop="if_binary",
- handle_unknown="ignore",
- sparse_output=False,
- feature_name_combiner=feature_name_combiner,
- ),
- ),
- ("scaler", StandardScaler()),
- ]
- )
- # Combine preprocessing steps
- preprocessor = ColumnTransformer(
- transformers=[
- ("num", numeric_transformer, numeric_features),
- ("cat", factor_transformer, factor_features),
- ]
- )
- return preprocessor
- def prepare_folds(self):
- """Prepare repeated cross-validation folds with preprocessing."""
- print("Preparing repeated cross-validation folds...")
- self.fold_data = []
- for repeat_idx, seed in enumerate(self.repeat_seeds):
- cv = StratifiedKFold(
- n_splits=self.num_folds, shuffle=True, random_state=seed
- )
- for fold_idx, (train_idx, test_idx) in enumerate(cv.split(self.X, self.y)):
- X_train_fold = self.X.iloc[train_idx]
- X_test_fold = self.X.iloc[test_idx]
- y_train_fold = self.y.iloc[train_idx]
- y_test_fold = self.y.iloc[test_idx]
- preprocessor = self.create_preprocessor()
- X_train_processed = preprocessor.fit_transform(X_train_fold)
- X_test_processed = preprocessor.transform(X_test_fold)
- # Get feature names
- feature_names = []
- numeric_features = X_train_fold.select_dtypes(
- include=[np.number]
- ).columns.tolist()
- feature_names.extend(numeric_features)
- factor_features = X_train_fold.select_dtypes(
- include=["category", "object"]
- ).columns.tolist()
- cat_pipe = preprocessor.named_transformers_["cat"]
- cat_encoder = cat_pipe.named_steps["onehot"]
- cat_feature_names_out = cat_encoder.get_feature_names_out(
- factor_features
- )
- feature_names.extend(cat_feature_names_out)
- X_train_processed = pd.DataFrame(
- X_train_processed, columns=feature_names, index=X_train_fold.index
- )
- X_test_processed = pd.DataFrame(
- X_test_processed, columns=feature_names, index=X_test_fold.index
- )
- self.fold_data.append(
- {
- "X_train": X_train_processed,
- "X_test": X_test_processed,
- "y_train": y_train_fold.values,
- "y_test": y_test_fold.values,
- }
- )
- self.unique_features = np.unique(
- np.concatenate(
- [fold["X_train"].columns.tolist() for fold in self.fold_data]
- )
- ).tolist()
- print(
- f"Prepared {self.num_folds} folds across {len(self.repeat_seeds)} repeats (total {len(self.fold_data)} fold iterations).\n"
- )
- def run_feature_selection(self):
- """Run Boruta feature selection on each fold"""
- print("Starting Boruta feature selection...")
- print(f"Running for a maximum of {self.boruta_max_iter} iterations per fold.")
- self.selected_features = []
- self.selected_indices = []
- for fold_idx in range(len(self.fold_data)):
- fold = self.fold_data[fold_idx]
- X_train = fold["X_train"]
- y_train = fold["y_train"]
- rf = RandomForestClassifier(n_jobs=-1, random_state=self.seed)
- boruta_selector = BorutaPy(
- rf,
- n_estimators="auto",
- verbose=0,
- random_state=self.seed,
- max_iter=self.boruta_max_iter,
- )
- boruta_selector.fit(X_train, y_train)
- selected_features = [
- X_train.columns[i]
- for i in range(len(X_train.columns))
- if boruta_selector.support_[i] or boruta_selector.support_weak_[i]
- ]
- self.selected_features.append(selected_features)
- selected_indices = [
- X_train.columns.get_loc(feature) for feature in selected_features
- ]
- self.selected_indices.append(selected_indices)
- print(
- f"Boruta selected {len(selected_features)} features for fold {fold_idx + 1}"
- )
- print(
- f"{sum(boruta_selector.support_)} confirmed, {sum(boruta_selector.support_weak_)} tentative."
- )
- self.unique_features = np.unique(
- np.concatenate(self.selected_features)
- ).tolist()
- print("Boruta feature selection complete.")
- def run_logistic_regression(
- self, alpha=1.0, selected_features=None, model_name="Logistic"
- ):
- """Run logistic regression (LASSO/Ridge based on alpha)"""
- print(f"Running {model_name}...")
- y_pred_list = []
- fitted_models = []
- for fold_idx in range(len(self.fold_data)):
- fold = self.fold_data[fold_idx]
- X_train = fold["X_train"]
- X_test = fold["X_test"]
- y_train = fold["y_train"]
- # Feature selection if provided
- if selected_features is not None:
- X_train = X_train.loc[:, selected_features[fold_idx]]
- X_test = X_test.loc[:, selected_features[fold_idx]]
- # Model with cross-validation for C parameter
- if alpha == 1.0: # LASSO
- model = LogisticRegression(
- penalty="l1",
- solver="liblinear",
- random_state=self.seed,
- max_iter=1000,
- )
- elif alpha == 0.0: # Ridge
- model = LogisticRegression(
- penalty="l2",
- random_state=self.seed,
- max_iter=1000,
- )
- else: # Elastic Net
- model = LogisticRegression(
- penalty="elasticnet",
- l1_ratio=alpha,
- solver="saga",
- random_state=self.seed,
- max_iter=1000,
- )
- # Hyperparameter tuning
- param_grid = {"C": np.logspace(-4, 4, 100)}
- grid_search = GridSearchCV(
- model,
- param_grid,
- cv=5,
- scoring=self.hyperparam_scoring,
- n_jobs=-1,
- )
- grid_search.fit(X_train.values, y_train)
- fitted_models.append(grid_search.best_estimator_)
- # Predict
- y_pred_proba = grid_search.predict_proba(X_test.values)[:, 1]
- y_pred_list.append(y_pred_proba)
- return y_pred_list, fitted_models
- def run_xgboost(self, selected_features=None):
- """Run XGBoost"""
- print("Running XGBoost...")
- y_pred_list = []
- fitted_models = []
- for fold_idx in range(len(self.fold_data)):
- fold = self.fold_data[fold_idx]
- X_train = fold["X_train"]
- X_test = fold["X_test"]
- y_train = fold["y_train"]
- # Feature selection if provided
- if selected_features is not None:
- X_train = X_train.loc[:, selected_features[fold_idx]]
- X_test = X_test.loc[:, selected_features[fold_idx]]
- # Hyperparameter tuning
- param_grid = {
- "n_estimators": [100, 200, 300, 500],
- "max_depth": [3, 5, 7],
- "learning_rate": [0.05, 0.1, 0.2, 0.3],
- "subsample": [0.8, 0.9, 1.0],
- "colsample_bytree": [0.8, 0.9, 1.0],
- "reg_lambda": [0, 0.5, 1.0, 5.0, 10.0],
- "min_child_weight": [1, 3, 5, 7, 10],
- }
- xgb_model = xgb.XGBClassifier(
- tree_method="hist",
- seed=self.seed,
- eval_metric="logloss",
- # max_delta_step=1,
- )
- grid_search = RandomizedSearchCV(
- xgb_model,
- param_grid,
- n_iter=150,
- cv=5,
- scoring=self.hyperparam_scoring,
- random_state=self.seed,
- )
- grid_search.fit(X_train, y_train)
- fitted_models.append(grid_search.best_estimator_)
- # Predict
- y_pred_proba = grid_search.predict_proba(X_test)[:, 1]
- y_pred_list.append(y_pred_proba)
- return y_pred_list, fitted_models
- def run_nn(self, selected_features=None):
- """Run Neural Network"""
- print("Running Neural Network...")
- torch.cuda.empty_cache()
- dropout_prob = 0.5
- lr = 0.001
- weight_decay = 0.0001
- num_epochs = 100
- patience = 7
- min_delta = 1e-6
- batch_size = 32
- y_pred_list = []
- fitted_models = []
- for fold_idx in range(len(self.fold_data)):
- fold = self.fold_data[fold_idx]
- X_train = fold["X_train"]
- X_test = fold["X_test"]
- y_train = fold["y_train"]
- # Feature selection if provided
- if selected_features is not None:
- X_train = X_train.loc[:, selected_features[fold_idx]]
- X_test = X_test.loc[:, selected_features[fold_idx]]
- # Prepare dataset
- X_train, X_val, y_train, y_val = train_test_split(
- X_train, y_train, test_size=0.1, random_state=self.seed,
- # stratify=y_train # NOTE: Not present in submitted results
- )
- train_dataset = TensorDataset(
- torch.tensor(X_train.values, dtype=torch.float32),
- torch.tensor(y_train, dtype=torch.float32),
- )
- train_loader = DataLoader(
- train_dataset, batch_size=batch_size, shuffle=True
- )
- X_val_tensor = torch.tensor(X_val.values, dtype=torch.float32)
- y_val_tensor = torch.tensor(y_val, dtype=torch.float32)
- X_test_tensor = torch.tensor(X_test.values, dtype=torch.float32)
- model = MLPClassifier(
- input_size=X_train.shape[1], dropout_prob=dropout_prob
- )
- model.to(self.device)
- criterion = nn.BCEWithLogitsLoss()
- optimizer = optim.AdamW(
- model.parameters(), lr=lr, weight_decay=weight_decay
- )
- # For early stopping
- best_val_loss = float("inf")
- patience_counter = 0
- best_model_state = None
- train_losses = []
- val_losses = []
- # Training loop
- for epoch in range(num_epochs):
- model.train()
- train_epoch_loss = 0.0
- for inputs, labels in train_loader:
- inputs = inputs.to(self.device)
- labels = labels.to(self.device)
- optimizer.zero_grad()
- outputs = model(inputs).squeeze()
- loss = criterion(outputs, labels)
- loss.backward()
- optimizer.step()
- train_epoch_loss += loss.item()
- train_epoch_loss /= len(train_loader)
- train_losses.append(train_epoch_loss)
- model.eval()
- val_outputs = model(X_val_tensor.to(self.device)).squeeze()
- val_loss = criterion(val_outputs, y_val_tensor.to(self.device))
- val_losses.append(val_loss.item())
- if val_loss < best_val_loss - min_delta:
- best_val_loss = val_loss
- patience_counter = 0
- best_model_state = deepcopy(model.state_dict())
- else:
- patience_counter += 1
- if patience_counter >= patience:
- break
- # Load best model
- model.load_state_dict(best_model_state)
- # Predict
- model.eval()
- with torch.no_grad():
- y_pred_proba = (
- torch.sigmoid(model(X_test_tensor.to(self.device)))
- .squeeze()
- .cpu()
- .numpy()
- )
- y_pred_list.append(y_pred_proba)
- fitted_models.append(model.cpu())
- return y_pred_list, fitted_models
- def evaluate_metrics(self, y_true, y_pred_proba, threshold=0.5):
- """Calculate comprehensive evaluation metrics"""
- y_pred = (y_pred_proba >= threshold).astype(int)
- # Calculate metrics
- accuracy = accuracy_score(y_true, y_pred)
- sensitivity = recall_score(y_true, y_pred)
- tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
- specificity = tn / (tn + fp) if (tn + fp) > 0 else 0
- ppv = precision_score(y_true, y_pred, zero_division=0)
- npv = tn / (tn + fn) if (tn + fn) > 0 else 0
- kappa = cohen_kappa_score(y_true, y_pred)
- f1 = f1_score(y_true, y_pred)
- auroc = roc_auc_score(y_true, y_pred_proba)
- auprc = average_precision_score(y_true, y_pred_proba)
- return {
- "Accuracy": accuracy,
- "Sensitivity": sensitivity,
- "Specificity": specificity,
- "PPV": ppv,
- "NPV": npv,
- "F1": f1,
- "Kappa": kappa,
- "AUROC": auroc,
- "AUPRC": auprc,
- }
- def calculate_fold_metrics(
- self, y_pred_list, model_name
- ): # TODO: Add train metrics
- """Calculate metrics across all folds"""
- fold_metrics = []
- for fold_idx in range(len(self.fold_data)):
- y_true = self.fold_data[fold_idx]["y_test"]
- y_pred_proba = y_pred_list[fold_idx]
- metrics = self.evaluate_metrics(y_true, y_pred_proba)
- self.fold_results.append(
- {
- "Fold": fold_idx,
- "Model": model_name,
- **metrics,
- }
- )
- fold_metrics.append(metrics)
- # Calculate mean and std across folds
- metrics_df = pd.DataFrame(fold_metrics)
- summary = {}
- summary["Model"] = model_name
- for metric in metrics_df.columns:
- summary[f"{metric}_mean"] = metrics_df[metric].mean()
- summary[f"{metric}_sd"] = metrics_df[metric].std()
- return summary
- def compute_SAGE_values_across_folds(
- self, model_name, fitted_models, use_test_data=True
- ):
- """Compute SAGE values across all folds"""
- sage_data = []
- if self.perform_feature_selection:
- selected_indices = self.selected_indices
- present_features = self.unique_features
- else:
- selected_indices = None
- present_features = self.unique_features
- for fold_idx in range(len(self.fold_data)):
- fold = self.fold_data[fold_idx]
- if self.perform_feature_selection:
- current_features = fold["X_train"].columns[selected_indices[fold_idx]]
- else:
- current_features = fold["X_train"].columns
- X_bg = (
- fold["X_train"]
- if not selected_indices
- else fold["X_train"].loc[:, current_features]
- )
- if use_test_data:
- X_exp = (
- fold["X_test"]
- if not selected_indices
- else fold["X_test"].loc[:, current_features]
- )
- y_exp = fold["y_test"]
- else:
- X_exp = (
- fold["X_train"]
- if not selected_indices
- else fold["X_train"].loc[:, current_features]
- )
- y_exp = fold["y_train"]
- if model_name == "MLP":
- torch.cuda.empty_cache()
- model = fitted_models[fold_idx]
- model.sigmoid_output = True
- model.to(self.device)
- model.eval()
- n_jobs = 4
- else:
- model = fitted_models[fold_idx]
- n_jobs = -2
- sage_imputer = sage.MarginalImputer(fitted_models[fold_idx], X_bg.values)
- sage_estimator = sage.PermutationEstimator(
- sage_imputer, n_jobs=n_jobs, random_state=self.seed
- )
- sage_values = sage_estimator(X_exp.values, y_exp, bar=False, verbose=False)
- sage_series = pd.Series(sage_values.values, index=X_bg.columns)
- sage_series_reindexed = sage_series.reindex(present_features, fill_value=0)
- if model_name == "MLP":
- model.cpu()
- for feature, value in sage_series_reindexed.items():
- sage_data.append(
- {
- "Fold": fold_idx,
- "Model": model_name,
- "Feature": feature,
- "SAGE": value,
- }
- )
- return pd.DataFrame(sage_data)
- def run_all_models(self):
- """Run all models with all feature selection methods"""
- print(f"{'=' * 10} Running all models {'=' * 10}")
- # Define all model configurations
- model_configs = [
- # No feature selection
- (
- "LASSO",
- "None",
- lambda: self.run_logistic_regression(alpha=1.0, model_name="LASSO"),
- ),
- (
- "Ridge",
- "None",
- lambda: self.run_logistic_regression(alpha=0.0, model_name="Ridge"),
- ),
- ("XGBoost", "None", lambda: self.run_xgboost()),
- ("MLP", "None", lambda: self.run_nn()),
- # Boruta feature selection
- (
- "LASSO",
- "Boruta",
- lambda: self.run_logistic_regression(
- alpha=1.0,
- selected_features=self.selected_features,
- model_name="LASSO",
- ),
- ),
- (
- "Ridge",
- "Boruta",
- lambda: self.run_logistic_regression(
- alpha=0.0,
- selected_features=self.selected_features,
- model_name="Ridge",
- ),
- ),
- (
- "XGBoost",
- "Boruta",
- lambda: self.run_xgboost(
- selected_features=self.selected_features
- ),
- ),
- (
- "MLP",
- "Boruta",
- lambda: self.run_nn(selected_features=self.selected_features),
- ),
- ]
- all_sage_results = []
- # Run models
- if self.perform_feature_selection:
- model_config_filtered = [
- m_config for m_config in model_configs if m_config[1] != "None"
- ]
- else:
- model_config_filtered = [
- m_config for m_config in model_configs if m_config[1] == "None"
- ]
- for model_name, _, model_func in model_config_filtered:
- y_pred_list, fitted_models = model_func()
- metrics = self.calculate_fold_metrics(y_pred_list, model_name)
- self.results_summary.append(metrics)
- if self.calc_feature_importance and self.perform_feature_selection:
- sage_df = self.compute_SAGE_values_across_folds(
- model_name,
- fitted_models,
- use_test_data=True,
- )
- all_sage_results.append(sage_df)
- print(f"Completed: {model_name}")
- # Convert to DataFrame
- self.results_summary_df = pd.DataFrame(self.results_summary)
- self.fold_results_df = pd.DataFrame(self.fold_results)
- if self.calc_feature_importance:
- self.sage_results_df = pd.concat(all_sage_results, ignore_index=True)
- # Save results
- self.results_summary_df.to_csv(
- f"{self.results_path}/model_performance_summary.csv", index=False
- )
- self.fold_results_df.to_csv(
- f"{self.results_path}/fold_results.csv", index=False
- )
- if self.calc_feature_importance:
- self.sage_results_df.to_csv(
- f"{self.results_path}/sage_values.csv", index=False
- )
- print("Results saved to CSV.")
- def export_selected_features(self):
- """Export selected features for each feature selection method"""
- print("Exporting selected features...")
- if self.perform_feature_selection:
- df_selected_features = pd.DataFrame(self.selected_features).T
- df_selected_features.columns = [
- f"Fold {i + 1}" for i in range(self.num_folds * self.num_repeats)
- ]
- df_selected_features.to_csv(
- f"{self.results_path}/selected_features.csv", index=False
- )
- print("Selected features exported to CSV.")
- def run_complete_analysis(self):
- """Run the complete analysis pipeline"""
- print("Starting complete ML analysis pipeline...")
- print(f"Data Path: {self.data_path}")
- print(f"Results Path: {self.results_path}")
- (
- print(f"Seed: {self.seed}")
- if self.num_repeats == 1
- else print(f"Seeds: {self.repeat_seeds}")
- )
- print(
- f"Will {'not ' if not self.perform_feature_selection else ''}perform feature selection"
- )
- print(
- f"Will {'not ' if not self.calc_feature_importance else ''}calculate feature importance"
- )
- print()
- # Load and prepare data
- self.load_and_prepare_data()
- if self.export_prepared_data:
- self.export_prepared_dataset()
- # Prepare CV folds
- self.prepare_folds()
- # Run feature selection
- if self.perform_feature_selection:
- self.run_feature_selection()
- # Export selected features
- if self.perform_feature_selection:
- self.export_selected_features()
- # Run all models
- self.run_all_models()
- # Create summary table
- best_models = self.results_summary_df.reset_index(drop=True).sort_values(
- by="AUPRC_mean", ascending=False
- )
- print("\nResults:")
- print(
- best_models[
- [
- "Model",
- "AUPRC_mean",
- "AUROC_mean",
- "Accuracy_mean",
- "Sensitivity_mean",
- "Specificity_mean",
- "Kappa_mean",
- "F1_mean",
- ]
- ]
- )
- print("=== Analysis Complete ===")
- return self.results_summary_df
ModelComparer.py at commit 6e5adc5, under MIT · at the source
Overview
- Department of Laboratory Medicine, Institute of Biomedicine, Sahlgrenska Academy, University of Gothenburg,Box 440, 405 30 Gothenburg, Sweden
- Department of Clinical Genetics and Genomics, Sahlgrenska University Hospital,Gothenburg, Region Västra Götaland Sweden
- Bioinformatics and Data Centre, University of Gothenburg,Gothenburg, Sweden
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/
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
6e5adc52e7fd25f6e09eccd0a6510f6569b87c57, 21 April 2026Availability: 1 check, the latest on 29 September 2026: the link answers
- 29 September 2026: the link answers
10 files
- code/
MLPClassifier.py , Python, 45 lines - code/
ModelComparer.py , Python, 848 lines, 6 matches - code/
main.py , Python, 43 lines - code/
make_figures.ipynb , Jupyter, 223 lines - code/
make_variable_tables.R , R, 399 lines - manuscript/
abstract.qmd , Quarto, 1 line - manuscript/
manuscript.qmd , Quarto, 218 lines - manuscript/
supplement.qmd , Quarto, 202 lines, 2 matches - LICENSE, License, 21 lines
- README.md, Text, 56 lines
Code availability
Source code for this article is available in the following Github repository: https://
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://
BibTeX
@article{olsson2026predi
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/
url = {https://
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/
VL - 16
IS - 1
SP - 12462
SN - 2045-2322
PB - Nature Publishing Group
DO - 10.1038/
UR - https://
LA - en
ER -
CSL-JSON
{
"id": "10.1038/
"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":
"volume": "16",
"issue": "1",
"page": "12462",
"DOI": "10.1038/
"PMID": "41986523",
"PMCID": "PMC13083973",
"ISSN": "2045-2322",
"publisher": "Nature Publishing Group",
"URL": "https://
"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 medicineIn 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 agingIn 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 biologyIn 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 communicationsIn 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 biologyIn 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 geneticsIn 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 oncologyIn 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 biologyIn 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 reportsIn 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.
Claim this paper
Correct its record
Say what each link of this record is, remove the ones that are not the paper's, add the ones that are missing. The correction becomes a new version of the record, in its Versions section.
Validate its tracing map
You validate the map as this page shows it: 1 repository of the authors' code, each at its verified commit and with its license, 8 scripts, and 8 matches between paragraphs and code (see the Code and Map sections). It then receives a DOI on Zenodo, with you (your ORCID iD) and OSCR as its creators; the code itself is not deposited.
The map's fingerprint: sha256:7c3da4fdcbeba90f…
Add the badge to its README
The badge links the code to this page. Copy one of these into the README of the paper's code: only you decide where it goes, and nothing is changed for you.
Markdown
[, paste the snippet at the top, then “Commit changes…” and, to review it first, “Create a new branch and start a pull request”. You open the pull request; OSCR asks for no permission.
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.
