-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEval_Methods.py
More file actions
172 lines (135 loc) · 5.77 KB
/
Eval_Methods.py
File metadata and controls
172 lines (135 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
171
172
import os
import warnings
import numpy as np
import pandas as pd
from scipy.stats import chi2
from lifelines import CoxPHFitter
from typing import List, Tuple, Union
from pycox.evaluation import EvalSurv
from sksurv.metrics import concordance_index_censored
Numeric = Union[float, int]
NumericArrayLike = Union[List[Numeric], Tuple[Numeric], np.array]
warnings.filterwarnings('ignore')
ResPath = './SP_Results'
savePath = './SP_Results'
all_methods = ["Baseline", "DSCC", "CC", "CIMLR", "SNF", "LRACluster", "IntNMF", "ANF", "NEMO", "MOVICS", "MRGCN", "hMKL", "MDICC", "DLSF", "DSIR"]
#### DEFINE FUNCTIONS
## function to find indices for specific times
def find_idx(times, all_times):
idx_vec = []
for time in times:
temp_times = np.abs(all_times - time)
idx = np.where(temp_times == np.min(temp_times))[0]
idx_vec.append(idx[0])
return idx_vec
## calculate the survival probability using hazard ratio
def cal_survprob(pred_train, pred_val):
pred_train['log_HR'] = pred_train['predTrain'].apply(np.log)
cph = CoxPHFitter()
cph.fit(pred_train.loc[:, ['log_HR', 'time', 'status']], duration_col='time', event_col='status')
baseline_cum_hazard = cph.baseline_cumulative_hazard_
all_times = baseline_cum_hazard.index.values
max_time = np.max(all_times)
target_times = np.linspace(0, max_time, 20).tolist()
baseline_haz_indices = find_idx(target_times, all_times)
baseline_haz = baseline_cum_hazard.iloc[baseline_haz_indices, 0].values
HR_val = pred_val['predVal'].to_numpy()
CHF_val = HR_val[:, np.newaxis] * baseline_haz
survprob_val = np.exp(-CHF_val)
survprob_val = survprob_val.T
survprob_val = pd.DataFrame(survprob_val)
survprob_val.index = target_times
return survprob_val
## calculate risk score using survival probability
def cal_ci(predRes):
risk_score = -np.log(predRes)
risk_score = np.sum(risk_score, axis=1)
risk_score = risk_score / np.max(risk_score)
return risk_score
def to_array(array_like: NumericArrayLike, to_boolean: bool = False) -> np.array:
array = np.asarray(array_like)
shape = np.shape(array)
if len(shape) > 1:
raise ValueError(
f"Input should be a 1-d array. Got a shape of {shape} instead."
)
if np.any(array < 0):
raise ValueError("All event times must be greater than or equal to zero.")
if to_boolean:
check_indicators(array)
return array.astype(bool)
return array
# get the survival probability for each patient at the time they died or were censored
def get_1surv_prob(pred_df, survival_times):
time_points = pred_df.index.values
probabilities = np.zeros(len(survival_times))
for i, time in enumerate(survival_times):
distances = np.abs(time_points - time)
min_distance = np.min(distances)
candidate_indices = np.where(distances == min_distance)[0]
if len(candidate_indices) == 1:
selected_idx = candidate_indices[0]
else:
candidate_probs = pred_df.iloc[candidate_indices, i]
min_prob = candidate_probs.min()
min_prob_indices = candidate_indices[candidate_probs == min_prob]
selected_idx = min_prob_indices[0]
probabilities[i] = pred_df.iloc[selected_idx, i]
return probabilities
## function to create table for each metric
def create_metric_table(data, metric, datasets):
metric_data = {method: values[metric] for method, values in data.items()}
df = pd.DataFrame(metric_data, index=[f'Run {i + 1}' for i in range(len(datasets))])
df.index = datasets
return df
## create summary table for C-Index
def create_summary_table(cindex_table):
summary_data = {
'Cindex': cindex_table.mean(),
}
summary_df = pd.DataFrame(summary_data)
return summary_df
#### CALCULATE METRIC FOR ALL METHODS
allRes = {}
for method in all_methods:
all_datasets = os.listdir(os.path.join(ResPath, method))
# all_datasets = [x for x in all_datasets if "TCGA-" in x]
all_datasets = sorted(all_datasets) # Returns new sorted list
allCIndex = []
alltdCIndex = []
allCal = []
allIBS = []
for dataset in all_datasets:
NewPath = os.path.join(ResPath, method, dataset)
CIndex = []
tdCIndex = []
IBS = []
Cal = []
for time in range(1, 6):
for fold in range(1, 6):
# TrainRes = pd.read_csv(os.path.join(ResPath, method, dataset, "Time" + str(time)) + "/Train_Res_" + str(fold) + ".csv",
# header=0, index_col=0)
ValRes = pd.read_csv(
os.path.join(ResPath, method, dataset, "Time" + str(time)) + "/Val_Res_" + str(fold) + ".csv",
header=0,
index_col=0)
if len(ValRes.columns) <= 3:
tmp_CI = np.NaN
else:
trueDat = np.array(ValRes[['time', 'status']])
predVal = ValRes.drop(['time', 'status'], axis=1)
try:
riskScore = cal_ci(predVal.to_numpy())
tmp_CI = concordance_index_censored(event_indicator=ValRes['status'].values.astype(bool),
event_time=ValRes['time'].values,
estimate=riskScore)[0]
except:
tmp_CI = np.NaN
CIndex.append(tmp_CI)
CIndex = np.nanmean(CIndex)
allCIndex.append(CIndex)
# allCIndex = np.nanmean(allCIndex)
allRes[method] = {'CIndex': allCIndex}
CIndex_Table = create_metric_table(allRes, 'CIndex', all_datasets)
CIndex_Table.to_csv(savePath + "/CIndex_Table.csv", sep=",", header=True)
print(CIndex_Table)