-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_path.py
241 lines (171 loc) · 8.51 KB
/
find_path.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
from graph_embeddings import Embeddings
import numpy as np
import pandas as pd
from scipy import spatial
from scipy.spatial import distance
from collections import defaultdict
#Go from label to entity_uri (for PKL original labels file) or Label to Idenifier (for microbiome PKL)
# kg_type adds functionality for kg-covid19
def get_uri(labels,value, kg_type):
if kg_type == 'pkl':
uri = labels.loc[labels['label'] == value,'entity_uri'].values[0]
if kg_type == 'kg-covid19':
uri = labels.loc[labels['label'] == value,'id'].values[0]
return uri
def get_label(labels,value,kg_type):
if kg_type == 'pkl':
label = labels.loc[labels['entity_uri'] == value,'label'].values[0]
if kg_type == 'kg-covid19':
label = labels.loc[labels['id'] == value,'label'].values[0]
return label
def get_key(dictionary,value):
for key, val in dictionary.items():
if val == value:
return key
def define_path_triples(g_nodes,triples_df,path_nodes,search_type):
#Dict to store all dataframes of shortest mechanisms for this node pair
mechanism_dfs = {}
#Keep track of # of mechanisms generated for this node pair in file name for all shortest paths
count = 1
#When there is no connection in graph, path_nodes will equal 1 ([[]])
if len(path_nodes[0]) != 0:
for p in range(len(path_nodes)):
#Dataframe to append each triple to
full_df = pd.DataFrame()
n1 = g_nodes[path_nodes[p][0]]
for i in range(1,len(path_nodes[p])):
n2 = g_nodes[path_nodes[p][i]]
if search_type.lower() == 'all':
#Try first direction which is n1 --> n2
df = triples_df.loc[(triples_df['subject'] == n1) & (triples_df['object'] == n2)]
full_df = pd.concat([full_df,df])
if len(df) == 0:
#If no results, try second direction which is n2 --> n1
df = triples_df.loc[(triples_df['object'] == n1) & (triples_df['subject'] == n2)]
full_df = pd.concat([full_df,df])
elif search_type.lower() == 'out':
#Only try direction n1 --> n2
df = triples_df.loc[(triples_df['subject'] == n1) & (triples_df['object'] == n2)]
full_df = pd.concat([full_df,df])
full_df = full_df.reset_index(drop=True)
n1 = n2
#For all shortest path search
if len(path_nodes) > 1:
#Generate df
full_df.columns = ['S','P','O']
mechanism_dfs['mech#_'+str(count)] = full_df
count += 1
#For shortest path search
if len(path_nodes) == 1:
#Generate df
full_df.columns = ['S','P','O']
return full_df
#Return dictionary if all shortest paths search
if len(path_nodes) > 1:
return mechanism_dfs
def find_all_shortest_paths(start_node,end_node,graph,g_nodes,labels_all,triples_df,weights,search_type, kg_type):
node1 = get_uri(labels_all,start_node, kg_type)
node2 = get_uri(labels_all,end_node, kg_type)
#Add weights if specified
if weights:
w = graph.es["weight"]
else:
w = None
#Dict to store all dataframes of shortest mechanisms for this node pair
mechanism_dfs = {}
#list of nodes
path_nodes = graph.get_all_shortest_paths(v=node1, to=node2, weights=w, mode=search_type)
#Remove duplicates for bidirectional nodes, only matters when search type=all for mode
path_nodes = list(set(tuple(x) for x in path_nodes))
path_nodes = [list(tup) for tup in path_nodes]
#Dictionary of all triples that are shortest paths, not currently used
mechanism_dfs = define_path_triples(g_nodes,triples_df,path_nodes,search_type)
return path_nodes
def get_embedding(emb,node):
embedding_array = emb[str(node)]
embedding_array = np.array(embedding_array)
return embedding_array
def calc_cosine_sim(emb,path_nodes,g_nodes,triples_df,search_type,labels_all,kg_type):
target_emb = get_embedding(emb,path_nodes[0][len(path_nodes[0])-1])
#Dict of all embeddings to reuse if they exist
embeddings = defaultdict(list)
#List of total cosine similarity for each path in path_nodes, should be same length as path_nodes
paths_total_cs = []
for l in path_nodes:
cs = 0
for i in range(0,len(l)-1):
if l[i] not in list(embeddings.keys()):
e = get_embedding(emb,l[i])
embeddings[l[i]] = e
else:
e = embeddings[l[i]]
cs += 1 - spatial.distance.cosine(e,target_emb)
paths_total_cs.append(cs)
chosen_path_nodes_cs = select_path(paths_total_cs,path_nodes)
#Will only return 1 dataframe
df = define_path_triples(g_nodes,triples_df,chosen_path_nodes_cs,search_type)
df = convert_to_labels(df,labels_all,kg_type)
return df,paths_total_cs
def calc_pdp(path_nodes,graph,w,g_nodes,triples_df,search_type,labels_all,kg_type):
#List of pdp for each path in path_nodes, should be same length as path_nodes
paths_pdp = []
for l in path_nodes:
pdp = 1
for i in range(0,len(l)-1):
dp = graph.degree(l[i],mode='all',loops=True)
dp_damped = pow(dp,-w)
pdp = pdp*dp_damped
paths_pdp.append(pdp)
chosen_path_nodes_pdp = select_path(paths_pdp,path_nodes)
#Will only return 1 dataframe
df = define_path_triples(g_nodes,triples_df,chosen_path_nodes_pdp,search_type)
df = convert_to_labels(df,labels_all,kg_type)
return df,paths_pdp
def select_path(value_list,path_nodes):
#Get max cs from total_cs_path, use that idx of path_nodes then create mechanism
max_index = value_list.index(max(value_list))
#Must be list of lists for define_path_triples function
chosen_path_nodes = [path_nodes[max_index]]
return chosen_path_nodes
def convert_to_labels(df,labels_all,kg_type):
if kg_type == 'pkl':
for i in range(len(df)):
df.iloc[i].loc['S'] = labels_all.loc[labels_all['entity_uri'] == df.iloc[i].loc['S'],'label'].values[0]
df.iloc[i].loc['P'] = labels_all.loc[labels_all['entity_uri'] == df.iloc[i].loc['P'],'label'].values[0]
df.iloc[i].loc['O'] = labels_all.loc[labels_all['entity_uri'] == df.iloc[i].loc['O'],'label'].values[0]
if kg_type == 'kg-covid19':
for i in range(len(df)):
s_label = labels_all.loc[labels_all['id'] == df.iloc[i].loc['S'],'label'].values[0]
if s_label != "":
df.iloc[i].loc['S'] = s_label
df.iloc[i].loc['P'] = df.iloc[i].loc['P'].split(':')[-1]
o_label = labels_all.loc[labels_all['id'] == df.iloc[i].loc['O'],'label'].values[0]
if o_label != "":
df.iloc[i].loc['O'] = o_label
df = df.reset_index(drop=True)
return df
# Wrapper functions
#Returns the path as a dataframe of S/P/O of all triples' labels within the path
def find_shortest_path(start_node,end_node,graph,g_nodes,labels_all,triples_df,weights,search_type, kg_type):
node1 = get_uri(labels_all,start_node,kg_type)
node2 = get_uri(labels_all,end_node,kg_type)
#Add weights if specified
if weights:
w = graph.es["weight"]
else:
w = None
#list of nodes
path_nodes = graph.get_shortest_paths(v=node1, to=node2, weights=w, mode=search_type)
df = define_path_triples(g_nodes,triples_df,path_nodes,search_type)
df = convert_to_labels(df,labels_all,kg_type)
return df
def prioritize_path_cs(start_node,end_node,graph,g_nodes,labels_all,triples_df,weights,search_type,triples_file,output_dir,input_dir,embedding_dimensions, kg_type):
path_nodes = find_all_shortest_paths(start_node,end_node,graph,g_nodes,labels_all,triples_df,False,'all', kg_type)
e = Embeddings(triples_file,output_dir,input_dir,embedding_dimensions, kg_type)
emb = e.generate_graph_embeddings()
df,paths_total_cs = calc_cosine_sim(emb,path_nodes,g_nodes,triples_df,search_type,labels_all, kg_type)
return path_nodes,df,paths_total_cs
def prioritize_path_pdp(start_node,end_node,graph,g_nodes,labels_all,triples_df,weights,search_type,pdp_weight, kg_type):
path_nodes = find_all_shortest_paths(start_node,end_node,graph,g_nodes,labels_all,triples_df,False,'all', kg_type)
df,paths_pdp = calc_pdp(path_nodes,graph,pdp_weight,g_nodes,triples_df,search_type,labels_all, kg_type)
return path_nodes,df,paths_pdp