-
Notifications
You must be signed in to change notification settings - Fork 5
/
training_functions.py
144 lines (108 loc) · 4.3 KB
/
training_functions.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
'''
*******************************************************
Some functions that are needed for training process..
*******************************************************
'''
from copy import deepcopy
import numpy as np
import pandas as pd
import scipy
from scipy import stats
import scipy.spatial
from sklearn.preprocessing import MinMaxScaler
import collections
#---------------------------------------------------------------------------------------------
#### ALL needed functions for training process
#---------------------------------------------------------------------------------------------
######### TRANING FUNCTION ###############
def Mask_test_index(test_idx, x, DrTr, drugID, targetID):
DrTr_train = deepcopy(DrTr)
# get the drug index and target index
# mask drug,target = 1 of test data to be 0 (i.e. remove the edge)
for i in test_idx:
dr = x[i,0]
dr_index = drugID[dr]
tr = x[i,1]
tr_index = targetID[tr]
DrTr_train[dr_index, tr_index] = 0
return DrTr_train
##----------------------------------------------------
## normalize simiarities to be in positive range [0,1]
def normalizedMatrix(matrix):
# scaler = MinMaxScaler(copy=True, feature_range=(0, 1))
# scaler.fit(matrix)
# normMat = scaler.transform(matrix)
normMat = (matrix - matrix.min()) / (matrix.max() - matrix.min())
return normMat
#--------------------------------------------------------
def Strongest_k_sim(Mat,K):
m,n = Mat.shape
Ssim = np.zeros((m,n))
for i in range(m):
index = np.argsort(Mat[i,:])[-K:] # sort based on strongest k edges
Ssim[i,index] = Mat[i,index] # keep only the nearest neighbors (strongest k edges)
np.fill_diagonal(Ssim , 1)
return Ssim
#---------------------------------------------------------------------------------------------
## To compute drug-drug FV cosine similarity and target-target FV cosine similarity
def Cosine_Similarity(Matrix):
cos_sim_m = np.zeros((Matrix.shape[0],Matrix.shape[0]))
for item_i in range(Matrix.shape[0]):
for item_j in range(Matrix.shape[0]):
cos_sim_m[item_i][item_j] = 1-(scipy.spatial.distance.cosine(Matrix[item_i,:],Matrix[item_j,:]))
return cos_sim_m
#---------------------------------------------------------------------
## Get Similarity of targets or drugs within specific threshold
def keep_sim_threshold(simMat, threshold):
newMat = np.zeros(simMat.shape)
#print(newMat)
for i,x in enumerate(simMat):
#print(i)
for j,y in enumerate(x):
#print(i,j)
# if y <= threshold:
# simMat[i,j] = 0.0
if y >= threshold:
newMat[i,j] = simMat[i,j]
# print(newMat)
if (np.count_nonzero(newMat[i]) == 0):
col = np.argmax(simMat[i])
# print('col', col)
newMat[i,col] = simMat[i,col]
return newMat
#----------------------------------------------
# Convert weighted edgelist into adjacency matrix
def edgelist_to_adjMat(Wedgelist,ligDic,prDic):
row_inds = []
col_inds = []
adj = np.zeros((len(ligDic.keys()),len(prDic.keys())))
for element in Wedgelist:
i = ligDic[element[0]]
j = prDic[element[1]]
adj[i][j] = element[2]
row_inds.append(i)
col_inds.append(j)
return adj, row_inds,col_inds
#------------------------------------------------
#### Encoding Functions ###################
###########################################
def integer_encoding(s,dic,maxLen):
s_labelE = np.zeros(maxLen)
for i,char in enumerate(s[:maxLen]):
s_labelE[i] = dic[char]
return s_labelE
############################################3
def oneHOT_encoding(s, dic, maxLen):
oneHot_e = np.zeros((maxLen, len(dic)))
for i,char in enumerate(s[:maxLen]):
oneHot_e[i, (dic[char])-1] = 1
return oneHot_e
#--------------------------------
def get_unique_tokens(allSMILES):
all_smiles_char = []
for i in range(len(allSMILES)):
all_smiles_char += str(allSMILES[i])
tokens = sorted(list(set(all_smiles_char)))
num_Tokens = len(tokens)
return tokens, num_Tokens
#--------------------------------------------------------------------