-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
243 lines (192 loc) · 9.36 KB
/
main.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
############################# IMPORT LIBRARY #################################
seed=75
import numpy as np
from tensorflow import set_random_seed
import pandas as pd
import os
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import interp
from itertools import cycle
from xgboost import XGBClassifier
from collections import Counter
from sklearn.metrics import average_precision_score, precision_recall_curve, matthews_corrcoef, precision_score, recall_score, confusion_matrix, classification_report
from sklearn.metrics import f1_score, roc_auc_score, auc, cohen_kappa_score, precision_recall_curve, log_loss, roc_curve, classification_report, accuracy_score
from sklearn.model_selection import GridSearchCV, train_test_split, cross_val_score, cross_val_predict, StratifiedKFold, StratifiedShuffleSplit
from sklearn.metrics.classification import accuracy_score
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from sklearn.ensemble import RandomTreesEmbedding, RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.neural_network import MLPClassifier, BernoulliRBM
from sklearn import model_selection
from sklearn.decomposition import PCA, KernelPCA, SparsePCA, MiniBatchSparsePCA, NMF, TruncatedSVD, FastICA, FactorAnalysis, LatentDirichletAllocation
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import make_classification
from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
from sklearn.preprocessing import Normalizer, MinMaxScaler, StandardScaler, RobustScaler, LabelEncoder, label_binarize, QuantileTransformer
from sklearn.manifold import TSNE
from sklearn.feature_selection import SelectFromModel, SelectKBest, chi2, RFE, RFECV
from sklearn.pipeline import make_pipeline
from imblearn.over_sampling import RandomOverSampler, ADASYN, SMOTE
from imblearn.combine import SMOTEENN, SMOTETomek
from keras.initializers import RandomNormal
from keras.wrappers.scikit_learn import KerasClassifier
from keras.layers import Input, Dense
from keras.models import Model, load_model
from keras_adversarial import AdversarialOptimizerSimultaneous, normal_latent_sampling
from variational_autoencoder import *
#from aae_architechture import *
from aae_single_layer import *
from deep_autoencoder import *
from denoising_autoencoder import *
from shallow_autoencoder import *
from weight_analysis import *
matplotlib.use('Agg')
np.random.seed(seed)
def zero_mix(x, n):
temp = np.copy(x)
noise=n
if 'spilt' in noise:
frac = float(noise.split('-')[1])
for i in temp:
n = np.random.choice(len(i), int(round(frac * len(i))), replace=False)
i[n] = 0
return (temp)
def gaussian_mix(x):
n = np.random.normal(0, 0.1, (len(x), len(x[0])))
return (x + n)
# The above two functions are used to add noise in the data
# And used to train denoising autoencoder
######################### LOAD BREAST CANCER DATA ####################
file_1 = pd.read_csv('./data/subtype_molecular_rna_seq.csv')
data = file_1.iloc[0:20439,2:607].values
X=data.T
file_2 = pd.read_csv('./data/subtype_molecular_rna_seq_label.csv', low_memory=False)
label= file_2.iloc[0,2:607].values
y=label.T
print('Actual dataset shape {}'.format(Counter(y)))
########################## LOAD UCEC DATA ###########################
'''
file_1 = pd.read_csv('./data/ucec_rna_seq.csv')
data = file_1.iloc[0:20482,2:232].values
X=data.T
file_2 = pd.read_csv('./data/ucec_rna_seq_label.csv', low_memory=False)
label = file_2.iloc[0,2:232].values #First row then column from dataset
y=label.T
print('Actual dataset shape {}'.format(Counter(y)))
'''
sm = SMOTE(sampling_strategy='auto', kind='borderline1', random_state=seed)
#sm = SMOTEENN(ratio=1, random_state=seed) #this is for version 0.3.0
X, y = sm.fit_sample(X, y)
#x_train, y_train = sm.fit_sample(x_train, y_train)
#x_test, y_test = sm.fit_sample(x_test, y_test)
print('Resampled dataset shape {}'.format(Counter(y)))
######################## FEATURE SCALING/NORMALIZATION ##################
from sklearn.preprocessing import QuantileTransformer
qt = QuantileTransformer(n_quantiles=10, random_state=seed)
qt.fit(X)
X=qt.transform(X)
#x_train=qt.transform(x_train)
#x_test=qt.transform(x_test)
############## HEAT MAP INPUT DATA #######################
'''
sns_plot=sns.heatmap(X, cmap="PiYG", cbar=True, xticklabels=False, yticklabels=False)
fig = sns_plot.get_figure()
fig.savefig("./figures/input_data.png", format='png', dpi=500)
'''
#################### Spilt the data for training and testing ################
#x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
x_train=X
y_train=y
#noise_factor = 0.2
#x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
#x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
#x_train_noisy=gaussian_mix(x_train)
#x_test_noisy=gaussian_mix(x_test)
# this is used for denoising autoencoder
###############################################################################
###############################DIMENSION REDUCTION ############################
###############################################################################
################ VARIOUS AUTOENCODERS ###############
######### Shallow Autoencoder ############
'''
shallow_autoencoder_fit(x_train, dimension=50, optimizer="adadelta",
loss_function="binary_crossentropy", nb_epoch=100,
batch_size=20, path='./feature_extraction/shallowAE/'+aaenum+'/')
'''
######### Denoising Autoencoder ############
'''
denoising_autoencoder_fit(x_train, x_train_noisy, dimension=50, optimizer="adadelta",
loss_function="binary_crossentropy", nb_epoch=100,
batch_size=20, path='./feature_extractiondenoisingAE/'+aaenum+'/')
'''
######### Deep Autoencoder ################
'''
deep_autoencoder_fit(x_train, dimension=50, optimizer="adadelta",
loss_function="binary_crossentropy", nb_epoch=100,
batch_size=20, path='./feature_extraction/DeepAE/'+aaenum+'/')
'''
############## AAE ##############
aae_model('./feature_extraction/AAE/', AdversarialOptimizerSimultaneous(),
xtrain=x_train, encoded_dim=50,img_dim=x_train.shape[1], nb_epoch=100)
################ Variational Autoencoder ####################
'''
vae_model_single('./feature_extraction/VAE/'+aaenum+'/',x_train.shape[1],
x_train,intermediate_dim=1000,batch_size=20,latent_dim=50,epochs=100)
'''
#index = dataset.iloc[0:20482,0] # this is for valiadtion data
index = file_1.iloc[0:20439,0]
################ load model #########################
weight = load_model('./feature_extraction/AAE/aae_encoder.h5')
encoded_aae=weight.predict(x_train)
sns_plot1=sns.heatmap(encoded_aae, cmap="PiYG", cbar=True)
fig1 = sns_plot1.get_figure()
fig1.savefig("./figures/encoded_data.png", format='png', dpi=500)
weights = []
for layer in weight.layers:
weights.append(layer.get_weights())
d = pd.DataFrame(weights)
print ('Weight layer shape:', d.shape)
intermediate_weight_df = pd.DataFrame(weights[1][0])
'''
# use the following two lines when one hidden layer in autoencoder is used
# assign weight_matrix=abstracted_weight_df
intermediate_weight_df_2 = pd.DataFrame(weights[2][0])
abstracted_weight_df = intermediate_weight_df.dot(intermediate_weight_df_2)
'''
'''
# use the following lines including above two lines when two hidden layers in autoencoder are used
# assign weight_matrix= abstracted_weight_df_2
intermediate_weight_df_3 = pd.DataFrame(weights[3][0])
abstracted_weight_df_2 = abstracted_weight_df.dot(intermediate_weight_df_3)
'''
# assign the weight matrix based on how many layers in autoencoder are used
weight_matrix=intermediate_weight_df
print (weight_matrix)
####################### SAVE DECODED VALUE ##########################
decode = load_model('./feature_extraction/AAE/aae_decoder.h5')
decoded_data=decode.predict(encoded_aae)
dr_file =os.path.join('./feature_extraction/AAE/aae_decoded.tsv')
decoded = pd.DataFrame(decoded_data)
decoded.to_csv(dr_file, sep='\t')
# saving decoded file
# this has no use in this project
'''
sns_plot2=sns.heatmap(decoded_aae, cmap="PiYG", cbar=True)
fig2 = sns_plot2.get_figure()
fig2.savefig("./figures/decoded_data.png", format='png', dpi=500)
'''
'''
sns_plot3=sns.heatmap(weight_matrix, cmap="PiYG", cbar=True)
fig3 = sns_plot3.get_figure()
fig3.savefig("./figures/weight_matrix.png", format='png', dpi=500)
'''
top_gene(weight_matrix=weight_matrix, weight_file='./results/AAE/aae_weight_matrix.tsv',
encoded_matrix=encoded_aae, encoded_file='./results/AAE/aae_encoded.tsv',
gene_file='./results/AAE/aae_sorted_gene.tsv', index=index,
feature_distribution='./results/AAE/aae_weight_distribution.png')