-
Notifications
You must be signed in to change notification settings - Fork 2
/
help.py
185 lines (151 loc) · 6.15 KB
/
help.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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 24 09:54:33 2018
@author: yaoyu
"""
import os
from time import time
from keras.models import Sequential
from keras.layers import Dense, Input, Dropout, Conv1D, Reshape, MaxPooling1D, ZeroPadding1D, AveragePooling1D
from keras.layers.normalization import BatchNormalization
from sklearn.metrics import roc_curve, auc, roc_auc_score,average_precision_score
import numpy as np
from keras.layers import Dense, Dropout, Merge
import utils.tools as utils
from keras.regularizers import l2
from gensim.models import Word2Vec
import copy
import psutil
import random
import h5py
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from keras import backend as K
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.optimizers import SGD
from keras.initializers import glorot_uniform
from keras.layers import Add, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
lr = 0.01
epochs = 50
batch_size = 64
dr = 0.4
l2c = 0.01
def identify_block(X, f, filters, stage, block):
"""
X - 输入的tensor类型数据,维度为(m, n_H_prev, n_W_prev, n_H_prev)
f - kernal大小
filters - 整数列表,定义每一层卷积层过滤器的数量
stage - 整数 定义层位置
block - 字符串 定义层位置
X - 恒等输出,tensor类型,维度(n_H, n_W, n_C)
"""
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
F1, F2, F3 = filters # 定义输出特征的个数
X_shortcut = X #(?,14,256)
X = Conv1D(filters=F1, kernel_size=1, strides=1, padding='valid', name=conv_name_base + '2a',
kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=-1, name=bn_name_base + '2a')(X)
X = Activation('relu')(X)
X = Conv1D(filters=F2, kernel_size=f, strides=1, padding='same', name=conv_name_base + '2b',
kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=-1, name=bn_name_base + '2b')(X)
X = Activation('relu')(X)
X = Conv1D(filters=F3, kernel_size=1, strides=1, padding='valid', name=conv_name_base + '2c',
kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=-1, name=bn_name_base + '2c')(X)
# 没有激活
X = Add()([X, X_shortcut]) #(?,14,256)
X = Activation('relu')(X)
return X
def convolutional_block(X, f, filters, stage, block, s=2):
# 参数意义和上文相同
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
F1, F2, F3 = filters
X_shortcut = X
X = Conv1D(filters=F1, kernel_size=1, strides=s, padding='valid', name=conv_name_base + '2a',
kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=-1, name=bn_name_base + '2a')(X)
X = Activation('relu')(X)#(?,14,64)
X = Conv1D(filters=F2, kernel_size=f, strides=1, padding='same', name=conv_name_base + '2b',
kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=-1, name=bn_name_base + '2b')(X)
X = Activation('relu')(X)
X = Conv1D(filters=F3, kernel_size=1, strides=1, padding='valid', name=conv_name_base + '2c',
kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=-1, name=bn_name_base + '2c')(X)
# shortcut
X_shortcut = Conv1D(filters=F3, kernel_size=1, strides=s, padding='valid', name=conv_name_base + '1',
kernel_initializer=glorot_uniform(seed=0))(X_shortcut)
X_shortcut = BatchNormalization(axis=-1, name=bn_name_base + '1')(X_shortcut)
X = Add()([X, X_shortcut])
X = Activation('relu')(X) #(?, 7, 512)
return X
def token(dataset):
token_dataset = []
for i in range(len(dataset)):
seq = []
for j in range(len(dataset[i])):
seq.append(dataset[i][j])
token_dataset.append(seq)
return token_dataset
def pandding_J(protein,maxlen):
padded_protein = copy.deepcopy(protein)
for i in range(len(padded_protein)):
if len(padded_protein[i])<maxlen:
for j in range(len(padded_protein[i]),maxlen):
padded_protein[i].append('J')
return padded_protein
def averagenum(num):
nsum = 0
for i in range(len(num)):
nsum += num[i]
return nsum / len(num)
def max_min_avg_length(pro_swissProt):
length = []
for i in range(len(pro_swissProt)):
length.append(len(pro_swissProt[i]))
maxNum = max(length) #maxNum = 5
minNum = min(length) #minNum = 1
index_max = length.index(maxNum)
index_min = length.index(minNum)
avg = averagenum(length)
return index_max,index_min,avg
def protein_representation(wv,tokened_seq_protein,maxlen,size):
represented_protein = []
for i in range(len(tokened_seq_protein)):
temp_sentence = []
for j in range(maxlen):
if tokened_seq_protein[i][j]=='J':
temp_sentence.extend(np.zeros(size))
else:
temp_sentence.extend(wv[tokened_seq_protein[i][j]])
represented_protein.append(np.array(temp_sentence))
return represented_protein
def read_traingingData(file_name):
# read sample from a file
seq = []
with open(file_name, 'r') as fp:
i = 0
for line in fp:
seq.append(line.split('\n')[0])
i = i+1
return seq
def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
print("--- new folder... ---")
print("--- OK ---")
else:
print("--- There is this folder! ---")
def getMemorystate():
phymem = psutil.virtual_memory()
line = "Memory: %5s%% %6s/%s"%(phymem.percent,
str(int(phymem.used/1024/1024))+"M",
str(int(phymem.total/1024/1024))+"M")
return line