-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhacking.py
297 lines (215 loc) · 6.4 KB
/
hacking.py
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
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
# %% [markdown]
# ## testing models import
# %% get models
import torch
import pysurvival as psurv
from pysurvival.models.semi_parametric import NonLinearCoxPHModel
from pysurvival.utils.metrics import concordance_index
import pandas as pd
import numpy as np
import pickle as pi
import matplotlib.pyplot as plt
import plotly.graph_objects as go
#%% load dependencies
from pysurvival.utils import load_model
sampleInput = [1, 67, int(0), int(2), 1, 1, 0, 1, 0, 2, 2]
poorSample = [1, 74, 2, 3, 1, 1, 1, 9, 15, 5, 10]
#models yet to be loaded
#pfsMod = load_model('modelData/PFS_best.zip')
#rfsMod = load_model('modelData/RFS_best.zip')
#pfsTrain = pd.read_csv('../data/training_pfs.csv')
#pfsVal = pd.read_csv('../data/validation_pfs.csv')
#rfsTrain = pd.read_csv('../data/training_rfs.csv')
#rfsVal = pd.read_csv('../data/validation_rfs.csv')
#summ, iterer = pi.load(open('../data/gridSearch.p', 'rb'))
#%% define useful functions
# event_col is the header in the df that represents the 'Event / Status' indicator
# time_col is the header in the df that represents the event time
def dataframe_to_deepsurv_ds(df, event_col = 'Event', time_col = 'Time'):
# Extract the event and time columns as numpy arrays
e = df[event_col].values.astype(np.int32)
t = df[time_col].values.astype(np.float32)
# Extract the patient's covariates as a numpy array
x_df = df.drop([event_col, time_col], axis = 1)
x = x_df.values.astype(np.float32)
# Return the deep surv dataframe
return {
'x' : x,
'e' : e,
't' : t
}
# %% [markdown]
# ## Get prediction from the models
# %%
#predict sample PFS for our patient
haz = pfsMod.predict_hazard(sampleInput)
plt.plot(pfsMod.times, haz[0])
risk = pfsMod.predict_risk(sampleInput)
surv = pfsMod.predict_survival(poorSample)
plt.plot(pfsMod.times, surv[0])
# %%
def generateHighRes(risks, times, res = 0.001, maxTim = 5) :
hrTimes = []
hrProb = []
tmpRisk = 1
iteratorInd = 0
for i in range(0, int(maxTim / res)) :
tmpI = i * res
hrTimes.append(tmpI)
hrProb.append(tmpRisk)
if iteratorInd == len(risks) - 1 :
continue
if times[iteratorInd + 1] <= tmpI :
iteratorInd += 1
tmpRisk = risks[iteratorInd]
return [hrTimes, hrProb]
# %%
tst = generateHighRes(surv[0], pfsMod.times)
plt.plot(tst[0], tst[1])
# %%
pfsSet = generateHighRes(pfsMod.predict_survival(poorSample)[0], pfsMod.times)
rfsSet = generateHighRes(rfsMod.predict_survival(poorSample)[0], rfsMod.times)
#%% Create a plotly chart
fig = go.Figure(
data = [
go.Scatter(
x = pfsSet[0],
y = pfsSet[1],
name = 'PFS'
),
go.Scatter(
x = rfsSet[0],
y = rfsSet[1],
name = 'RFS'
)
],
layout = go.Layout(
template = 'simple_white',
xaxis = dict(
title = dict(
text = 'Survival time [years]'
),
range = [0, 5]
),
yaxis = dict(
title = dict(
text = 'Survival probability'
),
range = [0, 1]
),
hovermode = 'x unified'
)
)
fig.show()
# %% [markdown]
# ## Develop functions for calculating scores
# %% get the data
ds = pd.read_csv('../data/analysisData/combined.csv')
#add required 'prior reccurence column
ds['PriorRecurrence'] = 0
#%%
def calculateEORTC_orig(tumN, diam, recRate, category, conCis, grade) :
rec = 0
prog = 0
#number of tumors
prog += (tumN >=2) * 3
rec += ((tumN >= 2) * 3) + ((tumN >= 8) * 3)
#tumor diameter
prog += (diam >= 3) * 3
rec += (diam >= 3) * 3
#reccurence rate
prog += (recRate != 0) * 2
rec += ((recRate != 0) + (recRate > 1)) * 2
# category; assuming Ta = 0 and T1 = 1
prog += category * 4
rec += category
# concurrent cis
prog += conCis * 6
rec += conCis
# grade
prog += (grade == 3) * 5
rec += grade - 1
return [rec, prog]
def calculateEORTC(tumN, diam, category, conCis, grade) :
rec = 0
prog = 0
#number of tumors
prog += (tumN * 3)
rec += (tumN * 3)
#tumor diameter
prog += (diam * 3)
rec += (diam * 3)
# category; assuming Ta = 0 and T1 = 1
prog += category * 4
rec += category
# concurrent cis
prog += conCis * 6
rec += conCis
# grade
prog += (grade == 3) * 5
rec += grade - 1
return [rec, prog]
# %% test created eortc formula
for i in ds.index :
row = ds.loc[i, :]
rec, prog = calculateEORTC(row['No_tumors'], row['Diameter'], row['T'], row['CIS'], row['Grading'])
if rec != row['EORTC_R'] or prog != row['EORTC_P'] :
print('wa')
print('Rec: {} vs {}'.format(rec, row['EORTC_R']))
print('Prog: {} vs {}'.format(prog, row['EORTC_P']))
# %% define CUETO score calculator
# reverse - engineered - no official documentation found
def calculateCUETO_orig(gender, age, tumN, stage, conCis, grade) :
rec = 0
prog = 0
#gender
rec += (gender == 2) * 3
prog += 0
#age
rec += int(age >= 60) + int(age > 70)
prog += (age > 70) * 2
#number of tumors
rec += (tumN > 3) * 2
prog += (tumN > 3)
#stage
rec += 0
prog += stage * 2
#concurrent cis
rec += conCis * 2
prog += conCis * 2
#grade
rec += (grade == 2) + (grade == 3) * 3
prog += (grade == 2) * 2 + (grade == 3) * 6
return [rec, prog]
def calculateCUETO(gender, age, tumN, stage, conCis, grade) :
rec = 0
prog = 0
#gender
rec += (gender * 3)
prog += 0
#age
rec += int(age >= 60) + int(age > 70)
prog += (age > 70) * 2
#number of tumors
rec += tumN * 2
prog += int(tumN)
#stage
rec += 0
prog += stage * 2
#concurrent cis
rec += conCis * 2
prog += conCis
#grade
rec += (grade == 2) + (grade == 3) * 3
prog += (grade == 2) * 2 + (grade == 3) * 6
return [rec, prog]
# %% test created cueto formula
for i in ds.index :
row = ds.loc[i, :]
rec, prog = calculateCUETO(row['Gender'], row['Age'], row['No_tumors'], row['T'] , row['CIS'], row['Grading'])
if rec != row['CUETO_R'] or prog != row['CUETO_P'] :
print('wa')
print('Rec: {} vs {}'.format(rec, row['CUETO_R']))
print('Prog: {} vs {}'.format(prog, row['CUETO_P']))
print(i)
# %%