/
support.py
221 lines (187 loc) · 7.36 KB
/
support.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
from matplotlib import pyplot as plt
from lifelines.utils import concordance_index as ci
from lifelines.statistics import logrank_test
def mkdir(path):
import os
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print(path + 'Folder create successfully !')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print(path + ' Folader is exist')
return False
def cal_pval(time, pred):
event = np.zeros_like(time)
event[time > 0] = 1
pred_median = np.median(pred)
risk_group = np.zeros_like(pred)
risk_group[pred > pred_median] = 1
group_lowrisk_time = time[risk_group==0].copy()
group_highrisk_time = time[risk_group==1].copy()
group_lowrisk_event = event[risk_group==0].copy()
group_highrisk_event = event[risk_group==1].copy()
results = logrank_test(group_lowrisk_time, group_highrisk_time, event_observed_A=group_lowrisk_event , event_observed_B=group_highrisk_event)
# results.print_summary()
return results.p_value
def sort_data(X,Y):
T = - np.abs(np.squeeze(np.array(Y)))
sorted_idx = np.argsort(T)
return sorted_idx, X[sorted_idx], Y[sorted_idx]
def get_omic_data(fea_filename, seed, nfold = 5, fold_num =0):
X, Y, H = load_omic_data(fea_filename)
train_X, train_Y, test_X, test_Y = split_data(spilt_seed=seed, fea=X, label=Y, nfold=nfold, fold_num=fold_num)
return train_X, train_Y, test_X, test_Y
def label_extra(data, t_col="Time", e_col="Event"):
X = data[[c for c in data.columns if c not in [t_col, e_col]]]
Y = data[[t for t in data.columns if t in[t_col]]]
Y.loc[data[e_col]==0] = -Y.loc[data[e_col]==0]
return X.values,Y.values
def read_data(filename):
train_data = pd.read_csv(filename + "train.csv")
test_data = pd.read_csv(filename + "test.csv")
train_X,train_Y = label_extra(train_data)
test_X, test_Y = label_extra(test_data)
return train_X, train_Y, test_X, test_Y
def load_omic_data(fea_filename):
data_fea = pd.read_csv(fea_filename)
headers = data_fea.columns.values.tolist()
headers = headers[1:]
headers = np.array(headers)
time = data_fea.iloc[0,:].tolist()[1:]
time = np.array(time)
status = data_fea.iloc[1, :].tolist()[1:]
status = np.array(status)
data_fea = data_fea[2:] ##delete label
for i in range(len(time)):
if status[i] == 0:
time[i] = -time[i]
data_fea = data_fea.drop('GeneSymbol', axis=1)
data_fea = data_fea.values
data_fea = np.transpose(data_fea)
data_time = time.reshape(-1, 1)
print(len(data_fea))
print(len(data_time))
return data_fea, data_time, headers
def plot_curve(curve_data, title="train epoch-Cindex curve", x_label="epoch", y_label="Cindex"):
plt.figure()
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.plot(curve_data[0], curve_data[1], color='black', markerfacecolor='black', marker='o', markersize=1)
plt.show()
###print sorted survival time:
def split_data(spilt_seed, fea, label, nfold = 5, fold_num = 0):
kf = KFold(n_splits=nfold, shuffle=True, random_state=spilt_seed)
###对齐输入
label_flat = label.flatten()
censor_index = np.where(label_flat < 0)
no_cen_index = np.where(label_flat >= 0)
censor = label[[tuple(censor_index)]]
censor_fea = fea[[tuple(censor_index)]]
censor = censor[0]
censor_fea = censor_fea[0]
no_cen = label[[tuple(no_cen_index)]]
nocen_fea = fea[[tuple(no_cen_index)]]
no_cen = no_cen[0]
nocen_fea = nocen_fea[0]
num = 0
for train_index, test_index in kf.split(censor_fea):
train_X1 = censor_fea[train_index]
train_Y1 = censor[train_index]
test_X1 = censor_fea[test_index]
test_Y1 = censor[test_index]
if num == fold_num:
break
num += 1
num = 0
for train_index, test_index in kf.split(nocen_fea):
train_X2 = nocen_fea[train_index]
train_Y2 = no_cen[train_index]
test_X2 = nocen_fea[test_index]
test_Y2 = no_cen[test_index]
if num == fold_num:
break
num += 1
train_X = np.vstack((train_X1, train_X2))
train_Y = np.vstack((train_Y1, train_Y2))
test_X = np.vstack((test_X1, test_X2))
test_Y = np.vstack((test_Y1, test_Y2))
return train_X, train_Y, test_X, test_Y
def split_data_with_headers(spilt_seed, fea, label,headers, nfold = 5, fold_num = 0):
kf = KFold(n_splits=nfold, shuffle=True, random_state=spilt_seed)
###对齐输入
label_flat = label.flatten()
censor_index = np.where(label_flat < 0)
no_cen_index = np.where(label_flat >= 0)
censor = label[[tuple(censor_index)]]
censor_fea = fea[[tuple(censor_index)]]
censor_headers = headers[[tuple(censor_index)]]
censor = censor[0]
censor_fea = censor_fea[0]
censor_headers = censor_headers[0]
censor_headers = censor_headers.reshape(((len(censor_headers), 1))) # (3,1)
no_cen = label[[tuple(no_cen_index)]]
nocen_fea = fea[[tuple(no_cen_index)]]
no_cen_headers = headers[[tuple(no_cen_index)]]
no_cen = no_cen[0]
nocen_fea = nocen_fea[0]
no_cen_headers = no_cen_headers[0]
no_cen_headers = no_cen_headers.reshape(((len(no_cen_headers), 1))) # (3,1)
num = 0
for train_index, test_index in kf.split(censor_fea):
train_X1 = censor_fea[train_index]
train_Y1 = censor[train_index]
train_headers1 = censor_headers[train_index]
test_X1 = censor_fea[test_index]
test_Y1 = censor[test_index]
test_headers1 = censor_headers[test_index]
if num == fold_num:
break
num +=1
num = 0
for train_index, test_index in kf.split(nocen_fea):
train_X2 = nocen_fea[train_index]
train_Y2 = no_cen[train_index]
train_headers2 = no_cen_headers[train_index]
test_X2 = nocen_fea[test_index]
test_Y2 = no_cen[test_index]
test_headers2 = no_cen_headers[test_index]
if num == fold_num:
break
num +=1
train_X = np.vstack((train_X1, train_X2))
train_Y = np.vstack((train_Y1, train_Y2))
train_headers = np.vstack((train_headers1,train_headers2))
test_X = np.vstack((test_X1, test_X2))
test_Y = np.vstack((test_Y1, test_Y2))
test_headers = np.vstack((test_headers1,test_headers2))
return train_X, train_Y, test_X, test_Y,train_headers, test_headers
# def split_data(spilt_seed, fea, label,headers, nfold=5, fold_num=0):
# kf = KFold(n_splits=nfold, shuffle=True, random_state=spilt_seed)
# train_X = []
# train_Y = []
# test_X = []
# test_Y = []
# num = 0
# for train_index, test_index in kf.split(fea):
# train_X = fea[train_index]
# train_Y = label[train_index]
# train_headers = headers[train_index]
# test_X = fea[test_index]
# test_Y = label[test_index]
# test_headers=headers[test_index]
# if num == fold_num:
# print(num)
# break
# num += 1
# train_X, train_Y, train_headers = sort_surv_data(train_X, train_Y, train_headers)
# test_X, test_Y, test_headers= sort_surv_data(test_X, test_Y, test_headers)
# return train_X, train_Y, test_X, test_Y,train_headers, test_headers