Synav commited on
Commit
3e4e0a3
·
verified ·
1 Parent(s): 808cca5

Update src/cox_train_once.py

Browse files
Files changed (1) hide show
  1. src/cox_train_once.py +22 -0
src/cox_train_once.py CHANGED
@@ -11,6 +11,7 @@ from lifelines.exceptions import ConvergenceError
11
 
12
  from src.survival_utils import prepare_cox_df, fit_cox
13
  from src.cox_persist import save_cox_artifacts
 
14
 
15
 
16
  def _default_cox_dir() -> Path:
@@ -155,6 +156,24 @@ def train_and_save_cox(
155
  except Exception as e:
156
  raise RuntimeError(f"Cox fit error: {e}")
157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  # Resolve output directory
159
  out_dir = Path(save_dir) if save_dir is not None else _default_cox_dir()
160
  out_dir.mkdir(parents=True, exist_ok=True)
@@ -178,6 +197,9 @@ def train_and_save_cox(
178
  "n_events": int(df_cox["Event_clean"].sum()) if "Event_clean" in df_cox.columns else None,
179
  "duration_col": "OS_time_days",
180
  "event_col": "Event_clean",
 
 
 
181
  },
182
  )
183
 
 
11
 
12
  from src.survival_utils import prepare_cox_df, fit_cox
13
  from src.cox_persist import save_cox_artifacts
14
+ from src.calibration_utils import bootstrap_c_index_ci
15
 
16
 
17
  def _default_cox_dir() -> Path:
 
156
  except Exception as e:
157
  raise RuntimeError(f"Cox fit error: {e}")
158
 
159
+ # ------------------------------------------------------------------
160
+ # Bootstrap 95% CI for the training-cohort C-index.
161
+ # Risk scores come from the fitted Cox model's partial hazard prediction.
162
+ # ------------------------------------------------------------------
163
+ try:
164
+ partial_hazard = cph.predict_partial_hazard(df_cox).values.ravel()
165
+ c_point, c_lo, c_hi = bootstrap_c_index_ci(
166
+ durations=df_cox["OS_time_days"].values,
167
+ events=df_cox["Event_clean"].values,
168
+ risk_scores=partial_hazard,
169
+ n_bootstraps=1000,
170
+ seed=42,
171
+ )
172
+ except Exception:
173
+ c_point, c_lo, c_hi = (float(cph.concordance_index_), float("nan"), float("nan"))
174
+
175
+
176
+
177
  # Resolve output directory
178
  out_dir = Path(save_dir) if save_dir is not None else _default_cox_dir()
179
  out_dir.mkdir(parents=True, exist_ok=True)
 
197
  "n_events": int(df_cox["Event_clean"].sum()) if "Event_clean" in df_cox.columns else None,
198
  "duration_col": "OS_time_days",
199
  "event_col": "Event_clean",
200
+ "c_index": float(c_point) if c_point is not None else None,
201
+ "c_index_ci_low": float(c_lo) if c_lo is not None else None,
202
+ "c_index_ci_high": float(c_hi) if c_hi is not None else None,
203
  },
204
  )
205