Analysis¶
Summaries, quality checks and plots for a generated dataset.
Task-oriented walkthroughs: Summarising a dataset and Plotting.
Summaries¶
summary
¶
Utilities for summarizing and validating survival datasets.
This module provides functions to summarize survival data, check data quality, and identify potential issues.
summarize_survival_dataset
¶
summarize_survival_dataset(
data: DataFrame,
time_col: str = "time",
status_col: str = "status",
id_col: str | None = None,
covariate_cols: list[str] | None = None,
verbose: bool = True,
) -> dict[str, Any]
Generate a comprehensive summary of a survival dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame containing survival data. |
required |
time_col
|
str
|
Name of the column containing time-to-event values. |
"time"
|
status_col
|
str
|
Name of the column containing event indicators (1=event, 0=censored). |
"status"
|
id_col
|
str
|
Name of the column containing subject identifiers. |
None
|
covariate_cols
|
list of str
|
List of column names to include as covariates in the summary. If None, all columns except time_col, status_col, and id_col are considered. |
None
|
verbose
|
bool
|
Whether to print the summary to console. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing all summary statistics. |
Examples:
>>> from gen_surv import generate
>>> from gen_surv.summary import summarize_survival_dataset
>>>
>>> # Generate example data
>>> df = generate(model="cphm", n=100, model_cens="uniform",
... cens_par=1.0, beta=0.5, covariate_range=2.0)
>>>
>>> # Summarize the dataset
>>> summary = summarize_survival_dataset(df)
Source code in gen_surv/summary.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
check_survival_data_quality
¶
check_survival_data_quality(
data: DataFrame,
time_col: str = "time",
status_col: str = "status",
id_col: str | None = None,
min_time: float = 0.0,
max_time: float | None = None,
status_values: list[int] | None = None,
fix_issues: bool = False,
) -> tuple[DataFrame, dict[str, Any]]
Check for common issues in survival data and optionally fix them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame containing survival data. |
required |
time_col
|
str
|
Name of the column containing time-to-event values. |
"time"
|
status_col
|
str
|
Name of the column containing event indicators. |
"status"
|
id_col
|
str
|
Name of the column containing subject identifiers. |
None
|
min_time
|
float
|
Minimum acceptable value for time column. |
0.0
|
max_time
|
float
|
Maximum acceptable value for time column. |
None
|
status_values
|
list of int
|
List of valid status values. Default is [0, 1]. |
None
|
fix_issues
|
bool
|
Whether to attempt fixing issues (returns a modified DataFrame). |
False
|
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, dict[str, Any]]
|
Tuple containing (possibly fixed) DataFrame and issues report. |
Examples:
>>> from gen_surv import generate
>>> from gen_surv.summary import check_survival_data_quality
>>>
>>> # Generate example data with some issues
>>> df = generate(model="cphm", n=100, model_cens="uniform",
... cens_par=1.0, beta=0.5, covariate_range=2.0)
>>> # Introduce some issues
>>> df.loc[0, "time"] = np.nan
>>> df.loc[1, "status"] = 2 # Invalid status
>>>
>>> # Check and fix issues
>>> fixed_df, issues = check_survival_data_quality(df, fix_issues=True)
>>> print(issues)
Source code in gen_surv/summary.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
compare_survival_datasets
¶
compare_survival_datasets(
datasets: dict[str, DataFrame],
time_col: str = "time",
status_col: str = "status",
covariate_cols: list[str] | None = None,
) -> DataFrame
Compare multiple survival datasets and summarize their differences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasets
|
dict[str, DataFrame]
|
Dictionary mapping dataset names to DataFrames. |
required |
time_col
|
str
|
Name of the time column in each dataset. |
"time"
|
status_col
|
str
|
Name of the status column in each dataset. |
"status"
|
covariate_cols
|
List[str]
|
List of covariate columns to compare. If None, compares all common columns. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Comparison table with datasets as columns and metrics as rows. |
Examples:
>>> from gen_surv import generate
>>> from gen_surv.summary import compare_survival_datasets
>>>
>>> # Generate datasets with different parameters
>>> datasets = {
... "CPHM": generate(model="cphm", n=100, model_cens="uniform",
... cens_par=1.0, beta=0.5, covariate_range=2.0),
... "Weibull AFT": generate(model="aft_weibull", n=100, beta=[0.5],
... shape=1.5, scale=1.0, model_cens="uniform", cens_par=1.0)
... }
>>>
>>> # Compare datasets
>>> comparison = compare_survival_datasets(datasets)
>>> print(comparison)
Source code in gen_surv/summary.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
Plots¶
visualization
¶
Visualization utilities for survival data.
This module provides functions to visualize survival data generated by gen_surv, including Kaplan-Meier survival curves and other commonly used plots in survival analysis.
plot_survival_curve
¶
plot_survival_curve(
data: DataFrame,
time_col: str = "time",
status_col: str = "status",
group_col: str | None = None,
confidence_intervals: bool = True,
title: str = "Kaplan-Meier Survival Curve",
figsize: tuple[float, float] = (10, 6),
ci_alpha: float = 0.2,
) -> tuple[Figure, Axes]
Plot Kaplan-Meier survival curves from simulated data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame containing the survival data. |
required |
time_col
|
str
|
Name of the column containing event/censoring times. |
"time"
|
status_col
|
str
|
Name of the column containing event indicators (1=event, 0=censored). |
"status"
|
group_col
|
str
|
Name of the column to use for stratification (creates separate curves). |
None
|
confidence_intervals
|
bool
|
Whether to display confidence intervals around the survival curves. |
True
|
title
|
str
|
Plot title. |
"Kaplan-Meier Survival Curve"
|
figsize
|
tuple
|
Figure size (width, height) in inches. |
(10, 6)
|
ci_alpha
|
float
|
Transparency level for confidence interval bands. |
0.2
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Matplotlib figure object. |
ax |
Axes
|
Matplotlib axes object. |
Examples:
>>> from gen_surv import generate
>>> from gen_surv.visualization import plot_survival_curve
>>>
>>> # Generate data
>>> df = generate(model="cphm", n=100, model_cens="uniform", cens_par=1.0, beta=0.5, covariate_range=2.0)
>>>
>>> # Create a categorical group based on covariate
>>> df["group"] = pd.cut(df["covariate"], bins=2, labels=["Low", "High"])
>>>
>>> # Plot survival curves by group
>>> fig, ax = plot_survival_curve(df, group_col="group")
>>> plt.show()
Source code in gen_surv/visualization.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
plot_hazard_comparison
¶
plot_hazard_comparison(
models: dict[str, DataFrame],
time_col: str = "time",
status_col: str = "status",
title: str = "Hazard Function Comparison",
figsize: tuple[float, float] = (10, 6),
bandwidth: float = 0.5,
) -> tuple[Figure, Axes]
Compare hazard functions from multiple generated datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
dict
|
Dictionary mapping model names to their respective DataFrames. |
required |
time_col
|
str
|
Name of the column containing event/censoring times. |
"time"
|
status_col
|
str
|
Name of the column containing event indicators (1=event, 0=censored). |
"status"
|
title
|
str
|
Plot title. |
"Hazard Function Comparison"
|
figsize
|
tuple
|
Figure size (width, height) in inches. |
(10, 6)
|
bandwidth
|
float
|
Bandwidth parameter for kernel density estimation of the hazard function. |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Matplotlib figure object. |
ax |
Axes
|
Matplotlib axes object. |
Examples:
>>> from gen_surv import generate
>>> from gen_surv.visualization import plot_hazard_comparison
>>>
>>> # Generate data from multiple models
>>> models = {
>>> "CPHM": generate(model="cphm", n=100, model_cens="uniform", cens_par=1.0, beta=0.5, covariate_range=2.0),
>>> "AFT Weibull": generate(model="aft_weibull", n=100, beta=[0.5], shape=1.5, scale=2.0,
>>> model_cens="uniform", cens_par=1.0)
>>> }
>>>
>>> # Compare hazard functions
>>> fig, ax = plot_hazard_comparison(models)
>>> plt.show()
Source code in gen_surv/visualization.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
plot_covariate_effect
¶
plot_covariate_effect(
data: DataFrame,
covariate_col: str,
time_col: str = "time",
status_col: str = "status",
n_groups: int = 3,
title: str = "Effect of Covariate on Survival",
figsize: tuple[float, float] = (10, 6),
ci_alpha: float = 0.2,
) -> tuple[Figure, Axes]
Visualize the effect of a continuous covariate on survival by discretizing it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame containing the survival data. |
required |
covariate_col
|
str
|
Name of the covariate column to visualize. |
required |
time_col
|
str
|
Name of the column containing event/censoring times. |
"time"
|
status_col
|
str
|
Name of the column containing event indicators (1=event, 0=censored). |
"status"
|
n_groups
|
int
|
Number of groups to divide the covariate into (e.g., 3 for tertiles). |
3
|
title
|
str
|
Plot title. |
"Effect of Covariate on Survival"
|
figsize
|
tuple
|
Figure size (width, height) in inches. |
(10, 6)
|
ci_alpha
|
float
|
Transparency level for confidence interval bands. |
0.2
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Matplotlib figure object. |
ax |
Axes
|
Matplotlib axes object. |
Examples:
>>> from gen_surv import generate
>>> from gen_surv.visualization import plot_covariate_effect
>>>
>>> # Generate data with a continuous covariate
>>> df = generate(model="cphm", n=200, model_cens="uniform", cens_par=1.0, beta=0.5, covariate_range=2.0)
>>>
>>> # Visualize the effect of the covariate on survival
>>> fig, ax = plot_covariate_effect(df, covariate_col="covariate", n_groups=3)
>>> plt.show()
Source code in gen_surv/visualization.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
describe_survival
¶
describe_survival(
data: DataFrame,
time_col: str = "time",
status_col: str = "status",
) -> DataFrame
Generate a summary of survival data including median survival time, event counts, and other descriptive statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame containing the survival data. |
required |
time_col
|
str
|
Name of the column containing event/censoring times. |
"time"
|
status_col
|
str
|
Name of the column containing event indicators (1=event, 0=censored). |
"status"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Summary statistics dataframe. |
Examples:
>>> from gen_surv import generate
>>> from gen_surv.visualization import describe_survival
>>>
>>> # Generate data
>>> df = generate(model="cphm", n=200, model_cens="uniform", cens_par=1.0, beta=0.5, covariate_range=2.0)
>>>
>>> # Get survival summary
>>> summary = describe_survival(df)
>>> print(summary)
Source code in gen_surv/visualization.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |