Python API Reference¶
This section documents the Python API for CRISP-NAM.
Model¶
crisp_nam.models.crisp_nam_model
¶
CrispNamModel for competing-risks survival analysis.
PyTorch implementation of CrispNamModel for competing risks survival analysis with L2 normalized projection weights.
CrispNamModel
¶
Bases: Module
Competing risks CoxNAM with L2 normalized projection weights.
Each feature contributes to each risk through a separate shape function. All projection weights are constrained to unit L2 norm.
Source code in crisp_nam/models/crisp_nam_model.py
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 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 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 376 377 378 379 380 381 382 383 384 | |
__init__
¶
__init__(num_features, num_competing_risks, hidden_sizes=(64, 64), dropout_rate=0.1, feature_dropout=0.1, batch_norm=False, normalize_projections=True, eps=1e-08)
Initialize the CrispNamModel.
Source code in crisp_nam/models/crisp_nam_model.py
forward
¶
Forward pass to compute risk scores for all competing risks.
Returns:
| Type | Description |
|---|---|
risk_scores: List of (batch_size, 1) Tensors
|
|
Source code in crisp_nam/models/crisp_nam_model.py
get_projection_norms
¶
Get the L2 norms of all projection weights (should be ~1.0 if normalized).
Returns:
| Type | Description |
|---|---|
Dictionary of weight norms by feature and risk
|
|
Source code in crisp_nam/models/crisp_nam_model.py
get_normalized_projection_weights
¶
Get the actual L2 normalized weights used in computation.
Returns:
| Type | Description |
|---|---|
Dictionary of normalized weights
|
|
Source code in crisp_nam/models/crisp_nam_model.py
calculate_feature_importance
¶
Calculate feature importance based on the magnitude of risk-specific projection outputs.
With L2 normalized weights, this gives a fair comparison across features.
Returns:
| Type | Description |
|---|---|
Dictionary of feature importances by risk type
|
|
Source code in crisp_nam/models/crisp_nam_model.py
analyze_projection_weights
¶
Analyze the L2 norms and statistics of projection weights.
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in crisp_nam/models/crisp_nam_model.py
crisp_nam.models.deephit_model
¶
PyTorch implementation of DeepHit for competing risks survival analysis.
DeepHit
¶
Bases: Module
PyTorch implementation of DeepHit for competing risks survival analysis.
Source code in crisp_nam/models/deephit_model.py
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 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 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 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 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 | |
forward
¶
Forward pass through the network.
Returns:
| Type | Description |
|---|---|
risk_scores: List of (batch_size, 1) Tensors
|
|
Source code in crisp_nam/models/deephit_model.py
compute_loss
¶
Compute total loss.
Returns:
| Type | Description |
|---|---|
total_loss: Torch.tensor
|
|
Source code in crisp_nam/models/deephit_model.py
predict
¶
Predict risk scores for input x.
Returns:
| Type | Description |
|---|---|
out: Tensor of shape (batch_size, num_Event, num_Category)
|
|
Source code in crisp_nam/models/deephit_model.py
## Metrics
crisp_nam.metrics.calibration
¶
Calibration metrics for time-to-event models with competing risks.
This module contains functions to compute the Brier score and integrated Brier score for competing risks.
estimate_ipcw
¶
Estimate the inverse probability of censoring weights (IPCW) using a Kaplan-Meier estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
km
|
tuple or KaplanMeierFitter
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
kmf |
KaplanMeierFitter
|
A KaplanMeierFitter instance fitted to the provided data or the input instance if already fitted. |
Source code in crisp_nam/metrics/ipcw.py
brier_score
¶
Compute the corrected Brier score for a given competing risk.
This implementation is based on the work of Schoop et al. on quantifying the predictive accuracy of time-to-event models in the presence of competing risks.
Returns:
| Type | Description |
|---|---|
brier (float): The corrected Brier score evaluated at time t.
|
km (object): Updated Kaplan–Meier estimator (if applicable). |
Source code in crisp_nam/metrics/calibration.py
integrated_brier_score
¶
integrated_brier_score(e_test, t_test, risk_predicted_test, times, t_eval=None, km=None, primary_risk=1)
Compute the integrated Brier score for competing risks over a range of time points.
The integrated Brier score is computed by numerically integrating the Brier score over the evaluation times.
Returns:
| Type | Description |
|---|---|
ibs (float): Integrated Brier score.
|
km (object): Updated Kaplan–Meier estimator. |
Source code in crisp_nam/metrics/calibration.py
crisp_nam.metrics.discrimination
¶
Discrimination metrics for time-to-event models with competing risks.
This module contains functions to compute the cumulative and single time-dependent AUC and time-dependent C-index for evaluating competing risks.
estimate_ipcw
¶
Estimate the inverse probability of censoring weights (IPCW) using a Kaplan-Meier estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
km
|
tuple or KaplanMeierFitter
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
kmf |
KaplanMeierFitter
|
A KaplanMeierFitter instance fitted to the provided data or the input instance if already fitted. |
Source code in crisp_nam/metrics/ipcw.py
auc_td
¶
Compute the time-dependent AUC for a given competing risk using predicted CIFs.
Returns:
| Type | Description |
|---|---|
auc_value : float
|
km : Updated Kaplan-Meier estimator |
Source code in crisp_nam/metrics/discrimination.py
cumulative_dynamic_auc
¶
cumulative_dynamic_auc(e_test, t_test, risk_predicted_test, times, t_eval=None, km=None, primary_risk=1)
Compute the cumulative dynamic AUC by numerically integrating the time-dependent AUC over a range of time points.
Returns:
| Type | Description |
|---|---|
auc_integral: float
|
km: object Updated Kaplan-Meier estimator. |
Source code in crisp_nam/metrics/discrimination.py
truncated_concordance_td
¶
truncated_concordance_td(e_test, t_test, risk_predicted_test, times, t, km=None, primary_risk=1, tied_tol=1e-08)
Compute the truncated time-dependent concordance index (C-index).
Returns:
| Type | Description |
|---|---|
c_index : float
|
km : Updated km object |
Source code in crisp_nam/metrics/discrimination.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 207 208 209 210 211 212 213 214 | |
crisp_nam.metrics.ipcw
¶
IPCW estimation for time-to-event models with competing risks.
This module provides a function to estimate the inverse probability of censoring weights (IPCW) using a Kaplan-Meier estimator.
estimate_ipcw
¶
Estimate the inverse probability of censoring weights (IPCW) using a Kaplan-Meier estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
km
|
tuple or KaplanMeierFitter
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
kmf |
KaplanMeierFitter
|
A KaplanMeierFitter instance fitted to the provided data or the input instance if already fitted. |
Source code in crisp_nam/metrics/ipcw.py
## Utilities
crisp_nam.utils.plotting
¶
Utility functions for plotting.
This module provides functions to visualize feature importance and shape functions for both crisp-nam and deephit models.
plot_feature_importance
¶
plot_feature_importance(model, x_data, feature_names=None, n_top=5, n_bottom=5, risk_idx=1, figsize=(8, 6), output_file='', color_positive='#2196F3', color_negative='#F44336')
Plot feature importance with both top positive and negative influences, handling both CPU and CUDA devices automatically.
Returns:
| Type | Description |
|---|---|
- fig: Matplotlib figure object
|
|
- ax: Matplotlib axes object
|
|
- top_pos: List of top positive feature names
|
|
- top_neg: List of top negative feature names
|
|
Source code in crisp_nam/utils/plotting.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 | |
plot_coxnam_shape_functions
¶
plot_coxnam_shape_functions(model, X, risk_to_plot=1, feature_names=None, top_features=None, ncols=3, figsize=(12, 8), output_file='')
Plot shape functions for each feature in a CoxNAM model, automatically handling CPU vs CUDA inputs.
Returns:
| Type | Description |
|---|---|
- fig: Matplotlib figure object
|
|
- axes: List of Matplotlib axes objects for each plotted feature
|
|
Source code in crisp_nam/utils/plotting.py
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 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 207 208 209 210 211 212 213 214 215 216 217 | |
crisp_nam.utils.risk_cif
¶
Risk functions for evaluation.
This module provides functions to compute cumulative incidence functions (CIFs) and risk scores for competing risk models.
compute_baseline_cif
¶
Compute baseline cumulative incidence function for a specific event type.
Args: times: Numpy array of event times events: Numpy array of event indicators (0=censored, 1...K=event types) eval_times: Times at which to evaluate the CIF event_type: Event type to compute CIF for (1...K)
Returns:
| Type | Description |
|---|---|
Numpy array of baseline CIF values at eval_times
|
|
Source code in crisp_nam/utils/risk_cif.py
predict_cif
¶
Predict cumulative incidence function for a specific competing risk.
Returns:
| Type | Description |
|---|---|
cif_pred: Array of shape (n_samples, len(times)) — predicted CIF per sample.
|
|
Source code in crisp_nam/utils/risk_cif.py
predict_risk
¶
Predicts relative risk scores for each competing risk.
Args: model : Trained model. x_input (np.ndarray or torch.Tensor): Input features of shape (n_samples, n_features). device (str): Device to run the computation on.
Returns:
| Type | Description |
|---|---|
np.ndarray: Array of shape (n_samples, num_risks) with relative risk scores.
|
|
Source code in crisp_nam/utils/risk_cif.py
predict_absolute_risk
¶
Predict absolute risk (CIF) for each competing event by given time points.
Returns:
| Type | Description |
|---|---|
np.ndarray: Shape (n_samples, num_events, n_times) with predicted CIFs.
|
|
Source code in crisp_nam/utils/risk_cif.py
crisp_nam.utils.loss
¶
Loss functions for competing risks.
This module implements weighted and un-weighted negative log-likelihood loss, L2 penalty loss functions.
weighted_negative_log_likelihood_loss
¶
weighted_negative_log_likelihood_loss(risk_scores, times, events, num_competing_risks, event_weights=None, sample_weights=None, eps=1e-08)
Compute the weighted negative log-likelihood loss for competing risks Cox model.
Returns:
| Type | Description |
|---|---|
Weighted negative log partial likelihood loss
|
|
Source code in crisp_nam/utils/loss.py
negative_log_likelihood_loss
¶
Compute the negative log-likelihood loss for competing risks Cox model.
Returns:
| Type | Description |
|---|---|
Negative log partial likelihood loss
|
|
Source code in crisp_nam/utils/loss.py
compute_l2_penalty
¶
Compute L2 regularization penalty on model parameters.
Returns:
| Type | Description |
|---|---|
L2 penalty term
|
|