mcpbeat Sign in

Bio Machine Learning Survival Analysis Agent Skill

Analyzes time-to-event data using Kaplan-Meier curves, log-rank tests, and Cox proportional hazards regression with lifelines. Builds survival models from clinical and omics features. Use when predicting patient survival or modeling time-to-event outcomes.

1.7k tokens
context cost
SKILL.md, read when the skill is used
4
files
ships runnable scripts
0
copies elsewhere
how many repositories repackaged it
132
stars on the repo
on the repository, not the skill itself

Install

one command, takes just this skill from the repository
npx skills add https://github.com/BioTender-max/awesome-bio-agent-skills --skill bio-machine-learning-survival-analysis

What comes with it

6 452 bytes besides the instruction
examples/cox_regression.py
examples/kaplan_meier.py
usage-guide.md

The instruction itself

11 sections, as written by the author

Version Compatibility

Reference examples tested with: matplotlib 3.8+, pandas 2.2+

Before using code patterns, verify installed versions match. If versions differ:

  • Python: pip show <package> then help(module.function) to check signatures

If code throws ImportError, AttributeError, or TypeError, introspect the installed

package and adapt the example to match the actual API rather than retrying.

Survival Prediction with lifelines

"Analyze patient survival data" → Estimate survival curves (Kaplan-Meier), compare groups (log-rank test), and model time-to-event outcomes with Cox proportional hazards regression.

  • Python: lifelines.KaplanMeierFitter(), lifelines.CoxPHFitter()

Kaplan-Meier Curves

Goal: Estimate and visualize the survival probability function from time-to-event data.

Approach: Fit a nonparametric Kaplan-Meier estimator to censored survival data and plot the step function.

from lifelines import KaplanMeierFitter
import matplotlib.pyplot as plt

kmf = KaplanMeierFitter()

# T: time to event or censoring
# E: event indicator (1=event occurred, 0=censored)
kmf.fit(T, event_observed=E)

# Plot survival curve
kmf.plot_survival_function()
plt.xlabel('Time (months)')
plt.ylabel('Survival probability')
plt.savefig('km_curve.png', dpi=150)

Compare Groups with Log-Rank Test

Goal: Test whether survival distributions differ significantly between risk groups.

Approach: Fit separate Kaplan-Meier curves per group, overlay them, and apply a log-rank test for statistical comparison.

from lifelines import KaplanMeierFitter
from lifelines.statistics import logrank_test
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(8, 6))

for group, color in zip(['high', 'low'], ['red', 'blue']):
    mask = df['risk_group'] == group
    kmf = KaplanMeierFitter()
    kmf.fit(df.loc[mask, 'time'], event_observed=df.loc[mask, 'event'], label=group)
    kmf.plot_survival_function(ax=ax, color=color)

# Log-rank test
high = df[df['risk_group'] == 'high']
low = df[df['risk_group'] == 'low']
results = logrank_test(high['time'], low['time'], event_observed_A=high['event'], event_observed_B=low['event'])
print(f'Log-rank p-value: {results.p_value:.4e}')

ax.set_xlabel('Time (months)')
ax.set_ylabel('Survival probability')
ax.set_title(f'Log-rank p = {results.p_value:.4e}')
plt.savefig('km_comparison.png', dpi=150)

Cox Proportional Hazards Regression

Goal: Model the effect of covariates on survival time using a semi-parametric hazard model.

Approach: Fit a Cox PH model to extract hazard ratios, confidence intervals, and a concordance index for predictive accuracy.

from lifelines import CoxPHFitter

# Prepare data: must have 'time' and 'event' columns
# Include covariates as additional columns
cph = CoxPHFitter()
cph.fit(df, duration_col='time', event_col='event')

# Summary with hazard ratios
cph.print_summary()

# Get hazard ratios as DataFrame
hr = cph.summary[['exp(coef)', 'exp(coef) lower 95%', 'exp(coef) upper 95%', 'p']]
print(hr)

# Concordance index (c-index): 0.5=random, 1.0=perfect
print(f'C-index: {cph.concordance_index_:.3f}')

Multivariate Cox Model

Goal: Assess the independent prognostic value of clinical and molecular features in a single model.

Approach: Combine clinical covariates and gene expression values into a regularized Cox model to identify independently prognostic variables.

from lifelines import CoxPHFitter
import pandas as pd

# Combine clinical and omics features
cox_df = pd.DataFrame({
    'time': meta['survival_months'],
    'event': meta['vital_status'],
    'age': meta['age'],
    'stage': meta['stage_numeric'],
    'GENE1': expr.loc['GENE1'],
    'GENE2': expr.loc['GENE2']
})

cph = CoxPHFitter(penalizer=0.1)  # L2 regularization for stability
cph.fit(cox_df, duration_col='time', event_col='event')
cph.print_summary()

Predict Risk Scores

Goal: Stratify patients into risk groups based on a fitted Cox model.

Approach: Compute partial hazard scores from model coefficients and split at the median to define high/low risk groups for downstream KM visualization.

# Partial hazard (risk score)
risk_scores = cph.predict_partial_hazard(cox_df)

# Median risk split for KM plot
df['risk_group'] = (risk_scores > risk_scores.median()).map({True: 'high', False: 'low'})

Check Proportional Hazards Assumption

Goal: Verify that the proportional hazards assumption holds for all covariates.

Approach: Run the built-in Schoenfeld residual tests and inspect diagnostic plots for time-varying effects.

# Test PH assumption
cph.check_assumptions(df, p_value_threshold=0.05, show_plots=True)

Survival at Specific Time

Goal: Extract survival probability estimates at clinically meaningful time points.

Approach: Query the fitted Kaplan-Meier survival function at specific durations and report the median survival time.

# Survival probability at specific times
survival_probs = kmf.survival_function_at_times([12, 24, 60])
print(survival_probs)

# Median survival
print(f'Median survival: {kmf.median_survival_time_:.1f}')

Feature Selection for Survival

Goal: Screen thousands of genes to identify those significantly associated with patient survival.

Approach: Fit univariate Cox models for each gene, extract hazard ratios and p-values, and rank candidates for multivariate modeling.

from lifelines import CoxPHFitter
import pandas as pd

# Univariate screening
results = []
for gene in expr.index[:1000]:
    cox_df = pd.DataFrame({
        'time': meta['survival_months'],
        'event': meta['vital_status'],
        'gene': expr.loc[gene]
    })
    cph = CoxPHFitter()
    cph.fit(cox_df, duration_col='time', event_col='event')
    results.append({
        'gene': gene,
        'hr': cph.hazard_ratios_['gene'],
        'p': cph.summary.loc['gene', 'p']
    })

results_df = pd.DataFrame(results)
sig_genes = results_df[results_df['p'] < 0.05].sort_values('p')
  • clinical-databases/variant-prioritization - Clinical variant interpretation
  • differential-expression/de-results - Find DE genes for survival model
  • machine-learning/biomarker-discovery - Select predictive features

Other skills for the same job

different authors, same section of the catalogue
Webapp Testing
by anthropics
vendor ×15

Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.

978 tokens scripts
Finishing A Development Branch
by ZhanlinCui
×9

Use when implementation is complete, all tests pass, and you need to decide how to integrate the work - guides completion of development work by presenting structured options for merge, PR, or cleanup

1.1k tokens
Test Driven Development
by w95
×9

Use when implementing any feature or bugfix, before writing implementation code

2.5k tokens
Systematic Debugging
by ratacat
×9

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes

2.5k tokens scripts
Verification Before Completion
by ZhanlinCui
×8

Use when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always

1.1k tokens
Backtest Expert
by BaggaT236
×3

Expert guidance for systematic backtesting of trading strategies. Use when developing, testing, stress-testing, or validating quantitative trading strategies. Covers "beating ideas to death" methodology, parameter robustness testing, slippage modeling, bias prevention, and interpreting backtest results. Applicable when user asks about backtesting, strategy validation, robustness testing, avoiding overfitting, or systematic trading development.

2.2k tokens scripts
Adaptyv
by christophacham
×3

Cloud laboratory platform for automated protein testing and validation. Use when designing proteins and needing experimental validation including binding assays, expression testing, thermostability measurements, enzyme activity assays, or protein sequence optimization. Also use for submitting experiments via API, tracking experiment status, downloading results, optimizing protein sequences for better expression using computational tools (NetSolP, SoluProt, SolubleMPNN, ESM), or managing protein design workflows with wet-lab validation.

1.1k tokens
Aeon
by christophacham
×3

This skill should be used for time series machine learning tasks including classification, regression, clustering, forecasting, anomaly detection, segmentation, and similarity search. Use when working with temporal data, sequential patterns, or time-indexed observations requiring specialized algorithms beyond standard ML approaches. Particularly suited for univariate and multivariate time series analysis with scikit-learn compatible APIs.

2.9k tokens

How to use it

Copy the folder

Take biotender-max/bio-machine-learning-survival-analysis from the repository into ~/.claude/skills for personal use, or into .claude/skills inside a project.

Check the name does not clash

The agent identifies a skill by the name field in its header. Two skills with the same name cannot sit side by side — one of them will be ignored.