Spaces:
Runtime error
Runtime error
File size: 16,063 Bytes
fce1f4b |
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
#%%
import logging
from symbol import parameters
from textwrap import indent
import os
import tempfile
import sys
from matplotlib import collections
import pandas as pd
import json
from glob import glob
from tqdm import tqdm
import numpy as np
from pprint import pprint
import torch
import pickle as pkl
from collections import Counter
# print(dir(collections))
import networkx as nx
from collections import Counter
import utils
from torch.nn import functional as F
sys.path.append("..")
import Parameters
from DiseaseSpecific.attack import calculate_edge_bound, get_model_loss_without_softmax
#%%
def load_data(file_name):
df = pd.read_csv(file_name, sep='\t', header=None, names=None, dtype=str)
df = df.drop_duplicates()
return df.values
parser = utils.get_argument_parser()
parser.add_argument('--reasonable-rate', type = float, default=0.7, help = 'The added edge\'s existance rank prob greater than this rate')
parser.add_argument('--init-mode', type = str, default='single', help = 'How to select target nodes') # 'single' for case study
parser.add_argument('--added-edge-num', type = str, default = '', help = 'Added edge num')
args = parser.parse_args()
args = utils.set_hyperparams(args)
utils.seed_all(args.seed)
graph_edge_path = '../DiseaseSpecific/processed_data/GNBR/all.txt'
idtomeshid_path = '../DiseaseSpecific/processed_data/GNBR/entities_reverse_dict.json'
model_path = f'../DiseaseSpecific/saved_models/GNBR_{args.model}_128_0.2_0.3_0.3.model'
data_path = '../DiseaseSpecific/processed_data/GNBR'
with open(Parameters.GNBRfile+'original_entity_raw_name', 'rb') as fl:
full_entity_raw_name = pkl.load(fl)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
args.device = device
n_ent, n_rel, ent_to_id, rel_to_id = utils.generate_dicts(data_path)
model = utils.load_model(model_path, args, n_ent, n_rel, args.device)
print(device)
graph_edge = utils.load_data(graph_edge_path)
with open(idtomeshid_path, 'r') as fl:
idtomeshid = json.load(fl)
print(graph_edge.shape, len(idtomeshid))
divide_bound, data_mean, data_std = calculate_edge_bound(graph_edge, model, args.device, n_ent)
print('Defender ...')
print(divide_bound, data_mean, data_std)
meshids = list(idtomeshid.values())
cal = {
'chemical' : 0,
'disease' : 0,
'gene' : 0
}
for meshid in meshids:
cal[meshid.split('_')[0]] += 1
# pprint(cal)
def check_reasonable(s, r, o):
train_trip = np.asarray([[s, r, o]])
train_trip = torch.from_numpy(train_trip.astype('int64')).to(device)
edge_loss = get_model_loss_without_softmax(train_trip, model, device).squeeze()
# edge_losse_log_prob = torch.log(F.softmax(-edge_loss, dim = -1))
edge_loss = edge_loss.item()
edge_loss = (edge_loss - data_mean) / data_std
edge_losses_prob = 1 / ( 1 + np.exp(edge_loss - divide_bound) )
bound = 1 - args.reasonable_rate
return (edge_losses_prob > bound), edge_losses_prob
edgeid_to_edgetype = {}
edgeid_to_reversemask = {}
for k, id_list in Parameters.edge_type_to_id.items():
for iid, mask in zip(id_list, Parameters.reverse_mask[k]):
edgeid_to_edgetype[str(iid)] = k
edgeid_to_reversemask[str(iid)] = mask
reverse_tot = 0
G = nx.DiGraph()
for s, r, o in graph_edge:
assert idtomeshid[s].split('_')[0] == edgeid_to_edgetype[r].split('-')[0]
if edgeid_to_reversemask[r] == 1:
reverse_tot += 1
G.add_edge(int(o), int(s))
else:
G.add_edge(int(s), int(o))
# print(reverse_tot)
print('Edge num:', G.number_of_edges(), 'Node num:', G.number_of_nodes())
pagerank_value_1 = nx.pagerank(G, max_iter = 200, tol=1.0e-7)
#%%
with open(Parameters.UMLSfile+'drug_term', 'rb') as fl:
drug_term = pkl.load(fl)
with open(Parameters.GNBRfile+'entity_raw_name', 'rb') as fl:
entity_raw_name = pkl.load(fl)
drug_meshid = []
for meshid, nm in entity_raw_name.items():
if nm.lower() in drug_term and meshid.split('_')[0] == 'chemical':
drug_meshid.append(meshid)
drug_meshid = set(drug_meshid)
pr = list(pagerank_value_1.items())
pr.sort(key = lambda x: x[1])
sorted_rank = { 'chemical' : [],
'gene' : [],
'disease': [],
'merged' : []}
for iid, score in pr:
tp = idtomeshid[str(iid)].split('_')[0]
if tp == 'chemical':
if idtomeshid[str(iid)] in drug_meshid:
sorted_rank[tp].append((iid, score))
else:
sorted_rank[tp].append((iid, score))
sorted_rank['merged'].append((iid, score))
llen = len(sorted_rank['merged'])
sorted_rank['merged'] = sorted_rank['merged'][llen * 3 // 4 : ]
print(len(sorted_rank['chemical']))
print(len(sorted_rank['gene']), len(sorted_rank['disease']), len(sorted_rank['merged']))
#%%
Target_node_list = []
Attack_edge_list = []
if args.init_mode == '':
if args.added_edge_num != '' and args.added_edge_num != '1':
raise Exception('added_edge_num must be 1 when init_mode=='' ')
for init_p in [0.1, 0.3, 0.5, 0.7, 0.9]:
p = len(sorted_rank['chemical']) * init_p
print('Init p:', init_p)
target_node_list = []
attack_edge_list = []
num_max_eq = 0
mean_rank_of_total_max = 0
for pp in tqdm(range(int(p)-10, int(p)+10)):
target = sorted_rank['chemical'][pp][0]
target_node_list.append(target)
candidate_list = []
score_list = []
loss_list = []
for iid, score in sorted_rank['merged']:
a = G.number_of_edges(iid, target) + 1
if a != 1:
continue
b = G.out_degree(iid) + 1
tp = idtomeshid[str(iid)].split('_')[0]
edge_losses = []
r_list = []
for r in range(len(edgeid_to_edgetype)):
r_tp = edgeid_to_edgetype[str(r)]
if (edgeid_to_reversemask[str(r)] == 0 and r_tp.split('-')[0] == tp and r_tp.split('-')[1] == 'chemical'):
train_trip = np.array([[iid, r, target]])
train_trip = torch.from_numpy(train_trip.astype('int64')).to(device)
edge_loss = get_model_loss_without_softmax(train_trip, model, device).squeeze()
edge_losses.append(edge_loss.unsqueeze(0).detach())
r_list.append(r)
elif(edgeid_to_reversemask[str(r)] == 1 and r_tp.split('-')[0] == 'chemical' and r_tp.split('-')[1] == tp):
train_trip = np.array([[iid, r, target]]) # add batch dim
train_trip = torch.from_numpy(train_trip.astype('int64')).to(device)
edge_loss = get_model_loss_without_softmax(train_trip, model, device).squeeze()
edge_losses.append(edge_loss.unsqueeze(0).detach())
r_list.append(r)
if len(edge_losses)==0:
continue
min_index = torch.argmin(torch.cat(edge_losses, dim = 0))
r = r_list[min_index]
r_tp = edgeid_to_edgetype[str(r)]
if (edgeid_to_reversemask[str(r)] == 0):
bo, prob = check_reasonable(iid, r, target)
if bo:
candidate_list.append((iid, r, target))
score_list.append(score * a / b)
loss_list.append(edge_losses[min_index].item())
if (edgeid_to_reversemask[str(r)] == 1):
bo, prob = check_reasonable(target, r, iid)
if bo:
candidate_list.append((target, r, iid))
score_list.append(score * a / b)
loss_list.append(edge_losses[min_index].item())
if len(candidate_list) == 0:
attack_edge_list.append((-1, -1, -1))
continue
norm_score = np.array(score_list) / np.sum(score_list)
norm_loss = np.exp(-np.array(loss_list)) / np.sum(np.exp(-np.array(loss_list)))
total_score = norm_score * norm_loss
max_index = np.argmax(total_score)
attack_edge_list.append(candidate_list[max_index])
score_max_index = np.argmax(norm_score)
if score_max_index == max_index:
num_max_eq += 1
score_index_list = list(zip(list(range(len(norm_score))), norm_score))
score_index_list.sort(key = lambda x: x[1], reverse = True)
max_index_in_score = score_index_list.index((max_index, norm_score[max_index]))
mean_rank_of_total_max += max_index_in_score / len(norm_score)
print('num_max_eq:', num_max_eq)
print('mean_rank_of_total_max:', mean_rank_of_total_max / 20)
Target_node_list.append(target_node_list)
Attack_edge_list.append(attack_edge_list)
else:
assert args.init_mode == 'random' or args.init_mode == 'single'
print(f'Init mode : {args.init_mode}')
utils.seed_all(args.seed)
if args.init_mode == 'random':
index = np.random.choice(len(sorted_rank['chemical']), 400, replace = False)
else:
# index = [5807, 6314, 5799, 5831, 3954, 5654, 5649, 5624, 2412, 2407]
index = np.random.choice(len(sorted_rank['chemical']), 400, replace = False)
with open(f'../pagerank/results/After_distmult_0.7random10.pkl', 'rb') as fl:
edge = pkl.load(fl)
with open('../pagerank/results/Init_0.7random.pkl', 'rb') as fl:
init = pkl.load(fl)
increase = (np.array(init) - np.array(edge)) / np.array(init)
increase = increase.reshape(-1)
selected_index = np.argsort(increase)[::-1][:10]
# print(selected_index)
# print(increase[selected_index])
# print(np.array(init)[selected_index])
# print(np.array(edge)[selected_index])
index = [index[i] for i in selected_index]
# llen = len(sorted_rank['chemical'])
# index = np.random.choice(range(llen//4, llen), 4, replace = False)
# index = selected_index + list(index)
# for i in index:
# ii = str(sorted_rank['chemical'][i][0])
# nm = entity_raw_name[idtomeshid[ii]]
# nmset = full_entity_raw_name[idtomeshid[ii]]
# print('**'*10)
# print(i)
# print(nm)
# print(nmset)
# raise Exception('stop')
target_node_list = []
attack_edge_list = []
num_max_eq = 0
mean_rank_of_total_max = 0
for pp in tqdm(index):
target = sorted_rank['chemical'][pp][0]
target_node_list.append(target)
print('Target:', entity_raw_name[idtomeshid[str(target)]])
candidate_list = []
score_list = []
loss_list = []
main_dict = {}
for iid, score in sorted_rank['merged']:
a = G.number_of_edges(iid, target) + 1
if a != 1:
continue
b = G.out_degree(iid) + 1
tp = idtomeshid[str(iid)].split('_')[0]
edge_losses = []
r_list = []
for r in range(len(edgeid_to_edgetype)):
r_tp = edgeid_to_edgetype[str(r)]
if (edgeid_to_reversemask[str(r)] == 0 and r_tp.split('-')[0] == tp and r_tp.split('-')[1] == 'chemical'):
train_trip = np.array([[iid, r, target]])
train_trip = torch.from_numpy(train_trip.astype('int64')).to(device)
edge_loss = get_model_loss_without_softmax(train_trip, model, device).squeeze()
edge_losses.append(edge_loss.unsqueeze(0).detach())
r_list.append(r)
elif(edgeid_to_reversemask[str(r)] == 1 and r_tp.split('-')[0] == 'chemical' and r_tp.split('-')[1] == tp):
train_trip = np.array([[iid, r, target]]) # add batch dim
train_trip = torch.from_numpy(train_trip.astype('int64')).to(device)
edge_loss = get_model_loss_without_softmax(train_trip, model, device).squeeze()
edge_losses.append(edge_loss.unsqueeze(0).detach())
r_list.append(r)
if len(edge_losses)==0:
continue
min_index = torch.argmin(torch.cat(edge_losses, dim = 0))
r = r_list[min_index]
r_tp = edgeid_to_edgetype[str(r)]
old_len = len(candidate_list)
if (edgeid_to_reversemask[str(r)] == 0):
bo, prob = check_reasonable(iid, r, target)
if bo:
candidate_list.append((iid, r, target))
score_list.append(score * a / b)
loss_list.append(edge_losses[min_index].item())
if (edgeid_to_reversemask[str(r)] == 1):
bo, prob = check_reasonable(target, r, iid)
if bo:
candidate_list.append((target, r, iid))
score_list.append(score * a / b)
loss_list.append(edge_losses[min_index].item())
if len(candidate_list) != old_len:
if int(iid) in main_iid:
main_dict[iid] = len(candidate_list) - 1
if len(candidate_list) == 0:
if args.added_edge_num == '' or int(args.added_edge_num) == 1:
attack_edge_list.append((-1,-1,-1))
else:
attack_edge_list.append([])
continue
norm_score = np.array(score_list) / np.sum(score_list)
norm_loss = np.exp(-np.array(loss_list)) / np.sum(np.exp(-np.array(loss_list)))
total_score = norm_score * norm_loss
total_score_index = list(zip(range(len(total_score)), total_score))
total_score_index.sort(key = lambda x: x[1], reverse = True)
norm_score_index = np.argsort(norm_score)[::-1]
norm_loss_index = np.argsort(norm_loss)[::-1]
total_index = np.argsort(total_score)[::-1]
assert total_index[0] == total_score_index[0][0]
# find rank of main index
for k, v in main_dict.items():
k = int(k)
index = v
print(f'score rank of {entity_raw_name[idtomeshid[str(k)]]}: ', norm_score_index.tolist().index(index))
print(f'loss rank of {entity_raw_name[idtomeshid[str(k)]]}: ', norm_loss_index.tolist().index(index))
print(f'total rank of {entity_raw_name[idtomeshid[str(k)]]}: ', total_index.tolist().index(index))
max_index = np.argmax(total_score)
assert max_index == total_score_index[0][0]
tmp_add = []
add_num = 1
if args.added_edge_num == '' or int(args.added_edge_num) == 1:
attack_edge_list.append(candidate_list[max_index])
else:
add_num = int(args.added_edge_num)
for i in range(add_num):
tmp_add.append(candidate_list[total_score_index[i][0]])
attack_edge_list.append(tmp_add)
score_max_index = np.argmax(norm_score)
if score_max_index == max_index:
num_max_eq += 1
score_index_list = list(zip(list(range(len(norm_score))), norm_score))
score_index_list.sort(key = lambda x: x[1], reverse = True)
max_index_in_score = score_index_list.index((max_index, norm_score[max_index]))
mean_rank_of_total_max += max_index_in_score / len(norm_score)
print('num_max_eq:', num_max_eq)
print('mean_rank_of_total_max:', mean_rank_of_total_max / 400)
Target_node_list = target_node_list
Attack_edge_list = attack_edge_list
print(np.array(Target_node_list).shape)
print(np.array(Attack_edge_list).shape)
# with open(f'processed_data/target_{args.reasonable_rate}{args.init_mode}.pkl', 'wb') as fl:
# pkl.dump(Target_node_list, fl)
# with open(f'processed_data/attack_edge_{args.model}_{args.reasonable_rate}{args.init_mode}{args.added_edge_num}.pkl', 'wb') as fl:
# pkl.dump(Attack_edge_list, fl) |