-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_data.py
165 lines (149 loc) · 8.02 KB
/
load_data.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
import os
import dgl
import torch
import random
import pandas as pd
import numpy as np
from tqdm import tqdm
from torch.utils.data import DataLoader
def load_data(args):
"""Load dataset
Returns
-------
g : dgl.graph
Heterogeneous graph representing the drug-disease network.
feature : dict[node_types, feature_tensors]
Initialized node features of g.
data : np.array
Bags of meta-path instances with a form of [drug_A, drug_B, disease_A, disease_B].
Given a drug d_a(id:0) and a disease d_b(id:1). Its meta-path instances can be:
[[0, 0, 1, 1],
[0, 23, 1, 1],
[0, 0, 145, 1],
[0, 289, 36, 1],
...]
label : np.array
Labels of data.
"""
dataset = args.dataset
k = args.k
drug_drug = pd.read_csv('./dataset/{}/drug_drug.csv'.format(dataset), header=None)
drug_drug_link = topk_filtering(drug_drug.values, k)
disease_disease = pd.read_csv('./dataset/{}/disease_disease.csv'.format(dataset), header=None)
disease_disease_link = topk_filtering(disease_disease.values, k)
drug_disease = pd.read_csv('./dataset/{}/drug_disease.csv'.format(dataset), header=None)
drug_disease_link = np.array(np.where(drug_disease == 1)).T
disease_drug_link = np.array(np.where(drug_disease.T == 1)).T
links = {'drug-drug': drug_drug_link, 'drug-disease': drug_disease_link,
'disease-disease': disease_disease_link}
graph_data = {('drug', 'drug-drug', 'drug'): (torch.tensor(drug_drug_link[:, 0]),
torch.tensor(drug_drug_link[:, 1])),
('drug', 'drug-disease', 'disease'): (torch.tensor(drug_disease_link[:, 0]),
torch.tensor(drug_disease_link[:, 1])),
('disease', 'disease-drug', 'drug'): (torch.tensor(disease_drug_link[:, 0]),
torch.tensor(disease_drug_link[:, 1])),
('disease', 'disease-disease', 'disease'): (torch.tensor(disease_disease_link[:, 0]),
torch.tensor(disease_disease_link[:, 1]))}
g = dgl.heterograph(graph_data)
drug_feature = np.hstack((drug_drug.values, np.zeros(drug_disease.shape)))
dis_feature = np.hstack((np.zeros(drug_disease.T.shape), disease_disease.values))
g.nodes['drug'].data['h'] = torch.from_numpy(drug_feature).to(torch.float32)
g.nodes['disease'].data['h'] = torch.from_numpy(dis_feature).to(torch.float32)
if '{}_temp_{}k'.format(args.dataset, args.k) in os.listdir():
print('Load data and label(It takes time)...')
data = np.load('{}_temp_{}k/data.npy'.format(args.dataset, args.k))
label = np.load('{}_temp_{}k/label.npy'.format(args.dataset, args.k))
else:
os.mkdir('{}_temp_{}k'.format(args.dataset, args.k))
data, label = [], []
print('Generating Meta-Path Instances(It takes time)...')
with tqdm(total=drug_disease.shape[0] * drug_disease.shape[1]) as pbar:
pbar.set_description('Drug {} * Disease {}'.format(drug_disease.shape[0],
drug_disease.shape[1]))
for drug_id in range(drug_disease.shape[0]):
for disease_id in range(drug_disease.shape[1]):
data.append(meta_path_instance(args, drug_id, disease_id, links, k))
label.append(int(drug_disease.iloc[drug_id, disease_id]))
pbar.update(drug_disease.shape[1])
print('Preparing dataset...')
data = np.array(data)
label = np.array(label)
np.save('{}_temp_{}k/data.npy'.format(args.dataset, args.k), data)
np.save('{}_temp_{}k/label.npy'.format(args.dataset, args.k), label)
print('Data prepared !')
return g, data, label
def load_graph(dataset: str, k: int):
"""Construct heterogeneous drug-disease graph for given dataset.
Parameters
----------
dataset : string
The dataset to be used, including 'B-dataset', 'C-dataset' and 'F-dataset'.
k : int
The topk similarities to be binaried.
Returns
-------
g : dgl.graph
Heterogeneous graph representing the drug-disease network.
"""
drug_drug = pd.read_csv('./dataset/{}/drug_drug.csv'.format(dataset), header=None)
drug_drug_link = topk_filtering(drug_drug.values, k)
disease_disease = pd.read_csv('./dataset/{}/disease_disease.csv'.format(dataset), header=None)
disease_disease_link = topk_filtering(disease_disease.values, k)
drug_disease = pd.read_csv('./dataset/{}/drug_disease.csv'.format(dataset), header=None)
drug_disease_link = np.array(np.where(drug_disease == 1)).T
disease_drug_link = np.array(np.where(drug_disease.T == 1)).T
graph_data = {('drug', 'drug-drug', 'drug'): (torch.tensor(drug_drug_link[:, 0]),
torch.tensor(drug_drug_link[:, 1])),
('drug', 'drug-disease', 'disease'): (torch.tensor(drug_disease_link[:, 0]),
torch.tensor(drug_disease_link[:, 1])),
('disease', 'disease-drug', 'drug'): (torch.tensor(disease_drug_link[:, 0]),
torch.tensor(disease_drug_link[:, 1])),
('disease', 'disease-disease', 'disease'): (torch.tensor(disease_disease_link[:, 0]),
torch.tensor(disease_disease_link[:, 1]))}
g = dgl.heterograph(graph_data)
drug_feature = np.hstack((drug_drug.values, np.zeros(drug_disease.shape)))
dis_feature = np.hstack((np.zeros(drug_disease.T.shape), disease_disease.values))
g.nodes['drug'].data['h'] = torch.from_numpy(drug_feature).to(torch.float32)
g.nodes['disease'].data['h'] = torch.from_numpy(dis_feature).to(torch.float32)
return g
def topk_filtering(d_d: np.array, k: int):
"""Convert the Topk similarities to 1 and generate the Topk interactions.
"""
for i in range(len(d_d)):
sorted_idx = np.argpartition(d_d[i], -k - 1)
d_d[i, sorted_idx[-k - 1:-1]] = 1
return np.array(np.where(d_d == 1)).T
def meta_path_instance(args, drug_id: int, disease_id: int, links: dict, k: int):
"""Generate the pseudo meta-path instances.
"""
mpi = [[drug_id, drug_id, disease_id, disease_id]]
mpi.extend([[drug_id, drug, disease_id, disease_id]
for drug in links['drug-drug'][links['drug-drug'][:, 0] == drug_id][:, 1]])
mpi.extend([[drug_id, drug_id, dis, disease_id]
for dis in links['disease-disease'][links['disease-disease'][:, 0] == disease_id][:, 1]])
mpi.extend([[drug_id, drug, dis, disease_id]
for drug in links['drug-drug'][links['drug-drug'][:, 0] == drug_id][:, 1]
for dis in links['disease-disease'][links['disease-disease'][:, 0] == disease_id][:, 1]])
if len(mpi) < k * (k + 2) + 1:
for i in range(k * (k + 2) + 1 - len(mpi)):
random.seed(args.seed)
mpi.append(random.choice(mpi))
elif len(mpi) > k * (k + 2) + 1:
mpi = mpi[:k * (k + 2) + 1]
return mpi
def remove_graph(g, test_drug_id, test_disease_id):
etype = ('drug', 'drug-disease', 'disease')
edges_id = g.edge_ids(torch.tensor(test_drug_id),
torch.tensor(test_disease_id),
etype=etype)
g = dgl.remove_edges(g, edges_id, etype=etype)
etype = ('disease', 'disease-drug', 'drug')
edges_id = g.edge_ids(torch.tensor(test_disease_id),
torch.tensor(test_drug_id),
etype=etype)
g = dgl.remove_edges(g, edges_id, etype=etype)
return g
def get_data_loaders(data, batch_size, shuffle, drop=False):
"""Build data loader for train data and test data.
"""
return DataLoader(data, batch_size=batch_size, shuffle=shuffle, drop_last=drop)