-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
252 lines (194 loc) · 7.08 KB
/
preprocess.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
241
242
243
244
245
246
247
248
249
250
251
252
import torch
from torch_geometric.data import Data
import numpy as np
N_SOURCE_NODES = 35
N_TARGET_NODES = 35
N_SUBJECTS = 67
N_SOURCE_NODES_F = int((N_SOURCE_NODES*(N_SOURCE_NODES-1))/2)
N_TARGET_NODES_F = int((N_TARGET_NODES*(N_TARGET_NODES-1))/2)
def convert_matrices_to_with_features(dataset):
'''
dataset: list of length of N_dataset where each of them has shape
[N_subjects, N_timepoints, N_roi, N_roi]. Also, N_dataset equals to N_views.
return [N_dataset*N_timepoints, N_features]
'''
number_of_dataset = len(dataset)
number_of_timepoints = dataset[0].shape[1]
indices_i, indices_j = np.triu_indices(dataset[0].shape[2], 1)
all_views_and_timepoints = []
for i in range(number_of_dataset):
for t in range(number_of_timepoints):
data = dataset[i][:, t, :, :] # shape is [n_subjects, n_roi, n_roi]
features = data[:, indices_i, indices_j] # shape is [n_subjects, n_features]
all_views_and_timepoints.append(features)
return all_views_and_timepoints
###############################################################
# The later functions are all taken from IMANGraphNet
def convert_vector_to_graph_RH(data):
"""
convert subject vector to adjacency matrix then use it to create a graph
edge_index:
edge_attr:
x:
"""
data.reshape(1, N_SOURCE_NODES_F)
# create adjacency matrix
tri = np.zeros((N_SOURCE_NODES, N_SOURCE_NODES))
tri[np.triu_indices(N_SOURCE_NODES, 1)] = data
tri = tri + tri.T
tri[np.diag_indices(N_SOURCE_NODES)] = 0
edge_attr = torch.Tensor(tri).view(N_SOURCE_NODES**2, 1)
edge_attr = torch.tensor(edge_attr, dtype=torch.float)
counter = 0
pos_counter = 0
neg_counter = 0
N_ROI = N_SOURCE_NODES
pos_edge_index = torch.zeros(2, N_ROI * N_ROI)
neg_edge_indexe = []
# pos_edge_indexe = []
for i in range(N_ROI):
for j in range(N_ROI):
pos_edge_index[:, counter] = torch.tensor([i, j])
counter += 1
# xx = torch.ones(160, 160, dtype=torch.float)
x = torch.tensor(tri, dtype=torch.float)
pos_edge_index = torch.tensor(pos_edge_index, dtype=torch.long)
return Data(x=x, pos_edge_index=pos_edge_index, edge_attr=edge_attr)
def convert_vector_to_graph_HHR(data):
"""
convert subject vector to adjacency matrix then use it to create a graph
edge_index:
edge_attr:
x:
"""
data.reshape(1, 35778)
# create adjacency matrix
tri = np.zeros((268, 268))
tri[np.triu_indices(268, 1)] = data
tri = tri + tri.T
tri[np.diag_indices(268)] = 1
edge_attr = torch.Tensor(tri).view(71824, 1)
edge_attr = torch.tensor(edge_attr, dtype=torch.float)
counter = 0
pos_counter = 0
neg_counter = 0
N_ROI = 268
pos_edge_index = torch.zeros(2, N_ROI * N_ROI)
neg_edge_indexe = []
# pos_edge_indexe = []
for i in range(N_ROI):
for j in range(N_ROI):
pos_edge_index[:, counter] = torch.tensor([i, j])
counter += 1
# xx = torch.ones(268, 268, dtype=torch.float)
x = torch.tensor(tri, dtype=torch.float)
pos_edge_index = torch.tensor(pos_edge_index, dtype=torch.long)
return Data(x=x, pos_edge_index=pos_edge_index, edge_attr=edge_attr)
def convert_vector_to_graph_FC(data):
"""
convert subject vector to adjacency matrix then use it to create a graph
edge_index:
edge_attr:
x:
"""
data.reshape(1, N_TARGET_NODES_F)
# create adjacency matrix
tri = np.zeros((N_TARGET_NODES, N_TARGET_NODES))
tri[np.triu_indices(N_TARGET_NODES, 1)] = data
tri = tri + tri.T
tri[np.diag_indices(N_TARGET_NODES)] = 0
edge_attr = torch.Tensor(tri).view(N_TARGET_NODES**2, 1)
edge_attr = torch.tensor(edge_attr, dtype=torch.float)
counter = 0
pos_counter = 0
neg_counter = 0
N_ROI = N_TARGET_NODES
pos_edge_index = torch.zeros(2, N_ROI * N_ROI)
neg_edge_indexe = []
# pos_edge_indexe = []
for i in range(N_ROI):
for j in range(N_ROI):
pos_edge_index[:, counter] = torch.tensor([i, j])
counter += 1
# xx = torch.ones(160, 160, dtype=torch.float)
x = torch.tensor(tri, dtype=torch.float)
pos_edge_index = torch.tensor(pos_edge_index, dtype=torch.long)
return Data(x=x, pos_edge_index=pos_edge_index, edge_attr=edge_attr)
def cast_data_vector_RH(dataset):
"""
convert subject vectors to graph and append it in a list
"""
dataset_g = []
for subj in range(dataset.shape[0]):
dataset_g.append(convert_vector_to_graph_RH(dataset[subj]))
return dataset_g
def cast_data_vector_HHR(dataset):
"""
convert subject vectors to graph and append it in a list
"""
dataset_g = []
for subj in range(dataset.shape[0]):
dataset_g.append(convert_vector_to_graph_HHR(dataset[subj]))
return dataset_g
def cast_data_vector_FC(dataset):
"""
convert subject vectors to graph and append it in a list
"""
dataset_g = []
for subj in range(dataset.shape[0]):
dataset_g.append(convert_vector_to_graph_FC(dataset[subj]))
return dataset_g
def convert_generated_to_graph_268(data1):
"""
convert generated output from G to a graph
"""
dataset = []
for data in data1:
counter = 0
N_ROI = 268
pos_edge_index = torch.zeros(2, N_ROI * N_ROI, dtype=torch.long)
for i in range(N_ROI):
for j in range(N_ROI):
pos_edge_index[:, counter] = torch.tensor([i, j])
counter += 1
x = data
pos_edge_index = torch.tensor(pos_edge_index, dtype=torch.long)
data = Data(x=x, pos_edge_index= pos_edge_index, edge_attr=data.view(71824, 1))
dataset.append(data)
return dataset
def convert_generated_to_graph(data):
"""
convert generated output from G to a graph
"""
dataset = []
# for data in data1:
counter = 0
N_ROI = N_TARGET_NODES
pos_edge_index = torch.zeros(2, N_ROI * N_ROI, dtype=torch.long)
for i in range(N_ROI):
for j in range(N_ROI):
pos_edge_index[:, counter] = torch.tensor([i, j])
counter += 1
x = data
pos_edge_index = torch.tensor(pos_edge_index, dtype=torch.long)
data = Data(x=x, pos_edge_index= pos_edge_index, edge_attr=data.view(N_TARGET_NODES**2, 1))
dataset.append(data)
return dataset
def convert_generated_to_graph_Al(data1):
"""
convert generated output from G to a graph
"""
dataset = []
# for data in data1:
counter = 0
N_ROI = N_SOURCE_NODES
pos_edge_index = torch.zeros(2, N_ROI * N_ROI, dtype=torch.long)
for i in range(N_ROI):
for j in range(N_ROI):
pos_edge_index[:, counter] = torch.tensor([i, j])
counter += 1
# x = data
pos_edge_index = torch.tensor(pos_edge_index, dtype=torch.long)
data = Data(x=data1, pos_edge_index=pos_edge_index, edge_attr=data1.view(N_SOURCE_NODES*N_SOURCE_NODES, 1))
dataset.append(data)
return dataset