-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpredict_analysis_more_kegg_tfs_average_whole_new_rand.py
172 lines (160 loc) · 6.54 KB
/
predict_analysis_more_kegg_tfs_average_whole_new_rand.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
from sklearn.metrics import precision_recall_curve
import os
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from sklearn import metrics
from scipy import interp
import seaborn as sns
import pandas as pd
sns.set_style("whitegrid")
data_augmentation = False
# num_predictions = 20
batch_size = 256
num_classes = 3
epochs = 200
data_augmentation = False
# num_predictions = 20
model_name = 'keras_cnn_trained_model_shallow.h5'
# The data, shuffled and split between train and test sets:
current_path = os.path.abspath('.')
save_dir = os.path.join(os.getcwd(),'_Ycv_LR_as_nega_rg_5-7_lr_1-6_e100')
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
# plt.grid()
mean_fpr = np.linspace(0, 1, 100)
y_testy = np.empty([0,1])
y_predicty = np.empty([0,1])
#count_setx = pd.read_table('/home/yey3/nn_project2/data/human_brain/pathways/kegg/unique_rand_labelx_num.txt',header=None)
#count_set = [i[0] for i in np.array(count_setx)]
count_set = [0]
for test_indel in range(1,11): ################## three fold cross validation ## for KEGG and Reactiome 3 fold CV #for KEGG and Reactiome 3 fold CV
X_data_test = np.load(current_path+'/rand_1_10fold/'+str(test_indel)+'_test_X_data_array.npy')
Y_data_test = np.load(current_path+'/rand_1_10fold/'+str(test_indel)+'_test_Y_data_array.npy')
#gene_pair_index_test = np.load('/home/yey3/spatial_nn/processed_data/new_split/rand_1_10fold/'+str(test_indel)+'_test_gene_pair_list_array.npy')
count_setz = np.load(current_path+'/rand_1_10fold/'+str(test_indel)+'_test_gene_pair_index_array.npy')
#(x_train, y_train,count_set_train) = load_data_TF2(train_TF,data_train)
y_predictyz = np.load(current_path+'/'+str(test_indel)+'_Ycv_LR_as_nega_rg_5-7_lr_1-6_e100' + '/end_y_predict.npy')
y_testyz = np.load(current_path+'/'+str(test_indel)+'_Ycv_LR_as_nega_rg_5-7_lr_1-6_e100' + '/end_y_test.npy')
y_testy = np.concatenate((y_testy,y_testyz),axis = 0)
y_predicty = np.concatenate((y_predicty, y_predictyz), axis=0)
count_set = count_set + [i + count_set[-1] if len(count_set)>0 else i for i in count_setz[1:]]
############
print (len(count_set))
###############whole performance
#y_test = y_testy
#y_predict = y_predicty
AUC_set =[]
s = open(save_dir+'/whole_RPKM_AUCs1+2.txt','w')
tprs = []
mean_fpr = np.linspace(0, 1, 100)
#######################################
##################################
fig = plt.figure(figsize=(5, 5))
plt.plot([0, 1], [0, 1])
total_pair = 0
total_auc = 0
print (y_predicty.shape)
############
for jj in range(len(count_set)-1):#len(count_set)-1):
if count_set[jj] < count_set[jj+1]:
print (test_indel,jj,count_set[jj],count_set[jj+1])
current_pair = count_set[jj+1] - count_set[jj]
total_pair = total_pair + current_pair
y_test = y_testy[count_set[jj]:count_set[jj+1]]
y_predict = y_predicty[count_set[jj]:count_set[jj+1],0]
# Score trained model.
fpr, tpr, thresholds = metrics.roc_curve(y_test, y_predict, pos_label=1)
tprs.append(interp(mean_fpr, fpr, tpr))
tprs[-1][0] = 0.0
# Print ROC curve
plt.plot(fpr, tpr, color='0.5', lw=0.001,alpha=.2)
auc = np.trapz(tpr, fpr)
s.write(str(jj)+'\t'+str(count_set[jj])+'\t'+str(count_set[jj+1])+'\t'+str(auc) + '\n')
print('AUC:', auc)
AUC_set.append(auc)
total_auc = total_auc + auc * current_pair
mean_tpr = np.median(tprs, axis=0)
mean_tpr[-1] = 1.0
per_tpr = np.percentile(tprs,[40,50,60],axis=0)
mean_auc = np.trapz(mean_tpr,mean_fpr)
plt.plot(mean_fpr, mean_tpr,'k',lw=3,label = 'median ROC')
plt.title("{:.4f}".format(mean_auc),fontsize=15)
plt.fill_between(mean_fpr, per_tpr[0,:], per_tpr[2,:], color='g', alpha=.2,label='quantile')
plt.plot(mean_fpr, per_tpr[0,:],'g',lw=3,alpha=.2)
plt.legend(loc='lower right',fontsize=15)
plt.ylim([0, 1])
plt.xlim([0, 1])
plt.grid()
plt.xlabel('FP', fontsize=15)
plt.ylabel('TP', fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.savefig(save_dir+'/whole_kegg_ROCs1+2_percentile.pdf')
del fig
fig = plt.figure(figsize=(3, 3))
plt.hist(AUC_set,bins = 50)
plt.savefig(save_dir + '/whole_kegg_ROCs1+2_hist.pdf')
del fig
s.close()
fig = plt.figure(figsize=(3, 3))
plt.boxplot(AUC_set)
plt.savefig(save_dir + '/whole_kegg_ROCs1+2_box.pdf')
del fig
############################
###################################################################### PR
##################################
AUC_set =[]
s = open(save_dir+'/whole_RPKM_AUCs1+2_PR.txt','w')
tprs = []
mean_fpr = np.linspace(0, 1, 100)
fig = plt.figure(figsize=(5, 5))
total_pair = 0
total_auc = 0
print (y_predicty.shape)
############
for jj in range(len(count_set)-1):#len(count_set)-1):
if count_set[jj] < count_set[jj+1]:
print (test_indel,jj,count_set[jj],count_set[jj+1])
current_pair = count_set[jj+1] - count_set[jj]
total_pair = total_pair + current_pair
y_test = y_testy[count_set[jj]:count_set[jj+1]]
y_predict = y_predicty[count_set[jj]:count_set[jj+1]]
# Score trained model.
tpr, fpr, thresholds = metrics.precision_recall_curve(y_test, y_predict) # , pos_label=1)
tpr = np.flip(tpr)
fpr = np.flip(fpr)
tprs.append(interp(mean_fpr, fpr, tpr))
plt.plot(fpr, tpr, color='0.5', lw=0.001,alpha=.2)
auc = np.trapz(tpr, fpr)
s.write(str(jj)+'\t'+str(count_set[jj])+'\t'+str(count_set[jj+1])+'\t'+str(auc) + '\n')
print('AUC:', auc)
AUC_set.append(auc)
total_auc = total_auc + auc * current_pair
mean_tpr = np.median(tprs, axis=0)
per_tpr = np.percentile(tprs,[40,50,60],axis=0)
mean_auc = np.trapz(mean_tpr,mean_fpr)
plt.plot(mean_fpr, mean_tpr,'k',lw=3,label = 'median ROC')
plt.title("{:.4f}".format(mean_auc),fontsize=15)
plt.fill_between(mean_fpr, per_tpr[0,:], per_tpr[2,:], color='g', alpha=.2,label='quantile')
plt.plot(mean_fpr, per_tpr[0,:],'g',lw=3,alpha=.2)
plt.legend(loc='lower right',fontsize=15)
plt.ylim([0, 1])
plt.xlim([0, 1])
plt.grid()
plt.xlabel('FP', fontsize=15)
plt.ylabel('TP', fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.savefig(save_dir +'/whole_kegg_ROCs1+2_percentile_PR.pdf')
del fig
fig = plt.figure(figsize=(3, 3))
plt.hist(AUC_set,bins = 50)
plt.savefig(save_dir +'/whole_kegg_ROCs1+2_hist_PR.pdf')
del fig
s.close()
fig = plt.figure(figsize=(3, 3))
plt.boxplot(AUC_set)
plt.savefig(save_dir +'/whole_kegg_ROCs1+2_box_PR.pdf')
del fig