Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.
Python agent-skillsai-scientistbioinformaticschemoinformaticsclaudeclaude-skillsclaudecodeclinical-researchcomputational-biologydata-analysisdrug-discoverygenomicsmaterials-sciencemetabolomicsproteomicsscientific-computingscientific-visualization
Update scikit-survival for current APIs
Timothy Kassis5a62c8a07511
diff --git a/skills/scikit-survival/SKILL.md b/skills/scikit-survival/SKILL.mdindex 84674e93..a53d3e4f 100644--- a/skills/scikit-survival/SKILL.md+++ b/skills/scikit-survival/SKILL.md@@ -1,396 +1,313 @@---name: scikit-survival-description: Comprehensive toolkit for survival analysis and time-to-event modeling in Python using scikit-survival. Use this skill when working with censored survival data, performing time-to-event analysis, fitting Cox models, Random Survival Forests, Gradient Boosting models, or Survival SVMs, evaluating survival predictions with concordance index or Brier score, handling competing risks, or implementing any survival analysis workflow with the scikit-survival library.-license: GPL-3.0 license-metadata: {"version": "1.0", "skill-author": "K-Dense Inc."}+description: Build, evaluate, and audit right-censored or competing-risk survival workflows with scikit-survival, including leakage-safe preprocessing, model selection, probability prediction, and censoring-aware metrics.+license: MIT+compatibility: Requires Python 3.11+, uv, and the pinned scikit-survival 0.28.0 stack for executable examples. Bundled CLIs are local and network-free by default.+allowed-tools: Read, Write, Edit, Bash+metadata:+ version: "1.1"+ skill-author: K-Dense Inc.----# scikit-survival: Survival Analysis in Python--## Overview--scikit-survival is a Python library for survival analysis built on top of scikit-learn. It provides specialized tools for time-to-event analysis, handling the unique challenge of censored data where some observations are only partially known.--Survival analysis aims to establish connections between covariates and the time of an event, accounting for censored records (particularly right-censored data from studies where participants don't experience events during observation periods).--## When to Use This Skill--Use this skill when:-- Performing survival analysis or time-to-event modeling-- Working with censored data (right-censored, left-censored, or interval-censored)-- Fitting Cox proportional hazards models (standard or penalized)-- Building ensemble survival models (Random Survival Forests, Gradient Boosting)-- Training Survival Support Vector Machines-- Evaluating survival model performance (concordance index, Brier score, time-dependent AUC)-- Estimating Kaplan-Meier or Nelson-Aalen curves-- Analyzing competing risks-- Preprocessing survival data or handling missing values in survival datasets-- Conducting any analysis using the scikit-survival library--## Core Capabilities--### 1. Model Types and Selection--scikit-survival provides multiple model families, each suited for different scenarios:--#### Cox Proportional Hazards Models-**Use for**: Standard survival analysis with interpretable coefficients-- `CoxPHSurvivalAnalysis`: Basic Cox model-- `CoxnetSurvivalAnalysis`: Penalized Cox with elastic net for high-dimensional data-- `IPCRidge`: Ridge regression for accelerated failure time models--**See**: `references/cox-models.md` for detailed guidance on Cox models, regularization, and interpretation--#### Ensemble Methods-**Use for**: High predictive performance with complex non-linear relationships-- `RandomSurvivalForest`: Robust, non-parametric ensemble method-- `GradientBoostingSurvivalAnalysis`: Tree-based boosting for maximum performance-- `ComponentwiseGradientBoostingSurvivalAnalysis`: Linear boosting with feature selection-- `ExtraSurvivalTrees`: Extremely randomized trees for additional regularization--**See**: `references/ensemble-models.md` for comprehensive guidance on ensemble methods, hyperparameter tuning, and when to use each model--#### Survival Support Vector Machines-**Use for**: Medium-sized datasets with margin-based learning-- `FastSurvivalSVM`: Linear SVM optimized for speed-- `FastKernelSurvivalSVM`: Kernel SVM for non-linear relationships-- `HingeLossSurvivalSVM`: SVM with hinge loss-- `ClinicalKernelTransform`: Specialized kernel for clinical + molecular data--**See**: `references/svm-models.md` for detailed SVM guidance, kernel selection, and hyperparameter tuning--#### Model Selection Decision Tree--```-Start-├─ High-dimensional data (p > n)?-│ ├─ Yes → CoxnetSurvivalAnalysis (elastic net)-│ └─ No → Continue-│-├─ Need interpretable coefficients?-│ ├─ Yes → CoxPHSurvivalAnalysis or ComponentwiseGradientBoostingSurvivalAnalysis-│ └─ No → Continue-│-├─ Complex non-linear relationships expected?-│ ├─ Yes-│ │ ├─ Large dataset (n > 1000) → GradientBoostingSurvivalAnalysis-│ │ ├─ Medium dataset → RandomSurvivalForest or FastKernelSurvivalSVM-│ │ └─ Small dataset → RandomSurvivalForest-│ └─ No → CoxPHSurvivalAnalysis or FastSurvivalSVM-│-└─ For maximum performance → Try multiple models and compare+# scikit-survival++## Scope++Use this skill for scikit-survival 0.28.0 workflows involving:++- right-censored structured outcomes;+- Cox PH, Coxnet, IPC ridge, survival trees, forests, boosting, and SVMs;+- discrimination, prediction error, calibration-oriented checks, and time-dependent prediction;+- nonparametric cumulative incidence with competing risks;+- scikit-learn pipelines, nested model selection, and reproducible reports.++scikit-survival primarily models right-censored outcomes. Its built-in competing-risk+support is nonparametric cumulative incidence; it does not provide Fine-Gray regression.+Do not present model output as clinical advice, causal evidence, or proof of clinical+utility.++## Current release and installation++Verified 2026-07-23:++- Latest stable: **scikit-survival 0.28.0**, released 2026-07-05.+- Python: **3.11 or later**; PyPI wheels cover CPython 3.11-3.14 on Linux+ x86-64, macOS x86-64/ARM64, and Windows x86-64.+- Runtime bounds: NumPy >=2.0.0, pandas >=2.2.0, SciPy >=1.13.0,+ scikit-learn >=1.9.0,<1.10, OSQP >=1.0.2, narwhals >=2.0.1.+- 0.28 adds pandas/Polars estimator support through narwhals and removes+ `criterion` from `GradientBoostingSurvivalAnalysis`.++Create an isolated environment and install the tested snapshot:++```bash+uv venv --python 3.11+source .venv/bin/activate+uv pip install \+ "scikit-survival==0.28.0" \+ "scikit-learn==1.9.0" \+ "numpy==2.4.6" \+ "pandas==3.0.5" \+ "scipy==1.17.1" \+ "ecos==2.0.14" \+ "osqp==1.1.3" \+ "joblib==1.5.3" \+ "numexpr==2.14.2" \+ "narwhals==2.24.0"```-### 2. Data Preparation and Preprocessing--Before modeling, properly prepare survival data:+Binary wheels are preferred. A source build requires a C/C++ compiler; OSQP may+also require CMake. This skill is MIT-licensed; the upstream scikit-survival package+is GPL-3.0-or-later, so review upstream licensing before redistribution.++## Non-negotiable workflow++1. **Define the estimand and event coding.** Decide whether the target is+ all-event survival, cause-specific hazard, or cause-specific cumulative incidence.+2. **Validate outcomes.** Standard estimators need a two-field structured array:+ boolean event first, observed time second. Competing-risk CIF instead needs a+ separate integer event vector: 0=censored, 1..K=causes.+3. **Split before learned preprocessing.** Never fit imputers, encoders, scalers,+ feature selectors, or alpha choices on all rows before splitting.+4. **Fit preprocessing inside a pipeline.** Unknown categories and missingness must+ be handled using training-fold state only.+5. **Tune without reusing evaluation data.** Use nested CV when reporting+ cross-validated tuned performance, or reserve a truly untouched final holdout.+6. **Fit censoring distributions on training data.** IPCW concordance, dynamic AUC,+ and Brier metrics receive `survival_train`, never a pooled train+test outcome.+7. **Restrict evaluation times.** Use a strictly increasing grid inside test+ follow-up and below the end of training support where the estimated censoring+ survival remains positive.+8. **Match predictions to metrics.** Concordance/dynamic AUC consume higher-is-riskier+ scores. Brier metrics consume survival probabilities with shape+ `(n_test, n_times)`, not risk scores or unevaluated step functions.+9. **Handle competing causes explicitly.** Standard survival probabilities and CIFs+ answer different questions. Never estimate event-specific probability with+ `1 - Kaplan-Meier` while censoring competing events.+10. **Report limits.** Separate discrimination, calibration, prediction error,+ and cumulative incidence. None alone establishes decision or clinical utility.++## Outcome construction-#### Creating Survival Outcomes```pythonfrom sksurv.util import Surv-# From separate arrays-y = Surv.from_arrays(event=event_array, time=time_array)--# From DataFrame-y = Surv.from_dataframe('event', 'time', df)-```--#### Essential Preprocessing Steps-1. **Handle missing values**: Imputation strategies for features-2. **Encode categorical variables**: One-hot encoding or label encoding-3. **Standardize features**: Critical for SVMs and regularized Cox models-4. **Validate data quality**: Check for negative times, sufficient events per feature-5. **Train-test split**: Maintain similar censoring rates across splits--**See**: `references/data-handling.md` for complete preprocessing workflows, data validation, and best practices--### 3. Model Evaluation--Proper evaluation is critical for survival models. Use appropriate metrics that account for censoring:--#### Concordance Index (C-index)-Primary metric for ranking/discrimination:-- **Harrell's C-index**: Use for low censoring (<40%)-- **Uno's C-index**: Use for moderate to high censoring (>40%) - more robust--```python-from sksurv.metrics import concordance_index_censored, concordance_index_ipcw--# Harrell's C-index-c_harrell = concordance_index_censored(y_test['event'], y_test['time'], risk_scores)[0]--# Uno's C-index (recommended)-c_uno = concordance_index_ipcw(y_train, y_test, risk_scores)[0]+y = Surv.from_arrays(event=event_bool, time=observed_time)+# Equivalent for pandas or Polars:+y = Surv.from_dataframe("event", "time", frame)```-#### Time-Dependent AUC-Evaluate discrimination at specific time points:--```python-from sksurv.metrics import cumulative_dynamic_auc--times = [365, 730, 1095] # 1, 2, 3 years-auc, mean_auc = cumulative_dynamic_auc(y_train, y_test, risk_scores, times)-```+The first field is boolean (`True`=event, `False`=right-censored); the second is+floating-point time. Field names may vary, but field order and meaning may not.+Use `references/data-handling.md` before loading custom or competing-risk data.-#### Brier Score-Assess both discrimination and calibration:+## Leakage-safe pipeline```python-from sksurv.metrics import integrated_brier_score--ibs = integrated_brier_score(y_train, y_test, survival_functions, times)-```--**See**: `references/evaluation-metrics.md` for comprehensive evaluation guidance, metric selection, and using scorers with cross-validation--### 4. Competing Risks Analysis--Handle situations with multiple mutually exclusive event types:+from sklearn.compose import ColumnTransformer+from sklearn.impute import SimpleImputer+from sklearn.model_selection import train_test_split+from sklearn.pipeline import make_pipeline+from sklearn.preprocessing import OneHotEncoder, StandardScaler+from sksurv.linear_model import CoxPHSurvivalAnalysis-```python-from sksurv.nonparametric import cumulative_incidence_competing_risks+X_train, X_test, y_train, y_test = train_test_split(+ X, y, test_size=0.25, stratify=y["event"], random_state=20260723+)-# Estimate cumulative incidence for each event type-time_points, cif_event1, cif_event2 = cumulative_incidence_competing_risks(y)+preprocess = ColumnTransformer(+ [+ ("num", make_pipeline(SimpleImputer(strategy="median"), StandardScaler()), numeric),+ (+ "cat",+ make_pipeline(+ SimpleImputer(strategy="most_frequent"),+ OneHotEncoder(handle_unknown="ignore", drop="first", sparse_output=False),+ ),+ categorical,+ ),+ ],+ sparse_threshold=0.0,+)+model = make_pipeline(preprocess, CoxPHSurvivalAnalysis(alpha=0.1, ties="efron"))+model.fit(X_train, y_train)+risk = model.predict(X_test)```-**Use competing risks when**:-- Multiple mutually exclusive event types exist (e.g., death from different causes)-- Occurrence of one event prevents others-- Need probability estimates for specific event types--**See**: `references/competing-risks.md` for detailed competing risks methods, cause-specific hazard models, and interpretation+The split precedes every learned transformation. For repeated or grouped records,+use a group-aware split; for temporal deployment, use a time-respecting split.++## Model choice++- `CoxPHSurvivalAnalysis`: interpretable log-hazard coefficients under proportional+ hazards; `alpha` is ridge shrinkage and `ties` is `"breslow"` or `"efron"`.+- `CoxnetSurvivalAnalysis`: LASSO/elastic-net path for high-dimensional data.+ `l1_ratio` is in `(0, 1]`; use `fit_baseline_model=True` before requesting+ survival or cumulative-hazard functions.+- `IPCRidge`: IPC-weighted ridge AFT model; prediction is on a time/log-time scale,+ not a Cox risk score.+- `RandomSurvivalForest` / `ExtraSurvivalTrees`: nonlinear survival and cumulative+ hazard predictions; use permutation importance, not impurity importance.+- `GradientBoostingSurvivalAnalysis`: tree boosting with `"coxph"`, `"squared"`,+ or `"ipcwls"` loss. `criterion` was removed in 0.28.+- `ComponentwiseGradientBoostingSurvivalAnalysis`: sparse linear componentwise+ boosting.+- `FastSurvivalSVM` / `FastKernelSurvivalSVM`: ranking or regression objectives.+ Only `rank_ratio=1` directly returns higher-is-riskier scores; SVMs do not yield+ survival probabilities for Brier metrics.++Read the model-specific reference before interpreting coefficients or predictions:+`references/cox-models.md`, `references/ensemble-models.md`, or+`references/svm-models.md`.++## Prediction and metric contracts-### 5. Non-parametric Estimation--Estimate survival functions without parametric assumptions:--#### Kaplan-Meier Estimator```python-from sksurv.nonparametric import kaplan_meier_estimator--time, survival_prob = kaplan_meier_estimator(y['event'], y['time'])-```+import numpy as np+from sksurv.metrics import (+ brier_score,+ concordance_index_ipcw,+ cumulative_dynamic_auc,+ integrated_brier_score,+)-#### Nelson-Aalen Estimator-```python-from sksurv.nonparametric import nelson_aalen_estimator+risk = model.predict(X_test) # (n_test,), higher means higher event risk+uno_c = concordance_index_ipcw(y_train, y_test, risk, tau=times[-1])[0]+auc_t, mean_auc = cumulative_dynamic_auc(y_train, y_test, risk, times)-time, cumulative_hazard = nelson_aalen_estimator(y['event'], y['time'])+surv_fns = model.predict_survival_function(X_test)+surv_prob = np.vstack([fn(times) for fn in surv_fns]) # (n_test, n_times)+_, brier_t = brier_score(y_train, y_test, surv_prob, times)+ibs = integrated_brier_score(y_train, y_test, surv_prob, times)```-## Typical Workflows--### Workflow 1: Standard Survival Analysis--```python-from sksurv.datasets import load_breast_cancer-from sksurv.linear_model import CoxPHSurvivalAnalysis-from sksurv.metrics import concordance_index_ipcw-from sklearn.model_selection import train_test_split-from sklearn.preprocessing import StandardScaler--# 1. Load and prepare data-X, y = load_breast_cancer()-X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)--# 2. Preprocess-scaler = StandardScaler()-X_train_scaled = scaler.fit_transform(X_train)-X_test_scaled = scaler.transform(X_test)+- Harrell C and Uno C measure rank discrimination, not calibration.+- Cumulative/dynamic AUC measures discrimination at selected horizons and accepts+ 1D or time-dependent 2D risk scores; it rejects survival probabilities.+- Brier score is censoring-weighted probability error and reflects both+ discrimination and calibration. It is not a standalone calibration curve.+- Calibration requires horizon-specific predicted-versus-observed checks on+ independent data. scikit-survival 0.28 has no dedicated calibration-curve API.-# 3. Fit model-estimator = CoxPHSurvivalAnalysis()-estimator.fit(X_train_scaled, y_train)+See `references/evaluation-metrics.md` for assumptions, primary literature, safe+time-grid construction, and scorer wrappers.-# 4. Predict-risk_scores = estimator.predict(X_test_scaled)+## Pipelines, metadata routing, and tuning-# 5. Evaluate-c_index = concordance_index_ipcw(y_train, y_test, risk_scores)[0]-print(f"C-index: {c_index:.3f}")-```--### Workflow 2: High-Dimensional Data with Feature Selection+Ordinary `Pipeline.fit(X, y)` needs no metadata-routing setup. Metric wrappers such+as `as_concordance_index_ipcw_scorer` are estimator wrappers, not `scoring=`+callables:```python-from sksurv.linear_model import CoxnetSurvivalAnalysisfrom sklearn.model_selection import GridSearchCVfrom sksurv.metrics import as_concordance_index_ipcw_scorer-# 1. Use penalized Cox for feature selection-estimator = CoxnetSurvivalAnalysis(l1_ratio=0.9) # Lasso-like--# 2. Tune regularization with cross-validation-param_grid = {'alpha_min_ratio': [0.01, 0.001]}-cv = GridSearchCV(estimator, param_grid,- scoring=as_concordance_index_ipcw_scorer(), cv=5)-cv.fit(X, y)--# 3. Identify selected features-best_model = cv.best_estimator_-selected_features = np.where(best_model.coef_ != 0)[0]+wrapped = as_concordance_index_ipcw_scorer(model, tau=tau)+search = GridSearchCV(+ wrapped,+ {"estimator__coxphsurvivalanalysis__alpha": [0.01, 0.1, 1.0]},+ cv=inner_splits,+)```-### Workflow 3: Ensemble Method for Maximum Performance--```python-from sksurv.ensemble import GradientBoostingSurvivalAnalysis-from sklearn.model_selection import GridSearchCV+The wrapper learns the censoring distribution from each fit fold. Prefix wrapped+parameters with `estimator__`. Enable scikit-learn metadata routing only when+passing extra metadata through a meta-estimator. For example, Coxnet's+`set_predict_request(alpha=True)` matters only when routing the `alpha` prediction+argument with `sklearn.set_config(enable_metadata_routing=True)`.-# 1. Define parameter grid-param_grid = {- 'learning_rate': [0.01, 0.05, 0.1],- 'n_estimators': [100, 200, 300],- 'max_depth': [3, 5, 7]-}--# 2. Grid search-gbs = GradientBoostingSurvivalAnalysis()-cv = GridSearchCV(gbs, param_grid, cv=5,- scoring=as_concordance_index_ipcw_scorer(), n_jobs=-1)-cv.fit(X_train, y_train)--# 3. Evaluate best model-best_model = cv.best_estimator_-risk_scores = best_model.predict(X_test)-c_index = concordance_index_ipcw(y_train, y_test, risk_scores)[0]-```+Use an outer CV loop for an unbiased CV performance estimate after inner tuning.+Do not select parameters and report performance from the same folds as if external.-### Workflow 4: Comprehensive Model Comparison+## Competing risks```python-from sksurv.linear_model import CoxPHSurvivalAnalysis-from sksurv.ensemble import RandomSurvivalForest, GradientBoostingSurvivalAnalysis-from sksurv.svm import FastSurvivalSVM-from sksurv.metrics import concordance_index_ipcw, integrated_brier_score--# Define models-models = {- 'Cox': CoxPHSurvivalAnalysis(),- 'RSF': RandomSurvivalForest(n_estimators=100, random_state=42),- 'GBS': GradientBoostingSurvivalAnalysis(random_state=42),- 'SVM': FastSurvivalSVM(random_state=42)-}--# Evaluate each model-results = {}-for name, model in models.items():- model.fit(X_train_scaled, y_train)- risk_scores = model.predict(X_test_scaled)- c_index = concordance_index_ipcw(y_train, y_test, risk_scores)[0]- results[name] = c_index- print(f"{name}: C-index = {c_index:.3f}")--# Select best model-best_model_name = max(results, key=results.get)-print(f"\nBest model: {best_model_name}")-```--## Integration with scikit-learn--scikit-survival fully integrates with scikit-learn's ecosystem:+from sksurv.nonparametric import cumulative_incidence_competing_risks-```python-from sklearn.pipeline import Pipeline-from sklearn.preprocessing import StandardScaler-from sklearn.model_selection import cross_val_score, GridSearchCV--# Use pipelines-pipeline = Pipeline([- ('scaler', StandardScaler()),- ('model', CoxPHSurvivalAnalysis())-])--# Use cross-validation-scores = cross_val_score(pipeline, X, y, cv=5,- scoring=as_concordance_index_ipcw_scorer())--# Use grid search-param_grid = {'model__alpha': [0.1, 1.0, 10.0]}-cv = GridSearchCV(pipeline, param_grid, cv=5)-cv.fit(X, y)+# status: integer array, 0=censored, 1..K=mutually exclusive causes+time, cif = cumulative_incidence_competing_risks(status, observed_time)+total_cif = cif[0]+cause_1_cif = cif[1]```-## Best Practices--1. **Always standardize features** for SVMs and regularized Cox models-2. **Use Uno's C-index** instead of Harrell's when censoring > 40%-3. **Report multiple evaluation metrics** (C-index, integrated Brier score, time-dependent AUC)-4. **Check proportional hazards assumption** for Cox models-5. **Use cross-validation** for hyperparameter tuning with appropriate scorers-6. **Validate data quality** before modeling (check for negative times, sufficient events per feature)-7. **Compare multiple model types** to find best performance-8. **Use permutation importance** for Random Survival Forests (not built-in importance)-9. **Consider competing risks** when multiple event types exist-10. **Document censoring mechanism** and rates in analysis--## Common Pitfalls to Avoid--1. **Using Harrell's C-index with high censoring** → Use Uno's C-index-2. **Not standardizing features for SVMs** → Always standardize-3. **Forgetting to pass y_train to concordance_index_ipcw** → Required for IPCW calculation-4. **Treating competing events as censored** → Use competing risks methods-5. **Not checking for sufficient events per feature** → Rule of thumb: 10+ events per feature-6. **Using built-in feature importance for RSF** → Use permutation importance-7. **Ignoring proportional hazards assumption** → Validate or use alternative models-8. **Not using appropriate scorers in cross-validation** → Use as_concordance_index_ipcw_scorer()--## Reference Files--This skill includes detailed reference files for specific topics:--- **`references/cox-models.md`**: Complete guide to Cox proportional hazards models, penalized Cox (CoxNet), IPCRidge, regularization strategies, and interpretation-- **`references/ensemble-models.md`**: Random Survival Forests, Gradient Boosting, hyperparameter tuning, feature importance, and model selection-- **`references/evaluation-metrics.md`**: Concordance index (Harrell's vs Uno's), time-dependent AUC, Brier score, comprehensive evaluation pipelines-- **`references/data-handling.md`**: Data loading, preprocessing workflows, handling missing data, feature encoding, validation checks-- **`references/svm-models.md`**: Survival Support Vector Machines, kernel selection, clinical kernel transform, hyperparameter tuning-- **`references/competing-risks.md`**: Competing risks analysis, cumulative incidence functions, cause-specific hazard models+`cif` has shape `(K + 1, n_times)`; row 0 is total risk and rows 1..K are+cause-specific cumulative incidence. Cause-specific Cox models treat other causes+as censored to estimate cause-specific hazards, but one such model's+`1 - survival` is not the cause-specific CIF. See `references/competing-risks.md`.-Load these reference files when detailed information is needed for specific tasks.+## Bundled local CLIs-## Additional Resources+All helpers use deterministic synthetic data when no input is given. They make no+network calls, reject URLs and symlinks, bound files/rows/features, avoid unsafe+pickle loading, and lazily import scientific packages.-- **Official Documentation**: https://scikit-survival.readthedocs.io/-- **GitHub Repository**: https://github.com/sebp/scikit-survival-- **Built-in Datasets**: Use `sksurv.datasets` for practice datasets (GBSG2, WHAS500, veterans lung cancer, etc.)-- **API Reference**: Complete list of classes and functions at https://scikit-survival.readthedocs.io/en/stable/api/index.html+```bash+python skills/scikit-survival/scripts/validate_survival_csv.py --help+python skills/scikit-survival/scripts/train_survival_model.py --help+python skills/scikit-survival/scripts/evaluate_survival_metrics.py --help+python skills/scikit-survival/scripts/competing_risk_cif.py --help+python skills/scikit-survival/scripts/model_report.py --help+```-## Quick Reference: Key Imports+Typical local flow:-```python-# Models-from sksurv.linear_model import CoxPHSurvivalAnalysis, CoxnetSurvivalAnalysis, IPCRidge-from sksurv.ensemble import RandomSurvivalForest, GradientBoostingSurvivalAnalysis-from sksurv.svm import FastSurvivalSVM, FastKernelSurvivalSVM-from sksurv.tree import SurvivalTree+```bash+python skills/scikit-survival/scripts/validate_survival_csv.py \+ --input data.csv --event-column event --time-column time \+ --feature-columns age,group,measurement --structured-output outcome.npy-# Evaluation metrics-from sksurv.metrics import (- concordance_index_censored,- concordance_index_ipcw,- cumulative_dynamic_auc,- brier_score,- integrated_brier_score,- as_concordance_index_ipcw_scorer,- as_integrated_brier_score_scorer-)+python skills/scikit-survival/scripts/train_survival_model.py \+ --input data.csv --event-column event --time-column time \+ --numeric-columns age,measurement --categorical-columns group \+ --model coxph --tune --prediction-output predictions.npz \+ --output training-summary.json-# Non-parametric estimation-from sksurv.nonparametric import (- kaplan_meier_estimator,- nelson_aalen_estimator,- cumulative_incidence_competing_risks-)--# Data handling-from sksurv.util import Surv-from sksurv.preprocessing import OneHotEncoder, encode_categorical-from sksurv.datasets import load_gbsg2, load_breast_cancer, load_veterans_lung_cancer+python skills/scikit-survival/scripts/evaluate_survival_metrics.py \+ --input predictions.npz --output metrics-summary.json-# Kernels-from sksurv.kernels import ClinicalKernelTransform+python skills/scikit-survival/scripts/model_report.py \+ --training-summary training-summary.json \+ --metrics-summary metrics-summary.json --output model-report.md```+Use only de-identified, authorized local data. The bundled tests contain synthetic+records only and no patient data or PHI.++## Security triage++`SECURITY.md` previously claimed this skill bundled package-shadowing files named+`sklearn.py` and `sksurv.py`. The 2026-07-23 inventory confirmed those files did+not exist; the claim was a phantom analyzer finding. This refresh adds only+descriptively named helpers and no shadow modules, environment reads, or network+calls.++Never name a project script after an imported package (including `sklearn.py`,+`sksurv.py`, `numpy.py`, or `pandas.py`), because Python may import the local file+instead of the installed library. Inspect the working directory before executing+examples copied from untrusted sources.++## Reference files++- `references/data-handling.md` — structured arrays, datasets, schema validation,+ pandas/Polars preprocessing, and leakage-safe splitting.+- `references/cox-models.md` — Cox PH, Coxnet, IPCRidge, assumptions, and tuning.+- `references/ensemble-models.md` — forests, trees, boosting, predictions, and+ permutation importance.+- `references/svm-models.md` — SVM objectives, prediction direction, scaling,+ kernels, and limitations.+- `references/evaluation-metrics.md` — metric inputs, censoring assumptions,+ time grids, calibration, nested CV, and primary literature.+- `references/competing-risks.md` — integer event coding, CIF API, built-in+ datasets, cause-specific hazards, and unsupported Fine-Gray regression.++## Dated sources++Official API and compatibility sources, checked 2026-07-23:++- [PyPI 0.28.0](https://pypi.org/project/scikit-survival/) — released 2026-07-05.+- [GitHub v0.28.0 release](https://github.com/sebp/scikit-survival/releases/tag/v0.28.0)+ — published 2026-07-05.+- [0.28 release notes](https://scikit-survival.readthedocs.io/en/stable/release_notes/v0.28.html).+- [Installation guide](https://scikit-survival.readthedocs.io/en/stable/install.html).+- [Stable user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/index.html).+- [Stable API reference](https://scikit-survival.readthedocs.io/en/stable/api/index.html).diff --git a/skills/scikit-survival/references/competing-risks.md b/skills/scikit-survival/references/competing-risks.mdindex 2a989b25..00f979ee 100644--- a/skills/scikit-survival/references/competing-risks.md+++ b/skills/scikit-survival/references/competing-risks.md@@ -1,397 +1,302 @@-# Competing Risks Analysis+# Competing risks and cumulative incidence-## Overview+Verified for scikit-survival 0.28.0 on 2026-07-23.-Competing risks occur when subjects can experience one of several mutually exclusive events (event types). When one event occurs, it prevents ("competes with") the occurrence of other events.+## Estimand-### Examples of Competing Risks+Competing risks are mutually exclusive causes \(J \in \{1,\ldots,K\}\), where the+first observed cause prevents observing the others as first events.-**Medical Research:**-- Death from cancer vs. death from cardiovascular disease vs. death from other causes-- Relapse vs. death without relapse in cancer studies-- Different types of infections in transplant patients+The cause-\(k\) cumulative incidence function (CIF) is:-**Other Applications:**-- Job termination: retirement vs. resignation vs. termination for cause-- Equipment failure: different failure modes-- Customer churn: different reasons for leaving+\[+F_k(t) = P(T \le t, J=k).+\]-### Key Concept: Cumulative Incidence Function (CIF)+It is an absolute cause-specific event probability accounting for all competing+causes. It is not:-The **Cumulative Incidence Function (CIF)** represents the probability of experiencing a specific event type by time *t*, accounting for the presence of competing risks.+- a cause-specific hazard;+- `1 - Kaplan-Meier` after censoring other causes;+- a conditional probability among only those still event-free;+- a causal effect or clinical-utility measure.-**CIF_k(t) = P(T ≤ t, event type = k)**+The total risk is \(\sum_k F_k(t)\). Its complement is estimated all-cause+event-free survival. Censoring is an observation mechanism, not an additional+event-free state.-This differs from the Kaplan-Meier estimator, which would overestimate event probabilities when competing risks are present.+## Event coding-## When to Use Competing Risks Analysis+The nonparametric CIF API takes two separate arrays:-**Use competing risks when:**-- Multiple mutually exclusive event types exist-- Occurrence of one event prevents others-- Need to estimate probability of specific event types-- Want to understand how covariates affect different event types+```python+# event: 0=censored; 1..K=mutually exclusive causes+event = frame["status"].to_numpy(dtype=int)+time = frame["time"].to_numpy(dtype=float)+```-**Don't use when:**-- Only one event type of interest (standard survival analysis)-- Events are not mutually exclusive (use recurrent events methods)-- Competing events are extremely rare (can treat as censoring)+Requirements:-## Cumulative Incidence with Competing Risks+- `event` is integer and non-negative;+- 0 always denotes right-censoring;+- positive codes 1..K are contiguous;+- the data contains observations for every code 1..K;+- `time` is finite and positive;+- event/time lengths match.-### cumulative_incidence_competing_risks Function+Do not pass a boolean `Surv` outcome to+`cumulative_incidence_competing_risks()`. `Surv` intentionally collapses event+status to event versus censoring and loses cause identity.-Estimates the cumulative incidence function for each event type.+## Nonparametric CIF API```pythonfrom sksurv.nonparametric import cumulative_incidence_competing_risks-from sksurv.datasets import load_leukemia--# Load data with competing risks-X, y = load_leukemia()-# y has event types: 0=censored, 1=relapse, 2=death--# Compute cumulative incidence for each event type-# Returns: time points, CIF for event 1, CIF for event 2, ...-time_points, cif_1, cif_2 = cumulative_incidence_competing_risks(y)--# Plot cumulative incidence functions-import matplotlib.pyplot as plt--plt.figure(figsize=(10, 6))-plt.step(time_points, cif_1, where='post', label='Relapse', linewidth=2)-plt.step(time_points, cif_2, where='post', label='Death in remission', linewidth=2)-plt.xlabel('Time (weeks)')-plt.ylabel('Cumulative Incidence')-plt.title('Competing Risks: Relapse vs Death')-plt.legend()-plt.grid(True, alpha=0.3)-plt.show()++time_points, cumulative_incidence = (+ cumulative_incidence_competing_risks(event, time)+)```-### Interpretation+Current signature:-- **CIF at time t**: Probability of experiencing that specific event by time t-- **Sum of all CIFs**: Total probability of experiencing any event (all cause)-- **1 - sum of CIFs**: Probability of being event-free and uncensored+```text+cumulative_incidence_competing_risks(+ event,+ time_exit,+ time_min=None,+ conf_level=0.95,+ conf_type=None,+ var_type="Aalen",+)+```-## Data Format for Competing Risks+Returns:-### Creating Structured Array with Event Types+- `time_points`: shape `(n_times,)`;+- `cumulative_incidence`: shape `(K + 1, n_times)`;+- row 0: total risk of any cause;+- row `k`: CIF for cause `k`.```python-import numpy as np-from sksurv.util import Surv--# Event types: 0 = censored, 1 = event type 1, 2 = event type 2-event_types = np.array([0, 1, 2, 1, 0, 2, 1])-times = np.array([10.2, 5.3, 8.1, 3.7, 12.5, 6.8, 4.2])+total_risk = cumulative_incidence[0]+cause_1 = cumulative_incidence[1]+cause_2 = cumulative_incidence[2]-# Create survival array-# For competing risks: event=True if any event occurred-# Store event type separately or encode in the event field-y = Surv.from_arrays(- event=(event_types > 0), # True if any event- time=times+assert np.allclose(+ total_risk,+ cumulative_incidence[1:].sum(axis=0),)--# Keep event_types for distinguishing between event types```-### Converting Data with Event Types+`time_min` estimates conditionally on surviving at least to that time. This changes+the target population and must not be selected after viewing outcomes.++### Confidence intervals```python-import pandas as pd-from sksurv.util import Surv+time_points, cumulative_incidence, confidence_interval = (+ cumulative_incidence_competing_risks(+ event,+ time,+ conf_type="log-log",+ conf_level=0.95,+ var_type="Aalen",+ )+)+```-# Assume data has: time, event_type columns-# event_type: 0=censored, 1=type1, 2=type2, etc.+`confidence_interval` has shape `(K + 1, 2, n_times)`, where axis 1 is lower/upper.+Current variance choices are:-df = pd.read_csv('competing_risks_data.csv')+- `"Aalen"`+- `"Dinse"`+- `"Dinse_Approx"`-# Create survival outcome-y = Surv.from_arrays(- event=(df['event_type'] > 0),- time=df['time']-)+Pointwise confidence intervals are not simultaneous confidence bands. Sparse+causes and late follow-up can make estimates unstable even when the function+returns a result.-# Store event types-event_types = df['event_type'].values+## Built-in competing-risk datasets++```python+from sksurv.datasets import load_bmt, load_cgvhd++X_bmt, y_bmt = load_bmt() # status codes 0, 1, 2+X_cgvhd, y_cgvhd = load_cgvhd() # status codes 0, 1, 2, 3```-## Comparing Cumulative Incidence Between Groups+The first structured field is integer cause status, not boolean. These are real+study datasets distributed for examples. The bundled tests do not use them; they+use synthetic non-clinical outcomes only.++## Why `1 - Kaplan-Meier` is wrong for one cause++If cause 2 prevents cause 1, censoring cause 2 in a Kaplan-Meier curve treats those+subjects as if they could still experience cause 1 later under non-informative+censoring. That counterfactual risk set does not estimate the observed-world+probability \(F_1(t)\) and typically overstates cause-1 probability.-### Stratified Analysis+Use CIF for cause-specific absolute probability:```python-from sksurv.nonparametric import cumulative_incidence_competing_risks-import matplotlib.pyplot as plt--# Split by treatment group-mask_treatment = X['treatment'] == 'A'-mask_control = X['treatment'] == 'B'--y_treatment = y[mask_treatment]-y_control = y[mask_control]--# Compute CIF for each group-time_trt, cif1_trt, cif2_trt = cumulative_incidence_competing_risks(y_treatment)-time_ctl, cif1_ctl, cif2_ctl = cumulative_incidence_competing_risks(y_control)--# Plot comparison-fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))--# Event type 1-ax1.step(time_trt, cif1_trt, where='post', label='Treatment', linewidth=2)-ax1.step(time_ctl, cif1_ctl, where='post', label='Control', linewidth=2)-ax1.set_xlabel('Time')-ax1.set_ylabel('Cumulative Incidence')-ax1.set_title('Event Type 1')-ax1.legend()-ax1.grid(True, alpha=0.3)--# Event type 2-ax2.step(time_trt, cif2_trt, where='post', label='Treatment', linewidth=2)-ax2.step(time_ctl, cif2_ctl, where='post', label='Control', linewidth=2)-ax2.set_xlabel('Time')-ax2.set_ylabel('Cumulative Incidence')-ax2.set_title('Event Type 2')-ax2.legend()-ax2.grid(True, alpha=0.3)--plt.tight_layout()-plt.show()+time_points, cif = cumulative_incidence_competing_risks(event, time)+probability_cause_1_by_t = cif[1]```-## Statistical Testing with Competing Risks+Kaplan-Meier remains appropriate for all-cause event-free survival after collapsing+all causes to event, if that is the estimand and censoring assumptions hold.-### Gray's Test+## Comparing groups-Compare cumulative incidence functions between groups using Gray's test (available in other packages like lifelines).+Estimate group-specific curves without fitting preprocessing on the full dataset:```python-# Note: Gray's test not directly available in scikit-survival-# Consider using lifelines or other packages--# from lifelines.statistics import multivariate_logrank_test-# result = multivariate_logrank_test(times, groups, events, event_of_interest=1)+curves = {}+for label in prespecified_groups:+ mask = group == label+ curves[label] = cumulative_incidence_competing_risks(+ event[mask],+ time[mask],+ conf_type="log-log",+ )```-## Modeling with Competing Risks+Plotting pointwise intervals does not test equality. scikit-survival 0.28 does not+provide Gray's test in this API. Do not substitute an ordinary log-rank test:+survival and CIF group hypotheses differ.-### Approach 1: Cause-Specific Hazard Models+Group labels and comparison times should be prespecified. Report at-risk/event+support; late visual separation with few rows can be misleading.-Fit separate Cox models for each event type, treating other event types as censored.+## Cause-specific Cox hazards++For cause \(k\), a cause-specific hazard model encodes that cause as an event and+other causes as censored at their occurrence time:```pythonfrom sksurv.linear_model import CoxPHSurvivalAnalysisfrom sksurv.util import Surv-# Separate outcome for each event type-# Event type 1: treat type 2 as censored-y_event1 = Surv.from_arrays(- event=(event_types == 1),- time=times-)--# Event type 2: treat type 1 as censored-y_event2 = Surv.from_arrays(- event=(event_types == 2),- time=times+y_cause_1 = Surv.from_arrays(+ event=(event == 1),+ time=time,)+cause_1_hazard_model = CoxPHSurvivalAnalysis(alpha=0.1)+cause_1_hazard_model.fit(X_train, y_cause_1_train)+```-# Fit cause-specific models-cox_event1 = CoxPHSurvivalAnalysis()-cox_event1.fit(X, y_event1)+This estimates association with the instantaneous cause-specific hazard under a+PH model. Other causes are censored for this hazard likelihood, which is different+from pretending they are independent censoring when estimating absolute CIF.-cox_event2 = CoxPHSurvivalAnalysis()-cox_event2.fit(X, y_event2)+To derive cause-specific CIF predictions from cause-specific hazards, all modeled+causes must be combined:-# Interpret coefficients for each event type-print("Event Type 1 (e.g., Relapse):")-print(cox_event1.coef_)+\[+F_k(t \mid x) =+\int_0^t S(u^- \mid x)\,dH_k(u \mid x),+\quad+S(t \mid x)=\exp\left[-\sum_j H_j(t \mid x)\right].+\]-print("\nEvent Type 2 (e.g., Death):")-print(cox_event2.coef_)-```+Therefore, `1 - cause_1_model.predict_survival_function(...)` is not the cause-1+CIF. A set of separately fitted cause-specific models requires careful joint+integration, common time grids, and external validation.-**Interpretation:**-- Separate model for each competing event-- Coefficients show effect on cause-specific hazard for that event type-- A covariate may increase risk for one event type but decrease for another+## Fine-Gray regression-### Approach 2: Fine-Gray Sub-distribution Hazard Model+scikit-survival 0.28 does not implement Fine-Gray subdistribution-hazard+regression. Do not invent an import or describe `cumulative_incidence_competing_risks`+as Fine-Gray; it is a nonparametric CIF estimator.-Models the cumulative incidence directly (not available directly in scikit-survival, but can use other packages).+If using another implementation:-```python-# Note: Fine-Gray model not directly in scikit-survival-# Consider using lifelines or rpy2 to access R's cmprsk package+- verify it is actively maintained and supports the required censoring/truncation;+- use its official API documentation;+- distinguish subdistribution from cause-specific hazard coefficients;+- keep preprocessing and tuning leakage-safe;+- validate cause-specific absolute probabilities, not only coefficients.-# from lifelines import CRCSplineFitter-# crc = CRCSplineFitter()-# crc.fit(df, event_col='event', duration_col='time')-```+Neither hazard parameterization is universally "better." The estimand determines+the method.-## Practical Example: Complete Competing Risks Analysis+## Prediction evaluation-```python-import numpy as np-import pandas as pd-import matplotlib.pyplot as plt-from sksurv.nonparametric import cumulative_incidence_competing_risks-from sksurv.linear_model import CoxPHSurvivalAnalysis-from sksurv.util import Surv+Standard `concordance_index_ipcw`, `cumulative_dynamic_auc`, and Brier APIs in+scikit-survival are documented for right-censored single-event outcomes. A+competing-risk prediction question needs:-# Simulate competing risks data-np.random.seed(42)-n = 200--# Create features-age = np.random.normal(60, 10, n)-treatment = np.random.choice(['A', 'B'], n)--# Simulate event times and types-# Event types: 0=censored, 1=relapse, 2=death-times = np.random.exponential(100, n)-event_types = np.zeros(n, dtype=int)--# Higher age increases both events, treatment A reduces relapse-for i in range(n):- if times[i] < 150: # Event occurred- # Probability of each event type- p_relapse = 0.6 if treatment[i] == 'B' else 0.4- event_types[i] = 1 if np.random.rand() < p_relapse else 2- else:- times[i] = 150 # Censored at study end--# Create DataFrame-df = pd.DataFrame({- 'time': times,- 'event_type': event_types,- 'age': age,- 'treatment': treatment-})--# Encode treatment-df['treatment_A'] = (df['treatment'] == 'A').astype(int)--# 1. OVERALL CUMULATIVE INCIDENCE-print("=" * 60)-print("OVERALL CUMULATIVE INCIDENCE")-print("=" * 60)--y_all = Surv.from_arrays(event=(df['event_type'] > 0), time=df['time'])-time_points, cif_relapse, cif_death = cumulative_incidence_competing_risks(y_all)--plt.figure(figsize=(10, 6))-plt.step(time_points, cif_relapse, where='post', label='Relapse', linewidth=2)-plt.step(time_points, cif_death, where='post', label='Death', linewidth=2)-plt.xlabel('Time (days)')-plt.ylabel('Cumulative Incidence')-plt.title('Competing Risks: Relapse vs Death')-plt.legend()-plt.grid(True, alpha=0.3)-plt.show()--print(f"5-year relapse incidence: {cif_relapse[-1]:.2%}")-print(f"5-year death incidence: {cif_death[-1]:.2%}")--# 2. STRATIFIED BY TREATMENT-print("\n" + "=" * 60)-print("CUMULATIVE INCIDENCE BY TREATMENT")-print("=" * 60)--for trt in ['A', 'B']:- mask = df['treatment'] == trt- y_trt = Surv.from_arrays(- event=(df.loc[mask, 'event_type'] > 0),- time=df.loc[mask, 'time']- )- time_trt, cif1_trt, cif2_trt = cumulative_incidence_competing_risks(y_trt)- print(f"\nTreatment {trt}:")- print(f" 5-year relapse: {cif1_trt[-1]:.2%}")- print(f" 5-year death: {cif2_trt[-1]:.2%}")--# 3. CAUSE-SPECIFIC MODELS-print("\n" + "=" * 60)-print("CAUSE-SPECIFIC HAZARD MODELS")-print("=" * 60)--X = df[['age', 'treatment_A']]--# Model for relapse (event type 1)-y_relapse = Surv.from_arrays(- event=(df['event_type'] == 1),- time=df['time']-)-cox_relapse = CoxPHSurvivalAnalysis()-cox_relapse.fit(X, y_relapse)+- a named cause;+- a case/control definition at each horizon;+- handling of other causes consistent with that definition;+- cause-specific probability predictions for calibration/Brier evaluation;+- censoring weights fitted on training data;+- evaluation times supported by training follow-up;+- nested tuning or an untouched holdout.-print("\nRelapse Model:")-print(f" Age: HR = {np.exp(cox_relapse.coef_[0]):.3f}")-print(f" Treatment A: HR = {np.exp(cox_relapse.coef_[1]):.3f}")+Do not label an all-event C-index as cause-specific discrimination, and do not use+an all-event survival probability as a cause-specific CIF.-# Model for death (event type 2)-y_death = Surv.from_arrays(- event=(df['event_type'] == 2),- time=df['time']-)-cox_death = CoxPHSurvivalAnalysis()-cox_death.fit(X, y_death)+## Bundled helper-print("\nDeath Model:")-print(f" Age: HR = {np.exp(cox_death.coef_[0]):.3f}")-print(f" Treatment A: HR = {np.exp(cox_death.coef_[1]):.3f}")+The helper defaults to deterministic synthetic data:-print("\n" + "=" * 60)+```bash+python skills/scikit-survival/scripts/competing_risk_cif.py```-## Important Considerations--### Censoring in Competing Risks--- **Administrative censoring**: Subject still at risk at end of study-- **Loss to follow-up**: Subject leaves study before event-- **Competing event**: Other event occurred - NOT censored for CIF, but censored for cause-specific models+For local CSV:++```bash+python skills/scikit-survival/scripts/competing_risk_cif.py \+ --input competing.csv \+ --event-column status \+ --time-column time \+ --horizons 2,5,10 \+ --confidence \+ --curve-output cif-curves.npz \+ --output cif-summary.json+```-### Choosing Between Cause-Specific and Sub-distribution Models+It:-**Cause-Specific Hazard Models:**-- Easier to interpret-- Direct effect on hazard rate-- Better for understanding etiology-- Can fit with scikit-survival+- rejects URLs, symlinks, missing/non-contiguous causes, and invalid times;+- bounds file size and row count;+- verifies that cause-specific rows sum to total CIF;+- writes numeric arrays without pickle;+- reports point estimates at requested horizons;+- makes no network calls.-**Fine-Gray Sub-distribution Models:**-- Models cumulative incidence directly-- Better for prediction and risk stratification-- More appropriate for clinical decision-making-- Requires other packages+Use only authorized, de-identified local data. Do not include row-level data or PHI+in reports.-### Common Mistakes+## Reporting checklist-**Mistake 1**: Using Kaplan-Meier to estimate event-specific probabilities-- **Wrong**: Kaplan-Meier for event type 1, treating type 2 as censored-- **Correct**: Cumulative incidence function accounting for competing risks+- cause definitions and code mapping;+- censoring definition and follow-up window;+- CIF versus cause-specific or subdistribution hazard estimand;+- number of rows/events for every cause;+- horizon-specific CIF with uncertainty and support;+- whether intervals are pointwise;+- handling of `time_min`, if any;+- competing-risk-specific prediction evaluation;+- no causal or clinical-utility claim from association/probability alone.-**Mistake 2**: Ignoring competing risks when they're substantial-- If competing event rate > 10-20%, should use competing risks methods+## Sources-**Mistake 3**: Confusing cause-specific and sub-distribution hazards-- They answer different questions-- Use appropriate model for your research question+Official scikit-survival sources checked 2026-07-23:-## Summary+- [Competing-risks user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/competing-risks.html)+- [CIF API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.nonparametric.cumulative_incidence_competing_risks.html)+- [Dataset API](https://scikit-survival.readthedocs.io/en/stable/api/datasets.html)+- [0.24 release notes introducing CIF](https://scikit-survival.readthedocs.io/en/stable/release_notes/v0.24.html)-**Key Functions:**-- `cumulative_incidence_competing_risks`: Estimate CIF for each event type-- Fit separate Cox models for cause-specific hazards-- Use stratified analysis to compare groups+Primary methods:-**Best Practices:**-1. Always plot cumulative incidence functions-2. Report both event-specific and overall incidence-3. Use cause-specific models in scikit-survival-4. Consider Fine-Gray models (other packages) for prediction-5. Be explicit about which events are competing vs censored+- Aalen O. "Nonparametric estimation of partial transition probabilities in+ multiple decrement models." *Annals of Statistics* 6 (1978), 534-545.+ [Project Euclid record](https://projecteuclid.org/journals/annals-of-statistics/volume-6/issue-3/Nonparametric-Estimation-of-Partial-Transition-Probabilities-in-Multiple-Decrement-Models/10.1214/aos/1176344198.full)+- Gray RJ. "A class of K-sample tests for comparing the cumulative incidence of+ a competing risk." *Annals of Statistics* 16 (1988), 1141-1154.+ [doi:10.1214/aos/1176350951](https://doi.org/10.1214/aos/1176350951)diff --git a/skills/scikit-survival/references/cox-models.md b/skills/scikit-survival/references/cox-models.mdindex d66550a0..01d79733 100644--- a/skills/scikit-survival/references/cox-models.md+++ b/skills/scikit-survival/references/cox-models.md@@ -1,182 +1,252 @@-# Cox Proportional Hazards Models+# Cox, Coxnet, and IPC ridge models-## Overview+Verified for scikit-survival 0.28.0 on 2026-07-23.-Cox proportional hazards models are semi-parametric models that relate covariates to the time of an event. The hazard function for individual *i* is expressed as:+## Cox proportional hazards model-**h_i(t) = h_0(t) × exp(β^T x_i)**+For covariates \(x\),-where:-- h_0(t) is the baseline hazard function (unspecified)-- β is the vector of coefficients-- x_i is the covariate vector for individual *i*+\[+h(t \mid x) = h_0(t)\exp(x^\top\beta).+\]-The key assumption is that the hazard ratio between two individuals is constant over time (proportional hazards).+`CoxPHSurvivalAnalysis` estimates coefficients by partial likelihood. The model+assumes covariate effects multiply the hazard by a time-constant factor. A fitted+coefficient is a log hazard ratio only under the model, coding, scale, and PH+assumptions.-## CoxPHSurvivalAnalysis--Basic Cox proportional hazards model for survival analysis.--### When to Use-- Standard survival analysis with censored data-- Need interpretable coefficients (log hazard ratios)-- Proportional hazards assumption holds-- Dataset has relatively few features--### Key Parameters-- `alpha`: Regularization parameter (default: 0, no regularization)-- `ties`: Method for handling tied event times ('breslow' or 'efron')-- `n_iter`: Maximum number of iterations for optimization--### Example Usage```pythonfrom sksurv.linear_model import CoxPHSurvivalAnalysis-from sksurv.datasets import load_gbsg2-# Load data-X, y = load_gbsg2()+model = CoxPHSurvivalAnalysis(+ alpha=0.1,+ ties="efron",+ n_iter=100,+ tol=1e-9,+)+model.fit(X_train, y_train)+risk = model.predict(X_test)+survival = model.predict_survival_function(X_test)+hazard = model.predict_cumulative_hazard_function(X_test)+```-# Fit Cox model-estimator = CoxPHSurvivalAnalysis()-estimator.fit(X, y)+Current key parameters:-# Get coefficients (log hazard ratios)-coefficients = estimator.coef_+- `alpha`: non-negative L2/ridge penalty. It may be a scalar or feature-specific+ vector where documented. `alpha=0` is unpenalized.+- `ties`: `"breslow"` (default) or `"efron"`.+- `n_iter`, `tol`, `verbose`: Newton-Raphson controls.-# Predict risk scores-risk_scores = estimator.predict(X)-```+`predict()` returns the linear predictor \(x^\top\hat\beta\); higher means higher+event risk. Absolute survival probabilities come from the fitted baseline survival,+not from transforming a risk score by itself.-## CoxnetSurvivalAnalysis+### Stability and interpretation-Cox model with elastic net penalty for feature selection and regularization.+- Encode and scale inside a training-fitted pipeline.+- Use ridge shrinkage for unstable or correlated designs; a successful numerical+ fit does not establish inferential validity.+- Check coefficient sensitivity to coding, scaling, missingness, influential rows,+ and regularization.+- Exponentiating a coefficient gives a model-based hazard ratio for one unit of its+ encoded feature, holding other modeled features fixed.+- A hazard ratio is not a risk ratio, probability difference, causal effect, or+ clinical utility measure.-### When to Use-- High-dimensional data (many features)-- Need automatic feature selection-- Want to handle multicollinearity-- Require sparse models+scikit-survival does not provide a complete PH-diagnostics workflow. Assess the PH+assumption using residual/graphical/domain methods appropriate to the study. If it+fails, consider time interactions, stratification in a method that supports it, a+time-varying model, an AFT model, or a flexible prediction model. Do not merely+switch models and retain Cox coefficient interpretation.-### Penalty Types-- **Ridge (L2)**: alpha_min_ratio=1.0, l1_ratio=0- - Shrinks all coefficients- - Good when all features are relevant+## Penalized Cox path with Coxnet-- **Lasso (L1)**: l1_ratio=1.0- - Performs feature selection (sets coefficients to zero)- - Good for sparse models+`CoxnetSurvivalAnalysis` implements a Cox elastic-net path:-- **Elastic Net**: 0 < l1_ratio < 1- - Combination of L1 and L2- - Balances feature selection and grouping+\[+\text{penalty} =+\alpha\left(\rho\|\beta\|_1 + \frac{1-\rho}{2}\|\beta\|_2^2\right),+\]-### Key Parameters-- `l1_ratio`: Balance between L1 and L2 penalty (0=Ridge, 1=Lasso)-- `alpha_min_ratio`: Ratio of smallest to largest penalty in regularization path-- `n_alphas`: Number of alphas along regularization path-- `fit_baseline_model`: Whether to fit unpenalized baseline model+where `l1_ratio` is \(\rho\).-### Example Usage```pythonfrom sksurv.linear_model import CoxnetSurvivalAnalysis-# Fit with elastic net penalty-estimator = CoxnetSurvivalAnalysis(l1_ratio=0.5, alpha_min_ratio=0.01)-estimator.fit(X, y)--# Access regularization path-alphas = estimator.alphas_-coefficients_path = estimator.coef_path_--# Predict with specific alpha-risk_scores = estimator.predict(X, alpha=0.1)+model = CoxnetSurvivalAnalysis(+ n_alphas=100,+ alpha_min_ratio="auto",+ l1_ratio=0.9,+ fit_baseline_model=True,+)+model.fit(X_train_scaled, y_train)```-### Cross-Validation for Alpha Selection+Current details:++- `l1_ratio` must be in `(0, 1]`; `1.0` is LASSO and values below 1 mix L1/L2.+ Exact pure ridge is handled by `CoxPHSurvivalAnalysis(alpha=...)`, not by setting+ `l1_ratio=0`.+- `alphas=None` estimates a decreasing path; explicit `alphas` selects the path.+- `alpha_min_ratio` controls the smallest/largest path ratio. It is not the+ L1/L2 mixing parameter.+- `penalty_factor` can vary penalties by feature; zero leaves a feature unpenalized.+- `normalize=False` is current. Prefer an explicit `StandardScaler` pipeline so+ fold behavior and feature scaling are visible.+- `coef_` has shape `(n_features, n_alphas)`. There is no current `coef_path_`+ attribute.+- `predict(X, alpha=...)` uses the selected path point (or interpolation).+- `predict_survival_function()` and+ `predict_cumulative_hazard_function()` require+ `fit_baseline_model=True`.++### Leakage-safe alpha selection++Do not estimate the alpha path on all rows and then claim nested-CV performance.+For a final held-out evaluation:++1. split train/test;+2. define an alpha grid from subject-matter scale or from training data only;+3. fit scaler and Coxnet inside each inner fold;+4. tune alpha on inner validation folds;+5. evaluate the selected procedure in an outer fold or untouched test set.+```pythonfrom sklearn.model_selection import GridSearchCV-from sksurv.metrics import concordance_index_censored--# Define parameter grid-param_grid = {'l1_ratio': [0.1, 0.5, 0.9],- 'alpha_min_ratio': [0.01, 0.001]}+from sklearn.pipeline import make_pipeline+from sklearn.preprocessing import StandardScaler++pipeline = make_pipeline(+ StandardScaler(),+ CoxnetSurvivalAnalysis(l1_ratio=0.9, fit_baseline_model=True),+)+search = GridSearchCV(+ pipeline,+ {+ "coxnetsurvivalanalysis__alphas": [+ [0.01],+ [0.05],+ [0.2],+ ]+ },+ cv=inner_splits,+ error_score="raise",+)+search.fit(X_outer_train, y_outer_train)+```-# Grid search with C-index-cv = GridSearchCV(CoxnetSurvivalAnalysis(),- param_grid,- scoring='concordance_index_ipcw',- cv=5)-cv.fit(X, y)+When using censoring-aware scorer wrappers, wrap the entire pipeline and prefix+the parameter again:-# Best parameters-best_params = cv.best_params_+```python+from sksurv.metrics import as_concordance_index_ipcw_scorer++wrapped = as_concordance_index_ipcw_scorer(pipeline, tau=tau)+search = GridSearchCV(+ wrapped,+ {+ "estimator__coxnetsurvivalanalysis__alphas": [+ [0.01],+ [0.05],+ [0.2],+ ]+ },+ cv=inner_splits,+)```-## IPCRidge+The wrapper is the estimator passed to `GridSearchCV`; it is not a zero-argument+`scoring` callable. Its fit fold supplies the censoring distribution. Time support+still has to be valid in every score fold.++### Feature selection is uncertain-Inverse probability of censoring weighted Ridge regression for accelerated failure time models.+Non-zero coefficients at one alpha do not prove that a feature is biologically,+causally, or clinically important. Report:-### When to Use-- Prefer accelerated failure time (AFT) framework over proportional hazards-- Need to model how features accelerate/decelerate survival time-- High censoring rates-- Want regularization with Ridge penalty+- the exact preprocessing and penalty grid;+- nested-CV or holdout protocol;+- coefficient stability across resamples;+- correlated alternatives and selection frequency;+- the selected alpha and `l1_ratio`;+- calibration and discrimination on independent data.-### Key Difference from Cox Models-AFT models assume features multiply survival time by a constant factor, rather than multiplying the hazard rate. The model predicts log survival time directly.+## IPC ridge AFT model++`IPCRidge` is an inverse-probability-of-censoring weighted ridge regression model+for a log-time/AFT objective:-### Example Usage```pythonfrom sksurv.linear_model import IPCRidge-# Fit IPCRidge model-estimator = IPCRidge(alpha=1.0)-estimator.fit(X, y)+model = IPCRidge(alpha=1.0)+model.fit(X_train_scaled, y_train)+predicted_log_time = model.predict(X_test_scaled)+```++This output is time-oriented: larger predicted values imply longer predicted+survival time, unlike higher-is-riskier Cox scores. Do not pass it unchanged to+metrics expecting higher event risk. If a discrimination analysis requires a+risk direction, use the negative prediction and state that transformation.++IPCW estimation relies on censoring assumptions and support. High censoring is not+by itself a license to prefer the model; inspect weight stability and whether the+training censoring distribution is positive over the target range.++## Time-dependent prediction++For Cox PH:++\[+S(t \mid x) = S_0(t)^{\exp(x^\top\beta)}.+\]-# Predict log survival time-log_time = estimator.predict(X)+Evaluate returned step functions on a shared, train-supported grid:++```python+import numpy as np++functions = model.predict_survival_function(X_test)+survival_probability = np.vstack([fn(times) for fn in functions])```-## Model Comparison and Selection+The result is `(n_test, n_times)` and is suitable for Brier metrics when `times`+also satisfies the metric's test/training support constraints. Extrapolation+beyond learned event-time support is not justified.-### Choosing Between Models+## Calibration and claims-**Use CoxPHSurvivalAnalysis when:**-- Small to moderate number of features-- Want interpretable hazard ratios-- Standard survival analysis setting+Risk ranking can remain similar after a monotone transformation while probability+calibration changes. Therefore:-**Use CoxnetSurvivalAnalysis when:**-- High-dimensional data (p >> n)-- Need feature selection-- Want to identify important predictors-- Presence of multicollinearity+- report C-index or dynamic AUC as discrimination;+- report Brier score as probability prediction error;+- inspect horizon-specific calibration separately;+- validate on data independent of fitting and tuning;+- do not infer treatment effects from predictive Cox coefficients;+- do not call a model clinically useful without decision-focused evaluation.-**Use IPCRidge when:**-- AFT framework is more appropriate-- High censoring rates-- Want to model time directly rather than hazard+## Metadata routing-### Checking Proportional Hazards Assumption+Current estimators expose `get_metadata_routing()`. Coxnet also exposes+`set_predict_request(alpha=...)` for passing its optional `alpha` prediction+argument through a meta-estimator. This only matters when:-The proportional hazards assumption should be verified using:-- Schoenfeld residuals-- Log-log survival plots-- Statistical tests (available in other packages like lifelines)+```python+from sklearn import set_config++set_config(enable_metadata_routing=True)+```-If violated, consider:-- Stratification by violating covariates-- Time-varying coefficients-- Alternative models (AFT, parametric models)+and an enclosing meta-estimator is expected to route that metadata. Ordinary+`pipeline.fit(X, y)` and direct `pipeline.predict(X)` do not require enabling it.-## Interpretation+## Sources-### Cox Model Coefficients-- Positive coefficient: increased hazard (shorter survival)-- Negative coefficient: decreased hazard (longer survival)-- Hazard ratio = exp(β) for one-unit increase in covariate-- Example: β=0.693 → HR=2.0 (doubles the hazard)+Official sources checked 2026-07-23:-### Risk Scores-- Higher risk score = higher risk of event = shorter expected survival-- Risk scores are relative; use survival functions for absolute predictions+- [CoxPHSurvivalAnalysis API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.linear_model.CoxPHSurvivalAnalysis.html)+- [CoxnetSurvivalAnalysis API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.linear_model.CoxnetSurvivalAnalysis.html)+- [IPCRidge API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.linear_model.IPCRidge.html)+- [Penalized Cox user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/coxnet.html)+- [Understanding predictions](https://scikit-survival.readthedocs.io/en/stable/user_guide/understanding_predictions.html)diff --git a/skills/scikit-survival/references/data-handling.md b/skills/scikit-survival/references/data-handling.mdindex 7fd4cbf3..402933e1 100644--- a/skills/scikit-survival/references/data-handling.md+++ b/skills/scikit-survival/references/data-handling.md@@ -1,494 +1,278 @@-# Data Handling and Preprocessing+# Data handling and leakage-safe preprocessing-## Understanding Survival Data+Verified for scikit-survival 0.28.0 on 2026-07-23.-### The Surv Object+## Standard right-censored outcome-Survival data in scikit-survival is represented using structured arrays with two fields:-- **event**: Boolean indicating whether the event occurred (True) or was censored (False)-- **time**: Time to event or censoring+scikit-survival estimators expect a one-dimensional NumPy structured array with+exactly two fields:-```python-from sksurv.util import Surv--# Create survival outcome from separate arrays-event = np.array([True, False, True, False, True])-time = np.array([5.2, 10.1, 3.7, 8.9, 6.3])--y = Surv.from_arrays(event=event, time=time)-print(y.dtype) # [('event', '?'), ('time', '<f8')]-```--### Types of Censoring--**Right Censoring** (most common):-- Subject didn't experience event by end of study-- Subject lost to follow-up-- Subject withdrew from study+1. a boolean event indicator (`True`=event observed, `False`=right-censored);+2. a floating-point observed time (event or censoring time).-**Left Censoring**:-- Event occurred before observation began-- Rare in practice--**Interval Censoring**:-- Event occurred in a known time interval-- Requires specialized methods--scikit-survival primarily handles right-censored data.--## Loading Data--### Built-in Datasets+Field names are configurable, but field order and meaning are fixed.```python-from sksurv.datasets import (- load_aids,- load_breast_cancer,- load_gbsg2,- load_veterans_lung_cancer,- load_whas500-)--# Load dataset-X, y = load_breast_cancer()--# X is pandas DataFrame with features-# y is structured array with 'event' and 'time'-print(f"Features shape: {X.shape}")-print(f"Number of events: {y['event'].sum()}")-print(f"Censoring rate: {1 - y['event'].mean():.2%}")-```--### Loading Custom Data--#### From Pandas DataFrame--```python-import pandas as pdfrom sksurv.util import Surv-# Load data-df = pd.read_csv('survival_data.csv')--# Separate features and outcome-X = df.drop(['time', 'event'], axis=1)-y = Surv.from_dataframe('event', 'time', df)-```--#### From CSV with Surv.from_arrays--```python-import numpy as np-import pandas as pd-from sksurv.util import Surv--# Load data-df = pd.read_csv('survival_data.csv')--# Create feature matrix-X = df.drop(['time', 'event'], axis=1)--# Create survival outcomey = Surv.from_arrays(- event=df['event'].astype(bool),- time=df['time'].astype(float)+ event=[True, False, True],+ time=[2.5, 4.0, 7.25],+ name_event="event",+ name_time="time",)+assert y.dtype.names == ("event", "time")```-### Loading ARFF Files+`Surv.from_arrays()` accepts boolean or strict 0/1 event values.+`Surv.from_dataframe(event, time, data)` accepts pandas and, in 0.28, Polars+DataFrames:```python-from sksurv.io import loadarff--# Load ARFF format (Weka format)-data = loadarff('survival_data.arff')--# Extract X and y-X = data[0] # pandas DataFrame-y = data[1] # structured array+y = Surv.from_dataframe("event", "time", frame)```-## Data Preprocessing+Do not use `astype(bool)` on unvalidated strings: `"False"` is a non-empty string+and therefore converts to `True`. Validate accepted values explicitly.-### Handling Categorical Variables+## Competing-risk outcome is different-#### Method 1: OneHotEncoder (scikit-survival)+`Surv` is not the input contract for nonparametric competing-risk cumulative+incidence. Use two arrays:```python-from sksurv.preprocessing import OneHotEncoder-import pandas as pd--# Identify categorical columns-categorical_cols = ['gender', 'race', 'treatment']--# One-hot encode-encoder = OneHotEncoder()-X_encoded = encoder.fit_transform(X[categorical_cols])--# Combine with numerical features-numerical_cols = [col for col in X.columns if col not in categorical_cols]-X_processed = pd.concat([X[numerical_cols], X_encoded], axis=1)+# 0 = right-censored; 1..K = mutually exclusive causes+event_code = frame["status"].to_numpy(dtype=int)+observed_time = frame["time"].to_numpy(dtype=float)```-#### Method 2: encode_categorical--```python-from sksurv.preprocessing import encode_categorical--# Automatically encode all categorical columns-X_encoded = encode_categorical(X)+Positive cause codes must be understood before modeling. Do not collapse them to+boolean until the estimand explicitly requires all-cause event status or a+cause-specific hazard outcome. See `competing-risks.md`.++## Minimum validation++Before splitting:++- event and time lengths match feature rows;+- standard event values are boolean/0/1;+- competing-risk codes are non-negative integers and 0 means censoring;+- time is numeric, finite, and strictly positive;+- outcomes are not included among predictors;+- feature names are unique and schema roles are explicit;+- repeated entities, temporal ordering, or sites are identified for the split;+- every planned training/CV fold contains events and censored observations;+- missingness is described, but imputation is not yet fitted.++The bundled validator performs these checks on bounded local CSV input:++```bash+python skills/scikit-survival/scripts/validate_survival_csv.py \+ --input data.csv \+ --event-column event \+ --time-column time \+ --feature-columns x1,x2,group \+ --structured-output outcome.npy```-#### Method 3: Pandas get_dummies+The `.npy` file contains a non-object structured array and can be loaded with+`numpy.load(path, allow_pickle=False)`.-```python-import pandas as pd+## Split before learned preprocessing-# One-hot encode categorical variables-X_encoded = pd.get_dummies(X, drop_first=True)-```+This order is mandatory:-### Standardization+1. validate types and outcome coding;+2. split rows;+3. fit imputation, encoding, scaling, and feature selection on training rows;+4. transform validation/test rows with training-fitted state;+5. fit the survival estimator;+6. evaluate once on the held-out rows.-Standardization is important for:-- Cox models with regularization-- SVMs-- Models sensitive to feature scales+Computing medians, category levels, scaling moments, univariate scores, or+regularization paths on all rows leaks validation/test information.```python-from sklearn.preprocessing import StandardScaler--# Standardize features-scaler = StandardScaler()-X_scaled = scaler.fit_transform(X)+from sklearn.model_selection import train_test_split-# Convert back to DataFrame-X_scaled = pd.DataFrame(X_scaled, columns=X.columns, index=X.index)+X_train, X_test, y_train, y_test = train_test_split(+ X,+ y,+ test_size=0.25,+ stratify=y["event"],+ random_state=20260723,+)```-### Handling Missing Data+Event stratification does not guarantee balanced follow-up times. Inspect each+split. Use group-aware splitting for repeated entities and time-respecting+splitting for future-deployment questions. A random split is not automatically+appropriate.-#### Check for Missing Values+## Explicit heterogeneous pipeline-```python-# Check missing values-missing = X.isnull().sum()-print(missing[missing > 0])--# Visualize missing data-import seaborn as sns-sns.heatmap(X.isnull(), cbar=False)-```--#### Imputation Strategies+Use scikit-learn's `ColumnTransformer` when numeric and categorical columns need+different imputers or when unseen categories must be handled explicitly.```python+from sklearn.compose import ColumnTransformerfrom sklearn.impute import SimpleImputer+from sklearn.pipeline import make_pipeline+from sklearn.preprocessing import OneHotEncoder, StandardScaler+from sksurv.linear_model import CoxPHSurvivalAnalysis-# Mean imputation for numerical features-num_imputer = SimpleImputer(strategy='mean')-X_num = X.select_dtypes(include=[np.number])-X_num_imputed = num_imputer.fit_transform(X_num)--# Most frequent for categorical-cat_imputer = SimpleImputer(strategy='most_frequent')-X_cat = X.select_dtypes(include=['object', 'category'])-X_cat_imputed = cat_imputer.fit_transform(X_cat)-```--#### Advanced Imputation--```python-from sklearn.experimental import enable_iterative_imputer-from sklearn.impute import IterativeImputer--# Iterative imputation-imputer = IterativeImputer(random_state=42)-X_imputed = imputer.fit_transform(X)-```--### Feature Selection--#### Variance Threshold--```python-from sklearn.feature_selection import VarianceThreshold--# Remove low variance features-selector = VarianceThreshold(threshold=0.01)-X_selected = selector.fit_transform(X)--# Get selected feature names-selected_features = X.columns[selector.get_support()]+numeric_pipe = make_pipeline(+ SimpleImputer(strategy="median"),+ StandardScaler(),+)+categorical_pipe = make_pipeline(+ SimpleImputer(strategy="most_frequent"),+ OneHotEncoder(+ handle_unknown="ignore",+ drop="first",+ sparse_output=False,+ ),+)+preprocess = ColumnTransformer(+ [+ ("numeric", numeric_pipe, numeric_columns),+ ("categorical", categorical_pipe, categorical_columns),+ ],+ sparse_threshold=0.0,+)+pipeline = make_pipeline(+ preprocess,+ CoxPHSurvivalAnalysis(alpha=0.1, ties="efron"),+)+pipeline.fit(X_train, y_train)```-#### Univariate Feature Selection+Keep the estimator in the same pipeline used by cross-validation so each fold+fits its own preprocessing state.-```python-from sklearn.feature_selection import SelectKBest-from sksurv.util import Surv+### scikit-survival encoder-# Select top k features-selector = SelectKBest(k=10)-X_selected = selector.fit_transform(X, y)+`sksurv.preprocessing.OneHotEncoder(allow_drop=True)`:-# Get selected features-selected_features = X.columns[selector.get_support()]-```+- treats pandas `category`/`object` and Polars categorical/enum/string columns as+ categorical;+- leaves non-categorical column order in place;+- drops one category per categorical feature;+- returns the same DataFrame library as its input;+- requires `fit` and `transform` to use the same DataFrame library;+- supports `get_feature_names_out()` and pipeline use.-## Complete Preprocessing Pipeline--### Using sklearn Pipeline+It is convenient for already clean DataFrames:```python-from sklearn.pipeline import Pipeline-from sklearn.preprocessing import StandardScaler-from sklearn.impute import SimpleImputer-from sksurv.linear_model import CoxPHSurvivalAnalysis--# Create preprocessing and modeling pipeline-pipeline = Pipeline([- ('imputer', SimpleImputer(strategy='mean')),- ('scaler', StandardScaler()),- ('model', CoxPHSurvivalAnalysis())-])--# Fit pipeline-pipeline.fit(X, y)--# Predict-predictions = pipeline.predict(X_test)-```--### Custom Preprocessing Function+from sklearn.pipeline import make_pipeline+from sksurv.preprocessing import OneHotEncoder-```python-def preprocess_survival_data(X, y=None, scaler=None, encoder=None):- """- Complete preprocessing pipeline for survival data-- Parameters:- ------------ X : DataFrame- Feature matrix- y : structured array, optional- Survival outcome (for filtering invalid samples)- scaler : StandardScaler, optional- Fitted scaler (for test data)- encoder : OneHotEncoder, optional- Fitted encoder (for test data)-- Returns:- --------- X_processed : DataFrame- Processed features- scaler : StandardScaler- Fitted scaler- encoder : OneHotEncoder- Fitted encoder- """- from sklearn.preprocessing import StandardScaler- from sksurv.preprocessing import encode_categorical-- # 1. Handle missing values- # Remove rows with missing outcome- if y is not None:- mask = np.isfinite(y['time']) & (y['time'] > 0)- X = X[mask]- y = y[mask]-- # Impute missing features- X = X.fillna(X.median())-- # 2. Encode categorical variables- if encoder is None:- X_processed = encode_categorical(X)- encoder = None # encode_categorical doesn't return encoder- else:- X_processed = encode_categorical(X)-- # 3. Standardize numerical features- if scaler is None:- scaler = StandardScaler()- X_processed = pd.DataFrame(- scaler.fit_transform(X_processed),- columns=X_processed.columns,- index=X_processed.index- )- else:- X_processed = pd.DataFrame(- scaler.transform(X_processed),- columns=X_processed.columns,- index=X_processed.index- )-- if y is not None:- return X_processed, y, scaler, encoder- else:- return X_processed, scaler, encoder--# Usage-X_train_processed, y_train_processed, scaler, encoder = preprocess_survival_data(X_train, y_train)-X_test_processed, _, _ = preprocess_survival_data(X_test, scaler=scaler, encoder=encoder)+pipeline = make_pipeline(+ OneHotEncoder(),+ CoxPHSurvivalAnalysis(alpha=0.1),+)+pipeline.fit(X_train, y_train)```-## Data Quality Checks--### Validate Survival Data--```python-def validate_survival_data(y):- """Check survival data quality"""-- # Check for negative times- if np.any(y['time'] <= 0):- print("WARNING: Found non-positive survival times")- print(f"Negative times: {np.sum(y['time'] <= 0)}")-- # Check for missing values- if np.any(~np.isfinite(y['time'])):- print("WARNING: Found missing survival times")- print(f"Missing times: {np.sum(~np.isfinite(y['time']))}")-- # Censoring rate- censor_rate = 1 - y['event'].mean()- print(f"Censoring rate: {censor_rate:.2%}")+For custom files, an explicit `ColumnTransformer` usually makes missing-value,+unknown-category, and scaling behavior easier to audit.- if censor_rate > 0.7:- print("WARNING: High censoring rate (>70%)")- print("Consider using Uno's C-index instead of Harrell's")+`encode_categorical()` is a one-shot transformation, not a fitted train/test+transformer. Do not call it separately on all data or independently on train and+test when category sets can differ.- # Event rate- print(f"Number of events: {y['event'].sum()}")- print(f"Number of censored: {(~y['event']).sum()}")-- # Time statistics- print(f"Median time: {np.median(y['time']):.2f}")- print(f"Time range: [{np.min(y['time']):.2f}, {np.max(y['time']):.2f}]")--# Use validation-validate_survival_data(y)-```+## Scaling and missing values-### Check for Sufficient Events+- Scale Coxnet, survival SVM, IPC ridge, and other coefficient/penalty models.+- Tree ensembles generally do not require scaling.+- Impute inside the pipeline unless the selected estimator explicitly supports the+ observed missing-value pattern.+- SurvivalTree, RandomSurvivalForest, and ExtraSurvivalTrees support missing-value+ splitting in current releases, but preprocessing may still be needed for+ categorical data and operational consistency.+- Never impute event indicators or event/censoring times as ordinary features.-```python-def check_events_per_feature(X, y, min_events_per_feature=10):- """- Check if there are sufficient events per feature.- Rule of thumb: at least 10 events per feature for Cox models.- """- n_events = y['event'].sum()- n_features = X.shape[1]- events_per_feature = n_events / n_features-- print(f"Number of events: {n_events}")- print(f"Number of features: {n_features}")- print(f"Events per feature: {events_per_feature:.1f}")-- if events_per_feature < min_events_per_feature:- print(f"WARNING: Low events per feature ratio (<{min_events_per_feature})")- print("Consider:")- print(" - Feature selection")- print(" - Regularization (CoxnetSurvivalAnalysis)")- print(" - Collecting more data")-- return events_per_feature--# Use check-check_events_per_feature(X, y)-```+Missingness can be informative. A convenient imputer does not justify a+missing-at-random assumption or transportability claim.-## Train-Test Split+## Feature selection-### Random Split+Feature selection is learned preprocessing and belongs inside inner CV:```python-from sklearn.model_selection import train_test_split--# Split data-X_train, X_test, y_train, y_test = train_test_split(- X, y, test_size=0.2, random_state=42+pipeline = make_pipeline(+ preprocess,+ selector,+ estimator,)```-### Stratified Split+Do not use a standard classification `SelectKBest` score with structured survival+outcomes unless the score function explicitly supports censoring. Coxnet or+componentwise boosting can perform embedded selection, but regularization strength+still requires fold-contained tuning.-Ensure similar censoring rates and time distributions:+Fixed "events per variable" thresholds are not universal guarantees. Consider+effective degrees of freedom, censoring, shrinkage, separation, stability, and+external validation instead of declaring a model valid from one ratio.-```python-from sklearn.model_selection import train_test_split+## Built-in datasets-# Create stratification labels-# Stratify by event status and time quartiles-time_quartiles = pd.qcut(y['time'], q=4, labels=False)-strat_labels = y['event'].astype(int) * 10 + time_quartiles+Current loaders:-# Stratified split-X_train, X_test, y_train, y_test = train_test_split(- X, y, test_size=0.2, stratify=strat_labels, random_state=42-)+- `load_aids(endpoint=...)`+- `load_bmt()`+- `load_cgvhd()`+- `load_breast_cancer()`+- `load_flchain()`+- `load_gbsg2()`+- `load_whas500()`+- `load_veterans_lung_cancer()`+- `load_arff_files_standardized(...)`-# Verify similar distributions-print("Training set:")-print(f" Censoring rate: {1 - y_train['event'].mean():.2%}")-print(f" Median time: {np.median(y_train['time']):.2f}")+`load_bmt()` returns event codes 0, 1, 2 and `load_cgvhd()` returns 0, 1, 2, 3;+these are competing-risk outcomes. The remaining listed study loaders return the+standard boolean right-censored outcome (subject to endpoint options).-print("Test set:")-print(f" Censoring rate: {1 - y_test['event'].mean():.2%}")-print(f" Median time: {np.median(y_test['time']):.2f}")-```+These packaged datasets are useful for reproducing documentation, but they are+real study datasets. The bundled CLIs and tests do not use them; they use synthetic+data only. Do not treat examples as clinical advice or a substitute for data-use+review.-## Working with Time-Varying Covariates+In 0.28, dataset loaders accept an `output_type` option where documented, allowing+pandas (default) or Polars feature output.-Note: scikit-survival doesn't directly support time-varying covariates. For such data, consider:-1. Time-stratified analysis-2. Landmarking approach-3. Using other packages (e.g., lifelines)+## Unsupported or specialized structures-## Summary: Complete Data Preparation Workflow+Standard estimators do not directly encode:-```python-from sksurv.util import Surv-from sklearn.model_selection import train_test_split-from sklearn.preprocessing import StandardScaler-from sksurv.preprocessing import encode_categorical-import pandas as pd-import numpy as np+- interval-censored outcomes;+- ordinary left-censoring;+- time-varying covariates in counting-process form;+- recurrent-event dependence;+- multi-state transitions;+- delayed entry in the two-field `Surv` estimator outcome.-# 1. Load data-df = pd.read_csv('data.csv')+Some nonparametric APIs expose entry-time arguments, but that does not make every+estimator support left truncation. Choose a method whose likelihood and input+contract match the observation process.-# 2. Create survival outcome-y = Surv.from_dataframe('event', 'time', df)+## Local-data safeguards-# 3. Prepare features-X = df.drop(['event', 'time'], axis=1)+- Use only authorized, appropriately de-identified local data.+- Do not put row-level data in logs or model reports.+- Do not load untrusted pickle/joblib model files.+- Keep feature and row bounds proportionate to available resources.+- Use descriptive script names; never create files named after packages such as+ `sklearn.py` or `sksurv.py`.-# 4. Validate data-validate_survival_data(y)-check_events_per_feature(X, y)+## Sources-# 5. Handle missing values-X = X.fillna(X.median())+Official sources checked 2026-07-23:-# 6. Split data-X_train, X_test, y_train, y_test = train_test_split(- X, y, test_size=0.2, random_state=42-)--# 7. Encode categorical variables-X_train = encode_categorical(X_train)-X_test = encode_categorical(X_test)--# 8. Standardize-scaler = StandardScaler()-X_train_scaled = scaler.fit_transform(X_train)-X_test_scaled = scaler.transform(X_test)--# Convert back to DataFrames-X_train_scaled = pd.DataFrame(X_train_scaled, columns=X_train.columns)-X_test_scaled = pd.DataFrame(X_test_scaled, columns=X_test.columns)--# Now ready for modeling!-```+- [Surv API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.util.Surv.html)+- [OneHotEncoder API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.preprocessing.OneHotEncoder.html)+- [Dataset API](https://scikit-survival.readthedocs.io/en/stable/api/datasets.html)+- [0.28 release notes](https://scikit-survival.readthedocs.io/en/stable/release_notes/v0.28.html)+- [Introduction user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/00-introduction.html)diff --git a/skills/scikit-survival/references/ensemble-models.md b/skills/scikit-survival/references/ensemble-models.mdindex 3e918245..af04a1b5 100644--- a/skills/scikit-survival/references/ensemble-models.md+++ b/skills/scikit-survival/references/ensemble-models.md@@ -1,327 +1,287 @@-# Ensemble Models for Survival Analysis+# Survival trees, forests, and boosting-## Random Survival Forests+Verified for scikit-survival 0.28.0 on 2026-07-23.-### Overview+## Model families-Random Survival Forests extend the random forest algorithm to survival analysis with censored data. They build multiple decision trees on bootstrap samples and aggregate predictions.+- `SurvivalTree`: one log-rank survival tree.+- `RandomSurvivalForest`: bootstrap-aggregated survival trees with random feature+ subsets.+- `ExtraSurvivalTrees`: additional randomization of candidate split thresholds.+- `GradientBoostingSurvivalAnalysis`: regression-tree gradient boosting.+- `ComponentwiseGradientBoostingSurvivalAnalysis`: linear componentwise base+ learners and implicit sparse selection.-### How They Work+Model family does not determine quality in advance. Compare prespecified candidates+with identical outer resamples, censoring assumptions, time grids, and preprocessing.-1. **Bootstrap Sampling**: Each tree is built on a different bootstrap sample of the training data-2. **Feature Randomness**: At each node, only a random subset of features is considered for splitting-3. **Survival Function Estimation**: At terminal nodes, Kaplan-Meier and Nelson-Aalen estimators compute survival functions-4. **Ensemble Aggregation**: Final predictions average survival functions across all trees--### When to Use--- Complex non-linear relationships between features and survival-- No assumptions about functional form needed-- Want robust predictions with minimal tuning-- Need feature importance estimates-- Have sufficient sample size (typically n > 100)--### Key Parameters--- `n_estimators`: Number of trees (default: 100)- - More trees = more stable predictions but slower- - Typical range: 100-1000--- `max_depth`: Maximum depth of trees- - Controls tree complexity- - None = nodes expanded until pure or min_samples_split--- `min_samples_split`: Minimum samples to split a node (default: 6)- - Larger values = more regularization--- `min_samples_leaf`: Minimum samples at leaf nodes (default: 3)- - Prevents overfitting to small groups--- `max_features`: Number of features to consider at each split- - 'sqrt': sqrt(n_features) - good default- - 'log2': log2(n_features)- - None: all features--- `n_jobs`: Number of parallel jobs (-1 uses all processors)--### Example Usage+## Random survival forest```pythonfrom sksurv.ensemble import RandomSurvivalForest-from sksurv.datasets import load_breast_cancer--# Load data-X, y = load_breast_cancer()-# Fit Random Survival Forest-rsf = RandomSurvivalForest(n_estimators=1000,- min_samples_split=10,- min_samples_leaf=15,- max_features="sqrt",- n_jobs=-1,- random_state=42)-rsf.fit(X, y)--# Predict risk scores-risk_scores = rsf.predict(X)+model = RandomSurvivalForest(+ n_estimators=500,+ min_samples_split=10,+ min_samples_leaf=8,+ max_features="sqrt",+ n_jobs=1,+ random_state=20260723,+)+model.fit(X_train, y_train)+```-# Predict survival functions-surv_funcs = rsf.predict_survival_function(X)+Current defaults include `n_estimators=100`, `min_samples_split=6`,+`min_samples_leaf=3`, `max_features="sqrt"`, `bootstrap=True`, and+`low_memory=False`.-# Predict cumulative hazard functions-chf_funcs = rsf.predict_cumulative_hazard_function(X)-```+Each terminal node estimates:-### Feature Importance+- a survival function using Kaplan-Meier;+- a cumulative hazard function using Nelson-Aalen;+- a risk summary representing expected events.-**Important**: Built-in feature importance based on split impurity is not reliable for survival data. Use permutation-based feature importance instead.+Forest predictions average tree predictions:```python-from sklearn.inspection import permutation_importance-from sksurv.metrics import concordance_index_censored--# Define scoring function-def score_survival_model(model, X, y):- prediction = model.predict(X)- result = concordance_index_censored(y['event'], y['time'], prediction)- return result[0]--# Compute permutation importance-perm_importance = permutation_importance(- rsf, X, y,- n_repeats=10,- random_state=42,- scoring=score_survival_model+risk = model.predict(X_test) # (n_test,), higher is riskier+survival = model.predict_survival_function(X_test, return_array=True)+cumulative_hazard = model.predict_cumulative_hazard_function(+ X_test, return_array=True)--# Get feature importance-feature_importance = perm_importance.importances_mean+times = model.unique_times_```-## Gradient Boosting Survival Analysis--### Overview+Array predictions use the model's `unique_times_`. For an evaluation grid, returned+step functions are often more convenient:-Gradient boosting builds an ensemble by sequentially adding weak learners that correct errors of previous learners. The model is: **f(x) = Σ β_m g(x; θ_m)**--### Model Types--#### GradientBoostingSurvivalAnalysis+```python+import numpy as np-Uses regression trees as base learners. Can capture complex non-linear relationships.+functions = model.predict_survival_function(X_test, return_array=False)+survival_on_grid = np.vstack([fn(evaluation_times) for fn in functions])+```-**When to Use:**-- Need to model complex non-linear relationships-- Want high predictive performance-- Have sufficient data to avoid overfitting-- Can tune hyperparameters carefully+Do not treat the numeric magnitude of `predict()` as an event probability.-#### ComponentwiseGradientBoostingSurvivalAnalysis+### Missing values and memory-Uses component-wise least squares as base learners. Produces linear models with automatic feature selection.+Current survival trees and forest split logic supports missing values, with fixes+aligned to scikit-learn 1.8 in scikit-survival 0.27. This does not remove the need+to:-**When to Use:**-- Want interpretable linear model-- Need automatic feature selection (like Lasso)-- Have high-dimensional data-- Prefer sparse models+- verify which columns and missingness patterns are supported;+- encode categorical columns consistently;+- keep all learned preprocessing within training folds;+- assess whether missingness itself changes across deployment settings.-### Loss Functions+`low_memory=True` reduces stored prediction state but disables survival-function+and cumulative-hazard prediction. It is incompatible with Brier-score workflows+that require survival probabilities.-#### Cox's Partial Likelihood (default)+### OOB estimates-Maintains proportional hazards framework but replaces linear model with additive ensemble model.+With `oob_score=True` and bootstrap sampling, `oob_score_` provides an internal+out-of-bag concordance estimate. It is not a substitute for:-**Appropriate for:**-- Standard survival analysis settings-- When proportional hazards is reasonable-- Most use cases+- nested tuning when parameters were selected using OOB results;+- an independent test set;+- censoring-aware probability metrics;+- external validation.-#### Accelerated Failure Time (AFT)+## Extra survival trees-Assumes features accelerate or decelerate survival time by a constant factor. Loss function: **(1/n) Σ ω_i (log y_i - f(x_i))²**+```python+from sksurv.ensemble import ExtraSurvivalTrees-**Appropriate for:**-- AFT framework preferred over proportional hazards-- Want to model time directly-- Need to interpret effects on survival time+model = ExtraSurvivalTrees(+ n_estimators=500,+ min_samples_leaf=8,+ max_features="sqrt",+ n_jobs=1,+ random_state=20260723,+)+model.fit(X_train, y_train)+```-### Regularization Strategies+Extra trees randomize split thresholds in addition to feature selection. They are+not guaranteed to be faster, better regularized, or better calibrated for a given+dataset. Tune and evaluate them as a distinct candidate under the same protocol.-Three main techniques prevent overfitting:+## Permutation importance-1. **Learning Rate** (`learning_rate < 1`)- - Shrinks contribution of each base learner- - Smaller values need more iterations but better generalization- - Typical range: 0.01 - 0.1+Survival forest impurity importance is not implemented as a valid+`feature_importances_` measure. Use held-out permutation importance with an+explicit score:-2. **Dropout** (`dropout_rate > 0`)- - Randomly drops previous learners during training- - Forces learners to be more robust- - Typical range: 0.01 - 0.2+```python+from sklearn.inspection import permutation_importance-3. **Subsampling** (`subsample < 1`)- - Uses random subset of data for each iteration- - Adds randomness and reduces overfitting- - Typical range: 0.5 - 0.9+result = permutation_importance(+ fitted_pipeline,+ X_test,+ y_test,+ scoring=None, # estimator.score: Harrell concordance+ n_repeats=20,+ random_state=20260723,+ n_jobs=1,+)+```-**Recommendation**: Combine small learning rate with early stopping for best performance.+If Harrell C is not the target, wrap the estimator with the appropriate+scikit-survival scorer class before permutation or write a scorer that fits no+state on the test set. Importance depends on:-### Key Parameters+- the metric and horizon;+- correlated features;+- the held-out population;+- preprocessing and random seed.-- `loss`: Loss function ('coxph' or 'ipcwls')-- `learning_rate`: Shrinks contribution of each tree (default: 0.1)-- `n_estimators`: Number of boosting iterations (default: 100)-- `subsample`: Fraction of samples for each iteration (default: 1.0)-- `dropout_rate`: Dropout rate for learners (default: 0.0)-- `max_depth`: Maximum depth of trees (default: 3)-- `min_samples_split`: Minimum samples to split node (default: 2)-- `min_samples_leaf`: Minimum samples at leaf (default: 1)-- `max_features`: Features to consider at each split+It is predictive sensitivity, not causal importance.-### Example Usage+## Tree gradient boosting```pythonfrom sksurv.ensemble import GradientBoostingSurvivalAnalysis-from sklearn.model_selection import train_test_split-# Split data-X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)--# Fit gradient boosting model-gbs = GradientBoostingSurvivalAnalysis(- loss='coxph',+model = GradientBoostingSurvivalAnalysis(+ loss="coxph",learning_rate=0.05,- n_estimators=200,+ n_estimators=300,+ max_depth=2,subsample=0.8,- dropout_rate=0.1,- max_depth=3,- random_state=42+ random_state=20260723,)-gbs.fit(X_train, y_train)+model.fit(X_train, y_train)+```-# Predict risk scores-risk_scores = gbs.predict(X_test)+Current losses:-# Predict survival functions-surv_funcs = gbs.predict_survival_function(X_test)+- `"coxph"`: Cox partial-likelihood objective; `predict()` is a higher-is-riskier+ score, and baseline-based survival/cumulative-hazard methods are available.+- `"ipcwls"`: IPC-weighted least-squares AFT objective.+- `"squared"`: squared-error time-oriented objective.-# Predict cumulative hazard functions-chf_funcs = gbs.predict_cumulative_hazard_function(X_test)-```+Time-oriented losses do not make `predict()` a Cox risk score and do not provide+the same baseline survival-function interface. Confirm prediction direction before+using concordance or dynamic AUC; negate a predicted-time output only when that+conversion is explicitly intended and reported.-### Early Stopping+Current regularization controls:-Use validation set to prevent overfitting:+- `learning_rate`+- `n_estimators`+- `subsample`+- `dropout_rate`+- tree depth/leaf controls+- `ccp_alpha`+- `validation_fraction`, `n_iter_no_change`, and `tol`+- a custom `monitor` passed to `fit()` for controlled early stopping-```python-from sklearn.model_selection import train_test_split--# Create train/validation split-X_tr, X_val, y_tr, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)--# Fit with early stopping-gbs = GradientBoostingSurvivalAnalysis(- n_estimators=1000,- learning_rate=0.01,- max_depth=3,- validation_fraction=0.2,- n_iter_no_change=10,- random_state=42-)-gbs.fit(X_tr, y_tr)+The old `criterion` parameter was removed in 0.28. Do not copy it from older+examples.-# Number of iterations used-print(f"Used {gbs.n_estimators_} iterations")-```+Use only training data for internal early stopping. The final test set must not be+the monitor or validation fraction.-### Hyperparameter Tuning+## Componentwise boosting```python-from sklearn.model_selection import GridSearchCV+from sksurv.ensemble import ComponentwiseGradientBoostingSurvivalAnalysis-param_grid = {- 'learning_rate': [0.01, 0.05, 0.1],- 'n_estimators': [100, 200, 300],- 'max_depth': [3, 5, 7],- 'subsample': [0.8, 1.0]-}--cv = GridSearchCV(- GradientBoostingSurvivalAnalysis(),- param_grid,- scoring='concordance_index_ipcw',- cv=5,- n_jobs=-1+model = ComponentwiseGradientBoostingSurvivalAnalysis(+ loss="coxph",+ learning_rate=0.1,+ n_estimators=300,+ subsample=0.8,+ random_state=20260723,)-cv.fit(X, y)--best_model = cv.best_estimator_+model.fit(X_train_scaled, y_train)```-## ComponentwiseGradientBoostingSurvivalAnalysis+At each iteration, a componentwise learner updates one encoded feature. The final+model is linear and often sparse. Its `coef_` includes the fitted intercept entry+used by the implementation; align coefficients with transformed feature names+carefully.-### Overview+Iteration count is a selection parameter. Repeatedly checking test performance+while increasing `n_estimators` leaks the test set. Tune it inside inner CV, and+assess selected-feature stability across outer resamples.-Uses component-wise least squares, producing sparse linear models with automatic feature selection similar to Lasso.+## Leakage-safe nested tuning-### When to Use+```python+from sklearn.model_selection import GridSearchCV-- Want interpretable linear model-- Need automatic feature selection-- Have high-dimensional data with many irrelevant features-- Prefer coefficient-based interpretation+inner_search = GridSearchCV(+ pipeline,+ {+ "model__min_samples_leaf": [3, 8, 16],+ "model__max_features": ["sqrt", 0.5, 1.0],+ },+ cv=inner_splits,+ error_score="raise",+ n_jobs=1,+)+inner_search.fit(X_outer_train, y_outer_train)+risk_outer = inner_search.predict(X_outer_valid)+```-### Example Usage+Run this search inside each outer fold when reporting cross-validated tuned+performance. Every outer score must use:-```python-from sksurv.ensemble import ComponentwiseGradientBoostingSurvivalAnalysis+- outer-training preprocessing only;+- outer-training censoring distribution for IPCW metrics;+- an evaluation grid supported by that outer-training fold;+- outer-validation predictions never used in parameter selection.-# Fit componentwise boosting-cgbs = ComponentwiseGradientBoostingSurvivalAnalysis(- loss='coxph',- learning_rate=0.1,- n_estimators=100-)-cgbs.fit(X, y)+After protocol assessment, tune on all development data and evaluate once on the+reserved test set.-# Get selected features and coefficients-coef = cgbs.coef_-selected_features = [i for i, c in enumerate(coef) if c != 0]-```+## Probability prediction and calibration-## ExtraSurvivalTrees+Forests and Cox-loss boosting can produce survival probabilities. To use Brier+metrics:-Extremely randomized survival trees - similar to Random Survival Forest but with additional randomness in split selection.+```python+functions = fitted_pipeline.predict_survival_function(X_test)+survival_probability = np.vstack([fn(times) for fn in functions])+```-### When to Use+Then verify:-- Want even more regularization than Random Survival Forest-- Have limited data-- Need faster training+- shape is `(n_test, n_times)`;+- values are within `[0, 1]`;+- each row is non-increasing over time;+- `times` is strictly increasing and train-supported;+- censoring weights are learned from training outcomes.-### Key Difference+A lower Brier score does not prove good calibration in every subgroup or horizon.+Use horizon-specific calibration assessment on independent data. Do not claim+clinical utility from concordance, AUC, or Brier score alone.-Instead of finding the best split for selected features, it randomly selects split points, adding more diversity to the ensemble.+## Practical selection questions-```python-from sksurv.ensemble import ExtraSurvivalTrees+- Need coefficient-level PH interpretation? Start with a prespecified Cox model.+- Need nonlinear interactions and probability curves? Compare forest and Cox-loss+ boosting.+- Need sparse linear prediction? Compare Coxnet and componentwise boosting.+- Need time-oriented AFT prediction? Consider IPC-weighted losses and state the+ censoring assumptions.+- Need very large data? Benchmark memory and run time; kernel SVM and large+ survival forests can be expensive.-est = ExtraSurvivalTrees(n_estimators=100, random_state=42)-est.fit(X, y)-```+These are candidate-selection prompts, not performance guarantees.-## Model Comparison+## Sources-| Model | Complexity | Interpretability | Performance | Speed |-|-------|-----------|------------------|-------------|-------|-| Random Survival Forest | Medium | Low | High | Medium |-| GradientBoostingSurvivalAnalysis | High | Low | Highest | Slow |-| ComponentwiseGradientBoostingSurvivalAnalysis | Low | High | Medium | Fast |-| ExtraSurvivalTrees | Medium | Low | Medium-High | Fast |+Official sources checked 2026-07-23:-**General Recommendations:**-- **Best overall performance**: GradientBoostingSurvivalAnalysis with tuning-- **Best balance**: RandomSurvivalForest-- **Best interpretability**: ComponentwiseGradientBoostingSurvivalAnalysis-- **Fastest training**: ExtraSurvivalTrees+- [Random survival forest user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/random-survival-forest.html)+- [Gradient boosting user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/boosting.html)+- [RandomSurvivalForest API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.ensemble.RandomSurvivalForest.html)+- [ExtraSurvivalTrees API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.ensemble.ExtraSurvivalTrees.html)+- [GradientBoostingSurvivalAnalysis API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.ensemble.GradientBoostingSurvivalAnalysis.html)+- [ComponentwiseGradientBoostingSurvivalAnalysis API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.ensemble.ComponentwiseGradientBoostingSurvivalAnalysis.html)+- [0.28 release notes](https://scikit-survival.readthedocs.io/en/stable/release_notes/v0.28.html)+- [0.27 release notes](https://scikit-survival.readthedocs.io/en/stable/release_notes/v0.27.html)diff --git a/skills/scikit-survival/references/evaluation-metrics.md b/skills/scikit-survival/references/evaluation-metrics.mdindex 1e80f801..94559524 100644--- a/skills/scikit-survival/references/evaluation-metrics.md+++ b/skills/scikit-survival/references/evaluation-metrics.md@@ -1,378 +1,391 @@-# Evaluation Metrics for Survival Models+# Censoring-aware evaluation, calibration, and model selection-## Overview+Verified for scikit-survival 0.28.0 on 2026-07-23. API statements below use+official scikit-survival documentation; interpretation is anchored to the cited+primary methodological literature.-Evaluating survival models requires specialized metrics that account for censored data. scikit-survival provides three main categories of metrics:-1. Concordance Index (C-index)-2. Time-dependent ROC and AUC-3. Brier Score+## Keep the targets distinct-## Concordance Index (C-index)+- **Discrimination:** can the score order subjects by event risk?+ Harrell C, Uno C, and cumulative/dynamic AUC.+- **Probability prediction error:** how close are predicted survival probabilities+ to observed event-free status after censoring adjustment? Brier score and IBS.+- **Calibration:** do predicted probabilities agree with observed probabilities at+ a specified horizon and population? Requires horizon-specific assessment; Brier+ score is not a pure calibration measure.+- **Cause-specific cumulative incidence:** what is the absolute probability of a+ particular competing cause by time \(t\)? Standard survival metrics are not+ automatically cause-specific CIF metrics.+- **Clinical/decision utility:** do decisions based on predictions improve outcomes+ under defined consequences? None of the metrics above establishes this.-### What It Measures+Do not label one aggregate number "model accuracy" without its estimand, horizon,+censoring estimator, and input type.-The concordance index measures the rank correlation between predicted risk scores and observed event times. It represents the probability that, for a random pair of subjects, the model correctly orders their survival times.+## Prediction contracts-**Range**: 0 to 1-- 0.5 = random predictions-- 1.0 = perfect concordance-- Typical good performance: 0.7-0.8+### Higher-is-riskier scalar-### Two Implementations+Shape `(n_test,)`. Used by:-#### Harrell's C-index (concordance_index_censored)+- `concordance_index_censored`+- `concordance_index_ipcw`+- `cumulative_dynamic_auc` (same score at each requested time)-The traditional estimator, simpler but has limitations.--**When to Use:**-- Low censoring rates (< 40%)-- Quick evaluation during development-- Comparing models on same dataset--**Limitations:**-- Becomes increasingly biased with high censoring rates-- Overestimates performance starting at approximately 49% censoring+Typical source:```python-from sksurv.metrics import concordance_index_censored--# Compute Harrell's C-index-result = concordance_index_censored(y_test['event'], y_test['time'], risk_scores)-c_index = result[0]-print(f"Harrell's C-index: {c_index:.3f}")+risk = estimator.predict(X_test)```-#### Uno's C-index (concordance_index_ipcw)+Confirm direction. Cox and ranking-only SVM predictions are higher-is-riskier.+Predicted survival/log-time outputs are the opposite direction and must not be+silently treated as risk.-Inverse probability of censoring weighted (IPCW) estimator that corrects for censoring bias.+### Time-dependent risk-**When to Use:**-- Moderate to high censoring rates (> 40%)-- Need unbiased estimates-- Comparing models across different datasets-- Publishing results (more robust)--**Advantages:**-- Remains stable even with high censoring-- More reliable estimates-- Less biased+Shape `(n_test, n_times)`. Accepted by `cumulative_dynamic_auc`, where column+`j` is risk at `times[j]`. For a random survival forest:```python-from sksurv.metrics import concordance_index_ipcw--# Compute Uno's C-index-# Requires training data for IPCW calculation-c_index, concordant, discordant, tied_risk = concordance_index_ipcw(- y_train, y_test, risk_scores-)-print(f"Uno's C-index: {c_index:.3f}")+functions = estimator.predict_cumulative_hazard_function(X_test)+risk_by_time = np.vstack([fn(times) for fn in functions])```-### Choosing Between Harrell's and Uno's+Cumulative hazard is risk-oriented. Survival probability is not accepted by+`cumulative_dynamic_auc`; do not pass it without an explicitly justified+transformation.-**Use Uno's C-index when:**-- Censoring rate > 40%-- Need most accurate estimates-- Comparing models from different studies-- Publishing research+### Survival probability-**Use Harrell's C-index when:**-- Low censoring rates-- Quick model comparisons during development-- Computational efficiency is critical+Shape `(n_test, n_times)`, values in `[0, 1]`, non-increasing across columns.+Used by:-### Example Comparison+- `brier_score`+- `integrated_brier_score````python-from sksurv.metrics import concordance_index_censored, concordance_index_ipcw--# Harrell's C-index-harrell = concordance_index_censored(y_test['event'], y_test['time'], risk_scores)[0]--# Uno's C-index-uno = concordance_index_ipcw(y_train, y_test, risk_scores)[0]--print(f"Harrell's C-index: {harrell:.3f}")-print(f"Uno's C-index: {uno:.3f}")+functions = estimator.predict_survival_function(X_test)+survival_probability = np.vstack([fn(times) for fn in functions])```-## Time-Dependent ROC and AUC+Metric functions expect the numeric matrix, not a list of unevaluated+`StepFunction` objects and not a 1D risk score.-### What It Measures+## Harrell concordance-Time-dependent AUC evaluates model discrimination at specific time points. It distinguishes subjects who experience events by time *t* from those who don't.--**Question answered**: "How well does the model predict who will have an event by time t?"+```python+from sksurv.metrics import concordance_index_censored-### When to Use+harrell_c = concordance_index_censored(+ y_test["event"],+ y_test["time"],+ risk,+)[0]+```-- Predicting event occurrence within specific time windows-- Clinical decision-making at specific timepoints (e.g., 5-year survival)-- Want to evaluate performance across different time horizons-- Need both discrimination and timing information+Harrell C is the fraction of comparable pairs whose risk ordering is concordant,+with handling for tied risk and event times. It measures rank discrimination over+the observed follow-up mix.-### Key Function: cumulative_dynamic_auc+Important limitations:-```python-from sksurv.metrics import cumulative_dynamic_auc+- It does not assess probability calibration.+- It does not focus on a prespecified horizon.+- The set of comparable pairs changes under censoring.+- Uno et al. showed the conventional estimator's limiting value can depend on the+ censoring distribution.-# Define evaluation times-times = [365, 730, 1095, 1460, 1825] # 1, 2, 3, 4, 5 years+There is no universal censoring-percentage cutoff at which Harrell C suddenly+becomes invalid. Report censoring, compare sensitivity to IPCW concordance, and+justify the estimand.-# Compute time-dependent AUC-auc, mean_auc = cumulative_dynamic_auc(- y_train, y_test, risk_scores, times-)+## Uno IPCW concordance-# Plot AUC over time-import matplotlib.pyplot as plt-plt.plot(times, auc, marker='o')-plt.xlabel('Time (days)')-plt.ylabel('Time-dependent AUC')-plt.title('Model Discrimination Over Time')-plt.show()+```python+from sksurv.metrics import concordance_index_ipcw-print(f"Mean AUC: {mean_auc:.3f}")+uno_c = concordance_index_ipcw(+ y_train,+ y_test,+ risk,+ tau=tau,+)[0]```-### Interpretation+`survival_train` estimates the censoring distribution. Never pass pooled+train+test outcomes or `y_test` as the training distribution.-- **AUC at time t**: Probability model correctly ranks a subject who had event by time t above one who didn't-- **Varying AUC over time**: Indicates model performance changes with time horizon-- **Mean AUC**: Overall summary of discrimination across all time points+`tau` truncates the concordance target. Choose it before seeing model performance+and where the estimated training censoring survival is positive. Test follow-up+must be supported by training follow-up; otherwise scikit-survival raises a+`ValueError`.-### Example: Comparing Models+The implementation uses Kaplan-Meier censoring weights and assumes censoring is+random/independent of features. If censoring depends on covariates, this marginal+weight model may be inadequate. IPCW is not a generic correction for informative+censoring.++## Cumulative/dynamic AUC```python-# Compare two models-auc1, mean_auc1 = cumulative_dynamic_auc(y_train, y_test, risk_scores1, times)-auc2, mean_auc2 = cumulative_dynamic_auc(y_train, y_test, risk_scores2, times)--plt.plot(times, auc1, marker='o', label='Model 1')-plt.plot(times, auc2, marker='s', label='Model 2')-plt.xlabel('Time (days)')-plt.ylabel('Time-dependent AUC')-plt.legend()-plt.show()+from sksurv.metrics import cumulative_dynamic_auc++auc, mean_auc = cumulative_dynamic_auc(+ y_train,+ y_test,+ risk, # (n_test,) or (n_test, n_times)+ times,+)```-## Brier Score+At each \(t\), cumulative cases have an observed event by \(t\), while dynamic+controls remain event-free after \(t\). IPCW handles right censoring.-### What It Measures+Requirements:-Brier score extends mean squared error to survival data with censoring. It measures both discrimination (ranking) and calibration (accuracy of predicted probabilities).+- `times` is one-dimensional, unique, and strictly increasing;+- every value lies within test follow-up;+- training follow-up supports test outcomes and the grid;+- the training censoring survival is positive over the grid;+- risk has shape `(n_test,)` or `(n_test, n_times)`;+- higher values mean higher event risk.-**Formula**: **(1/n) Σ (S(t|x_i) - I(T_i > t))²**+The returned `mean_auc` is not the arithmetic mean. It integrates+\(\widehat{AUC}(t)\) over the time range, weighted by the estimated survival+function.-where S(t|x_i) is predicted survival probability at time t for subject i.+The cumulative/dynamic definition is one time-dependent ROC estimand. State it;+other incident/dynamic or competing-risk definitions answer different questions.-**Range**: 0 to 1-- 0 = perfect predictions-- Lower is better-- Typical good performance: < 0.2+## Brier score-### When to Use+```python+from sksurv.metrics import brier_score, integrated_brier_score-- Need calibration assessment (not just ranking)-- Want to evaluate predicted probabilities, not just risk scores-- Comparing models that output survival functions-- Clinical applications requiring probability estimates+returned_times, brier = brier_score(+ y_train,+ y_test,+ survival_probability,+ times,+)+ibs = integrated_brier_score(+ y_train,+ y_test,+ survival_probability,+ times,+)+```-### Key Functions+The time-dependent Brier score is an IPC-weighted squared error between predicted+survival probability and event-free status at \(t\). Lower is better.-#### brier_score: Single Time Point+Requirements:-```python-from sksurv.metrics import brier_score+- predictions are survival probabilities, not risk scores;+- prediction shape is `(n_test, n_times)`;+- time and training-support rules match the IPCW setting;+- the marginal Kaplan-Meier censoring estimator's independence assumption is+ plausible.-# Compute Brier score at specific time-time_point = 1825 # 5 years-surv_probs = model.predict_survival_function(X_test)-# Extract survival probability at time_point for each subject-surv_at_t = [fn(time_point) for fn in surv_probs]+IBS integrates Brier score over `[times[0], times[-1]]` with the implementation's+time weighting. It depends on the chosen interval; IBS values from different+time ranges are not directly comparable.-bs = brier_score(y_train, y_test, surv_at_t, time_point)[1]-print(f"Brier score at {time_point} days: {bs:.3f}")-```+Compare against useful reference predictions such as a training-derived+Kaplan-Meier survival curve. Do not estimate the reference curve on test outcomes.-#### integrated_brier_score: Summary Across Time+## Calibration-```python-from sksurv.metrics import integrated_brier_score+Brier score responds to both discrimination and calibration. A good Brier score+does not prove that predicted 20% risk corresponds to 20% observed risk in every+horizon or subgroup.-# Compute integrated Brier score-times = [365, 730, 1095, 1460, 1825]-surv_probs = model.predict_survival_function(X_test)+For a prespecified horizon:-ibs = integrated_brier_score(y_train, y_test, surv_probs, times)-print(f"Integrated Brier Score: {ibs:.3f}")-```+1. fit and tune the model without the calibration-evaluation rows;+2. obtain event probability `1 - S(t | x)` on independent validation rows;+3. compare predictions with a censoring-aware observed probability estimate;+4. inspect calibration-in-the-large, slope/shape, uncertainty, and sample support;+5. repeat only for prespecified horizons/subgroups or account for multiplicity.-### Interpretation+scikit-survival 0.28 does not expose a dedicated calibration-curve API. Do not use+`sklearn.calibration.calibration_curve` naively on censored binary labels. If an+external method is used, document its censoring assumptions and train/validation+separation.-- **Brier score at time t**: Expected squared difference between predicted and actual survival at time t-- **Integrated Brier Score**: Weighted average of Brier scores across time-- **Lower values = better predictions**+Any recalibration layer is another learned model. Fit it on a calibration split or+inner resampling, then assess on independent data.-### Comparison with Null Model+## Safe time-grid construction-Always compare against a baseline (e.g., Kaplan-Meier):+A pragmatic fold-specific grid:```python-from sksurv.nonparametric import kaplan_meier_estimator+import numpy as np+from sksurv.nonparametric import CensoringDistributionEstimator-# Compute Kaplan-Meier baseline-time_km, surv_km = kaplan_meier_estimator(y_train['event'], y_train['time'])+test_time = y_test["time"]+train_time = y_train["time"]-# Predict with KM for each test subject-surv_km_test = [surv_km[time_km <= time_point][-1] if any(time_km <= time_point) else 1.0- for _ in range(len(X_test))]+lower = np.quantile(test_time, 0.10)+upper = min(+ np.quantile(test_time, 0.80),+ np.nextafter(train_time.max(), -np.inf),+)+times = np.linspace(lower, upper, 50)-bs_km = brier_score(y_train, y_test, surv_km_test, time_point)[1]-bs_model = brier_score(y_train, y_test, surv_at_t, time_point)[1]+if not (test_time.min() < times[0] < times[-1] < test_time.max()):+ raise ValueError("grid is outside test follow-up")+if not test_time.max() < train_time.max():+ raise ValueError("test follow-up exceeds training support")-print(f"Kaplan-Meier Brier Score: {bs_km:.3f}")-print(f"Model Brier Score: {bs_model:.3f}")-print(f"Improvement: {(bs_km - bs_model) / bs_km * 100:.1f}%")+censoring = CensoringDistributionEstimator().fit(y_train)+if np.any(censoring.predict_proba(times) <= 0):+ raise ValueError("training censoring survival reaches zero on grid")```-## Using Metrics with Cross-Validation--### Concordance Index Scorer--```python-from sklearn.model_selection import cross_val_score-from sksurv.metrics import as_concordance_index_ipcw_scorer+Quantiles are an operational example, not a scientific default. Prefer+prespecified meaningful horizons, then verify fold support. Do not select a grid+because it maximizes a metric.-# Create scorer-scorer = as_concordance_index_ipcw_scorer()+The bundled evaluator rejects unsupported grids and shape/type confusion:-# Perform cross-validation-scores = cross_val_score(model, X, y, cv=5, scoring=scorer)-print(f"Mean C-index: {scores.mean():.3f} (±{scores.std():.3f})")+```bash+python skills/scikit-survival/scripts/evaluate_survival_metrics.py \+ --input predictions.npz \+ --output metrics-summary.json```-### Integrated Brier Score Scorer+Its NPZ contract is:-```python-from sksurv.metrics import as_integrated_brier_score_scorer+- `train_event`, `train_time`+- `test_event`, `test_time`+- `times`+- `risk`+- optional `survival`-# Define time points for evaluation-times = np.percentile(y['time'][y['event']], [25, 50, 75])+Archives are loaded with `allow_pickle=False`.-# Create scorer-scorer = as_integrated_brier_score_scorer(times)+## Scorer wrappers and model selection-# Perform cross-validation-scores = cross_val_score(model, X, y, cv=5, scoring=scorer)-print(f"Mean IBS: {scores.mean():.3f} (±{scores.std():.3f})")-```+Survival estimators' default `.score()` is Harrell concordance. For other targets,+scikit-survival provides estimator wrappers:++- `as_concordance_index_ipcw_scorer(estimator, tau=None, tied_tol=...)`+- `as_cumulative_dynamic_auc_scorer(estimator, times, tied_tol=...)`+- `as_integrated_brier_score_scorer(estimator, times)`-## Model Selection with GridSearchCV+Correct pattern:```pythonfrom sklearn.model_selection import GridSearchCV-from sksurv.ensemble import RandomSurvivalForest-from sksurv.metrics import as_concordance_index_ipcw_scorer--# Define parameter grid-param_grid = {- 'n_estimators': [100, 200, 300],- 'min_samples_split': [10, 20, 30],- 'max_depth': [None, 10, 20]-}--# Create scorer-scorer = as_concordance_index_ipcw_scorer()--# Perform grid search-cv = GridSearchCV(- RandomSurvivalForest(random_state=42),- param_grid,- scoring=scorer,- cv=5,- n_jobs=-1-)-cv.fit(X, y)--print(f"Best parameters: {cv.best_params_}")-print(f"Best C-index: {cv.best_score_:.3f}")-```--## Comprehensive Model Evaluation--### Recommended Evaluation Pipeline+from sksurv.metrics import as_integrated_brier_score_scorer-```python-from sksurv.metrics import (- concordance_index_censored,- concordance_index_ipcw,- cumulative_dynamic_auc,- integrated_brier_score+wrapped = as_integrated_brier_score_scorer(+ estimator,+ times=inner_times,)--def evaluate_survival_model(model, X_train, X_test, y_train, y_test):- """Comprehensive evaluation of survival model"""-- # Get predictions- risk_scores = model.predict(X_test)- surv_funcs = model.predict_survival_function(X_test)-- # 1. Concordance Index (both versions)- c_harrell = concordance_index_censored(y_test['event'], y_test['time'], risk_scores)[0]- c_uno = concordance_index_ipcw(y_train, y_test, risk_scores)[0]-- # 2. Time-dependent AUC- times = np.percentile(y_test['time'][y_test['event']], [25, 50, 75])- auc, mean_auc = cumulative_dynamic_auc(y_train, y_test, risk_scores, times)-- # 3. Integrated Brier Score- ibs = integrated_brier_score(y_train, y_test, surv_funcs, times)-- # Print results- print("=" * 50)- print("Model Evaluation Results")- print("=" * 50)- print(f"Harrell's C-index: {c_harrell:.3f}")- print(f"Uno's C-index: {c_uno:.3f}")- print(f"Mean AUC: {mean_auc:.3f}")- print(f"Integrated Brier: {ibs:.3f}")- print("=" * 50)-- return {- 'c_harrell': c_harrell,- 'c_uno': c_uno,- 'mean_auc': mean_auc,- 'ibs': ibs,- 'time_auc': dict(zip(times, auc))- }--# Use the evaluation function-results = evaluate_survival_model(model, X_train, X_test, y_train, y_test)+search = GridSearchCV(+ wrapped,+ {"estimator__model__max_depth": [1, 2, 4]},+ cv=inner_splits,+)+search.fit(X_outer_train, y_outer_train)```-## Choosing the Right Metric+The wrapper:++- is the estimator supplied to `GridSearchCV`;+- stores its fitted estimator in `estimator_`;+- exposes nested parameters under `estimator__...`;+- fits metric state, including training survival information, from each fit fold;+- negates IBS so larger wrapper score remains better.-### Decision Guide+Do not write `scoring=as_integrated_brier_score_scorer(times)`; the constructor+requires an estimator.-**Use C-index (Uno's) when:**-- Primary goal is ranking/discrimination-- Don't need calibrated probabilities-- Standard survival analysis setting-- Most common choice+## Nested CV protocol-**Use Time-dependent AUC when:**-- Need discrimination at specific time points-- Clinical decisions at specific horizons-- Want to understand how performance varies over time+For tuned performance:-**Use Brier Score when:**-- Need calibrated probability estimates-- Both discrimination AND calibration important-- Clinical decision-making requiring probabilities-- Want comprehensive assessment+```text+for each outer split:+ outer_train, outer_valid+ for each inner split within outer_train:+ fit preprocessing + model + censoring-dependent scorer state+ select hyperparameters+ refit selected pipeline on all outer_train+ evaluate once on outer_valid using outer_train censoring distribution+aggregate outer scores and uncertainty+```-**Best Practice**: Report multiple metrics for comprehensive evaluation. At minimum, report:-- Uno's C-index (discrimination)-- Integrated Brier Score (discrimination + calibration)-- Time-dependent AUC at clinically relevant time points+Every fold needs its own valid `tau`/time grid. A single global grid derived from+all outcome times leaks outer-validation support information and may fail when an+outer training fold has shorter follow-up.++After protocol evaluation, tune on all development data and evaluate once on an+untouched final test set. Do not report the best inner-CV score as generalization+performance.++## Competing risks++If events have causes:++- all-event risk combines causes and is not cause-specific discrimination;+- treating other causes as censored defines a cause-specific hazard analysis;+- a cause-specific CIF is an absolute probability accounting for all causes;+- standard Brier/AUC functions here are documented for right-censored+ single-event outcomes, not a complete competing-risk prediction evaluation.++State event coding and use methods designed for the cause-specific estimand. See+`competing-risks.md`.++## Reporting checklist++- split and nesting protocol;+- event/censoring counts per evaluation set;+- metric definition and score direction;+- `tau` and exact time grid/range;+- source of censoring weights;+- prediction shape and whether values are risks or survival probabilities;+- uncertainty across independent outer folds or bootstrap resamples;+- calibration assessment separate from discrimination;+- handling of competing causes;+- no claim of clinical utility from metrics alone.++## Primary methodological literature++- Harrell FE Jr, Califf RM, Pryor DB, Lee KL, Rosati RA. "Evaluating the+ yield of medical tests." *JAMA* (1982).+ [doi:10.1001/jama.1982.03320430047030](https://doi.org/10.1001/jama.1982.03320430047030)+- Uno H, Cai T, Pencina MJ, D'Agostino RB, Wei LJ. "On the C-statistics for+ evaluating overall adequacy of risk prediction procedures with censored+ survival data." *Statistics in Medicine* (2011).+ [doi:10.1002/sim.4154](https://doi.org/10.1002/sim.4154)+- Heagerty PJ, Lumley T, Pepe MS. "Time-dependent ROC curves for censored+ survival data and a diagnostic marker." *Biometrics* (2000).+ [doi:10.1111/j.0006-341X.2000.00337.x](https://doi.org/10.1111/j.0006-341X.2000.00337.x)+- Heagerty PJ, Zheng Y. "Survival model predictive accuracy and ROC curves."+ *Biometrics* (2005).+ [doi:10.1111/j.0006-341X.2005.030814.x](https://doi.org/10.1111/j.0006-341X.2005.030814.x)+- Graf E, Schmoor C, Sauerbrei W, Schumacher M. "Assessment and comparison+ of prognostic classification schemes for survival data."+ *Statistics in Medicine* (1999).+ [doi:10.1002/(SICI)1097-0258(19990915/30)18:17/18%3C2529::AID-SIM274%3E3.0.CO;2-5](https://doi.org/10.1002/%28SICI%291097-0258%2819990915/30%2918%3A17/18%3C2529%3A%3AAID-SIM274%3E3.0.CO%3B2-5)+- Gerds TA, Schumacher M. "Consistent estimation of the expected Brier score+ in general survival models with right-censored event times."+ *Biometrical Journal* (2006).+ [doi:10.1002/bimj.200610301](https://doi.org/10.1002/bimj.200610301)++## Official API sources++Checked 2026-07-23:++- [Evaluation user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/evaluating-survival-models.html)+- [Metrics API index](https://scikit-survival.readthedocs.io/en/stable/api/metrics.html)+- [IPCW concordance API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.metrics.concordance_index_ipcw.html)+- [Cumulative/dynamic AUC API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.metrics.cumulative_dynamic_auc.html)+- [Brier score API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.metrics.brier_score.html)+- [Integrated Brier score API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.metrics.integrated_brier_score.html)+- [IPCW scorer wrapper API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.metrics.as_concordance_index_ipcw_scorer.html)diff --git a/skills/scikit-survival/references/svm-models.md b/skills/scikit-survival/references/svm-models.mdindex b4dbd8fb..af16712e 100644--- a/skills/scikit-survival/references/svm-models.md+++ b/skills/scikit-survival/references/svm-models.md@@ -1,411 +1,277 @@-# Survival Support Vector Machines+# Survival support vector machines-## Overview+Verified for scikit-survival 0.28.0 on 2026-07-23.-Survival Support Vector Machines (SVMs) adapt the traditional SVM framework to survival analysis with censored data. They optimize a ranking objective that encourages correct ordering of survival times.+## What survival SVMs predict-### Core Idea+Survival SVMs optimize ranking, regression, or a mixture. They generally return a+scalar score, not a baseline survival function or cumulative hazard function.+Therefore:-SVMs for survival analysis learn a function f(x) that produces risk scores, where the optimization ensures that subjects with shorter survival times receive higher risk scores than those with longer times.+- use concordance or cumulative/dynamic AUC only after confirming score direction;+- do not pass SVM output to Brier metrics;+- do not convert a margin to event probability without a separately validated+ calibration model and protocol.-## When to Use Survival SVMs--**Appropriate for:**-- Medium-sized datasets (typically 100-10,000 samples)-- Need for non-linear decision boundaries (kernel SVMs)-- Want margin-based learning with regularization-- Have well-defined feature space--**Not ideal for:**-- Very large datasets (>100,000 samples) - ensemble methods may be faster-- Need interpretable coefficients - use Cox models instead-- Require survival function estimates - use Random Survival Forest-- Very high dimensional data - use regularized Cox or gradient boosting--## Model Types--### FastSurvivalSVM--Linear survival SVM optimized for speed using coordinate descent.--**When to Use:**-- Linear relationships expected-- Large datasets where speed matters-- Want fast training and prediction--**Key Parameters:**-- `alpha`: Regularization parameter (default: 1.0)- - Higher = more regularization-- `rank_ratio`: Trade-off between ranking and regression (default: 1.0)-- `max_iter`: Maximum iterations (default: 20)-- `tol`: Tolerance for stopping criterion (default: 1e-5)+## Fast linear survival SVM```python+from sklearn.pipeline import make_pipeline+from sklearn.preprocessing import StandardScalerfrom sksurv.svm import FastSurvivalSVM-# Fit linear survival SVM-estimator = FastSurvivalSVM(alpha=1.0, max_iter=100, tol=1e-5, random_state=42)-estimator.fit(X, y)--# Predict risk scores-risk_scores = estimator.predict(X_test)+model = make_pipeline(+ StandardScaler(),+ FastSurvivalSVM(+ alpha=1.0,+ rank_ratio=1.0,+ max_iter=1000,+ tol=1e-5,+ random_state=20260723,+ ),+)+model.fit(X_train, y_train)+risk = model.predict(X_test)```-### FastKernelSurvivalSVM+Current signature:++```text+FastSurvivalSVM(+ alpha=1, *,+ rank_ratio=1.0,+ fit_intercept=False,+ max_iter=20,+ verbose=False,+ tol=None,+ optimizer=None,+ random_state=None,+ timeit=False,+)+```-Kernel survival SVM for non-linear relationships.+Key semantics:-**When to Use:**-- Non-linear relationships between features and survival-- Medium-sized datasets-- Can afford longer training time for better performance+- `rank_ratio=1.0`: ranking-only objective; higher predictions indicate shorter+ survival/higher event risk.+- `0 < rank_ratio < 1`: mixed ranking and regression.+- `rank_ratio=0.0`: regression-only objective.+- When `rank_ratio < 1`, prediction is time-oriented (internally based on log+ observed time): lower prediction means shorter survival. For a metric requiring+ higher event risk, use `-prediction` and document the conversion.+- `alpha` controls regularization; tune it within inner CV.-**Kernel Options:**-- `'linear'`: Linear kernel, equivalent to FastSurvivalSVM-- `'poly'`: Polynomial kernel-- `'rbf'`: Radial basis function (Gaussian) kernel - most common-- `'sigmoid'`: Sigmoid kernel-- Custom kernel function+Do not use an arbitrary sign simply because a C-index improves. The sign follows+the model objective and target interpretation.-**Key Parameters:**-- `alpha`: Regularization parameter (default: 1.0)-- `kernel`: Kernel function (default: 'rbf')-- `gamma`: Kernel coefficient for rbf, poly, sigmoid-- `degree`: Degree for polynomial kernel-- `coef0`: Independent term for poly and sigmoid-- `rank_ratio`: Trade-off parameter (default: 1.0)-- `max_iter`: Maximum iterations (default: 20)+## Fast kernel survival SVM```pythonfrom sksurv.svm import FastKernelSurvivalSVM-# Fit RBF kernel survival SVM-estimator = FastKernelSurvivalSVM(+model = FastKernelSurvivalSVM(alpha=1.0,- kernel='rbf',- gamma='scale',- max_iter=50,- random_state=42+ rank_ratio=1.0,+ kernel="rbf",+ gamma=0.05,+ max_iter=100,+ tol=1e-5,+ random_state=20260723,)-estimator.fit(X, y)--# Predict risk scores-risk_scores = estimator.predict(X_test)+model.fit(X_train_scaled, y_train)+risk = model.predict(X_test_scaled)```-### HingeLossSurvivalSVM--Survival SVM using hinge loss, more similar to classification SVM.--**When to Use:**-- Want hinge loss instead of squared hinge-- Sparse solutions desired-- Similar behavior to classification SVMs--**Key Parameters:**-- `alpha`: Regularization parameter-- `fit_intercept`: Whether to fit intercept term (default: False)--```python-from sksurv.svm import HingeLossSurvivalSVM--# Fit hinge loss SVM-estimator = HingeLossSurvivalSVM(alpha=1.0, fit_intercept=False, random_state=42)-estimator.fit(X, y)--# Predict risk scores-risk_scores = estimator.predict(X_test)+Current signature:++```text+FastKernelSurvivalSVM(+ alpha=1, *,+ rank_ratio=1.0,+ fit_intercept=False,+ kernel="rbf",+ gamma=None,+ degree=3,+ coef0=1,+ kernel_params=None,+ max_iter=20,+ verbose=False,+ tol=None,+ optimizer=None,+ random_state=None,+ timeit=False,+)```-### NaiveSurvivalSVM+Kernel choices follow scikit-learn pairwise-kernel behavior, including `"linear"`,+`"poly"`, `"rbf"`, `"sigmoid"`, callable kernels, and `"precomputed"` where+supported. Do not copy older examples that use `gamma="scale"` without checking+the current API; the current scikit-survival parameter default is `None`.-Original formulation of survival SVM using quadratic programming.+Kernel fitting and prediction depend on training rows and can require+quadratic-size kernel matrices. Enforce row/memory bounds before fitting.-**When to Use:**-- Small datasets-- Research/benchmarking purposes-- Other methods don't converge+## Hinge, Minlip, and naive formulations-**Limitations:**-- Slower than Fast variants-- Less scalable+Current additional estimators:-```python-from sksurv.svm import NaiveSurvivalSVM+- `HingeLossSurvivalSVM(alpha=1.0, solver="ecos", kernel="linear", pairs="all", ...)`+- `MinlipSurvivalAnalysis(alpha=1.0, solver="ecos", kernel="linear",+ pairs="nearest", ...)`+- `NaiveSurvivalSVM(penalty="l2", loss="squared_hinge", dual=False,+ alpha=1.0, ...)`-# Fit naive SVM (slower)-estimator = NaiveSurvivalSVM(alpha=1.0, random_state=42)-estimator.fit(X, y)+Important current differences:-# Predict-risk_scores = estimator.predict(X_test)-```+- `HingeLossSurvivalSVM` and `MinlipSurvivalAnalysis` do not have a+ `random_state` constructor parameter.+- Their convex optimization defaults to the ECOS solver.+- Pair construction and kernel matrices can become expensive.+- `NaiveSurvivalSVM` uses a linear-SVM-style formulation and is mainly useful for+ small comparisons; it is not the fast implementation.-### MinlipSurvivalAnalysis+Use the exact current signature rather than transferring parameters across SVM+classes.-Survival analysis using minimizing Lipschitz constant approach.+## Scaling and explicit preprocessing-**When to Use:**-- Want different optimization objective-- Research applications-- Alternative to standard survival SVMs+Scale continuous features and fit scaling only on training folds:```python-from sksurv.svm import MinlipSurvivalAnalysis--# Fit Minlip model-estimator = MinlipSurvivalAnalysis(alpha=1.0, random_state=42)-estimator.fit(X, y)--# Predict-risk_scores = estimator.predict(X_test)-```--## Hyperparameter Tuning--### Tuning Alpha (Regularization)--```python-from sklearn.model_selection import GridSearchCV-from sksurv.metrics import as_concordance_index_ipcw_scorer--# Define parameter grid-param_grid = {- 'alpha': [0.1, 0.5, 1.0, 5.0, 10.0, 50.0]-}--# Grid search-cv = GridSearchCV(- FastSurvivalSVM(),- param_grid,- scoring=as_concordance_index_ipcw_scorer(),- cv=5,- n_jobs=-1+from sklearn.compose import ColumnTransformer+from sklearn.impute import SimpleImputer+from sklearn.pipeline import make_pipeline+from sklearn.preprocessing import OneHotEncoder, StandardScaler++preprocess = ColumnTransformer(+ [+ (+ "num",+ make_pipeline(SimpleImputer(strategy="median"), StandardScaler()),+ numeric_columns,+ ),+ (+ "cat",+ make_pipeline(+ SimpleImputer(strategy="most_frequent"),+ OneHotEncoder(+ drop="first",+ handle_unknown="ignore",+ sparse_output=False,+ ),+ ),+ categorical_columns,+ ),+ ],+ sparse_threshold=0.0,)-cv.fit(X, y)--print(f"Best alpha: {cv.best_params_['alpha']}")-print(f"Best C-index: {cv.best_score_:.3f}")-```--### Tuning Kernel Parameters--```python-from sklearn.model_selection import GridSearchCV--# Define parameter grid for kernel SVM-param_grid = {- 'alpha': [0.1, 1.0, 10.0],- 'gamma': ['scale', 'auto', 0.001, 0.01, 0.1, 1.0]-}--# Grid search-cv = GridSearchCV(- FastKernelSurvivalSVM(kernel='rbf'),- param_grid,- scoring=as_concordance_index_ipcw_scorer(),- cv=5,- n_jobs=-1+model = make_pipeline(+ preprocess,+ FastSurvivalSVM(rank_ratio=1.0, random_state=20260723),)-cv.fit(X, y)--print(f"Best parameters: {cv.best_params_}")-print(f"Best C-index: {cv.best_score_:.3f}")```-## Clinical Kernel Transform--### ClinicalKernelTransform+Do not call `StandardScaler.fit_transform(X)` before cross-validation. Keeping it+inside the pipeline makes every inner and outer fold independent.-Special kernel that combines clinical features with molecular data for improved predictions in medical applications.+## Kernel preprocessing-**Use Case:**-- Have both clinical variables (age, stage, etc.) and high-dimensional molecular data (gene expression, genomics)-- Clinical features should have different weighting-- Want to integrate heterogeneous data types+For `"precomputed"`, training input must be a square+`(n_train, n_train)` kernel matrix; test input must be+`(n_test, n_train)` with columns in the identical training order.-**Key Parameters:**-- `fit_once`: Whether to fit kernel once or refit during cross-validation (default: False)-- Clinical features should be passed separately from molecular features+`sksurv.kernels.ClinicalKernelTransform` and `clinical_kernel()` support mixed+continuous, ordinal, and nominal DataFrame columns, including pandas/Polars in+0.28. They do not accept an ad hoc list of `(clinical, molecular)` tuples as a+special API. Fit the transform on a clearly typed training DataFrame or explicitly+precompute the kernel:```python-from sksurv.kernels import ClinicalKernelTransform+from sksurv.kernels import clinical_kernelfrom sksurv.svm import FastKernelSurvivalSVM-from sklearn.pipeline import make_pipeline-# Separate clinical and molecular features-clinical_features = ['age', 'stage', 'grade']-X_clinical = X[clinical_features]-X_molecular = X.drop(clinical_features, axis=1)+kernel_train = clinical_kernel(X_train_typed)+kernel_test = clinical_kernel(X_test_typed, X_train_typed)-# Create pipeline with clinical kernel-estimator = make_pipeline(- ClinicalKernelTransform(),- FastKernelSurvivalSVM()+model = FastKernelSurvivalSVM(+ kernel="precomputed",+ rank_ratio=1.0,+ random_state=20260723,)--# Fit model-# ClinicalKernelTransform expects tuple (clinical, molecular)-X_combined = list(zip(X_clinical.values, X_molecular.values))-estimator.fit(X_combined, y)+model.fit(kernel_train, y_train)+risk = model.predict(kernel_test)```-## Practical Examples--### Example 1: Linear SVM with Cross-Validation--```python-from sksurv.svm import FastSurvivalSVM-from sklearn.model_selection import cross_val_score-from sksurv.metrics import as_concordance_index_ipcw_scorer-from sklearn.preprocessing import StandardScaler--# Standardize features (important for SVMs!)-scaler = StandardScaler()-X_scaled = scaler.fit_transform(X)--# Create model-svm = FastSurvivalSVM(alpha=1.0, max_iter=100, random_state=42)+Any data-dependent kernel typing, scaling, or parameter choice belongs inside the+training/CV protocol.-# Cross-validation-scores = cross_val_score(- svm, X_scaled, y,- cv=5,- scoring=as_concordance_index_ipcw_scorer(),- n_jobs=-1-)--print(f"Mean C-index: {scores.mean():.3f} (±{scores.std():.3f})")-```+## Nested tuning-### Example 2: Kernel SVM with Different Kernels+Tune at least `alpha`; for kernel models also tune kernel and its parameters.+Keep the grid bounded:```python-from sksurv.svm import FastKernelSurvivalSVM-from sklearn.model_selection import train_test_split-from sksurv.metrics import concordance_index_ipcw--# Split data-X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)--# Standardize-scaler = StandardScaler()-X_train_scaled = scaler.fit_transform(X_train)-X_test_scaled = scaler.transform(X_test)--# Compare different kernels-kernels = ['linear', 'poly', 'rbf', 'sigmoid']-results = {}--for kernel in kernels:- # Fit model- svm = FastKernelSurvivalSVM(kernel=kernel, alpha=1.0, random_state=42)- svm.fit(X_train_scaled, y_train)-- # Predict- risk_scores = svm.predict(X_test_scaled)-- # Evaluate- c_index = concordance_index_ipcw(y_train, y_test, risk_scores)[0]- results[kernel] = c_index-- print(f"{kernel:10s}: C-index = {c_index:.3f}")--# Best kernel-best_kernel = max(results, key=results.get)-print(f"\nBest kernel: {best_kernel} (C-index = {results[best_kernel]:.3f})")-```--### Example 3: Full Pipeline with Hyperparameter Tuning--```python-from sksurv.svm import FastKernelSurvivalSVM-from sklearn.model_selection import GridSearchCV, train_test_split-from sklearn.pipeline import Pipeline-from sklearn.preprocessing import StandardScaler-from sksurv.metrics import as_concordance_index_ipcw_scorer--# Split data-X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)--# Create pipeline-pipeline = Pipeline([- ('scaler', StandardScaler()),- ('svm', FastKernelSurvivalSVM(kernel='rbf'))-])--# Define parameter grid-param_grid = {- 'svm__alpha': [0.1, 1.0, 10.0],- 'svm__gamma': ['scale', 0.01, 0.1, 1.0]-}+from sklearn.model_selection import GridSearchCV-# Grid search-cv = GridSearchCV(+search = GridSearchCV(pipeline,- param_grid,- scoring=as_concordance_index_ipcw_scorer(),- cv=5,- n_jobs=-1,- verbose=1+ {+ "fastkernelsurvivalsvm__alpha": [0.1, 1.0, 10.0],+ "fastkernelsurvivalsvm__gamma": [0.01, 0.05, 0.2],+ },+ cv=inner_splits,+ error_score="raise",+ n_jobs=1,)-cv.fit(X_train, y_train)--# Best model-best_model = cv.best_estimator_-print(f"Best parameters: {cv.best_params_}")-print(f"Best CV C-index: {cv.best_score_:.3f}")--# Evaluate on test set-risk_scores = best_model.predict(X_test)-c_index = concordance_index_ipcw(y_train, y_test, risk_scores)[0]-print(f"Test C-index: {c_index:.3f}")+search.fit(X_outer_train, y_outer_train)```-## Important Considerations+The actual parameter prefix depends on pipeline step names. Run this search within+each outer fold for a nested-CV performance estimate. Do not:-### Feature Scaling+- fit a scaler or kernel transform before the outer split;+- select the sign, kernel, or horizon on outer-validation results;+- reuse the final test set to choose `alpha`/`gamma`;+- compare SVM Brier scores, because SVMs do not output survival probabilities.-**CRITICAL**: Always standardize features before using SVMs!+For IPCW metrics, fit the censoring distribution on the corresponding outer+training outcomes and keep the time grid within that fold's support.-```python-from sklearn.preprocessing import StandardScaler+## Choosing an SVM candidate-scaler = StandardScaler()-X_train_scaled = scaler.fit_transform(X_train)-X_test_scaled = scaler.transform(X_test)-```+- Linear ranking objective: a scalable margin-based discrimination baseline.+- Kernel ranking objective: nonlinear relationships when row count permits.+- Mixed/regression objective: time-oriented score, with different sign semantics.+- Need absolute survival probability: choose a model with+ `predict_survival_function()` or add a separately validated calibration stage.+- Need coefficient/hazard-ratio interpretation: use an appropriate Cox model,+ not an SVM margin.-### Computational Complexity+These are capability distinctions, not guarantees of performance.-- **FastSurvivalSVM**: O(n × p) per iteration - fast-- **FastKernelSurvivalSVM**: O(n² × p) - slower, scales quadratically-- **NaiveSurvivalSVM**: O(n³) - very slow for large datasets+## Interpretation-For large datasets (>10,000 samples), prefer:-- FastSurvivalSVM (linear)-- Gradient Boosting-- Random Survival Forest+SVM margins are arbitrary-scale predictions. A high C-index or dynamic AUC says+that orderings discriminate under the chosen censoring estimator and horizon; it+does not establish:-### When SVMs May Not Be Best Choice+- probability calibration;+- causal or treatment effects;+- transportability;+- subgroup fairness;+- clinical or decision utility.-- **Very large datasets**: Ensemble methods are faster-- **Need survival functions**: Use Random Survival Forest or Cox models-- **Need interpretability**: Use Cox models-- **Very high dimensional**: Use penalized Cox (Coxnet) or gradient boosting with feature selection+Report optimization convergence, score direction, kernel, preprocessing, tuning+resamples, and censoring assumptions.-## Model Selection Guide+## Sources-| Model | Speed | Non-linearity | Scalability | Interpretability |-|-------|-------|---------------|-------------|------------------|-| FastSurvivalSVM | Fast | No | High | Medium |-| FastKernelSurvivalSVM | Medium | Yes | Medium | Low |-| HingeLossSurvivalSVM | Fast | No | High | Medium |-| NaiveSurvivalSVM | Slow | No | Low | Medium |+Official sources checked 2026-07-23:-**General Recommendations:**-- Start with **FastSurvivalSVM** for baseline-- Try **FastKernelSurvivalSVM** with RBF if non-linearity expected-- Use grid search to tune alpha and gamma-- Always standardize features-- Compare with Random Survival Forest and Gradient Boosting+- [Survival SVM user guide](https://scikit-survival.readthedocs.io/en/stable/user_guide/survival-svm.html)+- [FastSurvivalSVM API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.svm.FastSurvivalSVM.html)+- [FastKernelSurvivalSVM API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.svm.FastKernelSurvivalSVM.html)+- [HingeLossSurvivalSVM API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.svm.HingeLossSurvivalSVM.html)+- [MinlipSurvivalAnalysis API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.svm.MinlipSurvivalAnalysis.html)+- [NaiveSurvivalSVM API](https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.svm.NaiveSurvivalSVM.html)+- [Clinical kernels API](https://scikit-survival.readthedocs.io/en/stable/api/kernels.html)diff --git a/skills/scikit-survival/scripts/_common.py b/skills/scikit-survival/scripts/_common.pynew file mode 100644index 00000000..8f4e7acb--- /dev/null+++ b/skills/scikit-survival/scripts/_common.py@@ -0,0 +1,456 @@+#!/usr/bin/env python3+"""Shared, standard-library-first helpers for the bundled survival CLIs."""++from __future__ import annotations++import io+import json+import math+import os+import stat+import tempfile+from pathlib import Path+from typing import Any, Iterable+++MAX_INPUT_BYTES = 32 * 1024 * 1024+MAX_REPORT_BYTES = 4 * 1024 * 1024+MAX_ROWS = 20_000+MAX_FEATURES = 256+MAX_TIME_POINTS = 512+DEFAULT_SEED = 20_260_723+PINNED_INSTALL = (+ 'uv pip install "scikit-survival==0.28.0" "scikit-learn==1.9.0" '+ '"numpy==2.4.6" "pandas==3.0.5" "scipy==1.17.1"'+)+++class CliError(ValueError):+ """An expected command-line validation error."""+++def bounded_int(minimum: int, maximum: int):+ """Return an argparse converter for a bounded integer."""++ def convert(value: str) -> int:+ try:+ parsed = int(value)+ except ValueError as exc:+ raise CliError(f"expected an integer, got {value!r}") from exc+ if not minimum <= parsed <= maximum:+ raise CliError(+ f"expected an integer from {minimum} through {maximum}, got {parsed}"+ )+ return parsed++ return convert+++def finite_float(value: str) -> float:+ """Parse a finite floating-point value."""++ try:+ parsed = float(value)+ except ValueError as exc:+ raise CliError(f"expected a number, got {value!r}") from exc+ if not math.isfinite(parsed):+ raise CliError("value must be finite")+ return parsed+++def positive_float(value: str) -> float:+ """Parse a finite positive floating-point value."""++ parsed = finite_float(value)+ if parsed <= 0:+ raise CliError("value must be greater than zero")+ return parsed+++def probability(value: str) -> float:+ """Parse a probability strictly between zero and one."""++ parsed = finite_float(value)+ if not 0 < parsed < 1:+ raise CliError("value must be strictly between zero and one")+ return parsed+++def parse_names(value: str | None) -> list[str]:+ """Parse a comma-separated list of unique, non-empty column names."""++ if value is None:+ return []+ names = [item.strip() for item in value.split(",")]+ if not names or any(not item for item in names):+ raise CliError("column lists must contain non-empty comma-separated names")+ if len(names) != len(set(names)):+ raise CliError("column lists must not contain duplicates")+ if len(names) > MAX_FEATURES:+ raise CliError(f"at most {MAX_FEATURES} columns are allowed")+ return names+++def parse_floats(value: str | None) -> list[float]:+ """Parse a comma-separated list of finite floating-point values."""++ if value is None:+ return []+ items = [item.strip() for item in value.split(",")]+ if not items or any(not item for item in items):+ raise CliError("expected non-empty comma-separated numbers")+ values = [finite_float(item) for item in items]+ if len(values) > MAX_TIME_POINTS:+ raise CliError(f"at most {MAX_TIME_POINTS} values are allowed")+ return values+++def checked_input_file(+ value: str | os.PathLike[str],+ *,+ suffixes: Iterable[str],+ max_bytes: int = MAX_INPUT_BYTES,+) -> Path:+ """Return a bounded regular local file, rejecting URLs and symlinks."""++ raw = os.fspath(value)+ if "://" in raw:+ raise CliError("network URLs are not accepted; provide a local file")+ path = Path(raw).expanduser()+ if path.is_symlink():+ raise CliError(f"input must not be a symlink: {path}")+ try:+ info = path.stat()+ except OSError as exc:+ raise CliError(f"cannot access input file {path}: {exc}") from exc+ if not stat.S_ISREG(info.st_mode):+ raise CliError(f"input is not a regular file: {path}")+ if info.st_size > max_bytes:+ raise CliError(f"input is {info.st_size} bytes; limit is {max_bytes} bytes")+ allowed = {suffix.lower() for suffix in suffixes}+ if path.suffix.lower() not in allowed:+ raise CliError(f"input suffix must be one of: {', '.join(sorted(allowed))}")+ return path.resolve()+++def checked_output_file(+ value: str | os.PathLike[str],+ *,+ suffixes: Iterable[str],+ force: bool = False,+) -> Path:+ """Validate an explicit local output without following symlinks."""++ raw = os.fspath(value)+ if "://" in raw:+ raise CliError("network URLs are not accepted as output paths")+ path = Path(raw).expanduser()+ if path.name in {"", ".", ".."}:+ raise CliError("output must name a file")+ if path.is_symlink():+ raise CliError(f"output must not be a symlink: {path}")+ allowed = {suffix.lower() for suffix in suffixes}+ if path.suffix.lower() not in allowed:+ raise CliError(f"output suffix must be one of: {', '.join(sorted(allowed))}")+ parent = path.parent+ if not parent.exists() or not parent.is_dir() or parent.is_symlink():+ raise CliError(f"output parent must be an existing regular directory: {parent}")+ if path.exists():+ if not path.is_file():+ raise CliError(f"output exists and is not a regular file: {path}")+ if not force:+ raise CliError(f"refusing to overwrite existing output: {path}")+ return parent.resolve() / path.name+++def atomic_write_bytes(path: Path, payload: bytes, *, force: bool = False) -> None:+ """Write bytes through a private same-directory temporary file."""++ destination = checked_output_file(path, suffixes={path.suffix.lower()}, force=force)+ descriptor, temporary_name = tempfile.mkstemp(+ prefix=f".{destination.name}.", suffix=".tmp", dir=destination.parent+ )+ temporary = Path(temporary_name)+ try:+ with os.fdopen(descriptor, "wb") as handle:+ handle.write(payload)+ handle.flush()+ os.fsync(handle.fileno())+ os.chmod(temporary, 0o600)+ if destination.exists() and not force:+ raise CliError(f"refusing to overwrite existing output: {destination}")+ os.replace(temporary, destination)+ finally:+ temporary.unlink(missing_ok=True)+++def _json_bytes(document: Any) -> bytes:+ """Serialize deterministic strict JSON."""++ try:+ payload = (+ json.dumps(+ document,+ indent=2,+ sort_keys=True,+ ensure_ascii=False,+ allow_nan=False,+ )+ + "\n"+ ).encode("utf-8")+ except (TypeError, ValueError) as exc:+ raise CliError(f"report is not strict JSON: {exc}") from exc+ if len(payload) > MAX_REPORT_BYTES:+ raise CliError(+ f"report is {len(payload)} bytes; limit is {MAX_REPORT_BYTES} bytes"+ )+ return payload+++def emit_json(+ document: Any,+ *,+ output: str | os.PathLike[str] | None = None,+ force: bool = False,+) -> None:+ """Print deterministic JSON or write it atomically."""++ payload = _json_bytes(document)+ if output is None:+ print(payload.decode("utf-8"), end="")+ return+ destination = checked_output_file(output, suffixes={".json"}, force=force)+ atomic_write_bytes(destination, payload, force=force)+++def emit_text(+ text: str,+ *,+ output: str | os.PathLike[str] | None = None,+ force: bool = False,+) -> None:+ """Print text or write it atomically to Markdown."""++ payload = text.encode("utf-8")+ if len(payload) > MAX_REPORT_BYTES:+ raise CliError(+ f"report is {len(payload)} bytes; limit is {MAX_REPORT_BYTES} bytes"+ )+ if output is None:+ print(text, end="" if text.endswith("\n") else "\n")+ return+ destination = checked_output_file(output, suffixes={".md"}, force=force)+ atomic_write_bytes(destination, payload, force=force)+++def load_json(value: str | os.PathLike[str]) -> Any:+ """Load bounded strict JSON from a local file."""++ path = checked_input_file(value, suffixes={".json"})++ def reject_constant(constant: str) -> None:+ raise CliError(f"non-standard JSON constant is not allowed: {constant}")++ try:+ with path.open("r", encoding="utf-8") as handle:+ return json.load(handle, parse_constant=reject_constant)+ except (OSError, json.JSONDecodeError) as exc:+ raise CliError(f"cannot read valid JSON from {path.name}: {exc}") from exc+++def read_csv(value: str | os.PathLike[str]):+ """Load a bounded local CSV with pandas, rejecting excessive dimensions."""++ path = checked_input_file(value, suffixes={".csv"})+ try:+ import pandas as pd+ except ImportError as exc:+ raise CliError(+ f"pandas is unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ try:+ frame = pd.read_csv(path, nrows=MAX_ROWS + 1)+ except Exception as exc:+ raise CliError(f"cannot parse CSV {path.name}: {exc}") from exc+ validate_frame_bounds(frame)+ return frame, path.name+++def validate_frame_bounds(frame: Any) -> None:+ """Reject empty or oversized tabular inputs."""++ rows, columns = frame.shape+ if rows == 0:+ raise CliError("input contains no rows")+ if rows > MAX_ROWS:+ raise CliError(f"input has {rows} rows; limit is {MAX_ROWS}")+ if columns > MAX_FEATURES + 2:+ raise CliError(f"input has {columns} columns; limit is {MAX_FEATURES + 2}")+ if not frame.columns.is_unique:+ raise CliError("column names must be unique")+++def normalize_binary_event(values: Any):+ """Return a strict boolean event vector from bool or 0/1 values."""++ try:+ import numpy as np+ import pandas as pd+ except ImportError as exc:+ raise CliError(+ f"scientific stack unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ series = pd.Series(values)+ if series.isna().any():+ raise CliError("event indicator contains missing values")+ if pd.api.types.is_bool_dtype(series.dtype):+ result = series.astype(bool).to_numpy()+ elif pd.api.types.is_numeric_dtype(series.dtype):+ numeric = pd.to_numeric(series, errors="raise").to_numpy(dtype=float)+ if not np.isfinite(numeric).all() or not np.isin(numeric, [0.0, 1.0]).all():+ raise CliError("event indicator must contain only boolean or 0/1 values")+ result = numeric.astype(bool)+ else:+ normalized = series.astype(str).str.strip().str.casefold()+ mapping = {"true": True, "false": False, "1": True, "0": False}+ if not normalized.isin(mapping).all():+ raise CliError(+ "event indicator strings must be one of true, false, 1, or 0"+ )+ result = normalized.map(mapping).to_numpy(dtype=bool)+ if not result.any():+ raise CliError("at least one observed event is required")+ return result+++def normalize_positive_times(values: Any, *, label: str = "time"):+ """Return a finite, strictly positive float time vector."""++ try:+ import numpy as np+ import pandas as pd+ except ImportError as exc:+ raise CliError(+ f"scientific stack unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ try:+ times = np.asarray(pd.to_numeric(values, errors="raise"), dtype=float)+ except (TypeError, ValueError) as exc:+ raise CliError(f"{label} must contain only numeric values") from exc+ if times.ndim != 1:+ raise CliError(f"{label} must be one-dimensional")+ if not np.isfinite(times).all():+ raise CliError(f"{label} contains missing or non-finite values")+ if (times <= 0).any():+ raise CliError(f"{label} must be strictly positive")+ return times+++def structured_survival(+ event: Any,+ time: Any,+ *,+ event_name: str = "event",+ time_name: str = "time",+):+ """Create scikit-survival's two-field structured outcome lazily."""++ if event_name == time_name:+ raise CliError("event and time field names must differ")+ try:+ from sksurv.util import Surv+ except ImportError as exc:+ raise CliError(+ f"scikit-survival is unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ return Surv.from_arrays(+ event=normalize_binary_event(event),+ time=normalize_positive_times(time),+ name_event=event_name,+ name_time=time_name,+ )+++def atomic_save_npy(+ value: Any, output: str | os.PathLike[str], *, force: bool = False+) -> None:+ """Write a NumPy array with pickle disabled."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError(+ f"NumPy is unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ destination = checked_output_file(output, suffixes={".npy"}, force=force)+ buffer = io.BytesIO()+ np.save(buffer, value, allow_pickle=False)+ atomic_write_bytes(destination, buffer.getvalue(), force=force)+++def atomic_save_npz(+ arrays: dict[str, Any],+ output: str | os.PathLike[str],+ *,+ force: bool = False,+) -> None:+ """Write named NumPy arrays to a compressed archive without object arrays."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError(+ f"NumPy is unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ if not arrays:+ raise CliError("at least one array is required")+ for name, value in arrays.items():+ array = np.asarray(value)+ if array.dtype.hasobject:+ raise CliError(f"array {name!r} has object dtype, which is not allowed")+ destination = checked_output_file(output, suffixes={".npz"}, force=force)+ buffer = io.BytesIO()+ np.savez_compressed(buffer, **arrays)+ atomic_write_bytes(destination, buffer.getvalue(), force=force)+++def synthetic_survival_frame(+ *, rows: int = 240, seed: int = DEFAULT_SEED+) -> tuple[Any, list[str], list[str]]:+ """Create deterministic, non-clinical right-censored tabular data."""++ if not 40 <= rows <= MAX_ROWS:+ raise CliError(f"synthetic rows must be between 40 and {MAX_ROWS}")+ try:+ import numpy as np+ import pandas as pd+ except ImportError as exc:+ raise CliError(+ f"scientific stack unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ rng = np.random.default_rng(seed)+ x_linear = rng.normal(size=rows)+ x_noise = rng.normal(size=rows)+ segment = rng.choice(["alpha", "beta", "gamma"], size=rows, p=[0.4, 0.35, 0.25])+ segment_effect = np.select(+ [segment == "beta", segment == "gamma"], [0.35, -0.25], default=0.0+ )+ linear_predictor = 0.8 * x_linear + segment_effect+ event_time = rng.exponential(scale=8.0 * np.exp(-linear_predictor))+ censor_time = rng.exponential(scale=11.0, size=rows)+ event = event_time <= censor_time+ observed_time = np.minimum(event_time, censor_time) + 0.05+ x_linear = x_linear.astype(float)+ x_linear[::29] = np.nan+ segment = segment.astype(object)+ segment[::37] = np.nan+ frame = pd.DataFrame(+ {+ "event": event,+ "time": observed_time,+ "x_linear": x_linear,+ "x_noise": x_noise,+ "segment": segment,+ }+ )+ return frame, ["x_linear", "x_noise"], ["segment"]diff --git a/skills/scikit-survival/scripts/competing_risk_cif.py b/skills/scikit-survival/scripts/competing_risk_cif.pynew file mode 100644index 00000000..7bb57197--- /dev/null+++ b/skills/scikit-survival/scripts/competing_risk_cif.py@@ -0,0 +1,286 @@+#!/usr/bin/env python3+"""Estimate nonparametric competing-risk cumulative incidence from local data."""++from __future__ import annotations++import argparse+from typing import Any++from _common import (+ DEFAULT_SEED,+ CliError,+ atomic_save_npz,+ bounded_int,+ emit_json,+ parse_floats,+ probability,+ read_csv,+)+++VARIANCE_CHOICES = ("Aalen", "Dinse", "Dinse_Approx")+MAX_CIF_ROWS = 5_000+MAX_CAUSES = 32+++def synthetic_competing_risks(+ *, rows: int = 300, seed: int = DEFAULT_SEED+) -> tuple[Any, Any]:+ """Create deterministic, non-clinical outcomes with three competing causes."""++ try:+ import numpy as np+ import pandas as pd+ except ImportError as exc:+ raise CliError("NumPy and pandas are required") from exc+ rng = np.random.default_rng(seed)+ latent = rng.normal(size=rows)+ cause_times = np.column_stack(+ [+ rng.exponential(scale=7.0 * np.exp(-0.25 * latent)),+ rng.exponential(scale=10.0 * np.exp(0.15 * latent)),+ rng.exponential(scale=14.0, size=rows),+ ]+ )+ censor_time = rng.exponential(scale=16.0, size=rows)+ cause = np.argmin(cause_times, axis=1) + 1+ event_time = cause_times[np.arange(rows), cause - 1]+ event = np.where(event_time <= censor_time, cause, 0)+ time = np.minimum(event_time, censor_time) + 0.05+ for cause_code in (1, 2, 3):+ event[cause_code - 1] = cause_code+ time[cause_code - 1] = float(cause_code)+ frame = pd.DataFrame({"status": event, "time": time})+ return frame, {"kind": "synthetic", "network_used": False, "seed": seed}+++def normalize_competing_event(values: Any):+ """Return non-negative contiguous integer cause codes."""++ try:+ import numpy as np+ import pandas as pd+ except ImportError as exc:+ raise CliError("NumPy and pandas are required") from exc+ series = pd.Series(values)+ if series.isna().any():+ raise CliError("competing-risk event codes contain missing values")+ try:+ numeric = pd.to_numeric(series, errors="raise").to_numpy(dtype=float)+ except (TypeError, ValueError) as exc:+ raise CliError("event codes must be integers") from exc+ if not np.isfinite(numeric).all() or (numeric < 0).any():+ raise CliError("event codes must be finite non-negative integers")+ if not np.equal(numeric, np.floor(numeric)).all():+ raise CliError("event codes must be integers")+ event = numeric.astype(int)+ causes = sorted(set(event.tolist()) - {0})+ if len(causes) < 2:+ raise CliError("at least two competing causes are required")+ if len(causes) > MAX_CAUSES:+ raise CliError(f"at most {MAX_CAUSES} competing causes are allowed")+ expected = list(range(1, max(causes) + 1))+ if causes != expected:+ raise CliError(+ "positive cause codes must be contiguous and every cause must occur"+ )+ return event+++def normalize_times(values: Any):+ """Return a finite, strictly positive time vector."""++ from _common import normalize_positive_times++ return normalize_positive_times(values, label="competing-risk time")+++def horizon_values(+ unique_times: Any,+ cumulative_incidence: Any,+ horizons: list[float],+) -> list[dict[str, Any]]:+ """Evaluate right-continuous step estimates at requested horizons."""++ import numpy as np++ records: list[dict[str, Any]] = []+ for horizon in horizons:+ if horizon <= 0 or not np.isfinite(horizon):+ raise CliError("horizons must be finite and strictly positive")+ index = int(np.searchsorted(unique_times, horizon, side="right") - 1)+ values = (+ np.zeros(cumulative_incidence.shape[0], dtype=float)+ if index < 0+ else cumulative_incidence[:, index]+ )+ records.append(+ {+ "cause_specific": {+ str(cause): float(values[cause])+ for cause in range(1, cumulative_incidence.shape[0])+ },+ "horizon": float(horizon),+ "total_risk": float(values[0]),+ }+ )+ return records+++def estimate_cif(+ event: Any,+ time: Any,+ *,+ horizons: list[float] | None = None,+ confidence: bool = False,+ confidence_level: float = 0.95,+ variance_type: str = "Aalen",+ time_min: float | None = None,+) -> tuple[dict[str, Any], dict[str, Any]]:+ """Estimate CIF curves and return a bounded summary plus numeric arrays."""++ try:+ import numpy as np+ from sksurv.nonparametric import cumulative_incidence_competing_risks+ except ImportError as exc:+ raise CliError("the pinned scikit-survival stack is required") from exc+ checked_event = normalize_competing_event(event)+ checked_time = normalize_times(time)+ if len(checked_event) != len(checked_time):+ raise CliError("event and time arrays must have equal length")+ if len(checked_event) > MAX_CIF_ROWS:+ raise CliError(f"CIF estimation is limited to {MAX_CIF_ROWS} rows")+ if confidence and variance_type == "Dinse" and len(checked_event) > 2_000:+ raise CliError("Dinse variance is limited to 2,000 rows")+ if time_min is not None and (+ not np.isfinite(time_min) or time_min < 0 or time_min >= checked_time.max()+ ):+ raise CliError("time_min must be finite, non-negative, and below max time")++ result = cumulative_incidence_competing_risks(+ checked_event,+ checked_time,+ time_min=time_min,+ conf_level=confidence_level,+ conf_type="log-log" if confidence else None,+ var_type=variance_type,+ )+ if confidence:+ unique_times, cumulative_incidence, intervals = result+ else:+ unique_times, cumulative_incidence = result+ intervals = None+ if (np.diff(cumulative_incidence, axis=1) < -1e-10).any():+ raise CliError("estimated cumulative incidence unexpectedly decreased")+ if not np.allclose(+ cumulative_incidence[0],+ cumulative_incidence[1:].sum(axis=0),+ rtol=1e-8,+ atol=1e-10,+ ):+ raise CliError("cause-specific CIFs do not sum to total risk")++ if horizons is None:+ horizons = [+ float(value) for value in np.quantile(checked_time, [0.25, 0.5, 0.75])+ ]+ if len(horizons) > 32:+ raise CliError("at most 32 reporting horizons are allowed")+ summary = {+ "cause_counts": {+ str(code): int((checked_event == code).sum())+ for code in range(0, int(checked_event.max()) + 1)+ },+ "confidence": {+ "enabled": confidence,+ "level": confidence_level if confidence else None,+ "type": "log-log" if confidence else None,+ "variance_type": variance_type if confidence else None,+ },+ "event_coding": "0=censored; positive contiguous integers=causes",+ "horizons": horizon_values(+ unique_times, cumulative_incidence, sorted(horizons)+ ),+ "n_causes": int(checked_event.max()),+ "rows": int(len(checked_event)),+ "schema_version": "1.0",+ "time_min": time_min,+ "warning": (+ "Cumulative incidence is cause-specific absolute event probability, "+ "not a cause-specific hazard or proof of treatment benefit."+ ),+ }+ arrays = {+ "cumulative_incidence": cumulative_incidence,+ "time": unique_times,+ }+ if intervals is not None:+ arrays["confidence_interval"] = intervals+ return summary, arrays+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Estimate nonparametric cumulative incidence for competing causes. "+ "No input uses deterministic synthetic data; no network is used."+ )+ )+ parser.add_argument("--input", help="Optional bounded local .csv")+ parser.add_argument("--event-column", default="status")+ parser.add_argument("--time-column", default="time")+ parser.add_argument("--horizons", help="Optional comma-separated report horizons")+ parser.add_argument("--time-min", type=float)+ parser.add_argument("--confidence", action="store_true")+ parser.add_argument("--confidence-level", type=probability, default=0.95)+ parser.add_argument("--variance-type", choices=VARIANCE_CHOICES, default="Aalen")+ parser.add_argument("--seed", type=bounded_int(0, 2**32 - 1), default=DEFAULT_SEED)+ parser.add_argument("--synthetic-rows", type=bounded_int(80, 20_000), default=300)+ parser.add_argument(+ "--curve-output", help="Optional local .npz containing full CIF arrays"+ )+ parser.add_argument("--output", help="Optional local .json summary")+ parser.add_argument("--force", action="store_true")+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ args = parser.parse_args(argv)+ try:+ if args.input:+ frame, name = read_csv(args.input)+ source = {"kind": "local_csv", "name": name}+ else:+ frame, source = synthetic_competing_risks(+ rows=args.synthetic_rows, seed=args.seed+ )+ missing = [+ name+ for name in (args.event_column, args.time_column)+ if name not in frame.columns+ ]+ if missing:+ raise CliError(f"missing columns: {', '.join(missing)}")+ horizons = parse_floats(args.horizons) or None+ summary, arrays = estimate_cif(+ frame[args.event_column],+ frame[args.time_column],+ horizons=horizons,+ confidence=args.confidence,+ confidence_level=args.confidence_level,+ variance_type=args.variance_type,+ time_min=args.time_min,+ )+ summary["source"] = source+ if args.curve_output:+ atomic_save_npz(arrays, args.curve_output, force=args.force)+ summary["curve_output"] = args.curve_output+ emit_json(summary, output=args.output, force=args.force)+ except CliError as exc:+ parser.error(str(exc))+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/scikit-survival/scripts/evaluate_survival_metrics.py b/skills/scikit-survival/scripts/evaluate_survival_metrics.pynew file mode 100644index 00000000..d08a8bb3--- /dev/null+++ b/skills/scikit-survival/scripts/evaluate_survival_metrics.py@@ -0,0 +1,296 @@+#!/usr/bin/env python3+"""Evaluate censoring-aware survival metrics with strict input contracts."""++from __future__ import annotations++import argparse+from typing import Any++from _common import (+ DEFAULT_SEED,+ MAX_TIME_POINTS,+ CliError,+ bounded_int,+ checked_input_file,+ emit_json,+ structured_survival,+)+++REQUIRED_ARRAYS = {+ "risk",+ "test_event",+ "test_time",+ "times",+ "train_event",+ "train_time",+}+++def synthetic_metric_inputs(seed: int = DEFAULT_SEED) -> dict[str, Any]:+ """Create deterministic, non-clinical predictions with supported follow-up."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError("NumPy is required") from exc+ rng = np.random.default_rng(seed)++ def observed(rows: int) -> tuple[Any, Any, Any]:+ risk = rng.normal(size=rows)+ event_time = rng.exponential(scale=9.0 * np.exp(-0.65 * risk)) + 0.1+ censor_time = rng.exponential(scale=13.0, size=rows) + 0.1+ event = event_time <= censor_time+ time = np.minimum(event_time, censor_time)+ return event, time, risk++ train_event, train_time, _ = observed(220)+ test_event, test_time, risk = observed(90)+ support_limit = float(test_time.max() + 5.0)+ train_event[-1] = False+ train_time[-1] = max(support_limit, float(train_time.max()))+ upper = float(min(np.quantile(test_time, 0.8), train_time.max() - 1e-6))+ lower = float(np.quantile(test_time, 0.2))+ times = np.linspace(lower, upper, 8)+ survival = np.exp(-np.exp(0.65 * risk[:, None]) * times[None, :] / 9.0)+ return {+ "risk": risk,+ "survival": survival,+ "test_event": test_event,+ "test_time": test_time,+ "times": times,+ "train_event": train_event,+ "train_time": train_time,+ }+++def load_prediction_archive(value: str) -> tuple[dict[str, Any], str]:+ """Load a bounded NPZ archive without pickle."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError("NumPy is required") from exc+ path = checked_input_file(value, suffixes={".npz"})+ try:+ with np.load(path, allow_pickle=False) as archive:+ names = set(archive.files)+ missing = REQUIRED_ARRAYS - names+ if missing:+ raise CliError(+ "prediction archive is missing: " + ", ".join(sorted(missing))+ )+ unexpected = names - REQUIRED_ARRAYS - {"survival"}+ if unexpected:+ raise CliError(+ "prediction archive has unsupported arrays: "+ + ", ".join(sorted(unexpected))+ )+ arrays = {name: archive[name] for name in archive.files}+ except (OSError, ValueError) as exc:+ raise CliError(f"cannot load safe NPZ archive {path.name}: {exc}") from exc+ return arrays, path.name+++def validate_time_grid(+ times: Any,+ y_train: Any,+ y_test: Any,+) -> Any:+ """Validate shape, order, observed support, and censoring support."""++ try:+ import numpy as np+ from sksurv.nonparametric import CensoringDistributionEstimator+ except ImportError as exc:+ raise CliError("the pinned survival stack is required") from exc+ values = np.asarray(times, dtype=float)+ if values.ndim != 1:+ raise CliError("times must be one-dimensional")+ if not 2 <= values.size <= MAX_TIME_POINTS:+ raise CliError(f"times must contain 2 through {MAX_TIME_POINTS} values")+ if not np.isfinite(values).all() or (values <= 0).any():+ raise CliError("times must be finite and strictly positive")+ if not (np.diff(values) > 0).all():+ raise CliError("times must be unique and strictly increasing")++ train_time = y_train[y_train.dtype.names[1]]+ test_time = y_test[y_test.dtype.names[1]]+ if not test_time.max() < train_time.max():+ raise CliError(+ "test follow-up must end before training follow-up for IPCW support"+ )+ if values[0] <= test_time.min() or values[-1] >= test_time.max():+ raise CliError(+ "evaluation times must lie strictly inside the test follow-up range"+ )+ if values[-1] >= train_time.max():+ raise CliError("evaluation times exceed training follow-up support")+ censoring = CensoringDistributionEstimator().fit(y_train)+ try:+ probability = censoring.predict_proba(values)+ except ValueError as exc:+ raise CliError(+ f"censoring distribution is undefined on the grid: {exc}"+ ) from exc+ if not np.isfinite(probability).all() or (probability <= 0).any():+ raise CliError(+ "training censoring survival must remain positive across the time grid"+ )+ return values+++def validate_predictions(+ arrays: dict[str, Any],+) -> tuple[Any, Any | None, Any, Any, Any]:+ """Validate risk/survival shapes and return metric-ready arrays."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError("NumPy is required") from exc+ y_train = structured_survival(arrays["train_event"], arrays["train_time"])+ y_test = structured_survival(arrays["test_event"], arrays["test_time"])+ times = validate_time_grid(arrays["times"], y_train, y_test)+ n_test = len(y_test)++ risk = np.asarray(arrays["risk"], dtype=float)+ valid_risk_shape = risk.shape == (n_test,) or risk.shape == (+ n_test,+ len(times),+ )+ if not valid_risk_shape:+ raise CliError("risk must have shape (n_test,) or (n_test, n_times)")+ if not np.isfinite(risk).all():+ raise CliError("risk contains non-finite values")++ survival: Any | None = None+ if "survival" in arrays:+ survival = np.asarray(arrays["survival"], dtype=float)+ if survival.shape != (n_test, len(times)):+ raise CliError("survival must have shape (n_test, n_times)")+ if not np.isfinite(survival).all():+ raise CliError("survival contains non-finite values")+ if ((survival < 0) | (survival > 1)).any():+ raise CliError("survival probabilities must be within [0, 1]")+ if (np.diff(survival, axis=1) > 1e-10).any():+ raise CliError(+ "each survival-probability row must be non-increasing over time"+ )+ return risk, survival, times, y_train, y_test+++def evaluate(arrays: dict[str, Any]) -> dict[str, Any]:+ """Compute discrimination and prediction-error metrics by input type."""++ try:+ import numpy as np+ from sksurv.metrics import (+ brier_score,+ concordance_index_censored,+ concordance_index_ipcw,+ cumulative_dynamic_auc,+ integrated_brier_score,+ )+ except ImportError as exc:+ raise CliError("the pinned survival stack is required") from exc+ risk, survival, times, y_train, y_test = validate_predictions(arrays)+ event_field, time_field = y_test.dtype.names++ auc, mean_auc = cumulative_dynamic_auc(y_train, y_test, risk, times)+ metrics: dict[str, Any] = {+ "cumulative_dynamic_auc": {+ "mean": float(mean_auc),+ "times": times.tolist(),+ "values": np.asarray(auc, dtype=float).tolist(),+ }+ }+ if risk.ndim == 1:+ metrics["harrell_c"] = float(+ concordance_index_censored(y_test[event_field], y_test[time_field], risk)[0]+ )+ metrics["uno_c"] = float(+ concordance_index_ipcw(y_train, y_test, risk, tau=float(times[-1]))[0]+ )+ else:+ metrics["harrell_c"] = None+ metrics["uno_c"] = None+ metrics["concordance_note"] = (+ "Skipped: concordance functions require one risk score per row; "+ "the supplied risk is time-dependent."+ )++ if survival is not None:+ returned_times, scores = brier_score(y_train, y_test, survival, times)+ metrics["brier_score"] = {+ "times": np.asarray(returned_times, dtype=float).tolist(),+ "values": np.asarray(scores, dtype=float).tolist(),+ }+ metrics["integrated_brier_score"] = float(+ integrated_brier_score(y_train, y_test, survival, times)+ )+ else:+ metrics["brier_score"] = None+ metrics["integrated_brier_score"] = None+ metrics["brier_note"] = (+ "Skipped: Brier metrics require survival probabilities, not risk scores."+ )++ return {+ "assumptions": {+ "censoring_distribution_fit_on_training_only": True,+ "independent_censoring_from_features": "required_by_IPCW_estimator",+ "test_follow_up_within_training_support": True,+ "time_grid_within_test_follow_up": True,+ },+ "input_contract": {+ "risk": ("higher means greater event risk; 1D or n_test-by-n_times"),+ "survival": ("optional n_test-by-n_times probabilities; never risk scores"),+ },+ "metrics": metrics,+ "schema_version": "1.0",+ "warning": (+ "Discrimination and prediction error do not establish calibration "+ "at every horizon, causal effects, or clinical utility."+ ),+ }+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Evaluate risk scores and optional survival probabilities from a "+ "bounded local NPZ archive. No input uses deterministic synthetic data."+ )+ )+ parser.add_argument("--input", help="Optional local .npz prediction archive")+ parser.add_argument("--seed", type=bounded_int(0, 2**32 - 1), default=DEFAULT_SEED)+ parser.add_argument("--output", help="Optional local .json report")+ parser.add_argument("--force", action="store_true")+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ args = parser.parse_args(argv)+ try:+ if args.input:+ arrays, name = load_prediction_archive(args.input)+ source = {"kind": "local_npz", "name": name}+ else:+ arrays = synthetic_metric_inputs(args.seed)+ source = {+ "kind": "synthetic",+ "network_used": False,+ "seed": args.seed,+ }+ report = evaluate(arrays)+ report["source"] = source+ emit_json(report, output=args.output, force=args.force)+ except CliError as exc:+ parser.error(str(exc))+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/scikit-survival/scripts/model_report.py b/skills/scikit-survival/scripts/model_report.pynew file mode 100644index 00000000..fecc3767--- /dev/null+++ b/skills/scikit-survival/scripts/model_report.py@@ -0,0 +1,297 @@+#!/usr/bin/env python3+"""Render a bounded Markdown model card from local aggregate JSON summaries."""++from __future__ import annotations++import argparse+import math+from typing import Any++from _common import CliError, emit_text, load_json+++COMPETING_RISK_CHOICES = ("not-assessed", "absent", "present")+++def _mapping(value: Any, label: str) -> dict[str, Any]:+ if not isinstance(value, dict):+ raise CliError(f"{label} must be a JSON object")+ return value+++def _scalar(value: Any, default: str = "not supplied") -> str:+ """Render a bounded scalar without accepting row-level structures."""++ if value is None:+ return default+ if isinstance(value, bool):+ return "yes" if value else "no"+ if isinstance(value, int):+ return str(value)+ if isinstance(value, float):+ if not math.isfinite(value):+ raise CliError("report values must be finite")+ return f"{value:.4g}"+ if isinstance(value, str):+ cleaned = " ".join(value.split())+ if len(cleaned) > 160:+ raise CliError("report strings must be at most 160 characters")+ return cleaned.replace("`", "'")+ raise CliError("model report accepts aggregate scalar values only")+++def synthetic_training_summary() -> dict[str, Any]:+ """Return a deterministic aggregate example with no person-level data."""++ return {+ "data": {+ "censored_test": 18,+ "censored_train": 54,+ "events_test": 42,+ "events_train": 126,+ "rows_test": 60,+ "rows_train": 180,+ },+ "evaluation": {+ "harrell_c": 0.71,+ "uno_c": {+ "available": True,+ "score": 0.69,+ "training_censoring_distribution": True,+ "truncation_time": 8.0,+ },+ },+ "leakage_controls": {+ "holdout_used_for_tuning": False,+ "preprocessing_fit_scope": "training folds only",+ "split_before_imputation_encoding_scaling": True,+ "tuning_evaluation": "not_run",+ },+ "model": {+ "best_params": None,+ "family": "coxph",+ "transformed_feature_count": 4,+ },+ "package_versions": {+ "numpy": "2.4.6",+ "pandas": "3.0.5",+ "scikit-learn": "1.9.0",+ "scikit-survival": "0.28.0",+ },+ "schema": {+ "categorical_features": ["segment"],+ "event": "event",+ "numeric_features": ["x_linear", "x_noise"],+ "time": "time",+ },+ "seed": 20_260_723,+ "source": {"kind": "synthetic"},+ "tuning": {"performed": False},+ }+++def synthetic_metric_summary() -> dict[str, Any]:+ """Return deterministic aggregate metric examples."""++ return {+ "assumptions": {+ "censoring_distribution_fit_on_training_only": True,+ "independent_censoring_from_features": "required_by_IPCW_estimator",+ "test_follow_up_within_training_support": True,+ "time_grid_within_test_follow_up": True,+ },+ "metrics": {+ "cumulative_dynamic_auc": {+ "mean": 0.70,+ "times": [2.0, 4.0, 6.0],+ "values": [0.68, 0.70, 0.72],+ },+ "harrell_c": 0.71,+ "uno_c": 0.69,+ "integrated_brier_score": 0.18,+ },+ "source": {"kind": "synthetic"},+ }+++def _feature_list(schema: dict[str, Any], key: str) -> str:+ value = schema.get(key, [])+ if not isinstance(value, list) or any(not isinstance(item, str) for item in value):+ raise CliError(f"schema.{key} must be a list of strings")+ if len(value) > 256:+ raise CliError("too many feature names in model summary")+ return ", ".join(f"`{_scalar(item)}`" for item in value) or "none"+++def render_report(+ training: dict[str, Any],+ metrics: dict[str, Any],+ *,+ competing_risks: str,+ title: str,+) -> str:+ """Render aggregate summaries while preserving metric distinctions."""++ training = _mapping(training, "training summary")+ metrics = _mapping(metrics, "metrics summary")+ data = _mapping(training.get("data", {}), "training.data")+ model = _mapping(training.get("model", {}), "training.model")+ schema = _mapping(training.get("schema", {}), "training.schema")+ leakage = _mapping(+ training.get("leakage_controls", {}), "training.leakage_controls"+ )+ versions = _mapping(+ training.get("package_versions", {}), "training.package_versions"+ )+ metric_values = _mapping(metrics.get("metrics", {}), "metrics.metrics")+ assumptions = _mapping(metrics.get("assumptions", {}), "metrics.assumptions")+ dynamic_auc = metric_values.get("cumulative_dynamic_auc")+ dynamic_auc_mean = (+ _mapping(dynamic_auc, "cumulative_dynamic_auc").get("mean")+ if dynamic_auc is not None+ else None+ )++ if competing_risks == "present":+ competing_note = (+ "Competing causes are present. Standard all-event metrics are not "+ "cause-specific CIF validation; report each cause separately."+ )+ elif competing_risks == "absent":+ competing_note = "No competing cause was declared for this analysis."+ else:+ competing_note = (+ "Competing risks were not assessed; this must be resolved before "+ "interpreting cause-specific event probability."+ )++ lines = [+ f"# {_scalar(title)}",+ "",+ "## Scope",+ "",+ "- Aggregate, local report only; no person-level rows are embedded.",+ "- Predictive performance does not establish causal effects or clinical utility.",+ "- This report is not clinical advice.",+ "",+ "## Data and split",+ "",+ f"- Training rows: {_scalar(data.get('rows_train'))}",+ f"- Test rows: {_scalar(data.get('rows_test'))}",+ f"- Training events / censored: {_scalar(data.get('events_train'))} / "+ f"{_scalar(data.get('censored_train'))}",+ f"- Test events / censored: {_scalar(data.get('events_test'))} / "+ f"{_scalar(data.get('censored_test'))}",+ f"- Deterministic seed: {_scalar(training.get('seed'))}",+ "",+ "## Explicit schema",+ "",+ f"- Event column: `{_scalar(schema.get('event'))}`",+ f"- Time column: `{_scalar(schema.get('time'))}`",+ f"- Numeric features: {_feature_list(schema, 'numeric_features')}",+ f"- Categorical features: {_feature_list(schema, 'categorical_features')}",+ "",+ "## Model and leakage controls",+ "",+ f"- Model family: `{_scalar(model.get('family'))}`",+ f"- Transformed features: {_scalar(model.get('transformed_feature_count'))}",+ "- Split before fitting imputation/encoding/scaling: "+ f"{_scalar(leakage.get('split_before_imputation_encoding_scaling'))}",+ f"- Preprocessing fit scope: {_scalar(leakage.get('preprocessing_fit_scope'))}",+ f"- Holdout used for tuning: {_scalar(leakage.get('holdout_used_for_tuning'))}",+ f"- Tuning evaluation: {_scalar(leakage.get('tuning_evaluation'))}",+ "",+ "## Performance",+ "",+ f"- Harrell C (rank discrimination): {_scalar(metric_values.get('harrell_c'))}",+ f"- Uno C (IPCW rank discrimination): {_scalar(metric_values.get('uno_c'))}",+ "- Mean cumulative/dynamic AUC (time-specific discrimination): "+ f"{_scalar(dynamic_auc_mean)}",+ "- Integrated Brier score (probability prediction error; lower is better): "+ f"{_scalar(metric_values.get('integrated_brier_score'))}",+ "- A Brier score mixes discrimination and calibration; it is not a "+ "standalone calibration curve.",+ "",+ "## Censoring and competing risks",+ "",+ "- IPCW censoring distribution fit on training data: "+ f"{_scalar(assumptions.get('censoring_distribution_fit_on_training_only'))}",+ "- Independent censoring from features: "+ f"{_scalar(assumptions.get('independent_censoring_from_features'))}",+ "- Evaluation grid within train/test support: "+ f"{_scalar(assumptions.get('test_follow_up_within_training_support'))} / "+ f"{_scalar(assumptions.get('time_grid_within_test_follow_up'))}",+ f"- {competing_note}",+ "",+ "## Runtime snapshot",+ "",+ f"- scikit-survival: {_scalar(versions.get('scikit-survival'))}",+ f"- scikit-learn: {_scalar(versions.get('scikit-learn'))}",+ f"- NumPy: {_scalar(versions.get('numpy'))}",+ f"- pandas: {_scalar(versions.get('pandas'))}",+ "",+ "## Required follow-up",+ "",+ "- Check proportional-hazards assumptions when interpreting Cox effects.",+ "- Inspect horizon-specific calibration on independent validation data.",+ "- Use nested CV for performance claims made during hyperparameter tuning.",+ "- Evaluate transportability, subgroup behavior, and decision consequences "+ "separately; these metrics alone do not establish utility.",+ "",+ ]+ return "\n".join(lines)+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Render a Markdown model report from aggregate local JSON summaries. "+ "No inputs produces a deterministic synthetic example."+ )+ )+ parser.add_argument("--training-summary", help="Optional local training .json")+ parser.add_argument("--metrics-summary", help="Optional local metrics .json")+ parser.add_argument(+ "--competing-risks",+ choices=COMPETING_RISK_CHOICES,+ default="not-assessed",+ )+ parser.add_argument("--title", default="Survival model report")+ parser.add_argument("--output", help="Optional local .md output")+ parser.add_argument("--force", action="store_true")+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ args = parser.parse_args(argv)+ try:+ if bool(args.training_summary) != bool(args.metrics_summary):+ raise CliError(+ "provide both --training-summary and --metrics-summary, or neither "+ "for the fully synthetic example"+ )+ training = (+ load_json(args.training_summary)+ if args.training_summary+ else synthetic_training_summary()+ )+ metrics = (+ load_json(args.metrics_summary)+ if args.metrics_summary+ else synthetic_metric_summary()+ )+ report = render_report(+ training,+ metrics,+ competing_risks=args.competing_risks,+ title=args.title,+ )+ emit_text(report, output=args.output, force=args.force)+ except CliError as exc:+ parser.error(str(exc))+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/scikit-survival/scripts/train_survival_model.py b/skills/scikit-survival/scripts/train_survival_model.pynew file mode 100644index 00000000..aae98152--- /dev/null+++ b/skills/scikit-survival/scripts/train_survival_model.py@@ -0,0 +1,583 @@+#!/usr/bin/env python3+"""Train a leakage-safe Cox or ensemble example on explicit local schema."""++from __future__ import annotations++import argparse+from importlib.metadata import version+from typing import Any++from _common import (+ DEFAULT_SEED,+ MAX_FEATURES,+ CliError,+ atomic_save_npz,+ bounded_int,+ emit_json,+ parse_names,+ probability,+ read_csv,+ structured_survival,+ synthetic_survival_frame,+)+++MODEL_CHOICES = (+ "coxph",+ "coxnet",+ "random-forest",+ "extra-trees",+ "gradient-boosting",+)+++def resolve_schema(+ frame: Any,+ *,+ event_column: str,+ time_column: str,+ numeric_columns: list[str],+ categorical_columns: list[str],+) -> tuple[Any, Any, dict[str, Any]]:+ """Select explicit features and build a validated structured outcome."""++ if not numeric_columns and not categorical_columns:+ raise CliError(+ "an explicit schema is required: provide --numeric-columns and/or "+ "--categorical-columns"+ )+ feature_columns = numeric_columns + categorical_columns+ if len(feature_columns) > MAX_FEATURES:+ raise CliError(f"at most {MAX_FEATURES} features are allowed")+ if len(feature_columns) != len(set(feature_columns)):+ raise CliError("numeric and categorical feature lists must be disjoint")+ if event_column == time_column:+ raise CliError("event and time columns must differ")+ forbidden = {event_column, time_column}.intersection(feature_columns)+ if forbidden:+ raise CliError(+ "outcomes must not be used as features: " + ", ".join(sorted(forbidden))+ )+ required = [event_column, time_column, *feature_columns]+ missing = [name for name in required if name not in frame.columns]+ if missing:+ raise CliError(f"missing schema columns: {', '.join(missing)}")++ y = structured_survival(+ frame[event_column],+ frame[time_column],+ event_name=event_column,+ time_name=time_column,+ )+ X = frame.loc[:, feature_columns].copy()+ schema = {+ "categorical_features": categorical_columns,+ "event": event_column,+ "numeric_features": numeric_columns,+ "time": time_column,+ }+ return X, y, schema+++def build_pipeline(+ model_name: str,+ numeric_columns: list[str],+ categorical_columns: list[str],+ *,+ seed: int,+):+ """Build a preprocessing-and-model pipeline without fitting it."""++ try:+ from sklearn.compose import ColumnTransformer+ from sklearn.impute import SimpleImputer+ from sklearn.pipeline import Pipeline+ from sklearn.preprocessing import OneHotEncoder, StandardScaler+ from sksurv.ensemble import (+ ExtraSurvivalTrees,+ GradientBoostingSurvivalAnalysis,+ RandomSurvivalForest,+ )+ from sksurv.linear_model import (+ CoxnetSurvivalAnalysis,+ CoxPHSurvivalAnalysis,+ )+ except ImportError as exc:+ from _common import PINNED_INSTALL++ raise CliError(+ f"survival stack unavailable; install with `{PINNED_INSTALL}`"+ ) from exc++ transformers: list[tuple[str, Any, list[str]]] = []+ if numeric_columns:+ numeric = Pipeline(+ [+ ("imputer", SimpleImputer(strategy="median")),+ ("scale", StandardScaler()),+ ]+ )+ transformers.append(("numeric", numeric, numeric_columns))+ if categorical_columns:+ categorical = Pipeline(+ [+ ("imputer", SimpleImputer(strategy="most_frequent")),+ (+ "encode",+ OneHotEncoder(+ drop="first",+ handle_unknown="ignore",+ sparse_output=False,+ ),+ ),+ ]+ )+ transformers.append(("categorical", categorical, categorical_columns))+ preprocess = ColumnTransformer(+ transformers,+ remainder="drop",+ sparse_threshold=0.0,+ verbose_feature_names_out=False,+ )++ if model_name == "coxph":+ model = CoxPHSurvivalAnalysis(alpha=0.1, ties="efron")+ elif model_name == "coxnet":+ model = CoxnetSurvivalAnalysis(+ alphas=[0.05],+ l1_ratio=0.5,+ fit_baseline_model=True,+ )+ elif model_name == "random-forest":+ model = RandomSurvivalForest(+ n_estimators=64,+ min_samples_leaf=5,+ n_jobs=1,+ random_state=seed,+ )+ elif model_name == "extra-trees":+ model = ExtraSurvivalTrees(+ n_estimators=64,+ min_samples_leaf=5,+ n_jobs=1,+ random_state=seed,+ )+ elif model_name == "gradient-boosting":+ model = GradientBoostingSurvivalAnalysis(+ n_estimators=64,+ learning_rate=0.05,+ max_depth=2,+ random_state=seed,+ )+ else:+ raise CliError(f"unsupported model: {model_name}")+ return Pipeline([("preprocess", preprocess), ("model", model)])+++def parameter_grid(model_name: str) -> dict[str, list[Any]]:+ """Return a deliberately small, bounded tuning grid."""++ if model_name == "coxph":+ return {"model__alpha": [0.01, 0.1, 1.0]}+ if model_name == "coxnet":+ return {"model__alphas": [[0.01], [0.05], [0.2]]}+ if model_name in {"random-forest", "extra-trees"}:+ return {"model__min_samples_leaf": [3, 6, 10]}+ if model_name == "gradient-boosting":+ return {+ "model__learning_rate": [0.03, 0.1],+ "model__max_depth": [1, 2],+ }+ raise CliError(f"unsupported model: {model_name}")+++def _event_field(y: Any) -> str:+ return str(y.dtype.names[0])+++def _time_field(y: Any) -> str:+ return str(y.dtype.names[1])+++def stratified_splits(y: Any, folds: int, *, seed: int) -> list[tuple[Any, Any]]:+ """Create deterministic event-stratified indices."""++ try:+ import numpy as np+ from sklearn.model_selection import StratifiedKFold+ except ImportError as exc:+ raise CliError("scikit-learn and NumPy are required") from exc+ labels = y[_event_field(y)].astype(int)+ counts = np.bincount(labels, minlength=2)+ if counts.min() < folds:+ raise CliError(+ f"{folds}-fold CV requires at least {folds} events and censored rows"+ )+ splitter = StratifiedKFold(n_splits=folds, shuffle=True, random_state=seed)+ return list(splitter.split(np.zeros(len(y)), labels))+++def uno_concordance(y_train: Any, y_test: Any, risk: Any) -> dict[str, Any]:+ """Evaluate IPCW concordance using only the training censoring distribution."""++ try:+ import numpy as np+ from sksurv.metrics import concordance_index_ipcw+ except ImportError as exc:+ raise CliError("scikit-survival and NumPy are required") from exc+ time_train = y_train[_time_field(y_train)]+ time_test = y_test[_time_field(y_test)]+ tau = float(min(np.quantile(time_train, 0.8), np.quantile(time_test, 0.8)))+ try:+ score = float(concordance_index_ipcw(y_train, y_test, risk, tau=tau)[0])+ except ValueError as exc:+ return {+ "available": False,+ "reason": str(exc),+ "training_censoring_distribution": True,+ "truncation_time": tau,+ }+ return {+ "available": True,+ "score": score,+ "training_censoring_distribution": True,+ "truncation_time": tau,+ }+++def nested_tune(+ X: Any,+ y: Any,+ *,+ model_name: str,+ numeric_columns: list[str],+ categorical_columns: list[str],+ outer_folds: int,+ inner_folds: int,+ seed: int,+) -> tuple[list[dict[str, Any]], dict[str, Any]]:+ """Run nested CV on training data, then tune once on all training rows."""++ try:+ from sklearn.model_selection import GridSearchCV+ from sksurv.metrics import concordance_index_censored+ except ImportError as exc:+ raise CliError("scikit-learn and scikit-survival are required") from exc++ outer_results: list[dict[str, Any]] = []+ for fold, (train_index, validation_index) in enumerate(+ stratified_splits(y, outer_folds, seed=seed), start=1+ ):+ X_outer_train = X.iloc[train_index]+ X_outer_validation = X.iloc[validation_index]+ y_outer_train = y[train_index]+ y_outer_validation = y[validation_index]+ inner = stratified_splits(y_outer_train, inner_folds, seed=seed + fold)+ search = GridSearchCV(+ build_pipeline(+ model_name,+ numeric_columns,+ categorical_columns,+ seed=seed + fold,+ ),+ parameter_grid(model_name),+ cv=inner,+ error_score="raise",+ n_jobs=1,+ )+ search.fit(X_outer_train, y_outer_train)+ risk = search.predict(X_outer_validation)+ harrell = float(+ concordance_index_censored(+ y_outer_validation[_event_field(y_outer_validation)],+ y_outer_validation[_time_field(y_outer_validation)],+ risk,+ )[0]+ )+ outer_results.append(+ {+ "best_params": search.best_params_,+ "fold": fold,+ "harrell_c": harrell,+ "uno_c": uno_concordance(y_outer_train, y_outer_validation, risk),+ "validation_rows": int(len(validation_index)),+ }+ )++ final_inner = stratified_splits(y, inner_folds, seed=seed + 10_000)+ final_search = GridSearchCV(+ build_pipeline(+ model_name,+ numeric_columns,+ categorical_columns,+ seed=seed,+ ),+ parameter_grid(model_name),+ cv=final_inner,+ error_score="raise",+ n_jobs=1,+ )+ final_search.fit(X, y)+ return outer_results, {+ "best_estimator": final_search.best_estimator_,+ "best_params": final_search.best_params_,+ }+++def prediction_archive(+ estimator: Any,+ X_test: Any,+ y_train: Any,+ y_test: Any,+ risk: Any,+) -> dict[str, Any]:+ """Build metric-evaluator input without serializing the estimator."""++ import numpy as np++ train_time = y_train[_time_field(y_train)]+ test_time = y_test[_time_field(y_test)]+ lower = float(np.quantile(test_time, 0.2))+ upper = float(+ min(+ np.quantile(test_time, 0.8),+ np.nextafter(train_time.max(), -np.inf),+ )+ )+ if not lower < upper:+ raise CliError("cannot construct a train-supported prediction time grid")+ times = np.linspace(lower, upper, 8)+ arrays: dict[str, Any] = {+ "risk": np.asarray(risk, dtype=float),+ "test_event": y_test[_event_field(y_test)],+ "test_time": test_time,+ "times": times,+ "train_event": y_train[_event_field(y_train)],+ "train_time": train_time,+ }+ if hasattr(estimator, "predict_survival_function"):+ try:+ functions = estimator.predict_survival_function(X_test)+ arrays["survival"] = np.vstack([function(times) for function in functions])+ except (AttributeError, NotImplementedError, ValueError):+ pass+ return arrays+++def train_and_report(+ frame: Any,+ *,+ event_column: str,+ time_column: str,+ numeric_columns: list[str],+ categorical_columns: list[str],+ model_name: str,+ test_fraction: float,+ seed: int,+ tune: bool,+ outer_folds: int,+ inner_folds: int,+) -> tuple[Any, Any, Any, dict[str, Any]]:+ """Split first, then fit all learned preprocessing inside a pipeline."""++ try:+ import numpy as np+ from sklearn.model_selection import train_test_split+ from sksurv.metrics import concordance_index_censored+ except ImportError as exc:+ raise CliError("the pinned survival stack is required") from exc++ X, y, schema = resolve_schema(+ frame,+ event_column=event_column,+ time_column=time_column,+ numeric_columns=numeric_columns,+ categorical_columns=categorical_columns,+ )+ X_train, X_test, y_train, y_test = train_test_split(+ X,+ y,+ test_size=test_fraction,+ random_state=seed,+ stratify=y[event_column],+ )++ nested_results: list[dict[str, Any]] | None = None+ best_params: dict[str, Any] | None = None+ if tune:+ nested_results, tuned = nested_tune(+ X_train,+ y_train,+ model_name=model_name,+ numeric_columns=numeric_columns,+ categorical_columns=categorical_columns,+ outer_folds=outer_folds,+ inner_folds=inner_folds,+ seed=seed,+ )+ estimator = tuned["best_estimator"]+ best_params = tuned["best_params"]+ else:+ estimator = build_pipeline(+ model_name, numeric_columns, categorical_columns, seed=seed+ )+ estimator.fit(X_train, y_train)++ risk = estimator.predict(X_test)+ harrell = float(+ concordance_index_censored(y_test[event_column], y_test[time_column], risk)[0]+ )+ transformed_features = int(+ estimator.named_steps["preprocess"].get_feature_names_out().shape[0]+ )+ nested_harrell = (+ None+ if not nested_results+ else {+ "mean": float(np.mean([item["harrell_c"] for item in nested_results])),+ "standard_deviation": float(+ np.std([item["harrell_c"] for item in nested_results], ddof=1)+ )+ if len(nested_results) > 1+ else 0.0,+ }+ )+ report = {+ "data": {+ "censored_test": int((~y_test[event_column]).sum()),+ "censored_train": int((~y_train[event_column]).sum()),+ "events_test": int(y_test[event_column].sum()),+ "events_train": int(y_train[event_column].sum()),+ "rows_test": int(len(y_test)),+ "rows_train": int(len(y_train)),+ },+ "evaluation": {+ "harrell_c": harrell,+ "nested_cv_harrell_c": nested_harrell,+ "uno_c": uno_concordance(y_train, y_test, risk),+ },+ "leakage_controls": {+ "holdout_used_for_tuning": False,+ "preprocessing_fit_scope": "training folds only",+ "split_before_imputation_encoding_scaling": True,+ "tuning_evaluation": "nested_cv_on_training_split" if tune else "not_run",+ },+ "model": {+ "best_params": best_params,+ "family": model_name,+ "transformed_feature_count": transformed_features,+ },+ "package_versions": {+ "numpy": version("numpy"),+ "pandas": version("pandas"),+ "scikit-learn": version("scikit-learn"),+ "scikit-survival": version("scikit-survival"),+ },+ "schema": schema,+ "schema_version": "1.0",+ "seed": seed,+ "tuning": {+ "inner_folds": inner_folds if tune else None,+ "outer_folds": outer_folds if tune else None,+ "outer_results": nested_results,+ "performed": tune,+ },+ "warning": (+ "Predictive metrics do not establish calibration, transportability, "+ "causal effects, or clinical utility. This tool is not clinical advice."+ ),+ }+ return estimator, (X_train, y_train), (X_test, y_test, risk), report+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Train a leakage-safe Cox or ensemble example. Local CSV use requires "+ "an explicit feature schema; no network access is performed."+ )+ )+ parser.add_argument("--input", help="Optional bounded local .csv")+ parser.add_argument("--event-column", default="event")+ parser.add_argument("--time-column", default="time")+ parser.add_argument("--numeric-columns", help="Comma-separated numeric features")+ parser.add_argument(+ "--categorical-columns", help="Comma-separated categorical features"+ )+ parser.add_argument("--model", choices=MODEL_CHOICES, default="coxph")+ parser.add_argument("--test-fraction", type=probability, default=0.25)+ parser.add_argument("--seed", type=bounded_int(0, 2**32 - 1), default=DEFAULT_SEED)+ parser.add_argument(+ "--synthetic-rows",+ type=bounded_int(80, 20_000),+ default=240,+ )+ parser.add_argument(+ "--tune",+ action="store_true",+ help="Run bounded nested CV on training data before final holdout evaluation",+ )+ parser.add_argument("--outer-folds", type=bounded_int(2, 5), default=3)+ parser.add_argument("--inner-folds", type=bounded_int(2, 5), default=3)+ parser.add_argument(+ "--prediction-output",+ help="Optional local .npz for evaluate_survival_metrics.py",+ )+ parser.add_argument("--output", help="Optional local .json summary")+ parser.add_argument("--force", action="store_true")+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ args = parser.parse_args(argv)+ try:+ if args.input:+ frame, source_name = read_csv(args.input)+ numeric = parse_names(args.numeric_columns)+ categorical = parse_names(args.categorical_columns)+ source = {"kind": "local_csv", "name": source_name}+ else:+ frame, default_numeric, default_categorical = synthetic_survival_frame(+ rows=args.synthetic_rows, seed=args.seed+ )+ numeric = parse_names(args.numeric_columns) or default_numeric+ categorical = parse_names(args.categorical_columns) or default_categorical+ source = {+ "kind": "synthetic",+ "network_used": False,+ "seed": args.seed,+ }++ estimator, train_data, test_data, report = train_and_report(+ frame,+ event_column=args.event_column,+ time_column=args.time_column,+ numeric_columns=numeric,+ categorical_columns=categorical,+ model_name=args.model,+ test_fraction=args.test_fraction,+ seed=args.seed,+ tune=args.tune,+ outer_folds=args.outer_folds,+ inner_folds=args.inner_folds,+ )+ report["source"] = source+ if args.prediction_output:+ X_train, y_train = train_data+ X_test, y_test, risk = test_data+ del X_train+ atomic_save_npz(+ prediction_archive(estimator, X_test, y_train, y_test, risk),+ args.prediction_output,+ force=args.force,+ )+ report["prediction_output"] = args.prediction_output+ emit_json(report, output=args.output, force=args.force)+ except CliError as exc:+ parser.error(str(exc))+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/scikit-survival/scripts/validate_survival_csv.py b/skills/scikit-survival/scripts/validate_survival_csv.pynew file mode 100644index 00000000..1ef072bb--- /dev/null+++ b/skills/scikit-survival/scripts/validate_survival_csv.py@@ -0,0 +1,172 @@+#!/usr/bin/env python3+"""Validate a local survival CSV and optionally write a structured NumPy array."""++from __future__ import annotations++import argparse+from typing import Any++from _common import (+ DEFAULT_SEED,+ MAX_FEATURES,+ CliError,+ atomic_save_npy,+ bounded_int,+ emit_json,+ parse_names,+ read_csv,+ structured_survival,+ synthetic_survival_frame,+)+++def validate_and_convert(+ frame: Any,+ *,+ event_column: str,+ time_column: str,+ feature_columns: list[str] | None = None,+) -> tuple[Any, dict[str, Any]]:+ """Validate outcome/schema columns and build a two-field survival array."""++ if event_column == time_column:+ raise CliError("event and time columns must differ")+ missing_outcomes = [+ name for name in (event_column, time_column) if name not in frame.columns+ ]+ if missing_outcomes:+ raise CliError(f"missing outcome columns: {', '.join(missing_outcomes)}")++ if feature_columns is None:+ features = [+ str(name)+ for name in frame.columns+ if name not in {event_column, time_column}+ ]+ else:+ features = list(feature_columns)+ if len(features) > MAX_FEATURES:+ raise CliError(f"at most {MAX_FEATURES} feature columns are allowed")+ if event_column in features or time_column in features:+ raise CliError("outcome columns must not be included as features")+ missing_features = [name for name in features if name not in frame.columns]+ if missing_features:+ raise CliError(f"missing feature columns: {', '.join(missing_features)}")++ outcome = structured_survival(+ frame[event_column],+ frame[time_column],+ event_name=event_column,+ time_name=time_column,+ )+ event = outcome[event_column]+ time = outcome[time_column]+ feature_dtypes = {name: str(frame[name].dtype) for name in features}+ report = {+ "censoring": {+ "censored_count": int((~event).sum()),+ "censored_fraction": float((~event).mean()),+ "event_count": int(event.sum()),+ "event_fraction": float(event.mean()),+ "type": "right",+ },+ "columns": {+ "event": event_column,+ "features": features,+ "feature_dtypes": feature_dtypes,+ "time": time_column,+ },+ "rows": int(len(frame)),+ "schema_version": "1.0",+ "structured_dtype": [+ [name, outcome.dtype.fields[name][0].str] for name in outcome.dtype.names+ ],+ "time": {+ "maximum": float(time.max()),+ "median": float(__import__("numpy").median(time)),+ "minimum": float(time.min()),+ "strictly_positive": True,+ },+ "validation": {+ "binary_event": True,+ "finite_time": True,+ "outcome_excluded_from_features": True,+ "valid": True,+ },+ }+ return outcome, report+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Validate right-censored survival outcomes in a bounded local CSV. "+ "With no --input, use deterministic synthetic data."+ )+ )+ parser.add_argument(+ "--input", help="Local .csv input; URLs and symlinks are rejected"+ )+ parser.add_argument("--event-column", default="event")+ parser.add_argument("--time-column", default="time")+ parser.add_argument(+ "--feature-columns",+ help="Optional explicit comma-separated features; defaults to other columns",+ )+ parser.add_argument(+ "--synthetic-rows",+ type=bounded_int(40, 20_000),+ default=240,+ help="Synthetic row count when --input is omitted (default: 240)",+ )+ parser.add_argument("--seed", type=bounded_int(0, 2**32 - 1), default=DEFAULT_SEED)+ parser.add_argument(+ "--structured-output",+ help="Optional .npy output containing the structured survival array",+ )+ parser.add_argument("--output", help="Optional .json validation report")+ parser.add_argument("--force", action="store_true", help="Replace explicit outputs")+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ args = parser.parse_args(argv)+ try:+ if args.input:+ frame, source_name = read_csv(args.input)+ source = {"kind": "local_csv", "name": source_name}+ defaults: list[str] | None = None+ else:+ frame, numeric, categorical = synthetic_survival_frame(+ rows=args.synthetic_rows, seed=args.seed+ )+ source = {+ "kind": "synthetic",+ "network_used": False,+ "seed": args.seed,+ }+ defaults = numeric + categorical++ requested_features = parse_names(args.feature_columns)+ feature_columns = requested_features or defaults+ outcome, report = validate_and_convert(+ frame,+ event_column=args.event_column,+ time_column=args.time_column,+ feature_columns=feature_columns,+ )+ report["source"] = source+ report["structured_output"] = (+ None if args.structured_output is None else args.structured_output+ )+ if args.structured_output:+ atomic_save_npy(outcome, args.structured_output, force=args.force)+ emit_json(report, output=args.output, force=args.force)+ except CliError as exc:+ parser.error(str(exc))+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/scikit-survival/tests/test_scripts.py b/skills/scikit-survival/tests/test_scripts.pynew file mode 100644index 00000000..382bd94a--- /dev/null+++ b/skills/scikit-survival/tests/test_scripts.py@@ -0,0 +1,254 @@+#!/usr/bin/env python3+"""Synthetic, network-free tests for the scikit-survival helper CLIs."""++from __future__ import annotations++import ast+import json+import stat+import sys+import tempfile+import unittest+from pathlib import Path+++SKILL_ROOT = Path(__file__).resolve().parents[1]+SCRIPTS = SKILL_ROOT / "scripts"+sys.path.insert(0, str(SCRIPTS))++import _common # noqa: E402+import competing_risk_cif # noqa: E402+import evaluate_survival_metrics # noqa: E402+import model_report # noqa: E402+import train_survival_model # noqa: E402+import validate_survival_csv # noqa: E402+++class CommonSafetyTests(unittest.TestCase):+ def test_json_output_is_private_and_refuses_overwrite(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ output = Path(directory) / "summary.json"+ _common.emit_json({"ok": True}, output=output)+ self.assertEqual(json.loads(output.read_text()), {"ok": True})+ self.assertEqual(stat.S_IMODE(output.stat().st_mode), 0o600)+ with self.assertRaises(_common.CliError):+ _common.emit_json({"ok": False}, output=output)++ def test_local_paths_reject_urls_and_symlinks(self) -> None:+ with self.assertRaises(_common.CliError):+ _common.checked_input_file(+ "https://example.invalid/data.csv", suffixes={".csv"}+ )+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory)+ real = root / "data.csv"+ real.write_text("event,time\n1,2\n", encoding="utf-8")+ link = root / "link.csv"+ try:+ link.symlink_to(real)+ except OSError:+ self.skipTest("symlinks unavailable")+ with self.assertRaises(_common.CliError):+ _common.checked_input_file(link, suffixes={".csv"})++ def test_no_shadow_modules_network_or_environment_reads(self) -> None:+ filenames = {path.name for path in SCRIPTS.glob("*.py")}+ self.assertNotIn("sklearn.py", filenames)+ self.assertNotIn("sksurv.py", filenames)+ self.assertNotIn("numpy.py", filenames)+ self.assertNotIn("pandas.py", filenames)+ banned_imports = {"aiohttp", "httpx", "requests", "socket", "urllib"}+ for path in SCRIPTS.glob("*.py"):+ tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))+ for node in ast.walk(tree):+ if isinstance(node, ast.Import):+ roots = {alias.name.split(".", 1)[0] for alias in node.names}+ self.assertFalse(roots & banned_imports, path.name)+ if isinstance(node, ast.ImportFrom) and node.module:+ self.assertNotIn(+ node.module.split(".", 1)[0], banned_imports, path.name+ )+ if isinstance(node, ast.Attribute):+ self.assertFalse(+ isinstance(node.value, ast.Name)+ and node.value.id == "os"+ and node.attr in {"environ", "getenv"},+ path.name,+ )++ def test_help_is_available_without_running_workflows(self) -> None:+ modules = (+ validate_survival_csv,+ train_survival_model,+ evaluate_survival_metrics,+ competing_risk_cif,+ model_report,+ )+ for module in modules:+ with self.subTest(module=module.__name__):+ help_text = module.build_parser().format_help()+ self.assertIn("usage:", help_text)+ self.assertIn("--help", help_text)+++class ValidationTests(unittest.TestCase):+ def test_synthetic_csv_schema_converts_to_structured_array(self) -> None:+ frame, numeric, categorical = _common.synthetic_survival_frame(rows=120)+ outcome, report = validate_survival_csv.validate_and_convert(+ frame,+ event_column="event",+ time_column="time",+ feature_columns=numeric + categorical,+ )+ self.assertEqual(outcome.dtype.names, ("event", "time"))+ self.assertEqual(len(outcome), 120)+ self.assertTrue(report["validation"]["valid"])++ def test_outcome_feature_leakage_is_rejected(self) -> None:+ frame, _, _ = _common.synthetic_survival_frame(rows=80)+ with self.assertRaises(_common.CliError):+ validate_survival_csv.validate_and_convert(+ frame,+ event_column="event",+ time_column="time",+ feature_columns=["event", "x_linear"],+ )++ def test_structured_output_disables_pickle(self) -> None:+ import numpy as np++ frame, _, _ = _common.synthetic_survival_frame(rows=80)+ outcome, _ = validate_survival_csv.validate_and_convert(+ frame,+ event_column="event",+ time_column="time",+ )+ with tempfile.TemporaryDirectory() as directory:+ output = Path(directory) / "outcome.npy"+ _common.atomic_save_npy(outcome, output)+ restored = np.load(output, allow_pickle=False)+ self.assertEqual(restored.dtype.names, ("event", "time"))+++class TrainingTests(unittest.TestCase):+ def test_cox_pipeline_splits_before_learned_preprocessing(self) -> None:+ frame, numeric, categorical = _common.synthetic_survival_frame(rows=140)+ estimator, train_data, test_data, report = (+ train_survival_model.train_and_report(+ frame,+ event_column="event",+ time_column="time",+ numeric_columns=numeric,+ categorical_columns=categorical,+ model_name="coxph",+ test_fraction=0.25,+ seed=_common.DEFAULT_SEED,+ tune=False,+ outer_folds=2,+ inner_folds=2,+ )+ )+ self.assertTrue(+ report["leakage_controls"]["split_before_imputation_encoding_scaling"]+ )+ self.assertEqual(report["data"]["rows_train"], 105)+ self.assertEqual(report["data"]["rows_test"], 35)+ self.assertEqual(+ estimator.named_steps["model"].__class__.__name__, "CoxPHSurvivalAnalysis"+ )+ self.assertEqual(len(train_data[1]), 105)+ self.assertEqual(len(test_data[1]), 35)++ def test_nested_tuning_runs_inside_training_data(self) -> None:+ frame, numeric, categorical = _common.synthetic_survival_frame(rows=120)+ _, _, _, report = train_survival_model.train_and_report(+ frame,+ event_column="event",+ time_column="time",+ numeric_columns=numeric,+ categorical_columns=categorical,+ model_name="coxph",+ test_fraction=0.25,+ seed=17,+ tune=True,+ outer_folds=2,+ inner_folds=2,+ )+ self.assertTrue(report["tuning"]["performed"])+ self.assertEqual(len(report["tuning"]["outer_results"]), 2)+ self.assertFalse(report["leakage_controls"]["holdout_used_for_tuning"])++ def test_explicit_schema_is_required(self) -> None:+ frame, _, _ = _common.synthetic_survival_frame(rows=80)+ with self.assertRaises(_common.CliError):+ train_survival_model.resolve_schema(+ frame,+ event_column="event",+ time_column="time",+ numeric_columns=[],+ categorical_columns=[],+ )+++class MetricTests(unittest.TestCase):+ def test_synthetic_metrics_cover_risk_and_probability_inputs(self) -> None:+ report = evaluate_survival_metrics.evaluate(+ evaluate_survival_metrics.synthetic_metric_inputs()+ )+ metrics = report["metrics"]+ self.assertIsInstance(metrics["harrell_c"], float)+ self.assertIsInstance(metrics["uno_c"], float)+ self.assertIsInstance(metrics["integrated_brier_score"], float)+ self.assertEqual(len(metrics["cumulative_dynamic_auc"]["values"]), 8)++ def test_risk_and_survival_shapes_are_distinct(self) -> None:+ arrays = evaluate_survival_metrics.synthetic_metric_inputs()+ arrays["survival"] = arrays["risk"]+ with self.assertRaises(_common.CliError):+ evaluate_survival_metrics.validate_predictions(arrays)++ def test_time_grid_outside_training_support_is_rejected(self) -> None:+ arrays = evaluate_survival_metrics.synthetic_metric_inputs()+ arrays["test_time"] = arrays["test_time"].copy()+ arrays["test_time"][0] = arrays["train_time"].max() + 1+ with self.assertRaises(_common.CliError):+ evaluate_survival_metrics.validate_predictions(arrays)+++class CompetingRiskTests(unittest.TestCase):+ def test_cif_shapes_and_sum_identity(self) -> None:+ import numpy as np++ frame, _ = competing_risk_cif.synthetic_competing_risks(rows=160)+ summary, arrays = competing_risk_cif.estimate_cif(+ frame["status"],+ frame["time"],+ confidence=True,+ )+ cif = arrays["cumulative_incidence"]+ self.assertEqual(summary["n_causes"], 3)+ self.assertEqual(cif.shape[0], 4)+ self.assertTrue(np.allclose(cif[0], cif[1:].sum(axis=0)))+ self.assertEqual(arrays["confidence_interval"].shape[:2], (4, 2))++ def test_noncontiguous_cause_codes_are_rejected(self) -> None:+ with self.assertRaises(_common.CliError):+ competing_risk_cif.normalize_competing_event([0, 1, 3, 1, 3])+++class ReportTests(unittest.TestCase):+ def test_report_distinguishes_metrics_and_disclaims_utility(self) -> None:+ report = model_report.render_report(+ model_report.synthetic_training_summary(),+ model_report.synthetic_metric_summary(),+ competing_risks="present",+ title="Synthetic survival report",+ )+ self.assertIn("rank discrimination", report)+ self.assertIn("probability prediction error", report)+ self.assertIn("cause-specific CIF", report)+ self.assertIn("not clinical advice", report)+++if __name__ == "__main__":+ unittest.main()